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