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