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