blob: 3c20934baf2593d8c419efb459dc7dbddc94aeba [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 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,
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000031 const llvm::TargetData &TD, const ABIInfo &Info)
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000032 : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000033 TheABIInfo(Info) {
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
Reid Spencer5f016e22007-07-11 17:01:13 +000047/// ConvertType - Convert the specified type to its LLVM form.
48const llvm::Type *CodeGenTypes::ConvertType(QualType T) {
Chris Lattnerfce71b82008-04-03 05:50:42 +000049 llvm::PATypeHolder Result = ConvertTypeRecursive(T);
50
51 // Any pointers that were converted defered evaluation of their pointee type,
52 // creating an opaque type instead. This is in order to avoid problems with
53 // circular types. Loop through all these defered pointees, if any, and
54 // resolve them now.
55 while (!PointersToResolve.empty()) {
Chris Lattner6f41c172010-01-11 19:58:10 +000056 std::pair<QualType, llvm::OpaqueType*> P = PointersToResolve.pop_back_val();
57
Chris Lattnerfce71b82008-04-03 05:50:42 +000058 // We can handle bare pointers here because we know that the only pointers
59 // to the Opaque type are P.second and from other types. Refining the
60 // opqaue type away will invalidate P.second, but we don't mind :).
Eli Friedman57a84fb2009-03-03 04:48:01 +000061 const llvm::Type *NT = ConvertTypeForMemRecursive(P.first);
Chris Lattnerfce71b82008-04-03 05:50:42 +000062 P.second->refineAbstractTypeTo(NT);
63 }
64
65 return Result;
66}
67
68const llvm::Type *CodeGenTypes::ConvertTypeRecursive(QualType T) {
Chris Lattner09dc6662009-04-01 02:00:48 +000069 T = Context.getCanonicalType(T);
Mike Stump1eb44332009-09-09 15:08:12 +000070
Devang Patel30ec9972007-10-25 18:32:36 +000071 // See if type is already cached.
Devang Patel0ffe89a2007-10-30 20:46:47 +000072 llvm::DenseMap<Type *, llvm::PATypeHolder>::iterator
Chris Lattner96196622008-07-26 22:37:01 +000073 I = TypeCache.find(T.getTypePtr());
Devang Patel3c400852007-12-21 19:35:28 +000074 // If type is found in map and this is not a definition for a opaque
Chris Lattnerfae6e292008-02-06 05:29:46 +000075 // place holder type then use it. Otherwise, convert type T.
Chris Lattner4581fff2008-02-06 05:21:55 +000076 if (I != TypeCache.end())
Devang Patel47c87b42007-10-30 23:22:14 +000077 return I->second.get();
Devang Patel30ec9972007-10-25 18:32:36 +000078
79 const llvm::Type *ResultType = ConvertNewType(T);
Mike Stump1eb44332009-09-09 15:08:12 +000080 TypeCache.insert(std::make_pair(T.getTypePtr(),
Chris Lattner4581fff2008-02-06 05:21:55 +000081 llvm::PATypeHolder(ResultType)));
Devang Patel30ec9972007-10-25 18:32:36 +000082 return ResultType;
83}
84
Eli Friedman57a84fb2009-03-03 04:48:01 +000085const llvm::Type *CodeGenTypes::ConvertTypeForMemRecursive(QualType T) {
86 const llvm::Type *ResultType = ConvertTypeRecursive(T);
Duncan Sandsf177d9d2010-02-15 16:14:01 +000087 if (ResultType->isIntegerTy(1))
Owen Anderson0032b272009-08-13 21:57:51 +000088 return llvm::IntegerType::get(getLLVMContext(),
89 (unsigned)Context.getTypeSize(T));
Chris Lattner6f41c172010-01-11 19:58:10 +000090 // FIXME: Should assert that the llvm type and AST type has the same size.
Eli Friedman57a84fb2009-03-03 04:48:01 +000091 return ResultType;
92}
93
Chris Lattner4581fff2008-02-06 05:21:55 +000094/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
95/// ConvertType in that it is used to convert to the memory representation for
96/// a type. For example, the scalar representation for _Bool is i1, but the
97/// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +000098const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
99 const llvm::Type *R = ConvertType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattner19009e62008-01-09 18:47:25 +0000101 // If this is a non-bool type, don't map it.
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000102 if (!R->isIntegerTy(1))
Chris Lattner19009e62008-01-09 18:47:25 +0000103 return R;
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Chris Lattner19009e62008-01-09 18:47:25 +0000105 // Otherwise, return an integer of the target-specified size.
Owen Anderson0032b272009-08-13 21:57:51 +0000106 return llvm::IntegerType::get(getLLVMContext(),
107 (unsigned)Context.getTypeSize(T));
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner19009e62008-01-09 18:47:25 +0000109}
110
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000111// Code to verify a given function type is complete, i.e. the return type
112// and all of the argument types are complete.
113static const TagType *VerifyFuncTypeComplete(const Type* T) {
114 const FunctionType *FT = cast<FunctionType>(T);
Ted Kremenek6217b802009-07-29 21:53:49 +0000115 if (const TagType* TT = FT->getResultType()->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000116 if (!TT->getDecl()->isDefinition())
117 return TT;
118 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T))
119 for (unsigned i = 0; i < FPT->getNumArgs(); i++)
Ted Kremenek6217b802009-07-29 21:53:49 +0000120 if (const TagType* TT = FPT->getArgType(i)->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000121 if (!TT->getDecl()->isDefinition())
122 return TT;
123 return 0;
124}
125
Chris Lattnerc5b88062008-02-06 05:08:19 +0000126/// UpdateCompletedType - When we find the full definition for a TagDecl,
127/// replace the 'opaque' type we previously made for it if applicable.
128void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
Mike Stumpe607ed02009-08-07 18:05:12 +0000129 const Type *Key = Context.getTagDeclType(TD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000130 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000131 TagDeclTypes.find(Key);
Chris Lattner6ef58e32008-02-06 05:12:09 +0000132 if (TDTI == TagDeclTypes.end()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattner6ef58e32008-02-06 05:12:09 +0000134 // Remember the opaque LLVM type for this tagdecl.
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000135 llvm::PATypeHolder OpaqueHolder = TDTI->second;
136 assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) &&
Chris Lattner6ef58e32008-02-06 05:12:09 +0000137 "Updating compilation of an already non-opaque type?");
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000139 // Remove it from TagDeclTypes so that it will be regenerated.
140 TagDeclTypes.erase(TDTI);
141
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000142 // Generate the new type.
143 const llvm::Type *NT = ConvertTagDeclType(TD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000144
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000145 // Refine the old opaque type to its new definition.
146 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT);
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000147
148 // Since we just completed a tag type, check to see if any function types
149 // were completed along with the tag type.
150 // FIXME: This is very inefficient; if we track which function types depend
151 // on which tag types, though, it should be reasonably efficient.
152 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator i;
153 for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) {
154 if (const TagType* TT = VerifyFuncTypeComplete(i->first)) {
155 // This function type still depends on an incomplete tag type; make sure
156 // that tag type has an associated opaque type.
157 ConvertTagDeclType(TT->getDecl());
158 } else {
159 // This function no longer depends on an incomplete tag type; create the
160 // function type, and refine the opaque type to the new function type.
161 llvm::PATypeHolder OpaqueHolder = i->second;
162 const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0));
163 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT);
164 FunctionTypes.erase(i);
165 }
166 }
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000167}
168
Mike Stump1eb44332009-09-09 15:08:12 +0000169static const llvm::Type* getTypeForFormat(llvm::LLVMContext &VMContext,
Owen Anderson0032b272009-08-13 21:57:51 +0000170 const llvm::fltSemantics &format) {
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000171 if (&format == &llvm::APFloat::IEEEsingle)
Owen Anderson0032b272009-08-13 21:57:51 +0000172 return llvm::Type::getFloatTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000173 if (&format == &llvm::APFloat::IEEEdouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000174 return llvm::Type::getDoubleTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000175 if (&format == &llvm::APFloat::IEEEquad)
Owen Anderson0032b272009-08-13 21:57:51 +0000176 return llvm::Type::getFP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000177 if (&format == &llvm::APFloat::PPCDoubleDouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000178 return llvm::Type::getPPC_FP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000179 if (&format == &llvm::APFloat::x87DoubleExtended)
Owen Anderson0032b272009-08-13 21:57:51 +0000180 return llvm::Type::getX86_FP80Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000181 assert(0 && "Unknown float format!");
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000182 return 0;
183}
184
Devang Patel30ec9972007-10-25 18:32:36 +0000185const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
John McCalle27ec8a2009-10-23 23:03:21 +0000186 const clang::Type &Ty = *Context.getCanonicalType(T).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 switch (Ty.getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000189#define TYPE(Class, Base)
190#define ABSTRACT_TYPE(Class, Base)
191#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
192#define DEPENDENT_TYPE(Class, Base) case Type::Class:
193#include "clang/AST/TypeNodes.def"
194 assert(false && "Non-canonical or dependent types aren't possible.");
195 break;
196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 case Type::Builtin: {
198 switch (cast<BuiltinType>(Ty).getKind()) {
199 case BuiltinType::Void:
Steve Naroffde2e22d2009-07-15 18:40:39 +0000200 case BuiltinType::ObjCId:
201 case BuiltinType::ObjCClass:
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000202 case BuiltinType::ObjCSel:
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 // LLVM void type can only be used as the result of a function call. Just
204 // map to the same as char.
Owen Anderson0032b272009-08-13 21:57:51 +0000205 return llvm::IntegerType::get(getLLVMContext(), 8);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206
207 case BuiltinType::Bool:
Chris Lattner19009e62008-01-09 18:47:25 +0000208 // Note that we always return bool as i1 for use as a scalar type.
Owen Anderson0032b272009-08-13 21:57:51 +0000209 return llvm::Type::getInt1Ty(getLLVMContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000211 case BuiltinType::Char_S:
212 case BuiltinType::Char_U:
213 case BuiltinType::SChar:
214 case BuiltinType::UChar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 case BuiltinType::Short:
216 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 case BuiltinType::Int:
218 case BuiltinType::UInt:
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 case BuiltinType::Long:
220 case BuiltinType::ULong:
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 case BuiltinType::LongLong:
222 case BuiltinType::ULongLong:
Argyrios Kyrtzidisafef76e2008-08-09 22:01:55 +0000223 case BuiltinType::WChar:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000224 case BuiltinType::Char16:
225 case BuiltinType::Char32:
Owen Anderson0032b272009-08-13 21:57:51 +0000226 return llvm::IntegerType::get(getLLVMContext(),
Chris Lattner98be4942008-03-05 18:54:05 +0000227 static_cast<unsigned>(Context.getTypeSize(T)));
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000229 case BuiltinType::Float:
Nate Begemanc8b12272008-04-18 05:41:31 +0000230 case BuiltinType::Double:
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 case BuiltinType::LongDouble:
Mike Stump1eb44332009-09-09 15:08:12 +0000232 return getTypeForFormat(getLLVMContext(),
Owen Anderson0032b272009-08-13 21:57:51 +0000233 Context.getFloatTypeSemantics(T));
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000235 case BuiltinType::NullPtr: {
236 // Model std::nullptr_t as i8*
237 const llvm::Type *Ty = llvm::IntegerType::get(getLLVMContext(), 8);
238 return llvm::PointerType::getUnqual(Ty);
239 }
240
Chris Lattner2df9ced2009-04-30 02:43:43 +0000241 case BuiltinType::UInt128:
242 case BuiltinType::Int128:
Owen Anderson0032b272009-08-13 21:57:51 +0000243 return llvm::IntegerType::get(getLLVMContext(), 128);
Eli Friedman8c692352009-12-18 23:28:34 +0000244
245 case BuiltinType::Overload:
246 case BuiltinType::Dependent:
247 case BuiltinType::UndeducedAuto:
248 assert(0 && "Unexpected builtin type!");
249 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 }
Eli Friedman8c692352009-12-18 23:28:34 +0000251 assert(0 && "Unknown builtin type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 break;
253 }
254 case Type::Complex: {
Mike Stump1eb44332009-09-09 15:08:12 +0000255 const llvm::Type *EltTy =
Chris Lattnerfce71b82008-04-03 05:50:42 +0000256 ConvertTypeRecursive(cast<ComplexType>(Ty).getElementType());
Owen Anderson47a434f2009-08-05 23:18:46 +0000257 return llvm::StructType::get(TheModule.getContext(), EltTy, EltTy, NULL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000259 case Type::LValueReference:
260 case Type::RValueReference: {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000261 const ReferenceType &RTy = cast<ReferenceType>(Ty);
262 QualType ETy = RTy.getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000263 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000264 PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
265 return llvm::PointerType::get(PointeeType, ETy.getAddressSpace());
266 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 case Type::Pointer: {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000268 const PointerType &PTy = cast<PointerType>(Ty);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000269 QualType ETy = PTy.getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000270 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000271 PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
Chris Lattnerfce71b82008-04-03 05:50:42 +0000272 return llvm::PointerType::get(PointeeType, ETy.getAddressSpace());
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Steve Narofffb22d962007-08-30 01:06:46 +0000275 case Type::VariableArray: {
276 const VariableArrayType &A = cast<VariableArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +0000277 assert(A.getIndexTypeCVRQualifiers() == 0 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 "FIXME: We only handle trivial array types so far!");
Eli Friedmanc5773c42008-02-15 18:16:39 +0000279 // VLAs resolve to the innermost element type; this matches
280 // the return of alloca, and there isn't any obviously better choice.
Eli Friedman57a84fb2009-03-03 04:48:01 +0000281 return ConvertTypeForMemRecursive(A.getElementType());
Eli Friedmanc5773c42008-02-15 18:16:39 +0000282 }
283 case Type::IncompleteArray: {
284 const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +0000285 assert(A.getIndexTypeCVRQualifiers() == 0 &&
Eli Friedmanc5773c42008-02-15 18:16:39 +0000286 "FIXME: We only handle trivial array types so far!");
287 // int X[] -> [0 x int]
Eli Friedman57a84fb2009-03-03 04:48:01 +0000288 return llvm::ArrayType::get(ConvertTypeForMemRecursive(A.getElementType()), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 }
Steve Narofffb22d962007-08-30 01:06:46 +0000290 case Type::ConstantArray: {
291 const ConstantArrayType &A = cast<ConstantArrayType>(Ty);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000292 const llvm::Type *EltTy = ConvertTypeForMemRecursive(A.getElementType());
Steve Narofffb22d962007-08-30 01:06:46 +0000293 return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue());
294 }
Nate Begeman213541a2008-04-18 23:10:10 +0000295 case Type::ExtVector:
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 case Type::Vector: {
297 const VectorType &VT = cast<VectorType>(Ty);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000298 return llvm::VectorType::get(ConvertTypeRecursive(VT.getElementType()),
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 VT.getNumElements());
300 }
301 case Type::FunctionNoProto:
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000302 case Type::FunctionProto: {
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000303 // First, check whether we can build the full function type.
304 if (const TagType* TT = VerifyFuncTypeComplete(&Ty)) {
305 // This function's type depends on an incomplete tag type; make sure
306 // we have an opaque type corresponding to the tag type.
307 ConvertTagDeclType(TT->getDecl());
308 // Create an opaque type for this function type, save it, and return it.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000309 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000310 FunctionTypes.insert(std::make_pair(&Ty, ResultType));
311 return ResultType;
312 }
313 // The function type can be built; call the appropriate routines to
314 // build it.
Chris Lattner9a1a9c42009-03-31 08:55:07 +0000315 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(&Ty))
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000316 return GetFunctionType(getFunctionInfo(FPT), FPT->isVariadic());
Chris Lattner9a1a9c42009-03-31 08:55:07 +0000317
318 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(&Ty);
319 return GetFunctionType(getFunctionInfo(FNPT), true);
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattner391d77a2008-03-30 23:03:07 +0000322 case Type::ObjCInterface: {
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000323 // Objective-C interfaces are always opaque (outside of the
324 // runtime, which can do whatever it likes); we never refine
325 // these.
326 const llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(&Ty)];
327 if (!T)
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000328 T = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000329 return T;
Chris Lattner391d77a2008-03-30 23:03:07 +0000330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Steve Naroff14108da2009-07-10 23:34:53 +0000332 case Type::ObjCObjectPointer: {
Daniel Dunbar28e47802009-07-11 21:12:14 +0000333 // Protocol qualifications do not influence the LLVM type, we just return a
334 // pointer to the underlying interface type. We don't need to worry about
335 // recursive conversion.
Mike Stump1eb44332009-09-09 15:08:12 +0000336 const llvm::Type *T =
Daniel Dunbar28e47802009-07-11 21:12:14 +0000337 ConvertTypeRecursive(cast<ObjCObjectPointerType>(Ty).getPointeeType());
338 return llvm::PointerType::getUnqual(T);
Steve Naroff14108da2009-07-10 23:34:53 +0000339 }
Daniel Dunbar28e47802009-07-11 21:12:14 +0000340
Douglas Gregor72564e72009-02-26 23:50:07 +0000341 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000342 case Type::Enum: {
Chris Lattnerde0efb32008-02-06 05:48:29 +0000343 const TagDecl *TD = cast<TagType>(Ty).getDecl();
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000344 const llvm::Type *Res = ConvertTagDeclType(TD);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnerde0efb32008-02-06 05:48:29 +0000346 std::string TypeName(TD->getKindName());
347 TypeName += '.';
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Chris Lattnerde0efb32008-02-06 05:48:29 +0000349 // Name the codegen type after the typedef name
350 // if there is no tag type name available
351 if (TD->getIdentifier())
Anders Carlsson6b7fc132009-09-26 19:03:24 +0000352 // FIXME: We should not have to check for a null decl context here.
353 // Right now we do it because the implicit Obj-C decls don't have one.
354 TypeName += TD->getDeclContext() ? TD->getQualifiedNameAsString() :
355 TD->getNameAsString();
Chris Lattnerde0efb32008-02-06 05:48:29 +0000356 else if (const TypedefType *TdT = dyn_cast<TypedefType>(T))
Anders Carlsson6b7fc132009-09-26 19:03:24 +0000357 // FIXME: We should not have to check for a null decl context here.
358 // Right now we do it because the implicit Obj-C decls don't have one.
359 TypeName += TdT->getDecl()->getDeclContext() ?
360 TdT->getDecl()->getQualifiedNameAsString() :
361 TdT->getDecl()->getNameAsString();
Chris Lattnerde0efb32008-02-06 05:48:29 +0000362 else
363 TypeName += "anon";
Mike Stump1eb44332009-09-09 15:08:12 +0000364
365 TheModule.addTypeName(TypeName, Res);
Chris Lattnerde0efb32008-02-06 05:48:29 +0000366 return Res;
367 }
Daniel Dunbar90488912008-08-28 18:02:04 +0000368
369 case Type::BlockPointer: {
Daniel Dunbar4e174f12009-01-09 02:48:46 +0000370 const QualType FTy = cast<BlockPointerType>(Ty).getPointeeType();
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000371 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
Fariborz Jahanian209bb432009-03-13 20:36:41 +0000372 PointersToResolve.push_back(std::make_pair(FTy, PointeeType));
373 return llvm::PointerType::get(PointeeType, FTy.getAddressSpace());
Daniel Dunbar90488912008-08-28 18:02:04 +0000374 }
Sebastian Redl424c51d2009-01-25 13:35:30 +0000375
Anders Carlsson0e650012009-05-17 17:41:20 +0000376 case Type::MemberPointer: {
377 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
378 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
379 // If we ever want to support other ABIs this needs to be abstracted.
380
381 QualType ETy = cast<MemberPointerType>(Ty).getPointeeType();
Eli Friedman4a2251b2009-12-03 12:44:31 +0000382 const llvm::Type *PtrDiffTy =
383 ConvertTypeRecursive(Context.getPointerDiffType());
Chris Lattner6f41c172010-01-11 19:58:10 +0000384 if (ETy->isFunctionType())
Eli Friedman4a2251b2009-12-03 12:44:31 +0000385 return llvm::StructType::get(TheModule.getContext(), PtrDiffTy, PtrDiffTy,
Anders Carlsson0e650012009-05-17 17:41:20 +0000386 NULL);
Chris Lattner6f41c172010-01-11 19:58:10 +0000387 return PtrDiffTy;
Anders Carlsson0e650012009-05-17 17:41:20 +0000388 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000389
390 case Type::TemplateSpecialization:
391 assert(false && "Dependent types can't get here");
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 // FIXME: implement.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000395 return llvm::OpaqueType::get(getLLVMContext());
Reid Spencer5f016e22007-07-11 17:01:13 +0000396}
397
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000398/// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
399/// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000400const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000401
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000402 // TagDecl's are not necessarily unique, instead use the (clang)
403 // type connected to the decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000404 const Type *Key =
Mike Stumpe607ed02009-08-07 18:05:12 +0000405 Context.getTagDeclType(TD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000406 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000407 TagDeclTypes.find(Key);
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattner5de00fc2008-02-06 06:03:51 +0000409 // If we've already compiled this tag type, use the previous definition.
410 if (TDTI != TagDeclTypes.end())
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000411 return TDTI->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
John McCall5cfa0112010-02-05 01:33:36 +0000413 // If this is still a forward declaration, just define an opaque
414 // type to use for this tagged decl.
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000415 if (!TD->isDefinition()) {
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000416 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000417 TagDeclTypes.insert(std::make_pair(Key, ResultType));
Chris Lattner5de00fc2008-02-06 06:03:51 +0000418 return ResultType;
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattner5de00fc2008-02-06 06:03:51 +0000421 // Okay, this is a definition of a type. Compile the implementation now.
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattner6f41c172010-01-11 19:58:10 +0000423 if (TD->isEnum()) // Don't bother storing enums in TagDeclTypes.
Chris Lattnerfce71b82008-04-03 05:50:42 +0000424 return ConvertTypeRecursive(cast<EnumDecl>(TD)->getIntegerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner5de00fc2008-02-06 06:03:51 +0000426 // This decl could well be recursive. In this case, insert an opaque
427 // definition of this type, which the recursive uses will get. We will then
428 // refine this opaque version later.
429
430 // Create new OpaqueType now for later use in case this is a recursive
431 // type. This will later be refined to the actual type.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000432 llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000433 TagDeclTypes.insert(std::make_pair(Key, ResultHolder));
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Chris Lattner5de00fc2008-02-06 06:03:51 +0000435 const RecordDecl *RD = cast<const RecordDecl>(TD);
Daniel Dunbarae287232009-04-22 08:50:59 +0000436
John McCall86ff3082010-02-04 22:26:26 +0000437 // Force conversion of non-virtual base classes recursively.
438 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) {
439 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
440 e = RD->bases_end(); i != e; ++i) {
441 if (!i->isVirtual()) {
442 const CXXRecordDecl *Base =
443 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
444 ConvertTagDeclType(Base);
445 }
446 }
447 }
448
Anders Carlsson696798f2009-07-27 17:10:54 +0000449 // Layout fields.
Chris Lattner6f41c172010-01-11 19:58:10 +0000450 CGRecordLayout *Layout = CGRecordLayoutBuilder::ComputeLayout(*this, RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Anders Carlsson696798f2009-07-27 17:10:54 +0000452 CGRecordLayouts[Key] = Layout;
Chris Lattner6f41c172010-01-11 19:58:10 +0000453 const llvm::Type *ResultType = Layout->getLLVMType();
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Chris Lattner5de00fc2008-02-06 06:03:51 +0000455 // Refine our Opaque type to ResultType. This can invalidate ResultType, so
456 // make sure to read the result out of the holder.
457 cast<llvm::OpaqueType>(ResultHolder.get())
458 ->refineAbstractTypeTo(ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattner5de00fc2008-02-06 06:03:51 +0000460 return ResultHolder.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000461}
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000462
Devang Patelb84a06e2007-10-23 02:10:49 +0000463/// getLLVMFieldNo - Return llvm::StructType element number
464/// that corresponds to the field FD.
465unsigned CodeGenTypes::getLLVMFieldNo(const FieldDecl *FD) {
Anders Carlsson8330cee2009-07-23 17:01:21 +0000466 assert(!FD->isBitField() && "Don't use getLLVMFieldNo on bit fields!");
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattnerce5605e2008-03-30 23:25:33 +0000468 llvm::DenseMap<const FieldDecl*, unsigned>::iterator I = FieldInfo.find(FD);
Hartmut Kaiser21fdf412007-10-24 00:07:36 +0000469 assert (I != FieldInfo.end() && "Unable to find field info");
Devang Patelb84a06e2007-10-23 02:10:49 +0000470 return I->second;
471}
472
Devang Patelc4c429a2007-10-24 00:56:23 +0000473/// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000474void CodeGenTypes::addFieldInfo(const FieldDecl *FD, unsigned No) {
475 FieldInfo[FD] = No;
476}
477
478/// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field FD.
479CodeGenTypes::BitFieldInfo CodeGenTypes::getBitFieldInfo(const FieldDecl *FD) {
480 llvm::DenseMap<const FieldDecl *, BitFieldInfo>::iterator
481 I = BitFields.find(FD);
482 assert (I != BitFields.end() && "Unable to find bitfield info");
483 return I->second;
484}
485
486/// addBitFieldInfo - Assign a start bit and a size to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000487void CodeGenTypes::addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
488 unsigned Start, unsigned Size) {
489 BitFields.insert(std::make_pair(FD, BitFieldInfo(FieldNo, Start, Size)));
Devang Patelb84a06e2007-10-23 02:10:49 +0000490}
491
Devang Patel88a981b2007-11-01 19:11:01 +0000492/// getCGRecordLayout - Return record layout info for the given llvm::Type.
Anders Carlssonad3e7112009-08-24 17:16:23 +0000493const CGRecordLayout &
Chris Lattneraf319132008-02-05 06:55:31 +0000494CodeGenTypes::getCGRecordLayout(const TagDecl *TD) const {
Chris Lattner6f41c172010-01-11 19:58:10 +0000495 const Type *Key = Context.getTagDeclType(TD).getTypePtr();
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000496 llvm::DenseMap<const Type*, CGRecordLayout *>::const_iterator I
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000497 = CGRecordLayouts.find(Key);
Mike Stump1eb44332009-09-09 15:08:12 +0000498 assert (I != CGRecordLayouts.end()
Devang Patelb84a06e2007-10-23 02:10:49 +0000499 && "Unable to find record layout information for type");
Anders Carlssonad3e7112009-08-24 17:16:23 +0000500 return *I->second;
Devang Patelb84a06e2007-10-23 02:10:49 +0000501}