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