Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 3fafa10 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/Serialize.h" |
| 22 | #include "llvm/Bitcode/Deserialize.h" |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | enum FloatingRank { |
| 27 | FloatRank, DoubleRank, LongDoubleRank |
| 28 | }; |
| 29 | |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 30 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t, |
| 31 | IdentifierTable &idents, SelectorTable &sels, |
| 32 | unsigned size_reserve) : |
| 33 | CFConstantStringTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), Target(t), |
| 34 | Idents(idents), Selectors(sels) |
| 35 | { |
| 36 | if (size_reserve > 0) Types.reserve(size_reserve); |
| 37 | InitBuiltinTypes(); |
| 38 | BuiltinInfo.InitializeBuiltins(idents, Target); |
| 39 | TUDecl = TranslationUnitDecl::Create(*this); |
| 40 | } |
| 41 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 42 | ASTContext::~ASTContext() { |
| 43 | // Deallocate all the types. |
| 44 | while (!Types.empty()) { |
Ted Kremenek | db4d597 | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 45 | Types.back()->Destroy(*this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 46 | Types.pop_back(); |
| 47 | } |
Eli Friedman | 65489b7 | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 48 | |
| 49 | TUDecl->Destroy(*this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void ASTContext::PrintStats() const { |
| 53 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 54 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 55 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
| 56 | unsigned NumVector = 0, NumComplex = 0; |
| 57 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
| 58 | |
| 59 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 60 | unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0; |
| 61 | unsigned NumObjCQualifiedIds = 0; |
Steve Naroff | e043063 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 62 | unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 63 | |
| 64 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 65 | Type *T = Types[i]; |
| 66 | if (isa<BuiltinType>(T)) |
| 67 | ++NumBuiltin; |
| 68 | else if (isa<PointerType>(T)) |
| 69 | ++NumPointer; |
| 70 | else if (isa<ReferenceType>(T)) |
| 71 | ++NumReference; |
| 72 | else if (isa<ComplexType>(T)) |
| 73 | ++NumComplex; |
| 74 | else if (isa<ArrayType>(T)) |
| 75 | ++NumArray; |
| 76 | else if (isa<VectorType>(T)) |
| 77 | ++NumVector; |
| 78 | else if (isa<FunctionTypeNoProto>(T)) |
| 79 | ++NumFunctionNP; |
| 80 | else if (isa<FunctionTypeProto>(T)) |
| 81 | ++NumFunctionP; |
| 82 | else if (isa<TypedefType>(T)) |
| 83 | ++NumTypeName; |
| 84 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 85 | ++NumTagged; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 86 | switch (TT->getDecl()->getTagKind()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 87 | default: assert(0 && "Unknown tagged type!"); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 88 | case TagDecl::TK_struct: ++NumTagStruct; break; |
| 89 | case TagDecl::TK_union: ++NumTagUnion; break; |
| 90 | case TagDecl::TK_class: ++NumTagClass; break; |
| 91 | case TagDecl::TK_enum: ++NumTagEnum; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 93 | } else if (isa<ObjCInterfaceType>(T)) |
| 94 | ++NumObjCInterfaces; |
| 95 | else if (isa<ObjCQualifiedInterfaceType>(T)) |
| 96 | ++NumObjCQualifiedInterfaces; |
| 97 | else if (isa<ObjCQualifiedIdType>(T)) |
| 98 | ++NumObjCQualifiedIds; |
Steve Naroff | e043063 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 99 | else if (isa<TypeOfType>(T)) |
| 100 | ++NumTypeOfTypes; |
| 101 | else if (isa<TypeOfExpr>(T)) |
| 102 | ++NumTypeOfExprs; |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 103 | else { |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 104 | QualType(T, 0).dump(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 105 | assert(0 && "Unknown type!"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 110 | fprintf(stderr, " %d pointer types\n", NumPointer); |
| 111 | fprintf(stderr, " %d reference types\n", NumReference); |
| 112 | fprintf(stderr, " %d complex types\n", NumComplex); |
| 113 | fprintf(stderr, " %d array types\n", NumArray); |
| 114 | fprintf(stderr, " %d vector types\n", NumVector); |
| 115 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 116 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 117 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 118 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 119 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 120 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 121 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 122 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 123 | fprintf(stderr, " %d interface types\n", NumObjCInterfaces); |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 124 | fprintf(stderr, " %d protocol qualified interface types\n", |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 125 | NumObjCQualifiedInterfaces); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 126 | fprintf(stderr, " %d protocol qualified id types\n", |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 127 | NumObjCQualifiedIds); |
Steve Naroff | e043063 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 128 | fprintf(stderr, " %d typeof types\n", NumTypeOfTypes); |
| 129 | fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs); |
| 130 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 131 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 132 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
| 133 | NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ |
| 134 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 135 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
Steve Naroff | e043063 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 136 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+ |
| 137 | NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 142 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
| 143 | } |
| 144 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 145 | void ASTContext::InitBuiltinTypes() { |
| 146 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 147 | |
| 148 | // C99 6.2.5p19. |
| 149 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 150 | |
| 151 | // C99 6.2.5p2. |
| 152 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 153 | // C99 6.2.5p3. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 154 | if (Target.isCharSigned()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 155 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 156 | else |
| 157 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 158 | // C99 6.2.5p4. |
| 159 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 160 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 161 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 162 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 163 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 164 | |
| 165 | // C99 6.2.5p6. |
| 166 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 167 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 168 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 169 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 170 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 171 | |
| 172 | // C99 6.2.5p10. |
| 173 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 174 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 175 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 176 | |
| 177 | // C++ 3.9.1p5 |
| 178 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 179 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 180 | // C99 6.2.5p11. |
| 181 | FloatComplexTy = getComplexType(FloatTy); |
| 182 | DoubleComplexTy = getComplexType(DoubleTy); |
| 183 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 184 | |
| 185 | BuiltinVaListType = QualType(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 186 | ObjCIdType = QualType(); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 187 | IdStructType = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 188 | ObjCClassType = QualType(); |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 189 | ClassStructType = 0; |
| 190 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 191 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 192 | |
| 193 | // void * type |
| 194 | VoidPtrTy = getPointerType(VoidTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | //===----------------------------------------------------------------------===// |
| 198 | // Type Sizing and Analysis |
| 199 | //===----------------------------------------------------------------------===// |
| 200 | |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 201 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 202 | /// scalar floating point type. |
| 203 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
| 204 | const BuiltinType *BT = T->getAsBuiltinType(); |
| 205 | assert(BT && "Not a floating point type!"); |
| 206 | switch (BT->getKind()) { |
| 207 | default: assert(0 && "Not a floating point type!"); |
| 208 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 209 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 210 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 215 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 216 | /// does not work on incomplete types. |
| 217 | std::pair<uint64_t, unsigned> |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 218 | ASTContext::getTypeInfo(QualType T) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 219 | T = getCanonicalType(T); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 220 | uint64_t Width; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 221 | unsigned Align; |
| 222 | switch (T->getTypeClass()) { |
| 223 | case Type::TypeName: assert(0 && "Not a canonical type!"); |
| 224 | case Type::FunctionNoProto: |
| 225 | case Type::FunctionProto: |
| 226 | default: |
| 227 | assert(0 && "Incomplete types have no size!"); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 228 | case Type::VariableArray: |
| 229 | assert(0 && "VLAs not implemented yet!"); |
| 230 | case Type::ConstantArray: { |
| 231 | ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
| 232 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 233 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 234 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 235 | Align = EltInfo.second; |
| 236 | break; |
Christopher Lamb | 82c758b | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 237 | } |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 238 | case Type::ExtVector: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 239 | case Type::Vector: { |
| 240 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 241 | getTypeInfo(cast<VectorType>(T)->getElementType()); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 242 | Width = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 243 | // FIXME: This isn't right for unusual vectors |
| 244 | Align = Width; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 248 | case Type::Builtin: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 249 | switch (cast<BuiltinType>(T)->getKind()) { |
| 250 | default: assert(0 && "Unknown builtin type!"); |
| 251 | case BuiltinType::Void: |
| 252 | assert(0 && "Incomplete types have no size!"); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 253 | case BuiltinType::Bool: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 254 | Width = Target.getBoolWidth(); |
| 255 | Align = Target.getBoolAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 256 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 257 | case BuiltinType::Char_S: |
| 258 | case BuiltinType::Char_U: |
| 259 | case BuiltinType::UChar: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 260 | case BuiltinType::SChar: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 261 | Width = Target.getCharWidth(); |
| 262 | Align = Target.getCharAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 263 | break; |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 264 | case BuiltinType::WChar: |
| 265 | Width = Target.getWCharWidth(); |
| 266 | Align = Target.getWCharAlign(); |
| 267 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 268 | case BuiltinType::UShort: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 269 | case BuiltinType::Short: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 270 | Width = Target.getShortWidth(); |
| 271 | Align = Target.getShortAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 272 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 273 | case BuiltinType::UInt: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 274 | case BuiltinType::Int: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 275 | Width = Target.getIntWidth(); |
| 276 | Align = Target.getIntAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 277 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 278 | case BuiltinType::ULong: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 279 | case BuiltinType::Long: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 280 | Width = Target.getLongWidth(); |
| 281 | Align = Target.getLongAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 282 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 283 | case BuiltinType::ULongLong: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 284 | case BuiltinType::LongLong: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 285 | Width = Target.getLongLongWidth(); |
| 286 | Align = Target.getLongLongAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 287 | break; |
| 288 | case BuiltinType::Float: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 289 | Width = Target.getFloatWidth(); |
| 290 | Align = Target.getFloatAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 291 | break; |
| 292 | case BuiltinType::Double: |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 293 | Width = Target.getDoubleWidth(); |
| 294 | Align = Target.getDoubleAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 295 | break; |
| 296 | case BuiltinType::LongDouble: |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 297 | Width = Target.getLongDoubleWidth(); |
| 298 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 299 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 300 | } |
| 301 | break; |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 302 | case Type::ASQual: |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 303 | // FIXME: Pointers into different addr spaces could have different sizes and |
| 304 | // alignment requirements: getPointerInfo should take an AddrSpace. |
| 305 | return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0)); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 306 | case Type::ObjCQualifiedId: |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 307 | Width = Target.getPointerWidth(0); |
Chris Lattner | 461a6c5 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 308 | Align = Target.getPointerAlign(0); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 309 | break; |
Chris Lattner | 461a6c5 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 310 | case Type::Pointer: { |
| 311 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 312 | Width = Target.getPointerWidth(AS); |
Chris Lattner | 461a6c5 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 313 | Align = Target.getPointerAlign(AS); |
| 314 | break; |
| 315 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 316 | case Type::Reference: |
| 317 | // "When applied to a reference or a reference type, the result is the size |
| 318 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 319 | // FIXME: This is wrong for struct layout: a reference in a struct has |
| 320 | // pointer size. |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 321 | return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 322 | |
| 323 | case Type::Complex: { |
| 324 | // Complex types have the same alignment as their elements, but twice the |
| 325 | // size. |
| 326 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 327 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 328 | Width = EltInfo.first*2; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 329 | Align = EltInfo.second; |
| 330 | break; |
| 331 | } |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 332 | case Type::ObjCInterface: { |
| 333 | ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
| 334 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 335 | Width = Layout.getSize(); |
| 336 | Align = Layout.getAlignment(); |
| 337 | break; |
| 338 | } |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 339 | case Type::Tagged: { |
Chris Lattner | fd79969 | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 340 | if (cast<TagType>(T)->getDecl()->isInvalidDecl()) { |
| 341 | Width = 1; |
| 342 | Align = 1; |
| 343 | break; |
| 344 | } |
| 345 | |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 346 | if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T))) |
| 347 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 348 | |
| 349 | RecordType *RT = cast<RecordType>(T); |
| 350 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 351 | Width = Layout.getSize(); |
| 352 | Align = Layout.getAlignment(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 355 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 356 | |
| 357 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 358 | return std::make_pair(Width, Align); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 361 | /// LayoutField - Field layout. |
| 362 | void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, |
| 363 | bool IsUnion, bool StructIsPacked, |
| 364 | ASTContext &Context) { |
| 365 | bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>(); |
| 366 | uint64_t FieldOffset = IsUnion ? 0 : Size; |
| 367 | uint64_t FieldSize; |
| 368 | unsigned FieldAlign; |
| 369 | |
| 370 | if (const Expr *BitWidthExpr = FD->getBitWidth()) { |
| 371 | // TODO: Need to check this algorithm on other targets! |
| 372 | // (tested on Linux-X86) |
Daniel Dunbar | 7cbcbf4 | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 373 | FieldSize = |
| 374 | BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue(); |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 375 | |
| 376 | std::pair<uint64_t, unsigned> FieldInfo = |
| 377 | Context.getTypeInfo(FD->getType()); |
| 378 | uint64_t TypeSize = FieldInfo.first; |
| 379 | |
| 380 | FieldAlign = FieldInfo.second; |
| 381 | if (FieldIsPacked) |
| 382 | FieldAlign = 1; |
| 383 | if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) |
| 384 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
| 385 | |
| 386 | // Check if we need to add padding to give the field the correct |
| 387 | // alignment. |
| 388 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
| 389 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 390 | |
| 391 | // Padding members don't affect overall alignment |
| 392 | if (!FD->getIdentifier()) |
| 393 | FieldAlign = 1; |
| 394 | } else { |
Chris Lattner | fd79969 | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 395 | if (FD->getType()->isIncompleteArrayType()) { |
| 396 | // This is a flexible array member; we can't directly |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 397 | // query getTypeInfo about these, so we figure it out here. |
| 398 | // Flexible array members don't have any size, but they |
| 399 | // have to be aligned appropriately for their element type. |
| 400 | FieldSize = 0; |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 401 | const ArrayType* ATy = Context.getAsArrayType(FD->getType()); |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 402 | FieldAlign = Context.getTypeAlign(ATy->getElementType()); |
| 403 | } else { |
| 404 | std::pair<uint64_t, unsigned> FieldInfo = |
| 405 | Context.getTypeInfo(FD->getType()); |
| 406 | FieldSize = FieldInfo.first; |
| 407 | FieldAlign = FieldInfo.second; |
| 408 | } |
| 409 | |
| 410 | if (FieldIsPacked) |
| 411 | FieldAlign = 8; |
| 412 | if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) |
| 413 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
| 414 | |
| 415 | // Round up the current record size to the field's alignment boundary. |
| 416 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 417 | } |
| 418 | |
| 419 | // Place this field at the current location. |
| 420 | FieldOffsets[FieldNo] = FieldOffset; |
| 421 | |
| 422 | // Reserve space for this field. |
| 423 | if (IsUnion) { |
| 424 | Size = std::max(Size, FieldSize); |
| 425 | } else { |
| 426 | Size = FieldOffset + FieldSize; |
| 427 | } |
| 428 | |
| 429 | // Remember max struct/class alignment. |
| 430 | Alignment = std::max(Alignment, FieldAlign); |
| 431 | } |
| 432 | |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 433 | |
| 434 | /// getASTObjcInterfaceLayout - Get or compute information about the layout of the |
| 435 | /// specified Objective C, which indicates its size and ivar |
| 436 | /// position information. |
| 437 | const ASTRecordLayout & |
| 438 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 439 | // Look up this layout, if already laid out, return what we have. |
| 440 | const ASTRecordLayout *&Entry = ASTObjCInterfaces[D]; |
| 441 | if (Entry) return *Entry; |
| 442 | |
| 443 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 444 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
Devang Patel | 8682d88 | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 445 | ASTRecordLayout *NewEntry = NULL; |
| 446 | unsigned FieldCount = D->ivar_size(); |
| 447 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
| 448 | FieldCount++; |
| 449 | const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD); |
| 450 | unsigned Alignment = SL.getAlignment(); |
| 451 | uint64_t Size = SL.getSize(); |
| 452 | NewEntry = new ASTRecordLayout(Size, Alignment); |
| 453 | NewEntry->InitializeLayout(FieldCount); |
| 454 | NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout. |
| 455 | } else { |
| 456 | NewEntry = new ASTRecordLayout(); |
| 457 | NewEntry->InitializeLayout(FieldCount); |
| 458 | } |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 459 | Entry = NewEntry; |
| 460 | |
Devang Patel | 4b6bf70 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 461 | bool IsPacked = D->getAttr<PackedAttr>(); |
| 462 | |
| 463 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 464 | NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), |
| 465 | AA->getAlignment())); |
| 466 | |
| 467 | // Layout each ivar sequentially. |
| 468 | unsigned i = 0; |
| 469 | for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(), |
| 470 | IVE = D->ivar_end(); IVI != IVE; ++IVI) { |
| 471 | const ObjCIvarDecl* Ivar = (*IVI); |
| 472 | NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this); |
| 473 | } |
| 474 | |
| 475 | // Finally, round the size of the total struct up to the alignment of the |
| 476 | // struct itself. |
| 477 | NewEntry->FinalizeLayout(); |
| 478 | return *NewEntry; |
| 479 | } |
| 480 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 481 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 482 | /// specified record (struct/union/class), which indicates its size and field |
| 483 | /// position information. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 484 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 485 | assert(D->isDefinition() && "Cannot get layout of forward declarations!"); |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 487 | // Look up this layout, if already laid out, return what we have. |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 488 | const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 489 | if (Entry) return *Entry; |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 490 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 491 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 492 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
| 493 | ASTRecordLayout *NewEntry = new ASTRecordLayout(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 494 | Entry = NewEntry; |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 495 | |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 496 | NewEntry->InitializeLayout(D->getNumMembers()); |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 497 | bool StructIsPacked = D->getAttr<PackedAttr>(); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 498 | bool IsUnion = D->isUnion(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 499 | |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 500 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 501 | NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), |
| 502 | AA->getAlignment())); |
Anders Carlsson | 058237f | 2008-02-18 07:13:09 +0000 | [diff] [blame] | 503 | |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 504 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 505 | // the future, this will need to be tweakable by targets. |
| 506 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 507 | const FieldDecl *FD = D->getMember(i); |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 508 | NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 509 | } |
Eli Friedman | 5949a02 | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 510 | |
| 511 | // Finally, round the size of the total struct up to the alignment of the |
| 512 | // struct itself. |
Devang Patel | bfe323c | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 513 | NewEntry->FinalizeLayout(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 514 | return *NewEntry; |
| 515 | } |
| 516 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 517 | //===----------------------------------------------------------------------===// |
| 518 | // Type creation/memoization methods |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 521 | QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 522 | QualType CanT = getCanonicalType(T); |
| 523 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 524 | return T; |
| 525 | |
| 526 | // Type's cannot have multiple ASQuals, therefore we know we only have to deal |
| 527 | // with CVR qualifiers from here on out. |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 528 | assert(CanT.getAddressSpace() == 0 && |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 529 | "Type is already address space qualified"); |
| 530 | |
| 531 | // Check if we've already instantiated an address space qual'd type of this |
| 532 | // type. |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 533 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 534 | ASQualType::Profile(ID, T.getTypePtr(), AddressSpace); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 535 | void *InsertPos = 0; |
| 536 | if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 537 | return QualType(ASQy, 0); |
| 538 | |
| 539 | // If the base type isn't canonical, this won't be a canonical type either, |
| 540 | // so fill in the canonical type field. |
| 541 | QualType Canonical; |
| 542 | if (!T->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 543 | Canonical = getASQualType(CanT, AddressSpace); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 544 | |
| 545 | // Get the new insert position for the node we care about. |
| 546 | ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 547 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 548 | } |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 549 | ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 550 | ASQualTypes.InsertNode(New, InsertPos); |
| 551 | Types.push_back(New); |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 552 | return QualType(New, T.getCVRQualifiers()); |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 555 | |
| 556 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 557 | /// number with the specified element type. |
| 558 | QualType ASTContext::getComplexType(QualType T) { |
| 559 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 560 | // structure. |
| 561 | llvm::FoldingSetNodeID ID; |
| 562 | ComplexType::Profile(ID, T); |
| 563 | |
| 564 | void *InsertPos = 0; |
| 565 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 566 | return QualType(CT, 0); |
| 567 | |
| 568 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 569 | // so fill in the canonical type field. |
| 570 | QualType Canonical; |
| 571 | if (!T->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 572 | Canonical = getComplexType(getCanonicalType(T)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 573 | |
| 574 | // Get the new insert position for the node we care about. |
| 575 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 576 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 577 | } |
| 578 | ComplexType *New = new ComplexType(T, Canonical); |
| 579 | Types.push_back(New); |
| 580 | ComplexTypes.InsertNode(New, InsertPos); |
| 581 | return QualType(New, 0); |
| 582 | } |
| 583 | |
| 584 | |
| 585 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 586 | /// the specified type. |
| 587 | QualType ASTContext::getPointerType(QualType T) { |
| 588 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 589 | // structure. |
| 590 | llvm::FoldingSetNodeID ID; |
| 591 | PointerType::Profile(ID, T); |
| 592 | |
| 593 | void *InsertPos = 0; |
| 594 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 595 | return QualType(PT, 0); |
| 596 | |
| 597 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 598 | // so fill in the canonical type field. |
| 599 | QualType Canonical; |
| 600 | if (!T->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 601 | Canonical = getPointerType(getCanonicalType(T)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 602 | |
| 603 | // Get the new insert position for the node we care about. |
| 604 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 605 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 606 | } |
| 607 | PointerType *New = new PointerType(T, Canonical); |
| 608 | Types.push_back(New); |
| 609 | PointerTypes.InsertNode(New, InsertPos); |
| 610 | return QualType(New, 0); |
| 611 | } |
| 612 | |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 613 | /// getBlockPointerType - Return the uniqued reference to the type for |
| 614 | /// a pointer to the specified block. |
| 615 | QualType ASTContext::getBlockPointerType(QualType T) { |
| 616 | assert(T->isFunctionType() && "closure of function types only"); |
| 617 | // Unique pointers, to guarantee there is only one closure of a particular |
| 618 | // structure. |
| 619 | llvm::FoldingSetNodeID ID; |
| 620 | BlockPointerType::Profile(ID, T); |
| 621 | |
| 622 | void *InsertPos = 0; |
| 623 | if (BlockPointerType *PT = |
| 624 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 625 | return QualType(PT, 0); |
| 626 | |
| 627 | // If the closure pointee type isn't canonical, this won't be a canonical |
| 628 | // type either so fill in the canonical type field. |
| 629 | QualType Canonical; |
| 630 | if (!T->isCanonical()) { |
| 631 | Canonical = getBlockPointerType(getCanonicalType(T)); |
| 632 | |
| 633 | // Get the new insert position for the node we care about. |
| 634 | BlockPointerType *NewIP = |
| 635 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 636 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 637 | } |
| 638 | BlockPointerType *New = new BlockPointerType(T, Canonical); |
| 639 | Types.push_back(New); |
| 640 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 641 | return QualType(New, 0); |
| 642 | } |
| 643 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 644 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 645 | /// to the specified type. |
| 646 | QualType ASTContext::getReferenceType(QualType T) { |
| 647 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 648 | // structure. |
| 649 | llvm::FoldingSetNodeID ID; |
| 650 | ReferenceType::Profile(ID, T); |
| 651 | |
| 652 | void *InsertPos = 0; |
| 653 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 654 | return QualType(RT, 0); |
| 655 | |
| 656 | // If the referencee type isn't canonical, this won't be a canonical type |
| 657 | // either, so fill in the canonical type field. |
| 658 | QualType Canonical; |
| 659 | if (!T->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 660 | Canonical = getReferenceType(getCanonicalType(T)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 661 | |
| 662 | // Get the new insert position for the node we care about. |
| 663 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 664 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 665 | } |
| 666 | |
| 667 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 668 | Types.push_back(New); |
| 669 | ReferenceTypes.InsertNode(New, InsertPos); |
| 670 | return QualType(New, 0); |
| 671 | } |
| 672 | |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 673 | /// getConstantArrayType - Return the unique reference to the type for an |
| 674 | /// array of the specified element type. |
| 675 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 676 | const llvm::APInt &ArySize, |
| 677 | ArrayType::ArraySizeModifier ASM, |
| 678 | unsigned EltTypeQuals) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 679 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 680 | ConstantArrayType::Profile(ID, EltTy, ArySize); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 681 | |
| 682 | void *InsertPos = 0; |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 683 | if (ConstantArrayType *ATP = |
| 684 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 685 | return QualType(ATP, 0); |
| 686 | |
| 687 | // If the element type isn't canonical, this won't be a canonical type either, |
| 688 | // so fill in the canonical type field. |
| 689 | QualType Canonical; |
| 690 | if (!EltTy->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 691 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 692 | ASM, EltTypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 693 | // Get the new insert position for the node we care about. |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 694 | ConstantArrayType *NewIP = |
| 695 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 696 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 697 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 698 | } |
| 699 | |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 700 | ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize, |
| 701 | ASM, EltTypeQuals); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 702 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 703 | Types.push_back(New); |
| 704 | return QualType(New, 0); |
| 705 | } |
| 706 | |
Steve Naroff | e2579e3 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 707 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 708 | /// variable array of the specified element type. |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 709 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
| 710 | ArrayType::ArraySizeModifier ASM, |
| 711 | unsigned EltTypeQuals) { |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 712 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 713 | // that have an expression provided for their size. |
| 714 | |
| 715 | VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 716 | ASM, EltTypeQuals); |
| 717 | |
| 718 | VariableArrayTypes.push_back(New); |
| 719 | Types.push_back(New); |
| 720 | return QualType(New, 0); |
| 721 | } |
| 722 | |
| 723 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 724 | ArrayType::ArraySizeModifier ASM, |
| 725 | unsigned EltTypeQuals) { |
| 726 | llvm::FoldingSetNodeID ID; |
| 727 | IncompleteArrayType::Profile(ID, EltTy); |
| 728 | |
| 729 | void *InsertPos = 0; |
| 730 | if (IncompleteArrayType *ATP = |
| 731 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 732 | return QualType(ATP, 0); |
| 733 | |
| 734 | // If the element type isn't canonical, this won't be a canonical type |
| 735 | // either, so fill in the canonical type field. |
| 736 | QualType Canonical; |
| 737 | |
| 738 | if (!EltTy->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 739 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 3793e1a | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 740 | ASM, EltTypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 741 | |
| 742 | // Get the new insert position for the node we care about. |
| 743 | IncompleteArrayType *NewIP = |
| 744 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 745 | |
| 746 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
Ted Kremenek | 3793e1a | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 747 | } |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 748 | |
| 749 | IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical, |
| 750 | ASM, EltTypeQuals); |
| 751 | |
| 752 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 753 | Types.push_back(New); |
| 754 | return QualType(New, 0); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 757 | /// getVectorType - Return the unique reference to a vector type of |
| 758 | /// the specified element type and size. VectorType must be a built-in type. |
| 759 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
| 760 | BuiltinType *baseType; |
| 761 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 762 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 763 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
| 764 | |
| 765 | // Check if we've already instantiated a vector of this type. |
| 766 | llvm::FoldingSetNodeID ID; |
| 767 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
| 768 | void *InsertPos = 0; |
| 769 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 770 | return QualType(VTP, 0); |
| 771 | |
| 772 | // If the element type isn't canonical, this won't be a canonical type either, |
| 773 | // so fill in the canonical type field. |
| 774 | QualType Canonical; |
| 775 | if (!vecType->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 776 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 777 | |
| 778 | // Get the new insert position for the node we care about. |
| 779 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 780 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 781 | } |
| 782 | VectorType *New = new VectorType(vecType, NumElts, Canonical); |
| 783 | VectorTypes.InsertNode(New, InsertPos); |
| 784 | Types.push_back(New); |
| 785 | return QualType(New, 0); |
| 786 | } |
| 787 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 788 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 789 | /// the specified element type and size. VectorType must be a built-in type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 790 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 791 | BuiltinType *baseType; |
| 792 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 793 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 794 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 795 | |
| 796 | // Check if we've already instantiated a vector of this type. |
| 797 | llvm::FoldingSetNodeID ID; |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 798 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 799 | void *InsertPos = 0; |
| 800 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 801 | return QualType(VTP, 0); |
| 802 | |
| 803 | // If the element type isn't canonical, this won't be a canonical type either, |
| 804 | // so fill in the canonical type field. |
| 805 | QualType Canonical; |
| 806 | if (!vecType->isCanonical()) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 807 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 808 | |
| 809 | // Get the new insert position for the node we care about. |
| 810 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 811 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 812 | } |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 813 | ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 814 | VectorTypes.InsertNode(New, InsertPos); |
| 815 | Types.push_back(New); |
| 816 | return QualType(New, 0); |
| 817 | } |
| 818 | |
| 819 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 820 | /// |
| 821 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 822 | // Unique functions, to guarantee there is only one function of a particular |
| 823 | // structure. |
| 824 | llvm::FoldingSetNodeID ID; |
| 825 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 826 | |
| 827 | void *InsertPos = 0; |
| 828 | if (FunctionTypeNoProto *FT = |
| 829 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 830 | return QualType(FT, 0); |
| 831 | |
| 832 | QualType Canonical; |
| 833 | if (!ResultTy->isCanonical()) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 834 | Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 835 | |
| 836 | // Get the new insert position for the node we care about. |
| 837 | FunctionTypeNoProto *NewIP = |
| 838 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 839 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 840 | } |
| 841 | |
| 842 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 843 | Types.push_back(New); |
Eli Friedman | aa0fdfd | 2008-02-25 22:11:40 +0000 | [diff] [blame] | 844 | FunctionTypeNoProtos.InsertNode(New, InsertPos); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 845 | return QualType(New, 0); |
| 846 | } |
| 847 | |
| 848 | /// getFunctionType - Return a normal function type with a typed argument |
| 849 | /// list. isVariadic indicates whether the argument list includes '...'. |
Eli Friedman | 36104c1 | 2008-08-22 00:59:49 +0000 | [diff] [blame] | 850 | QualType ASTContext::getFunctionType(QualType ResultTy, const QualType *ArgArray, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 851 | unsigned NumArgs, bool isVariadic) { |
| 852 | // Unique functions, to guarantee there is only one function of a particular |
| 853 | // structure. |
| 854 | llvm::FoldingSetNodeID ID; |
| 855 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic); |
| 856 | |
| 857 | void *InsertPos = 0; |
| 858 | if (FunctionTypeProto *FTP = |
| 859 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 860 | return QualType(FTP, 0); |
| 861 | |
| 862 | // Determine whether the type being created is already canonical or not. |
| 863 | bool isCanonical = ResultTy->isCanonical(); |
| 864 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 865 | if (!ArgArray[i]->isCanonical()) |
| 866 | isCanonical = false; |
| 867 | |
| 868 | // If this type isn't canonical, get the canonical version of it. |
| 869 | QualType Canonical; |
| 870 | if (!isCanonical) { |
| 871 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 872 | CanonicalArgs.reserve(NumArgs); |
| 873 | for (unsigned i = 0; i != NumArgs; ++i) |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 874 | CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 875 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 876 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 877 | &CanonicalArgs[0], NumArgs, |
| 878 | isVariadic); |
| 879 | |
| 880 | // Get the new insert position for the node we care about. |
| 881 | FunctionTypeProto *NewIP = |
| 882 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 883 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 884 | } |
| 885 | |
| 886 | // FunctionTypeProto objects are not allocated with new because they have a |
| 887 | // variable size array (for parameter types) at the end of them. |
| 888 | FunctionTypeProto *FTP = |
| 889 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
| 890 | NumArgs*sizeof(QualType)); |
| 891 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 892 | Canonical); |
| 893 | Types.push_back(FTP); |
| 894 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 895 | return QualType(FTP, 0); |
| 896 | } |
| 897 | |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 898 | /// getTypeDeclType - Return the unique reference to the type for the |
| 899 | /// specified type declaration. |
| 900 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl) { |
| 901 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 902 | |
| 903 | if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl)) |
| 904 | return getTypedefType(Typedef); |
| 905 | else if (ObjCInterfaceDecl *ObjCInterface |
| 906 | = dyn_cast_or_null<ObjCInterfaceDecl>(Decl)) |
| 907 | return getObjCInterfaceType(ObjCInterface); |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 908 | |
| 909 | if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl)) |
| 910 | Decl->TypeForDecl = new CXXRecordType(CXXRecord); |
| 911 | else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 912 | Decl->TypeForDecl = new RecordType(Record); |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 913 | else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 914 | Decl->TypeForDecl = new EnumType(Enum); |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 915 | else |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 916 | assert(false && "TypeDecl without a type?"); |
Argiris Kirtzidis | ea29d1e | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 917 | |
| 918 | Types.push_back(Decl->TypeForDecl); |
| 919 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 922 | /// getTypedefType - Return the unique reference to the type for the |
| 923 | /// specified typename decl. |
| 924 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 925 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 926 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 927 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 928 | Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 929 | Types.push_back(Decl->TypeForDecl); |
| 930 | return QualType(Decl->TypeForDecl, 0); |
| 931 | } |
| 932 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 933 | /// getObjCInterfaceType - Return the unique reference to the type for the |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 934 | /// specified ObjC interface decl. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 935 | QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) { |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 936 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 937 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 938 | Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl); |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 939 | Types.push_back(Decl->TypeForDecl); |
| 940 | return QualType(Decl->TypeForDecl, 0); |
| 941 | } |
| 942 | |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 943 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 944 | /// alphabetically. |
| 945 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 946 | const ObjCProtocolDecl *RHS) { |
| 947 | return strcmp(LHS->getName(), RHS->getName()) < 0; |
| 948 | } |
| 949 | |
| 950 | static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, |
| 951 | unsigned &NumProtocols) { |
| 952 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
| 953 | |
| 954 | // Sort protocols, keyed by name. |
| 955 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 956 | |
| 957 | // Remove duplicates. |
| 958 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 959 | NumProtocols = ProtocolsEnd-Protocols; |
| 960 | } |
| 961 | |
| 962 | |
Chris Lattner | b0c6a1f | 2008-04-07 04:44:08 +0000 | [diff] [blame] | 963 | /// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for |
| 964 | /// the given interface decl and the conforming protocol list. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 965 | QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, |
| 966 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 967 | // Sort the protocol list alphabetically to canonicalize it. |
| 968 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 969 | |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 970 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 7cdcb25 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 971 | ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 972 | |
| 973 | void *InsertPos = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 974 | if (ObjCQualifiedInterfaceType *QT = |
| 975 | ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 976 | return QualType(QT, 0); |
| 977 | |
| 978 | // No Match; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 979 | ObjCQualifiedInterfaceType *QType = |
| 980 | new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 981 | Types.push_back(QType); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 982 | ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 983 | return QualType(QType, 0); |
| 984 | } |
| 985 | |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 986 | /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl |
| 987 | /// and the conforming protocol list. |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 988 | QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols, |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 989 | unsigned NumProtocols) { |
Chris Lattner | e135230 | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 990 | // Sort the protocol list alphabetically to canonicalize it. |
| 991 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 992 | |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 993 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 994 | ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 995 | |
| 996 | void *InsertPos = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 997 | if (ObjCQualifiedIdType *QT = |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 998 | ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 999 | return QualType(QT, 0); |
| 1000 | |
| 1001 | // No Match; |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 1002 | ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1003 | Types.push_back(QType); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1004 | ObjCQualifiedIdTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1005 | return QualType(QType, 0); |
| 1006 | } |
| 1007 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1008 | /// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique |
| 1009 | /// TypeOfExpr AST's (since expression's are never shared). For example, |
| 1010 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 1011 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 1012 | /// on canonical type's (which are always unique). |
Steve Naroff | 11b649c | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 1013 | QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1014 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1015 | TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical); |
| 1016 | Types.push_back(toe); |
| 1017 | return QualType(toe, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1020 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 1021 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 1022 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 1023 | /// an issue. This doesn't effect the type checker, since it operates |
| 1024 | /// on canonical type's (which are always unique). |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1025 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1026 | QualType Canonical = getCanonicalType(tofType); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1027 | TypeOfType *tot = new TypeOfType(tofType, Canonical); |
| 1028 | Types.push_back(tot); |
| 1029 | return QualType(tot, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1032 | /// getTagDeclType - Return the unique reference to the type for the |
| 1033 | /// specified TagDecl (struct/union/class/enum) decl. |
| 1034 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
Ted Kremenek | ae8fa03 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 1035 | assert (Decl); |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1036 | return getTypeDeclType(Decl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 1040 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 1041 | /// needs to agree with the definition in <stddef.h>. |
| 1042 | QualType ASTContext::getSizeType() const { |
| 1043 | // On Darwin, size_t is defined as a "long unsigned int". |
| 1044 | // FIXME: should derive from "Target". |
| 1045 | return UnsignedLongTy; |
| 1046 | } |
| 1047 | |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 1048 | /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the |
Eli Friedman | fdd35d7 | 2008-02-12 08:29:21 +0000 | [diff] [blame] | 1049 | /// width of characters in wide strings, The value is target dependent and |
| 1050 | /// needs to agree with the definition in <stddef.h>. |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 1051 | QualType ASTContext::getWCharType() const { |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1052 | if (LangOpts.CPlusPlus) |
| 1053 | return WCharTy; |
| 1054 | |
Eli Friedman | fdd35d7 | 2008-02-12 08:29:21 +0000 | [diff] [blame] | 1055 | // On Darwin, wchar_t is defined as a "int". |
| 1056 | // FIXME: should derive from "Target". |
| 1057 | return IntTy; |
| 1058 | } |
| 1059 | |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1060 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 1061 | /// Used when in C++, as a GCC extension. |
| 1062 | QualType ASTContext::getSignedWCharType() const { |
| 1063 | // FIXME: derive from "Target" ? |
| 1064 | return WCharTy; |
| 1065 | } |
| 1066 | |
| 1067 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 1068 | /// Used when in C++, as a GCC extension. |
| 1069 | QualType ASTContext::getUnsignedWCharType() const { |
| 1070 | // FIXME: derive from "Target" ? |
| 1071 | return UnsignedIntTy; |
| 1072 | } |
| 1073 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1074 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 1075 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 1076 | QualType ASTContext::getPointerDiffType() const { |
| 1077 | // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug... |
| 1078 | // FIXME: should derive from "Target". |
| 1079 | return IntTy; |
| 1080 | } |
| 1081 | |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1082 | //===----------------------------------------------------------------------===// |
| 1083 | // Type Operators |
| 1084 | //===----------------------------------------------------------------------===// |
| 1085 | |
Chris Lattner | 3dae6f4 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1086 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 1087 | /// the specified potentially non-canonical type. The non-canonical version |
| 1088 | /// of a type may have many "decorated" versions of types. Decorators can |
| 1089 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 1090 | /// to be free of any of these, allowing two canonical types to be compared |
| 1091 | /// for exact equality with a simple pointer comparison. |
| 1092 | QualType ASTContext::getCanonicalType(QualType T) { |
| 1093 | QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1094 | |
| 1095 | // If the result has type qualifiers, make sure to canonicalize them as well. |
| 1096 | unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); |
| 1097 | if (TypeQuals == 0) return CanType; |
| 1098 | |
| 1099 | // If the type qualifiers are on an array type, get the canonical type of the |
| 1100 | // array with the qualifiers applied to the element type. |
| 1101 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 1102 | if (!AT) |
| 1103 | return CanType.getQualifiedType(TypeQuals); |
| 1104 | |
| 1105 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1106 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1107 | QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); |
| 1108 | NewEltTy = getCanonicalType(NewEltTy); |
| 1109 | |
| 1110 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1111 | return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(), |
| 1112 | CAT->getIndexTypeQualifier()); |
| 1113 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
| 1114 | return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
| 1115 | IAT->getIndexTypeQualifier()); |
| 1116 | |
| 1117 | // FIXME: What is the ownership of size expressions in VLAs? |
| 1118 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
| 1119 | return getVariableArrayType(NewEltTy, VAT->getSizeExpr(), |
| 1120 | VAT->getSizeModifier(), |
| 1121 | VAT->getIndexTypeQualifier()); |
| 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 1126 | // Handle the non-qualified case efficiently. |
| 1127 | if (T.getCVRQualifiers() == 0) { |
| 1128 | // Handle the common positive case fast. |
| 1129 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 1130 | return AT; |
| 1131 | } |
| 1132 | |
| 1133 | // Handle the common negative case fast, ignoring CVR qualifiers. |
| 1134 | QualType CType = T->getCanonicalTypeInternal(); |
| 1135 | |
| 1136 | // Make sure to look through type qualifiers (like ASQuals) for the negative |
| 1137 | // test. |
| 1138 | if (!isa<ArrayType>(CType) && |
| 1139 | !isa<ArrayType>(CType.getUnqualifiedType())) |
| 1140 | return 0; |
| 1141 | |
| 1142 | // Apply any CVR qualifiers from the array type to the element type. This |
| 1143 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 1144 | // any type qualifiers, the element type is so qualified, not the array type." |
| 1145 | |
| 1146 | // If we get here, we either have type qualifiers on the type, or we have |
| 1147 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
| 1148 | // we must propagate them down into the elemeng type. |
| 1149 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 1150 | unsigned AddrSpace = 0; |
| 1151 | Type *Ty = T.getTypePtr(); |
| 1152 | |
| 1153 | // Rip through ASQualType's and typedefs to get to a concrete type. |
| 1154 | while (1) { |
| 1155 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) { |
| 1156 | AddrSpace = ASQT->getAddressSpace(); |
| 1157 | Ty = ASQT->getBaseType(); |
| 1158 | } else { |
| 1159 | T = Ty->getDesugaredType(); |
| 1160 | if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) |
| 1161 | break; |
| 1162 | CVRQuals |= T.getCVRQualifiers(); |
| 1163 | Ty = T.getTypePtr(); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | // If we have a simple case, just return now. |
| 1168 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
| 1169 | if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) |
| 1170 | return ATy; |
| 1171 | |
| 1172 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 1173 | // qualifiers into the array element type and return a new array type. |
| 1174 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1175 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1176 | QualType NewEltTy = ATy->getElementType(); |
| 1177 | if (AddrSpace) |
| 1178 | NewEltTy = getASQualType(NewEltTy, AddrSpace); |
| 1179 | NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); |
| 1180 | |
| 1181 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 1182 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 1183 | CAT->getSizeModifier(), |
| 1184 | CAT->getIndexTypeQualifier())); |
| 1185 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 1186 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 1187 | IAT->getSizeModifier(), |
| 1188 | IAT->getIndexTypeQualifier())); |
| 1189 | |
| 1190 | // FIXME: What is the ownership of size expressions in VLAs? |
| 1191 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
| 1192 | return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(), |
| 1193 | VAT->getSizeModifier(), |
| 1194 | VAT->getIndexTypeQualifier())); |
Chris Lattner | 3dae6f4 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1198 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 1199 | /// specified array type to a pointer. This operation is non-trivial when |
| 1200 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 1201 | /// this returns a pointer to a properly qualified element of the array. |
| 1202 | /// |
| 1203 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 1204 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1205 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 1206 | // typedefs in the element type of the array. This also handles propagation |
| 1207 | // of type qualifiers from the array type into the element type if present |
| 1208 | // (C99 6.7.3p8). |
| 1209 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 1210 | assert(PrettyArrayType && "Not an array type!"); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1212 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1213 | |
| 1214 | // int x[restrict 4] -> int *restrict |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1215 | return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1218 | /// getFloatingRank - Return a relative rank for floating point types. |
| 1219 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1220 | static FloatingRank getFloatingRank(QualType T) { |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1221 | if (const ComplexType *CT = T->getAsComplexType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1222 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1223 | |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1224 | switch (T->getAsBuiltinType()->getKind()) { |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1225 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1226 | case BuiltinType::Float: return FloatRank; |
| 1227 | case BuiltinType::Double: return DoubleRank; |
| 1228 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 1229 | } |
| 1230 | } |
| 1231 | |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 1232 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 1233 | /// point or a complex type (based on typeDomain/typeSize). |
| 1234 | /// 'typeDomain' is a real floating point or complex type. |
| 1235 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 7794ae2 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 1236 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 1237 | QualType Domain) const { |
| 1238 | FloatingRank EltRank = getFloatingRank(Size); |
| 1239 | if (Domain->isComplexType()) { |
| 1240 | switch (EltRank) { |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 1241 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1242 | case FloatRank: return FloatComplexTy; |
| 1243 | case DoubleRank: return DoubleComplexTy; |
| 1244 | case LongDoubleRank: return LongDoubleComplexTy; |
| 1245 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1246 | } |
Chris Lattner | 7794ae2 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 1247 | |
| 1248 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 1249 | switch (EltRank) { |
| 1250 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 1251 | case FloatRank: return FloatTy; |
| 1252 | case DoubleRank: return DoubleTy; |
| 1253 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1254 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1257 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 1258 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 1259 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 1260 | /// LHS < RHS, return -1. |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1261 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 1262 | FloatingRank LHSR = getFloatingRank(LHS); |
| 1263 | FloatingRank RHSR = getFloatingRank(RHS); |
| 1264 | |
| 1265 | if (LHSR == RHSR) |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1266 | return 0; |
Chris Lattner | d7135b4 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1267 | if (LHSR > RHSR) |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1268 | return 1; |
| 1269 | return -1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1272 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 1273 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 1274 | /// or if it is not canonicalized. |
| 1275 | static unsigned getIntegerRank(Type *T) { |
| 1276 | assert(T->isCanonical() && "T should be canonicalized"); |
| 1277 | if (isa<EnumType>(T)) |
| 1278 | return 4; |
| 1279 | |
| 1280 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1281 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 1282 | case BuiltinType::Bool: |
| 1283 | return 1; |
| 1284 | case BuiltinType::Char_S: |
| 1285 | case BuiltinType::Char_U: |
| 1286 | case BuiltinType::SChar: |
| 1287 | case BuiltinType::UChar: |
| 1288 | return 2; |
| 1289 | case BuiltinType::Short: |
| 1290 | case BuiltinType::UShort: |
| 1291 | return 3; |
| 1292 | case BuiltinType::Int: |
| 1293 | case BuiltinType::UInt: |
| 1294 | return 4; |
| 1295 | case BuiltinType::Long: |
| 1296 | case BuiltinType::ULong: |
| 1297 | return 5; |
| 1298 | case BuiltinType::LongLong: |
| 1299 | case BuiltinType::ULongLong: |
| 1300 | return 6; |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1304 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
| 1305 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 1306 | /// LHS < RHS, return -1. |
| 1307 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1308 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 1309 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1310 | if (LHSC == RHSC) return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1312 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 1313 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1315 | unsigned LHSRank = getIntegerRank(LHSC); |
| 1316 | unsigned RHSRank = getIntegerRank(RHSC); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1317 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1318 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 1319 | if (LHSRank == RHSRank) return 0; |
| 1320 | return LHSRank > RHSRank ? 1 : -1; |
| 1321 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1323 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 1324 | if (LHSUnsigned) { |
| 1325 | // If the unsigned [LHS] type is larger, return it. |
| 1326 | if (LHSRank >= RHSRank) |
| 1327 | return 1; |
| 1328 | |
| 1329 | // If the signed type can represent all values of the unsigned type, it |
| 1330 | // wins. Because we are dealing with 2's complement and types that are |
| 1331 | // powers of two larger than each other, this is always safe. |
| 1332 | return -1; |
| 1333 | } |
Chris Lattner | c1b68db | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | 51285d8 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1335 | // If the unsigned [RHS] type is larger, return it. |
| 1336 | if (RHSRank >= LHSRank) |
| 1337 | return -1; |
| 1338 | |
| 1339 | // If the signed type can represent all values of the unsigned type, it |
| 1340 | // wins. Because we are dealing with 2's complement and types that are |
| 1341 | // powers of two larger than each other, this is always safe. |
| 1342 | return 1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1343 | } |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1344 | |
| 1345 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 1346 | QualType ASTContext::getCFConstantStringType() { |
| 1347 | if (!CFConstantStringTypeDecl) { |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 1348 | CFConstantStringTypeDecl = |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1349 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1350 | &Idents.get("NSConstantString"), 0); |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1351 | QualType FieldTypes[4]; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1352 | |
| 1353 | // const int *isa; |
| 1354 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1355 | // int flags; |
| 1356 | FieldTypes[1] = IntTy; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1357 | // const char *str; |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1358 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1359 | // long length; |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1360 | FieldTypes[3] = LongTy; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1361 | // Create fields |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1362 | FieldDecl *FieldDecls[4]; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1363 | |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1364 | for (unsigned i = 0; i < 4; ++i) |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1365 | FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0, |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 1366 | FieldTypes[i]); |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1367 | |
| 1368 | CFConstantStringTypeDecl->defineBody(FieldDecls, 4); |
| 1369 | } |
| 1370 | |
| 1371 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 61ce98c | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 1372 | } |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1373 | |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1374 | // This returns true if a type has been typedefed to BOOL: |
| 1375 | // typedef <type> BOOL; |
Chris Lattner | cb034cb | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 1376 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1377 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | cb034cb | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 1378 | return !strcmp(TT->getDecl()->getName(), "BOOL"); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1379 | |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1383 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1384 | /// purpose. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1385 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1386 | uint64_t sz = getTypeSize(type); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Make all integer and enum types at least as large as an int |
| 1389 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1390 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1391 | // Treat arrays as pointers, since that's how they're passed in. |
| 1392 | else if (type->isArrayType()) |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1393 | sz = getTypeSize(VoidPtrTy); |
| 1394 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1397 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1398 | /// declaration. |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame^] | 1399 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1400 | std::string& S) |
| 1401 | { |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame^] | 1402 | // FIXME: This is not very efficient. |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1403 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1404 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1405 | // Encode result type. |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1406 | getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1407 | // Compute size of all parameters. |
| 1408 | // Start with computing size of a pointer in number of bytes. |
| 1409 | // FIXME: There might(should) be a better way of doing this computation! |
| 1410 | SourceLocation Loc; |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1411 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1412 | // The first two arguments (self and _cmd) are pointers; account for |
| 1413 | // their size. |
| 1414 | int ParmOffset = 2 * PtrSize; |
| 1415 | int NumOfParams = Decl->getNumParams(); |
| 1416 | for (int i = 0; i < NumOfParams; i++) { |
| 1417 | QualType PType = Decl->getParamDecl(i)->getType(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1418 | int sz = getObjCEncodingTypeSize (PType); |
| 1419 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1420 | ParmOffset += sz; |
| 1421 | } |
| 1422 | S += llvm::utostr(ParmOffset); |
| 1423 | S += "@0:"; |
| 1424 | S += llvm::utostr(PtrSize); |
| 1425 | |
| 1426 | // Argument types. |
| 1427 | ParmOffset = 2 * PtrSize; |
| 1428 | for (int i = 0; i < NumOfParams; i++) { |
| 1429 | QualType PType = Decl->getParamDecl(i)->getType(); |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1430 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1431 | // 'in', 'inout', etc. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1432 | getObjCEncodingForTypeQualifier( |
| 1433 | Decl->getParamDecl(i)->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1434 | getObjCEncodingForType(PType, S, EncodingRecordTypes); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1435 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1436 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
Daniel Dunbar | 698d6f3 | 2008-08-28 04:38:10 +0000 | [diff] [blame^] | 1440 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
| 1441 | /// method declaration. If non-NULL, Container must be either an |
| 1442 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 1443 | /// NULL when getting encodings for protocol properties. |
| 1444 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 1445 | const Decl *Container, |
| 1446 | std::string& S) |
| 1447 | { |
| 1448 | // Collect information from the property implementation decl(s). |
| 1449 | bool Dynamic = false; |
| 1450 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 1451 | |
| 1452 | // FIXME: Duplicated code due to poor abstraction. |
| 1453 | if (Container) { |
| 1454 | if (const ObjCCategoryImplDecl *CID = |
| 1455 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 1456 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1457 | i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) { |
| 1458 | ObjCPropertyImplDecl *PID = *i; |
| 1459 | if (PID->getPropertyDecl() == PD) { |
| 1460 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 1461 | Dynamic = true; |
| 1462 | } else { |
| 1463 | SynthesizePID = PID; |
| 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | } else { |
| 1468 | const ObjCImplementationDecl *OID = cast<ObjCImplementationDecl>(Container); |
| 1469 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1470 | i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) { |
| 1471 | ObjCPropertyImplDecl *PID = *i; |
| 1472 | if (PID->getPropertyDecl() == PD) { |
| 1473 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 1474 | Dynamic = true; |
| 1475 | } else { |
| 1476 | SynthesizePID = PID; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | // FIXME: This is not very efficient. |
| 1484 | S = "T"; |
| 1485 | |
| 1486 | // Encode result type. |
| 1487 | // FIXME: GCC uses a generating_property_type_encoding mode during |
| 1488 | // this part. Investigate. |
| 1489 | getObjCEncodingForType(PD->getType(), S, EncodingRecordTypes); |
| 1490 | |
| 1491 | if (PD->isReadOnly()) { |
| 1492 | S += ",R"; |
| 1493 | } else { |
| 1494 | switch (PD->getSetterKind()) { |
| 1495 | case ObjCPropertyDecl::Assign: break; |
| 1496 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
| 1497 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | // It really isn't clear at all what this means, since properties |
| 1502 | // are "dynamic by default". |
| 1503 | if (Dynamic) |
| 1504 | S += ",D"; |
| 1505 | |
| 1506 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 1507 | S += ",G"; |
| 1508 | S += PD->getGetterName().getName(); |
| 1509 | } |
| 1510 | |
| 1511 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 1512 | S += ",S"; |
| 1513 | S += PD->getSetterName().getName(); |
| 1514 | } |
| 1515 | |
| 1516 | if (SynthesizePID) { |
| 1517 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 1518 | S += ",V"; |
| 1519 | S += OID->getName(); |
| 1520 | } |
| 1521 | |
| 1522 | // FIXME: OBJCGC: weak & strong |
| 1523 | } |
| 1524 | |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1525 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1526 | llvm::SmallVector<const RecordType *, 8> &ERType) const { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1527 | // FIXME: This currently doesn't encode: |
| 1528 | // @ An object (whether statically typed or typed id) |
| 1529 | // # A class object (Class) |
| 1530 | // : A method selector (SEL) |
| 1531 | // {name=type...} A structure |
| 1532 | // (name=type...) A union |
| 1533 | // bnum A bit field of num bits |
| 1534 | |
| 1535 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1536 | char encoding; |
| 1537 | switch (BT->getKind()) { |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1538 | default: assert(0 && "Unhandled builtin type kind"); |
| 1539 | case BuiltinType::Void: encoding = 'v'; break; |
| 1540 | case BuiltinType::Bool: encoding = 'B'; break; |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1541 | case BuiltinType::Char_U: |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1542 | case BuiltinType::UChar: encoding = 'C'; break; |
| 1543 | case BuiltinType::UShort: encoding = 'S'; break; |
| 1544 | case BuiltinType::UInt: encoding = 'I'; break; |
| 1545 | case BuiltinType::ULong: encoding = 'L'; break; |
| 1546 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1547 | case BuiltinType::Char_S: |
Chris Lattner | 2bf1d6c | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1548 | case BuiltinType::SChar: encoding = 'c'; break; |
| 1549 | case BuiltinType::Short: encoding = 's'; break; |
| 1550 | case BuiltinType::Int: encoding = 'i'; break; |
| 1551 | case BuiltinType::Long: encoding = 'l'; break; |
| 1552 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 1553 | case BuiltinType::Float: encoding = 'f'; break; |
| 1554 | case BuiltinType::Double: encoding = 'd'; break; |
| 1555 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | S += encoding; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1559 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1560 | else if (T->isObjCQualifiedIdType()) { |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1561 | // Treat id<P...> same as 'id' for encoding purposes. |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1562 | return getObjCEncodingForType(getObjCIdType(), S, ERType); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1563 | |
| 1564 | } |
| 1565 | else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1566 | QualType PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1567 | if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) { |
Fariborz Jahanian | 80faffa | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1568 | S += '@'; |
| 1569 | return; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1570 | } else if (isObjCClassType(PointeeTy)) { |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1571 | S += '#'; |
| 1572 | return; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1573 | } else if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1574 | S += ':'; |
| 1575 | return; |
Fariborz Jahanian | 80faffa | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1576 | } |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1577 | |
| 1578 | if (PointeeTy->isCharType()) { |
| 1579 | // char pointer types should be encoded as '*' unless it is a |
| 1580 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1581 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1582 | S += '*'; |
| 1583 | return; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | S += '^'; |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1588 | getObjCEncodingForType(PT->getPointeeType(), S, ERType); |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1589 | } else if (const ArrayType *AT = |
| 1590 | // Ignore type qualifiers etc. |
| 1591 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1592 | S += '['; |
| 1593 | |
| 1594 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1595 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 1596 | else |
| 1597 | assert(0 && "Unhandled array type!"); |
| 1598 | |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1599 | getObjCEncodingForType(AT->getElementType(), S, ERType); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1600 | S += ']'; |
Anders Carlsson | 5695bb7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 1601 | } else if (T->getAsFunctionType()) { |
| 1602 | S += '?'; |
Fariborz Jahanian | c8ba2bd | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1603 | } else if (const RecordType *RTy = T->getAsRecordType()) { |
| 1604 | RecordDecl *RDecl= RTy->getDecl(); |
Steve Naroff | 0338529 | 2008-08-14 15:00:38 +0000 | [diff] [blame] | 1605 | // This mimics the behavior in gcc's encode_aggregate_within(). |
| 1606 | // The idea is to only inline structure definitions for top level pointers |
| 1607 | // to structures and embedded structures. |
| 1608 | bool inlining = (S.size() == 1 && S[0] == '^' || |
| 1609 | S.size() > 1 && S[S.size()-1] != '^'); |
Fariborz Jahanian | c8ba2bd | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1610 | S += '{'; |
| 1611 | S += RDecl->getName(); |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1612 | bool found = false; |
| 1613 | for (unsigned i = 0, e = ERType.size(); i != e; ++i) |
| 1614 | if (ERType[i] == RTy) { |
| 1615 | found = true; |
| 1616 | break; |
| 1617 | } |
Steve Naroff | 0338529 | 2008-08-14 15:00:38 +0000 | [diff] [blame] | 1618 | if (!found && inlining) { |
Fariborz Jahanian | 248db26 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1619 | ERType.push_back(RTy); |
| 1620 | S += '='; |
| 1621 | for (int i = 0; i < RDecl->getNumMembers(); i++) { |
| 1622 | FieldDecl *field = RDecl->getMember(i); |
| 1623 | getObjCEncodingForType(field->getType(), S, ERType); |
| 1624 | } |
| 1625 | assert(ERType.back() == RTy && "Record Type stack mismatch."); |
| 1626 | ERType.pop_back(); |
Fariborz Jahanian | c8ba2bd | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1627 | } |
| 1628 | S += '}'; |
Steve Naroff | 49af3f3 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 1629 | } else if (T->isEnumeralType()) { |
| 1630 | S += 'i'; |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1631 | } else |
Steve Naroff | 53b6f4c | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 1632 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1635 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1636 | std::string& S) const { |
| 1637 | if (QT & Decl::OBJC_TQ_In) |
| 1638 | S += 'n'; |
| 1639 | if (QT & Decl::OBJC_TQ_Inout) |
| 1640 | S += 'N'; |
| 1641 | if (QT & Decl::OBJC_TQ_Out) |
| 1642 | S += 'o'; |
| 1643 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 1644 | S += 'O'; |
| 1645 | if (QT & Decl::OBJC_TQ_Byref) |
| 1646 | S += 'R'; |
| 1647 | if (QT & Decl::OBJC_TQ_Oneway) |
| 1648 | S += 'V'; |
| 1649 | } |
| 1650 | |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1651 | void ASTContext::setBuiltinVaListType(QualType T) |
| 1652 | { |
| 1653 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 1654 | |
| 1655 | BuiltinVaListType = T; |
| 1656 | } |
| 1657 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1658 | void ASTContext::setObjCIdType(TypedefDecl *TD) |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1659 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1660 | assert(ObjCIdType.isNull() && "'id' type already set!"); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1661 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1662 | ObjCIdType = getTypedefType(TD); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1663 | |
| 1664 | // typedef struct objc_object *id; |
| 1665 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1666 | assert(ptr && "'id' incorrectly typed"); |
| 1667 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1668 | assert(rec && "'id' incorrectly typed"); |
| 1669 | IdStructType = rec; |
| 1670 | } |
| 1671 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1672 | void ASTContext::setObjCSelType(TypedefDecl *TD) |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1673 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1674 | assert(ObjCSelType.isNull() && "'SEL' type already set!"); |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1675 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1676 | ObjCSelType = getTypedefType(TD); |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1677 | |
| 1678 | // typedef struct objc_selector *SEL; |
| 1679 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1680 | assert(ptr && "'SEL' incorrectly typed"); |
| 1681 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1682 | assert(rec && "'SEL' incorrectly typed"); |
| 1683 | SelStructType = rec; |
| 1684 | } |
| 1685 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1686 | void ASTContext::setObjCProtoType(QualType QT) |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1687 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1688 | assert(ObjCProtoType.isNull() && "'Protocol' type already set!"); |
| 1689 | ObjCProtoType = QT; |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1692 | void ASTContext::setObjCClassType(TypedefDecl *TD) |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1693 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1694 | assert(ObjCClassType.isNull() && "'Class' type already set!"); |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1695 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1696 | ObjCClassType = getTypedefType(TD); |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1697 | |
| 1698 | // typedef struct objc_class *Class; |
| 1699 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1700 | assert(ptr && "'Class' incorrectly typed"); |
| 1701 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1702 | assert(rec && "'Class' incorrectly typed"); |
| 1703 | ClassStructType = rec; |
| 1704 | } |
| 1705 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1706 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
| 1707 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1708 | "'NSConstantString' type already set!"); |
| 1709 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1710 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Ted Kremenek | 118930e | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 1713 | |
| 1714 | //===----------------------------------------------------------------------===// |
| 1715 | // Type Predicates. |
| 1716 | //===----------------------------------------------------------------------===// |
| 1717 | |
| 1718 | /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer |
| 1719 | /// to an object type. This includes "id" and "Class" (two 'special' pointers |
| 1720 | /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified |
| 1721 | /// ID type). |
| 1722 | bool ASTContext::isObjCObjectPointerType(QualType Ty) const { |
| 1723 | if (Ty->isObjCQualifiedIdType()) |
| 1724 | return true; |
| 1725 | |
| 1726 | if (!Ty->isPointerType()) |
| 1727 | return false; |
| 1728 | |
| 1729 | // Check to see if this is 'id' or 'Class', both of which are typedefs for |
| 1730 | // pointer types. This looks for the typedef specifically, not for the |
| 1731 | // underlying type. |
| 1732 | if (Ty == getObjCIdType() || Ty == getObjCClassType()) |
| 1733 | return true; |
| 1734 | |
| 1735 | // If this a pointer to an interface (e.g. NSString*), it is ok. |
| 1736 | return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType(); |
| 1737 | } |
| 1738 | |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1739 | //===----------------------------------------------------------------------===// |
| 1740 | // Type Compatibility Testing |
| 1741 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5003e8b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1742 | |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1743 | /// areCompatVectorTypes - Return true if the two specified vector types are |
| 1744 | /// compatible. |
| 1745 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 1746 | const VectorType *RHS) { |
| 1747 | assert(LHS->isCanonical() && RHS->isCanonical()); |
| 1748 | return LHS->getElementType() == RHS->getElementType() && |
| 1749 | LHS->getNumElements() == RHS->getNumElements(); |
| 1750 | } |
| 1751 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1752 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1753 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 1754 | /// protocol qualifiers on the LHS or RHS. |
| 1755 | /// |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1756 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 1757 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ff358b | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1758 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 1759 | // the LHS. |
| 1760 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 1761 | return false; |
| 1762 | |
| 1763 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 1764 | // protocol qualified at all, then we are good. |
| 1765 | if (!isa<ObjCQualifiedInterfaceType>(LHS)) |
| 1766 | return true; |
| 1767 | |
| 1768 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 1769 | // isn't a superset. |
| 1770 | if (!isa<ObjCQualifiedInterfaceType>(RHS)) |
| 1771 | return true; // FIXME: should return false! |
| 1772 | |
| 1773 | // Finally, we must have two protocol-qualified interfaces. |
| 1774 | const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS); |
| 1775 | const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS); |
| 1776 | ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(); |
| 1777 | ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end(); |
| 1778 | ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(); |
| 1779 | ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end(); |
| 1780 | |
| 1781 | // All protocols in LHS must have a presence in RHS. Since the protocol lists |
| 1782 | // are both sorted alphabetically and have no duplicates, we can scan RHS and |
| 1783 | // LHS in a single parallel scan until we run out of elements in LHS. |
| 1784 | assert(LHSPI != LHSPE && "Empty LHS protocol list?"); |
| 1785 | ObjCProtocolDecl *LHSProto = *LHSPI; |
| 1786 | |
| 1787 | while (RHSPI != RHSPE) { |
| 1788 | ObjCProtocolDecl *RHSProto = *RHSPI++; |
| 1789 | // If the RHS has a protocol that the LHS doesn't, ignore it. |
| 1790 | if (RHSProto != LHSProto) |
| 1791 | continue; |
| 1792 | |
| 1793 | // Otherwise, the RHS does have this element. |
| 1794 | ++LHSPI; |
| 1795 | if (LHSPI == LHSPE) |
| 1796 | return true; // All protocols in LHS exist in RHS. |
| 1797 | |
| 1798 | LHSProto = *LHSPI; |
| 1799 | } |
| 1800 | |
| 1801 | // If we got here, we didn't find one of the LHS's protocols in the RHS list. |
| 1802 | return false; |
| 1803 | } |
| 1804 | |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1805 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 1806 | /// both shall have the identically qualified version of a compatible type. |
| 1807 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 1808 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1809 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 1810 | return !mergeTypes(LHS, RHS).isNull(); |
| 1811 | } |
| 1812 | |
| 1813 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
| 1814 | const FunctionType *lbase = lhs->getAsFunctionType(); |
| 1815 | const FunctionType *rbase = rhs->getAsFunctionType(); |
| 1816 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 1817 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 1818 | bool allLTypes = true; |
| 1819 | bool allRTypes = true; |
| 1820 | |
| 1821 | // Check return type |
| 1822 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 1823 | if (retType.isNull()) return QualType(); |
| 1824 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) allLTypes = false; |
| 1825 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) allRTypes = false; |
| 1826 | |
| 1827 | if (lproto && rproto) { // two C99 style function prototypes |
| 1828 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 1829 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 1830 | |
| 1831 | // Compatible functions must have the same number of arguments |
| 1832 | if (lproto_nargs != rproto_nargs) |
| 1833 | return QualType(); |
| 1834 | |
| 1835 | // Variadic and non-variadic functions aren't compatible |
| 1836 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 1837 | return QualType(); |
| 1838 | |
| 1839 | // Check argument compatibility |
| 1840 | llvm::SmallVector<QualType, 10> types; |
| 1841 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 1842 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 1843 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 1844 | QualType argtype = mergeTypes(largtype, rargtype); |
| 1845 | if (argtype.isNull()) return QualType(); |
| 1846 | types.push_back(argtype); |
| 1847 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) allLTypes = false; |
| 1848 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) allRTypes = false; |
| 1849 | } |
| 1850 | if (allLTypes) return lhs; |
| 1851 | if (allRTypes) return rhs; |
| 1852 | return getFunctionType(retType, types.begin(), types.size(), |
| 1853 | lproto->isVariadic()); |
| 1854 | } |
| 1855 | |
| 1856 | if (lproto) allRTypes = false; |
| 1857 | if (rproto) allLTypes = false; |
| 1858 | |
| 1859 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 1860 | if (proto) { |
| 1861 | if (proto->isVariadic()) return QualType(); |
| 1862 | // Check that the types are compatible with the types that |
| 1863 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 1864 | // The only types actually affected are promotable integer |
| 1865 | // types and floats, which would be passed as a different |
| 1866 | // type depending on whether the prototype is visible. |
| 1867 | unsigned proto_nargs = proto->getNumArgs(); |
| 1868 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 1869 | QualType argTy = proto->getArgType(i); |
| 1870 | if (argTy->isPromotableIntegerType() || |
| 1871 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 1872 | return QualType(); |
| 1873 | } |
| 1874 | |
| 1875 | if (allLTypes) return lhs; |
| 1876 | if (allRTypes) return rhs; |
| 1877 | return getFunctionType(retType, proto->arg_type_begin(), |
| 1878 | proto->getNumArgs(), lproto->isVariadic()); |
| 1879 | } |
| 1880 | |
| 1881 | if (allLTypes) return lhs; |
| 1882 | if (allRTypes) return rhs; |
| 1883 | return getFunctionTypeNoProto(retType); |
| 1884 | } |
| 1885 | |
| 1886 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 6a9d854 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 1887 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 1888 | // type is adjusted to "T" prior to any further analysis, the expression |
| 1889 | // designates the object or function denoted by the reference, and the |
| 1890 | // expression is an lvalue. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1891 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 1892 | // enough that they should be handled separately. |
| 1893 | if (const ReferenceType *RT = LHS->getAsReferenceType()) |
Chris Lattner | 855fed4 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 1894 | LHS = RT->getPointeeType(); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1895 | if (const ReferenceType *RT = RHS->getAsReferenceType()) |
Chris Lattner | 855fed4 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 1896 | RHS = RT->getPointeeType(); |
Chris Lattner | d47d604 | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 1897 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1898 | QualType LHSCan = getCanonicalType(LHS), |
| 1899 | RHSCan = getCanonicalType(RHS); |
| 1900 | |
| 1901 | // If two types are identical, they are compatible. |
| 1902 | if (LHSCan == RHSCan) |
| 1903 | return LHS; |
| 1904 | |
| 1905 | // If the qualifiers are different, the types aren't compatible |
| 1906 | if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() || |
| 1907 | LHSCan.getAddressSpace() != RHSCan.getAddressSpace()) |
| 1908 | return QualType(); |
| 1909 | |
| 1910 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 1911 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
| 1912 | |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 1913 | // We want to consider the two function types to be the same for these |
| 1914 | // comparisons, just force one to the other. |
| 1915 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 1916 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 398837e | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 1917 | |
| 1918 | // Same as above for arrays |
Chris Lattner | b5709e2 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 1919 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 1920 | LHSClass = Type::ConstantArray; |
| 1921 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 1922 | RHSClass = Type::ConstantArray; |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1923 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1924 | // Canonicalize ExtVector -> Vector. |
| 1925 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 1926 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Chris Lattner | b5709e2 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 1927 | |
Chris Lattner | 7cdcb25 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 1928 | // Consider qualified interfaces and interfaces the same. |
| 1929 | if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface; |
| 1930 | if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | b5709e2 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 1932 | // If the canonical type classes don't match. |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 1933 | if (LHSClass != RHSClass) { |
Steve Naroff | 4454977 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 1934 | // ID is compatible with all qualified id types. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1935 | if (LHS->isObjCQualifiedIdType()) { |
Steve Naroff | 4454977 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 1936 | if (const PointerType *PT = RHS->getAsPointerType()) |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1937 | if (isObjCIdType(PT->getPointeeType())) |
| 1938 | return LHS; |
Steve Naroff | 4454977 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 1939 | } |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1940 | if (RHS->isObjCQualifiedIdType()) { |
Steve Naroff | 4454977 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 1941 | if (const PointerType *PT = LHS->getAsPointerType()) |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1942 | if (isObjCIdType(PT->getPointeeType())) |
| 1943 | return RHS; |
| 1944 | } |
| 1945 | |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 1946 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 1947 | // a signed integer type, or an unsigned integer type. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1948 | if (const EnumType* ETy = LHS->getAsEnumType()) { |
| 1949 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 1950 | return RHS; |
Eli Friedman | ad6c06c | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 1951 | } |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1952 | if (const EnumType* ETy = RHS->getAsEnumType()) { |
| 1953 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 1954 | return LHS; |
Eli Friedman | ad6c06c | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 1955 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 1956 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1957 | return QualType(); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1958 | } |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1959 | |
Steve Naroff | c88babe | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 1960 | // The canonical type classes match. |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 1961 | switch (LHSClass) { |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 1962 | case Type::Pointer: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1963 | { |
| 1964 | // Merge two pointer types, while trying to preserve typedef info |
| 1965 | QualType LHSPointee = LHS->getAsPointerType()->getPointeeType(); |
| 1966 | QualType RHSPointee = RHS->getAsPointerType()->getPointeeType(); |
| 1967 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 1968 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | c91a3f3 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 1969 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) return LHS; |
| 1970 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) return RHS; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1971 | return getPointerType(ResultType); |
| 1972 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 1973 | case Type::ConstantArray: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1974 | { |
| 1975 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 1976 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 1977 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 1978 | return QualType(); |
| 1979 | |
| 1980 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 1981 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 1982 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 1983 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | c91a3f3 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 1984 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 1985 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
| 1986 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 1987 | ArrayType::ArraySizeModifier(), 0); |
| 1988 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 1989 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1990 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 1991 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Eli Friedman | c91a3f3 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 1992 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 1993 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1994 | if (LVAT) { |
| 1995 | // FIXME: This isn't correct! But tricky to implement because |
| 1996 | // the array's size has to be the size of LHS, but the type |
| 1997 | // has to be different. |
| 1998 | return LHS; |
| 1999 | } |
| 2000 | if (RVAT) { |
| 2001 | // FIXME: This isn't correct! But tricky to implement because |
| 2002 | // the array's size has to be the size of RHS, but the type |
| 2003 | // has to be different. |
| 2004 | return RHS; |
| 2005 | } |
Eli Friedman | c91a3f3 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 2006 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 2007 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2008 | return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(), 0); |
| 2009 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2010 | case Type::FunctionNoProto: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2011 | return mergeFunctionTypes(LHS, RHS); |
| 2012 | case Type::Tagged: |
| 2013 | { |
| 2014 | // FIXME: Why are these compatible? |
| 2015 | if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS; |
| 2016 | if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS; |
| 2017 | return QualType(); |
| 2018 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2019 | case Type::Builtin: |
Chris Lattner | d1240fa | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 2020 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2021 | return QualType(); |
Chris Lattner | d1240fa | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 2022 | case Type::Vector: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2023 | if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) |
| 2024 | return LHS; |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2025 | case Type::ObjCInterface: |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2026 | { |
| 2027 | // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces |
| 2028 | // for checking assignment/comparison safety |
| 2029 | return QualType(); |
| 2030 | } |
Chris Lattner | c38d452 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2031 | default: |
| 2032 | assert(0 && "unexpected type"); |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2033 | return QualType(); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2034 | } |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2035 | } |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2036 | |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 2037 | //===----------------------------------------------------------------------===// |
Eli Friedman | 0832dbc | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 2038 | // Integer Predicates |
| 2039 | //===----------------------------------------------------------------------===// |
| 2040 | unsigned ASTContext::getIntWidth(QualType T) { |
| 2041 | if (T == BoolTy) |
| 2042 | return 1; |
| 2043 | // At the moment, only bool has padding bits |
| 2044 | return (unsigned)getTypeSize(T); |
| 2045 | } |
| 2046 | |
| 2047 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 2048 | assert(T->isSignedIntegerType() && "Unexpected type"); |
| 2049 | if (const EnumType* ETy = T->getAsEnumType()) |
| 2050 | T = ETy->getDecl()->getIntegerType(); |
| 2051 | const BuiltinType* BTy = T->getAsBuiltinType(); |
| 2052 | assert (BTy && "Unexpected signed integer type"); |
| 2053 | switch (BTy->getKind()) { |
| 2054 | case BuiltinType::Char_S: |
| 2055 | case BuiltinType::SChar: |
| 2056 | return UnsignedCharTy; |
| 2057 | case BuiltinType::Short: |
| 2058 | return UnsignedShortTy; |
| 2059 | case BuiltinType::Int: |
| 2060 | return UnsignedIntTy; |
| 2061 | case BuiltinType::Long: |
| 2062 | return UnsignedLongTy; |
| 2063 | case BuiltinType::LongLong: |
| 2064 | return UnsignedLongLongTy; |
| 2065 | default: |
| 2066 | assert(0 && "Unexpected signed integer type"); |
| 2067 | return QualType(); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | |
| 2072 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1d78a86 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 2073 | // Serialization Support |
| 2074 | //===----------------------------------------------------------------------===// |
| 2075 | |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2076 | /// Emit - Serialize an ASTContext object to Bitcode. |
| 2077 | void ASTContext::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2078 | S.Emit(LangOpts); |
Ted Kremenek | 9af4d5c | 2007-10-31 20:00:03 +0000 | [diff] [blame] | 2079 | S.EmitRef(SourceMgr); |
| 2080 | S.EmitRef(Target); |
| 2081 | S.EmitRef(Idents); |
| 2082 | S.EmitRef(Selectors); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2083 | |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2084 | // Emit the size of the type vector so that we can reserve that size |
| 2085 | // when we reconstitute the ASTContext object. |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 2086 | S.EmitInt(Types.size()); |
| 2087 | |
Ted Kremenek | 034a78c | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 2088 | for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); |
| 2089 | I!=E;++I) |
| 2090 | (*I)->Emit(S); |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 2091 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2092 | S.EmitOwnedPtr(TUDecl); |
| 2093 | |
Ted Kremenek | e1fed7a | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 2094 | // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Ted Kremenek | acba361 | 2007-11-13 00:25:37 +0000 | [diff] [blame] | 2097 | ASTContext* ASTContext::Create(llvm::Deserializer& D) { |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2098 | |
| 2099 | // Read the language options. |
| 2100 | LangOptions LOpts; |
| 2101 | LOpts.Read(D); |
| 2102 | |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2103 | SourceManager &SM = D.ReadRef<SourceManager>(); |
| 2104 | TargetInfo &t = D.ReadRef<TargetInfo>(); |
| 2105 | IdentifierTable &idents = D.ReadRef<IdentifierTable>(); |
| 2106 | SelectorTable &sels = D.ReadRef<SelectorTable>(); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2107 | |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2108 | unsigned size_reserve = D.ReadInt(); |
| 2109 | |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2110 | ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve); |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2111 | |
Ted Kremenek | 034a78c | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 2112 | for (unsigned i = 0; i < size_reserve; ++i) |
| 2113 | Type::Create(*A,i,D); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2114 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2115 | A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A)); |
| 2116 | |
Ted Kremenek | e1fed7a | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 2117 | // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2118 | |
| 2119 | return A; |
| 2120 | } |