Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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 | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/Serialize.h" |
| 21 | #include "llvm/Bitcode/Deserialize.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 22 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | enum FloatingRank { |
| 26 | FloatRank, DoubleRank, LongDoubleRank |
| 27 | }; |
| 28 | |
| 29 | ASTContext::~ASTContext() { |
| 30 | // Deallocate all the types. |
| 31 | while (!Types.empty()) { |
| 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; |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 47 | unsigned NumVector = 0, NumComplex = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
| 49 | |
| 50 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
Chris Lattner | beb6636 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 51 | unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | |
| 53 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 54 | Type *T = Types[i]; |
| 55 | if (isa<BuiltinType>(T)) |
| 56 | ++NumBuiltin; |
| 57 | else if (isa<PointerType>(T)) |
| 58 | ++NumPointer; |
| 59 | else if (isa<ReferenceType>(T)) |
| 60 | ++NumReference; |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 61 | else if (isa<ComplexType>(T)) |
| 62 | ++NumComplex; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | else if (isa<ArrayType>(T)) |
| 64 | ++NumArray; |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 65 | else if (isa<VectorType>(T)) |
| 66 | ++NumVector; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | else if (isa<FunctionTypeNoProto>(T)) |
| 68 | ++NumFunctionNP; |
| 69 | else if (isa<FunctionTypeProto>(T)) |
| 70 | ++NumFunctionP; |
| 71 | else if (isa<TypedefType>(T)) |
| 72 | ++NumTypeName; |
| 73 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 74 | ++NumTagged; |
| 75 | switch (TT->getDecl()->getKind()) { |
| 76 | default: assert(0 && "Unknown tagged type!"); |
| 77 | case Decl::Struct: ++NumTagStruct; break; |
| 78 | case Decl::Union: ++NumTagUnion; break; |
| 79 | case Decl::Class: ++NumTagClass; break; |
| 80 | case Decl::Enum: ++NumTagEnum; break; |
| 81 | } |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 82 | } else if (isa<ObjcInterfaceType>(T)) |
| 83 | ++NumObjcInterfaces; |
Chris Lattner | beb6636 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 84 | else if (isa<ObjcQualifiedInterfaceType>(T)) |
| 85 | ++NumObjcQualifiedInterfaces; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 86 | else { |
Chris Lattner | beb6636 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 87 | QualType(T, 0).dump(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | assert(0 && "Unknown type!"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 93 | fprintf(stderr, " %d pointer types\n", NumPointer); |
| 94 | fprintf(stderr, " %d reference types\n", NumReference); |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 95 | fprintf(stderr, " %d complex types\n", NumComplex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | fprintf(stderr, " %d array types\n", NumArray); |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 97 | fprintf(stderr, " %d vector types\n", NumVector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 99 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 100 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 101 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 102 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 103 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 104 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 105 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 106 | fprintf(stderr, " %d interface types\n", NumObjcInterfaces); |
Chris Lattner | beb6636 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 107 | fprintf(stderr, " %d protocol qualified interface types\n", |
| 108 | NumObjcQualifiedInterfaces); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 110 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 111 | NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 113 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
| 114 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType))); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 119 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
| 120 | } |
| 121 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | void ASTContext::InitBuiltinTypes() { |
| 123 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 124 | |
| 125 | // C99 6.2.5p19. |
| 126 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 127 | |
| 128 | // C99 6.2.5p2. |
| 129 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 130 | // C99 6.2.5p3. |
| 131 | if (Target.isCharSigned(SourceLocation())) |
| 132 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 133 | else |
| 134 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 135 | // C99 6.2.5p4. |
| 136 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 137 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 138 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 139 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 140 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 141 | |
| 142 | // C99 6.2.5p6. |
| 143 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 144 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 145 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 146 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 147 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 148 | |
| 149 | // C99 6.2.5p10. |
| 150 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 151 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 152 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
| 153 | |
| 154 | // C99 6.2.5p11. |
| 155 | FloatComplexTy = getComplexType(FloatTy); |
| 156 | DoubleComplexTy = getComplexType(DoubleTy); |
| 157 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 158 | |
| 159 | BuiltinVaListType = QualType(); |
| 160 | ObjcIdType = QualType(); |
| 161 | IdStructType = 0; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 162 | ObjcClassType = QualType(); |
| 163 | ClassStructType = 0; |
| 164 | |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 165 | ObjcConstantStringType = QualType(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 166 | |
| 167 | // void * type |
| 168 | VoidPtrTy = getPointerType(VoidTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 171 | //===----------------------------------------------------------------------===// |
| 172 | // Type Sizing and Analysis |
| 173 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 174 | |
| 175 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 176 | /// does not work on incomplete types. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 177 | std::pair<uint64_t, unsigned> |
| 178 | ASTContext::getTypeInfo(QualType T, SourceLocation L) { |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 179 | T = T.getCanonicalType(); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 180 | uint64_t Size; |
| 181 | unsigned Align; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 182 | switch (T->getTypeClass()) { |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 183 | case Type::TypeName: assert(0 && "Not a canonical type!"); |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 184 | case Type::FunctionNoProto: |
| 185 | case Type::FunctionProto: |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 186 | default: |
Chris Lattner | b1c2df9 | 2007-07-20 18:13:33 +0000 | [diff] [blame] | 187 | assert(0 && "Incomplete types have no size!"); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 188 | case Type::VariableArray: |
| 189 | assert(0 && "VLAs not implemented yet!"); |
| 190 | case Type::ConstantArray: { |
| 191 | ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
| 192 | |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 193 | std::pair<uint64_t, unsigned> EltInfo = |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 194 | getTypeInfo(CAT->getElementType(), L); |
| 195 | Size = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 196 | Align = EltInfo.second; |
| 197 | break; |
| 198 | } |
| 199 | case Type::Vector: { |
| 200 | std::pair<uint64_t, unsigned> EltInfo = |
| 201 | getTypeInfo(cast<VectorType>(T)->getElementType(), L); |
| 202 | Size = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
| 203 | // FIXME: Vector alignment is not the alignment of its elements. |
| 204 | Align = EltInfo.second; |
| 205 | break; |
| 206 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 207 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 208 | case Type::Builtin: { |
| 209 | // FIXME: need to use TargetInfo to derive the target specific sizes. This |
| 210 | // implementation will suffice for play with vector support. |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 211 | const llvm::fltSemantics *F; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 212 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 213 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 214 | case BuiltinType::Void: |
| 215 | assert(0 && "Incomplete types have no size!"); |
| 216 | case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 217 | case BuiltinType::Char_S: |
| 218 | case BuiltinType::Char_U: |
| 219 | case BuiltinType::UChar: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 220 | case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 221 | case BuiltinType::UShort: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 222 | case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 223 | case BuiltinType::UInt: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 224 | case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 225 | case BuiltinType::ULong: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 226 | case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 227 | case BuiltinType::ULongLong: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 228 | case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break; |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 229 | case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break; |
| 230 | case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break; |
| 231 | case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 233 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 234 | } |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 235 | case Type::Pointer: Target.getPointerInfo(Size, Align, L); break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 236 | case Type::Reference: |
Chris Lattner | 7ab2ed8 | 2007-07-13 22:16:13 +0000 | [diff] [blame] | 237 | // "When applied to a reference or a reference type, the result is the size |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 238 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
| 239 | // FIXME: This is wrong for struct layout! |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 240 | return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L); |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 241 | |
| 242 | case Type::Complex: { |
| 243 | // Complex types have the same alignment as their elements, but twice the |
| 244 | // size. |
| 245 | std::pair<uint64_t, unsigned> EltInfo = |
| 246 | getTypeInfo(cast<ComplexType>(T)->getElementType(), L); |
| 247 | Size = EltInfo.first*2; |
| 248 | Align = EltInfo.second; |
| 249 | break; |
| 250 | } |
| 251 | case Type::Tagged: |
Chris Lattner | 6cd862c | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 252 | TagType *TT = cast<TagType>(T); |
| 253 | if (RecordType *RT = dyn_cast<RecordType>(TT)) { |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 254 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L); |
Chris Lattner | 6cd862c | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 255 | Size = Layout.getSize(); |
| 256 | Align = Layout.getAlignment(); |
| 257 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) { |
Chris Lattner | e00b18c | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 258 | return getTypeInfo(ED->getIntegerType(), L); |
Chris Lattner | 6cd862c | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 259 | } else { |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 260 | assert(0 && "Unimplemented type sizes!"); |
Chris Lattner | 6cd862c | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 262 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 263 | } |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 265 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 266 | return std::make_pair(Size, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 269 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 270 | /// specified record (struct/union/class), which indicates its size and field |
| 271 | /// position information. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 272 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D, |
| 273 | SourceLocation L) { |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 274 | assert(D->isDefinition() && "Cannot get layout of forward declarations!"); |
| 275 | |
| 276 | // Look up this layout, if already laid out, return what we have. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 277 | const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 278 | if (Entry) return *Entry; |
| 279 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 280 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 281 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
| 282 | ASTRecordLayout *NewEntry = new ASTRecordLayout(); |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 283 | Entry = NewEntry; |
| 284 | |
| 285 | uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()]; |
| 286 | uint64_t RecordSize = 0; |
| 287 | unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits. |
| 288 | |
| 289 | if (D->getKind() != Decl::Union) { |
| 290 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 291 | // the future, this will need to be tweakable by targets. |
| 292 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 293 | const FieldDecl *FD = D->getMember(i); |
| 294 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 295 | uint64_t FieldSize = FieldInfo.first; |
| 296 | unsigned FieldAlign = FieldInfo.second; |
| 297 | |
| 298 | // Round up the current record size to the field's alignment boundary. |
| 299 | RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1); |
| 300 | |
| 301 | // Place this field at the current location. |
| 302 | FieldOffsets[i] = RecordSize; |
| 303 | |
| 304 | // Reserve space for this field. |
| 305 | RecordSize += FieldSize; |
| 306 | |
| 307 | // Remember max struct/class alignment. |
| 308 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 309 | } |
| 310 | |
| 311 | // Finally, round the size of the total struct up to the alignment of the |
| 312 | // struct itself. |
| 313 | RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1); |
| 314 | } else { |
| 315 | // Union layout just puts each member at the start of the record. |
| 316 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 317 | const FieldDecl *FD = D->getMember(i); |
| 318 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 319 | uint64_t FieldSize = FieldInfo.first; |
| 320 | unsigned FieldAlign = FieldInfo.second; |
| 321 | |
| 322 | // Round up the current record size to the field's alignment boundary. |
| 323 | RecordSize = std::max(RecordSize, FieldSize); |
| 324 | |
| 325 | // Place this field at the start of the record. |
| 326 | FieldOffsets[i] = 0; |
| 327 | |
| 328 | // Remember max struct/class alignment. |
| 329 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 330 | } |
| 331 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 332 | |
| 333 | NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets); |
| 334 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 337 | //===----------------------------------------------------------------------===// |
| 338 | // Type creation/memoization methods |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | |
| 341 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 343 | /// number with the specified element type. |
| 344 | QualType ASTContext::getComplexType(QualType T) { |
| 345 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 346 | // structure. |
| 347 | llvm::FoldingSetNodeID ID; |
| 348 | ComplexType::Profile(ID, T); |
| 349 | |
| 350 | void *InsertPos = 0; |
| 351 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 352 | return QualType(CT, 0); |
| 353 | |
| 354 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 355 | // so fill in the canonical type field. |
| 356 | QualType Canonical; |
| 357 | if (!T->isCanonical()) { |
| 358 | Canonical = getComplexType(T.getCanonicalType()); |
| 359 | |
| 360 | // Get the new insert position for the node we care about. |
| 361 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 362 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 363 | } |
| 364 | ComplexType *New = new ComplexType(T, Canonical); |
| 365 | Types.push_back(New); |
| 366 | ComplexTypes.InsertNode(New, InsertPos); |
| 367 | return QualType(New, 0); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 372 | /// the specified type. |
| 373 | QualType ASTContext::getPointerType(QualType T) { |
| 374 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 375 | // structure. |
| 376 | llvm::FoldingSetNodeID ID; |
| 377 | PointerType::Profile(ID, T); |
| 378 | |
| 379 | void *InsertPos = 0; |
| 380 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 381 | return QualType(PT, 0); |
| 382 | |
| 383 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 384 | // so fill in the canonical type field. |
| 385 | QualType Canonical; |
| 386 | if (!T->isCanonical()) { |
| 387 | Canonical = getPointerType(T.getCanonicalType()); |
| 388 | |
| 389 | // Get the new insert position for the node we care about. |
| 390 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 391 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 392 | } |
| 393 | PointerType *New = new PointerType(T, Canonical); |
| 394 | Types.push_back(New); |
| 395 | PointerTypes.InsertNode(New, InsertPos); |
| 396 | return QualType(New, 0); |
| 397 | } |
| 398 | |
| 399 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 400 | /// to the specified type. |
| 401 | QualType ASTContext::getReferenceType(QualType T) { |
| 402 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 403 | // structure. |
| 404 | llvm::FoldingSetNodeID ID; |
| 405 | ReferenceType::Profile(ID, T); |
| 406 | |
| 407 | void *InsertPos = 0; |
| 408 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 409 | return QualType(RT, 0); |
| 410 | |
| 411 | // If the referencee type isn't canonical, this won't be a canonical type |
| 412 | // either, so fill in the canonical type field. |
| 413 | QualType Canonical; |
| 414 | if (!T->isCanonical()) { |
| 415 | Canonical = getReferenceType(T.getCanonicalType()); |
| 416 | |
| 417 | // Get the new insert position for the node we care about. |
| 418 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 419 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 420 | } |
| 421 | |
| 422 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 423 | Types.push_back(New); |
| 424 | ReferenceTypes.InsertNode(New, InsertPos); |
| 425 | return QualType(New, 0); |
| 426 | } |
| 427 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 428 | /// getConstantArrayType - Return the unique reference to the type for an |
| 429 | /// array of the specified element type. |
| 430 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 431 | const llvm::APInt &ArySize, |
| 432 | ArrayType::ArraySizeModifier ASM, |
| 433 | unsigned EltTypeQuals) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 434 | llvm::FoldingSetNodeID ID; |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 435 | ConstantArrayType::Profile(ID, EltTy, ArySize); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 436 | |
| 437 | void *InsertPos = 0; |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 438 | if (ConstantArrayType *ATP = |
| 439 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 440 | return QualType(ATP, 0); |
| 441 | |
| 442 | // If the element type isn't canonical, this won't be a canonical type either, |
| 443 | // so fill in the canonical type field. |
| 444 | QualType Canonical; |
| 445 | if (!EltTy->isCanonical()) { |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 446 | Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize, |
| 447 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 448 | // Get the new insert position for the node we care about. |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 449 | ConstantArrayType *NewIP = |
| 450 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 451 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 453 | } |
| 454 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 455 | ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize, |
| 456 | ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 457 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | Types.push_back(New); |
| 459 | return QualType(New, 0); |
| 460 | } |
| 461 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 462 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 463 | /// variable array of the specified element type. |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 464 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
| 465 | ArrayType::ArraySizeModifier ASM, |
| 466 | unsigned EltTypeQuals) { |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 467 | if (NumElts) { |
| 468 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 469 | // that have an expression provided for their size. |
| 470 | |
Ted Kremenek | 347b9f3 | 2007-10-30 16:41:53 +0000 | [diff] [blame] | 471 | VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 472 | ASM, EltTypeQuals); |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 473 | |
Ted Kremenek | 347b9f3 | 2007-10-30 16:41:53 +0000 | [diff] [blame] | 474 | CompleteVariableArrayTypes.push_back(New); |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 475 | Types.push_back(New); |
| 476 | return QualType(New, 0); |
| 477 | } |
| 478 | else { |
| 479 | // No size is provided for the VLA. These we can unique. |
| 480 | llvm::FoldingSetNodeID ID; |
| 481 | VariableArrayType::Profile(ID, EltTy); |
| 482 | |
| 483 | void *InsertPos = 0; |
| 484 | if (VariableArrayType *ATP = |
| 485 | IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 486 | return QualType(ATP, 0); |
| 487 | |
| 488 | // If the element type isn't canonical, this won't be a canonical type |
| 489 | // either, so fill in the canonical type field. |
| 490 | QualType Canonical; |
| 491 | |
| 492 | if (!EltTy->isCanonical()) { |
| 493 | Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts, |
| 494 | ASM, EltTypeQuals); |
| 495 | |
| 496 | // Get the new insert position for the node we care about. |
| 497 | VariableArrayType *NewIP = |
| 498 | IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 499 | |
| 500 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 501 | } |
| 502 | |
| 503 | VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 504 | ASM, EltTypeQuals); |
| 505 | |
| 506 | IncompleteVariableArrayTypes.InsertNode(New, InsertPos); |
| 507 | Types.push_back(New); |
| 508 | return QualType(New, 0); |
| 509 | } |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 512 | /// getVectorType - Return the unique reference to a vector type of |
| 513 | /// the specified element type and size. VectorType must be a built-in type. |
| 514 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 515 | BuiltinType *baseType; |
| 516 | |
| 517 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 518 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | |
| 520 | // Check if we've already instantiated a vector of this type. |
| 521 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 522 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 523 | void *InsertPos = 0; |
| 524 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 525 | return QualType(VTP, 0); |
| 526 | |
| 527 | // If the element type isn't canonical, this won't be a canonical type either, |
| 528 | // so fill in the canonical type field. |
| 529 | QualType Canonical; |
| 530 | if (!vecType->isCanonical()) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 531 | Canonical = getVectorType(vecType.getCanonicalType(), NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | |
| 533 | // Get the new insert position for the node we care about. |
| 534 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 535 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 536 | } |
| 537 | VectorType *New = new VectorType(vecType, NumElts, Canonical); |
| 538 | VectorTypes.InsertNode(New, InsertPos); |
| 539 | Types.push_back(New); |
| 540 | return QualType(New, 0); |
| 541 | } |
| 542 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 543 | /// getOCUVectorType - Return the unique reference to an OCU vector type of |
| 544 | /// the specified element type and size. VectorType must be a built-in type. |
| 545 | QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) { |
| 546 | BuiltinType *baseType; |
| 547 | |
| 548 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 549 | assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type"); |
| 550 | |
| 551 | // Check if we've already instantiated a vector of this type. |
| 552 | llvm::FoldingSetNodeID ID; |
| 553 | VectorType::Profile(ID, vecType, NumElts, Type::OCUVector); |
| 554 | void *InsertPos = 0; |
| 555 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 556 | return QualType(VTP, 0); |
| 557 | |
| 558 | // If the element type isn't canonical, this won't be a canonical type either, |
| 559 | // so fill in the canonical type field. |
| 560 | QualType Canonical; |
| 561 | if (!vecType->isCanonical()) { |
| 562 | Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts); |
| 563 | |
| 564 | // Get the new insert position for the node we care about. |
| 565 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 566 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 567 | } |
| 568 | OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical); |
| 569 | VectorTypes.InsertNode(New, InsertPos); |
| 570 | Types.push_back(New); |
| 571 | return QualType(New, 0); |
| 572 | } |
| 573 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 574 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 575 | /// |
| 576 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 577 | // Unique functions, to guarantee there is only one function of a particular |
| 578 | // structure. |
| 579 | llvm::FoldingSetNodeID ID; |
| 580 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 581 | |
| 582 | void *InsertPos = 0; |
| 583 | if (FunctionTypeNoProto *FT = |
| 584 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 585 | return QualType(FT, 0); |
| 586 | |
| 587 | QualType Canonical; |
| 588 | if (!ResultTy->isCanonical()) { |
| 589 | Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType()); |
| 590 | |
| 591 | // Get the new insert position for the node we care about. |
| 592 | FunctionTypeNoProto *NewIP = |
| 593 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 594 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 595 | } |
| 596 | |
| 597 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 598 | Types.push_back(New); |
| 599 | FunctionTypeProtos.InsertNode(New, InsertPos); |
| 600 | return QualType(New, 0); |
| 601 | } |
| 602 | |
| 603 | /// getFunctionType - Return a normal function type with a typed argument |
| 604 | /// list. isVariadic indicates whether the argument list includes '...'. |
| 605 | QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray, |
| 606 | unsigned NumArgs, bool isVariadic) { |
| 607 | // Unique functions, to guarantee there is only one function of a particular |
| 608 | // structure. |
| 609 | llvm::FoldingSetNodeID ID; |
| 610 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic); |
| 611 | |
| 612 | void *InsertPos = 0; |
| 613 | if (FunctionTypeProto *FTP = |
| 614 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 615 | return QualType(FTP, 0); |
| 616 | |
| 617 | // Determine whether the type being created is already canonical or not. |
| 618 | bool isCanonical = ResultTy->isCanonical(); |
| 619 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 620 | if (!ArgArray[i]->isCanonical()) |
| 621 | isCanonical = false; |
| 622 | |
| 623 | // If this type isn't canonical, get the canonical version of it. |
| 624 | QualType Canonical; |
| 625 | if (!isCanonical) { |
| 626 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 627 | CanonicalArgs.reserve(NumArgs); |
| 628 | for (unsigned i = 0; i != NumArgs; ++i) |
| 629 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 630 | |
| 631 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 632 | &CanonicalArgs[0], NumArgs, |
| 633 | isVariadic); |
| 634 | |
| 635 | // Get the new insert position for the node we care about. |
| 636 | FunctionTypeProto *NewIP = |
| 637 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 638 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 639 | } |
| 640 | |
| 641 | // FunctionTypeProto objects are not allocated with new because they have a |
| 642 | // variable size array (for parameter types) at the end of them. |
| 643 | FunctionTypeProto *FTP = |
| 644 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 645 | NumArgs*sizeof(QualType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 646 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 647 | Canonical); |
| 648 | Types.push_back(FTP); |
| 649 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 650 | return QualType(FTP, 0); |
| 651 | } |
| 652 | |
| 653 | /// getTypedefType - Return the unique reference to the type for the |
| 654 | /// specified typename decl. |
| 655 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 656 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 657 | |
| 658 | QualType Canonical = Decl->getUnderlyingType().getCanonicalType(); |
| 659 | Decl->TypeForDecl = new TypedefType(Decl, Canonical); |
| 660 | Types.push_back(Decl->TypeForDecl); |
| 661 | return QualType(Decl->TypeForDecl, 0); |
| 662 | } |
| 663 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 664 | /// getObjcInterfaceType - Return the unique reference to the type for the |
| 665 | /// specified ObjC interface decl. |
| 666 | QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) { |
| 667 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 668 | |
| 669 | Decl->TypeForDecl = new ObjcInterfaceType(Decl); |
| 670 | Types.push_back(Decl->TypeForDecl); |
| 671 | return QualType(Decl->TypeForDecl, 0); |
| 672 | } |
| 673 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 674 | /// getObjcQualifiedInterfaceType - Return a |
| 675 | /// ObjcQualifiedInterfaceType type for the given interface decl and |
| 676 | /// the conforming protocol list. |
| 677 | QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl, |
| 678 | ObjcProtocolDecl **Protocols, unsigned NumProtocols) { |
| 679 | ObjcInterfaceType *IType = |
| 680 | cast<ObjcInterfaceType>(getObjcInterfaceType(Decl)); |
| 681 | |
| 682 | llvm::FoldingSetNodeID ID; |
| 683 | ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols); |
| 684 | |
| 685 | void *InsertPos = 0; |
| 686 | if (ObjcQualifiedInterfaceType *QT = |
| 687 | ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 688 | return QualType(QT, 0); |
| 689 | |
| 690 | // No Match; |
Chris Lattner | 00bb283 | 2007-10-11 03:36:41 +0000 | [diff] [blame] | 691 | ObjcQualifiedInterfaceType *QType = |
| 692 | new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 693 | Types.push_back(QType); |
| 694 | ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
| 695 | return QualType(QType, 0); |
| 696 | } |
| 697 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 698 | /// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique |
| 699 | /// TypeOfExpr AST's (since expression's are never shared). For example, |
| 700 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 701 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 702 | /// on canonical type's (which are always unique). |
Steve Naroff | 8d1a3b8 | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 703 | QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 704 | QualType Canonical = tofExpr->getType().getCanonicalType(); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 705 | TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical); |
| 706 | Types.push_back(toe); |
| 707 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 710 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 711 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 712 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 713 | /// an issue. This doesn't effect the type checker, since it operates |
| 714 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 715 | QualType ASTContext::getTypeOfType(QualType tofType) { |
| 716 | QualType Canonical = tofType.getCanonicalType(); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 717 | TypeOfType *tot = new TypeOfType(tofType, Canonical); |
| 718 | Types.push_back(tot); |
| 719 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 722 | /// getTagDeclType - Return the unique reference to the type for the |
| 723 | /// specified TagDecl (struct/union/class/enum) decl. |
| 724 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 725 | assert (Decl); |
| 726 | |
Ted Kremenek | ea0c6fb | 2007-11-14 00:03:20 +0000 | [diff] [blame] | 727 | // The decl stores the type cache. |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 728 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Ted Kremenek | ea0c6fb | 2007-11-14 00:03:20 +0000 | [diff] [blame] | 729 | |
| 730 | TagType* T = new TagType(Decl, QualType()); |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 731 | Types.push_back(T); |
| 732 | Decl->TypeForDecl = T; |
Ted Kremenek | ea0c6fb | 2007-11-14 00:03:20 +0000 | [diff] [blame] | 733 | |
| 734 | return QualType(T, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 738 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 739 | /// needs to agree with the definition in <stddef.h>. |
| 740 | QualType ASTContext::getSizeType() const { |
| 741 | // On Darwin, size_t is defined as a "long unsigned int". |
| 742 | // FIXME: should derive from "Target". |
| 743 | return UnsignedLongTy; |
| 744 | } |
| 745 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 746 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 747 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 748 | QualType ASTContext::getPointerDiffType() const { |
| 749 | // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug... |
| 750 | // FIXME: should derive from "Target". |
| 751 | return IntTy; |
| 752 | } |
| 753 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 754 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 755 | /// routine will assert if passed a built-in type that isn't an integer or enum. |
| 756 | static int getIntegerRank(QualType t) { |
| 757 | if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) { |
| 758 | assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum"); |
| 759 | return 4; |
| 760 | } |
| 761 | |
| 762 | const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType()); |
| 763 | switch (BT->getKind()) { |
| 764 | default: |
| 765 | assert(0 && "getIntegerRank(): not a built-in integer"); |
| 766 | case BuiltinType::Bool: |
| 767 | return 1; |
| 768 | case BuiltinType::Char_S: |
| 769 | case BuiltinType::Char_U: |
| 770 | case BuiltinType::SChar: |
| 771 | case BuiltinType::UChar: |
| 772 | return 2; |
| 773 | case BuiltinType::Short: |
| 774 | case BuiltinType::UShort: |
| 775 | return 3; |
| 776 | case BuiltinType::Int: |
| 777 | case BuiltinType::UInt: |
| 778 | return 4; |
| 779 | case BuiltinType::Long: |
| 780 | case BuiltinType::ULong: |
| 781 | return 5; |
| 782 | case BuiltinType::LongLong: |
| 783 | case BuiltinType::ULongLong: |
| 784 | return 6; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /// getFloatingRank - Return a relative rank for floating point types. |
| 789 | /// This routine will assert if passed a built-in type that isn't a float. |
| 790 | static int getFloatingRank(QualType T) { |
| 791 | T = T.getCanonicalType(); |
| 792 | if (ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 793 | return getFloatingRank(CT->getElementType()); |
| 794 | |
| 795 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 796 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 797 | case BuiltinType::Float: return FloatRank; |
| 798 | case BuiltinType::Double: return DoubleRank; |
| 799 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 800 | } |
| 801 | } |
| 802 | |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 803 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 804 | /// point or a complex type (based on typeDomain/typeSize). |
| 805 | /// 'typeDomain' is a real floating point or complex type. |
| 806 | /// 'typeSize' is a real floating point or complex type. |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 807 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain( |
| 808 | QualType typeSize, QualType typeDomain) const { |
| 809 | if (typeDomain->isComplexType()) { |
| 810 | switch (getFloatingRank(typeSize)) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 811 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 812 | case FloatRank: return FloatComplexTy; |
| 813 | case DoubleRank: return DoubleComplexTy; |
| 814 | case LongDoubleRank: return LongDoubleComplexTy; |
| 815 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 816 | } |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 817 | if (typeDomain->isRealFloatingType()) { |
| 818 | switch (getFloatingRank(typeSize)) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 819 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 820 | case FloatRank: return FloatTy; |
| 821 | case DoubleRank: return DoubleTy; |
| 822 | case LongDoubleRank: return LongDoubleTy; |
| 823 | } |
| 824 | } |
| 825 | assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain"); |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 826 | //an invalid return value, but the assert |
| 827 | //will ensure that this code is never reached. |
| 828 | return VoidTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 831 | /// compareFloatingType - Handles 3 different combos: |
| 832 | /// float/float, float/complex, complex/complex. |
| 833 | /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. |
| 834 | int ASTContext::compareFloatingType(QualType lt, QualType rt) { |
| 835 | if (getFloatingRank(lt) == getFloatingRank(rt)) |
| 836 | return 0; |
| 837 | if (getFloatingRank(lt) > getFloatingRank(rt)) |
| 838 | return 1; |
| 839 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | // maxIntegerType - Returns the highest ranked integer type. Handles 3 case: |
| 843 | // unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1. |
| 844 | QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) { |
| 845 | if (lhs == rhs) return lhs; |
| 846 | |
| 847 | bool t1Unsigned = lhs->isUnsignedIntegerType(); |
| 848 | bool t2Unsigned = rhs->isUnsignedIntegerType(); |
| 849 | |
| 850 | if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned)) |
| 851 | return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs; |
| 852 | |
| 853 | // We have two integer types with differing signs |
| 854 | QualType unsignedType = t1Unsigned ? lhs : rhs; |
| 855 | QualType signedType = t1Unsigned ? rhs : lhs; |
| 856 | |
| 857 | if (getIntegerRank(unsignedType) >= getIntegerRank(signedType)) |
| 858 | return unsignedType; |
| 859 | else { |
| 860 | // FIXME: Need to check if the signed type can represent all values of the |
| 861 | // unsigned type. If it can, then the result is the signed type. |
| 862 | // If it can't, then the result is the unsigned version of the signed type. |
| 863 | // Should probably add a helper that returns a signed integer type from |
| 864 | // an unsigned (and vice versa). C99 6.3.1.8. |
| 865 | return signedType; |
| 866 | } |
| 867 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 868 | |
| 869 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 870 | QualType ASTContext::getCFConstantStringType() { |
| 871 | if (!CFConstantStringTypeDecl) { |
| 872 | CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(), |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 873 | &Idents.get("NSConstantString"), |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 874 | 0); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 875 | QualType FieldTypes[4]; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 876 | |
| 877 | // const int *isa; |
| 878 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 879 | // int flags; |
| 880 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 881 | // const char *str; |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 882 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 883 | // long length; |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 884 | FieldTypes[3] = LongTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 885 | // Create fields |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 886 | FieldDecl *FieldDecls[4]; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 887 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 888 | for (unsigned i = 0; i < 4; ++i) |
Steve Naroff | f38661e | 2007-09-14 02:20:46 +0000 | [diff] [blame] | 889 | FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 890 | |
| 891 | CFConstantStringTypeDecl->defineBody(FieldDecls, 4); |
| 892 | } |
| 893 | |
| 894 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 895 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 896 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 897 | // This returns true if a type has been typedefed to BOOL: |
| 898 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 899 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 900 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 901 | return !strcmp(TT->getDecl()->getName(), "BOOL"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 902 | |
| 903 | return false; |
| 904 | } |
| 905 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 906 | /// getObjcEncodingTypeSize returns size of type for objective-c encoding |
| 907 | /// purpose. |
| 908 | int ASTContext::getObjcEncodingTypeSize(QualType type) { |
| 909 | SourceLocation Loc; |
| 910 | uint64_t sz = getTypeSize(type, Loc); |
| 911 | |
| 912 | // Make all integer and enum types at least as large as an int |
| 913 | if (sz > 0 && type->isIntegralType()) |
| 914 | sz = std::max(sz, getTypeSize(IntTy, Loc)); |
| 915 | // Treat arrays as pointers, since that's how they're passed in. |
| 916 | else if (type->isArrayType()) |
| 917 | sz = getTypeSize(VoidPtrTy, Loc); |
| 918 | return sz / getTypeSize(CharTy, Loc); |
| 919 | } |
| 920 | |
| 921 | /// getObjcEncodingForMethodDecl - Return the encoded type for this method |
| 922 | /// declaration. |
| 923 | void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, |
| 924 | std::string& S) |
| 925 | { |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 926 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
| 927 | getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 928 | // Encode result type. |
| 929 | getObjcEncodingForType(Decl->getResultType(), S); |
| 930 | // Compute size of all parameters. |
| 931 | // Start with computing size of a pointer in number of bytes. |
| 932 | // FIXME: There might(should) be a better way of doing this computation! |
| 933 | SourceLocation Loc; |
| 934 | int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc); |
| 935 | // The first two arguments (self and _cmd) are pointers; account for |
| 936 | // their size. |
| 937 | int ParmOffset = 2 * PtrSize; |
| 938 | int NumOfParams = Decl->getNumParams(); |
| 939 | for (int i = 0; i < NumOfParams; i++) { |
| 940 | QualType PType = Decl->getParamDecl(i)->getType(); |
| 941 | int sz = getObjcEncodingTypeSize (PType); |
| 942 | assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type"); |
| 943 | ParmOffset += sz; |
| 944 | } |
| 945 | S += llvm::utostr(ParmOffset); |
| 946 | S += "@0:"; |
| 947 | S += llvm::utostr(PtrSize); |
| 948 | |
| 949 | // Argument types. |
| 950 | ParmOffset = 2 * PtrSize; |
| 951 | for (int i = 0; i < NumOfParams; i++) { |
| 952 | QualType PType = Decl->getParamDecl(i)->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 953 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 954 | // 'in', 'inout', etc. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 955 | getObjcEncodingForTypeQualifier( |
| 956 | Decl->getParamDecl(i)->getObjcDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 957 | getObjcEncodingForType(PType, S); |
| 958 | S += llvm::utostr(ParmOffset); |
| 959 | ParmOffset += getObjcEncodingTypeSize(PType); |
| 960 | } |
| 961 | } |
| 962 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 963 | void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const |
| 964 | { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 965 | // FIXME: This currently doesn't encode: |
| 966 | // @ An object (whether statically typed or typed id) |
| 967 | // # A class object (Class) |
| 968 | // : A method selector (SEL) |
| 969 | // {name=type...} A structure |
| 970 | // (name=type...) A union |
| 971 | // bnum A bit field of num bits |
| 972 | |
| 973 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 974 | char encoding; |
| 975 | switch (BT->getKind()) { |
| 976 | case BuiltinType::Void: |
| 977 | encoding = 'v'; |
| 978 | break; |
| 979 | case BuiltinType::Bool: |
| 980 | encoding = 'B'; |
| 981 | break; |
| 982 | case BuiltinType::Char_U: |
| 983 | case BuiltinType::UChar: |
| 984 | encoding = 'C'; |
| 985 | break; |
| 986 | case BuiltinType::UShort: |
| 987 | encoding = 'S'; |
| 988 | break; |
| 989 | case BuiltinType::UInt: |
| 990 | encoding = 'I'; |
| 991 | break; |
| 992 | case BuiltinType::ULong: |
| 993 | encoding = 'L'; |
| 994 | break; |
| 995 | case BuiltinType::ULongLong: |
| 996 | encoding = 'Q'; |
| 997 | break; |
| 998 | case BuiltinType::Char_S: |
| 999 | case BuiltinType::SChar: |
| 1000 | encoding = 'c'; |
| 1001 | break; |
| 1002 | case BuiltinType::Short: |
| 1003 | encoding = 's'; |
| 1004 | break; |
| 1005 | case BuiltinType::Int: |
| 1006 | encoding = 'i'; |
| 1007 | break; |
| 1008 | case BuiltinType::Long: |
| 1009 | encoding = 'l'; |
| 1010 | break; |
| 1011 | case BuiltinType::LongLong: |
| 1012 | encoding = 'q'; |
| 1013 | break; |
| 1014 | case BuiltinType::Float: |
| 1015 | encoding = 'f'; |
| 1016 | break; |
| 1017 | case BuiltinType::Double: |
| 1018 | encoding = 'd'; |
| 1019 | break; |
| 1020 | case BuiltinType::LongDouble: |
| 1021 | encoding = 'd'; |
| 1022 | break; |
| 1023 | default: |
| 1024 | assert(0 && "Unhandled builtin type kind"); |
| 1025 | } |
| 1026 | |
| 1027 | S += encoding; |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1028 | } else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1029 | QualType PointeeTy = PT->getPointeeType(); |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1030 | if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) { |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1031 | S += '@'; |
| 1032 | return; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1033 | } else if (isObjcClassType(PointeeTy)) { |
| 1034 | S += '#'; |
| 1035 | return; |
| 1036 | } else if (isObjcSelType(PointeeTy)) { |
| 1037 | S += ':'; |
| 1038 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1039 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1040 | |
| 1041 | if (PointeeTy->isCharType()) { |
| 1042 | // char pointer types should be encoded as '*' unless it is a |
| 1043 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1044 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1045 | S += '*'; |
| 1046 | return; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | S += '^'; |
| 1051 | getObjcEncodingForType(PT->getPointeeType(), S); |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1052 | } else if (const ArrayType *AT = T->getAsArrayType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1053 | S += '['; |
| 1054 | |
| 1055 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1056 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 1057 | else |
| 1058 | assert(0 && "Unhandled array type!"); |
| 1059 | |
| 1060 | getObjcEncodingForType(AT->getElementType(), S); |
| 1061 | S += ']'; |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 1062 | } else if (T->getAsFunctionType()) { |
| 1063 | S += '?'; |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1064 | } else if (const RecordType *RTy = T->getAsRecordType()) { |
| 1065 | RecordDecl *RDecl= RTy->getDecl(); |
| 1066 | S += '{'; |
| 1067 | S += RDecl->getName(); |
| 1068 | S += '='; |
| 1069 | for (int i = 0; i < RDecl->getNumMembers(); i++) { |
| 1070 | FieldDecl *field = RDecl->getMember(i); |
| 1071 | getObjcEncodingForType(field->getType(), S); |
| 1072 | } |
| 1073 | S += '}'; |
Steve Naroff | 5e71124 | 2007-12-12 22:30:11 +0000 | [diff] [blame^] | 1074 | } else if (T->isEnumeralType()) { |
| 1075 | S += 'i'; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1076 | } else |
Steve Naroff | 5e71124 | 2007-12-12 22:30:11 +0000 | [diff] [blame^] | 1077 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1080 | void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT, |
| 1081 | std::string& S) const { |
| 1082 | if (QT & Decl::OBJC_TQ_In) |
| 1083 | S += 'n'; |
| 1084 | if (QT & Decl::OBJC_TQ_Inout) |
| 1085 | S += 'N'; |
| 1086 | if (QT & Decl::OBJC_TQ_Out) |
| 1087 | S += 'o'; |
| 1088 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 1089 | S += 'O'; |
| 1090 | if (QT & Decl::OBJC_TQ_Byref) |
| 1091 | S += 'R'; |
| 1092 | if (QT & Decl::OBJC_TQ_Oneway) |
| 1093 | S += 'V'; |
| 1094 | } |
| 1095 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1096 | void ASTContext::setBuiltinVaListType(QualType T) |
| 1097 | { |
| 1098 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 1099 | |
| 1100 | BuiltinVaListType = T; |
| 1101 | } |
| 1102 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1103 | void ASTContext::setObjcIdType(TypedefDecl *TD) |
| 1104 | { |
| 1105 | assert(ObjcIdType.isNull() && "'id' type already set!"); |
| 1106 | |
| 1107 | ObjcIdType = getTypedefType(TD); |
| 1108 | |
| 1109 | // typedef struct objc_object *id; |
| 1110 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1111 | assert(ptr && "'id' incorrectly typed"); |
| 1112 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1113 | assert(rec && "'id' incorrectly typed"); |
| 1114 | IdStructType = rec; |
| 1115 | } |
| 1116 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1117 | void ASTContext::setObjcSelType(TypedefDecl *TD) |
| 1118 | { |
| 1119 | assert(ObjcSelType.isNull() && "'SEL' type already set!"); |
| 1120 | |
| 1121 | ObjcSelType = getTypedefType(TD); |
| 1122 | |
| 1123 | // typedef struct objc_selector *SEL; |
| 1124 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1125 | assert(ptr && "'SEL' incorrectly typed"); |
| 1126 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1127 | assert(rec && "'SEL' incorrectly typed"); |
| 1128 | SelStructType = rec; |
| 1129 | } |
| 1130 | |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 1131 | void ASTContext::setObjcProtoType(QualType QT) |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1132 | { |
| 1133 | assert(ObjcProtoType.isNull() && "'Protocol' type already set!"); |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 1134 | ObjcProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1137 | void ASTContext::setObjcClassType(TypedefDecl *TD) |
| 1138 | { |
| 1139 | assert(ObjcClassType.isNull() && "'Class' type already set!"); |
| 1140 | |
| 1141 | ObjcClassType = getTypedefType(TD); |
| 1142 | |
| 1143 | // typedef struct objc_class *Class; |
| 1144 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1145 | assert(ptr && "'Class' incorrectly typed"); |
| 1146 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1147 | assert(rec && "'Class' incorrectly typed"); |
| 1148 | ClassStructType = rec; |
| 1149 | } |
| 1150 | |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1151 | void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) { |
| 1152 | assert(ObjcConstantStringType.isNull() && |
| 1153 | "'NSConstantString' type already set!"); |
| 1154 | |
| 1155 | ObjcConstantStringType = getObjcInterfaceType(Decl); |
| 1156 | } |
| 1157 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1158 | bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1159 | const BuiltinType *lBuiltin = lhs->getAsBuiltinType(); |
| 1160 | const BuiltinType *rBuiltin = rhs->getAsBuiltinType(); |
| 1161 | |
| 1162 | return lBuiltin->getKind() == rBuiltin->getKind(); |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1167 | if (lhs->isObjcInterfaceType() && isObjcIdType(rhs)) |
| 1168 | return true; |
| 1169 | else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType()) |
| 1170 | return true; |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1175 | return true; // FIXME: IMPLEMENT. |
| 1176 | } |
| 1177 | |
Fariborz Jahanian | 4ffc541 | 2007-12-12 01:00:23 +0000 | [diff] [blame] | 1178 | bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs, |
| 1179 | QualType rhs) { |
| 1180 | ObjcQualifiedInterfaceType *lhsQI = |
| 1181 | dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr()); |
| 1182 | assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type"); |
| 1183 | ObjcQualifiedInterfaceType *rhsQI = |
| 1184 | dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr()); |
| 1185 | assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type"); |
| 1186 | if (!interfaceTypesAreCompatible(QualType(lhsQI->getInterfaceType(), 0), |
| 1187 | QualType(rhsQI->getInterfaceType(), 0))) |
| 1188 | return false; |
| 1189 | /* All protocols in lhs must have a presense in rhs. */ |
| 1190 | for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) { |
| 1191 | bool match = false; |
| 1192 | ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i); |
| 1193 | for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) { |
| 1194 | ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j); |
| 1195 | if (lhsProto == rhsProto) { |
| 1196 | match = true; |
| 1197 | break; |
| 1198 | } |
| 1199 | } |
| 1200 | if (!match) |
| 1201 | return false; |
| 1202 | } |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1206 | bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1207 | const VectorType *lVector = lhs->getAsVectorType(); |
| 1208 | const VectorType *rVector = rhs->getAsVectorType(); |
| 1209 | |
| 1210 | if ((lVector->getElementType().getCanonicalType() == |
| 1211 | rVector->getElementType().getCanonicalType()) && |
| 1212 | (lVector->getNumElements() == rVector->getNumElements())) |
| 1213 | return true; |
| 1214 | return false; |
| 1215 | } |
| 1216 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1217 | // C99 6.2.7p1: If both are complete types, then the following additional |
| 1218 | // requirements apply...FIXME (handle compatibility across source files). |
| 1219 | bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1220 | TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl(); |
| 1221 | TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl(); |
| 1222 | |
| 1223 | if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) { |
| 1224 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1225 | return true; |
| 1226 | } |
| 1227 | if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) { |
| 1228 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1229 | return true; |
| 1230 | } |
Steve Naroff | ab37309 | 2007-11-07 06:03:51 +0000 | [diff] [blame] | 1231 | // "Class" and "id" are compatible built-in structure types. |
| 1232 | if (isObjcIdType(lhs) && isObjcClassType(rhs) || |
| 1233 | isObjcClassType(lhs) && isObjcIdType(rhs)) |
| 1234 | return true; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
| 1237 | |
| 1238 | bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1239 | // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be |
| 1240 | // identically qualified and both shall be pointers to compatible types. |
| 1241 | if (lhs.getQualifiers() != rhs.getQualifiers()) |
| 1242 | return false; |
| 1243 | |
| 1244 | QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType(); |
| 1245 | QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType(); |
| 1246 | |
| 1247 | return typesAreCompatible(ltype, rtype); |
| 1248 | } |
| 1249 | |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 1250 | // C++ 5.17p6: When the left operand of an assignment operator denotes a |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1251 | // reference to T, the operation assigns to the object of type T denoted by the |
| 1252 | // reference. |
| 1253 | bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1254 | QualType ltype = lhs; |
| 1255 | |
| 1256 | if (lhs->isReferenceType()) |
| 1257 | ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType(); |
| 1258 | |
| 1259 | QualType rtype = rhs; |
| 1260 | |
| 1261 | if (rhs->isReferenceType()) |
| 1262 | rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType(); |
| 1263 | |
| 1264 | return typesAreCompatible(ltype, rtype); |
| 1265 | } |
| 1266 | |
| 1267 | bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1268 | const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType()); |
| 1269 | const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType()); |
| 1270 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 1271 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 1272 | |
| 1273 | // first check the return types (common between C99 and K&R). |
| 1274 | if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType())) |
| 1275 | return false; |
| 1276 | |
| 1277 | if (lproto && rproto) { // two C99 style function prototypes |
| 1278 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 1279 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 1280 | |
| 1281 | if (lproto_nargs != rproto_nargs) |
| 1282 | return false; |
| 1283 | |
| 1284 | // both prototypes have the same number of arguments. |
| 1285 | if ((lproto->isVariadic() && !rproto->isVariadic()) || |
| 1286 | (rproto->isVariadic() && !lproto->isVariadic())) |
| 1287 | return false; |
| 1288 | |
| 1289 | // The use of ellipsis agree...now check the argument types. |
| 1290 | for (unsigned i = 0; i < lproto_nargs; i++) |
| 1291 | if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i))) |
| 1292 | return false; |
| 1293 | return true; |
| 1294 | } |
| 1295 | if (!lproto && !rproto) // two K&R style function decls, nothing to do. |
| 1296 | return true; |
| 1297 | |
| 1298 | // we have a mixture of K&R style with C99 prototypes |
| 1299 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 1300 | |
| 1301 | if (proto->isVariadic()) |
| 1302 | return false; |
| 1303 | |
| 1304 | // FIXME: Each parameter type T in the prototype must be compatible with the |
| 1305 | // type resulting from applying the usual argument conversions to T. |
| 1306 | return true; |
| 1307 | } |
| 1308 | |
| 1309 | bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1310 | QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType(); |
| 1311 | QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType(); |
| 1312 | |
| 1313 | if (!typesAreCompatible(ltype, rtype)) |
| 1314 | return false; |
| 1315 | |
| 1316 | // FIXME: If both types specify constant sizes, then the sizes must also be |
| 1317 | // the same. Even if the sizes are the same, GCC produces an error. |
| 1318 | return true; |
| 1319 | } |
| 1320 | |
| 1321 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 1322 | /// both shall have the identically qualified version of a compatible type. |
| 1323 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 1324 | /// same. See 6.7.[2,3,5] for additional rules. |
| 1325 | bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) { |
| 1326 | QualType lcanon = lhs.getCanonicalType(); |
| 1327 | QualType rcanon = rhs.getCanonicalType(); |
| 1328 | |
| 1329 | // If two types are identical, they are are compatible |
| 1330 | if (lcanon == rcanon) |
| 1331 | return true; |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 1332 | |
| 1333 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 1334 | // type is adjusted to "T" prior to any further analysis, the expression |
| 1335 | // designates the object or function denoted by the reference, and the |
| 1336 | // expression is an lvalue. |
| 1337 | if (lcanon->getTypeClass() == Type::Reference) |
| 1338 | lcanon = cast<ReferenceType>(lcanon)->getReferenceeType(); |
| 1339 | if (rcanon->getTypeClass() == Type::Reference) |
| 1340 | rcanon = cast<ReferenceType>(rcanon)->getReferenceeType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1341 | |
| 1342 | // If the canonical type classes don't match, they can't be compatible |
| 1343 | if (lcanon->getTypeClass() != rcanon->getTypeClass()) { |
| 1344 | // For Objective-C, it is possible for two types to be compatible |
| 1345 | // when their classes don't match (when dealing with "id"). If either type |
| 1346 | // is an interface, we defer to objcTypesAreCompatible(). |
| 1347 | if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType()) |
| 1348 | return objcTypesAreCompatible(lcanon, rcanon); |
| 1349 | return false; |
| 1350 | } |
| 1351 | switch (lcanon->getTypeClass()) { |
| 1352 | case Type::Pointer: |
| 1353 | return pointerTypesAreCompatible(lcanon, rcanon); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1354 | case Type::ConstantArray: |
| 1355 | case Type::VariableArray: |
| 1356 | return arrayTypesAreCompatible(lcanon, rcanon); |
| 1357 | case Type::FunctionNoProto: |
| 1358 | case Type::FunctionProto: |
| 1359 | return functionTypesAreCompatible(lcanon, rcanon); |
| 1360 | case Type::Tagged: // handle structures, unions |
| 1361 | return tagTypesAreCompatible(lcanon, rcanon); |
| 1362 | case Type::Builtin: |
| 1363 | return builtinTypesAreCompatible(lcanon, rcanon); |
| 1364 | case Type::ObjcInterface: |
| 1365 | return interfaceTypesAreCompatible(lcanon, rcanon); |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1366 | case Type::Vector: |
| 1367 | case Type::OCUVector: |
| 1368 | return vectorTypesAreCompatible(lcanon, rcanon); |
Fariborz Jahanian | 4ffc541 | 2007-12-12 01:00:23 +0000 | [diff] [blame] | 1369 | case Type::ObjcQualifiedInterface: |
| 1370 | return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1371 | default: |
| 1372 | assert(0 && "unexpected type"); |
| 1373 | } |
| 1374 | return true; // should never get here... |
| 1375 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1376 | |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1377 | /// Emit - Serialize an ASTContext object to Bitcode. |
| 1378 | void ASTContext::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 5451350 | 2007-10-31 20:00:03 +0000 | [diff] [blame] | 1379 | S.EmitRef(SourceMgr); |
| 1380 | S.EmitRef(Target); |
| 1381 | S.EmitRef(Idents); |
| 1382 | S.EmitRef(Selectors); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1383 | |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1384 | // Emit the size of the type vector so that we can reserve that size |
| 1385 | // when we reconstitute the ASTContext object. |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1386 | S.EmitInt(Types.size()); |
| 1387 | |
Ted Kremenek | 03ed440 | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 1388 | for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); |
| 1389 | I!=E;++I) |
| 1390 | (*I)->Emit(S); |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1391 | |
Ted Kremenek | a9a4a24 | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 1392 | // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Ted Kremenek | 0f84c00 | 2007-11-13 00:25:37 +0000 | [diff] [blame] | 1395 | ASTContext* ASTContext::Create(llvm::Deserializer& D) { |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1396 | SourceManager &SM = D.ReadRef<SourceManager>(); |
| 1397 | TargetInfo &t = D.ReadRef<TargetInfo>(); |
| 1398 | IdentifierTable &idents = D.ReadRef<IdentifierTable>(); |
| 1399 | SelectorTable &sels = D.ReadRef<SelectorTable>(); |
| 1400 | |
| 1401 | unsigned size_reserve = D.ReadInt(); |
| 1402 | |
| 1403 | ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve); |
| 1404 | |
Ted Kremenek | 03ed440 | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 1405 | for (unsigned i = 0; i < size_reserve; ++i) |
| 1406 | Type::Create(*A,i,D); |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1407 | |
Ted Kremenek | a9a4a24 | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 1408 | // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1409 | |
| 1410 | return A; |
| 1411 | } |