blob: c94dd89d5c4824c6c283b178c4dd5de7677fbde9 [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//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This is the code that handles AST -> LLVM type lowering.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenTypes.h"
Daniel Dunbar270e2032010-03-31 00:11:27 +000015#include "CGCall.h"
Daniel Dunbar2924ade2010-03-30 22:26:10 +000016#include "CGRecordLayout.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000019#include "clang/AST/DeclCXX.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000020#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/DerivedTypes.h"
Anders Carlsson4e533282007-08-17 22:00:32 +000023#include "llvm/Module.h"
Devang Pateld9e9ede2007-10-31 20:08:22 +000024#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26using namespace CodeGen;
27
Devang Patel7a4718e2007-10-31 20:01:01 +000028CodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M,
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000029 const llvm::TargetData &TD, const ABIInfo &Info)
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000030 : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000031 TheABIInfo(Info) {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000032}
Reid Spencer5f016e22007-07-11 17:01:13 +000033
Devang Patelb84a06e2007-10-23 02:10:49 +000034CodeGenTypes::~CodeGenTypes() {
Mike Stump1eb44332009-09-09 15:08:12 +000035 for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
36 I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
Devang Patelb84a06e2007-10-23 02:10:49 +000037 I != E; ++I)
38 delete I->second;
Chris Lattner6f41c172010-01-11 19:58:10 +000039
40 for (llvm::FoldingSet<CGFunctionInfo>::iterator
41 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
42 delete &*I++;
Devang Patelb84a06e2007-10-23 02:10:49 +000043}
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// ConvertType - Convert the specified type to its LLVM form.
46const llvm::Type *CodeGenTypes::ConvertType(QualType T) {
Chris Lattnerfce71b82008-04-03 05:50:42 +000047 llvm::PATypeHolder Result = ConvertTypeRecursive(T);
48
49 // Any pointers that were converted defered evaluation of their pointee type,
50 // creating an opaque type instead. This is in order to avoid problems with
51 // circular types. Loop through all these defered pointees, if any, and
52 // resolve them now.
53 while (!PointersToResolve.empty()) {
Chris Lattner6f41c172010-01-11 19:58:10 +000054 std::pair<QualType, llvm::OpaqueType*> P = PointersToResolve.pop_back_val();
55
Chris Lattnerfce71b82008-04-03 05:50:42 +000056 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +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);
Mike Stump1eb44332009-09-09 15:08:12 +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);
Duncan Sandsf177d9d2010-02-15 16:14:01 +000085 if (ResultType->isIntegerTy(1))
Owen Anderson0032b272009-08-13 21:57:51 +000086 return llvm::IntegerType::get(getLLVMContext(),
87 (unsigned)Context.getTypeSize(T));
Chris Lattner6f41c172010-01-11 19:58:10 +000088 // FIXME: Should assert that the llvm type and AST type has the same size.
Eli Friedman57a84fb2009-03-03 04:48:01 +000089 return ResultType;
90}
91
Chris Lattner4581fff2008-02-06 05:21:55 +000092/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
93/// ConvertType in that it is used to convert to the memory representation for
94/// a type. For example, the scalar representation for _Bool is i1, but the
95/// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +000096const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
97 const llvm::Type *R = ConvertType(T);
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattner19009e62008-01-09 18:47:25 +000099 // If this is a non-bool type, don't map it.
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000100 if (!R->isIntegerTy(1))
Chris Lattner19009e62008-01-09 18:47:25 +0000101 return R;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner19009e62008-01-09 18:47:25 +0000103 // Otherwise, return an integer of the target-specified size.
Owen Anderson0032b272009-08-13 21:57:51 +0000104 return llvm::IntegerType::get(getLLVMContext(),
105 (unsigned)Context.getTypeSize(T));
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner19009e62008-01-09 18:47:25 +0000107}
108
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000109// Code to verify a given function type is complete, i.e. the return type
110// and all of the argument types are complete.
Eli Friedmanc00129a2010-05-30 06:03:20 +0000111const TagType *CodeGenTypes::VerifyFuncTypeComplete(const Type* T) {
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000112 const FunctionType *FT = cast<FunctionType>(T);
Ted Kremenek6217b802009-07-29 21:53:49 +0000113 if (const TagType* TT = FT->getResultType()->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000114 if (!TT->getDecl()->isDefinition())
115 return TT;
116 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T))
117 for (unsigned i = 0; i < FPT->getNumArgs(); i++)
Ted Kremenek6217b802009-07-29 21:53:49 +0000118 if (const TagType* TT = FPT->getArgType(i)->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000119 if (!TT->getDecl()->isDefinition())
120 return TT;
121 return 0;
122}
123
Chris Lattnerc5b88062008-02-06 05:08:19 +0000124/// UpdateCompletedType - When we find the full definition for a TagDecl,
125/// replace the 'opaque' type we previously made for it if applicable.
126void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
Mike Stumpe607ed02009-08-07 18:05:12 +0000127 const Type *Key = Context.getTagDeclType(TD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000128 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000129 TagDeclTypes.find(Key);
Chris Lattner6ef58e32008-02-06 05:12:09 +0000130 if (TDTI == TagDeclTypes.end()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattner6ef58e32008-02-06 05:12:09 +0000132 // Remember the opaque LLVM type for this tagdecl.
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000133 llvm::PATypeHolder OpaqueHolder = TDTI->second;
134 assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) &&
Chris Lattner6ef58e32008-02-06 05:12:09 +0000135 "Updating compilation of an already non-opaque type?");
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000137 // Remove it from TagDeclTypes so that it will be regenerated.
138 TagDeclTypes.erase(TDTI);
139
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000140 // Generate the new type.
141 const llvm::Type *NT = ConvertTagDeclType(TD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000142
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000143 // Refine the old opaque type to its new definition.
144 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT);
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000145
146 // Since we just completed a tag type, check to see if any function types
147 // were completed along with the tag type.
148 // FIXME: This is very inefficient; if we track which function types depend
149 // on which tag types, though, it should be reasonably efficient.
150 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator i;
151 for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) {
152 if (const TagType* TT = VerifyFuncTypeComplete(i->first)) {
153 // This function type still depends on an incomplete tag type; make sure
154 // that tag type has an associated opaque type.
155 ConvertTagDeclType(TT->getDecl());
156 } else {
157 // This function no longer depends on an incomplete tag type; create the
158 // function type, and refine the opaque type to the new function type.
159 llvm::PATypeHolder OpaqueHolder = i->second;
160 const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0));
161 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT);
162 FunctionTypes.erase(i);
163 }
164 }
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000165}
166
Mike Stump1eb44332009-09-09 15:08:12 +0000167static const llvm::Type* getTypeForFormat(llvm::LLVMContext &VMContext,
Owen Anderson0032b272009-08-13 21:57:51 +0000168 const llvm::fltSemantics &format) {
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000169 if (&format == &llvm::APFloat::IEEEsingle)
Owen Anderson0032b272009-08-13 21:57:51 +0000170 return llvm::Type::getFloatTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000171 if (&format == &llvm::APFloat::IEEEdouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000172 return llvm::Type::getDoubleTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000173 if (&format == &llvm::APFloat::IEEEquad)
Owen Anderson0032b272009-08-13 21:57:51 +0000174 return llvm::Type::getFP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000175 if (&format == &llvm::APFloat::PPCDoubleDouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000176 return llvm::Type::getPPC_FP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000177 if (&format == &llvm::APFloat::x87DoubleExtended)
Owen Anderson0032b272009-08-13 21:57:51 +0000178 return llvm::Type::getX86_FP80Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000179 assert(0 && "Unknown float format!");
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000180 return 0;
181}
182
Devang Patel30ec9972007-10-25 18:32:36 +0000183const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
John McCalle27ec8a2009-10-23 23:03:21 +0000184 const clang::Type &Ty = *Context.getCanonicalType(T).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 switch (Ty.getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000187#define TYPE(Class, Base)
188#define ABSTRACT_TYPE(Class, Base)
189#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
190#define DEPENDENT_TYPE(Class, Base) case Type::Class:
John McCallad5e7382010-03-01 23:49:17 +0000191#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +0000192#include "clang/AST/TypeNodes.def"
193 assert(false && "Non-canonical or dependent types aren't possible.");
194 break;
195
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 case Type::Builtin: {
197 switch (cast<BuiltinType>(Ty).getKind()) {
198 case BuiltinType::Void:
Steve Naroffde2e22d2009-07-15 18:40:39 +0000199 case BuiltinType::ObjCId:
200 case BuiltinType::ObjCClass:
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000201 case BuiltinType::ObjCSel:
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 // LLVM void type can only be used as the result of a function call. Just
203 // map to the same as char.
Owen Anderson0032b272009-08-13 21:57:51 +0000204 return llvm::IntegerType::get(getLLVMContext(), 8);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205
206 case BuiltinType::Bool:
Chris Lattner19009e62008-01-09 18:47:25 +0000207 // Note that we always return bool as i1 for use as a scalar type.
Owen Anderson0032b272009-08-13 21:57:51 +0000208 return llvm::Type::getInt1Ty(getLLVMContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000210 case BuiltinType::Char_S:
211 case BuiltinType::Char_U:
212 case BuiltinType::SChar:
213 case BuiltinType::UChar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 case BuiltinType::Short:
215 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 case BuiltinType::Int:
217 case BuiltinType::UInt:
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 case BuiltinType::Long:
219 case BuiltinType::ULong:
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 case BuiltinType::LongLong:
221 case BuiltinType::ULongLong:
Argyrios Kyrtzidisafef76e2008-08-09 22:01:55 +0000222 case BuiltinType::WChar:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000223 case BuiltinType::Char16:
224 case BuiltinType::Char32:
Owen Anderson0032b272009-08-13 21:57:51 +0000225 return llvm::IntegerType::get(getLLVMContext(),
Chris Lattner98be4942008-03-05 18:54:05 +0000226 static_cast<unsigned>(Context.getTypeSize(T)));
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000228 case BuiltinType::Float:
Nate Begemanc8b12272008-04-18 05:41:31 +0000229 case BuiltinType::Double:
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 case BuiltinType::LongDouble:
Mike Stump1eb44332009-09-09 15:08:12 +0000231 return getTypeForFormat(getLLVMContext(),
Owen Anderson0032b272009-08-13 21:57:51 +0000232 Context.getFloatTypeSemantics(T));
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000234 case BuiltinType::NullPtr: {
235 // Model std::nullptr_t as i8*
236 const llvm::Type *Ty = llvm::IntegerType::get(getLLVMContext(), 8);
237 return llvm::PointerType::getUnqual(Ty);
238 }
239
Chris Lattner2df9ced2009-04-30 02:43:43 +0000240 case BuiltinType::UInt128:
241 case BuiltinType::Int128:
Owen Anderson0032b272009-08-13 21:57:51 +0000242 return llvm::IntegerType::get(getLLVMContext(), 128);
Eli Friedman8c692352009-12-18 23:28:34 +0000243
244 case BuiltinType::Overload:
245 case BuiltinType::Dependent:
246 case BuiltinType::UndeducedAuto:
247 assert(0 && "Unexpected builtin type!");
248 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 }
Eli Friedman8c692352009-12-18 23:28:34 +0000250 assert(0 && "Unknown builtin type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 break;
252 }
253 case Type::Complex: {
Mike Stump1eb44332009-09-09 15:08:12 +0000254 const llvm::Type *EltTy =
Chris Lattnerfce71b82008-04-03 05:50:42 +0000255 ConvertTypeRecursive(cast<ComplexType>(Ty).getElementType());
Owen Anderson47a434f2009-08-05 23:18:46 +0000256 return llvm::StructType::get(TheModule.getContext(), EltTy, EltTy, NULL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000258 case Type::LValueReference:
259 case Type::RValueReference: {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000260 const ReferenceType &RTy = cast<ReferenceType>(Ty);
261 QualType ETy = RTy.getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000262 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000263 PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
264 return llvm::PointerType::get(PointeeType, ETy.getAddressSpace());
265 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 case Type::Pointer: {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000267 const PointerType &PTy = cast<PointerType>(Ty);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000268 QualType ETy = PTy.getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000269 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000270 PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
Chris Lattnerfce71b82008-04-03 05:50:42 +0000271 return llvm::PointerType::get(PointeeType, ETy.getAddressSpace());
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 }
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Steve Narofffb22d962007-08-30 01:06:46 +0000274 case Type::VariableArray: {
275 const VariableArrayType &A = cast<VariableArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +0000276 assert(A.getIndexTypeCVRQualifiers() == 0 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 "FIXME: We only handle trivial array types so far!");
Eli Friedmanc5773c42008-02-15 18:16:39 +0000278 // VLAs resolve to the innermost element type; this matches
279 // the return of alloca, and there isn't any obviously better choice.
Eli Friedman57a84fb2009-03-03 04:48:01 +0000280 return ConvertTypeForMemRecursive(A.getElementType());
Eli Friedmanc5773c42008-02-15 18:16:39 +0000281 }
282 case Type::IncompleteArray: {
283 const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +0000284 assert(A.getIndexTypeCVRQualifiers() == 0 &&
Eli Friedmanc5773c42008-02-15 18:16:39 +0000285 "FIXME: We only handle trivial array types so far!");
286 // int X[] -> [0 x int]
Eli Friedman57a84fb2009-03-03 04:48:01 +0000287 return llvm::ArrayType::get(ConvertTypeForMemRecursive(A.getElementType()), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 }
Steve Narofffb22d962007-08-30 01:06:46 +0000289 case Type::ConstantArray: {
290 const ConstantArrayType &A = cast<ConstantArrayType>(Ty);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000291 const llvm::Type *EltTy = ConvertTypeForMemRecursive(A.getElementType());
Steve Narofffb22d962007-08-30 01:06:46 +0000292 return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue());
293 }
Nate Begeman213541a2008-04-18 23:10:10 +0000294 case Type::ExtVector:
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 case Type::Vector: {
296 const VectorType &VT = cast<VectorType>(Ty);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000297 return llvm::VectorType::get(ConvertTypeRecursive(VT.getElementType()),
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 VT.getNumElements());
299 }
300 case Type::FunctionNoProto:
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000301 case Type::FunctionProto: {
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000302 // First, check whether we can build the full function type.
303 if (const TagType* TT = VerifyFuncTypeComplete(&Ty)) {
304 // This function's type depends on an incomplete tag type; make sure
305 // we have an opaque type corresponding to the tag type.
306 ConvertTagDeclType(TT->getDecl());
307 // Create an opaque type for this function type, save it, and return it.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000308 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000309 FunctionTypes.insert(std::make_pair(&Ty, ResultType));
310 return ResultType;
311 }
312 // The function type can be built; call the appropriate routines to
313 // build it.
Chris Lattner9a1a9c42009-03-31 08:55:07 +0000314 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(&Ty))
John McCallead608a2010-02-26 00:48:12 +0000315 return GetFunctionType(getFunctionInfo(
316 CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT,0))),
317 FPT->isVariadic());
Chris Lattner9a1a9c42009-03-31 08:55:07 +0000318
319 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(&Ty);
John McCallead608a2010-02-26 00:48:12 +0000320 return GetFunctionType(getFunctionInfo(
321 CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT,0))),
322 true);
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
John McCallc12c5bb2010-05-15 11:32:37 +0000325 case Type::ObjCObject:
326 return ConvertTypeRecursive(cast<ObjCObjectType>(Ty).getBaseType());
327
Chris Lattner391d77a2008-03-30 23:03:07 +0000328 case Type::ObjCInterface: {
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000329 // Objective-C interfaces are always opaque (outside of the
330 // runtime, which can do whatever it likes); we never refine
331 // these.
332 const llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(&Ty)];
333 if (!T)
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000334 T = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000335 return T;
Chris Lattner391d77a2008-03-30 23:03:07 +0000336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Steve Naroff14108da2009-07-10 23:34:53 +0000338 case Type::ObjCObjectPointer: {
Daniel Dunbar28e47802009-07-11 21:12:14 +0000339 // Protocol qualifications do not influence the LLVM type, we just return a
340 // pointer to the underlying interface type. We don't need to worry about
341 // recursive conversion.
Mike Stump1eb44332009-09-09 15:08:12 +0000342 const llvm::Type *T =
Daniel Dunbar28e47802009-07-11 21:12:14 +0000343 ConvertTypeRecursive(cast<ObjCObjectPointerType>(Ty).getPointeeType());
344 return llvm::PointerType::getUnqual(T);
Steve Naroff14108da2009-07-10 23:34:53 +0000345 }
Daniel Dunbar28e47802009-07-11 21:12:14 +0000346
Douglas Gregor72564e72009-02-26 23:50:07 +0000347 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000348 case Type::Enum: {
Chris Lattnerde0efb32008-02-06 05:48:29 +0000349 const TagDecl *TD = cast<TagType>(Ty).getDecl();
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000350 const llvm::Type *Res = ConvertTagDeclType(TD);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattnerde0efb32008-02-06 05:48:29 +0000352 std::string TypeName(TD->getKindName());
353 TypeName += '.';
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattnerde0efb32008-02-06 05:48:29 +0000355 // Name the codegen type after the typedef name
356 // if there is no tag type name available
357 if (TD->getIdentifier())
Anders Carlsson6b7fc132009-09-26 19:03:24 +0000358 // FIXME: We should not have to check for a null decl context here.
359 // Right now we do it because the implicit Obj-C decls don't have one.
360 TypeName += TD->getDeclContext() ? TD->getQualifiedNameAsString() :
361 TD->getNameAsString();
Chris Lattnerde0efb32008-02-06 05:48:29 +0000362 else if (const TypedefType *TdT = dyn_cast<TypedefType>(T))
Anders Carlsson6b7fc132009-09-26 19:03:24 +0000363 // FIXME: We should not have to check for a null decl context here.
364 // Right now we do it because the implicit Obj-C decls don't have one.
365 TypeName += TdT->getDecl()->getDeclContext() ?
366 TdT->getDecl()->getQualifiedNameAsString() :
367 TdT->getDecl()->getNameAsString();
Chris Lattnerde0efb32008-02-06 05:48:29 +0000368 else
369 TypeName += "anon";
Mike Stump1eb44332009-09-09 15:08:12 +0000370
371 TheModule.addTypeName(TypeName, Res);
Chris Lattnerde0efb32008-02-06 05:48:29 +0000372 return Res;
373 }
Daniel Dunbar90488912008-08-28 18:02:04 +0000374
375 case Type::BlockPointer: {
Daniel Dunbar4e174f12009-01-09 02:48:46 +0000376 const QualType FTy = cast<BlockPointerType>(Ty).getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000377 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Fariborz Jahanian209bb432009-03-13 20:36:41 +0000378 PointersToResolve.push_back(std::make_pair(FTy, PointeeType));
379 return llvm::PointerType::get(PointeeType, FTy.getAddressSpace());
Daniel Dunbar90488912008-08-28 18:02:04 +0000380 }
Sebastian Redl424c51d2009-01-25 13:35:30 +0000381
Anders Carlsson0e650012009-05-17 17:41:20 +0000382 case Type::MemberPointer: {
383 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
384 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
385 // If we ever want to support other ABIs this needs to be abstracted.
386
387 QualType ETy = cast<MemberPointerType>(Ty).getPointeeType();
Eli Friedman4a2251b2009-12-03 12:44:31 +0000388 const llvm::Type *PtrDiffTy =
389 ConvertTypeRecursive(Context.getPointerDiffType());
Chris Lattner6f41c172010-01-11 19:58:10 +0000390 if (ETy->isFunctionType())
Eli Friedman4a2251b2009-12-03 12:44:31 +0000391 return llvm::StructType::get(TheModule.getContext(), PtrDiffTy, PtrDiffTy,
Anders Carlsson0e650012009-05-17 17:41:20 +0000392 NULL);
Chris Lattner6f41c172010-01-11 19:58:10 +0000393 return PtrDiffTy;
Anders Carlsson0e650012009-05-17 17:41:20 +0000394 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 // FIXME: implement.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000398 return llvm::OpaqueType::get(getLLVMContext());
Reid Spencer5f016e22007-07-11 17:01:13 +0000399}
400
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000401/// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
402/// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000403const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000404 // TagDecl's are not necessarily unique, instead use the (clang)
405 // type connected to the decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000406 const Type *Key =
Mike Stumpe607ed02009-08-07 18:05:12 +0000407 Context.getTagDeclType(TD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000408 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000409 TagDeclTypes.find(Key);
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Chris Lattner5de00fc2008-02-06 06:03:51 +0000411 // If we've already compiled this tag type, use the previous definition.
412 if (TDTI != TagDeclTypes.end())
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000413 return TDTI->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
John McCall5cfa0112010-02-05 01:33:36 +0000415 // If this is still a forward declaration, just define an opaque
416 // type to use for this tagged decl.
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000417 if (!TD->isDefinition()) {
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000418 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000419 TagDeclTypes.insert(std::make_pair(Key, ResultType));
Chris Lattner5de00fc2008-02-06 06:03:51 +0000420 return ResultType;
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattner5de00fc2008-02-06 06:03:51 +0000423 // Okay, this is a definition of a type. Compile the implementation now.
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner6f41c172010-01-11 19:58:10 +0000425 if (TD->isEnum()) // Don't bother storing enums in TagDeclTypes.
Chris Lattnerfce71b82008-04-03 05:50:42 +0000426 return ConvertTypeRecursive(cast<EnumDecl>(TD)->getIntegerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattner5de00fc2008-02-06 06:03:51 +0000428 // This decl could well be recursive. In this case, insert an opaque
429 // definition of this type, which the recursive uses will get. We will then
430 // refine this opaque version later.
431
432 // Create new OpaqueType now for later use in case this is a recursive
433 // type. This will later be refined to the actual type.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000434 llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000435 TagDeclTypes.insert(std::make_pair(Key, ResultHolder));
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattner5de00fc2008-02-06 06:03:51 +0000437 const RecordDecl *RD = cast<const RecordDecl>(TD);
Daniel Dunbarae287232009-04-22 08:50:59 +0000438
John McCall86ff3082010-02-04 22:26:26 +0000439 // Force conversion of non-virtual base classes recursively.
440 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) {
441 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
442 e = RD->bases_end(); i != e; ++i) {
443 if (!i->isVirtual()) {
444 const CXXRecordDecl *Base =
445 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
446 ConvertTagDeclType(Base);
447 }
448 }
449 }
450
Anders Carlsson696798f2009-07-27 17:10:54 +0000451 // Layout fields.
Daniel Dunbar270e2032010-03-31 00:11:27 +0000452 CGRecordLayout *Layout = ComputeRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Anders Carlsson696798f2009-07-27 17:10:54 +0000454 CGRecordLayouts[Key] = Layout;
Chris Lattner6f41c172010-01-11 19:58:10 +0000455 const llvm::Type *ResultType = Layout->getLLVMType();
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner5de00fc2008-02-06 06:03:51 +0000457 // Refine our Opaque type to ResultType. This can invalidate ResultType, so
458 // make sure to read the result out of the holder.
459 cast<llvm::OpaqueType>(ResultHolder.get())
460 ->refineAbstractTypeTo(ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner5de00fc2008-02-06 06:03:51 +0000462 return ResultHolder.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000463}
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000464
Devang Patel88a981b2007-11-01 19:11:01 +0000465/// getCGRecordLayout - Return record layout info for the given llvm::Type.
Anders Carlssonad3e7112009-08-24 17:16:23 +0000466const CGRecordLayout &
Daniel Dunbar7c465b92010-03-30 22:26:12 +0000467CodeGenTypes::getCGRecordLayout(const RecordDecl *TD) const {
Chris Lattner6f41c172010-01-11 19:58:10 +0000468 const Type *Key = Context.getTagDeclType(TD).getTypePtr();
Daniel Dunbar270e2032010-03-31 00:11:27 +0000469 const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
470 assert(Layout && "Unable to find record layout information for type");
471 return *Layout;
Devang Patelb84a06e2007-10-23 02:10:49 +0000472}
Anders Carlsson3e5af902010-05-14 19:41:56 +0000473
474bool CodeGenTypes::ContainsPointerToDataMember(QualType T) {
475 // No need to check for member pointers when not compiling C++.
476 if (!Context.getLangOptions().CPlusPlus)
477 return false;
478
479 T = Context.getBaseElementType(T);
480
481 if (const RecordType *RT = T->getAs<RecordType>()) {
482 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
483
Anders Carlssonc39211d2010-05-18 03:47:15 +0000484 return ContainsPointerToDataMember(RD);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000485 }
486
487 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
488 return !MPT->getPointeeType()->isFunctionType();
489
490 return false;
491}
Anders Carlssonc39211d2010-05-18 03:47:15 +0000492
493bool CodeGenTypes::ContainsPointerToDataMember(const CXXRecordDecl *RD) {
494
495 // FIXME: It would be better if there was a way to explicitly compute the
496 // record layout instead of converting to a type.
497 ConvertTagDeclType(RD);
498
499 const CGRecordLayout &Layout = getCGRecordLayout(RD);
500 return Layout.containsPointerToDataMember();
501}