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