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