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