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