blob: 4c1ae9b66d583da13cf615488a8aea45213b3f62 [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"
John McCallf16aa102010-08-22 21:01:12 +000016#include "CGCXXABI.h"
Daniel Dunbar2924ade2010-03-30 22:26:10 +000017#include "CGRecordLayout.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000020#include "clang/AST/DeclCXX.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000021#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "llvm/DerivedTypes.h"
Anders Carlsson4e533282007-08-17 22:00:32 +000024#include "llvm/Module.h"
Devang Pateld9e9ede2007-10-31 20:08:22 +000025#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27using namespace CodeGen;
28
Devang Patel7a4718e2007-10-31 20:01:01 +000029CodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M,
John McCallf16aa102010-08-22 21:01:12 +000030 const llvm::TargetData &TD, const ABIInfo &Info,
31 CGCXXABI &CXXABI)
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000032 : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD),
John McCallf16aa102010-08-22 21:01:12 +000033 TheABIInfo(Info), TheCXXABI(CXXABI) {
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() {
Mike Stump1eb44332009-09-09 15:08:12 +000037 for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
38 I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
Devang Patelb84a06e2007-10-23 02:10:49 +000039 I != E; ++I)
40 delete I->second;
Chris Lattner6f41c172010-01-11 19:58:10 +000041
42 for (llvm::FoldingSet<CGFunctionInfo>::iterator
43 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
44 delete &*I++;
Devang Patelb84a06e2007-10-23 02:10:49 +000045}
46
Chris Lattnera9fa8582010-07-01 06:20:47 +000047/// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
48/// pointers that are referenced but have not been converted yet. This is used
49/// to handle cyclic structures properly.
50void CodeGenTypes::HandleLateResolvedPointers() {
51 assert(!PointersToResolve.empty() && "No pointers to resolve!");
Chris Lattnerbcaedae2010-06-30 19:14:05 +000052
53 // Any pointers that were converted deferred evaluation of their pointee type,
Chris Lattnerfce71b82008-04-03 05:50:42 +000054 // creating an opaque type instead. This is in order to avoid problems with
55 // circular types. Loop through all these defered pointees, if any, and
56 // resolve them now.
57 while (!PointersToResolve.empty()) {
Chris Lattner6f41c172010-01-11 19:58:10 +000058 std::pair<QualType, llvm::OpaqueType*> P = PointersToResolve.pop_back_val();
59
Chris Lattnerfce71b82008-04-03 05:50:42 +000060 // We can handle bare pointers here because we know that the only pointers
61 // to the Opaque type are P.second and from other types. Refining the
62 // opqaue type away will invalidate P.second, but we don't mind :).
Eli Friedman57a84fb2009-03-03 04:48:01 +000063 const llvm::Type *NT = ConvertTypeForMemRecursive(P.first);
Chris Lattnerfce71b82008-04-03 05:50:42 +000064 P.second->refineAbstractTypeTo(NT);
65 }
Chris Lattnera9fa8582010-07-01 06:20:47 +000066}
Chris Lattnerfce71b82008-04-03 05:50:42 +000067
Anders Carlssone0047b12011-04-20 23:51:43 +000068void CodeGenTypes::addRecordTypeName(const RecordDecl *RD, const llvm::Type *Ty,
69 llvm::StringRef suffix) {
Anders Carlssone9742b02011-04-17 21:36:59 +000070 llvm::SmallString<256> TypeName;
71 llvm::raw_svector_ostream OS(TypeName);
Anders Carlssone0047b12011-04-20 23:51:43 +000072 OS << RD->getKindName() << '.';
Anders Carlssone9742b02011-04-17 21:36:59 +000073
74 // Name the codegen type after the typedef name
75 // if there is no tag type name available
Anders Carlssone0047b12011-04-20 23:51:43 +000076 if (RD->getIdentifier()) {
Anders Carlssone9742b02011-04-17 21:36:59 +000077 // FIXME: We should not have to check for a null decl context here.
78 // Right now we do it because the implicit Obj-C decls don't have one.
Anders Carlssone0047b12011-04-20 23:51:43 +000079 if (RD->getDeclContext())
80 OS << RD->getQualifiedNameAsString();
Anders Carlssone9742b02011-04-17 21:36:59 +000081 else
Anders Carlssone0047b12011-04-20 23:51:43 +000082 RD->printName(OS);
83 } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
Anders Carlssone9742b02011-04-17 21:36:59 +000084 // FIXME: We should not have to check for a null decl context here.
85 // Right now we do it because the implicit Obj-C decls don't have one.
86 if (TDD->getDeclContext())
87 OS << TDD->getQualifiedNameAsString();
88 else
89 TDD->printName(OS);
90 } else
91 OS << "anon";
92
93 if (!suffix.empty())
94 OS << suffix;
95
96 TheModule.addTypeName(OS.str(), Ty);
97}
Chris Lattnera9fa8582010-07-01 06:20:47 +000098
99/// ConvertType - Convert the specified type to its LLVM form.
100const llvm::Type *CodeGenTypes::ConvertType(QualType T, bool IsRecursive) {
101 const llvm::Type *Result = ConvertTypeRecursive(T);
102
103 // If this is a top-level call to ConvertType and sub-conversions caused
104 // pointers to get lazily built as opaque types, resolve the pointers, which
105 // might cause Result to be merged away.
106 if (!IsRecursive && !PointersToResolve.empty()) {
107 llvm::PATypeHolder ResultHandle = Result;
108 HandleLateResolvedPointers();
109 Result = ResultHandle;
110 }
Chris Lattnerfce71b82008-04-03 05:50:42 +0000111 return Result;
112}
113
114const llvm::Type *CodeGenTypes::ConvertTypeRecursive(QualType T) {
Chris Lattner09dc6662009-04-01 02:00:48 +0000115 T = Context.getCanonicalType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Devang Patel30ec9972007-10-25 18:32:36 +0000117 // See if type is already cached.
John McCallf4c73712011-01-19 06:33:43 +0000118 llvm::DenseMap<const Type *, llvm::PATypeHolder>::iterator
Chris Lattner96196622008-07-26 22:37:01 +0000119 I = TypeCache.find(T.getTypePtr());
Devang Patel3c400852007-12-21 19:35:28 +0000120 // If type is found in map and this is not a definition for a opaque
Chris Lattnerfae6e292008-02-06 05:29:46 +0000121 // place holder type then use it. Otherwise, convert type T.
Chris Lattner4581fff2008-02-06 05:21:55 +0000122 if (I != TypeCache.end())
Devang Patel47c87b42007-10-30 23:22:14 +0000123 return I->second.get();
Devang Patel30ec9972007-10-25 18:32:36 +0000124
125 const llvm::Type *ResultType = ConvertNewType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000126 TypeCache.insert(std::make_pair(T.getTypePtr(),
Chris Lattner4581fff2008-02-06 05:21:55 +0000127 llvm::PATypeHolder(ResultType)));
Devang Patel30ec9972007-10-25 18:32:36 +0000128 return ResultType;
129}
130
Chris Lattner4581fff2008-02-06 05:21:55 +0000131/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
132/// ConvertType in that it is used to convert to the memory representation for
133/// a type. For example, the scalar representation for _Bool is i1, but the
134/// memory representation is usually i8 or i32, depending on the target.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000135const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool IsRecursive){
136 const llvm::Type *R = ConvertType(T, IsRecursive);
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Chris Lattner19009e62008-01-09 18:47:25 +0000138 // If this is a non-bool type, don't map it.
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000139 if (!R->isIntegerTy(1))
Chris Lattner19009e62008-01-09 18:47:25 +0000140 return R;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattner19009e62008-01-09 18:47:25 +0000142 // Otherwise, return an integer of the target-specified size.
Owen Anderson0032b272009-08-13 21:57:51 +0000143 return llvm::IntegerType::get(getLLVMContext(),
144 (unsigned)Context.getTypeSize(T));
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattner19009e62008-01-09 18:47:25 +0000146}
147
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000148// Code to verify a given function type is complete, i.e. the return type
149// and all of the argument types are complete.
Eli Friedmanc00129a2010-05-30 06:03:20 +0000150const TagType *CodeGenTypes::VerifyFuncTypeComplete(const Type* T) {
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000151 const FunctionType *FT = cast<FunctionType>(T);
Ted Kremenek6217b802009-07-29 21:53:49 +0000152 if (const TagType* TT = FT->getResultType()->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000153 if (!TT->getDecl()->isDefinition())
154 return TT;
155 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T))
156 for (unsigned i = 0; i < FPT->getNumArgs(); i++)
Ted Kremenek6217b802009-07-29 21:53:49 +0000157 if (const TagType* TT = FPT->getArgType(i)->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000158 if (!TT->getDecl()->isDefinition())
159 return TT;
160 return 0;
161}
162
Chris Lattnerc5b88062008-02-06 05:08:19 +0000163/// UpdateCompletedType - When we find the full definition for a TagDecl,
164/// replace the 'opaque' type we previously made for it if applicable.
165void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
Mike Stumpe607ed02009-08-07 18:05:12 +0000166 const Type *Key = Context.getTagDeclType(TD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000167 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000168 TagDeclTypes.find(Key);
Chris Lattner6ef58e32008-02-06 05:12:09 +0000169 if (TDTI == TagDeclTypes.end()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Chris Lattner6ef58e32008-02-06 05:12:09 +0000171 // Remember the opaque LLVM type for this tagdecl.
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000172 llvm::PATypeHolder OpaqueHolder = TDTI->second;
173 assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) &&
Chris Lattner6ef58e32008-02-06 05:12:09 +0000174 "Updating compilation of an already non-opaque type?");
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000176 // Remove it from TagDeclTypes so that it will be regenerated.
177 TagDeclTypes.erase(TDTI);
178
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000179 // Generate the new type.
180 const llvm::Type *NT = ConvertTagDeclType(TD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000181
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000182 // Refine the old opaque type to its new definition.
183 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT);
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000184
185 // Since we just completed a tag type, check to see if any function types
186 // were completed along with the tag type.
187 // FIXME: This is very inefficient; if we track which function types depend
188 // on which tag types, though, it should be reasonably efficient.
189 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator i;
190 for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) {
191 if (const TagType* TT = VerifyFuncTypeComplete(i->first)) {
192 // This function type still depends on an incomplete tag type; make sure
193 // that tag type has an associated opaque type.
194 ConvertTagDeclType(TT->getDecl());
195 } else {
196 // This function no longer depends on an incomplete tag type; create the
197 // function type, and refine the opaque type to the new function type.
198 llvm::PATypeHolder OpaqueHolder = i->second;
199 const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0));
200 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT);
201 FunctionTypes.erase(i);
202 }
203 }
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000204}
205
Mike Stump1eb44332009-09-09 15:08:12 +0000206static const llvm::Type* getTypeForFormat(llvm::LLVMContext &VMContext,
Owen Anderson0032b272009-08-13 21:57:51 +0000207 const llvm::fltSemantics &format) {
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000208 if (&format == &llvm::APFloat::IEEEsingle)
Owen Anderson0032b272009-08-13 21:57:51 +0000209 return llvm::Type::getFloatTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000210 if (&format == &llvm::APFloat::IEEEdouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000211 return llvm::Type::getDoubleTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000212 if (&format == &llvm::APFloat::IEEEquad)
Owen Anderson0032b272009-08-13 21:57:51 +0000213 return llvm::Type::getFP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000214 if (&format == &llvm::APFloat::PPCDoubleDouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000215 return llvm::Type::getPPC_FP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000216 if (&format == &llvm::APFloat::x87DoubleExtended)
Owen Anderson0032b272009-08-13 21:57:51 +0000217 return llvm::Type::getX86_FP80Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000218 assert(0 && "Unknown float format!");
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000219 return 0;
220}
221
Devang Patel30ec9972007-10-25 18:32:36 +0000222const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
John McCalle27ec8a2009-10-23 23:03:21 +0000223 const clang::Type &Ty = *Context.getCanonicalType(T).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 switch (Ty.getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000226#define TYPE(Class, Base)
227#define ABSTRACT_TYPE(Class, Base)
228#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
229#define DEPENDENT_TYPE(Class, Base) case Type::Class:
John McCallad5e7382010-03-01 23:49:17 +0000230#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +0000231#include "clang/AST/TypeNodes.def"
John McCall864c0412011-04-26 20:42:42 +0000232 llvm_unreachable("Non-canonical or dependent types aren't possible.");
Douglas Gregor72564e72009-02-26 23:50:07 +0000233 break;
234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 case Type::Builtin: {
236 switch (cast<BuiltinType>(Ty).getKind()) {
237 case BuiltinType::Void:
Steve Naroffde2e22d2009-07-15 18:40:39 +0000238 case BuiltinType::ObjCId:
239 case BuiltinType::ObjCClass:
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000240 case BuiltinType::ObjCSel:
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // LLVM void type can only be used as the result of a function call. Just
242 // map to the same as char.
Chris Lattner77b89b82010-06-27 07:15:29 +0000243 return llvm::Type::getInt8Ty(getLLVMContext());
Reid Spencer5f016e22007-07-11 17:01:13 +0000244
245 case BuiltinType::Bool:
Chris Lattner19009e62008-01-09 18:47:25 +0000246 // Note that we always return bool as i1 for use as a scalar type.
Owen Anderson0032b272009-08-13 21:57:51 +0000247 return llvm::Type::getInt1Ty(getLLVMContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000249 case BuiltinType::Char_S:
250 case BuiltinType::Char_U:
251 case BuiltinType::SChar:
252 case BuiltinType::UChar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 case BuiltinType::Short:
254 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 case BuiltinType::Int:
256 case BuiltinType::UInt:
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 case BuiltinType::Long:
258 case BuiltinType::ULong:
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 case BuiltinType::LongLong:
260 case BuiltinType::ULongLong:
Chris Lattner3f59c972010-12-25 23:25:43 +0000261 case BuiltinType::WChar_S:
262 case BuiltinType::WChar_U:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000263 case BuiltinType::Char16:
264 case BuiltinType::Char32:
Owen Anderson0032b272009-08-13 21:57:51 +0000265 return llvm::IntegerType::get(getLLVMContext(),
Chris Lattner98be4942008-03-05 18:54:05 +0000266 static_cast<unsigned>(Context.getTypeSize(T)));
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000268 case BuiltinType::Float:
Nate Begemanc8b12272008-04-18 05:41:31 +0000269 case BuiltinType::Double:
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 case BuiltinType::LongDouble:
Mike Stump1eb44332009-09-09 15:08:12 +0000271 return getTypeForFormat(getLLVMContext(),
Owen Anderson0032b272009-08-13 21:57:51 +0000272 Context.getFloatTypeSemantics(T));
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000274 case BuiltinType::NullPtr: {
275 // Model std::nullptr_t as i8*
Chris Lattner77b89b82010-06-27 07:15:29 +0000276 const llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext());
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000277 return llvm::PointerType::getUnqual(Ty);
278 }
279
Chris Lattner2df9ced2009-04-30 02:43:43 +0000280 case BuiltinType::UInt128:
281 case BuiltinType::Int128:
Owen Anderson0032b272009-08-13 21:57:51 +0000282 return llvm::IntegerType::get(getLLVMContext(), 128);
Eli Friedman8c692352009-12-18 23:28:34 +0000283
284 case BuiltinType::Overload:
285 case BuiltinType::Dependent:
John McCall864c0412011-04-26 20:42:42 +0000286 case BuiltinType::BoundMember:
John McCall1de4d4e2011-04-07 08:22:57 +0000287 case BuiltinType::UnknownAny:
John McCall864c0412011-04-26 20:42:42 +0000288 llvm_unreachable("Unexpected placeholder builtin type!");
Eli Friedman8c692352009-12-18 23:28:34 +0000289 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 }
John McCall1de4d4e2011-04-07 08:22:57 +0000291 llvm_unreachable("Unknown builtin type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 break;
293 }
294 case Type::Complex: {
Mike Stump1eb44332009-09-09 15:08:12 +0000295 const llvm::Type *EltTy =
Chris Lattnerfce71b82008-04-03 05:50:42 +0000296 ConvertTypeRecursive(cast<ComplexType>(Ty).getElementType());
Chris Lattner7650d952011-06-18 22:49:11 +0000297 return llvm::StructType::get(EltTy, EltTy, NULL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000299 case Type::LValueReference:
300 case Type::RValueReference: {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000301 const ReferenceType &RTy = cast<ReferenceType>(Ty);
302 QualType ETy = RTy.getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000303 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000304 PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000305 unsigned AS = Context.getTargetAddressSpace(ETy);
306 return llvm::PointerType::get(PointeeType, AS);
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000307 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 case Type::Pointer: {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000309 const PointerType &PTy = cast<PointerType>(Ty);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000310 QualType ETy = PTy.getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000311 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000312 PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000313 unsigned AS = Context.getTargetAddressSpace(ETy);
314 return llvm::PointerType::get(PointeeType, AS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Steve Narofffb22d962007-08-30 01:06:46 +0000317 case Type::VariableArray: {
318 const VariableArrayType &A = cast<VariableArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +0000319 assert(A.getIndexTypeCVRQualifiers() == 0 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 "FIXME: We only handle trivial array types so far!");
Eli Friedmanc5773c42008-02-15 18:16:39 +0000321 // VLAs resolve to the innermost element type; this matches
322 // the return of alloca, and there isn't any obviously better choice.
Eli Friedman57a84fb2009-03-03 04:48:01 +0000323 return ConvertTypeForMemRecursive(A.getElementType());
Eli Friedmanc5773c42008-02-15 18:16:39 +0000324 }
325 case Type::IncompleteArray: {
326 const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +0000327 assert(A.getIndexTypeCVRQualifiers() == 0 &&
Eli Friedmanc5773c42008-02-15 18:16:39 +0000328 "FIXME: We only handle trivial array types so far!");
329 // int X[] -> [0 x int]
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000330 return llvm::ArrayType::get(ConvertTypeForMemRecursive(A.getElementType()),
331 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 }
Steve Narofffb22d962007-08-30 01:06:46 +0000333 case Type::ConstantArray: {
334 const ConstantArrayType &A = cast<ConstantArrayType>(Ty);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000335 const llvm::Type *EltTy = ConvertTypeForMemRecursive(A.getElementType());
Steve Narofffb22d962007-08-30 01:06:46 +0000336 return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue());
337 }
Nate Begeman213541a2008-04-18 23:10:10 +0000338 case Type::ExtVector:
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 case Type::Vector: {
340 const VectorType &VT = cast<VectorType>(Ty);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000341 return llvm::VectorType::get(ConvertTypeRecursive(VT.getElementType()),
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 VT.getNumElements());
343 }
344 case Type::FunctionNoProto:
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000345 case Type::FunctionProto: {
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000346 // First, check whether we can build the full function type. If the
347 // function type depends on an incomplete type (e.g. a struct or enum), we
348 // cannot lower the function type. Instead, turn it into an Opaque pointer
349 // and have UpdateCompletedType revisit the function type when/if the opaque
350 // argument type is defined.
Chris Lattnerce933992010-06-29 16:40:28 +0000351 if (const TagType *TT = VerifyFuncTypeComplete(&Ty)) {
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000352 // This function's type depends on an incomplete tag type; make sure
353 // we have an opaque type corresponding to the tag type.
354 ConvertTagDeclType(TT->getDecl());
355 // Create an opaque type for this function type, save it, and return it.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000356 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000357 FunctionTypes.insert(std::make_pair(&Ty, ResultType));
358 return ResultType;
359 }
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000360
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000361 // The function type can be built; call the appropriate routines to
362 // build it.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000363 const CGFunctionInfo *FI;
364 bool isVariadic;
365 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(&Ty)) {
366 FI = &getFunctionInfo(
367 CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)),
368 true /*Recursive*/);
369 isVariadic = FPT->isVariadic();
370 } else {
371 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(&Ty);
372 FI = &getFunctionInfo(
373 CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)),
374 true /*Recursive*/);
375 isVariadic = true;
376 }
Chris Lattner9a1a9c42009-03-31 08:55:07 +0000377
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000378 return GetFunctionType(*FI, isVariadic, true);
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
John McCallc12c5bb2010-05-15 11:32:37 +0000381 case Type::ObjCObject:
382 return ConvertTypeRecursive(cast<ObjCObjectType>(Ty).getBaseType());
383
Chris Lattner391d77a2008-03-30 23:03:07 +0000384 case Type::ObjCInterface: {
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000385 // Objective-C interfaces are always opaque (outside of the
386 // runtime, which can do whatever it likes); we never refine
387 // these.
388 const llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(&Ty)];
389 if (!T)
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000390 T = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000391 return T;
Chris Lattner391d77a2008-03-30 23:03:07 +0000392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Steve Naroff14108da2009-07-10 23:34:53 +0000394 case Type::ObjCObjectPointer: {
Daniel Dunbar28e47802009-07-11 21:12:14 +0000395 // Protocol qualifications do not influence the LLVM type, we just return a
396 // pointer to the underlying interface type. We don't need to worry about
397 // recursive conversion.
Mike Stump1eb44332009-09-09 15:08:12 +0000398 const llvm::Type *T =
Daniel Dunbar28e47802009-07-11 21:12:14 +0000399 ConvertTypeRecursive(cast<ObjCObjectPointerType>(Ty).getPointeeType());
400 return llvm::PointerType::getUnqual(T);
Steve Naroff14108da2009-07-10 23:34:53 +0000401 }
Daniel Dunbar28e47802009-07-11 21:12:14 +0000402
Douglas Gregor72564e72009-02-26 23:50:07 +0000403 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000404 case Type::Enum: {
Chris Lattnerde0efb32008-02-06 05:48:29 +0000405 const TagDecl *TD = cast<TagType>(Ty).getDecl();
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000406 const llvm::Type *Res = ConvertTagDeclType(TD);
Anders Carlssone0047b12011-04-20 23:51:43 +0000407
408 if (const RecordDecl *RD = dyn_cast<RecordDecl>(TD))
409 addRecordTypeName(RD, Res, llvm::StringRef());
Chris Lattnerde0efb32008-02-06 05:48:29 +0000410 return Res;
411 }
Daniel Dunbar90488912008-08-28 18:02:04 +0000412
413 case Type::BlockPointer: {
Daniel Dunbar4e174f12009-01-09 02:48:46 +0000414 const QualType FTy = cast<BlockPointerType>(Ty).getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000415 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Fariborz Jahanian209bb432009-03-13 20:36:41 +0000416 PointersToResolve.push_back(std::make_pair(FTy, PointeeType));
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000417 unsigned AS = Context.getTargetAddressSpace(FTy);
418 return llvm::PointerType::get(PointeeType, AS);
Daniel Dunbar90488912008-08-28 18:02:04 +0000419 }
Sebastian Redl424c51d2009-01-25 13:35:30 +0000420
Anders Carlsson0e650012009-05-17 17:41:20 +0000421 case Type::MemberPointer: {
John McCall0bab0cd2010-08-23 01:21:21 +0000422 return getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(&Ty));
Anders Carlsson0e650012009-05-17 17:41:20 +0000423 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 // FIXME: implement.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000427 return llvm::OpaqueType::get(getLLVMContext());
Reid Spencer5f016e22007-07-11 17:01:13 +0000428}
429
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000430/// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
431/// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000432const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000433 // TagDecl's are not necessarily unique, instead use the (clang)
434 // type connected to the decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000435 const Type *Key =
Mike Stumpe607ed02009-08-07 18:05:12 +0000436 Context.getTagDeclType(TD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000437 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000438 TagDeclTypes.find(Key);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattner5de00fc2008-02-06 06:03:51 +0000440 // If we've already compiled this tag type, use the previous definition.
441 if (TDTI != TagDeclTypes.end())
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000442 return TDTI->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000444 const EnumDecl *ED = dyn_cast<EnumDecl>(TD);
445
John McCall5cfa0112010-02-05 01:33:36 +0000446 // If this is still a forward declaration, just define an opaque
447 // type to use for this tagged decl.
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000448 // C++0x: If this is a enumeration type with fixed underlying type,
449 // consider it complete.
450 if (!TD->isDefinition() && !(ED && ED->isFixed())) {
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000451 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000452 TagDeclTypes.insert(std::make_pair(Key, ResultType));
Chris Lattner5de00fc2008-02-06 06:03:51 +0000453 return ResultType;
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Chris Lattner5de00fc2008-02-06 06:03:51 +0000456 // Okay, this is a definition of a type. Compile the implementation now.
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000458 if (ED) // Don't bother storing enums in TagDeclTypes.
459 return ConvertTypeRecursive(ED->getIntegerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattner5de00fc2008-02-06 06:03:51 +0000461 // This decl could well be recursive. In this case, insert an opaque
462 // definition of this type, which the recursive uses will get. We will then
463 // refine this opaque version later.
464
465 // Create new OpaqueType now for later use in case this is a recursive
466 // type. This will later be refined to the actual type.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000467 llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000468 TagDeclTypes.insert(std::make_pair(Key, ResultHolder));
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner5de00fc2008-02-06 06:03:51 +0000470 const RecordDecl *RD = cast<const RecordDecl>(TD);
Daniel Dunbarae287232009-04-22 08:50:59 +0000471
John McCall86ff3082010-02-04 22:26:26 +0000472 // Force conversion of non-virtual base classes recursively.
473 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) {
474 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
475 e = RD->bases_end(); i != e; ++i) {
476 if (!i->isVirtual()) {
477 const CXXRecordDecl *Base =
478 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
479 ConvertTagDeclType(Base);
480 }
481 }
482 }
483
Anders Carlsson696798f2009-07-27 17:10:54 +0000484 // Layout fields.
Daniel Dunbar270e2032010-03-31 00:11:27 +0000485 CGRecordLayout *Layout = ComputeRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Anders Carlsson696798f2009-07-27 17:10:54 +0000487 CGRecordLayouts[Key] = Layout;
Chris Lattner6f41c172010-01-11 19:58:10 +0000488 const llvm::Type *ResultType = Layout->getLLVMType();
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattner5de00fc2008-02-06 06:03:51 +0000490 // Refine our Opaque type to ResultType. This can invalidate ResultType, so
491 // make sure to read the result out of the holder.
492 cast<llvm::OpaqueType>(ResultHolder.get())
493 ->refineAbstractTypeTo(ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattner5de00fc2008-02-06 06:03:51 +0000495 return ResultHolder.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000496}
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000497
Anders Carlsson2d987772010-11-24 20:22:04 +0000498/// getCGRecordLayout - Return record layout info for the given record decl.
Anders Carlssonad3e7112009-08-24 17:16:23 +0000499const CGRecordLayout &
Anders Carlsson2d987772010-11-24 20:22:04 +0000500CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
501 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
Anders Carlsson82926962010-11-24 19:52:29 +0000502
Daniel Dunbar270e2032010-03-31 00:11:27 +0000503 const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000504 if (!Layout) {
Anders Carlsson2d987772010-11-24 20:22:04 +0000505 // Compute the type information.
506 ConvertTagDeclType(RD);
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000507
508 // Now try again.
509 Layout = CGRecordLayouts.lookup(Key);
510 }
511
Daniel Dunbar270e2032010-03-31 00:11:27 +0000512 assert(Layout && "Unable to find record layout information for type");
513 return *Layout;
Devang Patelb84a06e2007-10-23 02:10:49 +0000514}
Anders Carlsson3e5af902010-05-14 19:41:56 +0000515
Anders Carlsson6aed2a12011-04-17 21:40:34 +0000516void CodeGenTypes::addBaseSubobjectTypeName(const CXXRecordDecl *RD,
517 const CGRecordLayout &layout) {
518 llvm::StringRef suffix;
519 if (layout.getBaseSubobjectLLVMType() != layout.getLLVMType())
520 suffix = ".base";
521
Anders Carlssone0047b12011-04-20 23:51:43 +0000522 addRecordTypeName(RD, layout.getBaseSubobjectLLVMType(), suffix);
Anders Carlsson6aed2a12011-04-17 21:40:34 +0000523}
524
John McCallf16aa102010-08-22 21:01:12 +0000525bool CodeGenTypes::isZeroInitializable(QualType T) {
Anders Carlsson3e5af902010-05-14 19:41:56 +0000526 // No need to check for member pointers when not compiling C++.
527 if (!Context.getLangOptions().CPlusPlus)
John McCallf16aa102010-08-22 21:01:12 +0000528 return true;
Anders Carlsson3e5af902010-05-14 19:41:56 +0000529
530 T = Context.getBaseElementType(T);
531
John McCallf16aa102010-08-22 21:01:12 +0000532 // Records are non-zero-initializable if they contain any
533 // non-zero-initializable subobjects.
Anders Carlsson3e5af902010-05-14 19:41:56 +0000534 if (const RecordType *RT = T->getAs<RecordType>()) {
535 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCallf16aa102010-08-22 21:01:12 +0000536 return isZeroInitializable(RD);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000537 }
John McCallf16aa102010-08-22 21:01:12 +0000538
539 // We have to ask the ABI about member pointers.
Anders Carlsson3e5af902010-05-14 19:41:56 +0000540 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
John McCallf16aa102010-08-22 21:01:12 +0000541 return getCXXABI().isZeroInitializable(MPT);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000542
John McCallf16aa102010-08-22 21:01:12 +0000543 // Everything else is okay.
544 return true;
Anders Carlsson3e5af902010-05-14 19:41:56 +0000545}
Anders Carlssonc39211d2010-05-18 03:47:15 +0000546
John McCallf16aa102010-08-22 21:01:12 +0000547bool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlsson3379e9b2010-11-24 19:57:04 +0000548 return getCGRecordLayout(RD).isZeroInitializable();
Anders Carlssonc39211d2010-05-18 03:47:15 +0000549}