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