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