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