Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This is the code that handles AST -> LLVM type lowering. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenTypes.h" |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 15 | #include "CGCall.h" |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 16 | #include "CGRecordLayout.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Devang Patel | d9e9ede | 2007-10-31 20:08:22 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 28 | CodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M, |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 29 | const llvm::TargetData &TD, const ABIInfo &Info) |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 30 | : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD), |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 31 | TheABIInfo(Info) { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 32 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 34 | CodeGenTypes::~CodeGenTypes() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator |
| 36 | I = CGRecordLayouts.begin(), E = CGRecordLayouts.end(); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 37 | I != E; ++I) |
| 38 | delete I->second; |
Chris Lattner | 6f41c17 | 2010-01-11 19:58:10 +0000 | [diff] [blame] | 39 | |
| 40 | for (llvm::FoldingSet<CGFunctionInfo>::iterator |
| 41 | I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; ) |
| 42 | delete &*I++; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | /// ConvertType - Convert the specified type to its LLVM form. |
| 46 | const llvm::Type *CodeGenTypes::ConvertType(QualType T) { |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 47 | llvm::PATypeHolder Result = ConvertTypeRecursive(T); |
| 48 | |
| 49 | // Any pointers that were converted defered evaluation of their pointee type, |
| 50 | // creating an opaque type instead. This is in order to avoid problems with |
| 51 | // circular types. Loop through all these defered pointees, if any, and |
| 52 | // resolve them now. |
| 53 | while (!PointersToResolve.empty()) { |
Chris Lattner | 6f41c17 | 2010-01-11 19:58:10 +0000 | [diff] [blame] | 54 | std::pair<QualType, llvm::OpaqueType*> P = PointersToResolve.pop_back_val(); |
| 55 | |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 56 | // We can handle bare pointers here because we know that the only pointers |
| 57 | // to the Opaque type are P.second and from other types. Refining the |
| 58 | // opqaue type away will invalidate P.second, but we don't mind :). |
Eli Friedman | 57a84fb | 2009-03-03 04:48:01 +0000 | [diff] [blame] | 59 | const llvm::Type *NT = ConvertTypeForMemRecursive(P.first); |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 60 | P.second->refineAbstractTypeTo(NT); |
| 61 | } |
| 62 | |
| 63 | return Result; |
| 64 | } |
| 65 | |
| 66 | const llvm::Type *CodeGenTypes::ConvertTypeRecursive(QualType T) { |
Chris Lattner | 09dc666 | 2009-04-01 02:00:48 +0000 | [diff] [blame] | 67 | T = Context.getCanonicalType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 69 | // See if type is already cached. |
Devang Patel | 0ffe89a | 2007-10-30 20:46:47 +0000 | [diff] [blame] | 70 | llvm::DenseMap<Type *, llvm::PATypeHolder>::iterator |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 71 | I = TypeCache.find(T.getTypePtr()); |
Devang Patel | 3c40085 | 2007-12-21 19:35:28 +0000 | [diff] [blame] | 72 | // If type is found in map and this is not a definition for a opaque |
Chris Lattner | fae6e29 | 2008-02-06 05:29:46 +0000 | [diff] [blame] | 73 | // place holder type then use it. Otherwise, convert type T. |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 74 | if (I != TypeCache.end()) |
Devang Patel | 47c87b4 | 2007-10-30 23:22:14 +0000 | [diff] [blame] | 75 | return I->second.get(); |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 76 | |
| 77 | const llvm::Type *ResultType = ConvertNewType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | TypeCache.insert(std::make_pair(T.getTypePtr(), |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 79 | llvm::PATypeHolder(ResultType))); |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 80 | return ResultType; |
| 81 | } |
| 82 | |
Eli Friedman | 57a84fb | 2009-03-03 04:48:01 +0000 | [diff] [blame] | 83 | const llvm::Type *CodeGenTypes::ConvertTypeForMemRecursive(QualType T) { |
| 84 | const llvm::Type *ResultType = ConvertTypeRecursive(T); |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 85 | if (ResultType->isIntegerTy(1)) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 86 | return llvm::IntegerType::get(getLLVMContext(), |
| 87 | (unsigned)Context.getTypeSize(T)); |
Chris Lattner | 6f41c17 | 2010-01-11 19:58:10 +0000 | [diff] [blame] | 88 | // FIXME: Should assert that the llvm type and AST type has the same size. |
Eli Friedman | 57a84fb | 2009-03-03 04:48:01 +0000 | [diff] [blame] | 89 | return ResultType; |
| 90 | } |
| 91 | |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 92 | /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from |
| 93 | /// ConvertType in that it is used to convert to the memory representation for |
| 94 | /// a type. For example, the scalar representation for _Bool is i1, but the |
| 95 | /// memory representation is usually i8 or i32, depending on the target. |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 96 | const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) { |
| 97 | const llvm::Type *R = ConvertType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 99 | // If this is a non-bool type, don't map it. |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 100 | if (!R->isIntegerTy(1)) |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 101 | return R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 103 | // Otherwise, return an integer of the target-specified size. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 104 | return llvm::IntegerType::get(getLLVMContext(), |
| 105 | (unsigned)Context.getTypeSize(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Eli Friedman | b3b6b9b | 2009-03-05 03:16:41 +0000 | [diff] [blame] | 109 | // Code to verify a given function type is complete, i.e. the return type |
| 110 | // and all of the argument types are complete. |
Eli Friedman | c00129a | 2010-05-30 06:03:20 +0000 | [diff] [blame^] | 111 | const TagType *CodeGenTypes::VerifyFuncTypeComplete(const Type* T) { |
Eli Friedman | b3b6b9b | 2009-03-05 03:16:41 +0000 | [diff] [blame] | 112 | const FunctionType *FT = cast<FunctionType>(T); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 113 | if (const TagType* TT = FT->getResultType()->getAs<TagType>()) |
Eli Friedman | b3b6b9b | 2009-03-05 03:16:41 +0000 | [diff] [blame] | 114 | if (!TT->getDecl()->isDefinition()) |
| 115 | return TT; |
| 116 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T)) |
| 117 | for (unsigned i = 0; i < FPT->getNumArgs(); i++) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 118 | if (const TagType* TT = FPT->getArgType(i)->getAs<TagType>()) |
Eli Friedman | b3b6b9b | 2009-03-05 03:16:41 +0000 | [diff] [blame] | 119 | if (!TT->getDecl()->isDefinition()) |
| 120 | return TT; |
| 121 | return 0; |
| 122 | } |
| 123 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 124 | /// UpdateCompletedType - When we find the full definition for a TagDecl, |
| 125 | /// replace the 'opaque' type we previously made for it if applicable. |
| 126 | void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 127 | const Type *Key = Context.getTagDeclType(TD).getTypePtr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI = |
Daniel Dunbar | efb6d0d | 2008-09-06 02:26:43 +0000 | [diff] [blame] | 129 | TagDeclTypes.find(Key); |
Chris Lattner | 6ef58e3 | 2008-02-06 05:12:09 +0000 | [diff] [blame] | 130 | if (TDTI == TagDeclTypes.end()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 6ef58e3 | 2008-02-06 05:12:09 +0000 | [diff] [blame] | 132 | // Remember the opaque LLVM type for this tagdecl. |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 133 | llvm::PATypeHolder OpaqueHolder = TDTI->second; |
| 134 | assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) && |
Chris Lattner | 6ef58e3 | 2008-02-06 05:12:09 +0000 | [diff] [blame] | 135 | "Updating compilation of an already non-opaque type?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 137 | // Remove it from TagDeclTypes so that it will be regenerated. |
| 138 | TagDeclTypes.erase(TDTI); |
| 139 | |
Chris Lattner | 8fb1dd0 | 2008-02-06 06:06:49 +0000 | [diff] [blame] | 140 | // Generate the new type. |
| 141 | const llvm::Type *NT = ConvertTagDeclType(TD); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 8fb1dd0 | 2008-02-06 06:06:49 +0000 | [diff] [blame] | 143 | // Refine the old opaque type to its new definition. |
| 144 | cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT); |
Eli Friedman | b3b6b9b | 2009-03-05 03:16:41 +0000 | [diff] [blame] | 145 | |
| 146 | // Since we just completed a tag type, check to see if any function types |
| 147 | // were completed along with the tag type. |
| 148 | // FIXME: This is very inefficient; if we track which function types depend |
| 149 | // on which tag types, though, it should be reasonably efficient. |
| 150 | llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator i; |
| 151 | for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) { |
| 152 | if (const TagType* TT = VerifyFuncTypeComplete(i->first)) { |
| 153 | // This function type still depends on an incomplete tag type; make sure |
| 154 | // that tag type has an associated opaque type. |
| 155 | ConvertTagDeclType(TT->getDecl()); |
| 156 | } else { |
| 157 | // This function no longer depends on an incomplete tag type; create the |
| 158 | // function type, and refine the opaque type to the new function type. |
| 159 | llvm::PATypeHolder OpaqueHolder = i->second; |
| 160 | const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0)); |
| 161 | cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT); |
| 162 | FunctionTypes.erase(i); |
| 163 | } |
| 164 | } |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | static const llvm::Type* getTypeForFormat(llvm::LLVMContext &VMContext, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 168 | const llvm::fltSemantics &format) { |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 169 | if (&format == &llvm::APFloat::IEEEsingle) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 170 | return llvm::Type::getFloatTy(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 171 | if (&format == &llvm::APFloat::IEEEdouble) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 172 | return llvm::Type::getDoubleTy(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 173 | if (&format == &llvm::APFloat::IEEEquad) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 174 | return llvm::Type::getFP128Ty(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 175 | if (&format == &llvm::APFloat::PPCDoubleDouble) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 176 | return llvm::Type::getPPC_FP128Ty(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 177 | if (&format == &llvm::APFloat::x87DoubleExtended) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 178 | return llvm::Type::getX86_FP80Ty(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 179 | assert(0 && "Unknown float format!"); |
Eli Friedman | f6a943e | 2008-05-27 04:20:05 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 183 | const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 184 | const clang::Type &Ty = *Context.getCanonicalType(T).getTypePtr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | switch (Ty.getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 187 | #define TYPE(Class, Base) |
| 188 | #define ABSTRACT_TYPE(Class, Base) |
| 189 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 190 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 191 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 192 | #include "clang/AST/TypeNodes.def" |
| 193 | assert(false && "Non-canonical or dependent types aren't possible."); |
| 194 | break; |
| 195 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | case Type::Builtin: { |
| 197 | switch (cast<BuiltinType>(Ty).getKind()) { |
| 198 | case BuiltinType::Void: |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 199 | case BuiltinType::ObjCId: |
| 200 | case BuiltinType::ObjCClass: |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 201 | case BuiltinType::ObjCSel: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | // LLVM void type can only be used as the result of a function call. Just |
| 203 | // map to the same as char. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 204 | return llvm::IntegerType::get(getLLVMContext(), 8); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 205 | |
| 206 | case BuiltinType::Bool: |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 207 | // Note that we always return bool as i1 for use as a scalar type. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 208 | return llvm::Type::getInt1Ty(getLLVMContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 210 | case BuiltinType::Char_S: |
| 211 | case BuiltinType::Char_U: |
| 212 | case BuiltinType::SChar: |
| 213 | case BuiltinType::UChar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | case BuiltinType::Short: |
| 215 | case BuiltinType::UShort: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | case BuiltinType::Int: |
| 217 | case BuiltinType::UInt: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | case BuiltinType::Long: |
| 219 | case BuiltinType::ULong: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | case BuiltinType::LongLong: |
| 221 | case BuiltinType::ULongLong: |
Argyrios Kyrtzidis | afef76e | 2008-08-09 22:01:55 +0000 | [diff] [blame] | 222 | case BuiltinType::WChar: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 223 | case BuiltinType::Char16: |
| 224 | case BuiltinType::Char32: |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 225 | return llvm::IntegerType::get(getLLVMContext(), |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 226 | static_cast<unsigned>(Context.getTypeSize(T))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Eli Friedman | f6a943e | 2008-05-27 04:20:05 +0000 | [diff] [blame] | 228 | case BuiltinType::Float: |
Nate Begeman | c8b1227 | 2008-04-18 05:41:31 +0000 | [diff] [blame] | 229 | case BuiltinType::Double: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | case BuiltinType::LongDouble: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | return getTypeForFormat(getLLVMContext(), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 232 | Context.getFloatTypeSemantics(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Anders Carlsson | c1eb14a | 2009-09-15 04:39:46 +0000 | [diff] [blame] | 234 | case BuiltinType::NullPtr: { |
| 235 | // Model std::nullptr_t as i8* |
| 236 | const llvm::Type *Ty = llvm::IntegerType::get(getLLVMContext(), 8); |
| 237 | return llvm::PointerType::getUnqual(Ty); |
| 238 | } |
| 239 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 240 | case BuiltinType::UInt128: |
| 241 | case BuiltinType::Int128: |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 242 | return llvm::IntegerType::get(getLLVMContext(), 128); |
Eli Friedman | 8c69235 | 2009-12-18 23:28:34 +0000 | [diff] [blame] | 243 | |
| 244 | case BuiltinType::Overload: |
| 245 | case BuiltinType::Dependent: |
| 246 | case BuiltinType::UndeducedAuto: |
| 247 | assert(0 && "Unexpected builtin type!"); |
| 248 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | } |
Eli Friedman | 8c69235 | 2009-12-18 23:28:34 +0000 | [diff] [blame] | 250 | assert(0 && "Unknown builtin type!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | break; |
| 252 | } |
| 253 | case Type::Complex: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | const llvm::Type *EltTy = |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 255 | ConvertTypeRecursive(cast<ComplexType>(Ty).getElementType()); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 256 | return llvm::StructType::get(TheModule.getContext(), EltTy, EltTy, NULL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 258 | case Type::LValueReference: |
| 259 | case Type::RValueReference: { |
Daniel Dunbar | 6aeae7f | 2009-02-26 19:48:14 +0000 | [diff] [blame] | 260 | const ReferenceType &RTy = cast<ReferenceType>(Ty); |
| 261 | QualType ETy = RTy.getPointeeType(); |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 262 | llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext()); |
Daniel Dunbar | 6aeae7f | 2009-02-26 19:48:14 +0000 | [diff] [blame] | 263 | PointersToResolve.push_back(std::make_pair(ETy, PointeeType)); |
| 264 | return llvm::PointerType::get(PointeeType, ETy.getAddressSpace()); |
| 265 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | case Type::Pointer: { |
Daniel Dunbar | 6aeae7f | 2009-02-26 19:48:14 +0000 | [diff] [blame] | 267 | const PointerType &PTy = cast<PointerType>(Ty); |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 268 | QualType ETy = PTy.getPointeeType(); |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 269 | llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext()); |
Daniel Dunbar | 6aeae7f | 2009-02-26 19:48:14 +0000 | [diff] [blame] | 270 | PointersToResolve.push_back(std::make_pair(ETy, PointeeType)); |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 271 | return llvm::PointerType::get(PointeeType, ETy.getAddressSpace()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 274 | case Type::VariableArray: { |
| 275 | const VariableArrayType &A = cast<VariableArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 276 | assert(A.getIndexTypeCVRQualifiers() == 0 && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | "FIXME: We only handle trivial array types so far!"); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 278 | // VLAs resolve to the innermost element type; this matches |
| 279 | // the return of alloca, and there isn't any obviously better choice. |
Eli Friedman | 57a84fb | 2009-03-03 04:48:01 +0000 | [diff] [blame] | 280 | return ConvertTypeForMemRecursive(A.getElementType()); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 281 | } |
| 282 | case Type::IncompleteArray: { |
| 283 | const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 284 | assert(A.getIndexTypeCVRQualifiers() == 0 && |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 285 | "FIXME: We only handle trivial array types so far!"); |
| 286 | // int X[] -> [0 x int] |
Eli Friedman | 57a84fb | 2009-03-03 04:48:01 +0000 | [diff] [blame] | 287 | return llvm::ArrayType::get(ConvertTypeForMemRecursive(A.getElementType()), 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | } |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 289 | case Type::ConstantArray: { |
| 290 | const ConstantArrayType &A = cast<ConstantArrayType>(Ty); |
Eli Friedman | 57a84fb | 2009-03-03 04:48:01 +0000 | [diff] [blame] | 291 | const llvm::Type *EltTy = ConvertTypeForMemRecursive(A.getElementType()); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 292 | return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue()); |
| 293 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 294 | case Type::ExtVector: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | case Type::Vector: { |
| 296 | const VectorType &VT = cast<VectorType>(Ty); |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 297 | return llvm::VectorType::get(ConvertTypeRecursive(VT.getElementType()), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | VT.getNumElements()); |
| 299 | } |
| 300 | case Type::FunctionNoProto: |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 301 | case Type::FunctionProto: { |
Eli Friedman | b3b6b9b | 2009-03-05 03:16:41 +0000 | [diff] [blame] | 302 | // First, check whether we can build the full function type. |
| 303 | if (const TagType* TT = VerifyFuncTypeComplete(&Ty)) { |
| 304 | // This function's type depends on an incomplete tag type; make sure |
| 305 | // we have an opaque type corresponding to the tag type. |
| 306 | ConvertTagDeclType(TT->getDecl()); |
| 307 | // Create an opaque type for this function type, save it, and return it. |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 308 | llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext()); |
Eli Friedman | b3b6b9b | 2009-03-05 03:16:41 +0000 | [diff] [blame] | 309 | FunctionTypes.insert(std::make_pair(&Ty, ResultType)); |
| 310 | return ResultType; |
| 311 | } |
| 312 | // The function type can be built; call the appropriate routines to |
| 313 | // build it. |
Chris Lattner | 9a1a9c4 | 2009-03-31 08:55:07 +0000 | [diff] [blame] | 314 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(&Ty)) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 315 | return GetFunctionType(getFunctionInfo( |
| 316 | CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT,0))), |
| 317 | FPT->isVariadic()); |
Chris Lattner | 9a1a9c4 | 2009-03-31 08:55:07 +0000 | [diff] [blame] | 318 | |
| 319 | const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(&Ty); |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 320 | return GetFunctionType(getFunctionInfo( |
| 321 | CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT,0))), |
| 322 | true); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 323 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 325 | case Type::ObjCObject: |
| 326 | return ConvertTypeRecursive(cast<ObjCObjectType>(Ty).getBaseType()); |
| 327 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 328 | case Type::ObjCInterface: { |
Daniel Dunbar | 412f59b | 2009-04-22 10:28:39 +0000 | [diff] [blame] | 329 | // Objective-C interfaces are always opaque (outside of the |
| 330 | // runtime, which can do whatever it likes); we never refine |
| 331 | // these. |
| 332 | const llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(&Ty)]; |
| 333 | if (!T) |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 334 | T = llvm::OpaqueType::get(getLLVMContext()); |
Daniel Dunbar | 412f59b | 2009-04-22 10:28:39 +0000 | [diff] [blame] | 335 | return T; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 338 | case Type::ObjCObjectPointer: { |
Daniel Dunbar | 28e4780 | 2009-07-11 21:12:14 +0000 | [diff] [blame] | 339 | // Protocol qualifications do not influence the LLVM type, we just return a |
| 340 | // pointer to the underlying interface type. We don't need to worry about |
| 341 | // recursive conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | const llvm::Type *T = |
Daniel Dunbar | 28e4780 | 2009-07-11 21:12:14 +0000 | [diff] [blame] | 343 | ConvertTypeRecursive(cast<ObjCObjectPointerType>(Ty).getPointeeType()); |
| 344 | return llvm::PointerType::getUnqual(T); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 345 | } |
Daniel Dunbar | 28e4780 | 2009-07-11 21:12:14 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 347 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 348 | case Type::Enum: { |
Chris Lattner | de0efb3 | 2008-02-06 05:48:29 +0000 | [diff] [blame] | 349 | const TagDecl *TD = cast<TagType>(Ty).getDecl(); |
Chris Lattner | 8fb1dd0 | 2008-02-06 06:06:49 +0000 | [diff] [blame] | 350 | const llvm::Type *Res = ConvertTagDeclType(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Chris Lattner | de0efb3 | 2008-02-06 05:48:29 +0000 | [diff] [blame] | 352 | std::string TypeName(TD->getKindName()); |
| 353 | TypeName += '.'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Chris Lattner | de0efb3 | 2008-02-06 05:48:29 +0000 | [diff] [blame] | 355 | // Name the codegen type after the typedef name |
| 356 | // if there is no tag type name available |
| 357 | if (TD->getIdentifier()) |
Anders Carlsson | 6b7fc13 | 2009-09-26 19:03:24 +0000 | [diff] [blame] | 358 | // FIXME: We should not have to check for a null decl context here. |
| 359 | // Right now we do it because the implicit Obj-C decls don't have one. |
| 360 | TypeName += TD->getDeclContext() ? TD->getQualifiedNameAsString() : |
| 361 | TD->getNameAsString(); |
Chris Lattner | de0efb3 | 2008-02-06 05:48:29 +0000 | [diff] [blame] | 362 | else if (const TypedefType *TdT = dyn_cast<TypedefType>(T)) |
Anders Carlsson | 6b7fc13 | 2009-09-26 19:03:24 +0000 | [diff] [blame] | 363 | // FIXME: We should not have to check for a null decl context here. |
| 364 | // Right now we do it because the implicit Obj-C decls don't have one. |
| 365 | TypeName += TdT->getDecl()->getDeclContext() ? |
| 366 | TdT->getDecl()->getQualifiedNameAsString() : |
| 367 | TdT->getDecl()->getNameAsString(); |
Chris Lattner | de0efb3 | 2008-02-06 05:48:29 +0000 | [diff] [blame] | 368 | else |
| 369 | TypeName += "anon"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
| 371 | TheModule.addTypeName(TypeName, Res); |
Chris Lattner | de0efb3 | 2008-02-06 05:48:29 +0000 | [diff] [blame] | 372 | return Res; |
| 373 | } |
Daniel Dunbar | 9048891 | 2008-08-28 18:02:04 +0000 | [diff] [blame] | 374 | |
| 375 | case Type::BlockPointer: { |
Daniel Dunbar | 4e174f1 | 2009-01-09 02:48:46 +0000 | [diff] [blame] | 376 | const QualType FTy = cast<BlockPointerType>(Ty).getPointeeType(); |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 377 | llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext()); |
Fariborz Jahanian | 209bb43 | 2009-03-13 20:36:41 +0000 | [diff] [blame] | 378 | PointersToResolve.push_back(std::make_pair(FTy, PointeeType)); |
| 379 | return llvm::PointerType::get(PointeeType, FTy.getAddressSpace()); |
Daniel Dunbar | 9048891 | 2008-08-28 18:02:04 +0000 | [diff] [blame] | 380 | } |
Sebastian Redl | 424c51d | 2009-01-25 13:35:30 +0000 | [diff] [blame] | 381 | |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 382 | case Type::MemberPointer: { |
| 383 | // FIXME: This is ABI dependent. We use the Itanium C++ ABI. |
| 384 | // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers |
| 385 | // If we ever want to support other ABIs this needs to be abstracted. |
| 386 | |
| 387 | QualType ETy = cast<MemberPointerType>(Ty).getPointeeType(); |
Eli Friedman | 4a2251b | 2009-12-03 12:44:31 +0000 | [diff] [blame] | 388 | const llvm::Type *PtrDiffTy = |
| 389 | ConvertTypeRecursive(Context.getPointerDiffType()); |
Chris Lattner | 6f41c17 | 2010-01-11 19:58:10 +0000 | [diff] [blame] | 390 | if (ETy->isFunctionType()) |
Eli Friedman | 4a2251b | 2009-12-03 12:44:31 +0000 | [diff] [blame] | 391 | return llvm::StructType::get(TheModule.getContext(), PtrDiffTy, PtrDiffTy, |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 392 | NULL); |
Chris Lattner | 6f41c17 | 2010-01-11 19:58:10 +0000 | [diff] [blame] | 393 | return PtrDiffTy; |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 394 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 395 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | // FIXME: implement. |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 398 | return llvm::OpaqueType::get(getLLVMContext()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chris Lattner | fc3b8e9 | 2008-02-06 05:18:32 +0000 | [diff] [blame] | 401 | /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or |
| 402 | /// enum. |
Chris Lattner | 8fb1dd0 | 2008-02-06 06:06:49 +0000 | [diff] [blame] | 403 | const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) { |
Daniel Dunbar | efb6d0d | 2008-09-06 02:26:43 +0000 | [diff] [blame] | 404 | // TagDecl's are not necessarily unique, instead use the (clang) |
| 405 | // type connected to the decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | const Type *Key = |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 407 | Context.getTagDeclType(TD).getTypePtr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI = |
Daniel Dunbar | efb6d0d | 2008-09-06 02:26:43 +0000 | [diff] [blame] | 409 | TagDeclTypes.find(Key); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 5de00fc | 2008-02-06 06:03:51 +0000 | [diff] [blame] | 411 | // If we've already compiled this tag type, use the previous definition. |
| 412 | if (TDTI != TagDeclTypes.end()) |
Chris Lattner | fc3b8e9 | 2008-02-06 05:18:32 +0000 | [diff] [blame] | 413 | return TDTI->second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 415 | // If this is still a forward declaration, just define an opaque |
| 416 | // type to use for this tagged decl. |
Chris Lattner | fc3b8e9 | 2008-02-06 05:18:32 +0000 | [diff] [blame] | 417 | if (!TD->isDefinition()) { |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 418 | llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext()); |
Daniel Dunbar | efb6d0d | 2008-09-06 02:26:43 +0000 | [diff] [blame] | 419 | TagDeclTypes.insert(std::make_pair(Key, ResultType)); |
Chris Lattner | 5de00fc | 2008-02-06 06:03:51 +0000 | [diff] [blame] | 420 | return ResultType; |
| 421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 5de00fc | 2008-02-06 06:03:51 +0000 | [diff] [blame] | 423 | // Okay, this is a definition of a type. Compile the implementation now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 6f41c17 | 2010-01-11 19:58:10 +0000 | [diff] [blame] | 425 | if (TD->isEnum()) // Don't bother storing enums in TagDeclTypes. |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 426 | return ConvertTypeRecursive(cast<EnumDecl>(TD)->getIntegerType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 5de00fc | 2008-02-06 06:03:51 +0000 | [diff] [blame] | 428 | // This decl could well be recursive. In this case, insert an opaque |
| 429 | // definition of this type, which the recursive uses will get. We will then |
| 430 | // refine this opaque version later. |
| 431 | |
| 432 | // Create new OpaqueType now for later use in case this is a recursive |
| 433 | // type. This will later be refined to the actual type. |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 434 | llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(getLLVMContext()); |
Daniel Dunbar | efb6d0d | 2008-09-06 02:26:43 +0000 | [diff] [blame] | 435 | TagDeclTypes.insert(std::make_pair(Key, ResultHolder)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 5de00fc | 2008-02-06 06:03:51 +0000 | [diff] [blame] | 437 | const RecordDecl *RD = cast<const RecordDecl>(TD); |
Daniel Dunbar | ae28723 | 2009-04-22 08:50:59 +0000 | [diff] [blame] | 438 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 439 | // Force conversion of non-virtual base classes recursively. |
| 440 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) { |
| 441 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 442 | e = RD->bases_end(); i != e; ++i) { |
| 443 | if (!i->isVirtual()) { |
| 444 | const CXXRecordDecl *Base = |
| 445 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 446 | ConvertTagDeclType(Base); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
Anders Carlsson | 696798f | 2009-07-27 17:10:54 +0000 | [diff] [blame] | 451 | // Layout fields. |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 452 | CGRecordLayout *Layout = ComputeRecordLayout(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Anders Carlsson | 696798f | 2009-07-27 17:10:54 +0000 | [diff] [blame] | 454 | CGRecordLayouts[Key] = Layout; |
Chris Lattner | 6f41c17 | 2010-01-11 19:58:10 +0000 | [diff] [blame] | 455 | const llvm::Type *ResultType = Layout->getLLVMType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 5de00fc | 2008-02-06 06:03:51 +0000 | [diff] [blame] | 457 | // Refine our Opaque type to ResultType. This can invalidate ResultType, so |
| 458 | // make sure to read the result out of the holder. |
| 459 | cast<llvm::OpaqueType>(ResultHolder.get()) |
| 460 | ->refineAbstractTypeTo(ResultType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 5de00fc | 2008-02-06 06:03:51 +0000 | [diff] [blame] | 462 | return ResultHolder.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | } |
Chris Lattner | fc3b8e9 | 2008-02-06 05:18:32 +0000 | [diff] [blame] | 464 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 465 | /// getCGRecordLayout - Return record layout info for the given llvm::Type. |
Anders Carlsson | ad3e711 | 2009-08-24 17:16:23 +0000 | [diff] [blame] | 466 | const CGRecordLayout & |
Daniel Dunbar | 7c465b9 | 2010-03-30 22:26:12 +0000 | [diff] [blame] | 467 | CodeGenTypes::getCGRecordLayout(const RecordDecl *TD) const { |
Chris Lattner | 6f41c17 | 2010-01-11 19:58:10 +0000 | [diff] [blame] | 468 | const Type *Key = Context.getTagDeclType(TD).getTypePtr(); |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 469 | const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key); |
| 470 | assert(Layout && "Unable to find record layout information for type"); |
| 471 | return *Layout; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 472 | } |
Anders Carlsson | 3e5af90 | 2010-05-14 19:41:56 +0000 | [diff] [blame] | 473 | |
| 474 | bool CodeGenTypes::ContainsPointerToDataMember(QualType T) { |
| 475 | // No need to check for member pointers when not compiling C++. |
| 476 | if (!Context.getLangOptions().CPlusPlus) |
| 477 | return false; |
| 478 | |
| 479 | T = Context.getBaseElementType(T); |
| 480 | |
| 481 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 482 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 483 | |
Anders Carlsson | c39211d | 2010-05-18 03:47:15 +0000 | [diff] [blame] | 484 | return ContainsPointerToDataMember(RD); |
Anders Carlsson | 3e5af90 | 2010-05-14 19:41:56 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) |
| 488 | return !MPT->getPointeeType()->isFunctionType(); |
| 489 | |
| 490 | return false; |
| 491 | } |
Anders Carlsson | c39211d | 2010-05-18 03:47:15 +0000 | [diff] [blame] | 492 | |
| 493 | bool CodeGenTypes::ContainsPointerToDataMember(const CXXRecordDecl *RD) { |
| 494 | |
| 495 | // FIXME: It would be better if there was a way to explicitly compute the |
| 496 | // record layout instead of converting to a type. |
| 497 | ConvertTagDeclType(RD); |
| 498 | |
| 499 | const CGRecordLayout &Layout = getCGRecordLayout(RD); |
| 500 | return Layout.containsPointerToDataMember(); |
| 501 | } |