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