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