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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/RecordLayout.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/Serialize.h" |
| 22 | #include "llvm/Bitcode/Deserialize.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 23 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | enum FloatingRank { |
| 27 | FloatRank, DoubleRank, LongDoubleRank |
| 28 | }; |
| 29 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 30 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
| 31 | TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 32 | IdentifierTable &idents, SelectorTable &sels, |
| 33 | unsigned size_reserve) : |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 34 | CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0), |
| 35 | SourceMgr(SM), LangOpts(LOpts), Target(t), |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 36 | Idents(idents), Selectors(sels) |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 37 | { |
| 38 | if (size_reserve > 0) Types.reserve(size_reserve); |
| 39 | InitBuiltinTypes(); |
| 40 | BuiltinInfo.InitializeBuiltins(idents, Target); |
| 41 | TUDecl = TranslationUnitDecl::Create(*this); |
| 42 | } |
| 43 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | ASTContext::~ASTContext() { |
| 45 | // Deallocate all the types. |
| 46 | while (!Types.empty()) { |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 47 | Types.back()->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | Types.pop_back(); |
| 49 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 50 | |
| 51 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void ASTContext::PrintStats() const { |
| 55 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 56 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 57 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
Daniel Dunbar | 248e1c0 | 2008-09-26 03:23:00 +0000 | [diff] [blame] | 58 | unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
| 60 | |
| 61 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 62 | unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0; |
| 63 | unsigned NumObjCQualifiedIds = 0; |
Steve Naroff | 6cc1896 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 64 | unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | |
| 66 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 67 | Type *T = Types[i]; |
| 68 | if (isa<BuiltinType>(T)) |
| 69 | ++NumBuiltin; |
| 70 | else if (isa<PointerType>(T)) |
| 71 | ++NumPointer; |
Daniel Dunbar | 248e1c0 | 2008-09-26 03:23:00 +0000 | [diff] [blame] | 72 | else if (isa<BlockPointerType>(T)) |
| 73 | ++NumBlockPointer; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | else if (isa<ReferenceType>(T)) |
| 75 | ++NumReference; |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 76 | else if (isa<ComplexType>(T)) |
| 77 | ++NumComplex; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | else if (isa<ArrayType>(T)) |
| 79 | ++NumArray; |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 80 | else if (isa<VectorType>(T)) |
| 81 | ++NumVector; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | else if (isa<FunctionTypeNoProto>(T)) |
| 83 | ++NumFunctionNP; |
| 84 | else if (isa<FunctionTypeProto>(T)) |
| 85 | ++NumFunctionP; |
| 86 | else if (isa<TypedefType>(T)) |
| 87 | ++NumTypeName; |
| 88 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 89 | ++NumTagged; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 90 | switch (TT->getDecl()->getTagKind()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | default: assert(0 && "Unknown tagged type!"); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 92 | case TagDecl::TK_struct: ++NumTagStruct; break; |
| 93 | case TagDecl::TK_union: ++NumTagUnion; break; |
| 94 | case TagDecl::TK_class: ++NumTagClass; break; |
| 95 | case TagDecl::TK_enum: ++NumTagEnum; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 97 | } else if (isa<ObjCInterfaceType>(T)) |
| 98 | ++NumObjCInterfaces; |
| 99 | else if (isa<ObjCQualifiedInterfaceType>(T)) |
| 100 | ++NumObjCQualifiedInterfaces; |
| 101 | else if (isa<ObjCQualifiedIdType>(T)) |
| 102 | ++NumObjCQualifiedIds; |
Steve Naroff | 6cc1896 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 103 | else if (isa<TypeOfType>(T)) |
| 104 | ++NumTypeOfTypes; |
| 105 | else if (isa<TypeOfExpr>(T)) |
| 106 | ++NumTypeOfExprs; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 107 | else { |
Chris Lattner | beb6636 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 108 | QualType(T, 0).dump(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | assert(0 && "Unknown type!"); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 114 | fprintf(stderr, " %d pointer types\n", NumPointer); |
Daniel Dunbar | 248e1c0 | 2008-09-26 03:23:00 +0000 | [diff] [blame] | 115 | fprintf(stderr, " %d block pointer types\n", NumBlockPointer); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | fprintf(stderr, " %d reference types\n", NumReference); |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 117 | fprintf(stderr, " %d complex types\n", NumComplex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | fprintf(stderr, " %d array types\n", NumArray); |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 119 | fprintf(stderr, " %d vector types\n", NumVector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 121 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 122 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 123 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 124 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 125 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 126 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 127 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 128 | fprintf(stderr, " %d interface types\n", NumObjCInterfaces); |
Chris Lattner | beb6636 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 129 | fprintf(stderr, " %d protocol qualified interface types\n", |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 130 | NumObjCQualifiedInterfaces); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 131 | fprintf(stderr, " %d protocol qualified id types\n", |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 132 | NumObjCQualifiedIds); |
Steve Naroff | 6cc1896 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 133 | fprintf(stderr, " %d typeof types\n", NumTypeOfTypes); |
| 134 | fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs); |
| 135 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 137 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 138 | NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 140 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
Steve Naroff | 6cc1896 | 2008-05-21 15:59:22 +0000 | [diff] [blame] | 141 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+ |
| 142 | NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 147 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
| 148 | } |
| 149 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | void ASTContext::InitBuiltinTypes() { |
| 151 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 152 | |
| 153 | // C99 6.2.5p19. |
| 154 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 155 | |
| 156 | // C99 6.2.5p2. |
| 157 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 158 | // C99 6.2.5p3. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 159 | if (Target.isCharSigned()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 161 | else |
| 162 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 163 | // C99 6.2.5p4. |
| 164 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 165 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 166 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 167 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 168 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 169 | |
| 170 | // C99 6.2.5p6. |
| 171 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 172 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 173 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 174 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 175 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 176 | |
| 177 | // C99 6.2.5p10. |
| 178 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 179 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 180 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 181 | |
| 182 | // C++ 3.9.1p5 |
| 183 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 184 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 185 | // Placeholder type for functions. |
| 186 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 188 | // C99 6.2.5p11. |
| 189 | FloatComplexTy = getComplexType(FloatTy); |
| 190 | DoubleComplexTy = getComplexType(DoubleTy); |
| 191 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 192 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 193 | BuiltinVaListType = QualType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 194 | ObjCIdType = QualType(); |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 195 | IdStructType = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 196 | ObjCClassType = QualType(); |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 197 | ClassStructType = 0; |
| 198 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 199 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 200 | |
| 201 | // void * type |
| 202 | VoidPtrTy = getPointerType(VoidTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 205 | //===----------------------------------------------------------------------===// |
| 206 | // Type Sizing and Analysis |
| 207 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 208 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 209 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 210 | /// scalar floating point type. |
| 211 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
| 212 | const BuiltinType *BT = T->getAsBuiltinType(); |
| 213 | assert(BT && "Not a floating point type!"); |
| 214 | switch (BT->getKind()) { |
| 215 | default: assert(0 && "Not a floating point type!"); |
| 216 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 217 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 218 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 223 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 224 | /// does not work on incomplete types. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 225 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 226 | ASTContext::getTypeInfo(const Type *T) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 227 | T = getCanonicalType(T); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 228 | uint64_t Width; |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 229 | unsigned Align; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 230 | switch (T->getTypeClass()) { |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 231 | case Type::TypeName: assert(0 && "Not a canonical type!"); |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 232 | case Type::FunctionNoProto: |
| 233 | case Type::FunctionProto: |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 234 | default: |
Chris Lattner | b1c2df9 | 2007-07-20 18:13:33 +0000 | [diff] [blame] | 235 | assert(0 && "Incomplete types have no size!"); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 236 | case Type::VariableArray: |
| 237 | assert(0 && "VLAs not implemented yet!"); |
| 238 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 239 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 241 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 242 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 243 | Align = EltInfo.second; |
| 244 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 245 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 246 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 247 | case Type::Vector: { |
| 248 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 249 | getTypeInfo(cast<VectorType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 250 | Width = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 251 | // FIXME: This isn't right for unusual vectors |
| 252 | Align = Width; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 253 | break; |
| 254 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 256 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 257 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 258 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 259 | case BuiltinType::Void: |
| 260 | assert(0 && "Incomplete types have no size!"); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 261 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 262 | Width = Target.getBoolWidth(); |
| 263 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 264 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 265 | case BuiltinType::Char_S: |
| 266 | case BuiltinType::Char_U: |
| 267 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 268 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 269 | Width = Target.getCharWidth(); |
| 270 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 271 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 272 | case BuiltinType::WChar: |
| 273 | Width = Target.getWCharWidth(); |
| 274 | Align = Target.getWCharAlign(); |
| 275 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 276 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 277 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 278 | Width = Target.getShortWidth(); |
| 279 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 280 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 281 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 282 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 283 | Width = Target.getIntWidth(); |
| 284 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 285 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 286 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 287 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 288 | Width = Target.getLongWidth(); |
| 289 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 290 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 291 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 292 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 293 | Width = Target.getLongLongWidth(); |
| 294 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 295 | break; |
| 296 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 297 | Width = Target.getFloatWidth(); |
| 298 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 299 | break; |
| 300 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 301 | Width = Target.getDoubleWidth(); |
| 302 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 303 | break; |
| 304 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 305 | Width = Target.getLongDoubleWidth(); |
| 306 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 307 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 309 | break; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 310 | case Type::ASQual: |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 311 | // FIXME: Pointers into different addr spaces could have different sizes and |
| 312 | // alignment requirements: getPointerInfo should take an AddrSpace. |
| 313 | return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 314 | case Type::ObjCQualifiedId: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 315 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 316 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 317 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 318 | case Type::BlockPointer: { |
| 319 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 320 | Width = Target.getPointerWidth(AS); |
| 321 | Align = Target.getPointerAlign(AS); |
| 322 | break; |
| 323 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 324 | case Type::Pointer: { |
| 325 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 326 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 327 | Align = Target.getPointerAlign(AS); |
| 328 | break; |
| 329 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 330 | case Type::Reference: |
Chris Lattner | 7ab2ed8 | 2007-07-13 22:16:13 +0000 | [diff] [blame] | 331 | // "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] | 332 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 333 | // FIXME: This is wrong for struct layout: a reference in a struct has |
| 334 | // pointer size. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 335 | return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 336 | |
| 337 | case Type::Complex: { |
| 338 | // Complex types have the same alignment as their elements, but twice the |
| 339 | // size. |
| 340 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 341 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 342 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 343 | Align = EltInfo.second; |
| 344 | break; |
| 345 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 346 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 347 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 348 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 349 | Width = Layout.getSize(); |
| 350 | Align = Layout.getAlignment(); |
| 351 | break; |
| 352 | } |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 353 | case Type::Tagged: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 354 | const TagType *TT = cast<TagType>(T); |
| 355 | |
| 356 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 357 | Width = 1; |
| 358 | Align = 1; |
| 359 | break; |
| 360 | } |
| 361 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 362 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 363 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 364 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 365 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 366 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 367 | Width = Layout.getSize(); |
| 368 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 369 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 371 | } |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 372 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 373 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 374 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 377 | /// LayoutField - Field layout. |
| 378 | void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 379 | bool IsUnion, unsigned StructPacking, |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 380 | ASTContext &Context) { |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 381 | unsigned FieldPacking = StructPacking; |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 382 | uint64_t FieldOffset = IsUnion ? 0 : Size; |
| 383 | uint64_t FieldSize; |
| 384 | unsigned FieldAlign; |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 385 | |
| 386 | // FIXME: Should this override struct packing? Probably we want to |
| 387 | // take the minimum? |
| 388 | if (const PackedAttr *PA = FD->getAttr<PackedAttr>()) |
| 389 | FieldPacking = PA->getAlignment(); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 390 | |
| 391 | if (const Expr *BitWidthExpr = FD->getBitWidth()) { |
| 392 | // TODO: Need to check this algorithm on other targets! |
| 393 | // (tested on Linux-X86) |
Daniel Dunbar | 32442bb | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 394 | FieldSize = |
| 395 | BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue(); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 396 | |
| 397 | std::pair<uint64_t, unsigned> FieldInfo = |
| 398 | Context.getTypeInfo(FD->getType()); |
| 399 | uint64_t TypeSize = FieldInfo.first; |
| 400 | |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 401 | // Determine the alignment of this bitfield. The packing |
| 402 | // attributes define a maximum and the alignment attribute defines |
| 403 | // a minimum. |
| 404 | // FIXME: What is the right behavior when the specified alignment |
| 405 | // is smaller than the specified packing? |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 406 | FieldAlign = FieldInfo.second; |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 407 | if (FieldPacking) |
| 408 | FieldAlign = std::min(FieldAlign, FieldPacking); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 409 | if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) |
| 410 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
| 411 | |
| 412 | // Check if we need to add padding to give the field the correct |
| 413 | // alignment. |
| 414 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
| 415 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 416 | |
| 417 | // Padding members don't affect overall alignment |
| 418 | if (!FD->getIdentifier()) |
| 419 | FieldAlign = 1; |
| 420 | } else { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 421 | if (FD->getType()->isIncompleteArrayType()) { |
| 422 | // This is a flexible array member; we can't directly |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 423 | // query getTypeInfo about these, so we figure it out here. |
| 424 | // Flexible array members don't have any size, but they |
| 425 | // have to be aligned appropriately for their element type. |
| 426 | FieldSize = 0; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 427 | const ArrayType* ATy = Context.getAsArrayType(FD->getType()); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 428 | FieldAlign = Context.getTypeAlign(ATy->getElementType()); |
| 429 | } else { |
| 430 | std::pair<uint64_t, unsigned> FieldInfo = |
| 431 | Context.getTypeInfo(FD->getType()); |
| 432 | FieldSize = FieldInfo.first; |
| 433 | FieldAlign = FieldInfo.second; |
| 434 | } |
| 435 | |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 436 | // Determine the alignment of this bitfield. The packing |
| 437 | // attributes define a maximum and the alignment attribute defines |
| 438 | // a minimum. Additionally, the packing alignment must be at least |
| 439 | // a byte for non-bitfields. |
| 440 | // |
| 441 | // FIXME: What is the right behavior when the specified alignment |
| 442 | // is smaller than the specified packing? |
| 443 | if (FieldPacking) |
| 444 | FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking)); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 445 | if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) |
| 446 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
| 447 | |
| 448 | // Round up the current record size to the field's alignment boundary. |
| 449 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 450 | } |
| 451 | |
| 452 | // Place this field at the current location. |
| 453 | FieldOffsets[FieldNo] = FieldOffset; |
| 454 | |
| 455 | // Reserve space for this field. |
| 456 | if (IsUnion) { |
| 457 | Size = std::max(Size, FieldSize); |
| 458 | } else { |
| 459 | Size = FieldOffset + FieldSize; |
| 460 | } |
| 461 | |
| 462 | // Remember max struct/class alignment. |
| 463 | Alignment = std::max(Alignment, FieldAlign); |
| 464 | } |
| 465 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 467 | /// getASTObjcInterfaceLayout - Get or compute information about the layout of |
| 468 | /// the specified Objective C, which indicates its size and ivar |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 469 | /// position information. |
| 470 | const ASTRecordLayout & |
| 471 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 472 | // Look up this layout, if already laid out, return what we have. |
| 473 | const ASTRecordLayout *&Entry = ASTObjCInterfaces[D]; |
| 474 | if (Entry) return *Entry; |
| 475 | |
| 476 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 477 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
Devang Patel | 6a5a34c | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 478 | ASTRecordLayout *NewEntry = NULL; |
| 479 | unsigned FieldCount = D->ivar_size(); |
| 480 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
| 481 | FieldCount++; |
| 482 | const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD); |
| 483 | unsigned Alignment = SL.getAlignment(); |
| 484 | uint64_t Size = SL.getSize(); |
| 485 | NewEntry = new ASTRecordLayout(Size, Alignment); |
| 486 | NewEntry->InitializeLayout(FieldCount); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 487 | // Super class is at the beginning of the layout. |
| 488 | NewEntry->SetFieldOffset(0, 0); |
Devang Patel | 6a5a34c | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 489 | } else { |
| 490 | NewEntry = new ASTRecordLayout(); |
| 491 | NewEntry->InitializeLayout(FieldCount); |
| 492 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 493 | Entry = NewEntry; |
| 494 | |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 495 | unsigned StructPacking = 0; |
| 496 | if (const PackedAttr *PA = D->getAttr<PackedAttr>()) |
| 497 | StructPacking = PA->getAlignment(); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 498 | |
| 499 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 500 | NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), |
| 501 | AA->getAlignment())); |
| 502 | |
| 503 | // Layout each ivar sequentially. |
| 504 | unsigned i = 0; |
| 505 | for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(), |
| 506 | IVE = D->ivar_end(); IVI != IVE; ++IVI) { |
| 507 | const ObjCIvarDecl* Ivar = (*IVI); |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 508 | NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | // Finally, round the size of the total struct up to the alignment of the |
| 512 | // struct itself. |
| 513 | NewEntry->FinalizeLayout(); |
| 514 | return *NewEntry; |
| 515 | } |
| 516 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 517 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 518 | /// specified record (struct/union/class), which indicates its size and field |
| 519 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 520 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 521 | D = D->getDefinition(*this); |
| 522 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 524 | // Look up this layout, if already laid out, return what we have. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 525 | const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 526 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 527 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 528 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 529 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
| 530 | ASTRecordLayout *NewEntry = new ASTRecordLayout(); |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 531 | Entry = NewEntry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 532 | |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 533 | NewEntry->InitializeLayout(D->getNumMembers()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 534 | bool IsUnion = D->isUnion(); |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 535 | |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 536 | unsigned StructPacking = 0; |
| 537 | if (const PackedAttr *PA = D->getAttr<PackedAttr>()) |
| 538 | StructPacking = PA->getAlignment(); |
| 539 | |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 540 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 541 | NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), |
| 542 | AA->getAlignment())); |
Anders Carlsson | 8af226a | 2008-02-18 07:13:09 +0000 | [diff] [blame] | 543 | |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 544 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 545 | // the future, this will need to be tweakable by targets. |
| 546 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 547 | const FieldDecl *FD = D->getMember(i); |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 548 | NewEntry->LayoutField(FD, i, IsUnion, StructPacking, *this); |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 549 | } |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 550 | |
| 551 | // Finally, round the size of the total struct up to the alignment of the |
| 552 | // struct itself. |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 553 | NewEntry->FinalizeLayout(); |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 554 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 557 | //===----------------------------------------------------------------------===// |
| 558 | // Type creation/memoization methods |
| 559 | //===----------------------------------------------------------------------===// |
| 560 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 561 | QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 562 | QualType CanT = getCanonicalType(T); |
| 563 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 564 | return T; |
| 565 | |
| 566 | // Type's cannot have multiple ASQuals, therefore we know we only have to deal |
| 567 | // with CVR qualifiers from here on out. |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 568 | assert(CanT.getAddressSpace() == 0 && |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 569 | "Type is already address space qualified"); |
| 570 | |
| 571 | // Check if we've already instantiated an address space qual'd type of this |
| 572 | // type. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 573 | llvm::FoldingSetNodeID ID; |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 574 | ASQualType::Profile(ID, T.getTypePtr(), AddressSpace); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 575 | void *InsertPos = 0; |
| 576 | if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 577 | return QualType(ASQy, 0); |
| 578 | |
| 579 | // If the base type isn't canonical, this won't be a canonical type either, |
| 580 | // so fill in the canonical type field. |
| 581 | QualType Canonical; |
| 582 | if (!T->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 583 | Canonical = getASQualType(CanT, AddressSpace); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 584 | |
| 585 | // Get the new insert position for the node we care about. |
| 586 | ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 587 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 588 | } |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 589 | ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 590 | ASQualTypes.InsertNode(New, InsertPos); |
| 591 | Types.push_back(New); |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 592 | return QualType(New, T.getCVRQualifiers()); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 595 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 596 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 597 | /// number with the specified element type. |
| 598 | QualType ASTContext::getComplexType(QualType T) { |
| 599 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 600 | // structure. |
| 601 | llvm::FoldingSetNodeID ID; |
| 602 | ComplexType::Profile(ID, T); |
| 603 | |
| 604 | void *InsertPos = 0; |
| 605 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 606 | return QualType(CT, 0); |
| 607 | |
| 608 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 609 | // so fill in the canonical type field. |
| 610 | QualType Canonical; |
| 611 | if (!T->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 612 | Canonical = getComplexType(getCanonicalType(T)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 613 | |
| 614 | // Get the new insert position for the node we care about. |
| 615 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 616 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 617 | } |
| 618 | ComplexType *New = new ComplexType(T, Canonical); |
| 619 | Types.push_back(New); |
| 620 | ComplexTypes.InsertNode(New, InsertPos); |
| 621 | return QualType(New, 0); |
| 622 | } |
| 623 | |
| 624 | |
| 625 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 626 | /// the specified type. |
| 627 | QualType ASTContext::getPointerType(QualType T) { |
| 628 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 629 | // structure. |
| 630 | llvm::FoldingSetNodeID ID; |
| 631 | PointerType::Profile(ID, T); |
| 632 | |
| 633 | void *InsertPos = 0; |
| 634 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 635 | return QualType(PT, 0); |
| 636 | |
| 637 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 638 | // so fill in the canonical type field. |
| 639 | QualType Canonical; |
| 640 | if (!T->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 641 | Canonical = getPointerType(getCanonicalType(T)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 642 | |
| 643 | // Get the new insert position for the node we care about. |
| 644 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 645 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 646 | } |
| 647 | PointerType *New = new PointerType(T, Canonical); |
| 648 | Types.push_back(New); |
| 649 | PointerTypes.InsertNode(New, InsertPos); |
| 650 | return QualType(New, 0); |
| 651 | } |
| 652 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 653 | /// getBlockPointerType - Return the uniqued reference to the type for |
| 654 | /// a pointer to the specified block. |
| 655 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 656 | assert(T->isFunctionType() && "block of function types only"); |
| 657 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 658 | // structure. |
| 659 | llvm::FoldingSetNodeID ID; |
| 660 | BlockPointerType::Profile(ID, T); |
| 661 | |
| 662 | void *InsertPos = 0; |
| 663 | if (BlockPointerType *PT = |
| 664 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 665 | return QualType(PT, 0); |
| 666 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 667 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 668 | // type either so fill in the canonical type field. |
| 669 | QualType Canonical; |
| 670 | if (!T->isCanonical()) { |
| 671 | Canonical = getBlockPointerType(getCanonicalType(T)); |
| 672 | |
| 673 | // Get the new insert position for the node we care about. |
| 674 | BlockPointerType *NewIP = |
| 675 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 676 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 677 | } |
| 678 | BlockPointerType *New = new BlockPointerType(T, Canonical); |
| 679 | Types.push_back(New); |
| 680 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 681 | return QualType(New, 0); |
| 682 | } |
| 683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 685 | /// to the specified type. |
| 686 | QualType ASTContext::getReferenceType(QualType T) { |
| 687 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 688 | // structure. |
| 689 | llvm::FoldingSetNodeID ID; |
| 690 | ReferenceType::Profile(ID, T); |
| 691 | |
| 692 | void *InsertPos = 0; |
| 693 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 694 | return QualType(RT, 0); |
| 695 | |
| 696 | // If the referencee type isn't canonical, this won't be a canonical type |
| 697 | // either, so fill in the canonical type field. |
| 698 | QualType Canonical; |
| 699 | if (!T->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 700 | Canonical = getReferenceType(getCanonicalType(T)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 701 | |
| 702 | // Get the new insert position for the node we care about. |
| 703 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 704 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 708 | Types.push_back(New); |
| 709 | ReferenceTypes.InsertNode(New, InsertPos); |
| 710 | return QualType(New, 0); |
| 711 | } |
| 712 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 713 | /// getConstantArrayType - Return the unique reference to the type for an |
| 714 | /// array of the specified element type. |
| 715 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 716 | const llvm::APInt &ArySize, |
| 717 | ArrayType::ArraySizeModifier ASM, |
| 718 | unsigned EltTypeQuals) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | llvm::FoldingSetNodeID ID; |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 720 | ConstantArrayType::Profile(ID, EltTy, ArySize); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 721 | |
| 722 | void *InsertPos = 0; |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 723 | if (ConstantArrayType *ATP = |
| 724 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 725 | return QualType(ATP, 0); |
| 726 | |
| 727 | // If the element type isn't canonical, this won't be a canonical type either, |
| 728 | // so fill in the canonical type field. |
| 729 | QualType Canonical; |
| 730 | if (!EltTy->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 731 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 732 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 733 | // Get the new insert position for the node we care about. |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 734 | ConstantArrayType *NewIP = |
| 735 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 736 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 739 | ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize, |
| 740 | ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 741 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 742 | Types.push_back(New); |
| 743 | return QualType(New, 0); |
| 744 | } |
| 745 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 746 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 747 | /// variable array of the specified element type. |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 748 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
| 749 | ArrayType::ArraySizeModifier ASM, |
| 750 | unsigned EltTypeQuals) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 751 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 752 | // that have an expression provided for their size. |
| 753 | |
| 754 | VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 755 | ASM, EltTypeQuals); |
| 756 | |
| 757 | VariableArrayTypes.push_back(New); |
| 758 | Types.push_back(New); |
| 759 | return QualType(New, 0); |
| 760 | } |
| 761 | |
| 762 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 763 | ArrayType::ArraySizeModifier ASM, |
| 764 | unsigned EltTypeQuals) { |
| 765 | llvm::FoldingSetNodeID ID; |
| 766 | IncompleteArrayType::Profile(ID, EltTy); |
| 767 | |
| 768 | void *InsertPos = 0; |
| 769 | if (IncompleteArrayType *ATP = |
| 770 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 771 | return QualType(ATP, 0); |
| 772 | |
| 773 | // If the element type isn't canonical, this won't be a canonical type |
| 774 | // either, so fill in the canonical type field. |
| 775 | QualType Canonical; |
| 776 | |
| 777 | if (!EltTy->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 778 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 779 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 780 | |
| 781 | // Get the new insert position for the node we care about. |
| 782 | IncompleteArrayType *NewIP = |
| 783 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 784 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 785 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 786 | |
| 787 | IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical, |
| 788 | ASM, EltTypeQuals); |
| 789 | |
| 790 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 791 | Types.push_back(New); |
| 792 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 795 | /// getVectorType - Return the unique reference to a vector type of |
| 796 | /// the specified element type and size. VectorType must be a built-in type. |
| 797 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 798 | BuiltinType *baseType; |
| 799 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 800 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 801 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 802 | |
| 803 | // Check if we've already instantiated a vector of this type. |
| 804 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 805 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 806 | void *InsertPos = 0; |
| 807 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 808 | return QualType(VTP, 0); |
| 809 | |
| 810 | // If the element type isn't canonical, this won't be a canonical type either, |
| 811 | // so fill in the canonical type field. |
| 812 | QualType Canonical; |
| 813 | if (!vecType->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 814 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 815 | |
| 816 | // Get the new insert position for the node we care about. |
| 817 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 818 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 819 | } |
| 820 | VectorType *New = new VectorType(vecType, NumElts, Canonical); |
| 821 | VectorTypes.InsertNode(New, InsertPos); |
| 822 | Types.push_back(New); |
| 823 | return QualType(New, 0); |
| 824 | } |
| 825 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 826 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 827 | /// the specified element type and size. VectorType must be a built-in type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 828 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 829 | BuiltinType *baseType; |
| 830 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 831 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 832 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 833 | |
| 834 | // Check if we've already instantiated a vector of this type. |
| 835 | llvm::FoldingSetNodeID ID; |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 836 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 837 | void *InsertPos = 0; |
| 838 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 839 | return QualType(VTP, 0); |
| 840 | |
| 841 | // If the element type isn't canonical, this won't be a canonical type either, |
| 842 | // so fill in the canonical type field. |
| 843 | QualType Canonical; |
| 844 | if (!vecType->isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 845 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 846 | |
| 847 | // Get the new insert position for the node we care about. |
| 848 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 849 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 850 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 851 | ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 852 | VectorTypes.InsertNode(New, InsertPos); |
| 853 | Types.push_back(New); |
| 854 | return QualType(New, 0); |
| 855 | } |
| 856 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 857 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 858 | /// |
| 859 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 860 | // Unique functions, to guarantee there is only one function of a particular |
| 861 | // structure. |
| 862 | llvm::FoldingSetNodeID ID; |
| 863 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 864 | |
| 865 | void *InsertPos = 0; |
| 866 | if (FunctionTypeNoProto *FT = |
| 867 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 868 | return QualType(FT, 0); |
| 869 | |
| 870 | QualType Canonical; |
| 871 | if (!ResultTy->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 872 | Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 873 | |
| 874 | // Get the new insert position for the node we care about. |
| 875 | FunctionTypeNoProto *NewIP = |
| 876 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 877 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 881 | Types.push_back(New); |
Eli Friedman | 56cd7e3 | 2008-02-25 22:11:40 +0000 | [diff] [blame] | 882 | FunctionTypeNoProtos.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 883 | return QualType(New, 0); |
| 884 | } |
| 885 | |
| 886 | /// getFunctionType - Return a normal function type with a typed argument |
| 887 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 888 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 889 | unsigned NumArgs, bool isVariadic, |
| 890 | unsigned TypeQuals) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | // Unique functions, to guarantee there is only one function of a particular |
| 892 | // structure. |
| 893 | llvm::FoldingSetNodeID ID; |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 894 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
| 895 | TypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 896 | |
| 897 | void *InsertPos = 0; |
| 898 | if (FunctionTypeProto *FTP = |
| 899 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 900 | return QualType(FTP, 0); |
| 901 | |
| 902 | // Determine whether the type being created is already canonical or not. |
| 903 | bool isCanonical = ResultTy->isCanonical(); |
| 904 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 905 | if (!ArgArray[i]->isCanonical()) |
| 906 | isCanonical = false; |
| 907 | |
| 908 | // If this type isn't canonical, get the canonical version of it. |
| 909 | QualType Canonical; |
| 910 | if (!isCanonical) { |
| 911 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 912 | CanonicalArgs.reserve(NumArgs); |
| 913 | for (unsigned i = 0; i != NumArgs; ++i) |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 914 | CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 915 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 916 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 917 | &CanonicalArgs[0], NumArgs, |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 918 | isVariadic, TypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 919 | |
| 920 | // Get the new insert position for the node we care about. |
| 921 | FunctionTypeProto *NewIP = |
| 922 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 923 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | // FunctionTypeProto objects are not allocated with new because they have a |
| 927 | // variable size array (for parameter types) at the end of them. |
| 928 | FunctionTypeProto *FTP = |
| 929 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 930 | NumArgs*sizeof(QualType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 932 | TypeQuals, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 933 | Types.push_back(FTP); |
| 934 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 935 | return QualType(FTP, 0); |
| 936 | } |
| 937 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 938 | /// getTypeDeclType - Return the unique reference to the type for the |
| 939 | /// specified type declaration. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 940 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 941 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 942 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 943 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 944 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 945 | return getTypedefType(Typedef); |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 946 | else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 947 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 948 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 949 | if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 950 | Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl |
| 951 | : new CXXRecordType(CXXRecord); |
| 952 | } |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 953 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 954 | Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl |
| 955 | : new RecordType(Record); |
| 956 | } |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 957 | else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 958 | Decl->TypeForDecl = new EnumType(Enum); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 959 | else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 960 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 961 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 962 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 963 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 966 | /// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext |
| 967 | /// about which RecordDecl serves as the definition of a particular |
| 968 | /// struct/union/class. This will eventually be used by enums as well. |
| 969 | void ASTContext::setTagDefinition(TagDecl* D) { |
| 970 | assert (D->isDefinition()); |
| 971 | cast<TagType>(D->TypeForDecl)->decl = D; |
| 972 | } |
| 973 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | /// getTypedefType - Return the unique reference to the type for the |
| 975 | /// specified typename decl. |
| 976 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 977 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 978 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 979 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 980 | Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 981 | Types.push_back(Decl->TypeForDecl); |
| 982 | return QualType(Decl->TypeForDecl, 0); |
| 983 | } |
| 984 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 985 | /// getObjCInterfaceType - Return the unique reference to the type for the |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 986 | /// specified ObjC interface decl. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 987 | QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) { |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 988 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 989 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 990 | Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 991 | Types.push_back(Decl->TypeForDecl); |
| 992 | return QualType(Decl->TypeForDecl, 0); |
| 993 | } |
| 994 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 995 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 996 | /// alphabetically. |
| 997 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 998 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 999 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, |
| 1003 | unsigned &NumProtocols) { |
| 1004 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
| 1005 | |
| 1006 | // Sort protocols, keyed by name. |
| 1007 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 1008 | |
| 1009 | // Remove duplicates. |
| 1010 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 1011 | NumProtocols = ProtocolsEnd-Protocols; |
| 1012 | } |
| 1013 | |
| 1014 | |
Chris Lattner | 065f0d7 | 2008-04-07 04:44:08 +0000 | [diff] [blame] | 1015 | /// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for |
| 1016 | /// the given interface decl and the conforming protocol list. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1017 | QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, |
| 1018 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1019 | // Sort the protocol list alphabetically to canonicalize it. |
| 1020 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 1021 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1022 | llvm::FoldingSetNodeID ID; |
Chris Lattner | b048981 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 1023 | ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1024 | |
| 1025 | void *InsertPos = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1026 | if (ObjCQualifiedInterfaceType *QT = |
| 1027 | ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1028 | return QualType(QT, 0); |
| 1029 | |
| 1030 | // No Match; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1031 | ObjCQualifiedInterfaceType *QType = |
| 1032 | new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1033 | Types.push_back(QType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1034 | ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1035 | return QualType(QType, 0); |
| 1036 | } |
| 1037 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1038 | /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl |
| 1039 | /// and the conforming protocol list. |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 1040 | QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols, |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1041 | unsigned NumProtocols) { |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1042 | // Sort the protocol list alphabetically to canonicalize it. |
| 1043 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 1044 | |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1045 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1046 | ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1047 | |
| 1048 | void *InsertPos = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1049 | if (ObjCQualifiedIdType *QT = |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 1050 | ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1051 | return QualType(QT, 0); |
| 1052 | |
| 1053 | // No Match; |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 1054 | ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1055 | Types.push_back(QType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1056 | ObjCQualifiedIdTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1057 | return QualType(QType, 0); |
| 1058 | } |
| 1059 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1060 | /// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique |
| 1061 | /// TypeOfExpr AST's (since expression's are never shared). For example, |
| 1062 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 1063 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 1064 | /// on canonical type's (which are always unique). |
Steve Naroff | 8d1a3b8 | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 1065 | QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1066 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1067 | TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical); |
| 1068 | Types.push_back(toe); |
| 1069 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1072 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 1073 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 1074 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 1075 | /// an issue. This doesn't effect the type checker, since it operates |
| 1076 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1077 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1078 | QualType Canonical = getCanonicalType(tofType); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1079 | TypeOfType *tot = new TypeOfType(tofType, Canonical); |
| 1080 | Types.push_back(tot); |
| 1081 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1084 | /// getTagDeclType - Return the unique reference to the type for the |
| 1085 | /// specified TagDecl (struct/union/class/enum) decl. |
| 1086 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 1087 | assert (Decl); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1088 | return getTypeDeclType(Decl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 1092 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 1093 | /// needs to agree with the definition in <stddef.h>. |
| 1094 | QualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1095 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 1098 | /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the |
Eli Friedman | fd888a5 | 2008-02-12 08:29:21 +0000 | [diff] [blame] | 1099 | /// width of characters in wide strings, The value is target dependent and |
| 1100 | /// needs to agree with the definition in <stddef.h>. |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 1101 | QualType ASTContext::getWCharType() const { |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1102 | if (LangOpts.CPlusPlus) |
| 1103 | return WCharTy; |
| 1104 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1105 | // FIXME: In C, shouldn't WCharTy just be a typedef of the target's |
| 1106 | // wide-character type? |
| 1107 | return getFromTargetType(Target.getWCharType()); |
Eli Friedman | fd888a5 | 2008-02-12 08:29:21 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1110 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 1111 | /// Used when in C++, as a GCC extension. |
| 1112 | QualType ASTContext::getSignedWCharType() const { |
| 1113 | // FIXME: derive from "Target" ? |
| 1114 | return WCharTy; |
| 1115 | } |
| 1116 | |
| 1117 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 1118 | /// Used when in C++, as a GCC extension. |
| 1119 | QualType ASTContext::getUnsignedWCharType() const { |
| 1120 | // FIXME: derive from "Target" ? |
| 1121 | return UnsignedIntTy; |
| 1122 | } |
| 1123 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 1124 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 1125 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 1126 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1127 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1130 | //===----------------------------------------------------------------------===// |
| 1131 | // Type Operators |
| 1132 | //===----------------------------------------------------------------------===// |
| 1133 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1134 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 1135 | /// the specified potentially non-canonical type. The non-canonical version |
| 1136 | /// of a type may have many "decorated" versions of types. Decorators can |
| 1137 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 1138 | /// to be free of any of these, allowing two canonical types to be compared |
| 1139 | /// for exact equality with a simple pointer comparison. |
| 1140 | QualType ASTContext::getCanonicalType(QualType T) { |
| 1141 | QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1142 | |
| 1143 | // If the result has type qualifiers, make sure to canonicalize them as well. |
| 1144 | unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); |
| 1145 | if (TypeQuals == 0) return CanType; |
| 1146 | |
| 1147 | // If the type qualifiers are on an array type, get the canonical type of the |
| 1148 | // array with the qualifiers applied to the element type. |
| 1149 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 1150 | if (!AT) |
| 1151 | return CanType.getQualifiedType(TypeQuals); |
| 1152 | |
| 1153 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1154 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1155 | QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); |
| 1156 | NewEltTy = getCanonicalType(NewEltTy); |
| 1157 | |
| 1158 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1159 | return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(), |
| 1160 | CAT->getIndexTypeQualifier()); |
| 1161 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
| 1162 | return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
| 1163 | IAT->getIndexTypeQualifier()); |
| 1164 | |
| 1165 | // FIXME: What is the ownership of size expressions in VLAs? |
| 1166 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
| 1167 | return getVariableArrayType(NewEltTy, VAT->getSizeExpr(), |
| 1168 | VAT->getSizeModifier(), |
| 1169 | VAT->getIndexTypeQualifier()); |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 1174 | // Handle the non-qualified case efficiently. |
| 1175 | if (T.getCVRQualifiers() == 0) { |
| 1176 | // Handle the common positive case fast. |
| 1177 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 1178 | return AT; |
| 1179 | } |
| 1180 | |
| 1181 | // Handle the common negative case fast, ignoring CVR qualifiers. |
| 1182 | QualType CType = T->getCanonicalTypeInternal(); |
| 1183 | |
| 1184 | // Make sure to look through type qualifiers (like ASQuals) for the negative |
| 1185 | // test. |
| 1186 | if (!isa<ArrayType>(CType) && |
| 1187 | !isa<ArrayType>(CType.getUnqualifiedType())) |
| 1188 | return 0; |
| 1189 | |
| 1190 | // Apply any CVR qualifiers from the array type to the element type. This |
| 1191 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 1192 | // any type qualifiers, the element type is so qualified, not the array type." |
| 1193 | |
| 1194 | // If we get here, we either have type qualifiers on the type, or we have |
| 1195 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
| 1196 | // we must propagate them down into the elemeng type. |
| 1197 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 1198 | unsigned AddrSpace = 0; |
| 1199 | Type *Ty = T.getTypePtr(); |
| 1200 | |
| 1201 | // Rip through ASQualType's and typedefs to get to a concrete type. |
| 1202 | while (1) { |
| 1203 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) { |
| 1204 | AddrSpace = ASQT->getAddressSpace(); |
| 1205 | Ty = ASQT->getBaseType(); |
| 1206 | } else { |
| 1207 | T = Ty->getDesugaredType(); |
| 1208 | if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) |
| 1209 | break; |
| 1210 | CVRQuals |= T.getCVRQualifiers(); |
| 1211 | Ty = T.getTypePtr(); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | // If we have a simple case, just return now. |
| 1216 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
| 1217 | if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) |
| 1218 | return ATy; |
| 1219 | |
| 1220 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 1221 | // qualifiers into the array element type and return a new array type. |
| 1222 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1223 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1224 | QualType NewEltTy = ATy->getElementType(); |
| 1225 | if (AddrSpace) |
| 1226 | NewEltTy = getASQualType(NewEltTy, AddrSpace); |
| 1227 | NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); |
| 1228 | |
| 1229 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 1230 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 1231 | CAT->getSizeModifier(), |
| 1232 | CAT->getIndexTypeQualifier())); |
| 1233 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 1234 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 1235 | IAT->getSizeModifier(), |
| 1236 | IAT->getIndexTypeQualifier())); |
| 1237 | |
| 1238 | // FIXME: What is the ownership of size expressions in VLAs? |
| 1239 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
| 1240 | return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(), |
| 1241 | VAT->getSizeModifier(), |
| 1242 | VAT->getIndexTypeQualifier())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1246 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 1247 | /// specified array type to a pointer. This operation is non-trivial when |
| 1248 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 1249 | /// this returns a pointer to a properly qualified element of the array. |
| 1250 | /// |
| 1251 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 1252 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1253 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 1254 | // typedefs in the element type of the array. This also handles propagation |
| 1255 | // of type qualifiers from the array type into the element type if present |
| 1256 | // (C99 6.7.3p8). |
| 1257 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 1258 | assert(PrettyArrayType && "Not an array type!"); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1260 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1261 | |
| 1262 | // int x[restrict 4] -> int *restrict |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1263 | return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | /// getFloatingRank - Return a relative rank for floating point types. |
| 1267 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1268 | static FloatingRank getFloatingRank(QualType T) { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1269 | if (const ComplexType *CT = T->getAsComplexType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1270 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1271 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1272 | switch (T->getAsBuiltinType()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1273 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1274 | case BuiltinType::Float: return FloatRank; |
| 1275 | case BuiltinType::Double: return DoubleRank; |
| 1276 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 1277 | } |
| 1278 | } |
| 1279 | |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 1280 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 1281 | /// point or a complex type (based on typeDomain/typeSize). |
| 1282 | /// 'typeDomain' is a real floating point or complex type. |
| 1283 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 1284 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 1285 | QualType Domain) const { |
| 1286 | FloatingRank EltRank = getFloatingRank(Size); |
| 1287 | if (Domain->isComplexType()) { |
| 1288 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 1289 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1290 | case FloatRank: return FloatComplexTy; |
| 1291 | case DoubleRank: return DoubleComplexTy; |
| 1292 | case LongDoubleRank: return LongDoubleComplexTy; |
| 1293 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1294 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 1295 | |
| 1296 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 1297 | switch (EltRank) { |
| 1298 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 1299 | case FloatRank: return FloatTy; |
| 1300 | case DoubleRank: return DoubleTy; |
| 1301 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1302 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1305 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 1306 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 1307 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 1308 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1309 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 1310 | FloatingRank LHSR = getFloatingRank(LHS); |
| 1311 | FloatingRank RHSR = getFloatingRank(RHS); |
| 1312 | |
| 1313 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1314 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1315 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1316 | return 1; |
| 1317 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1320 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 1321 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 1322 | /// or if it is not canonicalized. |
| 1323 | static unsigned getIntegerRank(Type *T) { |
| 1324 | assert(T->isCanonical() && "T should be canonicalized"); |
| 1325 | if (isa<EnumType>(T)) |
| 1326 | return 4; |
| 1327 | |
| 1328 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1329 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 1330 | case BuiltinType::Bool: |
| 1331 | return 1; |
| 1332 | case BuiltinType::Char_S: |
| 1333 | case BuiltinType::Char_U: |
| 1334 | case BuiltinType::SChar: |
| 1335 | case BuiltinType::UChar: |
| 1336 | return 2; |
| 1337 | case BuiltinType::Short: |
| 1338 | case BuiltinType::UShort: |
| 1339 | return 3; |
| 1340 | case BuiltinType::Int: |
| 1341 | case BuiltinType::UInt: |
| 1342 | return 4; |
| 1343 | case BuiltinType::Long: |
| 1344 | case BuiltinType::ULong: |
| 1345 | return 5; |
| 1346 | case BuiltinType::LongLong: |
| 1347 | case BuiltinType::ULongLong: |
| 1348 | return 6; |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1352 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
| 1353 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 1354 | /// LHS < RHS, return -1. |
| 1355 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1356 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 1357 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1358 | if (LHSC == RHSC) return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1359 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1360 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 1361 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1363 | unsigned LHSRank = getIntegerRank(LHSC); |
| 1364 | unsigned RHSRank = getIntegerRank(RHSC); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1365 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1366 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 1367 | if (LHSRank == RHSRank) return 0; |
| 1368 | return LHSRank > RHSRank ? 1 : -1; |
| 1369 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1371 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 1372 | if (LHSUnsigned) { |
| 1373 | // If the unsigned [LHS] type is larger, return it. |
| 1374 | if (LHSRank >= RHSRank) |
| 1375 | return 1; |
| 1376 | |
| 1377 | // If the signed type can represent all values of the unsigned type, it |
| 1378 | // wins. Because we are dealing with 2's complement and types that are |
| 1379 | // powers of two larger than each other, this is always safe. |
| 1380 | return -1; |
| 1381 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1383 | // If the unsigned [RHS] type is larger, return it. |
| 1384 | if (RHSRank >= LHSRank) |
| 1385 | return -1; |
| 1386 | |
| 1387 | // If the signed type can represent all values of the unsigned type, it |
| 1388 | // wins. Because we are dealing with 2's complement and types that are |
| 1389 | // powers of two larger than each other, this is always safe. |
| 1390 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1391 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1392 | |
| 1393 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 1394 | QualType ASTContext::getCFConstantStringType() { |
| 1395 | if (!CFConstantStringTypeDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 1396 | CFConstantStringTypeDecl = |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1397 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1398 | &Idents.get("NSConstantString")); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1399 | QualType FieldTypes[4]; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1400 | |
| 1401 | // const int *isa; |
| 1402 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1403 | // int flags; |
| 1404 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1405 | // const char *str; |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1406 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1407 | // long length; |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1408 | FieldTypes[3] = LongTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1409 | // Create fields |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1410 | FieldDecl *FieldDecls[4]; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1411 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 1412 | for (unsigned i = 0; i < 4; ++i) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1413 | FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 1414 | FieldTypes[i]); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1415 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1416 | CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 1420 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1421 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 1422 | QualType ASTContext::getObjCFastEnumerationStateType() |
| 1423 | { |
| 1424 | if (!ObjCFastEnumerationStateTypeDecl) { |
| 1425 | QualType FieldTypes[] = { |
| 1426 | UnsignedLongTy, |
| 1427 | getPointerType(ObjCIdType), |
| 1428 | getPointerType(UnsignedLongTy), |
| 1429 | getConstantArrayType(UnsignedLongTy, |
| 1430 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 1431 | }; |
| 1432 | |
| 1433 | FieldDecl *FieldDecls[4]; |
| 1434 | for (size_t i = 0; i < 4; ++i) |
| 1435 | FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0, |
| 1436 | FieldTypes[i]); |
| 1437 | |
| 1438 | ObjCFastEnumerationStateTypeDecl = |
| 1439 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1440 | &Idents.get("__objcFastEnumerationState")); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 1441 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1442 | ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 1446 | } |
| 1447 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1448 | // This returns true if a type has been typedefed to BOOL: |
| 1449 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 1450 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1451 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1452 | return !strcmp(TT->getDecl()->getIdentifierName(), "BOOL"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1453 | |
| 1454 | return false; |
| 1455 | } |
| 1456 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1457 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1458 | /// purpose. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1459 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1460 | uint64_t sz = getTypeSize(type); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Make all integer and enum types at least as large as an int |
| 1463 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1464 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1465 | // Treat arrays as pointers, since that's how they're passed in. |
| 1466 | else if (type->isArrayType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1467 | sz = getTypeSize(VoidPtrTy); |
| 1468 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1471 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1472 | /// declaration. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1473 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 1474 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1475 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1476 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1477 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1478 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1479 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1480 | // Compute size of all parameters. |
| 1481 | // Start with computing size of a pointer in number of bytes. |
| 1482 | // FIXME: There might(should) be a better way of doing this computation! |
| 1483 | SourceLocation Loc; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1484 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1485 | // The first two arguments (self and _cmd) are pointers; account for |
| 1486 | // their size. |
| 1487 | int ParmOffset = 2 * PtrSize; |
| 1488 | int NumOfParams = Decl->getNumParams(); |
| 1489 | for (int i = 0; i < NumOfParams; i++) { |
| 1490 | QualType PType = Decl->getParamDecl(i)->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1491 | int sz = getObjCEncodingTypeSize (PType); |
| 1492 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1493 | ParmOffset += sz; |
| 1494 | } |
| 1495 | S += llvm::utostr(ParmOffset); |
| 1496 | S += "@0:"; |
| 1497 | S += llvm::utostr(PtrSize); |
| 1498 | |
| 1499 | // Argument types. |
| 1500 | ParmOffset = 2 * PtrSize; |
| 1501 | for (int i = 0; i < NumOfParams; i++) { |
| 1502 | QualType PType = Decl->getParamDecl(i)->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1503 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1504 | // 'in', 'inout', etc. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1505 | getObjCEncodingForTypeQualifier( |
| 1506 | Decl->getParamDecl(i)->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1507 | getObjCEncodingForType(PType, S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1508 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1509 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1513 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
| 1514 | /// method declaration. If non-NULL, Container must be either an |
| 1515 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 1516 | /// NULL when getting encodings for protocol properties. |
| 1517 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 1518 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 1519 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1520 | // Collect information from the property implementation decl(s). |
| 1521 | bool Dynamic = false; |
| 1522 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 1523 | |
| 1524 | // FIXME: Duplicated code due to poor abstraction. |
| 1525 | if (Container) { |
| 1526 | if (const ObjCCategoryImplDecl *CID = |
| 1527 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 1528 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1529 | i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) { |
| 1530 | ObjCPropertyImplDecl *PID = *i; |
| 1531 | if (PID->getPropertyDecl() == PD) { |
| 1532 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 1533 | Dynamic = true; |
| 1534 | } else { |
| 1535 | SynthesizePID = PID; |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1540 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1541 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1542 | i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) { |
| 1543 | ObjCPropertyImplDecl *PID = *i; |
| 1544 | if (PID->getPropertyDecl() == PD) { |
| 1545 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 1546 | Dynamic = true; |
| 1547 | } else { |
| 1548 | SynthesizePID = PID; |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | // FIXME: This is not very efficient. |
| 1556 | S = "T"; |
| 1557 | |
| 1558 | // Encode result type. |
| 1559 | // FIXME: GCC uses a generating_property_type_encoding mode during |
| 1560 | // this part. Investigate. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1561 | getObjCEncodingForType(PD->getType(), S); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1562 | |
| 1563 | if (PD->isReadOnly()) { |
| 1564 | S += ",R"; |
| 1565 | } else { |
| 1566 | switch (PD->getSetterKind()) { |
| 1567 | case ObjCPropertyDecl::Assign: break; |
| 1568 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
| 1569 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | // It really isn't clear at all what this means, since properties |
| 1574 | // are "dynamic by default". |
| 1575 | if (Dynamic) |
| 1576 | S += ",D"; |
| 1577 | |
| 1578 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 1579 | S += ",G"; |
| 1580 | S += PD->getGetterName().getName(); |
| 1581 | } |
| 1582 | |
| 1583 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 1584 | S += ",S"; |
| 1585 | S += PD->getSetterName().getName(); |
| 1586 | } |
| 1587 | |
| 1588 | if (SynthesizePID) { |
| 1589 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 1590 | S += ",V"; |
| 1591 | S += OID->getName(); |
| 1592 | } |
| 1593 | |
| 1594 | // FIXME: OBJCGC: weak & strong |
| 1595 | } |
| 1596 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1597 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1598 | bool NameFields) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1599 | // We follow the behavior of gcc, expanding structures which are |
| 1600 | // directly pointed to, and expanding embedded structures. Note that |
| 1601 | // these rules are sufficient to prevent recursive encoding of the |
| 1602 | // same type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1603 | getObjCEncodingForTypeImpl(T, S, true, true, NameFields); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 1607 | bool ExpandPointedToStructures, |
| 1608 | bool ExpandStructures, |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1609 | bool NameFields) const { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1610 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1611 | char encoding; |
| 1612 | switch (BT->getKind()) { |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1613 | default: assert(0 && "Unhandled builtin type kind"); |
| 1614 | case BuiltinType::Void: encoding = 'v'; break; |
| 1615 | case BuiltinType::Bool: encoding = 'B'; break; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1616 | case BuiltinType::Char_U: |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1617 | case BuiltinType::UChar: encoding = 'C'; break; |
| 1618 | case BuiltinType::UShort: encoding = 'S'; break; |
| 1619 | case BuiltinType::UInt: encoding = 'I'; break; |
| 1620 | case BuiltinType::ULong: encoding = 'L'; break; |
| 1621 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1622 | case BuiltinType::Char_S: |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1623 | case BuiltinType::SChar: encoding = 'c'; break; |
| 1624 | case BuiltinType::Short: encoding = 's'; break; |
| 1625 | case BuiltinType::Int: encoding = 'i'; break; |
| 1626 | case BuiltinType::Long: encoding = 'l'; break; |
| 1627 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 1628 | case BuiltinType::Float: encoding = 'f'; break; |
| 1629 | case BuiltinType::Double: encoding = 'd'; break; |
| 1630 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | S += encoding; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1634 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1635 | else if (T->isObjCQualifiedIdType()) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1636 | // Treat id<P...> same as 'id' for encoding purposes. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1637 | return getObjCEncodingForTypeImpl(getObjCIdType(), S, |
| 1638 | ExpandPointedToStructures, |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1639 | ExpandStructures, NameFields); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1640 | } |
| 1641 | else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1642 | QualType PointeeTy = PT->getPointeeType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1643 | if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) { |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1644 | S += '@'; |
| 1645 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1646 | } else if (isObjCClassType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1647 | S += '#'; |
| 1648 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1649 | } else if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1650 | S += ':'; |
| 1651 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1652 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1653 | |
| 1654 | if (PointeeTy->isCharType()) { |
| 1655 | // char pointer types should be encoded as '*' unless it is a |
| 1656 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1657 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1658 | S += '*'; |
| 1659 | return; |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | S += '^'; |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1664 | getObjCEncodingForTypeImpl(PT->getPointeeType(), S, |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1665 | false, ExpandPointedToStructures, |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1666 | NameFields); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1667 | } else if (const ArrayType *AT = |
| 1668 | // Ignore type qualifiers etc. |
| 1669 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1670 | S += '['; |
| 1671 | |
| 1672 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1673 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 1674 | else |
| 1675 | assert(0 && "Unhandled array type!"); |
| 1676 | |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1677 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1678 | false, ExpandStructures, NameFields); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1679 | S += ']'; |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 1680 | } else if (T->getAsFunctionType()) { |
| 1681 | S += '?'; |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1682 | } else if (const RecordType *RTy = T->getAsRecordType()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1683 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1684 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 1685 | // Anonymous structures print as '?' |
| 1686 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 1687 | S += II->getName(); |
| 1688 | } else { |
| 1689 | S += '?'; |
| 1690 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1691 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1692 | S += '='; |
| 1693 | for (int i = 0; i < RDecl->getNumMembers(); i++) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1694 | FieldDecl *FD = RDecl->getMember(i); |
| 1695 | if (NameFields) { |
| 1696 | S += '"'; |
| 1697 | S += FD->getName(); |
| 1698 | S += '"'; |
| 1699 | } |
| 1700 | |
| 1701 | // Special case bit-fields. |
| 1702 | if (const Expr *E = FD->getBitWidth()) { |
| 1703 | // FIXME: Fix constness. |
| 1704 | ASTContext *Ctx = const_cast<ASTContext*>(this); |
| 1705 | unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue(); |
| 1706 | // FIXME: Obj-C is losing information about the type size |
| 1707 | // here. Investigate if this is a problem. |
| 1708 | S += 'b'; |
| 1709 | S += llvm::utostr(N); |
| 1710 | } else { |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1711 | getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1712 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1713 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1714 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1715 | S += RDecl->isUnion() ? ')' : '}'; |
Steve Naroff | 5e71124 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 1716 | } else if (T->isEnumeralType()) { |
| 1717 | S += 'i'; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1718 | } else if (T->isBlockPointerType()) { |
| 1719 | S += '^'; // This type string is the same as general pointers. |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1720 | } else |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 1721 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1724 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1725 | std::string& S) const { |
| 1726 | if (QT & Decl::OBJC_TQ_In) |
| 1727 | S += 'n'; |
| 1728 | if (QT & Decl::OBJC_TQ_Inout) |
| 1729 | S += 'N'; |
| 1730 | if (QT & Decl::OBJC_TQ_Out) |
| 1731 | S += 'o'; |
| 1732 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 1733 | S += 'O'; |
| 1734 | if (QT & Decl::OBJC_TQ_Byref) |
| 1735 | S += 'R'; |
| 1736 | if (QT & Decl::OBJC_TQ_Oneway) |
| 1737 | S += 'V'; |
| 1738 | } |
| 1739 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1740 | void ASTContext::setBuiltinVaListType(QualType T) |
| 1741 | { |
| 1742 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 1743 | |
| 1744 | BuiltinVaListType = T; |
| 1745 | } |
| 1746 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1747 | void ASTContext::setObjCIdType(TypedefDecl *TD) |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1748 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1749 | ObjCIdType = getTypedefType(TD); |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1750 | |
| 1751 | // typedef struct objc_object *id; |
| 1752 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1753 | assert(ptr && "'id' incorrectly typed"); |
| 1754 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1755 | assert(rec && "'id' incorrectly typed"); |
| 1756 | IdStructType = rec; |
| 1757 | } |
| 1758 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1759 | void ASTContext::setObjCSelType(TypedefDecl *TD) |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1760 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1761 | ObjCSelType = getTypedefType(TD); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1762 | |
| 1763 | // typedef struct objc_selector *SEL; |
| 1764 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1765 | assert(ptr && "'SEL' incorrectly typed"); |
| 1766 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1767 | assert(rec && "'SEL' incorrectly typed"); |
| 1768 | SelStructType = rec; |
| 1769 | } |
| 1770 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1771 | void ASTContext::setObjCProtoType(QualType QT) |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1772 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1773 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1776 | void ASTContext::setObjCClassType(TypedefDecl *TD) |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1777 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1778 | ObjCClassType = getTypedefType(TD); |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1779 | |
| 1780 | // typedef struct objc_class *Class; |
| 1781 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1782 | assert(ptr && "'Class' incorrectly typed"); |
| 1783 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1784 | assert(rec && "'Class' incorrectly typed"); |
| 1785 | ClassStructType = rec; |
| 1786 | } |
| 1787 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1788 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
| 1789 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1790 | "'NSConstantString' type already set!"); |
| 1791 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1792 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1795 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 1796 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 1797 | /// is actually a value of type @c TargetInfo::IntType. |
| 1798 | QualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1799 | switch (Type) { |
| 1800 | case TargetInfo::NoInt: return QualType(); |
| 1801 | case TargetInfo::SignedShort: return ShortTy; |
| 1802 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 1803 | case TargetInfo::SignedInt: return IntTy; |
| 1804 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 1805 | case TargetInfo::SignedLong: return LongTy; |
| 1806 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 1807 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 1808 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 1809 | } |
| 1810 | |
| 1811 | assert(false && "Unhandled TargetInfo::IntType value"); |
Daniel Dunbar | b3ac543 | 2008-11-11 01:16:00 +0000 | [diff] [blame] | 1812 | return QualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1813 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 1814 | |
| 1815 | //===----------------------------------------------------------------------===// |
| 1816 | // Type Predicates. |
| 1817 | //===----------------------------------------------------------------------===// |
| 1818 | |
| 1819 | /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer |
| 1820 | /// to an object type. This includes "id" and "Class" (two 'special' pointers |
| 1821 | /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified |
| 1822 | /// ID type). |
| 1823 | bool ASTContext::isObjCObjectPointerType(QualType Ty) const { |
| 1824 | if (Ty->isObjCQualifiedIdType()) |
| 1825 | return true; |
| 1826 | |
Steve Naroff | 6ae9850 | 2008-10-21 18:24:04 +0000 | [diff] [blame] | 1827 | // Blocks are objects. |
| 1828 | if (Ty->isBlockPointerType()) |
| 1829 | return true; |
| 1830 | |
| 1831 | // All other object types are pointers. |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 1832 | if (!Ty->isPointerType()) |
| 1833 | return false; |
| 1834 | |
| 1835 | // Check to see if this is 'id' or 'Class', both of which are typedefs for |
| 1836 | // pointer types. This looks for the typedef specifically, not for the |
| 1837 | // underlying type. |
| 1838 | if (Ty == getObjCIdType() || Ty == getObjCClassType()) |
| 1839 | return true; |
| 1840 | |
| 1841 | // If this a pointer to an interface (e.g. NSString*), it is ok. |
| 1842 | return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType(); |
| 1843 | } |
| 1844 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1845 | //===----------------------------------------------------------------------===// |
| 1846 | // Type Compatibility Testing |
| 1847 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1848 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1849 | /// typesAreBlockCompatible - This routine is called when comparing two |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1850 | /// block types. Types must be strictly compatible here. For example, |
| 1851 | /// C unfortunately doesn't produce an error for the following: |
| 1852 | /// |
| 1853 | /// int (*emptyArgFunc)(); |
| 1854 | /// int (*intArgList)(int) = emptyArgFunc; |
| 1855 | /// |
| 1856 | /// For blocks, we will produce an error for the following (similar to C++): |
| 1857 | /// |
| 1858 | /// int (^emptyArgBlock)(); |
| 1859 | /// int (^intArgBlock)(int) = emptyArgBlock; |
| 1860 | /// |
| 1861 | /// FIXME: When the dust settles on this integration, fold this into mergeTypes. |
| 1862 | /// |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1863 | bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) { |
Steve Naroff | 3c1b912 | 2008-09-09 13:47:19 +0000 | [diff] [blame] | 1864 | return getCanonicalType(lhs) == getCanonicalType(rhs); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1867 | /// areCompatVectorTypes - Return true if the two specified vector types are |
| 1868 | /// compatible. |
| 1869 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 1870 | const VectorType *RHS) { |
| 1871 | assert(LHS->isCanonical() && RHS->isCanonical()); |
| 1872 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1873 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1876 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1877 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 1878 | /// protocol qualifiers on the LHS or RHS. |
| 1879 | /// |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1880 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 1881 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1882 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 1883 | // the LHS. |
| 1884 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 1885 | return false; |
| 1886 | |
| 1887 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 1888 | // protocol qualified at all, then we are good. |
| 1889 | if (!isa<ObjCQualifiedInterfaceType>(LHS)) |
| 1890 | return true; |
| 1891 | |
| 1892 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 1893 | // isn't a superset. |
| 1894 | if (!isa<ObjCQualifiedInterfaceType>(RHS)) |
| 1895 | return true; // FIXME: should return false! |
| 1896 | |
| 1897 | // Finally, we must have two protocol-qualified interfaces. |
| 1898 | const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS); |
| 1899 | const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS); |
| 1900 | ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(); |
| 1901 | ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end(); |
| 1902 | ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(); |
| 1903 | ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end(); |
| 1904 | |
| 1905 | // All protocols in LHS must have a presence in RHS. Since the protocol lists |
| 1906 | // are both sorted alphabetically and have no duplicates, we can scan RHS and |
| 1907 | // LHS in a single parallel scan until we run out of elements in LHS. |
| 1908 | assert(LHSPI != LHSPE && "Empty LHS protocol list?"); |
| 1909 | ObjCProtocolDecl *LHSProto = *LHSPI; |
| 1910 | |
| 1911 | while (RHSPI != RHSPE) { |
| 1912 | ObjCProtocolDecl *RHSProto = *RHSPI++; |
| 1913 | // If the RHS has a protocol that the LHS doesn't, ignore it. |
| 1914 | if (RHSProto != LHSProto) |
| 1915 | continue; |
| 1916 | |
| 1917 | // Otherwise, the RHS does have this element. |
| 1918 | ++LHSPI; |
| 1919 | if (LHSPI == LHSPE) |
| 1920 | return true; // All protocols in LHS exist in RHS. |
| 1921 | |
| 1922 | LHSProto = *LHSPI; |
| 1923 | } |
| 1924 | |
| 1925 | // If we got here, we didn't find one of the LHS's protocols in the RHS list. |
| 1926 | return false; |
| 1927 | } |
| 1928 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1929 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 1930 | /// both shall have the identically qualified version of a compatible type. |
| 1931 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 1932 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1933 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 1934 | return !mergeTypes(LHS, RHS).isNull(); |
| 1935 | } |
| 1936 | |
| 1937 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
| 1938 | const FunctionType *lbase = lhs->getAsFunctionType(); |
| 1939 | const FunctionType *rbase = rhs->getAsFunctionType(); |
| 1940 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 1941 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 1942 | bool allLTypes = true; |
| 1943 | bool allRTypes = true; |
| 1944 | |
| 1945 | // Check return type |
| 1946 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 1947 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1948 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 1949 | allLTypes = false; |
| 1950 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 1951 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1952 | |
| 1953 | if (lproto && rproto) { // two C99 style function prototypes |
| 1954 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 1955 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 1956 | |
| 1957 | // Compatible functions must have the same number of arguments |
| 1958 | if (lproto_nargs != rproto_nargs) |
| 1959 | return QualType(); |
| 1960 | |
| 1961 | // Variadic and non-variadic functions aren't compatible |
| 1962 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 1963 | return QualType(); |
| 1964 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1965 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 1966 | return QualType(); |
| 1967 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1968 | // Check argument compatibility |
| 1969 | llvm::SmallVector<QualType, 10> types; |
| 1970 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 1971 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 1972 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 1973 | QualType argtype = mergeTypes(largtype, rargtype); |
| 1974 | if (argtype.isNull()) return QualType(); |
| 1975 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1976 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 1977 | allLTypes = false; |
| 1978 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 1979 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1980 | } |
| 1981 | if (allLTypes) return lhs; |
| 1982 | if (allRTypes) return rhs; |
| 1983 | return getFunctionType(retType, types.begin(), types.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1984 | lproto->isVariadic(), lproto->getTypeQuals()); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | if (lproto) allRTypes = false; |
| 1988 | if (rproto) allLTypes = false; |
| 1989 | |
| 1990 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 1991 | if (proto) { |
| 1992 | if (proto->isVariadic()) return QualType(); |
| 1993 | // Check that the types are compatible with the types that |
| 1994 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 1995 | // The only types actually affected are promotable integer |
| 1996 | // types and floats, which would be passed as a different |
| 1997 | // type depending on whether the prototype is visible. |
| 1998 | unsigned proto_nargs = proto->getNumArgs(); |
| 1999 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 2000 | QualType argTy = proto->getArgType(i); |
| 2001 | if (argTy->isPromotableIntegerType() || |
| 2002 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 2003 | return QualType(); |
| 2004 | } |
| 2005 | |
| 2006 | if (allLTypes) return lhs; |
| 2007 | if (allRTypes) return rhs; |
| 2008 | return getFunctionType(retType, proto->arg_type_begin(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2009 | proto->getNumArgs(), lproto->isVariadic(), |
| 2010 | lproto->getTypeQuals()); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | if (allLTypes) return lhs; |
| 2014 | if (allRTypes) return rhs; |
| 2015 | return getFunctionTypeNoProto(retType); |
| 2016 | } |
| 2017 | |
| 2018 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 2019 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 2020 | // type is adjusted to "T" prior to any further analysis, the expression |
| 2021 | // designates the object or function denoted by the reference, and the |
| 2022 | // expression is an lvalue. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2023 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 2024 | // enough that they should be handled separately. |
| 2025 | if (const ReferenceType *RT = LHS->getAsReferenceType()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 2026 | LHS = RT->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2027 | if (const ReferenceType *RT = RHS->getAsReferenceType()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 2028 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 2029 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2030 | QualType LHSCan = getCanonicalType(LHS), |
| 2031 | RHSCan = getCanonicalType(RHS); |
| 2032 | |
| 2033 | // If two types are identical, they are compatible. |
| 2034 | if (LHSCan == RHSCan) |
| 2035 | return LHS; |
| 2036 | |
| 2037 | // If the qualifiers are different, the types aren't compatible |
| 2038 | if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() || |
| 2039 | LHSCan.getAddressSpace() != RHSCan.getAddressSpace()) |
| 2040 | return QualType(); |
| 2041 | |
| 2042 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 2043 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
| 2044 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2045 | // We want to consider the two function types to be the same for these |
| 2046 | // comparisons, just force one to the other. |
| 2047 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 2048 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2051 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 2052 | LHSClass = Type::ConstantArray; |
| 2053 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 2054 | RHSClass = Type::ConstantArray; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2055 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2056 | // Canonicalize ExtVector -> Vector. |
| 2057 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 2058 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | b048981 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 2060 | // Consider qualified interfaces and interfaces the same. |
| 2061 | if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface; |
| 2062 | if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2063 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2064 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2065 | if (LHSClass != RHSClass) { |
Steve Naroff | 9734162 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 2066 | // ID is compatible with all qualified id types. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2067 | if (LHS->isObjCQualifiedIdType()) { |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 2068 | if (const PointerType *PT = RHS->getAsPointerType()) { |
| 2069 | QualType pType = PT->getPointeeType(); |
| 2070 | if (isObjCIdType(pType)) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2071 | return LHS; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 2072 | // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). |
| 2073 | // Unfortunately, this API is part of Sema (which we don't have access |
| 2074 | // to. Need to refactor. The following check is insufficient, since we |
| 2075 | // need to make sure the class implements the protocol. |
| 2076 | if (pType->isObjCInterfaceType()) |
| 2077 | return LHS; |
| 2078 | } |
Steve Naroff | 9734162 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 2079 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2080 | if (RHS->isObjCQualifiedIdType()) { |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 2081 | if (const PointerType *PT = LHS->getAsPointerType()) { |
| 2082 | QualType pType = PT->getPointeeType(); |
| 2083 | if (isObjCIdType(pType)) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2084 | return RHS; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 2085 | // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). |
| 2086 | // Unfortunately, this API is part of Sema (which we don't have access |
| 2087 | // to. Need to refactor. The following check is insufficient, since we |
| 2088 | // need to make sure the class implements the protocol. |
| 2089 | if (pType->isObjCInterfaceType()) |
| 2090 | return RHS; |
| 2091 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2094 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 2095 | // a signed integer type, or an unsigned integer type. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2096 | if (const EnumType* ETy = LHS->getAsEnumType()) { |
| 2097 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 2098 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 2099 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2100 | if (const EnumType* ETy = RHS->getAsEnumType()) { |
| 2101 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 2102 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 2103 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2104 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2105 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2106 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2107 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 2108 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2109 | switch (LHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2110 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2111 | { |
| 2112 | // Merge two pointer types, while trying to preserve typedef info |
| 2113 | QualType LHSPointee = LHS->getAsPointerType()->getPointeeType(); |
| 2114 | QualType RHSPointee = RHS->getAsPointerType()->getPointeeType(); |
| 2115 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 2116 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2117 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 2118 | return LHS; |
| 2119 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 2120 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2121 | return getPointerType(ResultType); |
| 2122 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2123 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2124 | { |
| 2125 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 2126 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 2127 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 2128 | return QualType(); |
| 2129 | |
| 2130 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 2131 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 2132 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 2133 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2134 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 2135 | return LHS; |
| 2136 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 2137 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 2138 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 2139 | ArrayType::ArraySizeModifier(), 0); |
| 2140 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 2141 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2142 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 2143 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2144 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 2145 | return LHS; |
| 2146 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 2147 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2148 | if (LVAT) { |
| 2149 | // FIXME: This isn't correct! But tricky to implement because |
| 2150 | // the array's size has to be the size of LHS, but the type |
| 2151 | // has to be different. |
| 2152 | return LHS; |
| 2153 | } |
| 2154 | if (RVAT) { |
| 2155 | // FIXME: This isn't correct! But tricky to implement because |
| 2156 | // the array's size has to be the size of RHS, but the type |
| 2157 | // has to be different. |
| 2158 | return RHS; |
| 2159 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 2160 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 2161 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2162 | return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2163 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2164 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2165 | return mergeFunctionTypes(LHS, RHS); |
| 2166 | case Type::Tagged: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2167 | // FIXME: Why are these compatible? |
| 2168 | if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS; |
| 2169 | if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS; |
| 2170 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2171 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 2172 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2173 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 2174 | case Type::Vector: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2175 | if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) |
| 2176 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2177 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2178 | case Type::ObjCInterface: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2179 | // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces |
| 2180 | // for checking assignment/comparison safety |
| 2181 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2182 | default: |
| 2183 | assert(0 && "unexpected type"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2184 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2185 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2186 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2187 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 2188 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 2189 | // Integer Predicates |
| 2190 | //===----------------------------------------------------------------------===// |
| 2191 | unsigned ASTContext::getIntWidth(QualType T) { |
| 2192 | if (T == BoolTy) |
| 2193 | return 1; |
| 2194 | // At the moment, only bool has padding bits |
| 2195 | return (unsigned)getTypeSize(T); |
| 2196 | } |
| 2197 | |
| 2198 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 2199 | assert(T->isSignedIntegerType() && "Unexpected type"); |
| 2200 | if (const EnumType* ETy = T->getAsEnumType()) |
| 2201 | T = ETy->getDecl()->getIntegerType(); |
| 2202 | const BuiltinType* BTy = T->getAsBuiltinType(); |
| 2203 | assert (BTy && "Unexpected signed integer type"); |
| 2204 | switch (BTy->getKind()) { |
| 2205 | case BuiltinType::Char_S: |
| 2206 | case BuiltinType::SChar: |
| 2207 | return UnsignedCharTy; |
| 2208 | case BuiltinType::Short: |
| 2209 | return UnsignedShortTy; |
| 2210 | case BuiltinType::Int: |
| 2211 | return UnsignedIntTy; |
| 2212 | case BuiltinType::Long: |
| 2213 | return UnsignedLongTy; |
| 2214 | case BuiltinType::LongLong: |
| 2215 | return UnsignedLongLongTy; |
| 2216 | default: |
| 2217 | assert(0 && "Unexpected signed integer type"); |
| 2218 | return QualType(); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | |
| 2223 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 2224 | // Serialization Support |
| 2225 | //===----------------------------------------------------------------------===// |
| 2226 | |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2227 | /// Emit - Serialize an ASTContext object to Bitcode. |
| 2228 | void ASTContext::Emit(llvm::Serializer& S) const { |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2229 | S.Emit(LangOpts); |
Ted Kremenek | 5451350 | 2007-10-31 20:00:03 +0000 | [diff] [blame] | 2230 | S.EmitRef(SourceMgr); |
| 2231 | S.EmitRef(Target); |
| 2232 | S.EmitRef(Idents); |
| 2233 | S.EmitRef(Selectors); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2234 | |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2235 | // Emit the size of the type vector so that we can reserve that size |
| 2236 | // when we reconstitute the ASTContext object. |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 2237 | S.EmitInt(Types.size()); |
| 2238 | |
Ted Kremenek | 03ed440 | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 2239 | for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); |
| 2240 | I!=E;++I) |
| 2241 | (*I)->Emit(S); |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 2242 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2243 | S.EmitOwnedPtr(TUDecl); |
| 2244 | |
Ted Kremenek | a9a4a24 | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 2245 | // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Ted Kremenek | 0f84c00 | 2007-11-13 00:25:37 +0000 | [diff] [blame] | 2248 | ASTContext* ASTContext::Create(llvm::Deserializer& D) { |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2249 | |
| 2250 | // Read the language options. |
| 2251 | LangOptions LOpts; |
| 2252 | LOpts.Read(D); |
| 2253 | |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2254 | SourceManager &SM = D.ReadRef<SourceManager>(); |
| 2255 | TargetInfo &t = D.ReadRef<TargetInfo>(); |
| 2256 | IdentifierTable &idents = D.ReadRef<IdentifierTable>(); |
| 2257 | SelectorTable &sels = D.ReadRef<SelectorTable>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2258 | |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2259 | unsigned size_reserve = D.ReadInt(); |
| 2260 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2261 | ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, |
| 2262 | size_reserve); |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2263 | |
Ted Kremenek | 03ed440 | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 2264 | for (unsigned i = 0; i < size_reserve; ++i) |
| 2265 | Type::Create(*A,i,D); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2266 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2267 | A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A)); |
| 2268 | |
Ted Kremenek | a9a4a24 | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 2269 | // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2270 | |
| 2271 | return A; |
| 2272 | } |