blob: 48f8192094c80375027c954354dd2bd0a5d28636 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the code that handles AST -> LLVM type lowering.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenTypes.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000016#include "clang/AST/DeclObjC.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000019#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/DerivedTypes.h"
Anders Carlsson4e533282007-08-17 22:00:32 +000021#include "llvm/Module.h"
Devang Pateld9e9ede2007-10-31 20:08:22 +000022#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000024#include "CGCall.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000025#include "CGRecordLayoutBuilder.h"
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000026
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28using namespace CodeGen;
29
Devang Patel7a4718e2007-10-31 20:01:01 +000030CodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M,
31 const llvm::TargetData &TD)
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000032 : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD),
33 TheABIInfo(0) {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000034}
Reid Spencer5f016e22007-07-11 17:01:13 +000035
Devang Patelb84a06e2007-10-23 02:10:49 +000036CodeGenTypes::~CodeGenTypes() {
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000037 for(llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
Devang Patel88a981b2007-11-01 19:11:01 +000038 I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
Devang Patelb84a06e2007-10-23 02:10:49 +000039 I != E; ++I)
40 delete I->second;
Devang Patel88a981b2007-11-01 19:11:01 +000041 CGRecordLayouts.clear();
Devang Patelb84a06e2007-10-23 02:10:49 +000042}
43
Reid Spencer5f016e22007-07-11 17:01:13 +000044/// ConvertType - Convert the specified type to its LLVM form.
45const llvm::Type *CodeGenTypes::ConvertType(QualType T) {
Chris Lattnerfce71b82008-04-03 05:50:42 +000046 llvm::PATypeHolder Result = ConvertTypeRecursive(T);
47
48 // Any pointers that were converted defered evaluation of their pointee type,
49 // creating an opaque type instead. This is in order to avoid problems with
50 // circular types. Loop through all these defered pointees, if any, and
51 // resolve them now.
52 while (!PointersToResolve.empty()) {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000053 std::pair<QualType, llvm::OpaqueType*> P =
Chris Lattnerfce71b82008-04-03 05:50:42 +000054 PointersToResolve.back();
55 PointersToResolve.pop_back();
56 // We can handle bare pointers here because we know that the only pointers
57 // to the Opaque type are P.second and from other types. Refining the
58 // opqaue type away will invalidate P.second, but we don't mind :).
Eli Friedman57a84fb2009-03-03 04:48:01 +000059 const llvm::Type *NT = ConvertTypeForMemRecursive(P.first);
Chris Lattnerfce71b82008-04-03 05:50:42 +000060 P.second->refineAbstractTypeTo(NT);
61 }
62
63 return Result;
64}
65
66const llvm::Type *CodeGenTypes::ConvertTypeRecursive(QualType T) {
Chris Lattner09dc6662009-04-01 02:00:48 +000067 T = Context.getCanonicalType(T);
Chris Lattner96196622008-07-26 22:37:01 +000068
Devang Patel30ec9972007-10-25 18:32:36 +000069 // See if type is already cached.
Devang Patel0ffe89a2007-10-30 20:46:47 +000070 llvm::DenseMap<Type *, llvm::PATypeHolder>::iterator
Chris Lattner96196622008-07-26 22:37:01 +000071 I = TypeCache.find(T.getTypePtr());
Devang Patel3c400852007-12-21 19:35:28 +000072 // If type is found in map and this is not a definition for a opaque
Chris Lattnerfae6e292008-02-06 05:29:46 +000073 // place holder type then use it. Otherwise, convert type T.
Chris Lattner4581fff2008-02-06 05:21:55 +000074 if (I != TypeCache.end())
Devang Patel47c87b42007-10-30 23:22:14 +000075 return I->second.get();
Devang Patel30ec9972007-10-25 18:32:36 +000076
77 const llvm::Type *ResultType = ConvertNewType(T);
Chris Lattner96196622008-07-26 22:37:01 +000078 TypeCache.insert(std::make_pair(T.getTypePtr(),
Chris Lattner4581fff2008-02-06 05:21:55 +000079 llvm::PATypeHolder(ResultType)));
Devang Patel30ec9972007-10-25 18:32:36 +000080 return ResultType;
81}
82
Eli Friedman57a84fb2009-03-03 04:48:01 +000083const llvm::Type *CodeGenTypes::ConvertTypeForMemRecursive(QualType T) {
84 const llvm::Type *ResultType = ConvertTypeRecursive(T);
Owen Anderson0032b272009-08-13 21:57:51 +000085 if (ResultType == llvm::Type::getInt1Ty(getLLVMContext()))
86 return llvm::IntegerType::get(getLLVMContext(),
87 (unsigned)Context.getTypeSize(T));
Eli Friedman57a84fb2009-03-03 04:48:01 +000088 return ResultType;
89}
90
Chris Lattner4581fff2008-02-06 05:21:55 +000091/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
92/// ConvertType in that it is used to convert to the memory representation for
93/// a type. For example, the scalar representation for _Bool is i1, but the
94/// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +000095const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
96 const llvm::Type *R = ConvertType(T);
97
98 // If this is a non-bool type, don't map it.
Owen Anderson0032b272009-08-13 21:57:51 +000099 if (R != llvm::Type::getInt1Ty(getLLVMContext()))
Chris Lattner19009e62008-01-09 18:47:25 +0000100 return R;
101
102 // Otherwise, return an integer of the target-specified size.
Owen Anderson0032b272009-08-13 21:57:51 +0000103 return llvm::IntegerType::get(getLLVMContext(),
104 (unsigned)Context.getTypeSize(T));
Chris Lattner19009e62008-01-09 18:47:25 +0000105
106}
107
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000108// Code to verify a given function type is complete, i.e. the return type
109// and all of the argument types are complete.
110static const TagType *VerifyFuncTypeComplete(const Type* T) {
111 const FunctionType *FT = cast<FunctionType>(T);
Ted Kremenek6217b802009-07-29 21:53:49 +0000112 if (const TagType* TT = FT->getResultType()->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000113 if (!TT->getDecl()->isDefinition())
114 return TT;
115 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T))
116 for (unsigned i = 0; i < FPT->getNumArgs(); i++)
Ted Kremenek6217b802009-07-29 21:53:49 +0000117 if (const TagType* TT = FPT->getArgType(i)->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000118 if (!TT->getDecl()->isDefinition())
119 return TT;
120 return 0;
121}
122
Chris Lattnerc5b88062008-02-06 05:08:19 +0000123/// UpdateCompletedType - When we find the full definition for a TagDecl,
124/// replace the 'opaque' type we previously made for it if applicable.
125void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
Mike Stumpe607ed02009-08-07 18:05:12 +0000126 const Type *Key = Context.getTagDeclType(TD).getTypePtr();
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000127 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
128 TagDeclTypes.find(Key);
Chris Lattner6ef58e32008-02-06 05:12:09 +0000129 if (TDTI == TagDeclTypes.end()) return;
130
131 // Remember the opaque LLVM type for this tagdecl.
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000132 llvm::PATypeHolder OpaqueHolder = TDTI->second;
133 assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) &&
Chris Lattner6ef58e32008-02-06 05:12:09 +0000134 "Updating compilation of an already non-opaque type?");
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000135
136 // Remove it from TagDeclTypes so that it will be regenerated.
137 TagDeclTypes.erase(TDTI);
138
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000139 // Generate the new type.
140 const llvm::Type *NT = ConvertTagDeclType(TD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000141
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000142 // Refine the old opaque type to its new definition.
143 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT);
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000144
145 // Since we just completed a tag type, check to see if any function types
146 // were completed along with the tag type.
147 // FIXME: This is very inefficient; if we track which function types depend
148 // on which tag types, though, it should be reasonably efficient.
149 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator i;
150 for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) {
151 if (const TagType* TT = VerifyFuncTypeComplete(i->first)) {
152 // This function type still depends on an incomplete tag type; make sure
153 // that tag type has an associated opaque type.
154 ConvertTagDeclType(TT->getDecl());
155 } else {
156 // This function no longer depends on an incomplete tag type; create the
157 // function type, and refine the opaque type to the new function type.
158 llvm::PATypeHolder OpaqueHolder = i->second;
159 const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0));
160 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT);
161 FunctionTypes.erase(i);
162 }
163 }
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000164}
165
Owen Anderson0032b272009-08-13 21:57:51 +0000166static const llvm::Type* getTypeForFormat(llvm::LLVMContext &VMContext,
167 const llvm::fltSemantics &format) {
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000168 if (&format == &llvm::APFloat::IEEEsingle)
Owen Anderson0032b272009-08-13 21:57:51 +0000169 return llvm::Type::getFloatTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000170 if (&format == &llvm::APFloat::IEEEdouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000171 return llvm::Type::getDoubleTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000172 if (&format == &llvm::APFloat::IEEEquad)
Owen Anderson0032b272009-08-13 21:57:51 +0000173 return llvm::Type::getFP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000174 if (&format == &llvm::APFloat::PPCDoubleDouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000175 return llvm::Type::getPPC_FP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000176 if (&format == &llvm::APFloat::x87DoubleExtended)
Owen Anderson0032b272009-08-13 21:57:51 +0000177 return llvm::Type::getX86_FP80Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000178 assert(0 && "Unknown float format!");
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000179 return 0;
180}
181
Devang Patel30ec9972007-10-25 18:32:36 +0000182const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
Chris Lattner96196622008-07-26 22:37:01 +0000183 const clang::Type &Ty = *Context.getCanonicalType(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184
185 switch (Ty.getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000186#define TYPE(Class, Base)
187#define ABSTRACT_TYPE(Class, Base)
188#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
189#define DEPENDENT_TYPE(Class, Base) case Type::Class:
190#include "clang/AST/TypeNodes.def"
191 assert(false && "Non-canonical or dependent types aren't possible.");
192 break;
193
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 case Type::Builtin: {
195 switch (cast<BuiltinType>(Ty).getKind()) {
Argyrios Kyrtzidisafef76e2008-08-09 22:01:55 +0000196 default: assert(0 && "Unknown builtin type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 case BuiltinType::Void:
Steve Naroffde2e22d2009-07-15 18:40:39 +0000198 case BuiltinType::ObjCId:
199 case BuiltinType::ObjCClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 // LLVM void type can only be used as the result of a function call. Just
201 // map to the same as char.
Owen Anderson0032b272009-08-13 21:57:51 +0000202 return llvm::IntegerType::get(getLLVMContext(), 8);
Reid Spencer5f016e22007-07-11 17:01:13 +0000203
204 case BuiltinType::Bool:
Chris Lattner19009e62008-01-09 18:47:25 +0000205 // Note that we always return bool as i1 for use as a scalar type.
Owen Anderson0032b272009-08-13 21:57:51 +0000206 return llvm::Type::getInt1Ty(getLLVMContext());
Reid Spencer5f016e22007-07-11 17:01:13 +0000207
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000208 case BuiltinType::Char_S:
209 case BuiltinType::Char_U:
210 case BuiltinType::SChar:
211 case BuiltinType::UChar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 case BuiltinType::Short:
213 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 case BuiltinType::Int:
215 case BuiltinType::UInt:
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 case BuiltinType::Long:
217 case BuiltinType::ULong:
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 case BuiltinType::LongLong:
219 case BuiltinType::ULongLong:
Argyrios Kyrtzidisafef76e2008-08-09 22:01:55 +0000220 case BuiltinType::WChar:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000221 case BuiltinType::Char16:
222 case BuiltinType::Char32:
Owen Anderson0032b272009-08-13 21:57:51 +0000223 return llvm::IntegerType::get(getLLVMContext(),
Chris Lattner98be4942008-03-05 18:54:05 +0000224 static_cast<unsigned>(Context.getTypeSize(T)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000226 case BuiltinType::Float:
Nate Begemanc8b12272008-04-18 05:41:31 +0000227 case BuiltinType::Double:
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 case BuiltinType::LongDouble:
Owen Anderson0032b272009-08-13 21:57:51 +0000229 return getTypeForFormat(getLLVMContext(),
230 Context.getFloatTypeSemantics(T));
Chris Lattner2df9ced2009-04-30 02:43:43 +0000231
232 case BuiltinType::UInt128:
233 case BuiltinType::Int128:
Owen Anderson0032b272009-08-13 21:57:51 +0000234 return llvm::IntegerType::get(getLLVMContext(), 128);
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 }
236 break;
237 }
Eli Friedmanf98aba32009-02-13 02:31:07 +0000238 case Type::FixedWidthInt:
Owen Anderson0032b272009-08-13 21:57:51 +0000239 return llvm::IntegerType::get(getLLVMContext(),
240 cast<FixedWidthIntType>(T)->getWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 case Type::Complex: {
Chris Lattner572cf092008-03-19 05:24:56 +0000242 const llvm::Type *EltTy =
Chris Lattnerfce71b82008-04-03 05:50:42 +0000243 ConvertTypeRecursive(cast<ComplexType>(Ty).getElementType());
Owen Anderson47a434f2009-08-05 23:18:46 +0000244 return llvm::StructType::get(TheModule.getContext(), EltTy, EltTy, NULL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000246 case Type::LValueReference:
247 case Type::RValueReference: {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000248 const ReferenceType &RTy = cast<ReferenceType>(Ty);
249 QualType ETy = RTy.getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000250 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000251 PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
252 return llvm::PointerType::get(PointeeType, ETy.getAddressSpace());
253 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 case Type::Pointer: {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000255 const PointerType &PTy = cast<PointerType>(Ty);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000256 QualType ETy = PTy.getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000257 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000258 PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
Chris Lattnerfce71b82008-04-03 05:50:42 +0000259 return llvm::PointerType::get(PointeeType, ETy.getAddressSpace());
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000261
Steve Narofffb22d962007-08-30 01:06:46 +0000262 case Type::VariableArray: {
263 const VariableArrayType &A = cast<VariableArrayType>(Ty);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000264 assert(A.getIndexTypeQualifier() == 0 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 "FIXME: We only handle trivial array types so far!");
Eli Friedmanc5773c42008-02-15 18:16:39 +0000266 // VLAs resolve to the innermost element type; this matches
267 // the return of alloca, and there isn't any obviously better choice.
Eli Friedman57a84fb2009-03-03 04:48:01 +0000268 return ConvertTypeForMemRecursive(A.getElementType());
Eli Friedmanc5773c42008-02-15 18:16:39 +0000269 }
270 case Type::IncompleteArray: {
271 const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty);
272 assert(A.getIndexTypeQualifier() == 0 &&
273 "FIXME: We only handle trivial array types so far!");
274 // int X[] -> [0 x int]
Eli Friedman57a84fb2009-03-03 04:48:01 +0000275 return llvm::ArrayType::get(ConvertTypeForMemRecursive(A.getElementType()), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 }
Steve Narofffb22d962007-08-30 01:06:46 +0000277 case Type::ConstantArray: {
278 const ConstantArrayType &A = cast<ConstantArrayType>(Ty);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000279 const llvm::Type *EltTy = ConvertTypeForMemRecursive(A.getElementType());
Steve Narofffb22d962007-08-30 01:06:46 +0000280 return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue());
281 }
Nate Begeman213541a2008-04-18 23:10:10 +0000282 case Type::ExtVector:
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 case Type::Vector: {
284 const VectorType &VT = cast<VectorType>(Ty);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000285 return llvm::VectorType::get(ConvertTypeRecursive(VT.getElementType()),
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 VT.getNumElements());
287 }
288 case Type::FunctionNoProto:
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000289 case Type::FunctionProto: {
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000290 // First, check whether we can build the full function type.
291 if (const TagType* TT = VerifyFuncTypeComplete(&Ty)) {
292 // This function's type depends on an incomplete tag type; make sure
293 // we have an opaque type corresponding to the tag type.
294 ConvertTagDeclType(TT->getDecl());
295 // Create an opaque type for this function type, save it, and return it.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000296 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000297 FunctionTypes.insert(std::make_pair(&Ty, ResultType));
298 return ResultType;
299 }
300 // The function type can be built; call the appropriate routines to
301 // build it.
Chris Lattner9a1a9c42009-03-31 08:55:07 +0000302 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(&Ty))
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000303 return GetFunctionType(getFunctionInfo(FPT), FPT->isVariadic());
Chris Lattner9a1a9c42009-03-31 08:55:07 +0000304
305 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(&Ty);
306 return GetFunctionType(getFunctionInfo(FNPT), true);
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000307 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000308
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000309 case Type::ExtQual:
Chris Lattnerfce71b82008-04-03 05:50:42 +0000310 return
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000311 ConvertTypeRecursive(QualType(cast<ExtQualType>(Ty).getBaseType(), 0));
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000312
Chris Lattner391d77a2008-03-30 23:03:07 +0000313 case Type::ObjCInterface: {
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000314 // Objective-C interfaces are always opaque (outside of the
315 // runtime, which can do whatever it likes); we never refine
316 // these.
317 const llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(&Ty)];
318 if (!T)
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000319 T = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000320 return T;
Chris Lattner391d77a2008-03-30 23:03:07 +0000321 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000322
Steve Naroff14108da2009-07-10 23:34:53 +0000323 case Type::ObjCObjectPointer: {
Daniel Dunbar28e47802009-07-11 21:12:14 +0000324 // Protocol qualifications do not influence the LLVM type, we just return a
325 // pointer to the underlying interface type. We don't need to worry about
326 // recursive conversion.
327 const llvm::Type *T =
328 ConvertTypeRecursive(cast<ObjCObjectPointerType>(Ty).getPointeeType());
329 return llvm::PointerType::getUnqual(T);
Steve Naroff14108da2009-07-10 23:34:53 +0000330 }
Daniel Dunbar28e47802009-07-11 21:12:14 +0000331
Douglas Gregor72564e72009-02-26 23:50:07 +0000332 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000333 case Type::Enum: {
Chris Lattnerde0efb32008-02-06 05:48:29 +0000334 const TagDecl *TD = cast<TagType>(Ty).getDecl();
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000335 const llvm::Type *Res = ConvertTagDeclType(TD);
Chris Lattnerde0efb32008-02-06 05:48:29 +0000336
337 std::string TypeName(TD->getKindName());
338 TypeName += '.';
339
340 // Name the codegen type after the typedef name
341 // if there is no tag type name available
342 if (TD->getIdentifier())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000343 TypeName += TD->getNameAsString();
Chris Lattnerde0efb32008-02-06 05:48:29 +0000344 else if (const TypedefType *TdT = dyn_cast<TypedefType>(T))
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000345 TypeName += TdT->getDecl()->getNameAsString();
Chris Lattnerde0efb32008-02-06 05:48:29 +0000346 else
347 TypeName += "anon";
348
349 TheModule.addTypeName(TypeName, Res);
350 return Res;
351 }
Daniel Dunbar90488912008-08-28 18:02:04 +0000352
353 case Type::BlockPointer: {
Daniel Dunbar4e174f12009-01-09 02:48:46 +0000354 const QualType FTy = cast<BlockPointerType>(Ty).getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000355 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Fariborz Jahanian209bb432009-03-13 20:36:41 +0000356 PointersToResolve.push_back(std::make_pair(FTy, PointeeType));
357 return llvm::PointerType::get(PointeeType, FTy.getAddressSpace());
Daniel Dunbar90488912008-08-28 18:02:04 +0000358 }
Sebastian Redl424c51d2009-01-25 13:35:30 +0000359
Anders Carlsson0e650012009-05-17 17:41:20 +0000360 case Type::MemberPointer: {
361 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
362 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
363 // If we ever want to support other ABIs this needs to be abstracted.
364
365 QualType ETy = cast<MemberPointerType>(Ty).getPointeeType();
366 if (ETy->isFunctionType()) {
Owen Anderson47a434f2009-08-05 23:18:46 +0000367 return llvm::StructType::get(TheModule.getContext(),
368 ConvertType(Context.getPointerDiffType()),
Anders Carlsson0e650012009-05-17 17:41:20 +0000369 ConvertType(Context.getPointerDiffType()),
370 NULL);
371 } else
372 return ConvertType(Context.getPointerDiffType());
373 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000374
375 case Type::TemplateSpecialization:
376 assert(false && "Dependent types can't get here");
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 }
378
379 // FIXME: implement.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000380 return llvm::OpaqueType::get(getLLVMContext());
Reid Spencer5f016e22007-07-11 17:01:13 +0000381}
382
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000383/// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
384/// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000385const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000386
387 // FIXME. This may have to move to a better place.
388 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) {
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000389 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
390 e = RD->bases_end(); i != e; ++i) {
391 if (!i->isVirtual()) {
392 const CXXRecordDecl *Base =
Ted Kremenek6217b802009-07-29 21:53:49 +0000393 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000394 ConvertTagDeclType(Base);
395 }
396 }
397 }
398
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000399 // TagDecl's are not necessarily unique, instead use the (clang)
400 // type connected to the decl.
401 const Type *Key =
Mike Stumpe607ed02009-08-07 18:05:12 +0000402 Context.getTagDeclType(TD).getTypePtr();
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000403 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
404 TagDeclTypes.find(Key);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000405
Chris Lattner5de00fc2008-02-06 06:03:51 +0000406 // If we've already compiled this tag type, use the previous definition.
407 if (TDTI != TagDeclTypes.end())
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000408 return TDTI->second;
409
Chris Lattner5de00fc2008-02-06 06:03:51 +0000410 // If this is still a forward definition, just define an opaque type to use
411 // for this tagged decl.
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000412 if (!TD->isDefinition()) {
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000413 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000414 TagDeclTypes.insert(std::make_pair(Key, ResultType));
Chris Lattner5de00fc2008-02-06 06:03:51 +0000415 return ResultType;
416 }
417
418 // Okay, this is a definition of a type. Compile the implementation now.
419
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000420 if (TD->isEnum()) {
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000421 // Don't bother storing enums in TagDeclTypes.
Chris Lattnerfce71b82008-04-03 05:50:42 +0000422 return ConvertTypeRecursive(cast<EnumDecl>(TD)->getIntegerType());
Chris Lattner5de00fc2008-02-06 06:03:51 +0000423 }
424
425 // This decl could well be recursive. In this case, insert an opaque
426 // definition of this type, which the recursive uses will get. We will then
427 // refine this opaque version later.
428
429 // Create new OpaqueType now for later use in case this is a recursive
430 // type. This will later be refined to the actual type.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000431 llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000432 TagDeclTypes.insert(std::make_pair(Key, ResultHolder));
Chris Lattner5de00fc2008-02-06 06:03:51 +0000433
434 const llvm::Type *ResultType;
435 const RecordDecl *RD = cast<const RecordDecl>(TD);
Daniel Dunbarae287232009-04-22 08:50:59 +0000436
Anders Carlsson696798f2009-07-27 17:10:54 +0000437 // Layout fields.
438 CGRecordLayout *Layout =
439 CGRecordLayoutBuilder::ComputeLayout(*this, RD);
440
Anders Carlsson696798f2009-07-27 17:10:54 +0000441 CGRecordLayouts[Key] = Layout;
442 ResultType = Layout->getLLVMType();
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000443
Chris Lattner5de00fc2008-02-06 06:03:51 +0000444 // Refine our Opaque type to ResultType. This can invalidate ResultType, so
445 // make sure to read the result out of the holder.
446 cast<llvm::OpaqueType>(ResultHolder.get())
447 ->refineAbstractTypeTo(ResultType);
448
449 return ResultHolder.get();
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000450}
451
Devang Patelb84a06e2007-10-23 02:10:49 +0000452/// getLLVMFieldNo - Return llvm::StructType element number
453/// that corresponds to the field FD.
454unsigned CodeGenTypes::getLLVMFieldNo(const FieldDecl *FD) {
Anders Carlsson8330cee2009-07-23 17:01:21 +0000455 assert(!FD->isBitField() && "Don't use getLLVMFieldNo on bit fields!");
456
Chris Lattnerce5605e2008-03-30 23:25:33 +0000457 llvm::DenseMap<const FieldDecl*, unsigned>::iterator I = FieldInfo.find(FD);
Hartmut Kaiser21fdf412007-10-24 00:07:36 +0000458 assert (I != FieldInfo.end() && "Unable to find field info");
Devang Patelb84a06e2007-10-23 02:10:49 +0000459 return I->second;
460}
461
Devang Patelc4c429a2007-10-24 00:56:23 +0000462/// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000463void CodeGenTypes::addFieldInfo(const FieldDecl *FD, unsigned No) {
464 FieldInfo[FD] = No;
465}
466
467/// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field FD.
468CodeGenTypes::BitFieldInfo CodeGenTypes::getBitFieldInfo(const FieldDecl *FD) {
469 llvm::DenseMap<const FieldDecl *, BitFieldInfo>::iterator
470 I = BitFields.find(FD);
471 assert (I != BitFields.end() && "Unable to find bitfield info");
472 return I->second;
473}
474
475/// addBitFieldInfo - Assign a start bit and a size to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000476void CodeGenTypes::addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
477 unsigned Start, unsigned Size) {
478 BitFields.insert(std::make_pair(FD, BitFieldInfo(FieldNo, Start, Size)));
Devang Patelb84a06e2007-10-23 02:10:49 +0000479}
480
Devang Patel88a981b2007-11-01 19:11:01 +0000481/// getCGRecordLayout - Return record layout info for the given llvm::Type.
Anders Carlssonad3e7112009-08-24 17:16:23 +0000482const CGRecordLayout &
Chris Lattneraf319132008-02-05 06:55:31 +0000483CodeGenTypes::getCGRecordLayout(const TagDecl *TD) const {
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000484 const Type *Key =
Mike Stumpe607ed02009-08-07 18:05:12 +0000485 Context.getTagDeclType(TD).getTypePtr();
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000486 llvm::DenseMap<const Type*, CGRecordLayout *>::iterator I
487 = CGRecordLayouts.find(Key);
Devang Patel88a981b2007-11-01 19:11:01 +0000488 assert (I != CGRecordLayouts.end()
Devang Patelb84a06e2007-10-23 02:10:49 +0000489 && "Unable to find record layout information for type");
Anders Carlssonad3e7112009-08-24 17:16:23 +0000490 return *I->second;
Devang Patelb84a06e2007-10-23 02:10:49 +0000491}