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