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