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