Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/Decl.h" |
Steve Naroff | 3fafa10 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/Serialize.h" |
| 21 | #include "llvm/Bitcode/Deserialize.h" |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | enum FloatingRank { |
| 26 | FloatRank, DoubleRank, LongDoubleRank |
| 27 | }; |
| 28 | |
| 29 | ASTContext::~ASTContext() { |
| 30 | // Deallocate all the types. |
| 31 | while (!Types.empty()) { |
| 32 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) { |
| 33 | // Destroy the object, but don't call delete. These are malloc'd. |
| 34 | FT->~FunctionTypeProto(); |
| 35 | free(FT); |
| 36 | } else { |
| 37 | delete Types.back(); |
| 38 | } |
| 39 | Types.pop_back(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void ASTContext::PrintStats() const { |
| 44 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 45 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 46 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
| 47 | unsigned NumVector = 0, NumComplex = 0; |
| 48 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
| 49 | |
| 50 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 51 | unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 52 | unsigned NumObjcQualifiedIds = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 53 | |
| 54 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 55 | Type *T = Types[i]; |
| 56 | if (isa<BuiltinType>(T)) |
| 57 | ++NumBuiltin; |
| 58 | else if (isa<PointerType>(T)) |
| 59 | ++NumPointer; |
| 60 | else if (isa<ReferenceType>(T)) |
| 61 | ++NumReference; |
| 62 | else if (isa<ComplexType>(T)) |
| 63 | ++NumComplex; |
| 64 | else if (isa<ArrayType>(T)) |
| 65 | ++NumArray; |
| 66 | else if (isa<VectorType>(T)) |
| 67 | ++NumVector; |
| 68 | else if (isa<FunctionTypeNoProto>(T)) |
| 69 | ++NumFunctionNP; |
| 70 | else if (isa<FunctionTypeProto>(T)) |
| 71 | ++NumFunctionP; |
| 72 | else if (isa<TypedefType>(T)) |
| 73 | ++NumTypeName; |
| 74 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 75 | ++NumTagged; |
| 76 | switch (TT->getDecl()->getKind()) { |
| 77 | default: assert(0 && "Unknown tagged type!"); |
| 78 | case Decl::Struct: ++NumTagStruct; break; |
| 79 | case Decl::Union: ++NumTagUnion; break; |
| 80 | case Decl::Class: ++NumTagClass; break; |
| 81 | case Decl::Enum: ++NumTagEnum; break; |
| 82 | } |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 83 | } else if (isa<ObjcInterfaceType>(T)) |
| 84 | ++NumObjcInterfaces; |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 85 | else if (isa<ObjcQualifiedInterfaceType>(T)) |
| 86 | ++NumObjcQualifiedInterfaces; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 87 | else if (isa<ObjcQualifiedIdType>(T)) |
| 88 | ++NumObjcQualifiedIds; |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 89 | else { |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 90 | QualType(T, 0).dump(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 91 | assert(0 && "Unknown type!"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 96 | fprintf(stderr, " %d pointer types\n", NumPointer); |
| 97 | fprintf(stderr, " %d reference types\n", NumReference); |
| 98 | fprintf(stderr, " %d complex types\n", NumComplex); |
| 99 | fprintf(stderr, " %d array types\n", NumArray); |
| 100 | fprintf(stderr, " %d vector types\n", NumVector); |
| 101 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 102 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 103 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 104 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 105 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 106 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 107 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 108 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 109 | fprintf(stderr, " %d interface types\n", NumObjcInterfaces); |
Chris Lattner | 8a35b46 | 2007-12-12 06:43:05 +0000 | [diff] [blame] | 110 | fprintf(stderr, " %d protocol qualified interface types\n", |
| 111 | NumObjcQualifiedInterfaces); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 112 | fprintf(stderr, " %d protocol qualified id types\n", |
| 113 | NumObjcQualifiedIds); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 114 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 115 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
| 116 | NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ |
| 117 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 118 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
| 119 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType))); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 124 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
| 125 | } |
| 126 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 127 | void ASTContext::InitBuiltinTypes() { |
| 128 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 129 | |
| 130 | // C99 6.2.5p19. |
| 131 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 132 | |
| 133 | // C99 6.2.5p2. |
| 134 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 135 | // C99 6.2.5p3. |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 136 | if (Target.isCharSigned(FullSourceLoc())) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 137 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 138 | else |
| 139 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 140 | // C99 6.2.5p4. |
| 141 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 142 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 143 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 144 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 145 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 146 | |
| 147 | // C99 6.2.5p6. |
| 148 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 149 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 150 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 151 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 152 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 153 | |
| 154 | // C99 6.2.5p10. |
| 155 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 156 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 157 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
| 158 | |
| 159 | // C99 6.2.5p11. |
| 160 | FloatComplexTy = getComplexType(FloatTy); |
| 161 | DoubleComplexTy = getComplexType(DoubleTy); |
| 162 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 163 | |
| 164 | BuiltinVaListType = QualType(); |
| 165 | ObjcIdType = QualType(); |
| 166 | IdStructType = 0; |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 167 | ObjcClassType = QualType(); |
| 168 | ClassStructType = 0; |
| 169 | |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 170 | ObjcConstantStringType = QualType(); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 171 | |
| 172 | // void * type |
| 173 | VoidPtrTy = getPointerType(VoidTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | // Type Sizing and Analysis |
| 178 | //===----------------------------------------------------------------------===// |
| 179 | |
| 180 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 181 | /// does not work on incomplete types. |
| 182 | std::pair<uint64_t, unsigned> |
| 183 | ASTContext::getTypeInfo(QualType T, SourceLocation L) { |
| 184 | T = T.getCanonicalType(); |
| 185 | uint64_t Size; |
| 186 | unsigned Align; |
| 187 | switch (T->getTypeClass()) { |
| 188 | case Type::TypeName: assert(0 && "Not a canonical type!"); |
| 189 | case Type::FunctionNoProto: |
| 190 | case Type::FunctionProto: |
| 191 | default: |
| 192 | assert(0 && "Incomplete types have no size!"); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 193 | case Type::VariableArray: |
| 194 | assert(0 && "VLAs not implemented yet!"); |
| 195 | case Type::ConstantArray: { |
| 196 | ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
| 197 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 198 | std::pair<uint64_t, unsigned> EltInfo = |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 199 | getTypeInfo(CAT->getElementType(), L); |
| 200 | Size = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 201 | Align = EltInfo.second; |
| 202 | break; |
| 203 | } |
| 204 | case Type::Vector: { |
| 205 | std::pair<uint64_t, unsigned> EltInfo = |
| 206 | getTypeInfo(cast<VectorType>(T)->getElementType(), L); |
| 207 | Size = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
| 208 | // FIXME: Vector alignment is not the alignment of its elements. |
| 209 | Align = EltInfo.second; |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | case Type::Builtin: { |
| 214 | // FIXME: need to use TargetInfo to derive the target specific sizes. This |
| 215 | // implementation will suffice for play with vector support. |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 216 | const llvm::fltSemantics *F; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 217 | switch (cast<BuiltinType>(T)->getKind()) { |
| 218 | default: assert(0 && "Unknown builtin type!"); |
| 219 | case BuiltinType::Void: |
| 220 | assert(0 && "Incomplete types have no size!"); |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame^] | 221 | case BuiltinType::Bool: |
| 222 | Target.getBoolInfo(Size, Align, getFullLoc(L)); |
| 223 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 224 | case BuiltinType::Char_S: |
| 225 | case BuiltinType::Char_U: |
| 226 | case BuiltinType::UChar: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame^] | 227 | case BuiltinType::SChar: |
| 228 | Target.getCharInfo(Size, Align, getFullLoc(L)); |
| 229 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 230 | case BuiltinType::UShort: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame^] | 231 | case BuiltinType::Short: |
| 232 | Target.getShortInfo(Size, Align, getFullLoc(L)); |
| 233 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 234 | case BuiltinType::UInt: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame^] | 235 | case BuiltinType::Int: |
| 236 | Target.getIntInfo(Size, Align, getFullLoc(L)); |
| 237 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 238 | case BuiltinType::ULong: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame^] | 239 | case BuiltinType::Long: |
| 240 | Target.getLongInfo(Size, Align, getFullLoc(L)); |
| 241 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 242 | case BuiltinType::ULongLong: |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame^] | 243 | case BuiltinType::LongLong: |
| 244 | Target.getLongLongInfo(Size, Align, getFullLoc(L)); |
| 245 | break; |
| 246 | case BuiltinType::Float: |
| 247 | Target.getFloatInfo(Size, Align, F, getFullLoc(L)); |
| 248 | break; |
| 249 | case BuiltinType::Double: |
| 250 | Target.getDoubleInfo(Size, Align, F, getFullLoc(L)); |
| 251 | break; |
| 252 | case BuiltinType::LongDouble: |
| 253 | Target.getLongDoubleInfo(Size, Align, F, getFullLoc(L)); |
| 254 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 255 | } |
| 256 | break; |
| 257 | } |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame^] | 258 | case Type::ObjcQualifiedId: |
| 259 | Target.getPointerInfo(Size, Align, getFullLoc(L)); |
| 260 | break; |
| 261 | case Type::Pointer: |
| 262 | Target.getPointerInfo(Size, Align, getFullLoc(L)); |
| 263 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 264 | case Type::Reference: |
| 265 | // "When applied to a reference or a reference type, the result is the size |
| 266 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
Chris Lattner | b66237b | 2007-12-19 19:23:28 +0000 | [diff] [blame^] | 267 | // FIXME: This is wrong for struct layout: a reference in a struct has |
| 268 | // pointer size. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 269 | return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L); |
| 270 | |
| 271 | case Type::Complex: { |
| 272 | // Complex types have the same alignment as their elements, but twice the |
| 273 | // size. |
| 274 | std::pair<uint64_t, unsigned> EltInfo = |
| 275 | getTypeInfo(cast<ComplexType>(T)->getElementType(), L); |
| 276 | Size = EltInfo.first*2; |
| 277 | Align = EltInfo.second; |
| 278 | break; |
| 279 | } |
| 280 | case Type::Tagged: |
Chris Lattner | eb56d29 | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 281 | TagType *TT = cast<TagType>(T); |
| 282 | if (RecordType *RT = dyn_cast<RecordType>(TT)) { |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 283 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L); |
Chris Lattner | eb56d29 | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 284 | Size = Layout.getSize(); |
| 285 | Align = Layout.getAlignment(); |
| 286 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) { |
Chris Lattner | 90a018d | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 287 | return getTypeInfo(ED->getIntegerType(), L); |
Chris Lattner | eb56d29 | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 288 | } else { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 289 | assert(0 && "Unimplemented type sizes!"); |
Chris Lattner | eb56d29 | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | |
| 294 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
| 295 | return std::make_pair(Size, Align); |
| 296 | } |
| 297 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 298 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 299 | /// specified record (struct/union/class), which indicates its size and field |
| 300 | /// position information. |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 301 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D, |
| 302 | SourceLocation L) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 303 | assert(D->isDefinition() && "Cannot get layout of forward declarations!"); |
| 304 | |
| 305 | // Look up this layout, if already laid out, return what we have. |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 306 | const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 307 | if (Entry) return *Entry; |
| 308 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 309 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 310 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
| 311 | ASTRecordLayout *NewEntry = new ASTRecordLayout(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 312 | Entry = NewEntry; |
| 313 | |
| 314 | uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()]; |
| 315 | uint64_t RecordSize = 0; |
| 316 | unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits. |
| 317 | |
| 318 | if (D->getKind() != Decl::Union) { |
| 319 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 320 | // the future, this will need to be tweakable by targets. |
| 321 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 322 | const FieldDecl *FD = D->getMember(i); |
| 323 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 324 | uint64_t FieldSize = FieldInfo.first; |
| 325 | unsigned FieldAlign = FieldInfo.second; |
| 326 | |
| 327 | // Round up the current record size to the field's alignment boundary. |
| 328 | RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1); |
| 329 | |
| 330 | // Place this field at the current location. |
| 331 | FieldOffsets[i] = RecordSize; |
| 332 | |
| 333 | // Reserve space for this field. |
| 334 | RecordSize += FieldSize; |
| 335 | |
| 336 | // Remember max struct/class alignment. |
| 337 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 338 | } |
| 339 | |
| 340 | // Finally, round the size of the total struct up to the alignment of the |
| 341 | // struct itself. |
| 342 | RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1); |
| 343 | } else { |
| 344 | // Union layout just puts each member at the start of the record. |
| 345 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 346 | const FieldDecl *FD = D->getMember(i); |
| 347 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 348 | uint64_t FieldSize = FieldInfo.first; |
| 349 | unsigned FieldAlign = FieldInfo.second; |
| 350 | |
| 351 | // Round up the current record size to the field's alignment boundary. |
| 352 | RecordSize = std::max(RecordSize, FieldSize); |
| 353 | |
| 354 | // Place this field at the start of the record. |
| 355 | FieldOffsets[i] = 0; |
| 356 | |
| 357 | // Remember max struct/class alignment. |
| 358 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets); |
| 363 | return *NewEntry; |
| 364 | } |
| 365 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
| 367 | // Type creation/memoization methods |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | |
| 370 | |
| 371 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 372 | /// number with the specified element type. |
| 373 | QualType ASTContext::getComplexType(QualType T) { |
| 374 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 375 | // structure. |
| 376 | llvm::FoldingSetNodeID ID; |
| 377 | ComplexType::Profile(ID, T); |
| 378 | |
| 379 | void *InsertPos = 0; |
| 380 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 381 | return QualType(CT, 0); |
| 382 | |
| 383 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 384 | // so fill in the canonical type field. |
| 385 | QualType Canonical; |
| 386 | if (!T->isCanonical()) { |
| 387 | Canonical = getComplexType(T.getCanonicalType()); |
| 388 | |
| 389 | // Get the new insert position for the node we care about. |
| 390 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 391 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 392 | } |
| 393 | ComplexType *New = new ComplexType(T, Canonical); |
| 394 | Types.push_back(New); |
| 395 | ComplexTypes.InsertNode(New, InsertPos); |
| 396 | return QualType(New, 0); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 401 | /// the specified type. |
| 402 | QualType ASTContext::getPointerType(QualType T) { |
| 403 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 404 | // structure. |
| 405 | llvm::FoldingSetNodeID ID; |
| 406 | PointerType::Profile(ID, T); |
| 407 | |
| 408 | void *InsertPos = 0; |
| 409 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 410 | return QualType(PT, 0); |
| 411 | |
| 412 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 413 | // so fill in the canonical type field. |
| 414 | QualType Canonical; |
| 415 | if (!T->isCanonical()) { |
| 416 | Canonical = getPointerType(T.getCanonicalType()); |
| 417 | |
| 418 | // Get the new insert position for the node we care about. |
| 419 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 420 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 421 | } |
| 422 | PointerType *New = new PointerType(T, Canonical); |
| 423 | Types.push_back(New); |
| 424 | PointerTypes.InsertNode(New, InsertPos); |
| 425 | return QualType(New, 0); |
| 426 | } |
| 427 | |
| 428 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 429 | /// to the specified type. |
| 430 | QualType ASTContext::getReferenceType(QualType T) { |
| 431 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 432 | // structure. |
| 433 | llvm::FoldingSetNodeID ID; |
| 434 | ReferenceType::Profile(ID, T); |
| 435 | |
| 436 | void *InsertPos = 0; |
| 437 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 438 | return QualType(RT, 0); |
| 439 | |
| 440 | // If the referencee type isn't canonical, this won't be a canonical type |
| 441 | // either, so fill in the canonical type field. |
| 442 | QualType Canonical; |
| 443 | if (!T->isCanonical()) { |
| 444 | Canonical = getReferenceType(T.getCanonicalType()); |
| 445 | |
| 446 | // Get the new insert position for the node we care about. |
| 447 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 448 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 449 | } |
| 450 | |
| 451 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 452 | Types.push_back(New); |
| 453 | ReferenceTypes.InsertNode(New, InsertPos); |
| 454 | return QualType(New, 0); |
| 455 | } |
| 456 | |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 457 | /// getConstantArrayType - Return the unique reference to the type for an |
| 458 | /// array of the specified element type. |
| 459 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 460 | const llvm::APInt &ArySize, |
| 461 | ArrayType::ArraySizeModifier ASM, |
| 462 | unsigned EltTypeQuals) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 463 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 464 | ConstantArrayType::Profile(ID, EltTy, ArySize); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 465 | |
| 466 | void *InsertPos = 0; |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 467 | if (ConstantArrayType *ATP = |
| 468 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 469 | return QualType(ATP, 0); |
| 470 | |
| 471 | // If the element type isn't canonical, this won't be a canonical type either, |
| 472 | // so fill in the canonical type field. |
| 473 | QualType Canonical; |
| 474 | if (!EltTy->isCanonical()) { |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 475 | Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize, |
| 476 | ASM, EltTypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 477 | // Get the new insert position for the node we care about. |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 478 | ConstantArrayType *NewIP = |
| 479 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 480 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 481 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 482 | } |
| 483 | |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 484 | ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize, |
| 485 | ASM, EltTypeQuals); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 486 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 487 | Types.push_back(New); |
| 488 | return QualType(New, 0); |
| 489 | } |
| 490 | |
Steve Naroff | e2579e3 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 491 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 492 | /// variable array of the specified element type. |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 493 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
| 494 | ArrayType::ArraySizeModifier ASM, |
| 495 | unsigned EltTypeQuals) { |
Ted Kremenek | 3793e1a | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 496 | if (NumElts) { |
| 497 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 498 | // that have an expression provided for their size. |
| 499 | |
Ted Kremenek | 2058dc4 | 2007-10-30 16:41:53 +0000 | [diff] [blame] | 500 | VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 501 | ASM, EltTypeQuals); |
Ted Kremenek | 3793e1a | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | 2058dc4 | 2007-10-30 16:41:53 +0000 | [diff] [blame] | 503 | CompleteVariableArrayTypes.push_back(New); |
Ted Kremenek | 3793e1a | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 504 | Types.push_back(New); |
| 505 | return QualType(New, 0); |
| 506 | } |
| 507 | else { |
| 508 | // No size is provided for the VLA. These we can unique. |
| 509 | llvm::FoldingSetNodeID ID; |
| 510 | VariableArrayType::Profile(ID, EltTy); |
| 511 | |
| 512 | void *InsertPos = 0; |
| 513 | if (VariableArrayType *ATP = |
| 514 | IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 515 | return QualType(ATP, 0); |
| 516 | |
| 517 | // If the element type isn't canonical, this won't be a canonical type |
| 518 | // either, so fill in the canonical type field. |
| 519 | QualType Canonical; |
| 520 | |
| 521 | if (!EltTy->isCanonical()) { |
| 522 | Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts, |
| 523 | ASM, EltTypeQuals); |
| 524 | |
| 525 | // Get the new insert position for the node we care about. |
| 526 | VariableArrayType *NewIP = |
| 527 | IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 528 | |
| 529 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 530 | } |
| 531 | |
| 532 | VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 533 | ASM, EltTypeQuals); |
| 534 | |
| 535 | IncompleteVariableArrayTypes.InsertNode(New, InsertPos); |
| 536 | Types.push_back(New); |
| 537 | return QualType(New, 0); |
| 538 | } |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 541 | /// getVectorType - Return the unique reference to a vector type of |
| 542 | /// the specified element type and size. VectorType must be a built-in type. |
| 543 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
| 544 | BuiltinType *baseType; |
| 545 | |
| 546 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 547 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
| 548 | |
| 549 | // Check if we've already instantiated a vector of this type. |
| 550 | llvm::FoldingSetNodeID ID; |
| 551 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
| 552 | void *InsertPos = 0; |
| 553 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 554 | return QualType(VTP, 0); |
| 555 | |
| 556 | // If the element type isn't canonical, this won't be a canonical type either, |
| 557 | // so fill in the canonical type field. |
| 558 | QualType Canonical; |
| 559 | if (!vecType->isCanonical()) { |
| 560 | Canonical = getVectorType(vecType.getCanonicalType(), NumElts); |
| 561 | |
| 562 | // Get the new insert position for the node we care about. |
| 563 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 564 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 565 | } |
| 566 | VectorType *New = new VectorType(vecType, NumElts, Canonical); |
| 567 | VectorTypes.InsertNode(New, InsertPos); |
| 568 | Types.push_back(New); |
| 569 | return QualType(New, 0); |
| 570 | } |
| 571 | |
| 572 | /// getOCUVectorType - Return the unique reference to an OCU vector type of |
| 573 | /// the specified element type and size. VectorType must be a built-in type. |
| 574 | QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) { |
| 575 | BuiltinType *baseType; |
| 576 | |
| 577 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 578 | assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type"); |
| 579 | |
| 580 | // Check if we've already instantiated a vector of this type. |
| 581 | llvm::FoldingSetNodeID ID; |
| 582 | VectorType::Profile(ID, vecType, NumElts, Type::OCUVector); |
| 583 | void *InsertPos = 0; |
| 584 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 585 | return QualType(VTP, 0); |
| 586 | |
| 587 | // If the element type isn't canonical, this won't be a canonical type either, |
| 588 | // so fill in the canonical type field. |
| 589 | QualType Canonical; |
| 590 | if (!vecType->isCanonical()) { |
| 591 | Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts); |
| 592 | |
| 593 | // Get the new insert position for the node we care about. |
| 594 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 595 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 596 | } |
| 597 | OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical); |
| 598 | VectorTypes.InsertNode(New, InsertPos); |
| 599 | Types.push_back(New); |
| 600 | return QualType(New, 0); |
| 601 | } |
| 602 | |
| 603 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 604 | /// |
| 605 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 606 | // Unique functions, to guarantee there is only one function of a particular |
| 607 | // structure. |
| 608 | llvm::FoldingSetNodeID ID; |
| 609 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 610 | |
| 611 | void *InsertPos = 0; |
| 612 | if (FunctionTypeNoProto *FT = |
| 613 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 614 | return QualType(FT, 0); |
| 615 | |
| 616 | QualType Canonical; |
| 617 | if (!ResultTy->isCanonical()) { |
| 618 | Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType()); |
| 619 | |
| 620 | // Get the new insert position for the node we care about. |
| 621 | FunctionTypeNoProto *NewIP = |
| 622 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 623 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 624 | } |
| 625 | |
| 626 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 627 | Types.push_back(New); |
| 628 | FunctionTypeProtos.InsertNode(New, InsertPos); |
| 629 | return QualType(New, 0); |
| 630 | } |
| 631 | |
| 632 | /// getFunctionType - Return a normal function type with a typed argument |
| 633 | /// list. isVariadic indicates whether the argument list includes '...'. |
| 634 | QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray, |
| 635 | unsigned NumArgs, bool isVariadic) { |
| 636 | // Unique functions, to guarantee there is only one function of a particular |
| 637 | // structure. |
| 638 | llvm::FoldingSetNodeID ID; |
| 639 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic); |
| 640 | |
| 641 | void *InsertPos = 0; |
| 642 | if (FunctionTypeProto *FTP = |
| 643 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 644 | return QualType(FTP, 0); |
| 645 | |
| 646 | // Determine whether the type being created is already canonical or not. |
| 647 | bool isCanonical = ResultTy->isCanonical(); |
| 648 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 649 | if (!ArgArray[i]->isCanonical()) |
| 650 | isCanonical = false; |
| 651 | |
| 652 | // If this type isn't canonical, get the canonical version of it. |
| 653 | QualType Canonical; |
| 654 | if (!isCanonical) { |
| 655 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 656 | CanonicalArgs.reserve(NumArgs); |
| 657 | for (unsigned i = 0; i != NumArgs; ++i) |
| 658 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 659 | |
| 660 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 661 | &CanonicalArgs[0], NumArgs, |
| 662 | isVariadic); |
| 663 | |
| 664 | // Get the new insert position for the node we care about. |
| 665 | FunctionTypeProto *NewIP = |
| 666 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 667 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 668 | } |
| 669 | |
| 670 | // FunctionTypeProto objects are not allocated with new because they have a |
| 671 | // variable size array (for parameter types) at the end of them. |
| 672 | FunctionTypeProto *FTP = |
| 673 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
| 674 | NumArgs*sizeof(QualType)); |
| 675 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 676 | Canonical); |
| 677 | Types.push_back(FTP); |
| 678 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 679 | return QualType(FTP, 0); |
| 680 | } |
| 681 | |
| 682 | /// getTypedefType - Return the unique reference to the type for the |
| 683 | /// specified typename decl. |
| 684 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 685 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 686 | |
| 687 | QualType Canonical = Decl->getUnderlyingType().getCanonicalType(); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 688 | Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 689 | Types.push_back(Decl->TypeForDecl); |
| 690 | return QualType(Decl->TypeForDecl, 0); |
| 691 | } |
| 692 | |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 693 | /// getObjcInterfaceType - Return the unique reference to the type for the |
| 694 | /// specified ObjC interface decl. |
| 695 | QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) { |
| 696 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 697 | |
Fariborz Jahanian | 0c2f214 | 2007-12-13 20:47:42 +0000 | [diff] [blame] | 698 | Decl->TypeForDecl = new ObjcInterfaceType(Type::ObjcInterface, Decl); |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 699 | Types.push_back(Decl->TypeForDecl); |
| 700 | return QualType(Decl->TypeForDecl, 0); |
| 701 | } |
| 702 | |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 703 | /// getObjcQualifiedInterfaceType - Return a |
| 704 | /// ObjcQualifiedInterfaceType type for the given interface decl and |
| 705 | /// the conforming protocol list. |
| 706 | QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl, |
| 707 | ObjcProtocolDecl **Protocols, unsigned NumProtocols) { |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 708 | llvm::FoldingSetNodeID ID; |
Fariborz Jahanian | 0c2f214 | 2007-12-13 20:47:42 +0000 | [diff] [blame] | 709 | ObjcQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 710 | |
| 711 | void *InsertPos = 0; |
| 712 | if (ObjcQualifiedInterfaceType *QT = |
| 713 | ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 714 | return QualType(QT, 0); |
| 715 | |
| 716 | // No Match; |
Chris Lattner | d855a6e | 2007-10-11 03:36:41 +0000 | [diff] [blame] | 717 | ObjcQualifiedInterfaceType *QType = |
Fariborz Jahanian | 0c2f214 | 2007-12-13 20:47:42 +0000 | [diff] [blame] | 718 | new ObjcQualifiedInterfaceType(Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 719 | Types.push_back(QType); |
| 720 | ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
| 721 | return QualType(QType, 0); |
| 722 | } |
| 723 | |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 724 | /// getObjcQualifiedIdType - Return a |
Fariborz Jahanian | 2a0e543 | 2007-12-17 21:48:49 +0000 | [diff] [blame] | 725 | /// getObjcQualifiedIdType type for the 'id' decl and |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 726 | /// the conforming protocol list. |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 727 | QualType ASTContext::getObjcQualifiedIdType(QualType idType, |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 728 | ObjcProtocolDecl **Protocols, |
| 729 | unsigned NumProtocols) { |
| 730 | llvm::FoldingSetNodeID ID; |
| 731 | ObjcQualifiedIdType::Profile(ID, Protocols, NumProtocols); |
| 732 | |
| 733 | void *InsertPos = 0; |
| 734 | if (ObjcQualifiedIdType *QT = |
| 735 | ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 736 | return QualType(QT, 0); |
| 737 | |
| 738 | // No Match; |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 739 | QualType Canonical; |
| 740 | if (!idType->isCanonical()) { |
| 741 | Canonical = getObjcQualifiedIdType(idType.getCanonicalType(), |
| 742 | Protocols, NumProtocols); |
| 743 | ObjcQualifiedIdType *NewQT = |
| 744 | ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 745 | assert(NewQT == 0 && "Shouldn't be in the map!"); |
| 746 | } |
| 747 | |
| 748 | ObjcQualifiedIdType *QType = |
| 749 | new ObjcQualifiedIdType(Canonical, Protocols, NumProtocols); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 750 | Types.push_back(QType); |
| 751 | ObjcQualifiedIdTypes.InsertNode(QType, InsertPos); |
| 752 | return QualType(QType, 0); |
| 753 | } |
| 754 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 755 | /// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique |
| 756 | /// TypeOfExpr AST's (since expression's are never shared). For example, |
| 757 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 758 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 759 | /// on canonical type's (which are always unique). |
Steve Naroff | 11b649c | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 760 | QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 761 | QualType Canonical = tofExpr->getType().getCanonicalType(); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 762 | TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical); |
| 763 | Types.push_back(toe); |
| 764 | return QualType(toe, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 767 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 768 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 769 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 770 | /// an issue. This doesn't effect the type checker, since it operates |
| 771 | /// on canonical type's (which are always unique). |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 772 | QualType ASTContext::getTypeOfType(QualType tofType) { |
| 773 | QualType Canonical = tofType.getCanonicalType(); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 774 | TypeOfType *tot = new TypeOfType(tofType, Canonical); |
| 775 | Types.push_back(tot); |
| 776 | return QualType(tot, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 779 | /// getTagDeclType - Return the unique reference to the type for the |
| 780 | /// specified TagDecl (struct/union/class/enum) decl. |
| 781 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
Ted Kremenek | ae8fa03 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 782 | assert (Decl); |
| 783 | |
Ted Kremenek | f05026d | 2007-11-14 00:03:20 +0000 | [diff] [blame] | 784 | // The decl stores the type cache. |
Ted Kremenek | ae8fa03 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 785 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Ted Kremenek | f05026d | 2007-11-14 00:03:20 +0000 | [diff] [blame] | 786 | |
| 787 | TagType* T = new TagType(Decl, QualType()); |
Ted Kremenek | ae8fa03 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 788 | Types.push_back(T); |
| 789 | Decl->TypeForDecl = T; |
Ted Kremenek | f05026d | 2007-11-14 00:03:20 +0000 | [diff] [blame] | 790 | |
| 791 | return QualType(T, 0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 795 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 796 | /// needs to agree with the definition in <stddef.h>. |
| 797 | QualType ASTContext::getSizeType() const { |
| 798 | // On Darwin, size_t is defined as a "long unsigned int". |
| 799 | // FIXME: should derive from "Target". |
| 800 | return UnsignedLongTy; |
| 801 | } |
| 802 | |
| 803 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 804 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 805 | QualType ASTContext::getPointerDiffType() const { |
| 806 | // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug... |
| 807 | // FIXME: should derive from "Target". |
| 808 | return IntTy; |
| 809 | } |
| 810 | |
| 811 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 812 | /// routine will assert if passed a built-in type that isn't an integer or enum. |
| 813 | static int getIntegerRank(QualType t) { |
| 814 | if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) { |
| 815 | assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum"); |
| 816 | return 4; |
| 817 | } |
| 818 | |
| 819 | const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType()); |
| 820 | switch (BT->getKind()) { |
| 821 | default: |
| 822 | assert(0 && "getIntegerRank(): not a built-in integer"); |
| 823 | case BuiltinType::Bool: |
| 824 | return 1; |
| 825 | case BuiltinType::Char_S: |
| 826 | case BuiltinType::Char_U: |
| 827 | case BuiltinType::SChar: |
| 828 | case BuiltinType::UChar: |
| 829 | return 2; |
| 830 | case BuiltinType::Short: |
| 831 | case BuiltinType::UShort: |
| 832 | return 3; |
| 833 | case BuiltinType::Int: |
| 834 | case BuiltinType::UInt: |
| 835 | return 4; |
| 836 | case BuiltinType::Long: |
| 837 | case BuiltinType::ULong: |
| 838 | return 5; |
| 839 | case BuiltinType::LongLong: |
| 840 | case BuiltinType::ULongLong: |
| 841 | return 6; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /// getFloatingRank - Return a relative rank for floating point types. |
| 846 | /// This routine will assert if passed a built-in type that isn't a float. |
| 847 | static int getFloatingRank(QualType T) { |
| 848 | T = T.getCanonicalType(); |
| 849 | if (ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 850 | return getFloatingRank(CT->getElementType()); |
| 851 | |
| 852 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 5003e8b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 853 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 854 | case BuiltinType::Float: return FloatRank; |
| 855 | case BuiltinType::Double: return DoubleRank; |
| 856 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 857 | } |
| 858 | } |
| 859 | |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 860 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 861 | /// point or a complex type (based on typeDomain/typeSize). |
| 862 | /// 'typeDomain' is a real floating point or complex type. |
| 863 | /// 'typeSize' is a real floating point or complex type. |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 864 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain( |
| 865 | QualType typeSize, QualType typeDomain) const { |
| 866 | if (typeDomain->isComplexType()) { |
| 867 | switch (getFloatingRank(typeSize)) { |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 868 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 869 | case FloatRank: return FloatComplexTy; |
| 870 | case DoubleRank: return DoubleComplexTy; |
| 871 | case LongDoubleRank: return LongDoubleComplexTy; |
| 872 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 873 | } |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 874 | if (typeDomain->isRealFloatingType()) { |
| 875 | switch (getFloatingRank(typeSize)) { |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 876 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 877 | case FloatRank: return FloatTy; |
| 878 | case DoubleRank: return DoubleTy; |
| 879 | case LongDoubleRank: return LongDoubleTy; |
| 880 | } |
| 881 | } |
| 882 | assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain"); |
Chris Lattner | 1d2b461 | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 883 | //an invalid return value, but the assert |
| 884 | //will ensure that this code is never reached. |
| 885 | return VoidTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 888 | /// compareFloatingType - Handles 3 different combos: |
| 889 | /// float/float, float/complex, complex/complex. |
| 890 | /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. |
| 891 | int ASTContext::compareFloatingType(QualType lt, QualType rt) { |
| 892 | if (getFloatingRank(lt) == getFloatingRank(rt)) |
| 893 | return 0; |
| 894 | if (getFloatingRank(lt) > getFloatingRank(rt)) |
| 895 | return 1; |
| 896 | return -1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | // maxIntegerType - Returns the highest ranked integer type. Handles 3 case: |
| 900 | // unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1. |
| 901 | QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) { |
| 902 | if (lhs == rhs) return lhs; |
| 903 | |
| 904 | bool t1Unsigned = lhs->isUnsignedIntegerType(); |
| 905 | bool t2Unsigned = rhs->isUnsignedIntegerType(); |
| 906 | |
| 907 | if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned)) |
| 908 | return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs; |
| 909 | |
| 910 | // We have two integer types with differing signs |
| 911 | QualType unsignedType = t1Unsigned ? lhs : rhs; |
| 912 | QualType signedType = t1Unsigned ? rhs : lhs; |
| 913 | |
| 914 | if (getIntegerRank(unsignedType) >= getIntegerRank(signedType)) |
| 915 | return unsignedType; |
| 916 | else { |
| 917 | // FIXME: Need to check if the signed type can represent all values of the |
| 918 | // unsigned type. If it can, then the result is the signed type. |
| 919 | // If it can't, then the result is the unsigned version of the signed type. |
| 920 | // Should probably add a helper that returns a signed integer type from |
| 921 | // an unsigned (and vice versa). C99 6.3.1.8. |
| 922 | return signedType; |
| 923 | } |
| 924 | } |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 925 | |
| 926 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 927 | QualType ASTContext::getCFConstantStringType() { |
| 928 | if (!CFConstantStringTypeDecl) { |
| 929 | CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(), |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 930 | &Idents.get("NSConstantString"), |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 931 | 0); |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 932 | QualType FieldTypes[4]; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 933 | |
| 934 | // const int *isa; |
| 935 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 936 | // int flags; |
| 937 | FieldTypes[1] = IntTy; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 938 | // const char *str; |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 939 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 940 | // long length; |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 941 | FieldTypes[3] = LongTy; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 942 | // Create fields |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 943 | FieldDecl *FieldDecls[4]; |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 944 | |
Anders Carlsson | bb2cf51 | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 945 | for (unsigned i = 0; i < 4; ++i) |
Steve Naroff | dc1ad76 | 2007-09-14 02:20:46 +0000 | [diff] [blame] | 946 | FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]); |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 947 | |
| 948 | CFConstantStringTypeDecl->defineBody(FieldDecls, 4); |
| 949 | } |
| 950 | |
| 951 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 61ce98c | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 952 | } |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 953 | |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 954 | // This returns true if a type has been typedefed to BOOL: |
| 955 | // typedef <type> BOOL; |
Chris Lattner | cb034cb | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 956 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 957 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | cb034cb | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 958 | return !strcmp(TT->getDecl()->getName(), "BOOL"); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 959 | |
| 960 | return false; |
| 961 | } |
| 962 | |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 963 | /// getObjcEncodingTypeSize returns size of type for objective-c encoding |
| 964 | /// purpose. |
| 965 | int ASTContext::getObjcEncodingTypeSize(QualType type) { |
| 966 | SourceLocation Loc; |
| 967 | uint64_t sz = getTypeSize(type, Loc); |
| 968 | |
| 969 | // Make all integer and enum types at least as large as an int |
| 970 | if (sz > 0 && type->isIntegralType()) |
| 971 | sz = std::max(sz, getTypeSize(IntTy, Loc)); |
| 972 | // Treat arrays as pointers, since that's how they're passed in. |
| 973 | else if (type->isArrayType()) |
| 974 | sz = getTypeSize(VoidPtrTy, Loc); |
| 975 | return sz / getTypeSize(CharTy, Loc); |
| 976 | } |
| 977 | |
| 978 | /// getObjcEncodingForMethodDecl - Return the encoded type for this method |
| 979 | /// declaration. |
| 980 | void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, |
| 981 | std::string& S) |
| 982 | { |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 983 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
| 984 | getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 985 | // Encode result type. |
| 986 | getObjcEncodingForType(Decl->getResultType(), S); |
| 987 | // Compute size of all parameters. |
| 988 | // Start with computing size of a pointer in number of bytes. |
| 989 | // FIXME: There might(should) be a better way of doing this computation! |
| 990 | SourceLocation Loc; |
| 991 | int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc); |
| 992 | // The first two arguments (self and _cmd) are pointers; account for |
| 993 | // their size. |
| 994 | int ParmOffset = 2 * PtrSize; |
| 995 | int NumOfParams = Decl->getNumParams(); |
| 996 | for (int i = 0; i < NumOfParams; i++) { |
| 997 | QualType PType = Decl->getParamDecl(i)->getType(); |
| 998 | int sz = getObjcEncodingTypeSize (PType); |
| 999 | assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type"); |
| 1000 | ParmOffset += sz; |
| 1001 | } |
| 1002 | S += llvm::utostr(ParmOffset); |
| 1003 | S += "@0:"; |
| 1004 | S += llvm::utostr(PtrSize); |
| 1005 | |
| 1006 | // Argument types. |
| 1007 | ParmOffset = 2 * PtrSize; |
| 1008 | for (int i = 0; i < NumOfParams; i++) { |
| 1009 | QualType PType = Decl->getParamDecl(i)->getType(); |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1010 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1011 | // 'in', 'inout', etc. |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1012 | getObjcEncodingForTypeQualifier( |
| 1013 | Decl->getParamDecl(i)->getObjcDeclQualifier(), S); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1014 | getObjcEncodingForType(PType, S); |
| 1015 | S += llvm::utostr(ParmOffset); |
| 1016 | ParmOffset += getObjcEncodingTypeSize(PType); |
| 1017 | } |
| 1018 | } |
| 1019 | |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1020 | void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const |
| 1021 | { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1022 | // FIXME: This currently doesn't encode: |
| 1023 | // @ An object (whether statically typed or typed id) |
| 1024 | // # A class object (Class) |
| 1025 | // : A method selector (SEL) |
| 1026 | // {name=type...} A structure |
| 1027 | // (name=type...) A union |
| 1028 | // bnum A bit field of num bits |
| 1029 | |
| 1030 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1031 | char encoding; |
| 1032 | switch (BT->getKind()) { |
| 1033 | case BuiltinType::Void: |
| 1034 | encoding = 'v'; |
| 1035 | break; |
| 1036 | case BuiltinType::Bool: |
| 1037 | encoding = 'B'; |
| 1038 | break; |
| 1039 | case BuiltinType::Char_U: |
| 1040 | case BuiltinType::UChar: |
| 1041 | encoding = 'C'; |
| 1042 | break; |
| 1043 | case BuiltinType::UShort: |
| 1044 | encoding = 'S'; |
| 1045 | break; |
| 1046 | case BuiltinType::UInt: |
| 1047 | encoding = 'I'; |
| 1048 | break; |
| 1049 | case BuiltinType::ULong: |
| 1050 | encoding = 'L'; |
| 1051 | break; |
| 1052 | case BuiltinType::ULongLong: |
| 1053 | encoding = 'Q'; |
| 1054 | break; |
| 1055 | case BuiltinType::Char_S: |
| 1056 | case BuiltinType::SChar: |
| 1057 | encoding = 'c'; |
| 1058 | break; |
| 1059 | case BuiltinType::Short: |
| 1060 | encoding = 's'; |
| 1061 | break; |
| 1062 | case BuiltinType::Int: |
| 1063 | encoding = 'i'; |
| 1064 | break; |
| 1065 | case BuiltinType::Long: |
| 1066 | encoding = 'l'; |
| 1067 | break; |
| 1068 | case BuiltinType::LongLong: |
| 1069 | encoding = 'q'; |
| 1070 | break; |
| 1071 | case BuiltinType::Float: |
| 1072 | encoding = 'f'; |
| 1073 | break; |
| 1074 | case BuiltinType::Double: |
| 1075 | encoding = 'd'; |
| 1076 | break; |
| 1077 | case BuiltinType::LongDouble: |
| 1078 | encoding = 'd'; |
| 1079 | break; |
| 1080 | default: |
| 1081 | assert(0 && "Unhandled builtin type kind"); |
| 1082 | } |
| 1083 | |
| 1084 | S += encoding; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1085 | } |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 1086 | else if (T->isObjcQualifiedIdType()) { |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1087 | // Treat id<P...> same as 'id' for encoding purposes. |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 1088 | return getObjcEncodingForType(getObjcIdType(), S); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1089 | |
| 1090 | } |
| 1091 | else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1092 | QualType PointeeTy = PT->getPointeeType(); |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1093 | if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) { |
Fariborz Jahanian | 80faffa | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1094 | S += '@'; |
| 1095 | return; |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1096 | } else if (isObjcClassType(PointeeTy)) { |
| 1097 | S += '#'; |
| 1098 | return; |
| 1099 | } else if (isObjcSelType(PointeeTy)) { |
| 1100 | S += ':'; |
| 1101 | return; |
Fariborz Jahanian | 80faffa | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1102 | } |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (PointeeTy->isCharType()) { |
| 1105 | // char pointer types should be encoded as '*' unless it is a |
| 1106 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1107 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1108 | S += '*'; |
| 1109 | return; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | S += '^'; |
| 1114 | getObjcEncodingForType(PT->getPointeeType(), S); |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1115 | } else if (const ArrayType *AT = T->getAsArrayType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1116 | S += '['; |
| 1117 | |
| 1118 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1119 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 1120 | else |
| 1121 | assert(0 && "Unhandled array type!"); |
| 1122 | |
| 1123 | getObjcEncodingForType(AT->getElementType(), S); |
| 1124 | S += ']'; |
Anders Carlsson | 5695bb7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 1125 | } else if (T->getAsFunctionType()) { |
| 1126 | S += '?'; |
Fariborz Jahanian | c8ba2bd | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 1127 | } else if (const RecordType *RTy = T->getAsRecordType()) { |
| 1128 | RecordDecl *RDecl= RTy->getDecl(); |
| 1129 | S += '{'; |
| 1130 | S += RDecl->getName(); |
| 1131 | S += '='; |
| 1132 | for (int i = 0; i < RDecl->getNumMembers(); i++) { |
| 1133 | FieldDecl *field = RDecl->getMember(i); |
| 1134 | getObjcEncodingForType(field->getType(), S); |
| 1135 | } |
| 1136 | S += '}'; |
Steve Naroff | 49af3f3 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 1137 | } else if (T->isEnumeralType()) { |
| 1138 | S += 'i'; |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1139 | } else |
Steve Naroff | 49af3f3 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 1140 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Fariborz Jahanian | 65e7eb5 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1143 | void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT, |
| 1144 | std::string& S) const { |
| 1145 | if (QT & Decl::OBJC_TQ_In) |
| 1146 | S += 'n'; |
| 1147 | if (QT & Decl::OBJC_TQ_Inout) |
| 1148 | S += 'N'; |
| 1149 | if (QT & Decl::OBJC_TQ_Out) |
| 1150 | S += 'o'; |
| 1151 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 1152 | S += 'O'; |
| 1153 | if (QT & Decl::OBJC_TQ_Byref) |
| 1154 | S += 'R'; |
| 1155 | if (QT & Decl::OBJC_TQ_Oneway) |
| 1156 | S += 'V'; |
| 1157 | } |
| 1158 | |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1159 | void ASTContext::setBuiltinVaListType(QualType T) |
| 1160 | { |
| 1161 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 1162 | |
| 1163 | BuiltinVaListType = T; |
| 1164 | } |
| 1165 | |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1166 | void ASTContext::setObjcIdType(TypedefDecl *TD) |
| 1167 | { |
| 1168 | assert(ObjcIdType.isNull() && "'id' type already set!"); |
| 1169 | |
| 1170 | ObjcIdType = getTypedefType(TD); |
| 1171 | |
| 1172 | // typedef struct objc_object *id; |
| 1173 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1174 | assert(ptr && "'id' incorrectly typed"); |
| 1175 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1176 | assert(rec && "'id' incorrectly typed"); |
| 1177 | IdStructType = rec; |
| 1178 | } |
| 1179 | |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1180 | void ASTContext::setObjcSelType(TypedefDecl *TD) |
| 1181 | { |
| 1182 | assert(ObjcSelType.isNull() && "'SEL' type already set!"); |
| 1183 | |
| 1184 | ObjcSelType = getTypedefType(TD); |
| 1185 | |
| 1186 | // typedef struct objc_selector *SEL; |
| 1187 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1188 | assert(ptr && "'SEL' incorrectly typed"); |
| 1189 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1190 | assert(rec && "'SEL' incorrectly typed"); |
| 1191 | SelStructType = rec; |
| 1192 | } |
| 1193 | |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 1194 | void ASTContext::setObjcProtoType(QualType QT) |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1195 | { |
| 1196 | assert(ObjcProtoType.isNull() && "'Protocol' type already set!"); |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 1197 | ObjcProtoType = QT; |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Anders Carlsson | 7f23e3d | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1200 | void ASTContext::setObjcClassType(TypedefDecl *TD) |
| 1201 | { |
| 1202 | assert(ObjcClassType.isNull() && "'Class' type already set!"); |
| 1203 | |
| 1204 | ObjcClassType = getTypedefType(TD); |
| 1205 | |
| 1206 | // typedef struct objc_class *Class; |
| 1207 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1208 | assert(ptr && "'Class' incorrectly typed"); |
| 1209 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1210 | assert(rec && "'Class' incorrectly typed"); |
| 1211 | ClassStructType = rec; |
| 1212 | } |
| 1213 | |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1214 | void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) { |
| 1215 | assert(ObjcConstantStringType.isNull() && |
| 1216 | "'NSConstantString' type already set!"); |
| 1217 | |
| 1218 | ObjcConstantStringType = getObjcInterfaceType(Decl); |
| 1219 | } |
| 1220 | |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1221 | bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1222 | const BuiltinType *lBuiltin = lhs->getAsBuiltinType(); |
| 1223 | const BuiltinType *rBuiltin = rhs->getAsBuiltinType(); |
| 1224 | |
| 1225 | return lBuiltin->getKind() == rBuiltin->getKind(); |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1230 | if (lhs->isObjcInterfaceType() && isObjcIdType(rhs)) |
| 1231 | return true; |
| 1232 | else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType()) |
| 1233 | return true; |
| 1234 | return false; |
| 1235 | } |
| 1236 | |
| 1237 | bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1238 | return true; // FIXME: IMPLEMENT. |
| 1239 | } |
| 1240 | |
Fariborz Jahanian | 12519d4 | 2007-12-12 01:00:23 +0000 | [diff] [blame] | 1241 | bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs, |
| 1242 | QualType rhs) { |
| 1243 | ObjcQualifiedInterfaceType *lhsQI = |
| 1244 | dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr()); |
| 1245 | assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type"); |
| 1246 | ObjcQualifiedInterfaceType *rhsQI = |
| 1247 | dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr()); |
| 1248 | assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type"); |
Fariborz Jahanian | 0c2f214 | 2007-12-13 20:47:42 +0000 | [diff] [blame] | 1249 | if (!interfaceTypesAreCompatible(getObjcInterfaceType(lhsQI->getDecl()), |
| 1250 | getObjcInterfaceType(rhsQI->getDecl()))) |
Fariborz Jahanian | 12519d4 | 2007-12-12 01:00:23 +0000 | [diff] [blame] | 1251 | return false; |
| 1252 | /* All protocols in lhs must have a presense in rhs. */ |
| 1253 | for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) { |
| 1254 | bool match = false; |
| 1255 | ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i); |
| 1256 | for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) { |
| 1257 | ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j); |
| 1258 | if (lhsProto == rhsProto) { |
| 1259 | match = true; |
| 1260 | break; |
| 1261 | } |
| 1262 | } |
| 1263 | if (!match) |
| 1264 | return false; |
| 1265 | } |
| 1266 | return true; |
| 1267 | } |
| 1268 | |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1269 | /// ObjcQualifiedIdTypesAreCompatible - Compares two types, at least |
| 1270 | /// one of which is a protocol qualified 'id' type. |
| 1271 | bool ASTContext::ObjcQualifiedIdTypesAreCompatible(QualType lhs, |
| 1272 | QualType rhs) { |
| 1273 | // match id<P..> with an 'id' type in all cases. |
| 1274 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 1275 | QualType PointeeTy = PT->getPointeeType(); |
| 1276 | if (isObjcIdType(PointeeTy)) |
| 1277 | return true; |
| 1278 | |
| 1279 | } |
| 1280 | else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 1281 | QualType PointeeTy = PT->getPointeeType(); |
| 1282 | if (isObjcIdType(PointeeTy)) |
| 1283 | return true; |
| 1284 | |
| 1285 | } |
| 1286 | |
| 1287 | ObjcQualifiedInterfaceType *lhsQI = 0; |
| 1288 | ObjcQualifiedInterfaceType *rhsQI = 0; |
| 1289 | ObjcQualifiedIdType *lhsQID = dyn_cast<ObjcQualifiedIdType>(lhs); |
| 1290 | ObjcQualifiedIdType *rhsQID = dyn_cast<ObjcQualifiedIdType>(rhs); |
| 1291 | |
| 1292 | if (lhsQID) { |
| 1293 | if (!rhsQID && rhs->getTypeClass() == Type::Pointer) { |
| 1294 | QualType rtype = |
| 1295 | cast<PointerType>(rhs.getCanonicalType())->getPointeeType(); |
| 1296 | rhsQI = |
| 1297 | dyn_cast<ObjcQualifiedInterfaceType>( |
| 1298 | rtype.getCanonicalType().getTypePtr()); |
| 1299 | } |
| 1300 | if (!rhsQI && !rhsQID) |
| 1301 | return false; |
| 1302 | |
| 1303 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
| 1304 | bool match = false; |
| 1305 | ObjcProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
| 1306 | unsigned numRhsProtocols; |
| 1307 | ObjcProtocolDecl **rhsProtoList; |
| 1308 | if (rhsQI) { |
| 1309 | numRhsProtocols = rhsQI->getNumProtocols(); |
| 1310 | rhsProtoList = rhsQI->getReferencedProtocols(); |
| 1311 | } |
| 1312 | else { |
| 1313 | numRhsProtocols = rhsQID->getNumProtocols(); |
| 1314 | rhsProtoList = rhsQID->getReferencedProtocols(); |
| 1315 | } |
| 1316 | for (unsigned j = 0; j < numRhsProtocols; j++) { |
| 1317 | ObjcProtocolDecl *rhsProto = rhsProtoList[j]; |
| 1318 | if (lhsProto == rhsProto) { |
| 1319 | match = true; |
| 1320 | break; |
| 1321 | } |
| 1322 | } |
| 1323 | if (!match) |
| 1324 | return false; |
| 1325 | } |
| 1326 | } |
| 1327 | else if (rhsQID) { |
| 1328 | if (!lhsQID && lhs->getTypeClass() == Type::Pointer) { |
| 1329 | QualType ltype = |
| 1330 | cast<PointerType>(lhs.getCanonicalType())->getPointeeType(); |
| 1331 | lhsQI = |
| 1332 | dyn_cast<ObjcQualifiedInterfaceType>( |
| 1333 | ltype.getCanonicalType().getTypePtr()); |
| 1334 | } |
| 1335 | if (!lhsQI && !lhsQID) |
| 1336 | return false; |
| 1337 | unsigned numLhsProtocols; |
| 1338 | ObjcProtocolDecl **lhsProtoList; |
| 1339 | if (lhsQI) { |
| 1340 | numLhsProtocols = lhsQI->getNumProtocols(); |
| 1341 | lhsProtoList = lhsQI->getReferencedProtocols(); |
| 1342 | } |
| 1343 | else { |
| 1344 | numLhsProtocols = lhsQID->getNumProtocols(); |
| 1345 | lhsProtoList = lhsQID->getReferencedProtocols(); |
| 1346 | } |
| 1347 | for (unsigned i =0; i < numLhsProtocols; i++) { |
| 1348 | bool match = false; |
| 1349 | ObjcProtocolDecl *lhsProto = lhsProtoList[i]; |
| 1350 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 1351 | ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 1352 | if (lhsProto == rhsProto) { |
| 1353 | match = true; |
| 1354 | break; |
| 1355 | } |
| 1356 | } |
| 1357 | if (!match) |
| 1358 | return false; |
| 1359 | } |
| 1360 | } |
| 1361 | return true; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1362 | } |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | 5003e8b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1364 | bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1365 | const VectorType *lVector = lhs->getAsVectorType(); |
| 1366 | const VectorType *rVector = rhs->getAsVectorType(); |
| 1367 | |
| 1368 | if ((lVector->getElementType().getCanonicalType() == |
| 1369 | rVector->getElementType().getCanonicalType()) && |
| 1370 | (lVector->getNumElements() == rVector->getNumElements())) |
| 1371 | return true; |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1375 | // C99 6.2.7p1: If both are complete types, then the following additional |
| 1376 | // requirements apply...FIXME (handle compatibility across source files). |
| 1377 | bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1378 | TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl(); |
| 1379 | TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl(); |
| 1380 | |
| 1381 | if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) { |
| 1382 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1383 | return true; |
| 1384 | } |
| 1385 | if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) { |
| 1386 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1387 | return true; |
| 1388 | } |
Steve Naroff | 4a5e207 | 2007-11-07 06:03:51 +0000 | [diff] [blame] | 1389 | // "Class" and "id" are compatible built-in structure types. |
| 1390 | if (isObjcIdType(lhs) && isObjcClassType(rhs) || |
| 1391 | isObjcClassType(lhs) && isObjcIdType(rhs)) |
| 1392 | return true; |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1393 | return false; |
| 1394 | } |
| 1395 | |
| 1396 | bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1397 | // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be |
| 1398 | // identically qualified and both shall be pointers to compatible types. |
| 1399 | if (lhs.getQualifiers() != rhs.getQualifiers()) |
| 1400 | return false; |
| 1401 | |
| 1402 | QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType(); |
| 1403 | QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType(); |
| 1404 | |
| 1405 | return typesAreCompatible(ltype, rtype); |
| 1406 | } |
| 1407 | |
Bill Wendling | 6a9d854 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 1408 | // C++ 5.17p6: When the left operand of an assignment operator denotes a |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1409 | // reference to T, the operation assigns to the object of type T denoted by the |
| 1410 | // reference. |
| 1411 | bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1412 | QualType ltype = lhs; |
| 1413 | |
| 1414 | if (lhs->isReferenceType()) |
| 1415 | ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType(); |
| 1416 | |
| 1417 | QualType rtype = rhs; |
| 1418 | |
| 1419 | if (rhs->isReferenceType()) |
| 1420 | rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType(); |
| 1421 | |
| 1422 | return typesAreCompatible(ltype, rtype); |
| 1423 | } |
| 1424 | |
| 1425 | bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1426 | const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType()); |
| 1427 | const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType()); |
| 1428 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 1429 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 1430 | |
| 1431 | // first check the return types (common between C99 and K&R). |
| 1432 | if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType())) |
| 1433 | return false; |
| 1434 | |
| 1435 | if (lproto && rproto) { // two C99 style function prototypes |
| 1436 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 1437 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 1438 | |
| 1439 | if (lproto_nargs != rproto_nargs) |
| 1440 | return false; |
| 1441 | |
| 1442 | // both prototypes have the same number of arguments. |
| 1443 | if ((lproto->isVariadic() && !rproto->isVariadic()) || |
| 1444 | (rproto->isVariadic() && !lproto->isVariadic())) |
| 1445 | return false; |
| 1446 | |
| 1447 | // The use of ellipsis agree...now check the argument types. |
| 1448 | for (unsigned i = 0; i < lproto_nargs; i++) |
| 1449 | if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i))) |
| 1450 | return false; |
| 1451 | return true; |
| 1452 | } |
| 1453 | if (!lproto && !rproto) // two K&R style function decls, nothing to do. |
| 1454 | return true; |
| 1455 | |
| 1456 | // we have a mixture of K&R style with C99 prototypes |
| 1457 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 1458 | |
| 1459 | if (proto->isVariadic()) |
| 1460 | return false; |
| 1461 | |
| 1462 | // FIXME: Each parameter type T in the prototype must be compatible with the |
| 1463 | // type resulting from applying the usual argument conversions to T. |
| 1464 | return true; |
| 1465 | } |
| 1466 | |
| 1467 | bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1468 | QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType(); |
| 1469 | QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType(); |
| 1470 | |
| 1471 | if (!typesAreCompatible(ltype, rtype)) |
| 1472 | return false; |
| 1473 | |
| 1474 | // FIXME: If both types specify constant sizes, then the sizes must also be |
| 1475 | // the same. Even if the sizes are the same, GCC produces an error. |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
| 1479 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 1480 | /// both shall have the identically qualified version of a compatible type. |
| 1481 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 1482 | /// same. See 6.7.[2,3,5] for additional rules. |
| 1483 | bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) { |
| 1484 | QualType lcanon = lhs.getCanonicalType(); |
| 1485 | QualType rcanon = rhs.getCanonicalType(); |
| 1486 | |
| 1487 | // If two types are identical, they are are compatible |
| 1488 | if (lcanon == rcanon) |
| 1489 | return true; |
Bill Wendling | 6a9d854 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 1490 | |
| 1491 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 1492 | // type is adjusted to "T" prior to any further analysis, the expression |
| 1493 | // designates the object or function denoted by the reference, and the |
| 1494 | // expression is an lvalue. |
| 1495 | if (lcanon->getTypeClass() == Type::Reference) |
| 1496 | lcanon = cast<ReferenceType>(lcanon)->getReferenceeType(); |
| 1497 | if (rcanon->getTypeClass() == Type::Reference) |
| 1498 | rcanon = cast<ReferenceType>(rcanon)->getReferenceeType(); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1499 | |
| 1500 | // If the canonical type classes don't match, they can't be compatible |
| 1501 | if (lcanon->getTypeClass() != rcanon->getTypeClass()) { |
| 1502 | // For Objective-C, it is possible for two types to be compatible |
| 1503 | // when their classes don't match (when dealing with "id"). If either type |
| 1504 | // is an interface, we defer to objcTypesAreCompatible(). |
| 1505 | if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType()) |
| 1506 | return objcTypesAreCompatible(lcanon, rcanon); |
| 1507 | return false; |
| 1508 | } |
| 1509 | switch (lcanon->getTypeClass()) { |
| 1510 | case Type::Pointer: |
| 1511 | return pointerTypesAreCompatible(lcanon, rcanon); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1512 | case Type::ConstantArray: |
| 1513 | case Type::VariableArray: |
| 1514 | return arrayTypesAreCompatible(lcanon, rcanon); |
| 1515 | case Type::FunctionNoProto: |
| 1516 | case Type::FunctionProto: |
| 1517 | return functionTypesAreCompatible(lcanon, rcanon); |
| 1518 | case Type::Tagged: // handle structures, unions |
| 1519 | return tagTypesAreCompatible(lcanon, rcanon); |
| 1520 | case Type::Builtin: |
| 1521 | return builtinTypesAreCompatible(lcanon, rcanon); |
| 1522 | case Type::ObjcInterface: |
| 1523 | return interfaceTypesAreCompatible(lcanon, rcanon); |
Chris Lattner | 5003e8b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1524 | case Type::Vector: |
| 1525 | case Type::OCUVector: |
| 1526 | return vectorTypesAreCompatible(lcanon, rcanon); |
Fariborz Jahanian | 12519d4 | 2007-12-12 01:00:23 +0000 | [diff] [blame] | 1527 | case Type::ObjcQualifiedInterface: |
| 1528 | return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1529 | default: |
| 1530 | assert(0 && "unexpected type"); |
| 1531 | } |
| 1532 | return true; // should never get here... |
| 1533 | } |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1534 | |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1535 | /// Emit - Serialize an ASTContext object to Bitcode. |
| 1536 | void ASTContext::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 9af4d5c | 2007-10-31 20:00:03 +0000 | [diff] [blame] | 1537 | S.EmitRef(SourceMgr); |
| 1538 | S.EmitRef(Target); |
| 1539 | S.EmitRef(Idents); |
| 1540 | S.EmitRef(Selectors); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1541 | |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1542 | // Emit the size of the type vector so that we can reserve that size |
| 1543 | // when we reconstitute the ASTContext object. |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1544 | S.EmitInt(Types.size()); |
| 1545 | |
Ted Kremenek | 034a78c | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 1546 | for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); |
| 1547 | I!=E;++I) |
| 1548 | (*I)->Emit(S); |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1549 | |
Ted Kremenek | e1fed7a | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 1550 | // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); |
Ted Kremenek | 738e6c0 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Ted Kremenek | acba361 | 2007-11-13 00:25:37 +0000 | [diff] [blame] | 1553 | ASTContext* ASTContext::Create(llvm::Deserializer& D) { |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1554 | SourceManager &SM = D.ReadRef<SourceManager>(); |
| 1555 | TargetInfo &t = D.ReadRef<TargetInfo>(); |
| 1556 | IdentifierTable &idents = D.ReadRef<IdentifierTable>(); |
| 1557 | SelectorTable &sels = D.ReadRef<SelectorTable>(); |
| 1558 | |
| 1559 | unsigned size_reserve = D.ReadInt(); |
| 1560 | |
| 1561 | ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve); |
| 1562 | |
Ted Kremenek | 034a78c | 2007-11-13 22:02:55 +0000 | [diff] [blame] | 1563 | for (unsigned i = 0; i < size_reserve; ++i) |
| 1564 | Type::Create(*A,i,D); |
Ted Kremenek | 0199d9f | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1565 | |
Ted Kremenek | e1fed7a | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 1566 | // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); |
Ted Kremenek | 68228a9 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1567 | |
| 1568 | return A; |
| 1569 | } |