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