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