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. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1477 | getObjCEncodingForType(Decl->getResultType(), S); |
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); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1505 | getObjCEncodingForType(PType, S); |
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. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1560 | getObjCEncodingForType(PD->getType(), S); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 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, |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1597 | bool NameFields) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1598 | // We follow the behavior of gcc, expanding structures which are |
| 1599 | // directly pointed to, and expanding embedded structures. Note that |
| 1600 | // these rules are sufficient to prevent recursive encoding of the |
| 1601 | // same type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1602 | getObjCEncodingForTypeImpl(T, S, true, true, NameFields); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 1606 | bool ExpandPointedToStructures, |
| 1607 | bool ExpandStructures, |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1608 | bool NameFields) const { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1609 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1610 | char encoding; |
| 1611 | switch (BT->getKind()) { |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1612 | default: assert(0 && "Unhandled builtin type kind"); |
| 1613 | case BuiltinType::Void: encoding = 'v'; break; |
| 1614 | case BuiltinType::Bool: encoding = 'B'; break; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1615 | case BuiltinType::Char_U: |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1616 | case BuiltinType::UChar: encoding = 'C'; break; |
| 1617 | case BuiltinType::UShort: encoding = 'S'; break; |
| 1618 | case BuiltinType::UInt: encoding = 'I'; break; |
| 1619 | case BuiltinType::ULong: encoding = 'L'; break; |
| 1620 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1621 | case BuiltinType::Char_S: |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1622 | case BuiltinType::SChar: encoding = 'c'; break; |
| 1623 | case BuiltinType::Short: encoding = 's'; break; |
| 1624 | case BuiltinType::Int: encoding = 'i'; break; |
| 1625 | case BuiltinType::Long: encoding = 'l'; break; |
| 1626 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 1627 | case BuiltinType::Float: encoding = 'f'; break; |
| 1628 | case BuiltinType::Double: encoding = 'd'; break; |
| 1629 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | S += encoding; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1633 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1634 | else if (T->isObjCQualifiedIdType()) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1635 | // Treat id<P...> same as 'id' for encoding purposes. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1636 | return getObjCEncodingForTypeImpl(getObjCIdType(), S, |
| 1637 | ExpandPointedToStructures, |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1638 | ExpandStructures, NameFields); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1639 | } |
| 1640 | else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1641 | QualType PointeeTy = PT->getPointeeType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1642 | if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) { |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1643 | S += '@'; |
| 1644 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1645 | } else if (isObjCClassType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1646 | S += '#'; |
| 1647 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1648 | } else if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1649 | S += ':'; |
| 1650 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1651 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1652 | |
| 1653 | if (PointeeTy->isCharType()) { |
| 1654 | // char pointer types should be encoded as '*' unless it is a |
| 1655 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1656 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1657 | S += '*'; |
| 1658 | return; |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | S += '^'; |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1663 | getObjCEncodingForTypeImpl(PT->getPointeeType(), S, |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1664 | false, ExpandPointedToStructures, |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1665 | NameFields); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1666 | } else if (const ArrayType *AT = |
| 1667 | // Ignore type qualifiers etc. |
| 1668 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1669 | S += '['; |
| 1670 | |
| 1671 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1672 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 1673 | else |
| 1674 | assert(0 && "Unhandled array type!"); |
| 1675 | |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1676 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1677 | false, ExpandStructures, NameFields); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1678 | S += ']'; |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 1679 | } else if (T->getAsFunctionType()) { |
| 1680 | S += '?'; |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1681 | } else if (const RecordType *RTy = T->getAsRecordType()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 1682 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1683 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 1684 | // Anonymous structures print as '?' |
| 1685 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 1686 | S += II->getName(); |
| 1687 | } else { |
| 1688 | S += '?'; |
| 1689 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1690 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1691 | S += '='; |
| 1692 | for (int i = 0; i < RDecl->getNumMembers(); i++) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1693 | FieldDecl *FD = RDecl->getMember(i); |
| 1694 | if (NameFields) { |
| 1695 | S += '"'; |
| 1696 | S += FD->getName(); |
| 1697 | S += '"'; |
| 1698 | } |
| 1699 | |
| 1700 | // Special case bit-fields. |
| 1701 | if (const Expr *E = FD->getBitWidth()) { |
| 1702 | // FIXME: Fix constness. |
| 1703 | ASTContext *Ctx = const_cast<ASTContext*>(this); |
| 1704 | unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue(); |
| 1705 | // FIXME: Obj-C is losing information about the type size |
| 1706 | // here. Investigate if this is a problem. |
| 1707 | S += 'b'; |
| 1708 | S += llvm::utostr(N); |
| 1709 | } else { |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame^] | 1710 | getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1711 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1712 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1713 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 1714 | S += RDecl->isUnion() ? ')' : '}'; |
Steve Naroff | 5e71124 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 1715 | } else if (T->isEnumeralType()) { |
| 1716 | S += 'i'; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1717 | } else if (T->isBlockPointerType()) { |
| 1718 | S += '^'; // This type string is the same as general pointers. |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1719 | } else |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 1720 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1723 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1724 | std::string& S) const { |
| 1725 | if (QT & Decl::OBJC_TQ_In) |
| 1726 | S += 'n'; |
| 1727 | if (QT & Decl::OBJC_TQ_Inout) |
| 1728 | S += 'N'; |
| 1729 | if (QT & Decl::OBJC_TQ_Out) |
| 1730 | S += 'o'; |
| 1731 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 1732 | S += 'O'; |
| 1733 | if (QT & Decl::OBJC_TQ_Byref) |
| 1734 | S += 'R'; |
| 1735 | if (QT & Decl::OBJC_TQ_Oneway) |
| 1736 | S += 'V'; |
| 1737 | } |
| 1738 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1739 | void ASTContext::setBuiltinVaListType(QualType T) |
| 1740 | { |
| 1741 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 1742 | |
| 1743 | BuiltinVaListType = T; |
| 1744 | } |
| 1745 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1746 | void ASTContext::setObjCIdType(TypedefDecl *TD) |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1747 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1748 | ObjCIdType = getTypedefType(TD); |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1749 | |
| 1750 | // typedef struct objc_object *id; |
| 1751 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1752 | assert(ptr && "'id' incorrectly typed"); |
| 1753 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1754 | assert(rec && "'id' incorrectly typed"); |
| 1755 | IdStructType = rec; |
| 1756 | } |
| 1757 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1758 | void ASTContext::setObjCSelType(TypedefDecl *TD) |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1759 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1760 | ObjCSelType = getTypedefType(TD); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1761 | |
| 1762 | // typedef struct objc_selector *SEL; |
| 1763 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1764 | assert(ptr && "'SEL' incorrectly typed"); |
| 1765 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1766 | assert(rec && "'SEL' incorrectly typed"); |
| 1767 | SelStructType = rec; |
| 1768 | } |
| 1769 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1770 | void ASTContext::setObjCProtoType(QualType QT) |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1771 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1772 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1775 | void ASTContext::setObjCClassType(TypedefDecl *TD) |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1776 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1777 | ObjCClassType = getTypedefType(TD); |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1778 | |
| 1779 | // typedef struct objc_class *Class; |
| 1780 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1781 | assert(ptr && "'Class' incorrectly typed"); |
| 1782 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1783 | assert(rec && "'Class' incorrectly typed"); |
| 1784 | ClassStructType = rec; |
| 1785 | } |
| 1786 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1787 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
| 1788 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1789 | "'NSConstantString' type already set!"); |
| 1790 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1791 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 1794 | |
| 1795 | //===----------------------------------------------------------------------===// |
| 1796 | // Type Predicates. |
| 1797 | //===----------------------------------------------------------------------===// |
| 1798 | |
| 1799 | /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer |
| 1800 | /// to an object type. This includes "id" and "Class" (two 'special' pointers |
| 1801 | /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified |
| 1802 | /// ID type). |
| 1803 | bool ASTContext::isObjCObjectPointerType(QualType Ty) const { |
| 1804 | if (Ty->isObjCQualifiedIdType()) |
| 1805 | return true; |
| 1806 | |
| 1807 | if (!Ty->isPointerType()) |
| 1808 | return false; |
| 1809 | |
| 1810 | // Check to see if this is 'id' or 'Class', both of which are typedefs for |
| 1811 | // pointer types. This looks for the typedef specifically, not for the |
| 1812 | // underlying type. |
| 1813 | if (Ty == getObjCIdType() || Ty == getObjCClassType()) |
| 1814 | return true; |
| 1815 | |
| 1816 | // If this a pointer to an interface (e.g. NSString*), it is ok. |
| 1817 | return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType(); |
| 1818 | } |
| 1819 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1820 | //===----------------------------------------------------------------------===// |
| 1821 | // Type Compatibility Testing |
| 1822 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1823 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1824 | /// typesAreBlockCompatible - This routine is called when comparing two |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1825 | /// block types. Types must be strictly compatible here. For example, |
| 1826 | /// C unfortunately doesn't produce an error for the following: |
| 1827 | /// |
| 1828 | /// int (*emptyArgFunc)(); |
| 1829 | /// int (*intArgList)(int) = emptyArgFunc; |
| 1830 | /// |
| 1831 | /// For blocks, we will produce an error for the following (similar to C++): |
| 1832 | /// |
| 1833 | /// int (^emptyArgBlock)(); |
| 1834 | /// int (^intArgBlock)(int) = emptyArgBlock; |
| 1835 | /// |
| 1836 | /// FIXME: When the dust settles on this integration, fold this into mergeTypes. |
| 1837 | /// |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1838 | bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) { |
Steve Naroff | 3c1b912 | 2008-09-09 13:47:19 +0000 | [diff] [blame] | 1839 | return getCanonicalType(lhs) == getCanonicalType(rhs); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1842 | /// areCompatVectorTypes - Return true if the two specified vector types are |
| 1843 | /// compatible. |
| 1844 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 1845 | const VectorType *RHS) { |
| 1846 | assert(LHS->isCanonical() && RHS->isCanonical()); |
| 1847 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1848 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1851 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1852 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 1853 | /// protocol qualifiers on the LHS or RHS. |
| 1854 | /// |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1855 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 1856 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 1857 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 1858 | // the LHS. |
| 1859 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 1860 | return false; |
| 1861 | |
| 1862 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 1863 | // protocol qualified at all, then we are good. |
| 1864 | if (!isa<ObjCQualifiedInterfaceType>(LHS)) |
| 1865 | return true; |
| 1866 | |
| 1867 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 1868 | // isn't a superset. |
| 1869 | if (!isa<ObjCQualifiedInterfaceType>(RHS)) |
| 1870 | return true; // FIXME: should return false! |
| 1871 | |
| 1872 | // Finally, we must have two protocol-qualified interfaces. |
| 1873 | const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS); |
| 1874 | const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS); |
| 1875 | ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(); |
| 1876 | ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end(); |
| 1877 | ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(); |
| 1878 | ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end(); |
| 1879 | |
| 1880 | // All protocols in LHS must have a presence in RHS. Since the protocol lists |
| 1881 | // are both sorted alphabetically and have no duplicates, we can scan RHS and |
| 1882 | // LHS in a single parallel scan until we run out of elements in LHS. |
| 1883 | assert(LHSPI != LHSPE && "Empty LHS protocol list?"); |
| 1884 | ObjCProtocolDecl *LHSProto = *LHSPI; |
| 1885 | |
| 1886 | while (RHSPI != RHSPE) { |
| 1887 | ObjCProtocolDecl *RHSProto = *RHSPI++; |
| 1888 | // If the RHS has a protocol that the LHS doesn't, ignore it. |
| 1889 | if (RHSProto != LHSProto) |
| 1890 | continue; |
| 1891 | |
| 1892 | // Otherwise, the RHS does have this element. |
| 1893 | ++LHSPI; |
| 1894 | if (LHSPI == LHSPE) |
| 1895 | return true; // All protocols in LHS exist in RHS. |
| 1896 | |
| 1897 | LHSProto = *LHSPI; |
| 1898 | } |
| 1899 | |
| 1900 | // If we got here, we didn't find one of the LHS's protocols in the RHS list. |
| 1901 | return false; |
| 1902 | } |
| 1903 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1904 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 1905 | /// both shall have the identically qualified version of a compatible type. |
| 1906 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 1907 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1908 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 1909 | return !mergeTypes(LHS, RHS).isNull(); |
| 1910 | } |
| 1911 | |
| 1912 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
| 1913 | const FunctionType *lbase = lhs->getAsFunctionType(); |
| 1914 | const FunctionType *rbase = rhs->getAsFunctionType(); |
| 1915 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 1916 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 1917 | bool allLTypes = true; |
| 1918 | bool allRTypes = true; |
| 1919 | |
| 1920 | // Check return type |
| 1921 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 1922 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1923 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 1924 | allLTypes = false; |
| 1925 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 1926 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1927 | |
| 1928 | if (lproto && rproto) { // two C99 style function prototypes |
| 1929 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 1930 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 1931 | |
| 1932 | // Compatible functions must have the same number of arguments |
| 1933 | if (lproto_nargs != rproto_nargs) |
| 1934 | return QualType(); |
| 1935 | |
| 1936 | // Variadic and non-variadic functions aren't compatible |
| 1937 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 1938 | return QualType(); |
| 1939 | |
| 1940 | // Check argument compatibility |
| 1941 | llvm::SmallVector<QualType, 10> types; |
| 1942 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 1943 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 1944 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 1945 | QualType argtype = mergeTypes(largtype, rargtype); |
| 1946 | if (argtype.isNull()) return QualType(); |
| 1947 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1948 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 1949 | allLTypes = false; |
| 1950 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 1951 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1952 | } |
| 1953 | if (allLTypes) return lhs; |
| 1954 | if (allRTypes) return rhs; |
| 1955 | return getFunctionType(retType, types.begin(), types.size(), |
| 1956 | lproto->isVariadic()); |
| 1957 | } |
| 1958 | |
| 1959 | if (lproto) allRTypes = false; |
| 1960 | if (rproto) allLTypes = false; |
| 1961 | |
| 1962 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 1963 | if (proto) { |
| 1964 | if (proto->isVariadic()) return QualType(); |
| 1965 | // Check that the types are compatible with the types that |
| 1966 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 1967 | // The only types actually affected are promotable integer |
| 1968 | // types and floats, which would be passed as a different |
| 1969 | // type depending on whether the prototype is visible. |
| 1970 | unsigned proto_nargs = proto->getNumArgs(); |
| 1971 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 1972 | QualType argTy = proto->getArgType(i); |
| 1973 | if (argTy->isPromotableIntegerType() || |
| 1974 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 1975 | return QualType(); |
| 1976 | } |
| 1977 | |
| 1978 | if (allLTypes) return lhs; |
| 1979 | if (allRTypes) return rhs; |
| 1980 | return getFunctionType(retType, proto->arg_type_begin(), |
| 1981 | proto->getNumArgs(), lproto->isVariadic()); |
| 1982 | } |
| 1983 | |
| 1984 | if (allLTypes) return lhs; |
| 1985 | if (allRTypes) return rhs; |
| 1986 | return getFunctionTypeNoProto(retType); |
| 1987 | } |
| 1988 | |
| 1989 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 1990 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 1991 | // type is adjusted to "T" prior to any further analysis, the expression |
| 1992 | // designates the object or function denoted by the reference, and the |
| 1993 | // expression is an lvalue. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1994 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 1995 | // enough that they should be handled separately. |
| 1996 | if (const ReferenceType *RT = LHS->getAsReferenceType()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 1997 | LHS = RT->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1998 | if (const ReferenceType *RT = RHS->getAsReferenceType()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 1999 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 2000 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2001 | QualType LHSCan = getCanonicalType(LHS), |
| 2002 | RHSCan = getCanonicalType(RHS); |
| 2003 | |
| 2004 | // If two types are identical, they are compatible. |
| 2005 | if (LHSCan == RHSCan) |
| 2006 | return LHS; |
| 2007 | |
| 2008 | // If the qualifiers are different, the types aren't compatible |
| 2009 | if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() || |
| 2010 | LHSCan.getAddressSpace() != RHSCan.getAddressSpace()) |
| 2011 | return QualType(); |
| 2012 | |
| 2013 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 2014 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
| 2015 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2016 | // We want to consider the two function types to be the same for these |
| 2017 | // comparisons, just force one to the other. |
| 2018 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 2019 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 2020 | |
| 2021 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2022 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 2023 | LHSClass = Type::ConstantArray; |
| 2024 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 2025 | RHSClass = Type::ConstantArray; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2026 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2027 | // Canonicalize ExtVector -> Vector. |
| 2028 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 2029 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2030 | |
Chris Lattner | b048981 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 2031 | // Consider qualified interfaces and interfaces the same. |
| 2032 | if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface; |
| 2033 | if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 2035 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2036 | if (LHSClass != RHSClass) { |
Steve Naroff | 9734162 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 2037 | // ID is compatible with all qualified id types. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2038 | if (LHS->isObjCQualifiedIdType()) { |
Steve Naroff | 9734162 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 2039 | if (const PointerType *PT = RHS->getAsPointerType()) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2040 | if (isObjCIdType(PT->getPointeeType())) |
| 2041 | return LHS; |
Steve Naroff | 9734162 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 2042 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2043 | if (RHS->isObjCQualifiedIdType()) { |
Steve Naroff | 9734162 | 2008-06-04 15:07:33 +0000 | [diff] [blame] | 2044 | if (const PointerType *PT = LHS->getAsPointerType()) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2045 | if (isObjCIdType(PT->getPointeeType())) |
| 2046 | return RHS; |
| 2047 | } |
| 2048 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2049 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 2050 | // a signed integer type, or an unsigned integer type. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2051 | if (const EnumType* ETy = LHS->getAsEnumType()) { |
| 2052 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 2053 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 2054 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2055 | if (const EnumType* ETy = RHS->getAsEnumType()) { |
| 2056 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 2057 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 2058 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2059 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2060 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2061 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2062 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 2063 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2064 | switch (LHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2065 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2066 | { |
| 2067 | // Merge two pointer types, while trying to preserve typedef info |
| 2068 | QualType LHSPointee = LHS->getAsPointerType()->getPointeeType(); |
| 2069 | QualType RHSPointee = RHS->getAsPointerType()->getPointeeType(); |
| 2070 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 2071 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2072 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 2073 | return LHS; |
| 2074 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 2075 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2076 | return getPointerType(ResultType); |
| 2077 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2078 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2079 | { |
| 2080 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 2081 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 2082 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 2083 | return QualType(); |
| 2084 | |
| 2085 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 2086 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 2087 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 2088 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2089 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 2090 | return LHS; |
| 2091 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 2092 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 2093 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 2094 | ArrayType::ArraySizeModifier(), 0); |
| 2095 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 2096 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2097 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 2098 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2099 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 2100 | return LHS; |
| 2101 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 2102 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2103 | if (LVAT) { |
| 2104 | // FIXME: This isn't correct! But tricky to implement because |
| 2105 | // the array's size has to be the size of LHS, but the type |
| 2106 | // has to be different. |
| 2107 | return LHS; |
| 2108 | } |
| 2109 | if (RVAT) { |
| 2110 | // FIXME: This isn't correct! But tricky to implement because |
| 2111 | // the array's size has to be the size of RHS, but the type |
| 2112 | // has to be different. |
| 2113 | return RHS; |
| 2114 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 2115 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 2116 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2117 | return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2118 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2119 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2120 | return mergeFunctionTypes(LHS, RHS); |
| 2121 | case Type::Tagged: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2122 | // FIXME: Why are these compatible? |
| 2123 | if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS; |
| 2124 | if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS; |
| 2125 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2126 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 2127 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2128 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 2129 | case Type::Vector: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2130 | if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) |
| 2131 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2132 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2133 | case Type::ObjCInterface: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2134 | // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces |
| 2135 | // for checking assignment/comparison safety |
| 2136 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 2137 | default: |
| 2138 | assert(0 && "unexpected type"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2139 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2140 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2141 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2142 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 2143 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 2144 | // Integer Predicates |
| 2145 | //===----------------------------------------------------------------------===// |
| 2146 | unsigned ASTContext::getIntWidth(QualType T) { |
| 2147 | if (T == BoolTy) |
| 2148 | return 1; |
| 2149 | // At the moment, only bool has padding bits |
| 2150 | return (unsigned)getTypeSize(T); |
| 2151 | } |
| 2152 | |
| 2153 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 2154 | assert(T->isSignedIntegerType() && "Unexpected type"); |
| 2155 | if (const EnumType* ETy = T->getAsEnumType()) |
| 2156 | T = ETy->getDecl()->getIntegerType(); |
| 2157 | const BuiltinType* BTy = T->getAsBuiltinType(); |
| 2158 | assert (BTy && "Unexpected signed integer type"); |
| 2159 | switch (BTy->getKind()) { |
| 2160 | case BuiltinType::Char_S: |
| 2161 | case BuiltinType::SChar: |
| 2162 | return UnsignedCharTy; |
| 2163 | case BuiltinType::Short: |
| 2164 | return UnsignedShortTy; |
| 2165 | case BuiltinType::Int: |
| 2166 | return UnsignedIntTy; |
| 2167 | case BuiltinType::Long: |
| 2168 | return UnsignedLongTy; |
| 2169 | case BuiltinType::LongLong: |
| 2170 | return UnsignedLongLongTy; |
| 2171 | default: |
| 2172 | assert(0 && "Unexpected signed integer type"); |
| 2173 | return QualType(); |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | |
| 2178 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 2179 | // Serialization Support |
| 2180 | //===----------------------------------------------------------------------===// |
| 2181 | |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2182 | /// Emit - Serialize an ASTContext object to Bitcode. |
| 2183 | void ASTContext::Emit(llvm::Serializer& S) const { |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2184 | S.Emit(LangOpts); |
Ted Kremenek | 5451350 | 2007-10-31 20:00:03 +0000 | [diff] [blame] | 2185 | S.EmitRef(SourceMgr); |
| 2186 | S.EmitRef(Target); |
| 2187 | S.EmitRef(Idents); |
| 2188 | S.EmitRef(Selectors); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2189 | |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2190 | // Emit the size of the type vector so that we can reserve that size |
| 2191 | // when we reconstitute the ASTContext object. |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 2192 | S.EmitInt(Types.size()); |
| 2193 | |
Ted Kremenek | 03ed440 | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 2194 | for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); |
| 2195 | I!=E;++I) |
| 2196 | (*I)->Emit(S); |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 2197 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2198 | S.EmitOwnedPtr(TUDecl); |
| 2199 | |
Ted Kremenek | a9a4a24 | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 2200 | // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Ted Kremenek | 0f84c00 | 2007-11-13 00:25:37 +0000 | [diff] [blame] | 2203 | ASTContext* ASTContext::Create(llvm::Deserializer& D) { |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2204 | |
| 2205 | // Read the language options. |
| 2206 | LangOptions LOpts; |
| 2207 | LOpts.Read(D); |
| 2208 | |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2209 | SourceManager &SM = D.ReadRef<SourceManager>(); |
| 2210 | TargetInfo &t = D.ReadRef<TargetInfo>(); |
| 2211 | IdentifierTable &idents = D.ReadRef<IdentifierTable>(); |
| 2212 | SelectorTable &sels = D.ReadRef<SelectorTable>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2213 | |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2214 | unsigned size_reserve = D.ReadInt(); |
| 2215 | |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 2216 | ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve); |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2217 | |
Ted Kremenek | 03ed440 | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 2218 | for (unsigned i = 0; i < size_reserve; ++i) |
| 2219 | Type::Create(*A,i,D); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2220 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2221 | A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A)); |
| 2222 | |
Ted Kremenek | a9a4a24 | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 2223 | // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 2224 | |
| 2225 | return A; |
| 2226 | } |