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