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