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