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