Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 3fafa10 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
| 19 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/Serialize.h" |
| 23 | #include "llvm/Bitcode/Deserialize.h" |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | enum FloatingRank { |
| 29 | FloatRank, DoubleRank, LongDoubleRank |
| 30 | }; |
| 31 | |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 32 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
| 33 | TargetInfo &t, |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 34 | IdentifierTable &idents, SelectorTable &sels, |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 35 | bool FreeMem, unsigned size_reserve) : |
Anders Carlsson | f58cac7 | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 36 | CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0), |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 37 | SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t), |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 38 | Idents(idents), Selectors(sels) |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 39 | { |
| 40 | if (size_reserve > 0) Types.reserve(size_reserve); |
| 41 | InitBuiltinTypes(); |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 42 | BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.Freestanding); |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 43 | TUDecl = TranslationUnitDecl::Create(*this); |
| 44 | } |
| 45 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 46 | ASTContext::~ASTContext() { |
| 47 | // Deallocate all the types. |
| 48 | while (!Types.empty()) { |
Ted Kremenek | db4d597 | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 49 | Types.back()->Destroy(*this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 50 | Types.pop_back(); |
| 51 | } |
Eli Friedman | 65489b7 | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 52 | |
Nuno Lopes | 355a868 | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 53 | { |
| 54 | llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 55 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); |
| 56 | while (I != E) { |
| 57 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 58 | delete R; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | { |
| 63 | llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator |
| 64 | I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end(); |
| 65 | while (I != E) { |
| 66 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 67 | delete R; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | { |
| 72 | llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator |
| 73 | I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end(); |
| 74 | while (I != E) { |
| 75 | RecordDecl *R = const_cast<RecordDecl*>((I++)->second); |
| 76 | R->Destroy(*this); |
| 77 | } |
| 78 | } |
| 79 | |
Eli Friedman | 65489b7 | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 80 | TUDecl->Destroy(*this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void ASTContext::PrintStats() const { |
| 84 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 85 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 86 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
Daniel Dunbar | 4767734 | 2008-09-26 03:23:00 +0000 | [diff] [blame] | 87 | unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 88 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 89 | unsigned NumMemberPointer = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 90 | |
| 91 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 92 | unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0; |
| 93 | unsigned NumObjCQualifiedIds = 0; |
Steve Naroff | e043063 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 94 | unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 95 | |
| 96 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 97 | Type *T = Types[i]; |
| 98 | if (isa<BuiltinType>(T)) |
| 99 | ++NumBuiltin; |
| 100 | else if (isa<PointerType>(T)) |
| 101 | ++NumPointer; |
Daniel Dunbar | 4767734 | 2008-09-26 03:23:00 +0000 | [diff] [blame] | 102 | else if (isa<BlockPointerType>(T)) |
| 103 | ++NumBlockPointer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 104 | else if (isa<ReferenceType>(T)) |
| 105 | ++NumReference; |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 106 | else if (isa<MemberPointerType>(T)) |
| 107 | ++NumMemberPointer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 108 | else if (isa<ComplexType>(T)) |
| 109 | ++NumComplex; |
| 110 | else if (isa<ArrayType>(T)) |
| 111 | ++NumArray; |
| 112 | else if (isa<VectorType>(T)) |
| 113 | ++NumVector; |
| 114 | else if (isa<FunctionTypeNoProto>(T)) |
| 115 | ++NumFunctionNP; |
| 116 | else if (isa<FunctionTypeProto>(T)) |
| 117 | ++NumFunctionP; |
| 118 | else if (isa<TypedefType>(T)) |
| 119 | ++NumTypeName; |
| 120 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 121 | ++NumTagged; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 122 | switch (TT->getDecl()->getTagKind()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 123 | default: assert(0 && "Unknown tagged type!"); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 124 | case TagDecl::TK_struct: ++NumTagStruct; break; |
| 125 | case TagDecl::TK_union: ++NumTagUnion; break; |
| 126 | case TagDecl::TK_class: ++NumTagClass; break; |
| 127 | case TagDecl::TK_enum: ++NumTagEnum; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 128 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 129 | } else if (isa<ObjCInterfaceType>(T)) |
| 130 | ++NumObjCInterfaces; |
| 131 | else if (isa<ObjCQualifiedInterfaceType>(T)) |
| 132 | ++NumObjCQualifiedInterfaces; |
| 133 | else if (isa<ObjCQualifiedIdType>(T)) |
| 134 | ++NumObjCQualifiedIds; |
Steve Naroff | e043063 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 135 | else if (isa<TypeOfType>(T)) |
| 136 | ++NumTypeOfTypes; |
| 137 | else if (isa<TypeOfExpr>(T)) |
| 138 | ++NumTypeOfExprs; |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 139 | else { |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 140 | QualType(T, 0).dump(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 141 | assert(0 && "Unknown type!"); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 146 | fprintf(stderr, " %d pointer types\n", NumPointer); |
Daniel Dunbar | 4767734 | 2008-09-26 03:23:00 +0000 | [diff] [blame] | 147 | fprintf(stderr, " %d block pointer types\n", NumBlockPointer); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 148 | fprintf(stderr, " %d reference types\n", NumReference); |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 149 | fprintf(stderr, " %d member pointer types\n", NumMemberPointer); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 150 | fprintf(stderr, " %d complex types\n", NumComplex); |
| 151 | fprintf(stderr, " %d array types\n", NumArray); |
| 152 | fprintf(stderr, " %d vector types\n", NumVector); |
| 153 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 154 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 155 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 156 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 157 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 158 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 159 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 160 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 161 | fprintf(stderr, " %d interface types\n", NumObjCInterfaces); |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 162 | fprintf(stderr, " %d protocol qualified interface types\n", |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 163 | NumObjCQualifiedInterfaces); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 164 | fprintf(stderr, " %d protocol qualified id types\n", |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 165 | NumObjCQualifiedIds); |
Steve Naroff | e043063 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 166 | fprintf(stderr, " %d typeof types\n", NumTypeOfTypes); |
| 167 | fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs); |
| 168 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 169 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 170 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
| 171 | NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 172 | NumMemberPointer*sizeof(MemberPointerType)+ |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 173 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 174 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
Steve Naroff | e043063 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 175 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+ |
| 176 | NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
| 180 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 181 | Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 184 | void ASTContext::InitBuiltinTypes() { |
| 185 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 186 | |
| 187 | // C99 6.2.5p19. |
| 188 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 189 | |
| 190 | // C99 6.2.5p2. |
| 191 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 192 | // C99 6.2.5p3. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 193 | if (Target.isCharSigned()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 194 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 195 | else |
| 196 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 197 | // C99 6.2.5p4. |
| 198 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 199 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 200 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 201 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 202 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 203 | |
| 204 | // C99 6.2.5p6. |
| 205 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 206 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 207 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 208 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 209 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 210 | |
| 211 | // C99 6.2.5p10. |
| 212 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 213 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 214 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 215 | |
| 216 | // C++ 3.9.1p5 |
| 217 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 218 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 219 | // Placeholder type for functions. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 220 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 221 | |
| 222 | // Placeholder type for type-dependent expressions whose type is |
| 223 | // completely unknown. No code should ever check a type against |
| 224 | // DependentTy and users should never see it; however, it is here to |
| 225 | // help diagnose failures to properly check for type-dependent |
| 226 | // expressions. |
| 227 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 229 | // C99 6.2.5p11. |
| 230 | FloatComplexTy = getComplexType(FloatTy); |
| 231 | DoubleComplexTy = getComplexType(DoubleTy); |
| 232 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 233 | |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 234 | BuiltinVaListType = QualType(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 235 | ObjCIdType = QualType(); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 236 | IdStructType = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 237 | ObjCClassType = QualType(); |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 238 | ClassStructType = 0; |
| 239 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 240 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 241 | |
| 242 | // void * type |
| 243 | VoidPtrTy = getPointerType(VoidTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | //===----------------------------------------------------------------------===// |
| 247 | // Type Sizing and Analysis |
| 248 | //===----------------------------------------------------------------------===// |
| 249 | |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 250 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 251 | /// scalar floating point type. |
| 252 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
| 253 | const BuiltinType *BT = T->getAsBuiltinType(); |
| 254 | assert(BT && "Not a floating point type!"); |
| 255 | switch (BT->getKind()) { |
| 256 | default: assert(0 && "Not a floating point type!"); |
| 257 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 258 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 259 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 260 | } |
| 261 | } |
| 262 | |
Chris Lattner | bd3153e | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 263 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
| 264 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 265 | /// this method will assert on them. |
Daniel Dunbar | 96d1f1b | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 266 | unsigned ASTContext::getDeclAlignInBytes(const Decl *D) { |
Chris Lattner | bd3153e | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 267 | // FIXME: If attribute(align) is specified on the decl, round up to it. |
| 268 | |
| 269 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 270 | QualType T = VD->getType(); |
| 271 | // Incomplete or function types default to 1. |
| 272 | if (T->isIncompleteType() || T->isFunctionType()) |
| 273 | return 1; |
| 274 | |
| 275 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 276 | T = cast<ArrayType>(T)->getElementType(); |
| 277 | |
Daniel Dunbar | 96d1f1b | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 278 | return getTypeAlign(T) / Target.getCharWidth(); |
Chris Lattner | bd3153e | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | return 1; |
| 282 | } |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 284 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 285 | /// does not work on incomplete types. |
| 286 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 7d6a5d2 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 287 | ASTContext::getTypeInfo(const Type *T) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 288 | T = getCanonicalType(T); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 289 | uint64_t Width; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 290 | unsigned Align; |
| 291 | switch (T->getTypeClass()) { |
| 292 | case Type::TypeName: assert(0 && "Not a canonical type!"); |
| 293 | case Type::FunctionNoProto: |
| 294 | case Type::FunctionProto: |
| 295 | default: |
| 296 | assert(0 && "Incomplete types have no size!"); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 297 | case Type::VariableArray: |
| 298 | assert(0 && "VLAs not implemented yet!"); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 299 | case Type::DependentSizedArray: |
| 300 | assert(0 && "Dependently-sized arrays don't have a known size"); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 301 | case Type::ConstantArray: { |
Daniel Dunbar | 7d6a5d2 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 302 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 304 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 305 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 306 | Align = EltInfo.second; |
| 307 | break; |
Christopher Lamb | 82c758b | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 308 | } |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 309 | case Type::ExtVector: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 310 | case Type::Vector: { |
| 311 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 312 | getTypeInfo(cast<VectorType>(T)->getElementType()); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 313 | Width = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 314 | Align = Width; |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 315 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 316 | // This happens for non-power-of-2 length vectors. |
| 317 | // FIXME: this should probably be a target property. |
| 318 | Align = 1 << llvm::Log2_32_Ceil(Align); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 322 | case Type::Builtin: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 323 | switch (cast<BuiltinType>(T)->getKind()) { |
| 324 | default: assert(0 && "Unknown builtin type!"); |
| 325 | case BuiltinType::Void: |
| 326 | assert(0 && "Incomplete types have no size!"); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 327 | case BuiltinType::Bool: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 328 | Width = Target.getBoolWidth(); |
| 329 | Align = Target.getBoolAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 330 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 331 | case BuiltinType::Char_S: |
| 332 | case BuiltinType::Char_U: |
| 333 | case BuiltinType::UChar: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 334 | case BuiltinType::SChar: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 335 | Width = Target.getCharWidth(); |
| 336 | Align = Target.getCharAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 337 | break; |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 338 | case BuiltinType::WChar: |
| 339 | Width = Target.getWCharWidth(); |
| 340 | Align = Target.getWCharAlign(); |
| 341 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 342 | case BuiltinType::UShort: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 343 | case BuiltinType::Short: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 344 | Width = Target.getShortWidth(); |
| 345 | Align = Target.getShortAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 346 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 347 | case BuiltinType::UInt: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 348 | case BuiltinType::Int: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 349 | Width = Target.getIntWidth(); |
| 350 | Align = Target.getIntAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 351 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 352 | case BuiltinType::ULong: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 353 | case BuiltinType::Long: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 354 | Width = Target.getLongWidth(); |
| 355 | Align = Target.getLongAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 356 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 357 | case BuiltinType::ULongLong: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 358 | case BuiltinType::LongLong: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 359 | Width = Target.getLongLongWidth(); |
| 360 | Align = Target.getLongLongAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 361 | break; |
| 362 | case BuiltinType::Float: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 363 | Width = Target.getFloatWidth(); |
| 364 | Align = Target.getFloatAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 365 | break; |
| 366 | case BuiltinType::Double: |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 367 | Width = Target.getDoubleWidth(); |
| 368 | Align = Target.getDoubleAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 369 | break; |
| 370 | case BuiltinType::LongDouble: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 371 | Width = Target.getLongDoubleWidth(); |
| 372 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 373 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 374 | } |
| 375 | break; |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 376 | case Type::FixedWidthInt: |
| 377 | // FIXME: This isn't precisely correct; the width/alignment should depend |
| 378 | // on the available types for the target |
| 379 | Width = cast<FixedWidthIntType>(T)->getWidth(); |
Chris Lattner | e917498 | 2009-02-15 21:20:13 +0000 | [diff] [blame] | 380 | Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 381 | Align = Width; |
| 382 | break; |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 383 | case Type::ExtQual: |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 384 | // FIXME: Pointers into different addr spaces could have different sizes and |
| 385 | // alignment requirements: getPointerInfo should take an AddrSpace. |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 386 | return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0)); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 387 | case Type::ObjCQualifiedId: |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 388 | Width = Target.getPointerWidth(0); |
Chris Lattner | 461a6c5 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 389 | Align = Target.getPointerAlign(0); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 390 | break; |
Steve Naroff | 62f09f5 | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 391 | case Type::BlockPointer: { |
| 392 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 393 | Width = Target.getPointerWidth(AS); |
| 394 | Align = Target.getPointerAlign(AS); |
| 395 | break; |
| 396 | } |
Chris Lattner | 461a6c5 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 397 | case Type::Pointer: { |
| 398 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 399 | Width = Target.getPointerWidth(AS); |
Chris Lattner | 461a6c5 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 400 | Align = Target.getPointerAlign(AS); |
| 401 | break; |
| 402 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 403 | case Type::Reference: |
| 404 | // "When applied to a reference or a reference type, the result is the size |
| 405 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 406 | // FIXME: This is wrong for struct layout: a reference in a struct has |
| 407 | // pointer size. |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 408 | return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 409 | case Type::MemberPointer: { |
Sebastian Redl | 18cffee | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 410 | // FIXME: This is not only platform- but also ABI-dependent. We follow |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 411 | // the GCC ABI, where pointers to data are one pointer large, pointers to |
| 412 | // functions two pointers. But if we want to support ABI compatibility with |
Sebastian Redl | 18cffee | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 413 | // other compilers too, we need to delegate this completely to TargetInfo |
| 414 | // or some ABI abstraction layer. |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 415 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
| 416 | unsigned AS = Pointee.getAddressSpace(); |
| 417 | Width = Target.getPointerWidth(AS); |
| 418 | if (Pointee->isFunctionType()) |
| 419 | Width *= 2; |
| 420 | Align = Target.getPointerAlign(AS); |
| 421 | // GCC aligns at single pointer width. |
| 422 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 423 | case Type::Complex: { |
| 424 | // Complex types have the same alignment as their elements, but twice the |
| 425 | // size. |
| 426 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 427 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 428 | Width = EltInfo.first*2; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 429 | Align = EltInfo.second; |
| 430 | break; |
| 431 | } |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 432 | case Type::ObjCInterface: { |
Daniel Dunbar | 7d6a5d2 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 433 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 434 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 435 | Width = Layout.getSize(); |
| 436 | Align = Layout.getAlignment(); |
| 437 | break; |
| 438 | } |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 439 | case Type::Tagged: { |
Daniel Dunbar | 7d6a5d2 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 440 | const TagType *TT = cast<TagType>(T); |
| 441 | |
| 442 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | fd79969 | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 443 | Width = 1; |
| 444 | Align = 1; |
| 445 | break; |
| 446 | } |
| 447 | |
Daniel Dunbar | 7d6a5d2 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 448 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 449 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 450 | |
Daniel Dunbar | 7d6a5d2 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 451 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 452 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 453 | Width = Layout.getSize(); |
| 454 | Align = Layout.getAlignment(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 458 | |
| 459 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 460 | return std::make_pair(Width, Align); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Chris Lattner | 83165b5 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 463 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 464 | /// type for the current target in bits. This can be different than the ABI |
| 465 | /// alignment in cases where it is beneficial for performance to overalign |
| 466 | /// a data type. |
| 467 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 468 | unsigned ABIAlign = getTypeAlign(T); |
| 469 | |
| 470 | // Doubles should be naturally aligned if possible. |
Daniel Dunbar | c61a800 | 2009-02-18 19:59:32 +0000 | [diff] [blame] | 471 | if (T->isSpecificBuiltinType(BuiltinType::Double)) |
| 472 | return std::max(ABIAlign, 64U); |
Chris Lattner | 83165b5 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 473 | |
| 474 | return ABIAlign; |
| 475 | } |
| 476 | |
| 477 | |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 478 | /// LayoutField - Field layout. |
| 479 | void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 480 | bool IsUnion, unsigned StructPacking, |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 481 | ASTContext &Context) { |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 482 | unsigned FieldPacking = StructPacking; |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 483 | uint64_t FieldOffset = IsUnion ? 0 : Size; |
| 484 | uint64_t FieldSize; |
| 485 | unsigned FieldAlign; |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 486 | |
| 487 | // FIXME: Should this override struct packing? Probably we want to |
| 488 | // take the minimum? |
| 489 | if (const PackedAttr *PA = FD->getAttr<PackedAttr>()) |
| 490 | FieldPacking = PA->getAlignment(); |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 491 | |
| 492 | if (const Expr *BitWidthExpr = FD->getBitWidth()) { |
| 493 | // TODO: Need to check this algorithm on other targets! |
| 494 | // (tested on Linux-X86) |
Daniel Dunbar | 7cbcbf4 | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 495 | FieldSize = |
| 496 | BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue(); |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 497 | |
| 498 | std::pair<uint64_t, unsigned> FieldInfo = |
| 499 | Context.getTypeInfo(FD->getType()); |
| 500 | uint64_t TypeSize = FieldInfo.first; |
| 501 | |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 502 | // Determine the alignment of this bitfield. The packing |
| 503 | // attributes define a maximum and the alignment attribute defines |
| 504 | // a minimum. |
| 505 | // FIXME: What is the right behavior when the specified alignment |
| 506 | // is smaller than the specified packing? |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 507 | FieldAlign = FieldInfo.second; |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 508 | if (FieldPacking) |
| 509 | FieldAlign = std::min(FieldAlign, FieldPacking); |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 510 | if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) |
| 511 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
| 512 | |
| 513 | // Check if we need to add padding to give the field the correct |
| 514 | // alignment. |
| 515 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
| 516 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 517 | |
| 518 | // Padding members don't affect overall alignment |
| 519 | if (!FD->getIdentifier()) |
| 520 | FieldAlign = 1; |
| 521 | } else { |
Chris Lattner | fd79969 | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 522 | if (FD->getType()->isIncompleteArrayType()) { |
| 523 | // This is a flexible array member; we can't directly |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 524 | // query getTypeInfo about these, so we figure it out here. |
| 525 | // Flexible array members don't have any size, but they |
| 526 | // have to be aligned appropriately for their element type. |
| 527 | FieldSize = 0; |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 528 | const ArrayType* ATy = Context.getAsArrayType(FD->getType()); |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 529 | FieldAlign = Context.getTypeAlign(ATy->getElementType()); |
| 530 | } else { |
| 531 | std::pair<uint64_t, unsigned> FieldInfo = |
| 532 | Context.getTypeInfo(FD->getType()); |
| 533 | FieldSize = FieldInfo.first; |
| 534 | FieldAlign = FieldInfo.second; |
| 535 | } |
| 536 | |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 537 | // Determine the alignment of this bitfield. The packing |
| 538 | // attributes define a maximum and the alignment attribute defines |
| 539 | // a minimum. Additionally, the packing alignment must be at least |
| 540 | // a byte for non-bitfields. |
| 541 | // |
| 542 | // FIXME: What is the right behavior when the specified alignment |
| 543 | // is smaller than the specified packing? |
| 544 | if (FieldPacking) |
| 545 | FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking)); |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 546 | if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) |
| 547 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
| 548 | |
| 549 | // Round up the current record size to the field's alignment boundary. |
| 550 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 551 | } |
| 552 | |
| 553 | // Place this field at the current location. |
| 554 | FieldOffsets[FieldNo] = FieldOffset; |
| 555 | |
| 556 | // Reserve space for this field. |
| 557 | if (IsUnion) { |
| 558 | Size = std::max(Size, FieldSize); |
| 559 | } else { |
| 560 | Size = FieldOffset + FieldSize; |
| 561 | } |
| 562 | |
| 563 | // Remember max struct/class alignment. |
| 564 | Alignment = std::max(Alignment, FieldAlign); |
| 565 | } |
| 566 | |
Fariborz Jahanian | 0556b15 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 567 | static void CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 568 | std::vector<FieldDecl*> &Fields) { |
| 569 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 570 | if (SuperClass) |
| 571 | CollectObjCIvars(SuperClass, Fields); |
| 572 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 573 | E = OI->ivar_end(); I != E; ++I) { |
| 574 | ObjCIvarDecl *IVDecl = (*I); |
| 575 | if (!IVDecl->isInvalidDecl()) |
| 576 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /// addRecordToClass - produces record info. for the class for its |
| 581 | /// ivars and all those inherited. |
| 582 | /// |
| 583 | const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) |
| 584 | { |
| 585 | const RecordDecl *&RD = ASTRecordForInterface[D]; |
| 586 | if (RD) |
| 587 | return RD; |
| 588 | std::vector<FieldDecl*> RecFields; |
| 589 | CollectObjCIvars(D, RecFields); |
| 590 | RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, |
| 591 | D->getLocation(), |
| 592 | D->getIdentifier()); |
| 593 | /// FIXME! Can do collection of ivars and adding to the record while |
| 594 | /// doing it. |
| 595 | for (unsigned int i = 0; i != RecFields.size(); i++) { |
| 596 | FieldDecl *Field = FieldDecl::Create(*this, NewRD, |
| 597 | RecFields[i]->getLocation(), |
| 598 | RecFields[i]->getIdentifier(), |
| 599 | RecFields[i]->getType(), |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 600 | RecFields[i]->getBitWidth(), false); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 601 | NewRD->addDecl(Field); |
Fariborz Jahanian | 0556b15 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 602 | } |
| 603 | NewRD->completeDefinition(*this); |
| 604 | RD = NewRD; |
| 605 | return RD; |
| 606 | } |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 607 | |
Fariborz Jahanian | ea94484 | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 608 | /// setFieldDecl - maps a field for the given Ivar reference node. |
| 609 | // |
| 610 | void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI, |
| 611 | const ObjCIvarDecl *Ivar, |
| 612 | const ObjCIvarRefExpr *MRef) { |
| 613 | FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))-> |
| 614 | lookupFieldDeclForIvar(*this, Ivar); |
| 615 | ASTFieldForIvarRef[MRef] = FD; |
| 616 | } |
| 617 | |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 618 | /// getASTObjcInterfaceLayout - Get or compute information about the layout of |
| 619 | /// the specified Objective C, which indicates its size and ivar |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 620 | /// position information. |
| 621 | const ASTRecordLayout & |
| 622 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 623 | // Look up this layout, if already laid out, return what we have. |
| 624 | const ASTRecordLayout *&Entry = ASTObjCInterfaces[D]; |
| 625 | if (Entry) return *Entry; |
| 626 | |
| 627 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 628 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
Devang Patel | 8682d88 | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 629 | ASTRecordLayout *NewEntry = NULL; |
| 630 | unsigned FieldCount = D->ivar_size(); |
| 631 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
| 632 | FieldCount++; |
| 633 | const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD); |
| 634 | unsigned Alignment = SL.getAlignment(); |
| 635 | uint64_t Size = SL.getSize(); |
| 636 | NewEntry = new ASTRecordLayout(Size, Alignment); |
| 637 | NewEntry->InitializeLayout(FieldCount); |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 638 | // Super class is at the beginning of the layout. |
| 639 | NewEntry->SetFieldOffset(0, 0); |
Devang Patel | 8682d88 | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 640 | } else { |
| 641 | NewEntry = new ASTRecordLayout(); |
| 642 | NewEntry->InitializeLayout(FieldCount); |
| 643 | } |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 644 | Entry = NewEntry; |
| 645 | |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 646 | unsigned StructPacking = 0; |
| 647 | if (const PackedAttr *PA = D->getAttr<PackedAttr>()) |
| 648 | StructPacking = PA->getAlignment(); |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 649 | |
| 650 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 651 | NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), |
| 652 | AA->getAlignment())); |
| 653 | |
| 654 | // Layout each ivar sequentially. |
| 655 | unsigned i = 0; |
| 656 | for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(), |
| 657 | IVE = D->ivar_end(); IVI != IVE; ++IVI) { |
| 658 | const ObjCIvarDecl* Ivar = (*IVI); |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 659 | NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this); |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // Finally, round the size of the total struct up to the alignment of the |
| 663 | // struct itself. |
| 664 | NewEntry->FinalizeLayout(); |
| 665 | return *NewEntry; |
| 666 | } |
| 667 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 668 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 669 | /// specified record (struct/union/class), which indicates its size and field |
| 670 | /// position information. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 671 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 672 | D = D->getDefinition(*this); |
| 673 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 675 | // Look up this layout, if already laid out, return what we have. |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 676 | const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 677 | if (Entry) return *Entry; |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 678 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 679 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 680 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
| 681 | ASTRecordLayout *NewEntry = new ASTRecordLayout(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 682 | Entry = NewEntry; |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | 3967762 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 684 | // FIXME: Avoid linear walk through the fields, if possible. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 685 | NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end())); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 686 | bool IsUnion = D->isUnion(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 687 | |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 688 | unsigned StructPacking = 0; |
| 689 | if (const PackedAttr *PA = D->getAttr<PackedAttr>()) |
| 690 | StructPacking = PA->getAlignment(); |
| 691 | |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 692 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 693 | NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), |
| 694 | AA->getAlignment())); |
Anders Carlsson | 058237f | 2008-02-18 07:13:09 +0000 | [diff] [blame] | 695 | |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 696 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 697 | // the future, this will need to be tweakable by targets. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 698 | unsigned FieldIdx = 0; |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 699 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 700 | FieldEnd = D->field_end(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 701 | Field != FieldEnd; (void)++Field, ++FieldIdx) |
| 702 | NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this); |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 703 | |
| 704 | // Finally, round the size of the total struct up to the alignment of the |
| 705 | // struct itself. |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 706 | NewEntry->FinalizeLayout(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 707 | return *NewEntry; |
| 708 | } |
| 709 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 710 | //===----------------------------------------------------------------------===// |
| 711 | // Type creation/memoization methods |
| 712 | //===----------------------------------------------------------------------===// |
| 713 | |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 714 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 715 | QualType CanT = getCanonicalType(T); |
| 716 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 717 | return T; |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 718 | |
| 719 | // If we are composing extended qualifiers together, merge together into one |
| 720 | // ExtQualType node. |
| 721 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 722 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 723 | Type *TypeNode = T.getTypePtr(); |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 725 | if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { |
| 726 | // If this type already has an address space specified, it cannot get |
| 727 | // another one. |
| 728 | assert(EQT->getAddressSpace() == 0 && |
| 729 | "Type cannot be in multiple addr spaces!"); |
| 730 | GCAttr = EQT->getObjCGCAttr(); |
| 731 | TypeNode = EQT->getBaseType(); |
| 732 | } |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 733 | |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 734 | // Check if we've already instantiated this type. |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 735 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 736 | ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 737 | void *InsertPos = 0; |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 738 | if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 739 | return QualType(EXTQy, CVRQuals); |
| 740 | |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 741 | // If the base type isn't canonical, this won't be a canonical type either, |
| 742 | // so fill in the canonical type field. |
| 743 | QualType Canonical; |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 744 | if (!TypeNode->isCanonical()) { |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 745 | Canonical = getAddrSpaceQualType(CanT, AddressSpace); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 747 | // Update InsertPos, the previous call could have invalidated it. |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 748 | ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 749 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 750 | } |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 751 | ExtQualType *New = |
| 752 | new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 753 | ExtQualTypes.InsertNode(New, InsertPos); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 754 | Types.push_back(New); |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 755 | return QualType(New, CVRQuals); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 758 | QualType ASTContext::getObjCGCQualType(QualType T, |
| 759 | QualType::GCAttrTypes GCAttr) { |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 760 | QualType CanT = getCanonicalType(T); |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 761 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 762 | return T; |
| 763 | |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 764 | // If we are composing extended qualifiers together, merge together into one |
| 765 | // ExtQualType node. |
| 766 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 767 | Type *TypeNode = T.getTypePtr(); |
| 768 | unsigned AddressSpace = 0; |
| 769 | |
| 770 | if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { |
| 771 | // If this type already has an address space specified, it cannot get |
| 772 | // another one. |
| 773 | assert(EQT->getObjCGCAttr() == QualType::GCNone && |
| 774 | "Type cannot be in multiple addr spaces!"); |
| 775 | AddressSpace = EQT->getAddressSpace(); |
| 776 | TypeNode = EQT->getBaseType(); |
| 777 | } |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 778 | |
| 779 | // Check if we've already instantiated an gc qual'd type of this type. |
| 780 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 781 | ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 782 | void *InsertPos = 0; |
| 783 | if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 784 | return QualType(EXTQy, CVRQuals); |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 785 | |
| 786 | // If the base type isn't canonical, this won't be a canonical type either, |
| 787 | // so fill in the canonical type field. |
| 788 | QualType Canonical; |
| 789 | if (!T->isCanonical()) { |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 790 | Canonical = getObjCGCQualType(CanT, GCAttr); |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 791 | |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 792 | // Update InsertPos, the previous call could have invalidated it. |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 793 | ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 794 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 795 | } |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 796 | ExtQualType *New = |
| 797 | new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 798 | ExtQualTypes.InsertNode(New, InsertPos); |
| 799 | Types.push_back(New); |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 800 | return QualType(New, CVRQuals); |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 801 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 802 | |
| 803 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 804 | /// number with the specified element type. |
| 805 | QualType ASTContext::getComplexType(QualType T) { |
| 806 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 807 | // structure. |
| 808 | llvm::FoldingSetNodeID ID; |
| 809 | ComplexType::Profile(ID, T); |
| 810 | |
| 811 | void *InsertPos = 0; |
| 812 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 813 | return QualType(CT, 0); |
| 814 | |
| 815 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 816 | // so fill in the canonical type field. |
| 817 | QualType Canonical; |
| 818 | if (!T->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 819 | Canonical = getComplexType(getCanonicalType(T)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 820 | |
| 821 | // Get the new insert position for the node we care about. |
| 822 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 823 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 824 | } |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 825 | ComplexType *New = new (*this,8) ComplexType(T, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 826 | Types.push_back(New); |
| 827 | ComplexTypes.InsertNode(New, InsertPos); |
| 828 | return QualType(New, 0); |
| 829 | } |
| 830 | |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 831 | QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { |
| 832 | llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? |
| 833 | SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; |
| 834 | FixedWidthIntType *&Entry = Map[Width]; |
| 835 | if (!Entry) |
| 836 | Entry = new FixedWidthIntType(Width, Signed); |
| 837 | return QualType(Entry, 0); |
| 838 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 839 | |
| 840 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 841 | /// the specified type. |
| 842 | QualType ASTContext::getPointerType(QualType T) { |
| 843 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 844 | // structure. |
| 845 | llvm::FoldingSetNodeID ID; |
| 846 | PointerType::Profile(ID, T); |
| 847 | |
| 848 | void *InsertPos = 0; |
| 849 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 850 | return QualType(PT, 0); |
| 851 | |
| 852 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 853 | // so fill in the canonical type field. |
| 854 | QualType Canonical; |
| 855 | if (!T->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 856 | Canonical = getPointerType(getCanonicalType(T)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 857 | |
| 858 | // Get the new insert position for the node we care about. |
| 859 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 860 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 861 | } |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 862 | PointerType *New = new (*this,8) PointerType(T, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 863 | Types.push_back(New); |
| 864 | PointerTypes.InsertNode(New, InsertPos); |
| 865 | return QualType(New, 0); |
| 866 | } |
| 867 | |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 868 | /// getBlockPointerType - Return the uniqued reference to the type for |
| 869 | /// a pointer to the specified block. |
| 870 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 871 | assert(T->isFunctionType() && "block of function types only"); |
| 872 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 873 | // structure. |
| 874 | llvm::FoldingSetNodeID ID; |
| 875 | BlockPointerType::Profile(ID, T); |
| 876 | |
| 877 | void *InsertPos = 0; |
| 878 | if (BlockPointerType *PT = |
| 879 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 880 | return QualType(PT, 0); |
| 881 | |
Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 882 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 883 | // type either so fill in the canonical type field. |
| 884 | QualType Canonical; |
| 885 | if (!T->isCanonical()) { |
| 886 | Canonical = getBlockPointerType(getCanonicalType(T)); |
| 887 | |
| 888 | // Get the new insert position for the node we care about. |
| 889 | BlockPointerType *NewIP = |
| 890 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 891 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 892 | } |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 893 | BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical); |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 894 | Types.push_back(New); |
| 895 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 896 | return QualType(New, 0); |
| 897 | } |
| 898 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 899 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 900 | /// to the specified type. |
| 901 | QualType ASTContext::getReferenceType(QualType T) { |
| 902 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 903 | // structure. |
| 904 | llvm::FoldingSetNodeID ID; |
| 905 | ReferenceType::Profile(ID, T); |
| 906 | |
| 907 | void *InsertPos = 0; |
| 908 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 909 | return QualType(RT, 0); |
| 910 | |
| 911 | // If the referencee type isn't canonical, this won't be a canonical type |
| 912 | // either, so fill in the canonical type field. |
| 913 | QualType Canonical; |
| 914 | if (!T->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 915 | Canonical = getReferenceType(getCanonicalType(T)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 916 | |
| 917 | // Get the new insert position for the node we care about. |
| 918 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 919 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 922 | ReferenceType *New = new (*this,8) ReferenceType(T, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 923 | Types.push_back(New); |
| 924 | ReferenceTypes.InsertNode(New, InsertPos); |
| 925 | return QualType(New, 0); |
| 926 | } |
| 927 | |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 928 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 929 | /// member pointer to the specified type, in the specified class. |
| 930 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) |
| 931 | { |
| 932 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 933 | // structure. |
| 934 | llvm::FoldingSetNodeID ID; |
| 935 | MemberPointerType::Profile(ID, T, Cls); |
| 936 | |
| 937 | void *InsertPos = 0; |
| 938 | if (MemberPointerType *PT = |
| 939 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 940 | return QualType(PT, 0); |
| 941 | |
| 942 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 943 | // type either, so fill in the canonical type field. |
| 944 | QualType Canonical; |
| 945 | if (!T->isCanonical()) { |
| 946 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 947 | |
| 948 | // Get the new insert position for the node we care about. |
| 949 | MemberPointerType *NewIP = |
| 950 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 951 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 952 | } |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 953 | MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 954 | Types.push_back(New); |
| 955 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 956 | return QualType(New, 0); |
| 957 | } |
| 958 | |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 959 | /// getConstantArrayType - Return the unique reference to the type for an |
| 960 | /// array of the specified element type. |
| 961 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 962 | const llvm::APInt &ArySize, |
| 963 | ArrayType::ArraySizeModifier ASM, |
| 964 | unsigned EltTypeQuals) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 965 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 3f7a8f1 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 966 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 967 | |
| 968 | void *InsertPos = 0; |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 969 | if (ConstantArrayType *ATP = |
| 970 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 971 | return QualType(ATP, 0); |
| 972 | |
| 973 | // If the element type isn't canonical, this won't be a canonical type either, |
| 974 | // so fill in the canonical type field. |
| 975 | QualType Canonical; |
| 976 | if (!EltTy->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 977 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 978 | ASM, EltTypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 979 | // Get the new insert position for the node we care about. |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 980 | ConstantArrayType *NewIP = |
| 981 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 982 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 985 | ConstantArrayType *New = |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 986 | new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 987 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 988 | Types.push_back(New); |
| 989 | return QualType(New, 0); |
| 990 | } |
| 991 | |
Steve Naroff | e2579e3 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 992 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 993 | /// variable array of the specified element type. |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 994 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
| 995 | ArrayType::ArraySizeModifier ASM, |
| 996 | unsigned EltTypeQuals) { |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 997 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 998 | // that have an expression provided for their size. |
| 999 | |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1000 | VariableArrayType *New = |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1001 | new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1002 | |
| 1003 | VariableArrayTypes.push_back(New); |
| 1004 | Types.push_back(New); |
| 1005 | return QualType(New, 0); |
| 1006 | } |
| 1007 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1008 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1009 | /// the type for a dependently-sized array of the specified element |
| 1010 | /// type. FIXME: We will need these to be uniqued, or at least |
| 1011 | /// comparable, at some point. |
| 1012 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts, |
| 1013 | ArrayType::ArraySizeModifier ASM, |
| 1014 | unsigned EltTypeQuals) { |
| 1015 | assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && |
| 1016 | "Size must be type- or value-dependent!"); |
| 1017 | |
| 1018 | // Since we don't unique expressions, it isn't possible to unique |
| 1019 | // dependently-sized array types. |
| 1020 | |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1021 | DependentSizedArrayType *New = |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1022 | new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts, |
| 1023 | ASM, EltTypeQuals); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1024 | |
| 1025 | DependentSizedArrayTypes.push_back(New); |
| 1026 | Types.push_back(New); |
| 1027 | return QualType(New, 0); |
| 1028 | } |
| 1029 | |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1030 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1031 | ArrayType::ArraySizeModifier ASM, |
| 1032 | unsigned EltTypeQuals) { |
| 1033 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 3f7a8f1 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1034 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1035 | |
| 1036 | void *InsertPos = 0; |
| 1037 | if (IncompleteArrayType *ATP = |
| 1038 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1039 | return QualType(ATP, 0); |
| 1040 | |
| 1041 | // If the element type isn't canonical, this won't be a canonical type |
| 1042 | // either, so fill in the canonical type field. |
| 1043 | QualType Canonical; |
| 1044 | |
| 1045 | if (!EltTy->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1046 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 3793e1a | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1047 | ASM, EltTypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1048 | |
| 1049 | // Get the new insert position for the node we care about. |
| 1050 | IncompleteArrayType *NewIP = |
| 1051 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1052 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 3793e1a | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1053 | } |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1054 | |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1055 | IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical, |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1056 | ASM, EltTypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1057 | |
| 1058 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1059 | Types.push_back(New); |
| 1060 | return QualType(New, 0); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1063 | /// getVectorType - Return the unique reference to a vector type of |
| 1064 | /// the specified element type and size. VectorType must be a built-in type. |
| 1065 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
| 1066 | BuiltinType *baseType; |
| 1067 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1068 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1069 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
| 1070 | |
| 1071 | // Check if we've already instantiated a vector of this type. |
| 1072 | llvm::FoldingSetNodeID ID; |
| 1073 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
| 1074 | void *InsertPos = 0; |
| 1075 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1076 | return QualType(VTP, 0); |
| 1077 | |
| 1078 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1079 | // so fill in the canonical type field. |
| 1080 | QualType Canonical; |
| 1081 | if (!vecType->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1082 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1083 | |
| 1084 | // Get the new insert position for the node we care about. |
| 1085 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1086 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1087 | } |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1088 | VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1089 | VectorTypes.InsertNode(New, InsertPos); |
| 1090 | Types.push_back(New); |
| 1091 | return QualType(New, 0); |
| 1092 | } |
| 1093 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1094 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1095 | /// the specified element type and size. VectorType must be a built-in type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1096 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1097 | BuiltinType *baseType; |
| 1098 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1099 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1100 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1101 | |
| 1102 | // Check if we've already instantiated a vector of this type. |
| 1103 | llvm::FoldingSetNodeID ID; |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1104 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1105 | void *InsertPos = 0; |
| 1106 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1107 | return QualType(VTP, 0); |
| 1108 | |
| 1109 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1110 | // so fill in the canonical type field. |
| 1111 | QualType Canonical; |
| 1112 | if (!vecType->isCanonical()) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1113 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1114 | |
| 1115 | // Get the new insert position for the node we care about. |
| 1116 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1117 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1118 | } |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1119 | ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1120 | VectorTypes.InsertNode(New, InsertPos); |
| 1121 | Types.push_back(New); |
| 1122 | return QualType(New, 0); |
| 1123 | } |
| 1124 | |
| 1125 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 1126 | /// |
| 1127 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 1128 | // Unique functions, to guarantee there is only one function of a particular |
| 1129 | // structure. |
| 1130 | llvm::FoldingSetNodeID ID; |
| 1131 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 1132 | |
| 1133 | void *InsertPos = 0; |
| 1134 | if (FunctionTypeNoProto *FT = |
| 1135 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 1136 | return QualType(FT, 0); |
| 1137 | |
| 1138 | QualType Canonical; |
| 1139 | if (!ResultTy->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1140 | Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Get the new insert position for the node we care about. |
| 1143 | FunctionTypeNoProto *NewIP = |
| 1144 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1145 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1148 | FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1149 | Types.push_back(New); |
Eli Friedman | aa0fdfd | 2008-02-25 22:11:40 +0000 | [diff] [blame] | 1150 | FunctionTypeNoProtos.InsertNode(New, InsertPos); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1151 | return QualType(New, 0); |
| 1152 | } |
| 1153 | |
| 1154 | /// getFunctionType - Return a normal function type with a typed argument |
| 1155 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1156 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1157 | unsigned NumArgs, bool isVariadic, |
| 1158 | unsigned TypeQuals) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1159 | // Unique functions, to guarantee there is only one function of a particular |
| 1160 | // structure. |
| 1161 | llvm::FoldingSetNodeID ID; |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1162 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
| 1163 | TypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1164 | |
| 1165 | void *InsertPos = 0; |
| 1166 | if (FunctionTypeProto *FTP = |
| 1167 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 1168 | return QualType(FTP, 0); |
| 1169 | |
| 1170 | // Determine whether the type being created is already canonical or not. |
| 1171 | bool isCanonical = ResultTy->isCanonical(); |
| 1172 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 1173 | if (!ArgArray[i]->isCanonical()) |
| 1174 | isCanonical = false; |
| 1175 | |
| 1176 | // If this type isn't canonical, get the canonical version of it. |
| 1177 | QualType Canonical; |
| 1178 | if (!isCanonical) { |
| 1179 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1180 | CanonicalArgs.reserve(NumArgs); |
| 1181 | for (unsigned i = 0; i != NumArgs; ++i) |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1182 | CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1183 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1184 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1185 | &CanonicalArgs[0], NumArgs, |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1186 | isVariadic, TypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1187 | |
| 1188 | // Get the new insert position for the node we care about. |
| 1189 | FunctionTypeProto *NewIP = |
| 1190 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 578a37e | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1191 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1194 | // FunctionTypeProto objects are allocated with extra bytes after them |
| 1195 | // for a variable size array (for parameter types) at the end of them. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1196 | FunctionTypeProto *FTP = |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 1197 | (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) + |
| 1198 | NumArgs*sizeof(QualType), 8); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1199 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1200 | TypeQuals, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1201 | Types.push_back(FTP); |
| 1202 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 1203 | return QualType(FTP, 0); |
| 1204 | } |
| 1205 | |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1206 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1207 | /// specified type declaration. |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1208 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argiris Kirtzidis | eeec548 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1209 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1210 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1211 | |
Argiris Kirtzidis | eeec548 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1212 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1213 | return getTypedefType(Typedef); |
Douglas Gregor | a491877 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1214 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1215 | assert(false && "Template type parameter types are always available."); |
| 1216 | } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1217 | return getObjCInterfaceType(ObjCInterface); |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1218 | |
Argiris Kirtzidis | eeec548 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1219 | if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) { |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1220 | if (PrevDecl) |
| 1221 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1222 | else |
| 1223 | Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord); |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1224 | } |
Argiris Kirtzidis | eeec548 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1225 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1226 | if (PrevDecl) |
| 1227 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1228 | else |
| 1229 | Decl->TypeForDecl = new (*this,8) RecordType(Record); |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1230 | } |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1231 | else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
| 1232 | if (PrevDecl) |
| 1233 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1234 | else |
| 1235 | Decl->TypeForDecl = new (*this,8) EnumType(Enum); |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1236 | } |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1237 | else |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1238 | assert(false && "TypeDecl without a type?"); |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1239 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1240 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1241 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1244 | /// getTypedefType - Return the unique reference to the type for the |
| 1245 | /// specified typename decl. |
| 1246 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 1247 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1248 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1249 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1250 | Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1251 | Types.push_back(Decl->TypeForDecl); |
| 1252 | return QualType(Decl->TypeForDecl, 0); |
| 1253 | } |
| 1254 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1255 | /// getObjCInterfaceType - Return the unique reference to the type for the |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1256 | /// specified ObjC interface decl. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1257 | QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) { |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1258 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1259 | |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1260 | Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl); |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1261 | Types.push_back(Decl->TypeForDecl); |
| 1262 | return QualType(Decl->TypeForDecl, 0); |
| 1263 | } |
| 1264 | |
Fariborz Jahanian | 27ecc67 | 2009-02-14 20:13:28 +0000 | [diff] [blame] | 1265 | /// buildObjCInterfaceType - Returns a new type for the interface |
| 1266 | /// declaration, regardless. It also removes any previously built |
| 1267 | /// record declaration so caller can rebuild it. |
| 1268 | QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) { |
| 1269 | const RecordDecl *&RD = ASTRecordForInterface[Decl]; |
| 1270 | if (RD) |
| 1271 | RD = 0; |
| 1272 | Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl); |
| 1273 | Types.push_back(Decl->TypeForDecl); |
| 1274 | return QualType(Decl->TypeForDecl, 0); |
| 1275 | } |
| 1276 | |
Douglas Gregor | a491877 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1277 | /// \brief Retrieve the template type parameter type for a template |
| 1278 | /// parameter with the given depth, index, and (optionally) name. |
| 1279 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
| 1280 | IdentifierInfo *Name) { |
| 1281 | llvm::FoldingSetNodeID ID; |
| 1282 | TemplateTypeParmType::Profile(ID, Depth, Index, Name); |
| 1283 | void *InsertPos = 0; |
| 1284 | TemplateTypeParmType *TypeParm |
| 1285 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1286 | |
| 1287 | if (TypeParm) |
| 1288 | return QualType(TypeParm, 0); |
| 1289 | |
| 1290 | if (Name) |
| 1291 | TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name, |
| 1292 | getTemplateTypeParmType(Depth, Index)); |
| 1293 | else |
| 1294 | TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index); |
| 1295 | |
| 1296 | Types.push_back(TypeParm); |
| 1297 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1298 | |
| 1299 | return QualType(TypeParm, 0); |
| 1300 | } |
| 1301 | |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1302 | QualType |
| 1303 | ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template, |
| 1304 | unsigned NumArgs, |
| 1305 | uintptr_t *Args, bool *ArgIsType, |
| 1306 | QualType Canon) { |
| 1307 | llvm::FoldingSetNodeID ID; |
| 1308 | ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args, |
| 1309 | ArgIsType); |
| 1310 | void *InsertPos = 0; |
| 1311 | ClassTemplateSpecializationType *Spec |
| 1312 | = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1313 | |
| 1314 | if (Spec) |
| 1315 | return QualType(Spec, 0); |
| 1316 | |
| 1317 | void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) + |
| 1318 | (sizeof(uintptr_t) * |
| 1319 | (ClassTemplateSpecializationType:: |
| 1320 | getNumPackedWords(NumArgs) + |
| 1321 | NumArgs)), 8); |
| 1322 | Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args, |
| 1323 | ArgIsType, Canon); |
| 1324 | Types.push_back(Spec); |
| 1325 | ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 1326 | |
| 1327 | return QualType(Spec, 0); |
| 1328 | } |
| 1329 | |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1330 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 1331 | /// alphabetically. |
| 1332 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 1333 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1334 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, |
| 1338 | unsigned &NumProtocols) { |
| 1339 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
| 1340 | |
| 1341 | // Sort protocols, keyed by name. |
| 1342 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 1343 | |
| 1344 | // Remove duplicates. |
| 1345 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 1346 | NumProtocols = ProtocolsEnd-Protocols; |
| 1347 | } |
| 1348 | |
| 1349 | |
Chris Lattner | b0c6a1f | 2008-04-07 04:44:08 +0000 | [diff] [blame] | 1350 | /// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for |
| 1351 | /// the given interface decl and the conforming protocol list. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1352 | QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, |
| 1353 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1354 | // Sort the protocol list alphabetically to canonicalize it. |
| 1355 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 1356 | |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1357 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 7cdcb25 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 1358 | ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1359 | |
| 1360 | void *InsertPos = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1361 | if (ObjCQualifiedInterfaceType *QT = |
| 1362 | ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1363 | return QualType(QT, 0); |
| 1364 | |
| 1365 | // No Match; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1366 | ObjCQualifiedInterfaceType *QType = |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1367 | new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols); |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1368 | |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1369 | Types.push_back(QType); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1370 | ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1371 | return QualType(QType, 0); |
| 1372 | } |
| 1373 | |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1374 | /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl |
| 1375 | /// and the conforming protocol list. |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 1376 | QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols, |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1377 | unsigned NumProtocols) { |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1378 | // Sort the protocol list alphabetically to canonicalize it. |
| 1379 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 1380 | |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1381 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1382 | ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1383 | |
| 1384 | void *InsertPos = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1385 | if (ObjCQualifiedIdType *QT = |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 1386 | ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1387 | return QualType(QT, 0); |
| 1388 | |
| 1389 | // No Match; |
Ted Kremenek | c70e7d0 | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1390 | ObjCQualifiedIdType *QType = |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1391 | new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1392 | Types.push_back(QType); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1393 | ObjCQualifiedIdTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1394 | return QualType(QType, 0); |
| 1395 | } |
| 1396 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1397 | /// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique |
| 1398 | /// TypeOfExpr AST's (since expression's are never shared). For example, |
| 1399 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 1400 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 1401 | /// on canonical type's (which are always unique). |
Steve Naroff | 11b649c | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 1402 | QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1403 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1404 | TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1405 | Types.push_back(toe); |
| 1406 | return QualType(toe, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1409 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 1410 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 1411 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 1412 | /// an issue. This doesn't effect the type checker, since it operates |
| 1413 | /// on canonical type's (which are always unique). |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1414 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1415 | QualType Canonical = getCanonicalType(tofType); |
Steve Naroff | 93fd211 | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1416 | TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1417 | Types.push_back(tot); |
| 1418 | return QualType(tot, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1421 | /// getTagDeclType - Return the unique reference to the type for the |
| 1422 | /// specified TagDecl (struct/union/class/enum) decl. |
| 1423 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
Ted Kremenek | ae8fa03 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 1424 | assert (Decl); |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1425 | return getTypeDeclType(Decl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 1429 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 1430 | /// needs to agree with the definition in <stddef.h>. |
| 1431 | QualType ASTContext::getSizeType() const { |
Douglas Gregor | c6507e4 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1432 | return getFromTargetType(Target.getSizeType()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 1435 | /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the |
Eli Friedman | fdd35d7 | 2008-02-12 08:29:21 +0000 | [diff] [blame] | 1436 | /// width of characters in wide strings, The value is target dependent and |
| 1437 | /// needs to agree with the definition in <stddef.h>. |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 1438 | QualType ASTContext::getWCharType() const { |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1439 | if (LangOpts.CPlusPlus) |
| 1440 | return WCharTy; |
| 1441 | |
Douglas Gregor | c6507e4 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1442 | // FIXME: In C, shouldn't WCharTy just be a typedef of the target's |
| 1443 | // wide-character type? |
| 1444 | return getFromTargetType(Target.getWCharType()); |
Eli Friedman | fdd35d7 | 2008-02-12 08:29:21 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1447 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 1448 | /// Used when in C++, as a GCC extension. |
| 1449 | QualType ASTContext::getSignedWCharType() const { |
| 1450 | // FIXME: derive from "Target" ? |
| 1451 | return WCharTy; |
| 1452 | } |
| 1453 | |
| 1454 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 1455 | /// Used when in C++, as a GCC extension. |
| 1456 | QualType ASTContext::getUnsignedWCharType() const { |
| 1457 | // FIXME: derive from "Target" ? |
| 1458 | return UnsignedIntTy; |
| 1459 | } |
| 1460 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1461 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 1462 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 1463 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | c6507e4 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1464 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1467 | //===----------------------------------------------------------------------===// |
| 1468 | // Type Operators |
| 1469 | //===----------------------------------------------------------------------===// |
| 1470 | |
Chris Lattner | 3dae6f4 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1471 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 1472 | /// the specified potentially non-canonical type. The non-canonical version |
| 1473 | /// of a type may have many "decorated" versions of types. Decorators can |
| 1474 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 1475 | /// to be free of any of these, allowing two canonical types to be compared |
| 1476 | /// for exact equality with a simple pointer comparison. |
| 1477 | QualType ASTContext::getCanonicalType(QualType T) { |
| 1478 | QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1479 | |
| 1480 | // If the result has type qualifiers, make sure to canonicalize them as well. |
| 1481 | unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); |
| 1482 | if (TypeQuals == 0) return CanType; |
| 1483 | |
| 1484 | // If the type qualifiers are on an array type, get the canonical type of the |
| 1485 | // array with the qualifiers applied to the element type. |
| 1486 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 1487 | if (!AT) |
| 1488 | return CanType.getQualifiedType(TypeQuals); |
| 1489 | |
| 1490 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1491 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1492 | QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); |
| 1493 | NewEltTy = getCanonicalType(NewEltTy); |
| 1494 | |
| 1495 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1496 | return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(), |
| 1497 | CAT->getIndexTypeQualifier()); |
| 1498 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
| 1499 | return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
| 1500 | IAT->getIndexTypeQualifier()); |
| 1501 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1502 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
| 1503 | return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(), |
| 1504 | DSAT->getSizeModifier(), |
| 1505 | DSAT->getIndexTypeQualifier()); |
| 1506 | |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1507 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
| 1508 | return getVariableArrayType(NewEltTy, VAT->getSizeExpr(), |
| 1509 | VAT->getSizeModifier(), |
| 1510 | VAT->getIndexTypeQualifier()); |
| 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 1515 | // Handle the non-qualified case efficiently. |
| 1516 | if (T.getCVRQualifiers() == 0) { |
| 1517 | // Handle the common positive case fast. |
| 1518 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 1519 | return AT; |
| 1520 | } |
| 1521 | |
| 1522 | // Handle the common negative case fast, ignoring CVR qualifiers. |
| 1523 | QualType CType = T->getCanonicalTypeInternal(); |
| 1524 | |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1525 | // Make sure to look through type qualifiers (like ExtQuals) for the negative |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1526 | // test. |
| 1527 | if (!isa<ArrayType>(CType) && |
| 1528 | !isa<ArrayType>(CType.getUnqualifiedType())) |
| 1529 | return 0; |
| 1530 | |
| 1531 | // Apply any CVR qualifiers from the array type to the element type. This |
| 1532 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 1533 | // any type qualifiers, the element type is so qualified, not the array type." |
| 1534 | |
| 1535 | // If we get here, we either have type qualifiers on the type, or we have |
| 1536 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
| 1537 | // we must propagate them down into the elemeng type. |
| 1538 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 1539 | unsigned AddrSpace = 0; |
| 1540 | Type *Ty = T.getTypePtr(); |
| 1541 | |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1542 | // Rip through ExtQualType's and typedefs to get to a concrete type. |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1543 | while (1) { |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1544 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) { |
| 1545 | AddrSpace = EXTQT->getAddressSpace(); |
| 1546 | Ty = EXTQT->getBaseType(); |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1547 | } else { |
| 1548 | T = Ty->getDesugaredType(); |
| 1549 | if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) |
| 1550 | break; |
| 1551 | CVRQuals |= T.getCVRQualifiers(); |
| 1552 | Ty = T.getTypePtr(); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | // If we have a simple case, just return now. |
| 1557 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
| 1558 | if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) |
| 1559 | return ATy; |
| 1560 | |
| 1561 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 1562 | // qualifiers into the array element type and return a new array type. |
| 1563 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1564 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1565 | QualType NewEltTy = ATy->getElementType(); |
| 1566 | if (AddrSpace) |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1567 | NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace); |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1568 | NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); |
| 1569 | |
| 1570 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 1571 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 1572 | CAT->getSizeModifier(), |
| 1573 | CAT->getIndexTypeQualifier())); |
| 1574 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 1575 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 1576 | IAT->getSizeModifier(), |
| 1577 | IAT->getIndexTypeQualifier())); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1578 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1579 | if (const DependentSizedArrayType *DSAT |
| 1580 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 1581 | return cast<ArrayType>( |
| 1582 | getDependentSizedArrayType(NewEltTy, |
| 1583 | DSAT->getSizeExpr(), |
| 1584 | DSAT->getSizeModifier(), |
| 1585 | DSAT->getIndexTypeQualifier())); |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1586 | |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1587 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
| 1588 | return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(), |
| 1589 | VAT->getSizeModifier(), |
| 1590 | VAT->getIndexTypeQualifier())); |
Chris Lattner | 3dae6f4 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1594 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 1595 | /// specified array type to a pointer. This operation is non-trivial when |
| 1596 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 1597 | /// this returns a pointer to a properly qualified element of the array. |
| 1598 | /// |
| 1599 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 1600 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1601 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 1602 | // typedefs in the element type of the array. This also handles propagation |
| 1603 | // of type qualifiers from the array type into the element type if present |
| 1604 | // (C99 6.7.3p8). |
| 1605 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 1606 | assert(PrettyArrayType && "Not an array type!"); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1608 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1609 | |
| 1610 | // int x[restrict 4] -> int *restrict |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1611 | return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Daniel Dunbar | 4a0b75c | 2009-01-05 22:14:37 +0000 | [diff] [blame] | 1614 | QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) { |
Anders Carlsson | 76d19c8 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 1615 | QualType ElemTy = VAT->getElementType(); |
| 1616 | |
| 1617 | if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy)) |
| 1618 | return getBaseElementType(VAT); |
| 1619 | |
| 1620 | return ElemTy; |
| 1621 | } |
| 1622 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1623 | /// getFloatingRank - Return a relative rank for floating point types. |
| 1624 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1625 | static FloatingRank getFloatingRank(QualType T) { |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1626 | if (const ComplexType *CT = T->getAsComplexType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1627 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1628 | |
Daniel Dunbar | 4a0b75c | 2009-01-05 22:14:37 +0000 | [diff] [blame] | 1629 | assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type"); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1630 | switch (T->getAsBuiltinType()->getKind()) { |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1631 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1632 | case BuiltinType::Float: return FloatRank; |
| 1633 | case BuiltinType::Double: return DoubleRank; |
| 1634 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 1635 | } |
| 1636 | } |
| 1637 | |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 1638 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 1639 | /// point or a complex type (based on typeDomain/typeSize). |
| 1640 | /// 'typeDomain' is a real floating point or complex type. |
| 1641 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 7794ae2 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 1642 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 1643 | QualType Domain) const { |
| 1644 | FloatingRank EltRank = getFloatingRank(Size); |
| 1645 | if (Domain->isComplexType()) { |
| 1646 | switch (EltRank) { |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 1647 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1648 | case FloatRank: return FloatComplexTy; |
| 1649 | case DoubleRank: return DoubleComplexTy; |
| 1650 | case LongDoubleRank: return LongDoubleComplexTy; |
| 1651 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1652 | } |
Chris Lattner | 7794ae2 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 1653 | |
| 1654 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 1655 | switch (EltRank) { |
| 1656 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 1657 | case FloatRank: return FloatTy; |
| 1658 | case DoubleRank: return DoubleTy; |
| 1659 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1660 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1663 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 1664 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 1665 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 1666 | /// LHS < RHS, return -1. |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1667 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 1668 | FloatingRank LHSR = getFloatingRank(LHS); |
| 1669 | FloatingRank RHSR = getFloatingRank(RHS); |
| 1670 | |
| 1671 | if (LHSR == RHSR) |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1672 | return 0; |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1673 | if (LHSR > RHSR) |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1674 | return 1; |
| 1675 | return -1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1678 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 1679 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 1680 | /// or if it is not canonicalized. |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1681 | unsigned ASTContext::getIntegerRank(Type *T) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1682 | assert(T->isCanonical() && "T should be canonicalized"); |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1683 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
| 1684 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
| 1685 | |
| 1686 | // There are two things which impact the integer rank: the width, and |
| 1687 | // the ordering of builtins. The builtin ordering is encoded in the |
| 1688 | // bottom three bits; the width is encoded in the bits above that. |
| 1689 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) { |
| 1690 | return FWIT->getWidth() << 3; |
| 1691 | } |
| 1692 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1693 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1694 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 1695 | case BuiltinType::Bool: |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1696 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1697 | case BuiltinType::Char_S: |
| 1698 | case BuiltinType::Char_U: |
| 1699 | case BuiltinType::SChar: |
| 1700 | case BuiltinType::UChar: |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1701 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1702 | case BuiltinType::Short: |
| 1703 | case BuiltinType::UShort: |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1704 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1705 | case BuiltinType::Int: |
| 1706 | case BuiltinType::UInt: |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1707 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1708 | case BuiltinType::Long: |
| 1709 | case BuiltinType::ULong: |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1710 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1711 | case BuiltinType::LongLong: |
| 1712 | case BuiltinType::ULongLong: |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1713 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } |
| 1716 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1717 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
| 1718 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 1719 | /// LHS < RHS, return -1. |
| 1720 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1721 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 1722 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1723 | if (LHSC == RHSC) return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1724 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1725 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 1726 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1728 | unsigned LHSRank = getIntegerRank(LHSC); |
| 1729 | unsigned RHSRank = getIntegerRank(RHSC); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1730 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1731 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 1732 | if (LHSRank == RHSRank) return 0; |
| 1733 | return LHSRank > RHSRank ? 1 : -1; |
| 1734 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1735 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1736 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 1737 | if (LHSUnsigned) { |
| 1738 | // If the unsigned [LHS] type is larger, return it. |
| 1739 | if (LHSRank >= RHSRank) |
| 1740 | return 1; |
| 1741 | |
| 1742 | // If the signed type can represent all values of the unsigned type, it |
| 1743 | // wins. Because we are dealing with 2's complement and types that are |
| 1744 | // powers of two larger than each other, this is always safe. |
| 1745 | return -1; |
| 1746 | } |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1748 | // If the unsigned [RHS] type is larger, return it. |
| 1749 | if (RHSRank >= LHSRank) |
| 1750 | return -1; |
| 1751 | |
| 1752 | // If the signed type can represent all values of the unsigned type, it |
| 1753 | // wins. Because we are dealing with 2's complement and types that are |
| 1754 | // powers of two larger than each other, this is always safe. |
| 1755 | return 1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1756 | } |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1757 | |
| 1758 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 1759 | QualType ASTContext::getCFConstantStringType() { |
| 1760 | if (!CFConstantStringTypeDecl) { |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 1761 | CFConstantStringTypeDecl = |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1762 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1763 | &Idents.get("NSConstantString")); |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1764 | QualType FieldTypes[4]; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1765 | |
| 1766 | // const int *isa; |
| 1767 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1768 | // int flags; |
| 1769 | FieldTypes[1] = IntTy; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1770 | // const char *str; |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1771 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1772 | // long length; |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1773 | FieldTypes[3] = LongTy; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1774 | |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1775 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1776 | for (unsigned i = 0; i < 4; ++i) { |
| 1777 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
| 1778 | SourceLocation(), 0, |
| 1779 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1780 | /*Mutable=*/false); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1781 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | CFConstantStringTypeDecl->completeDefinition(*this); |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 61ce98c | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 1788 | } |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1789 | |
Anders Carlsson | f58cac7 | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 1790 | QualType ASTContext::getObjCFastEnumerationStateType() |
| 1791 | { |
| 1792 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1793 | ObjCFastEnumerationStateTypeDecl = |
| 1794 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 1795 | &Idents.get("__objcFastEnumerationState")); |
| 1796 | |
Anders Carlsson | f58cac7 | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 1797 | QualType FieldTypes[] = { |
| 1798 | UnsignedLongTy, |
| 1799 | getPointerType(ObjCIdType), |
| 1800 | getPointerType(UnsignedLongTy), |
| 1801 | getConstantArrayType(UnsignedLongTy, |
| 1802 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 1803 | }; |
| 1804 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1805 | for (size_t i = 0; i < 4; ++i) { |
| 1806 | FieldDecl *Field = FieldDecl::Create(*this, |
| 1807 | ObjCFastEnumerationStateTypeDecl, |
| 1808 | SourceLocation(), 0, |
| 1809 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1810 | /*Mutable=*/false); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1811 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1812 | } |
Anders Carlsson | f58cac7 | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1814 | ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); |
Anders Carlsson | f58cac7 | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 1818 | } |
| 1819 | |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1820 | // This returns true if a type has been typedefed to BOOL: |
| 1821 | // typedef <type> BOOL; |
Chris Lattner | cb034cb | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 1822 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1823 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 85fb384 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 1824 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 1825 | return II->isStr("BOOL"); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1826 | |
| 1827 | return false; |
| 1828 | } |
| 1829 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1830 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1831 | /// purpose. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1832 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1833 | uint64_t sz = getTypeSize(type); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Make all integer and enum types at least as large as an int |
| 1836 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1837 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1838 | // Treat arrays as pointers, since that's how they're passed in. |
| 1839 | else if (type->isArrayType()) |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1840 | sz = getTypeSize(VoidPtrTy); |
| 1841 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1844 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1845 | /// declaration. |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1846 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | ae43029 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 1847 | std::string& S) { |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1848 | // FIXME: This is not very efficient. |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1849 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1850 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1851 | // Encode result type. |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1852 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1853 | // Compute size of all parameters. |
| 1854 | // Start with computing size of a pointer in number of bytes. |
| 1855 | // FIXME: There might(should) be a better way of doing this computation! |
| 1856 | SourceLocation Loc; |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1857 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1858 | // The first two arguments (self and _cmd) are pointers; account for |
| 1859 | // their size. |
| 1860 | int ParmOffset = 2 * PtrSize; |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1861 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 1862 | E = Decl->param_end(); PI != E; ++PI) { |
| 1863 | QualType PType = (*PI)->getType(); |
| 1864 | int sz = getObjCEncodingTypeSize(PType); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1865 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1866 | ParmOffset += sz; |
| 1867 | } |
| 1868 | S += llvm::utostr(ParmOffset); |
| 1869 | S += "@0:"; |
| 1870 | S += llvm::utostr(PtrSize); |
| 1871 | |
| 1872 | // Argument types. |
| 1873 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1874 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 1875 | E = Decl->param_end(); PI != E; ++PI) { |
| 1876 | ParmVarDecl *PVDecl = *PI; |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1877 | QualType PType = PVDecl->getOriginalType(); |
| 1878 | if (const ArrayType *AT = |
| 1879 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) |
| 1880 | // Use array's original type only if it has known number of |
| 1881 | // elements. |
| 1882 | if (!dyn_cast<ConstantArrayType>(AT)) |
| 1883 | PType = PVDecl->getType(); |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1884 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1885 | // 'in', 'inout', etc. |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1886 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1887 | getObjCEncodingForType(PType, S); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1888 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1889 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1893 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 501ef5c | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 1894 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1895 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 1896 | /// NULL when getting encodings for protocol properties. |
Fariborz Jahanian | 501ef5c | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 1897 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 1898 | /// attributes readonly and bycopy are encoded as single characters. The |
| 1899 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 1900 | /// encoded as single characters, followed by an identifier. Property types |
| 1901 | /// are also encoded as a parametrized attribute. The characters used to encode |
| 1902 | /// these attributes are defined by the following enumeration: |
| 1903 | /// @code |
| 1904 | /// enum PropertyAttributes { |
| 1905 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 1906 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 1907 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 1908 | /// kPropertyDynamic = 'D', // property is dynamic |
| 1909 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 1910 | /// kPropertySetter = 'S', // followed by setter selector name |
| 1911 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 1912 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 1913 | /// kPropertyWeak = 'W' // 'weak' property |
| 1914 | /// kPropertyStrong = 'P' // property GC'able |
| 1915 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 1916 | /// }; |
| 1917 | /// @endcode |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1918 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 1919 | const Decl *Container, |
Chris Lattner | ae43029 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 1920 | std::string& S) { |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1921 | // Collect information from the property implementation decl(s). |
| 1922 | bool Dynamic = false; |
| 1923 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 1924 | |
| 1925 | // FIXME: Duplicated code due to poor abstraction. |
| 1926 | if (Container) { |
| 1927 | if (const ObjCCategoryImplDecl *CID = |
| 1928 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 1929 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1930 | i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) { |
| 1931 | ObjCPropertyImplDecl *PID = *i; |
| 1932 | if (PID->getPropertyDecl() == PD) { |
| 1933 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 1934 | Dynamic = true; |
| 1935 | } else { |
| 1936 | SynthesizePID = PID; |
| 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | } else { |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1941 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1942 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1943 | i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) { |
| 1944 | ObjCPropertyImplDecl *PID = *i; |
| 1945 | if (PID->getPropertyDecl() == PD) { |
| 1946 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 1947 | Dynamic = true; |
| 1948 | } else { |
| 1949 | SynthesizePID = PID; |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | // FIXME: This is not very efficient. |
| 1957 | S = "T"; |
| 1958 | |
| 1959 | // Encode result type. |
Fariborz Jahanian | 892d5db | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 1960 | // GCC has some special rules regarding encoding of properties which |
| 1961 | // closely resembles encoding of ivars. |
| 1962 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL, |
| 1963 | true /* outermost type */, |
| 1964 | true /* encoding for property */); |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1965 | |
| 1966 | if (PD->isReadOnly()) { |
| 1967 | S += ",R"; |
| 1968 | } else { |
| 1969 | switch (PD->getSetterKind()) { |
| 1970 | case ObjCPropertyDecl::Assign: break; |
| 1971 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
| 1972 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | // It really isn't clear at all what this means, since properties |
| 1977 | // are "dynamic by default". |
| 1978 | if (Dynamic) |
| 1979 | S += ",D"; |
| 1980 | |
Fariborz Jahanian | 892d5db | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 1981 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 1982 | S += ",N"; |
| 1983 | |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1984 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 1985 | S += ",G"; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1986 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 1990 | S += ",S"; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1991 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | if (SynthesizePID) { |
| 1995 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 1996 | S += ",V"; |
Chris Lattner | 6c5ec62 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1997 | S += OID->getNameAsString(); |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | // FIXME: OBJCGC: weak & strong |
| 2001 | } |
| 2002 | |
Fariborz Jahanian | e07d9ec | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2003 | /// getLegacyIntegralTypeEncoding - |
| 2004 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
Fariborz Jahanian | 8915595 | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2005 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | e07d9ec | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2006 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 2007 | /// |
| 2008 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
| 2009 | if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) { |
| 2010 | if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) { |
Fariborz Jahanian | 8915595 | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2011 | if (BT->getKind() == BuiltinType::ULong && |
| 2012 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | e07d9ec | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2013 | PointeeTy = UnsignedIntTy; |
Fariborz Jahanian | 8915595 | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2014 | else |
| 2015 | if (BT->getKind() == BuiltinType::Long && |
| 2016 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | e07d9ec | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2017 | PointeeTy = IntTy; |
| 2018 | } |
| 2019 | } |
| 2020 | } |
| 2021 | |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2022 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2023 | FieldDecl *Field) const { |
Daniel Dunbar | f8cfe56 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2024 | // We follow the behavior of gcc, expanding structures which are |
| 2025 | // directly pointed to, and expanding embedded structures. Note that |
| 2026 | // these rules are sufficient to prevent recursive encoding of the |
| 2027 | // same type. |
Fariborz Jahanian | 89ed86b | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 2028 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
| 2029 | true /* outermost type */); |
Daniel Dunbar | f8cfe56 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Fariborz Jahanian | d136195 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2032 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
| 2033 | FieldDecl *FD) { |
| 2034 | const Expr *E = FD->getBitWidth(); |
| 2035 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 2036 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
| 2037 | unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue(); |
| 2038 | S += 'b'; |
| 2039 | S += llvm::utostr(N); |
| 2040 | } |
| 2041 | |
Daniel Dunbar | f8cfe56 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2042 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 2043 | bool ExpandPointedToStructures, |
| 2044 | bool ExpandStructures, |
Fariborz Jahanian | 89ed86b | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 2045 | FieldDecl *FD, |
Fariborz Jahanian | 892d5db | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2046 | bool OutermostType, |
| 2047 | bool EncodingProperty) const { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2048 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2049 | if (FD && FD->isBitField()) { |
Fariborz Jahanian | d136195 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2050 | EncodeBitField(this, S, FD); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2051 | } |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2052 | else { |
| 2053 | char encoding; |
| 2054 | switch (BT->getKind()) { |
| 2055 | default: assert(0 && "Unhandled builtin type kind"); |
| 2056 | case BuiltinType::Void: encoding = 'v'; break; |
| 2057 | case BuiltinType::Bool: encoding = 'B'; break; |
| 2058 | case BuiltinType::Char_U: |
| 2059 | case BuiltinType::UChar: encoding = 'C'; break; |
| 2060 | case BuiltinType::UShort: encoding = 'S'; break; |
| 2061 | case BuiltinType::UInt: encoding = 'I'; break; |
Fariborz Jahanian | ebd9575 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 2062 | case BuiltinType::ULong: |
| 2063 | encoding = |
| 2064 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
| 2065 | break; |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2066 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 2067 | case BuiltinType::Char_S: |
| 2068 | case BuiltinType::SChar: encoding = 'c'; break; |
| 2069 | case BuiltinType::Short: encoding = 's'; break; |
| 2070 | case BuiltinType::Int: encoding = 'i'; break; |
Fariborz Jahanian | ebd9575 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 2071 | case BuiltinType::Long: |
| 2072 | encoding = |
| 2073 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
| 2074 | break; |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2075 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 2076 | case BuiltinType::Float: encoding = 'f'; break; |
| 2077 | case BuiltinType::Double: encoding = 'd'; break; |
| 2078 | case BuiltinType::LongDouble: encoding = 'd'; break; |
| 2079 | } |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2080 | |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2081 | S += encoding; |
| 2082 | } |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2083 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2084 | else if (T->isObjCQualifiedIdType()) { |
Fariborz Jahanian | 892d5db | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2085 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
| 2086 | ExpandPointedToStructures, |
| 2087 | ExpandStructures, FD); |
| 2088 | if (FD || EncodingProperty) { |
| 2089 | // Note that we do extended encoding of protocol qualifer list |
| 2090 | // Only when doing ivar or property encoding. |
| 2091 | const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType(); |
| 2092 | S += '"'; |
| 2093 | for (unsigned i =0; i < QIDT->getNumProtocols(); i++) { |
| 2094 | ObjCProtocolDecl *Proto = QIDT->getProtocols(i); |
| 2095 | S += '<'; |
| 2096 | S += Proto->getNameAsString(); |
| 2097 | S += '>'; |
| 2098 | } |
| 2099 | S += '"'; |
| 2100 | } |
| 2101 | return; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2102 | } |
| 2103 | else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2104 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | e07d9ec | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2105 | bool isReadOnly = false; |
| 2106 | // For historical/compatibility reasons, the read-only qualifier of the |
| 2107 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 2108 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
| 2109 | // Also, do not emit the 'r' for anything but the outermost type! |
| 2110 | if (dyn_cast<TypedefType>(T.getTypePtr())) { |
| 2111 | if (OutermostType && T.isConstQualified()) { |
| 2112 | isReadOnly = true; |
| 2113 | S += 'r'; |
| 2114 | } |
| 2115 | } |
| 2116 | else if (OutermostType) { |
| 2117 | QualType P = PointeeTy; |
| 2118 | while (P->getAsPointerType()) |
| 2119 | P = P->getAsPointerType()->getPointeeType(); |
| 2120 | if (P.isConstQualified()) { |
| 2121 | isReadOnly = true; |
| 2122 | S += 'r'; |
| 2123 | } |
| 2124 | } |
| 2125 | if (isReadOnly) { |
| 2126 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 2127 | // combinations need to be rearranged. |
| 2128 | // Rewrite "in const" from "nr" to "rn" |
| 2129 | const char * s = S.c_str(); |
| 2130 | int len = S.length(); |
| 2131 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 2132 | std::string replace = "rn"; |
| 2133 | S.replace(S.end()-2, S.end(), replace); |
| 2134 | } |
| 2135 | } |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2136 | if (isObjCIdStructType(PointeeTy)) { |
Fariborz Jahanian | 80faffa | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 2137 | S += '@'; |
| 2138 | return; |
Fariborz Jahanian | c867947 | 2008-12-19 00:14:49 +0000 | [diff] [blame] | 2139 | } |
| 2140 | else if (PointeeTy->isObjCInterfaceType()) { |
Fariborz Jahanian | 9467504 | 2009-02-16 21:41:04 +0000 | [diff] [blame] | 2141 | if (!EncodingProperty && |
Fariborz Jahanian | 6bc0f2d | 2009-02-16 22:09:26 +0000 | [diff] [blame] | 2142 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
Fariborz Jahanian | d3498aa | 2008-12-23 21:30:15 +0000 | [diff] [blame] | 2143 | // Another historical/compatibility reason. |
| 2144 | // We encode the underlying type which comes out as |
| 2145 | // {...}; |
| 2146 | S += '^'; |
| 2147 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 2148 | false, ExpandPointedToStructures, |
| 2149 | NULL); |
| 2150 | return; |
| 2151 | } |
Fariborz Jahanian | c867947 | 2008-12-19 00:14:49 +0000 | [diff] [blame] | 2152 | S += '@'; |
Fariborz Jahanian | 892d5db | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2153 | if (FD || EncodingProperty) { |
| 2154 | const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType(); |
| 2155 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 320ac42 | 2008-12-20 19:17:01 +0000 | [diff] [blame] | 2156 | S += '"'; |
| 2157 | S += OI->getNameAsCString(); |
Fariborz Jahanian | 892d5db | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2158 | for (unsigned i =0; i < OIT->getNumProtocols(); i++) { |
| 2159 | ObjCProtocolDecl *Proto = OIT->getProtocol(i); |
| 2160 | S += '<'; |
| 2161 | S += Proto->getNameAsString(); |
| 2162 | S += '>'; |
| 2163 | } |
Fariborz Jahanian | 320ac42 | 2008-12-20 19:17:01 +0000 | [diff] [blame] | 2164 | S += '"'; |
| 2165 | } |
Fariborz Jahanian | c867947 | 2008-12-19 00:14:49 +0000 | [diff] [blame] | 2166 | return; |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2167 | } else if (isObjCClassStructType(PointeeTy)) { |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2168 | S += '#'; |
| 2169 | return; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2170 | } else if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2171 | S += ':'; |
| 2172 | return; |
Fariborz Jahanian | 80faffa | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 2173 | } |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2174 | |
| 2175 | if (PointeeTy->isCharType()) { |
| 2176 | // char pointer types should be encoded as '*' unless it is a |
| 2177 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2178 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2179 | S += '*'; |
| 2180 | return; |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | S += '^'; |
Fariborz Jahanian | e07d9ec | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2185 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 2186 | |
| 2187 | getObjCEncodingForTypeImpl(PointeeTy, S, |
Daniel Dunbar | aa91310 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2188 | false, ExpandPointedToStructures, |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2189 | NULL); |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2190 | } else if (const ArrayType *AT = |
| 2191 | // Ignore type qualifiers etc. |
| 2192 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2193 | S += '['; |
| 2194 | |
| 2195 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 2196 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 2197 | else |
| 2198 | assert(0 && "Unhandled array type!"); |
| 2199 | |
Daniel Dunbar | f8cfe56 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2200 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2201 | false, ExpandStructures, FD); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2202 | S += ']'; |
Anders Carlsson | 5695bb7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 2203 | } else if (T->getAsFunctionType()) { |
| 2204 | S += '?'; |
Fariborz Jahanian | c8ba2bd | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 2205 | } else if (const RecordType *RTy = T->getAsRecordType()) { |
Daniel Dunbar | f8cfe56 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2206 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | aa91310 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2207 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 146b2d0 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 2208 | // Anonymous structures print as '?' |
| 2209 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 2210 | S += II->getName(); |
| 2211 | } else { |
| 2212 | S += '?'; |
| 2213 | } |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2214 | if (ExpandStructures) { |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2215 | S += '='; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2216 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 2217 | FieldEnd = RDecl->field_end(); |
| 2218 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2219 | if (FD) { |
Daniel Dunbar | aa91310 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2220 | S += '"'; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2221 | S += Field->getNameAsString(); |
Daniel Dunbar | aa91310 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2222 | S += '"'; |
| 2223 | } |
| 2224 | |
| 2225 | // Special case bit-fields. |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2226 | if (Field->isBitField()) { |
| 2227 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
| 2228 | (*Field)); |
Daniel Dunbar | aa91310 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2229 | } else { |
Fariborz Jahanian | e07d9ec | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2230 | QualType qt = Field->getType(); |
| 2231 | getLegacyIntegralTypeEncoding(qt); |
| 2232 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2233 | FD); |
Daniel Dunbar | aa91310 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2234 | } |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2235 | } |
Fariborz Jahanian | c8ba2bd | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 2236 | } |
Daniel Dunbar | aa91310 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2237 | S += RDecl->isUnion() ? ')' : '}'; |
Steve Naroff | 49af3f3 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 2238 | } else if (T->isEnumeralType()) { |
Fariborz Jahanian | d136195 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2239 | if (FD && FD->isBitField()) |
| 2240 | EncodeBitField(this, S, FD); |
| 2241 | else |
| 2242 | S += 'i'; |
Steve Naroff | 62f09f5 | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 2243 | } else if (T->isBlockPointerType()) { |
Steve Naroff | 725e066 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 2244 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Fariborz Jahanian | 0cd547f | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2245 | } else if (T->isObjCInterfaceType()) { |
| 2246 | // @encode(class_name) |
| 2247 | ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl(); |
| 2248 | S += '{'; |
| 2249 | const IdentifierInfo *II = OI->getIdentifier(); |
| 2250 | S += II->getName(); |
| 2251 | S += '='; |
| 2252 | std::vector<FieldDecl*> RecFields; |
| 2253 | CollectObjCIvars(OI, RecFields); |
| 2254 | for (unsigned int i = 0; i != RecFields.size(); i++) { |
| 2255 | if (RecFields[i]->isBitField()) |
| 2256 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
| 2257 | RecFields[i]); |
| 2258 | else |
| 2259 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
| 2260 | FD); |
| 2261 | } |
| 2262 | S += '}'; |
| 2263 | } |
| 2264 | else |
Steve Naroff | 53b6f4c | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 2265 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2268 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 2269 | std::string& S) const { |
| 2270 | if (QT & Decl::OBJC_TQ_In) |
| 2271 | S += 'n'; |
| 2272 | if (QT & Decl::OBJC_TQ_Inout) |
| 2273 | S += 'N'; |
| 2274 | if (QT & Decl::OBJC_TQ_Out) |
| 2275 | S += 'o'; |
| 2276 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 2277 | S += 'O'; |
| 2278 | if (QT & Decl::OBJC_TQ_Byref) |
| 2279 | S += 'R'; |
| 2280 | if (QT & Decl::OBJC_TQ_Oneway) |
| 2281 | S += 'V'; |
| 2282 | } |
| 2283 | |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2284 | void ASTContext::setBuiltinVaListType(QualType T) |
| 2285 | { |
| 2286 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 2287 | |
| 2288 | BuiltinVaListType = T; |
| 2289 | } |
| 2290 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2291 | void ASTContext::setObjCIdType(TypedefDecl *TD) |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2292 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2293 | ObjCIdType = getTypedefType(TD); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2294 | |
| 2295 | // typedef struct objc_object *id; |
| 2296 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
Fariborz Jahanian | de93967 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2297 | // User error - caller will issue diagnostics. |
| 2298 | if (!ptr) |
| 2299 | return; |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2300 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
Fariborz Jahanian | de93967 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2301 | // User error - caller will issue diagnostics. |
| 2302 | if (!rec) |
| 2303 | return; |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2304 | IdStructType = rec; |
| 2305 | } |
| 2306 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2307 | void ASTContext::setObjCSelType(TypedefDecl *TD) |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2308 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2309 | ObjCSelType = getTypedefType(TD); |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2310 | |
| 2311 | // typedef struct objc_selector *SEL; |
| 2312 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
Fariborz Jahanian | de93967 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2313 | if (!ptr) |
| 2314 | return; |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2315 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
Fariborz Jahanian | de93967 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2316 | if (!rec) |
| 2317 | return; |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2318 | SelStructType = rec; |
| 2319 | } |
| 2320 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2321 | void ASTContext::setObjCProtoType(QualType QT) |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2322 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2323 | ObjCProtoType = QT; |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2326 | void ASTContext::setObjCClassType(TypedefDecl *TD) |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2327 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2328 | ObjCClassType = getTypedefType(TD); |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2329 | |
| 2330 | // typedef struct objc_class *Class; |
| 2331 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 2332 | assert(ptr && "'Class' incorrectly typed"); |
| 2333 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 2334 | assert(rec && "'Class' incorrectly typed"); |
| 2335 | ClassStructType = rec; |
| 2336 | } |
| 2337 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2338 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
| 2339 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 2340 | "'NSConstantString' type already set!"); |
| 2341 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2342 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Douglas Gregor | c6507e4 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2345 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | bb66b41 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 2346 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 2347 | /// is actually a value of type @c TargetInfo::IntType. |
| 2348 | QualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | c6507e4 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2349 | switch (Type) { |
| 2350 | case TargetInfo::NoInt: return QualType(); |
| 2351 | case TargetInfo::SignedShort: return ShortTy; |
| 2352 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 2353 | case TargetInfo::SignedInt: return IntTy; |
| 2354 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 2355 | case TargetInfo::SignedLong: return LongTy; |
| 2356 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 2357 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 2358 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 2359 | } |
| 2360 | |
| 2361 | assert(false && "Unhandled TargetInfo::IntType value"); |
Daniel Dunbar | 7b0dcc2 | 2008-11-11 01:16:00 +0000 | [diff] [blame] | 2362 | return QualType(); |
Douglas Gregor | c6507e4 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2363 | } |
Ted Kremenek | 118930e | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2364 | |
| 2365 | //===----------------------------------------------------------------------===// |
| 2366 | // Type Predicates. |
| 2367 | //===----------------------------------------------------------------------===// |
| 2368 | |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2369 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 2370 | /// NSObject attribute on a c-style pointer type. |
| 2371 | /// FIXME - Make it work directly on types. |
| 2372 | /// |
| 2373 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 2374 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 2375 | if (TypedefDecl *TD = TDT->getDecl()) |
| 2376 | if (TD->getAttr<ObjCNSObjectAttr>()) |
| 2377 | return true; |
| 2378 | } |
| 2379 | return false; |
| 2380 | } |
| 2381 | |
Ted Kremenek | 118930e | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2382 | /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer |
| 2383 | /// to an object type. This includes "id" and "Class" (two 'special' pointers |
| 2384 | /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified |
| 2385 | /// ID type). |
| 2386 | bool ASTContext::isObjCObjectPointerType(QualType Ty) const { |
| 2387 | if (Ty->isObjCQualifiedIdType()) |
| 2388 | return true; |
| 2389 | |
Steve Naroff | d9e0080 | 2008-10-21 18:24:04 +0000 | [diff] [blame] | 2390 | // Blocks are objects. |
| 2391 | if (Ty->isBlockPointerType()) |
| 2392 | return true; |
| 2393 | |
| 2394 | // All other object types are pointers. |
Ted Kremenek | 118930e | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2395 | if (!Ty->isPointerType()) |
| 2396 | return false; |
| 2397 | |
| 2398 | // Check to see if this is 'id' or 'Class', both of which are typedefs for |
| 2399 | // pointer types. This looks for the typedef specifically, not for the |
| 2400 | // underlying type. |
| 2401 | if (Ty == getObjCIdType() || Ty == getObjCClassType()) |
| 2402 | return true; |
| 2403 | |
| 2404 | // If this a pointer to an interface (e.g. NSString*), it is ok. |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2405 | if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType()) |
| 2406 | return true; |
| 2407 | |
| 2408 | // If is has NSObject attribute, OK as well. |
| 2409 | return isObjCNSObjectType(Ty); |
Ted Kremenek | 118930e | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Fariborz Jahanian | b8ca6ff | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2412 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 2413 | /// garbage collection attribute. |
| 2414 | /// |
| 2415 | QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2416 | QualType::GCAttrTypes GCAttrs = QualType::GCNone; |
Fariborz Jahanian | b8ca6ff | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2417 | if (getLangOptions().ObjC1 && |
| 2418 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2419 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | b8ca6ff | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2420 | // Default behavious under objective-c's gc is for objective-c pointers |
Fariborz Jahanian | bbd4ca9 | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2421 | // (or pointers to them) be treated as though they were declared |
| 2422 | // as __strong. |
| 2423 | if (GCAttrs == QualType::GCNone) { |
| 2424 | if (isObjCObjectPointerType(Ty)) |
| 2425 | GCAttrs = QualType::Strong; |
| 2426 | else if (Ty->isPointerType()) |
| 2427 | return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType()); |
| 2428 | } |
Fariborz Jahanian | b8ca6ff | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2429 | } |
Chris Lattner | 18b5a9a | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2430 | return GCAttrs; |
Fariborz Jahanian | b8ca6ff | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2433 | //===----------------------------------------------------------------------===// |
| 2434 | // Type Compatibility Testing |
| 2435 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5003e8b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 2436 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2437 | /// typesAreBlockCompatible - This routine is called when comparing two |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2438 | /// block types. Types must be strictly compatible here. For example, |
| 2439 | /// C unfortunately doesn't produce an error for the following: |
| 2440 | /// |
| 2441 | /// int (*emptyArgFunc)(); |
| 2442 | /// int (*intArgList)(int) = emptyArgFunc; |
| 2443 | /// |
| 2444 | /// For blocks, we will produce an error for the following (similar to C++): |
| 2445 | /// |
| 2446 | /// int (^emptyArgBlock)(); |
| 2447 | /// int (^intArgBlock)(int) = emptyArgBlock; |
| 2448 | /// |
| 2449 | /// FIXME: When the dust settles on this integration, fold this into mergeTypes. |
| 2450 | /// |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2451 | bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) { |
Steve Naroff | 09e1b9e | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 2452 | const FunctionType *lbase = lhs->getAsFunctionType(); |
| 2453 | const FunctionType *rbase = rhs->getAsFunctionType(); |
| 2454 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 2455 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 2456 | if (lproto && rproto) |
| 2457 | return !mergeTypes(lhs, rhs).isNull(); |
| 2458 | return false; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2461 | /// areCompatVectorTypes - Return true if the two specified vector types are |
| 2462 | /// compatible. |
| 2463 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 2464 | const VectorType *RHS) { |
| 2465 | assert(LHS->isCanonical() && RHS->isCanonical()); |
| 2466 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2467 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2470 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2471 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 2472 | /// protocol qualifiers on the LHS or RHS. |
| 2473 | /// |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2474 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 2475 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2476 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 2477 | // the LHS. |
| 2478 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 2479 | return false; |
| 2480 | |
| 2481 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 2482 | // protocol qualified at all, then we are good. |
| 2483 | if (!isa<ObjCQualifiedInterfaceType>(LHS)) |
| 2484 | return true; |
| 2485 | |
| 2486 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 2487 | // isn't a superset. |
| 2488 | if (!isa<ObjCQualifiedInterfaceType>(RHS)) |
| 2489 | return true; // FIXME: should return false! |
| 2490 | |
| 2491 | // Finally, we must have two protocol-qualified interfaces. |
| 2492 | const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS); |
| 2493 | const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS); |
| 2494 | ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(); |
| 2495 | ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end(); |
| 2496 | ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(); |
| 2497 | ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end(); |
| 2498 | |
| 2499 | // All protocols in LHS must have a presence in RHS. Since the protocol lists |
| 2500 | // are both sorted alphabetically and have no duplicates, we can scan RHS and |
| 2501 | // LHS in a single parallel scan until we run out of elements in LHS. |
| 2502 | assert(LHSPI != LHSPE && "Empty LHS protocol list?"); |
| 2503 | ObjCProtocolDecl *LHSProto = *LHSPI; |
| 2504 | |
| 2505 | while (RHSPI != RHSPE) { |
| 2506 | ObjCProtocolDecl *RHSProto = *RHSPI++; |
| 2507 | // If the RHS has a protocol that the LHS doesn't, ignore it. |
| 2508 | if (RHSProto != LHSProto) |
| 2509 | continue; |
| 2510 | |
| 2511 | // Otherwise, the RHS does have this element. |
| 2512 | ++LHSPI; |
| 2513 | if (LHSPI == LHSPE) |
| 2514 | return true; // All protocols in LHS exist in RHS. |
| 2515 | |
| 2516 | LHSProto = *LHSPI; |
| 2517 | } |
| 2518 | |
| 2519 | // If we got here, we didn't find one of the LHS's protocols in the RHS list. |
| 2520 | return false; |
| 2521 | } |
| 2522 | |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2523 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 2524 | // get the "pointed to" types |
| 2525 | const PointerType *LHSPT = LHS->getAsPointerType(); |
| 2526 | const PointerType *RHSPT = RHS->getAsPointerType(); |
| 2527 | |
| 2528 | if (!LHSPT || !RHSPT) |
| 2529 | return false; |
| 2530 | |
| 2531 | QualType lhptee = LHSPT->getPointeeType(); |
| 2532 | QualType rhptee = RHSPT->getPointeeType(); |
| 2533 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2534 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2535 | // ID acts sort of like void* for ObjC interfaces |
| 2536 | if (LHSIface && isObjCIdStructType(rhptee)) |
| 2537 | return true; |
| 2538 | if (RHSIface && isObjCIdStructType(lhptee)) |
| 2539 | return true; |
| 2540 | if (!LHSIface || !RHSIface) |
| 2541 | return false; |
| 2542 | return canAssignObjCInterfaces(LHSIface, RHSIface) || |
| 2543 | canAssignObjCInterfaces(RHSIface, LHSIface); |
| 2544 | } |
| 2545 | |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2546 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 2547 | /// both shall have the identically qualified version of a compatible type. |
| 2548 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 2549 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2550 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 2551 | return !mergeTypes(LHS, RHS).isNull(); |
| 2552 | } |
| 2553 | |
| 2554 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
| 2555 | const FunctionType *lbase = lhs->getAsFunctionType(); |
| 2556 | const FunctionType *rbase = rhs->getAsFunctionType(); |
| 2557 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 2558 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 2559 | bool allLTypes = true; |
| 2560 | bool allRTypes = true; |
| 2561 | |
| 2562 | // Check return type |
| 2563 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 2564 | if (retType.isNull()) return QualType(); |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2565 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 2566 | allLTypes = false; |
| 2567 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 2568 | allRTypes = false; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2569 | |
| 2570 | if (lproto && rproto) { // two C99 style function prototypes |
| 2571 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 2572 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 2573 | |
| 2574 | // Compatible functions must have the same number of arguments |
| 2575 | if (lproto_nargs != rproto_nargs) |
| 2576 | return QualType(); |
| 2577 | |
| 2578 | // Variadic and non-variadic functions aren't compatible |
| 2579 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 2580 | return QualType(); |
| 2581 | |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2582 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 2583 | return QualType(); |
| 2584 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2585 | // Check argument compatibility |
| 2586 | llvm::SmallVector<QualType, 10> types; |
| 2587 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 2588 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 2589 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 2590 | QualType argtype = mergeTypes(largtype, rargtype); |
| 2591 | if (argtype.isNull()) return QualType(); |
| 2592 | types.push_back(argtype); |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2593 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 2594 | allLTypes = false; |
| 2595 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 2596 | allRTypes = false; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2597 | } |
| 2598 | if (allLTypes) return lhs; |
| 2599 | if (allRTypes) return rhs; |
| 2600 | return getFunctionType(retType, types.begin(), types.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2601 | lproto->isVariadic(), lproto->getTypeQuals()); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | if (lproto) allRTypes = false; |
| 2605 | if (rproto) allLTypes = false; |
| 2606 | |
| 2607 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 2608 | if (proto) { |
| 2609 | if (proto->isVariadic()) return QualType(); |
| 2610 | // Check that the types are compatible with the types that |
| 2611 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 2612 | // The only types actually affected are promotable integer |
| 2613 | // types and floats, which would be passed as a different |
| 2614 | // type depending on whether the prototype is visible. |
| 2615 | unsigned proto_nargs = proto->getNumArgs(); |
| 2616 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 2617 | QualType argTy = proto->getArgType(i); |
| 2618 | if (argTy->isPromotableIntegerType() || |
| 2619 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 2620 | return QualType(); |
| 2621 | } |
| 2622 | |
| 2623 | if (allLTypes) return lhs; |
| 2624 | if (allRTypes) return rhs; |
| 2625 | return getFunctionType(retType, proto->arg_type_begin(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2626 | proto->getNumArgs(), lproto->isVariadic(), |
| 2627 | lproto->getTypeQuals()); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | if (allLTypes) return lhs; |
| 2631 | if (allRTypes) return rhs; |
| 2632 | return getFunctionTypeNoProto(retType); |
| 2633 | } |
| 2634 | |
| 2635 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 6a9d854 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 2636 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 2637 | // type is adjusted to "T" prior to any further analysis, the expression |
| 2638 | // designates the object or function denoted by the reference, and the |
| 2639 | // expression is an lvalue. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2640 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 2641 | // enough that they should be handled separately. |
| 2642 | if (const ReferenceType *RT = LHS->getAsReferenceType()) |
Chris Lattner | 855fed4 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 2643 | LHS = RT->getPointeeType(); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2644 | if (const ReferenceType *RT = RHS->getAsReferenceType()) |
Chris Lattner | 855fed4 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 2645 | RHS = RT->getPointeeType(); |
Chris Lattner | d47d604 | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 2646 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2647 | QualType LHSCan = getCanonicalType(LHS), |
| 2648 | RHSCan = getCanonicalType(RHS); |
| 2649 | |
| 2650 | // If two types are identical, they are compatible. |
| 2651 | if (LHSCan == RHSCan) |
| 2652 | return LHS; |
| 2653 | |
| 2654 | // If the qualifiers are different, the types aren't compatible |
| 2655 | if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() || |
| 2656 | LHSCan.getAddressSpace() != RHSCan.getAddressSpace()) |
| 2657 | return QualType(); |
| 2658 | |
| 2659 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 2660 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
| 2661 | |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2662 | // We want to consider the two function types to be the same for these |
| 2663 | // comparisons, just force one to the other. |
| 2664 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 2665 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 398837e | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 2666 | |
| 2667 | // Same as above for arrays |
Chris Lattner | b5709e2 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2668 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 2669 | LHSClass = Type::ConstantArray; |
| 2670 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 2671 | RHSClass = Type::ConstantArray; |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2672 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2673 | // Canonicalize ExtVector -> Vector. |
| 2674 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 2675 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Chris Lattner | b5709e2 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2676 | |
Chris Lattner | 7cdcb25 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 2677 | // Consider qualified interfaces and interfaces the same. |
| 2678 | if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface; |
| 2679 | if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2680 | |
Chris Lattner | b5709e2 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2681 | // If the canonical type classes don't match. |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2682 | if (LHSClass != RHSClass) { |
Steve Naroff | 0bbc135 | 2009-02-21 16:18:07 +0000 | [diff] [blame^] | 2683 | const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); |
| 2684 | const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); |
| 2685 | |
| 2686 | // ID acts sort of like void* for ObjC interfaces |
| 2687 | if (LHSIface && isObjCIdStructType(RHS)) |
| 2688 | return LHS; |
| 2689 | if (RHSIface && isObjCIdStructType(LHS)) |
| 2690 | return RHS; |
| 2691 | |
Steve Naroff | 28ceff7 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 2692 | // ID is compatible with all qualified id types. |
| 2693 | if (LHS->isObjCQualifiedIdType()) { |
| 2694 | if (const PointerType *PT = RHS->getAsPointerType()) { |
| 2695 | QualType pType = PT->getPointeeType(); |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2696 | if (isObjCIdStructType(pType)) |
Steve Naroff | 28ceff7 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 2697 | return LHS; |
| 2698 | // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). |
| 2699 | // Unfortunately, this API is part of Sema (which we don't have access |
| 2700 | // to. Need to refactor. The following check is insufficient, since we |
| 2701 | // need to make sure the class implements the protocol. |
| 2702 | if (pType->isObjCInterfaceType()) |
| 2703 | return LHS; |
| 2704 | } |
| 2705 | } |
| 2706 | if (RHS->isObjCQualifiedIdType()) { |
| 2707 | if (const PointerType *PT = LHS->getAsPointerType()) { |
| 2708 | QualType pType = PT->getPointeeType(); |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2709 | if (isObjCIdStructType(pType)) |
Steve Naroff | 28ceff7 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 2710 | return RHS; |
| 2711 | // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). |
| 2712 | // Unfortunately, this API is part of Sema (which we don't have access |
| 2713 | // to. Need to refactor. The following check is insufficient, since we |
| 2714 | // need to make sure the class implements the protocol. |
| 2715 | if (pType->isObjCInterfaceType()) |
| 2716 | return RHS; |
| 2717 | } |
| 2718 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2719 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 2720 | // a signed integer type, or an unsigned integer type. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2721 | if (const EnumType* ETy = LHS->getAsEnumType()) { |
| 2722 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 2723 | return RHS; |
Eli Friedman | ad6c06c | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 2724 | } |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2725 | if (const EnumType* ETy = RHS->getAsEnumType()) { |
| 2726 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 2727 | return LHS; |
Eli Friedman | ad6c06c | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 2728 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2729 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2730 | return QualType(); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2731 | } |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2732 | |
Steve Naroff | c88babe | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 2733 | // The canonical type classes match. |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2734 | switch (LHSClass) { |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2735 | case Type::Pointer: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2736 | { |
| 2737 | // Merge two pointer types, while trying to preserve typedef info |
| 2738 | QualType LHSPointee = LHS->getAsPointerType()->getPointeeType(); |
| 2739 | QualType RHSPointee = RHS->getAsPointerType()->getPointeeType(); |
| 2740 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 2741 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2742 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 2743 | return LHS; |
| 2744 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 2745 | return RHS; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2746 | return getPointerType(ResultType); |
| 2747 | } |
Steve Naroff | 09e1b9e | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 2748 | case Type::BlockPointer: |
| 2749 | { |
| 2750 | // Merge two block pointer types, while trying to preserve typedef info |
| 2751 | QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType(); |
| 2752 | QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType(); |
| 2753 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 2754 | if (ResultType.isNull()) return QualType(); |
| 2755 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 2756 | return LHS; |
| 2757 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 2758 | return RHS; |
| 2759 | return getBlockPointerType(ResultType); |
| 2760 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2761 | case Type::ConstantArray: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2762 | { |
| 2763 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 2764 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 2765 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 2766 | return QualType(); |
| 2767 | |
| 2768 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 2769 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 2770 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 2771 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2772 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 2773 | return LHS; |
| 2774 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 2775 | return RHS; |
Eli Friedman | c91a3f3 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 2776 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 2777 | ArrayType::ArraySizeModifier(), 0); |
| 2778 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 2779 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2780 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 2781 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2782 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 2783 | return LHS; |
| 2784 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 2785 | return RHS; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2786 | if (LVAT) { |
| 2787 | // FIXME: This isn't correct! But tricky to implement because |
| 2788 | // the array's size has to be the size of LHS, but the type |
| 2789 | // has to be different. |
| 2790 | return LHS; |
| 2791 | } |
| 2792 | if (RVAT) { |
| 2793 | // FIXME: This isn't correct! But tricky to implement because |
| 2794 | // the array's size has to be the size of RHS, but the type |
| 2795 | // has to be different. |
| 2796 | return RHS; |
| 2797 | } |
Eli Friedman | c91a3f3 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 2798 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 2799 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2800 | return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2801 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2802 | case Type::FunctionNoProto: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2803 | return mergeFunctionTypes(LHS, RHS); |
| 2804 | case Type::Tagged: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2805 | // FIXME: Why are these compatible? |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2806 | if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS; |
| 2807 | if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2808 | return QualType(); |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2809 | case Type::Builtin: |
Chris Lattner | d1240fa | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 2810 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2811 | return QualType(); |
Daniel Dunbar | 457f33d | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 2812 | case Type::Complex: |
| 2813 | // Distinct complex types are incompatible. |
| 2814 | return QualType(); |
Chris Lattner | d1240fa | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 2815 | case Type::Vector: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2816 | if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) |
| 2817 | return LHS; |
Chris Lattner | 2fda0ed | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2818 | return QualType(); |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2819 | case Type::ObjCInterface: |
Steve Naroff | 0bbc135 | 2009-02-21 16:18:07 +0000 | [diff] [blame^] | 2820 | // Check if the interfaces are assignment compatible. |
| 2821 | const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); |
| 2822 | const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); |
| 2823 | if (LHSIface && RHSIface && |
| 2824 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 2825 | return LHS; |
| 2826 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2827 | return QualType(); |
Steve Naroff | 28ceff7 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 2828 | case Type::ObjCQualifiedId: |
| 2829 | // Distinct qualified id's are not compatible. |
| 2830 | return QualType(); |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2831 | default: |
| 2832 | assert(0 && "unexpected type"); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2833 | return QualType(); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2834 | } |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2835 | } |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2836 | |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 2837 | //===----------------------------------------------------------------------===// |
Eli Friedman | 0832dbc | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 2838 | // Integer Predicates |
| 2839 | //===----------------------------------------------------------------------===// |
Chris Lattner | 74f6701 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 2840 | |
Eli Friedman | 0832dbc | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 2841 | unsigned ASTContext::getIntWidth(QualType T) { |
| 2842 | if (T == BoolTy) |
| 2843 | return 1; |
Eli Friedman | ff3fcdf | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2844 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) { |
| 2845 | return FWIT->getWidth(); |
| 2846 | } |
| 2847 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 0832dbc | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 2848 | return (unsigned)getTypeSize(T); |
| 2849 | } |
| 2850 | |
| 2851 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 2852 | assert(T->isSignedIntegerType() && "Unexpected type"); |
| 2853 | if (const EnumType* ETy = T->getAsEnumType()) |
| 2854 | T = ETy->getDecl()->getIntegerType(); |
| 2855 | const BuiltinType* BTy = T->getAsBuiltinType(); |
| 2856 | assert (BTy && "Unexpected signed integer type"); |
| 2857 | switch (BTy->getKind()) { |
| 2858 | case BuiltinType::Char_S: |
| 2859 | case BuiltinType::SChar: |
| 2860 | return UnsignedCharTy; |
| 2861 | case BuiltinType::Short: |
| 2862 | return UnsignedShortTy; |
| 2863 | case BuiltinType::Int: |
| 2864 | return UnsignedIntTy; |
| 2865 | case BuiltinType::Long: |
| 2866 | return UnsignedLongTy; |
| 2867 | case BuiltinType::LongLong: |
| 2868 | return UnsignedLongLongTy; |
| 2869 | default: |
| 2870 | assert(0 && "Unexpected signed integer type"); |
| 2871 | return QualType(); |
| 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | |
| 2876 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 2877 | // Serialization Support |
| 2878 | //===----------------------------------------------------------------------===// |
| 2879 | |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2880 | /// Emit - Serialize an ASTContext object to Bitcode. |
| 2881 | void ASTContext::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2882 | S.Emit(LangOpts); |
Ted Kremenek | 9af4d5c | 2007-10-31 20:00:03 +0000 | [diff] [blame] | 2883 | S.EmitRef(SourceMgr); |
| 2884 | S.EmitRef(Target); |
| 2885 | S.EmitRef(Idents); |
| 2886 | S.EmitRef(Selectors); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2887 | |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2888 | // Emit the size of the type vector so that we can reserve that size |
| 2889 | // when we reconstitute the ASTContext object. |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 2890 | S.EmitInt(Types.size()); |
| 2891 | |
Ted Kremenek | 034a78c | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 2892 | for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); |
| 2893 | I!=E;++I) |
| 2894 | (*I)->Emit(S); |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 2895 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2896 | S.EmitOwnedPtr(TUDecl); |
| 2897 | |
Ted Kremenek | e1fed7a | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 2898 | // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Ted Kremenek | acba361 | 2007-11-13 00:25:37 +0000 | [diff] [blame] | 2901 | ASTContext* ASTContext::Create(llvm::Deserializer& D) { |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2902 | |
| 2903 | // Read the language options. |
| 2904 | LangOptions LOpts; |
| 2905 | LOpts.Read(D); |
| 2906 | |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2907 | SourceManager &SM = D.ReadRef<SourceManager>(); |
| 2908 | TargetInfo &t = D.ReadRef<TargetInfo>(); |
| 2909 | IdentifierTable &idents = D.ReadRef<IdentifierTable>(); |
| 2910 | SelectorTable &sels = D.ReadRef<SelectorTable>(); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2911 | |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2912 | unsigned size_reserve = D.ReadInt(); |
| 2913 | |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2914 | ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, |
| 2915 | size_reserve); |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2916 | |
Ted Kremenek | 034a78c | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 2917 | for (unsigned i = 0; i < size_reserve; ++i) |
| 2918 | Type::Create(*A,i,D); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2919 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2920 | A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A)); |
| 2921 | |
Ted Kremenek | e1fed7a | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 2922 | // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2923 | |
| 2924 | return A; |
| 2925 | } |