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