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