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