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