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