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