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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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; |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 51 | unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 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 | } |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 83 | } else if (isa<ObjcInterfaceType>(T)) |
| 84 | ++NumObjcInterfaces; |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 85 | else if (isa<ObjcQualifiedInterfaceType>(T)) |
| 86 | ++NumObjcQualifiedInterfaces; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 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); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +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", |
| 111 | NumObjcQualifiedInterfaces); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 112 | fprintf(stderr, " %d protocol qualified id types\n", |
| 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(); |
| 165 | ObjcIdType = QualType(); |
| 166 | IdStructType = 0; |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 167 | ObjcClassType = QualType(); |
| 168 | ClassStructType = 0; |
| 169 | |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +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 | } |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 259 | case Type::ObjcQualifiedId: |
| 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 | |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 694 | /// getObjcInterfaceType - Return the unique reference to the type for the |
| 695 | /// specified ObjC interface decl. |
| 696 | QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) { |
| 697 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 698 | |
Fariborz Jahanian | 0c2f214 | 2007-12-13 20:47:42 +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 | |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 704 | /// getObjcQualifiedInterfaceType - Return a |
| 705 | /// ObjcQualifiedInterfaceType type for the given interface decl and |
| 706 | /// the conforming protocol list. |
| 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; |
Fariborz Jahanian | 0c2f214 | 2007-12-13 20:47:42 +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; |
| 713 | if (ObjcQualifiedInterfaceType *QT = |
| 714 | ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 715 | return QualType(QT, 0); |
| 716 | |
| 717 | // No Match; |
Chris Lattner | d855a6e | 2007-10-11 03:36:41 +0000 | [diff] [blame] | 718 | ObjcQualifiedInterfaceType *QType = |
Fariborz Jahanian | 0c2f214 | 2007-12-13 20:47:42 +0000 | [diff] [blame] | 719 | new ObjcQualifiedInterfaceType(Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 720 | Types.push_back(QType); |
| 721 | ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
| 722 | return QualType(QType, 0); |
| 723 | } |
| 724 | |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 725 | /// getObjcQualifiedIdType - Return a |
Fariborz Jahanian | 2a0e543 | 2007-12-17 21:48:49 +0000 | [diff] [blame] | 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. |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 728 | QualType ASTContext::getObjcQualifiedIdType(QualType idType, |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 729 | ObjcProtocolDecl **Protocols, |
| 730 | unsigned NumProtocols) { |
| 731 | llvm::FoldingSetNodeID ID; |
| 732 | ObjcQualifiedIdType::Profile(ID, Protocols, NumProtocols); |
| 733 | |
| 734 | void *InsertPos = 0; |
| 735 | if (ObjcQualifiedIdType *QT = |
| 736 | ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 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()) { |
| 742 | Canonical = getObjcQualifiedIdType(idType.getCanonicalType(), |
| 743 | Protocols, NumProtocols); |
| 744 | ObjcQualifiedIdType *NewQT = |
| 745 | ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 746 | assert(NewQT == 0 && "Shouldn't be in the map!"); |
| 747 | } |
| 748 | |
| 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); |
| 752 | ObjcQualifiedIdTypes.InsertNode(QType, InsertPos); |
| 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 | |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 964 | /// getObjcEncodingTypeSize returns size of type for objective-c encoding |
| 965 | /// purpose. |
| 966 | int ASTContext::getObjcEncodingTypeSize(QualType type) { |
| 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 | |
| 979 | /// getObjcEncodingForMethodDecl - Return the encoded type for this method |
| 980 | /// declaration. |
| 981 | void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, |
| 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. |
| 985 | getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 986 | // Encode result type. |
| 987 | getObjcEncodingForType(Decl->getResultType(), S); |
| 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(); |
| 999 | int sz = getObjcEncodingTypeSize (PType); |
| 1000 | assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type"); |
| 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. |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1013 | getObjcEncodingForTypeQualifier( |
| 1014 | Decl->getParamDecl(i)->getObjcDeclQualifier(), S); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1015 | getObjcEncodingForType(PType, S); |
| 1016 | S += llvm::utostr(ParmOffset); |
| 1017 | ParmOffset += getObjcEncodingTypeSize(PType); |
| 1018 | } |
| 1019 | } |
| 1020 | |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1021 | void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const |
| 1022 | { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1023 | // FIXME: This currently doesn't encode: |
| 1024 | // @ An object (whether statically typed or typed id) |
| 1025 | // # A class object (Class) |
| 1026 | // : A method selector (SEL) |
| 1027 | // {name=type...} A structure |
| 1028 | // (name=type...) A union |
| 1029 | // bnum A bit field of num bits |
| 1030 | |
| 1031 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1032 | char encoding; |
| 1033 | switch (BT->getKind()) { |
| 1034 | case BuiltinType::Void: |
| 1035 | encoding = 'v'; |
| 1036 | break; |
| 1037 | case BuiltinType::Bool: |
| 1038 | encoding = 'B'; |
| 1039 | break; |
| 1040 | case BuiltinType::Char_U: |
| 1041 | case BuiltinType::UChar: |
| 1042 | encoding = 'C'; |
| 1043 | break; |
| 1044 | case BuiltinType::UShort: |
| 1045 | encoding = 'S'; |
| 1046 | break; |
| 1047 | case BuiltinType::UInt: |
| 1048 | encoding = 'I'; |
| 1049 | break; |
| 1050 | case BuiltinType::ULong: |
| 1051 | encoding = 'L'; |
| 1052 | break; |
| 1053 | case BuiltinType::ULongLong: |
| 1054 | encoding = 'Q'; |
| 1055 | break; |
| 1056 | case BuiltinType::Char_S: |
| 1057 | case BuiltinType::SChar: |
| 1058 | encoding = 'c'; |
| 1059 | break; |
| 1060 | case BuiltinType::Short: |
| 1061 | encoding = 's'; |
| 1062 | break; |
| 1063 | case BuiltinType::Int: |
| 1064 | encoding = 'i'; |
| 1065 | break; |
| 1066 | case BuiltinType::Long: |
| 1067 | encoding = 'l'; |
| 1068 | break; |
| 1069 | case BuiltinType::LongLong: |
| 1070 | encoding = 'q'; |
| 1071 | break; |
| 1072 | case BuiltinType::Float: |
| 1073 | encoding = 'f'; |
| 1074 | break; |
| 1075 | case BuiltinType::Double: |
| 1076 | encoding = 'd'; |
| 1077 | break; |
| 1078 | case BuiltinType::LongDouble: |
| 1079 | encoding = 'd'; |
| 1080 | break; |
| 1081 | default: |
| 1082 | assert(0 && "Unhandled builtin type kind"); |
| 1083 | } |
| 1084 | |
| 1085 | S += encoding; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1086 | } |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 1087 | else if (T->isObjcQualifiedIdType()) { |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1088 | // Treat id<P...> same as 'id' for encoding purposes. |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 1089 | return getObjcEncodingForType(getObjcIdType(), S); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1090 | |
| 1091 | } |
| 1092 | else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1093 | QualType PointeeTy = PT->getPointeeType(); |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1094 | if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) { |
Fariborz Jahanian | 80faffa | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1095 | S += '@'; |
| 1096 | return; |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1097 | } else if (isObjcClassType(PointeeTy)) { |
| 1098 | S += '#'; |
| 1099 | return; |
| 1100 | } else if (isObjcSelType(PointeeTy)) { |
| 1101 | S += ':'; |
| 1102 | return; |
Fariborz Jahanian | 80faffa | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1103 | } |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1104 | |
| 1105 | if (PointeeTy->isCharType()) { |
| 1106 | // char pointer types should be encoded as '*' unless it is a |
| 1107 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1108 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1109 | S += '*'; |
| 1110 | return; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | S += '^'; |
| 1115 | getObjcEncodingForType(PT->getPointeeType(), S); |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1116 | } else if (const ArrayType *AT = T->getAsArrayType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1117 | S += '['; |
| 1118 | |
| 1119 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1120 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 1121 | else |
| 1122 | assert(0 && "Unhandled array type!"); |
| 1123 | |
| 1124 | getObjcEncodingForType(AT->getElementType(), S); |
| 1125 | S += ']'; |
Anders Carlsson | 5695bb7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 1126 | } else if (T->getAsFunctionType()) { |
| 1127 | S += '?'; |
Fariborz Jahanian | c8ba2bd | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1128 | } else if (const RecordType *RTy = T->getAsRecordType()) { |
| 1129 | RecordDecl *RDecl= RTy->getDecl(); |
| 1130 | S += '{'; |
| 1131 | S += RDecl->getName(); |
| 1132 | S += '='; |
| 1133 | for (int i = 0; i < RDecl->getNumMembers(); i++) { |
| 1134 | FieldDecl *field = RDecl->getMember(i); |
| 1135 | getObjcEncodingForType(field->getType(), S); |
| 1136 | } |
| 1137 | S += '}'; |
Steve Naroff | 49af3f3 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 1138 | } else if (T->isEnumeralType()) { |
| 1139 | S += 'i'; |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1140 | } else |
Steve Naroff | 49af3f3 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 1141 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1144 | void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT, |
| 1145 | std::string& S) const { |
| 1146 | if (QT & Decl::OBJC_TQ_In) |
| 1147 | S += 'n'; |
| 1148 | if (QT & Decl::OBJC_TQ_Inout) |
| 1149 | S += 'N'; |
| 1150 | if (QT & Decl::OBJC_TQ_Out) |
| 1151 | S += 'o'; |
| 1152 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 1153 | S += 'O'; |
| 1154 | if (QT & Decl::OBJC_TQ_Byref) |
| 1155 | S += 'R'; |
| 1156 | if (QT & Decl::OBJC_TQ_Oneway) |
| 1157 | S += 'V'; |
| 1158 | } |
| 1159 | |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1160 | void ASTContext::setBuiltinVaListType(QualType T) |
| 1161 | { |
| 1162 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 1163 | |
| 1164 | BuiltinVaListType = T; |
| 1165 | } |
| 1166 | |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1167 | void ASTContext::setObjcIdType(TypedefDecl *TD) |
| 1168 | { |
| 1169 | assert(ObjcIdType.isNull() && "'id' type already set!"); |
| 1170 | |
| 1171 | ObjcIdType = getTypedefType(TD); |
| 1172 | |
| 1173 | // typedef struct objc_object *id; |
| 1174 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1175 | assert(ptr && "'id' incorrectly typed"); |
| 1176 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1177 | assert(rec && "'id' incorrectly typed"); |
| 1178 | IdStructType = rec; |
| 1179 | } |
| 1180 | |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1181 | void ASTContext::setObjcSelType(TypedefDecl *TD) |
| 1182 | { |
| 1183 | assert(ObjcSelType.isNull() && "'SEL' type already set!"); |
| 1184 | |
| 1185 | ObjcSelType = getTypedefType(TD); |
| 1186 | |
| 1187 | // typedef struct objc_selector *SEL; |
| 1188 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1189 | assert(ptr && "'SEL' incorrectly typed"); |
| 1190 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1191 | assert(rec && "'SEL' incorrectly typed"); |
| 1192 | SelStructType = rec; |
| 1193 | } |
| 1194 | |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 1195 | void ASTContext::setObjcProtoType(QualType QT) |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1196 | { |
| 1197 | assert(ObjcProtoType.isNull() && "'Protocol' type already set!"); |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 1198 | ObjcProtoType = QT; |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1201 | void ASTContext::setObjcClassType(TypedefDecl *TD) |
| 1202 | { |
| 1203 | assert(ObjcClassType.isNull() && "'Class' type already set!"); |
| 1204 | |
| 1205 | ObjcClassType = getTypedefType(TD); |
| 1206 | |
| 1207 | // typedef struct objc_class *Class; |
| 1208 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1209 | assert(ptr && "'Class' incorrectly typed"); |
| 1210 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1211 | assert(rec && "'Class' incorrectly typed"); |
| 1212 | ClassStructType = rec; |
| 1213 | } |
| 1214 | |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1215 | void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) { |
| 1216 | assert(ObjcConstantStringType.isNull() && |
| 1217 | "'NSConstantString' type already set!"); |
| 1218 | |
| 1219 | ObjcConstantStringType = getObjcInterfaceType(Decl); |
| 1220 | } |
| 1221 | |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1222 | bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1223 | const BuiltinType *lBuiltin = lhs->getAsBuiltinType(); |
| 1224 | const BuiltinType *rBuiltin = rhs->getAsBuiltinType(); |
| 1225 | |
| 1226 | return lBuiltin->getKind() == rBuiltin->getKind(); |
| 1227 | } |
| 1228 | |
Fariborz Jahanian | 274dbf0 | 2007-12-21 17:34:43 +0000 | [diff] [blame] | 1229 | /// objcTypesAreCompatible - This routine is called when two types |
| 1230 | /// are of different class; one is interface type or is |
| 1231 | /// a qualified interface type and the other type is of a different class. |
| 1232 | /// Example, II or II<P>. |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1233 | bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1234 | if (lhs->isObjcInterfaceType() && isObjcIdType(rhs)) |
| 1235 | return true; |
| 1236 | else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType()) |
| 1237 | return true; |
Fariborz Jahanian | 274dbf0 | 2007-12-21 17:34:43 +0000 | [diff] [blame] | 1238 | if (ObjcInterfaceType *lhsIT = |
| 1239 | dyn_cast<ObjcInterfaceType>(lhs.getCanonicalType().getTypePtr())) { |
| 1240 | ObjcQualifiedInterfaceType *rhsQI = |
| 1241 | dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr()); |
| 1242 | return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl()); |
| 1243 | } |
| 1244 | else if (ObjcInterfaceType *rhsIT = |
| 1245 | dyn_cast<ObjcInterfaceType>(rhs.getCanonicalType().getTypePtr())) { |
| 1246 | ObjcQualifiedInterfaceType *lhsQI = |
| 1247 | dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr()); |
| 1248 | return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl()); |
| 1249 | } |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1250 | return false; |
| 1251 | } |
| 1252 | |
| 1253 | bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) { |
Fariborz Jahanian | ce2de81 | 2007-12-20 22:37:58 +0000 | [diff] [blame] | 1254 | if (lhs == rhs) |
| 1255 | return true; |
| 1256 | ObjcInterfaceType *lhsIT = cast<ObjcInterfaceType>(lhs.getTypePtr()); |
| 1257 | ObjcInterfaceType *rhsIT = cast<ObjcInterfaceType>(rhs.getTypePtr()); |
| 1258 | ObjcInterfaceDecl *rhsIDecl = rhsIT->getDecl(); |
| 1259 | ObjcInterfaceDecl *lhsIDecl = lhsIT->getDecl(); |
| 1260 | // rhs is derived from lhs it is OK; else it is not OK. |
| 1261 | while (rhsIDecl != NULL) { |
| 1262 | if (rhsIDecl == lhsIDecl) |
| 1263 | return true; |
| 1264 | rhsIDecl = rhsIDecl->getSuperClass(); |
| 1265 | } |
| 1266 | return false; |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Fariborz Jahanian | 12519d4 | 2007-12-12 01:00:23 +0000 | [diff] [blame] | 1269 | bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs, |
| 1270 | QualType rhs) { |
| 1271 | ObjcQualifiedInterfaceType *lhsQI = |
| 1272 | dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr()); |
| 1273 | assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type"); |
| 1274 | ObjcQualifiedInterfaceType *rhsQI = |
| 1275 | dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr()); |
| 1276 | assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type"); |
Fariborz Jahanian | 0c2f214 | 2007-12-13 20:47:42 +0000 | [diff] [blame] | 1277 | if (!interfaceTypesAreCompatible(getObjcInterfaceType(lhsQI->getDecl()), |
| 1278 | getObjcInterfaceType(rhsQI->getDecl()))) |
Fariborz Jahanian | 12519d4 | 2007-12-12 01:00:23 +0000 | [diff] [blame] | 1279 | return false; |
| 1280 | /* All protocols in lhs must have a presense in rhs. */ |
| 1281 | for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) { |
| 1282 | bool match = false; |
| 1283 | ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i); |
| 1284 | for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) { |
| 1285 | ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j); |
| 1286 | if (lhsProto == rhsProto) { |
| 1287 | match = true; |
| 1288 | break; |
| 1289 | } |
| 1290 | } |
| 1291 | if (!match) |
| 1292 | return false; |
| 1293 | } |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
Fariborz Jahanian | cd71bf4 | 2007-12-21 00:33:59 +0000 | [diff] [blame] | 1297 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 1298 | /// inheritance hierarchy of 'rProto'. |
| 1299 | static bool ProtocolCompatibleWithProtocol(ObjcProtocolDecl *lProto, |
| 1300 | ObjcProtocolDecl *rProto) { |
| 1301 | if (lProto == rProto) |
| 1302 | return true; |
| 1303 | ObjcProtocolDecl** RefPDecl = rProto->getReferencedProtocols(); |
| 1304 | for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++) |
| 1305 | if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i])) |
| 1306 | return true; |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
Fariborz Jahanian | f4e6804 | 2007-12-21 22:22:33 +0000 | [diff] [blame] | 1310 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 1311 | /// has been implemented in IDecl class, its super class or categories (if |
| 1312 | /// lookupCategory is true). |
| 1313 | static bool ClassImplementsProtocol(ObjcProtocolDecl *lProto, |
| 1314 | ObjcInterfaceDecl *IDecl, |
| 1315 | bool lookupCategory) { |
| 1316 | |
| 1317 | // 1st, look up the class. |
| 1318 | ObjcProtocolDecl **protoList = IDecl->getReferencedProtocols(); |
| 1319 | for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) { |
| 1320 | if (ProtocolCompatibleWithProtocol(lProto, protoList[i])) |
| 1321 | return true; |
| 1322 | } |
| 1323 | |
| 1324 | // 2nd, look up the category. |
| 1325 | if (lookupCategory) |
| 1326 | for (ObjcCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 1327 | CDecl = CDecl->getNextClassCategory()) { |
| 1328 | protoList = CDecl->getReferencedProtocols(); |
| 1329 | for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) { |
| 1330 | if (ProtocolCompatibleWithProtocol(lProto, protoList[i])) |
| 1331 | return true; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | // 3rd, look up the super class(s) |
| 1336 | if (IDecl->getSuperClass()) |
| 1337 | return |
| 1338 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory); |
| 1339 | |
| 1340 | return false; |
| 1341 | } |
| 1342 | |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1343 | /// ObjcQualifiedIdTypesAreCompatible - Compares two types, at least |
Fariborz Jahanian | cd71bf4 | 2007-12-21 00:33:59 +0000 | [diff] [blame] | 1344 | /// one of which is a protocol qualified 'id' type. When 'compare' |
| 1345 | /// is true it is for comparison; when false, for assignment/initialization. |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1346 | bool ASTContext::ObjcQualifiedIdTypesAreCompatible(QualType lhs, |
Fariborz Jahanian | cd71bf4 | 2007-12-21 00:33:59 +0000 | [diff] [blame] | 1347 | QualType rhs, |
| 1348 | bool compare) { |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1349 | // match id<P..> with an 'id' type in all cases. |
| 1350 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 1351 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1352 | if (isObjcIdType(PointeeTy) || PointeeTy->isVoidType()) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1353 | return true; |
| 1354 | |
| 1355 | } |
| 1356 | else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 1357 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1358 | if (isObjcIdType(PointeeTy) || PointeeTy->isVoidType()) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1359 | return true; |
| 1360 | |
| 1361 | } |
| 1362 | |
| 1363 | ObjcQualifiedInterfaceType *lhsQI = 0; |
| 1364 | ObjcQualifiedInterfaceType *rhsQI = 0; |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1365 | ObjcInterfaceDecl *lhsID = 0; |
| 1366 | ObjcInterfaceDecl *rhsID = 0; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1367 | ObjcQualifiedIdType *lhsQID = dyn_cast<ObjcQualifiedIdType>(lhs); |
| 1368 | ObjcQualifiedIdType *rhsQID = dyn_cast<ObjcQualifiedIdType>(rhs); |
| 1369 | |
| 1370 | if (lhsQID) { |
| 1371 | if (!rhsQID && rhs->getTypeClass() == Type::Pointer) { |
| 1372 | QualType rtype = |
| 1373 | cast<PointerType>(rhs.getCanonicalType())->getPointeeType(); |
| 1374 | rhsQI = |
| 1375 | dyn_cast<ObjcQualifiedInterfaceType>( |
| 1376 | rtype.getCanonicalType().getTypePtr()); |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1377 | if (!rhsQI) { |
| 1378 | ObjcInterfaceType *IT = dyn_cast<ObjcInterfaceType>( |
| 1379 | rtype.getCanonicalType().getTypePtr()); |
| 1380 | if (IT) |
| 1381 | rhsID = IT->getDecl(); |
| 1382 | } |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1383 | } |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1384 | if (!rhsQI && !rhsQID && !rhsID) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1385 | return false; |
| 1386 | |
Fariborz Jahanian | f4e6804 | 2007-12-21 22:22:33 +0000 | [diff] [blame] | 1387 | unsigned numRhsProtocols; |
| 1388 | ObjcProtocolDecl **rhsProtoList; |
| 1389 | if (rhsQI) { |
| 1390 | numRhsProtocols = rhsQI->getNumProtocols(); |
| 1391 | rhsProtoList = rhsQI->getReferencedProtocols(); |
| 1392 | } |
| 1393 | else if (rhsQID) { |
| 1394 | numRhsProtocols = rhsQID->getNumProtocols(); |
| 1395 | rhsProtoList = rhsQID->getReferencedProtocols(); |
| 1396 | } |
| 1397 | |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1398 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1399 | ObjcProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
Fariborz Jahanian | f4e6804 | 2007-12-21 22:22:33 +0000 | [diff] [blame] | 1400 | bool match = false; |
| 1401 | |
| 1402 | // when comparing an id<P> on lhs with a static type on rhs, |
| 1403 | // see if static class implements all of id's protocols, directly or |
| 1404 | // through its super class and categories. |
| 1405 | if (rhsID) { |
| 1406 | if (ClassImplementsProtocol(lhsProto, rhsID, true)) |
| 1407 | match = true; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1408 | } |
Fariborz Jahanian | f4e6804 | 2007-12-21 22:22:33 +0000 | [diff] [blame] | 1409 | else for (unsigned j = 0; j < numRhsProtocols; j++) { |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1410 | ObjcProtocolDecl *rhsProto = rhsProtoList[j]; |
Fariborz Jahanian | cd71bf4 | 2007-12-21 00:33:59 +0000 | [diff] [blame] | 1411 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 1412 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1413 | match = true; |
| 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | if (!match) |
| 1418 | return false; |
| 1419 | } |
| 1420 | } |
| 1421 | else if (rhsQID) { |
| 1422 | if (!lhsQID && lhs->getTypeClass() == Type::Pointer) { |
| 1423 | QualType ltype = |
| 1424 | cast<PointerType>(lhs.getCanonicalType())->getPointeeType(); |
| 1425 | lhsQI = |
| 1426 | dyn_cast<ObjcQualifiedInterfaceType>( |
| 1427 | ltype.getCanonicalType().getTypePtr()); |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1428 | if (!lhsQI) { |
| 1429 | ObjcInterfaceType *IT = dyn_cast<ObjcInterfaceType>( |
| 1430 | ltype.getCanonicalType().getTypePtr()); |
| 1431 | if (IT) |
| 1432 | lhsID = IT->getDecl(); |
| 1433 | } |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1434 | } |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1435 | if (!lhsQI && !lhsQID && !lhsID) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1436 | return false; |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1437 | |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1438 | unsigned numLhsProtocols; |
| 1439 | ObjcProtocolDecl **lhsProtoList; |
| 1440 | if (lhsQI) { |
| 1441 | numLhsProtocols = lhsQI->getNumProtocols(); |
| 1442 | lhsProtoList = lhsQI->getReferencedProtocols(); |
| 1443 | } |
Fariborz Jahanian | 8782907 | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 1444 | else if (lhsQID) { |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1445 | numLhsProtocols = lhsQID->getNumProtocols(); |
| 1446 | lhsProtoList = lhsQID->getReferencedProtocols(); |
Fariborz Jahanian | f4e6804 | 2007-12-21 22:22:33 +0000 | [diff] [blame] | 1447 | } |
| 1448 | bool match = false; |
| 1449 | // for static type vs. qualified 'id' type, check that class implements |
| 1450 | // one of 'id's protocols. |
| 1451 | if (lhsID) { |
| 1452 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 1453 | ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 1454 | if (ClassImplementsProtocol(rhsProto, lhsID, compare)) { |
| 1455 | match = true; |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | else for (unsigned i =0; i < numLhsProtocols; i++) { |
| 1461 | match = false; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1462 | ObjcProtocolDecl *lhsProto = lhsProtoList[i]; |
| 1463 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 1464 | ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
Fariborz Jahanian | cd71bf4 | 2007-12-21 00:33:59 +0000 | [diff] [blame] | 1465 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 1466 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1467 | match = true; |
| 1468 | break; |
| 1469 | } |
| 1470 | } |
Fariborz Jahanian | f4e6804 | 2007-12-21 22:22:33 +0000 | [diff] [blame] | 1471 | } |
| 1472 | if (!match) |
| 1473 | return false; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1474 | } |
| 1475 | return true; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1476 | } |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | 5003e8b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1478 | bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1479 | const VectorType *lVector = lhs->getAsVectorType(); |
| 1480 | const VectorType *rVector = rhs->getAsVectorType(); |
| 1481 | |
| 1482 | if ((lVector->getElementType().getCanonicalType() == |
| 1483 | rVector->getElementType().getCanonicalType()) && |
| 1484 | (lVector->getNumElements() == rVector->getNumElements())) |
| 1485 | return true; |
| 1486 | return false; |
| 1487 | } |
| 1488 | |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1489 | // C99 6.2.7p1: If both are complete types, then the following additional |
| 1490 | // requirements apply...FIXME (handle compatibility across source files). |
| 1491 | bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1492 | TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl(); |
| 1493 | TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl(); |
| 1494 | |
| 1495 | if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) { |
| 1496 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1497 | return true; |
| 1498 | } |
| 1499 | if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) { |
| 1500 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1501 | return true; |
| 1502 | } |
Steve Naroff | 4a5e207 | 2007-11-07 06:03:51 +0000 | [diff] [blame] | 1503 | // "Class" and "id" are compatible built-in structure types. |
| 1504 | if (isObjcIdType(lhs) && isObjcClassType(rhs) || |
| 1505 | isObjcClassType(lhs) && isObjcIdType(rhs)) |
| 1506 | return true; |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1507 | return false; |
| 1508 | } |
| 1509 | |
| 1510 | bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1511 | // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be |
| 1512 | // identically qualified and both shall be pointers to compatible types. |
| 1513 | if (lhs.getQualifiers() != rhs.getQualifiers()) |
| 1514 | return false; |
| 1515 | |
| 1516 | QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType(); |
| 1517 | QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType(); |
| 1518 | |
| 1519 | return typesAreCompatible(ltype, rtype); |
| 1520 | } |
| 1521 | |
Bill Wendling | 6a9d854 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 1522 | // 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] | 1523 | // reference to T, the operation assigns to the object of type T denoted by the |
| 1524 | // reference. |
| 1525 | bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1526 | QualType ltype = lhs; |
| 1527 | |
| 1528 | if (lhs->isReferenceType()) |
| 1529 | ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType(); |
| 1530 | |
| 1531 | QualType rtype = rhs; |
| 1532 | |
| 1533 | if (rhs->isReferenceType()) |
| 1534 | rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType(); |
| 1535 | |
| 1536 | return typesAreCompatible(ltype, rtype); |
| 1537 | } |
| 1538 | |
| 1539 | bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1540 | const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType()); |
| 1541 | const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType()); |
| 1542 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 1543 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 1544 | |
| 1545 | // first check the return types (common between C99 and K&R). |
| 1546 | if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType())) |
| 1547 | return false; |
| 1548 | |
| 1549 | if (lproto && rproto) { // two C99 style function prototypes |
| 1550 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 1551 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 1552 | |
| 1553 | if (lproto_nargs != rproto_nargs) |
| 1554 | return false; |
| 1555 | |
| 1556 | // both prototypes have the same number of arguments. |
| 1557 | if ((lproto->isVariadic() && !rproto->isVariadic()) || |
| 1558 | (rproto->isVariadic() && !lproto->isVariadic())) |
| 1559 | return false; |
| 1560 | |
| 1561 | // The use of ellipsis agree...now check the argument types. |
| 1562 | for (unsigned i = 0; i < lproto_nargs; i++) |
| 1563 | if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i))) |
| 1564 | return false; |
| 1565 | return true; |
| 1566 | } |
| 1567 | if (!lproto && !rproto) // two K&R style function decls, nothing to do. |
| 1568 | return true; |
| 1569 | |
| 1570 | // we have a mixture of K&R style with C99 prototypes |
| 1571 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 1572 | |
| 1573 | if (proto->isVariadic()) |
| 1574 | return false; |
| 1575 | |
| 1576 | // FIXME: Each parameter type T in the prototype must be compatible with the |
| 1577 | // type resulting from applying the usual argument conversions to T. |
| 1578 | return true; |
| 1579 | } |
| 1580 | |
| 1581 | bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1582 | QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType(); |
| 1583 | QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType(); |
| 1584 | |
| 1585 | if (!typesAreCompatible(ltype, rtype)) |
| 1586 | return false; |
| 1587 | |
| 1588 | // FIXME: If both types specify constant sizes, then the sizes must also be |
| 1589 | // the same. Even if the sizes are the same, GCC produces an error. |
| 1590 | return true; |
| 1591 | } |
| 1592 | |
| 1593 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 1594 | /// both shall have the identically qualified version of a compatible type. |
| 1595 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 1596 | /// same. See 6.7.[2,3,5] for additional rules. |
| 1597 | bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) { |
| 1598 | QualType lcanon = lhs.getCanonicalType(); |
| 1599 | QualType rcanon = rhs.getCanonicalType(); |
| 1600 | |
| 1601 | // If two types are identical, they are are compatible |
| 1602 | if (lcanon == rcanon) |
| 1603 | return true; |
Bill Wendling | 6a9d854 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 1604 | |
| 1605 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 1606 | // type is adjusted to "T" prior to any further analysis, the expression |
| 1607 | // designates the object or function denoted by the reference, and the |
| 1608 | // expression is an lvalue. |
| 1609 | if (lcanon->getTypeClass() == Type::Reference) |
| 1610 | lcanon = cast<ReferenceType>(lcanon)->getReferenceeType(); |
| 1611 | if (rcanon->getTypeClass() == Type::Reference) |
| 1612 | rcanon = cast<ReferenceType>(rcanon)->getReferenceeType(); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1613 | |
| 1614 | // If the canonical type classes don't match, they can't be compatible |
| 1615 | if (lcanon->getTypeClass() != rcanon->getTypeClass()) { |
| 1616 | // For Objective-C, it is possible for two types to be compatible |
| 1617 | // when their classes don't match (when dealing with "id"). If either type |
| 1618 | // is an interface, we defer to objcTypesAreCompatible(). |
| 1619 | if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType()) |
| 1620 | return objcTypesAreCompatible(lcanon, rcanon); |
| 1621 | return false; |
| 1622 | } |
| 1623 | switch (lcanon->getTypeClass()) { |
| 1624 | case Type::Pointer: |
| 1625 | return pointerTypesAreCompatible(lcanon, rcanon); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1626 | case Type::ConstantArray: |
| 1627 | case Type::VariableArray: |
| 1628 | return arrayTypesAreCompatible(lcanon, rcanon); |
| 1629 | case Type::FunctionNoProto: |
| 1630 | case Type::FunctionProto: |
| 1631 | return functionTypesAreCompatible(lcanon, rcanon); |
| 1632 | case Type::Tagged: // handle structures, unions |
| 1633 | return tagTypesAreCompatible(lcanon, rcanon); |
| 1634 | case Type::Builtin: |
| 1635 | return builtinTypesAreCompatible(lcanon, rcanon); |
| 1636 | case Type::ObjcInterface: |
| 1637 | return interfaceTypesAreCompatible(lcanon, rcanon); |
Chris Lattner | 5003e8b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1638 | case Type::Vector: |
| 1639 | case Type::OCUVector: |
| 1640 | return vectorTypesAreCompatible(lcanon, rcanon); |
Fariborz Jahanian | 12519d4 | 2007-12-12 01:00:23 +0000 | [diff] [blame] | 1641 | case Type::ObjcQualifiedInterface: |
| 1642 | return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1643 | default: |
| 1644 | assert(0 && "unexpected type"); |
| 1645 | } |
| 1646 | return true; // should never get here... |
| 1647 | } |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1648 | |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1649 | /// Emit - Serialize an ASTContext object to Bitcode. |
| 1650 | void ASTContext::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 9af4d5c | 2007-10-31 20:00:03 +0000 | [diff] [blame] | 1651 | S.EmitRef(SourceMgr); |
| 1652 | S.EmitRef(Target); |
| 1653 | S.EmitRef(Idents); |
| 1654 | S.EmitRef(Selectors); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1655 | |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1656 | // Emit the size of the type vector so that we can reserve that size |
| 1657 | // when we reconstitute the ASTContext object. |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1658 | S.EmitInt(Types.size()); |
| 1659 | |
Ted Kremenek | 034a78c | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 1660 | for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); |
| 1661 | I!=E;++I) |
| 1662 | (*I)->Emit(S); |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1663 | |
Ted Kremenek | e1fed7a | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 1664 | // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Ted Kremenek | acba361 | 2007-11-13 00:25:37 +0000 | [diff] [blame] | 1667 | ASTContext* ASTContext::Create(llvm::Deserializer& D) { |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1668 | SourceManager &SM = D.ReadRef<SourceManager>(); |
| 1669 | TargetInfo &t = D.ReadRef<TargetInfo>(); |
| 1670 | IdentifierTable &idents = D.ReadRef<IdentifierTable>(); |
| 1671 | SelectorTable &sels = D.ReadRef<SelectorTable>(); |
| 1672 | |
| 1673 | unsigned size_reserve = D.ReadInt(); |
| 1674 | |
| 1675 | ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve); |
| 1676 | |
Ted Kremenek | 034a78c | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 1677 | for (unsigned i = 0; i < size_reserve; ++i) |
| 1678 | Type::Create(*A,i,D); |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1679 | |
Ted Kremenek | e1fed7a | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 1680 | // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1681 | |
| 1682 | return A; |
| 1683 | } |