blob: c89879fdb6c40c45f6ead6bbc52a7cb1ebd764f7 [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,
31 const llvm::TargetData &TD)
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000032 : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD),
33 TheABIInfo(0) {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000034}
Reid Spencer5f016e22007-07-11 17:01:13 +000035
Devang Patelb84a06e2007-10-23 02:10:49 +000036CodeGenTypes::~CodeGenTypes() {
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;
Eli Friedmanf8f18932009-11-20 05:53:06 +000041 {
42 llvm::FoldingSet<CGFunctionInfo>::iterator
43 I = FunctionInfos.begin(), E = FunctionInfos.end();
44 while (I != E)
45 delete &*I++;
46 }
47 delete TheABIInfo;
Devang Patelb84a06e2007-10-23 02:10:49 +000048}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050/// ConvertType - Convert the specified type to its LLVM form.
51const llvm::Type *CodeGenTypes::ConvertType(QualType T) {
Chris Lattnerfce71b82008-04-03 05:50:42 +000052 llvm::PATypeHolder Result = ConvertTypeRecursive(T);
53
54 // Any pointers that were converted defered evaluation of their pointee type,
55 // creating an opaque type instead. This is in order to avoid problems with
56 // circular types. Loop through all these defered pointees, if any, and
57 // resolve them now.
58 while (!PointersToResolve.empty()) {
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000059 std::pair<QualType, llvm::OpaqueType*> P =
Chris Lattnerfce71b82008-04-03 05:50:42 +000060 PointersToResolve.back();
61 PointersToResolve.pop_back();
62 // We can handle bare pointers here because we know that the only pointers
63 // to the Opaque type are P.second and from other types. Refining the
64 // opqaue type away will invalidate P.second, but we don't mind :).
Eli Friedman57a84fb2009-03-03 04:48:01 +000065 const llvm::Type *NT = ConvertTypeForMemRecursive(P.first);
Chris Lattnerfce71b82008-04-03 05:50:42 +000066 P.second->refineAbstractTypeTo(NT);
67 }
68
69 return Result;
70}
71
72const llvm::Type *CodeGenTypes::ConvertTypeRecursive(QualType T) {
Chris Lattner09dc6662009-04-01 02:00:48 +000073 T = Context.getCanonicalType(T);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Devang Patel30ec9972007-10-25 18:32:36 +000075 // See if type is already cached.
Devang Patel0ffe89a2007-10-30 20:46:47 +000076 llvm::DenseMap<Type *, llvm::PATypeHolder>::iterator
Chris Lattner96196622008-07-26 22:37:01 +000077 I = TypeCache.find(T.getTypePtr());
Devang Patel3c400852007-12-21 19:35:28 +000078 // If type is found in map and this is not a definition for a opaque
Chris Lattnerfae6e292008-02-06 05:29:46 +000079 // place holder type then use it. Otherwise, convert type T.
Chris Lattner4581fff2008-02-06 05:21:55 +000080 if (I != TypeCache.end())
Devang Patel47c87b42007-10-30 23:22:14 +000081 return I->second.get();
Devang Patel30ec9972007-10-25 18:32:36 +000082
83 const llvm::Type *ResultType = ConvertNewType(T);
Mike Stump1eb44332009-09-09 15:08:12 +000084 TypeCache.insert(std::make_pair(T.getTypePtr(),
Chris Lattner4581fff2008-02-06 05:21:55 +000085 llvm::PATypeHolder(ResultType)));
Devang Patel30ec9972007-10-25 18:32:36 +000086 return ResultType;
87}
88
Eli Friedman57a84fb2009-03-03 04:48:01 +000089const llvm::Type *CodeGenTypes::ConvertTypeForMemRecursive(QualType T) {
90 const llvm::Type *ResultType = ConvertTypeRecursive(T);
Owen Anderson0032b272009-08-13 21:57:51 +000091 if (ResultType == llvm::Type::getInt1Ty(getLLVMContext()))
92 return llvm::IntegerType::get(getLLVMContext(),
93 (unsigned)Context.getTypeSize(T));
Eli Friedman57a84fb2009-03-03 04:48:01 +000094 return ResultType;
95}
96
Chris Lattner4581fff2008-02-06 05:21:55 +000097/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
98/// ConvertType in that it is used to convert to the memory representation for
99/// a type. For example, the scalar representation for _Bool is i1, but the
100/// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000101const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
102 const llvm::Type *R = ConvertType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattner19009e62008-01-09 18:47:25 +0000104 // If this is a non-bool type, don't map it.
Owen Anderson0032b272009-08-13 21:57:51 +0000105 if (R != llvm::Type::getInt1Ty(getLLVMContext()))
Chris Lattner19009e62008-01-09 18:47:25 +0000106 return R;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattner19009e62008-01-09 18:47:25 +0000108 // Otherwise, return an integer of the target-specified size.
Owen Anderson0032b272009-08-13 21:57:51 +0000109 return llvm::IntegerType::get(getLLVMContext(),
110 (unsigned)Context.getTypeSize(T));
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner19009e62008-01-09 18:47:25 +0000112}
113
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000114// Code to verify a given function type is complete, i.e. the return type
115// and all of the argument types are complete.
116static const TagType *VerifyFuncTypeComplete(const Type* T) {
117 const FunctionType *FT = cast<FunctionType>(T);
Ted Kremenek6217b802009-07-29 21:53:49 +0000118 if (const TagType* TT = FT->getResultType()->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000119 if (!TT->getDecl()->isDefinition())
120 return TT;
121 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T))
122 for (unsigned i = 0; i < FPT->getNumArgs(); i++)
Ted Kremenek6217b802009-07-29 21:53:49 +0000123 if (const TagType* TT = FPT->getArgType(i)->getAs<TagType>())
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000124 if (!TT->getDecl()->isDefinition())
125 return TT;
126 return 0;
127}
128
Chris Lattnerc5b88062008-02-06 05:08:19 +0000129/// UpdateCompletedType - When we find the full definition for a TagDecl,
130/// replace the 'opaque' type we previously made for it if applicable.
131void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
Mike Stumpe607ed02009-08-07 18:05:12 +0000132 const Type *Key = Context.getTagDeclType(TD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000133 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000134 TagDeclTypes.find(Key);
Chris Lattner6ef58e32008-02-06 05:12:09 +0000135 if (TDTI == TagDeclTypes.end()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattner6ef58e32008-02-06 05:12:09 +0000137 // Remember the opaque LLVM type for this tagdecl.
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000138 llvm::PATypeHolder OpaqueHolder = TDTI->second;
139 assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) &&
Chris Lattner6ef58e32008-02-06 05:12:09 +0000140 "Updating compilation of an already non-opaque type?");
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000142 // Remove it from TagDeclTypes so that it will be regenerated.
143 TagDeclTypes.erase(TDTI);
144
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000145 // Generate the new type.
146 const llvm::Type *NT = ConvertTagDeclType(TD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000147
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000148 // Refine the old opaque type to its new definition.
149 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT);
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000150
151 // Since we just completed a tag type, check to see if any function types
152 // were completed along with the tag type.
153 // FIXME: This is very inefficient; if we track which function types depend
154 // on which tag types, though, it should be reasonably efficient.
155 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator i;
156 for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) {
157 if (const TagType* TT = VerifyFuncTypeComplete(i->first)) {
158 // This function type still depends on an incomplete tag type; make sure
159 // that tag type has an associated opaque type.
160 ConvertTagDeclType(TT->getDecl());
161 } else {
162 // This function no longer depends on an incomplete tag type; create the
163 // function type, and refine the opaque type to the new function type.
164 llvm::PATypeHolder OpaqueHolder = i->second;
165 const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0));
166 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT);
167 FunctionTypes.erase(i);
168 }
169 }
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000170}
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172static const llvm::Type* getTypeForFormat(llvm::LLVMContext &VMContext,
Owen Anderson0032b272009-08-13 21:57:51 +0000173 const llvm::fltSemantics &format) {
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000174 if (&format == &llvm::APFloat::IEEEsingle)
Owen Anderson0032b272009-08-13 21:57:51 +0000175 return llvm::Type::getFloatTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000176 if (&format == &llvm::APFloat::IEEEdouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000177 return llvm::Type::getDoubleTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000178 if (&format == &llvm::APFloat::IEEEquad)
Owen Anderson0032b272009-08-13 21:57:51 +0000179 return llvm::Type::getFP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000180 if (&format == &llvm::APFloat::PPCDoubleDouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000181 return llvm::Type::getPPC_FP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000182 if (&format == &llvm::APFloat::x87DoubleExtended)
Owen Anderson0032b272009-08-13 21:57:51 +0000183 return llvm::Type::getX86_FP80Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000184 assert(0 && "Unknown float format!");
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000185 return 0;
186}
187
Devang Patel30ec9972007-10-25 18:32:36 +0000188const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
John McCalle27ec8a2009-10-23 23:03:21 +0000189 const clang::Type &Ty = *Context.getCanonicalType(T).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 switch (Ty.getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000192#define TYPE(Class, Base)
193#define ABSTRACT_TYPE(Class, Base)
194#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
195#define DEPENDENT_TYPE(Class, Base) case Type::Class:
196#include "clang/AST/TypeNodes.def"
197 assert(false && "Non-canonical or dependent types aren't possible.");
198 break;
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 case Type::Builtin: {
201 switch (cast<BuiltinType>(Ty).getKind()) {
Argyrios Kyrtzidisafef76e2008-08-09 22:01:55 +0000202 default: assert(0 && "Unknown builtin type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 case BuiltinType::Void:
Steve Naroffde2e22d2009-07-15 18:40:39 +0000204 case BuiltinType::ObjCId:
205 case BuiltinType::ObjCClass:
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000206 case BuiltinType::ObjCSel:
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 // LLVM void type can only be used as the result of a function call. Just
208 // map to the same as char.
Owen Anderson0032b272009-08-13 21:57:51 +0000209 return llvm::IntegerType::get(getLLVMContext(), 8);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210
211 case BuiltinType::Bool:
Chris Lattner19009e62008-01-09 18:47:25 +0000212 // Note that we always return bool as i1 for use as a scalar type.
Owen Anderson0032b272009-08-13 21:57:51 +0000213 return llvm::Type::getInt1Ty(getLLVMContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000215 case BuiltinType::Char_S:
216 case BuiltinType::Char_U:
217 case BuiltinType::SChar:
218 case BuiltinType::UChar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 case BuiltinType::Short:
220 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 case BuiltinType::Int:
222 case BuiltinType::UInt:
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 case BuiltinType::Long:
224 case BuiltinType::ULong:
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 case BuiltinType::LongLong:
226 case BuiltinType::ULongLong:
Argyrios Kyrtzidisafef76e2008-08-09 22:01:55 +0000227 case BuiltinType::WChar:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000228 case BuiltinType::Char16:
229 case BuiltinType::Char32:
Owen Anderson0032b272009-08-13 21:57:51 +0000230 return llvm::IntegerType::get(getLLVMContext(),
Chris Lattner98be4942008-03-05 18:54:05 +0000231 static_cast<unsigned>(Context.getTypeSize(T)));
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000233 case BuiltinType::Float:
Nate Begemanc8b12272008-04-18 05:41:31 +0000234 case BuiltinType::Double:
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 case BuiltinType::LongDouble:
Mike Stump1eb44332009-09-09 15:08:12 +0000236 return getTypeForFormat(getLLVMContext(),
Owen Anderson0032b272009-08-13 21:57:51 +0000237 Context.getFloatTypeSemantics(T));
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000239 case BuiltinType::NullPtr: {
240 // Model std::nullptr_t as i8*
241 const llvm::Type *Ty = llvm::IntegerType::get(getLLVMContext(), 8);
242 return llvm::PointerType::getUnqual(Ty);
243 }
244
Chris Lattner2df9ced2009-04-30 02:43:43 +0000245 case BuiltinType::UInt128:
246 case BuiltinType::Int128:
Owen Anderson0032b272009-08-13 21:57:51 +0000247 return llvm::IntegerType::get(getLLVMContext(), 128);
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 }
249 break;
250 }
Eli Friedmanf98aba32009-02-13 02:31:07 +0000251 case Type::FixedWidthInt:
Mike Stump1eb44332009-09-09 15:08:12 +0000252 return llvm::IntegerType::get(getLLVMContext(),
Owen Anderson0032b272009-08-13 21:57:51 +0000253 cast<FixedWidthIntType>(T)->getWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 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();
382 if (ETy->isFunctionType()) {
Owen Anderson47a434f2009-08-05 23:18:46 +0000383 return llvm::StructType::get(TheModule.getContext(),
Mike Stump1eb44332009-09-09 15:08:12 +0000384 ConvertType(Context.getPointerDiffType()),
Anders Carlsson0e650012009-05-17 17:41:20 +0000385 ConvertType(Context.getPointerDiffType()),
386 NULL);
387 } else
388 return ConvertType(Context.getPointerDiffType());
389 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000390
391 case Type::TemplateSpecialization:
392 assert(false && "Dependent types can't get here");
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 // FIXME: implement.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000396 return llvm::OpaqueType::get(getLLVMContext());
Reid Spencer5f016e22007-07-11 17:01:13 +0000397}
398
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000399/// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
400/// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000401const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000402
403 // FIXME. This may have to move to a better place.
404 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) {
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000405 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
406 e = RD->bases_end(); i != e; ++i) {
407 if (!i->isVirtual()) {
408 const CXXRecordDecl *Base =
Ted Kremenek6217b802009-07-29 21:53:49 +0000409 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000410 ConvertTagDeclType(Base);
411 }
412 }
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000415 // TagDecl's are not necessarily unique, instead use the (clang)
416 // type connected to the decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000417 const Type *Key =
Mike Stumpe607ed02009-08-07 18:05:12 +0000418 Context.getTagDeclType(TD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000419 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000420 TagDeclTypes.find(Key);
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner5de00fc2008-02-06 06:03:51 +0000422 // If we've already compiled this tag type, use the previous definition.
423 if (TDTI != TagDeclTypes.end())
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000424 return TDTI->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner5de00fc2008-02-06 06:03:51 +0000426 // If this is still a forward definition, just define an opaque type to use
427 // for this tagged decl.
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000428 if (!TD->isDefinition()) {
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000429 llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000430 TagDeclTypes.insert(std::make_pair(Key, ResultType));
Chris Lattner5de00fc2008-02-06 06:03:51 +0000431 return ResultType;
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattner5de00fc2008-02-06 06:03:51 +0000434 // Okay, this is a definition of a type. Compile the implementation now.
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000436 if (TD->isEnum()) {
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000437 // Don't bother storing enums in TagDeclTypes.
Chris Lattnerfce71b82008-04-03 05:50:42 +0000438 return ConvertTypeRecursive(cast<EnumDecl>(TD)->getIntegerType());
Chris Lattner5de00fc2008-02-06 06:03:51 +0000439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chris Lattner5de00fc2008-02-06 06:03:51 +0000441 // This decl could well be recursive. In this case, insert an opaque
442 // definition of this type, which the recursive uses will get. We will then
443 // refine this opaque version later.
444
445 // Create new OpaqueType now for later use in case this is a recursive
446 // type. This will later be refined to the actual type.
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000447 llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(getLLVMContext());
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000448 TagDeclTypes.insert(std::make_pair(Key, ResultHolder));
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner5de00fc2008-02-06 06:03:51 +0000450 const llvm::Type *ResultType;
451 const RecordDecl *RD = cast<const RecordDecl>(TD);
Daniel Dunbarae287232009-04-22 08:50:59 +0000452
Anders Carlsson696798f2009-07-27 17:10:54 +0000453 // Layout fields.
Mike Stump1eb44332009-09-09 15:08:12 +0000454 CGRecordLayout *Layout =
Anders Carlsson696798f2009-07-27 17:10:54 +0000455 CGRecordLayoutBuilder::ComputeLayout(*this, RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Anders Carlsson696798f2009-07-27 17:10:54 +0000457 CGRecordLayouts[Key] = Layout;
458 ResultType = Layout->getLLVMType();
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattner5de00fc2008-02-06 06:03:51 +0000460 // Refine our Opaque type to ResultType. This can invalidate ResultType, so
461 // make sure to read the result out of the holder.
462 cast<llvm::OpaqueType>(ResultHolder.get())
463 ->refineAbstractTypeTo(ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattner5de00fc2008-02-06 06:03:51 +0000465 return ResultHolder.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000466}
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000467
Devang Patelb84a06e2007-10-23 02:10:49 +0000468/// getLLVMFieldNo - Return llvm::StructType element number
469/// that corresponds to the field FD.
470unsigned CodeGenTypes::getLLVMFieldNo(const FieldDecl *FD) {
Anders Carlsson8330cee2009-07-23 17:01:21 +0000471 assert(!FD->isBitField() && "Don't use getLLVMFieldNo on bit fields!");
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattnerce5605e2008-03-30 23:25:33 +0000473 llvm::DenseMap<const FieldDecl*, unsigned>::iterator I = FieldInfo.find(FD);
Hartmut Kaiser21fdf412007-10-24 00:07:36 +0000474 assert (I != FieldInfo.end() && "Unable to find field info");
Devang Patelb84a06e2007-10-23 02:10:49 +0000475 return I->second;
476}
477
Devang Patelc4c429a2007-10-24 00:56:23 +0000478/// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000479void CodeGenTypes::addFieldInfo(const FieldDecl *FD, unsigned No) {
480 FieldInfo[FD] = No;
481}
482
483/// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field FD.
484CodeGenTypes::BitFieldInfo CodeGenTypes::getBitFieldInfo(const FieldDecl *FD) {
485 llvm::DenseMap<const FieldDecl *, BitFieldInfo>::iterator
486 I = BitFields.find(FD);
487 assert (I != BitFields.end() && "Unable to find bitfield info");
488 return I->second;
489}
490
491/// addBitFieldInfo - Assign a start bit and a size to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000492void CodeGenTypes::addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
493 unsigned Start, unsigned Size) {
494 BitFields.insert(std::make_pair(FD, BitFieldInfo(FieldNo, Start, Size)));
Devang Patelb84a06e2007-10-23 02:10:49 +0000495}
496
Devang Patel88a981b2007-11-01 19:11:01 +0000497/// getCGRecordLayout - Return record layout info for the given llvm::Type.
Anders Carlssonad3e7112009-08-24 17:16:23 +0000498const CGRecordLayout &
Chris Lattneraf319132008-02-05 06:55:31 +0000499CodeGenTypes::getCGRecordLayout(const TagDecl *TD) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000500 const Type *Key =
Mike Stumpe607ed02009-08-07 18:05:12 +0000501 Context.getTagDeclType(TD).getTypePtr();
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000502 llvm::DenseMap<const Type*, CGRecordLayout *>::const_iterator I
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000503 = CGRecordLayouts.find(Key);
Mike Stump1eb44332009-09-09 15:08:12 +0000504 assert (I != CGRecordLayouts.end()
Devang Patelb84a06e2007-10-23 02:10:49 +0000505 && "Unable to find record layout information for type");
Anders Carlssonad3e7112009-08-24 17:16:23 +0000506 return *I->second;
Devang Patelb84a06e2007-10-23 02:10:49 +0000507}