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" |
| 20 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | enum FloatingRank { |
| 24 | FloatRank, DoubleRank, LongDoubleRank |
| 25 | }; |
| 26 | |
| 27 | ASTContext::~ASTContext() { |
| 28 | // Deallocate all the types. |
| 29 | while (!Types.empty()) { |
| 30 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) { |
| 31 | // Destroy the object, but don't call delete. These are malloc'd. |
| 32 | FT->~FunctionTypeProto(); |
| 33 | free(FT); |
| 34 | } else { |
| 35 | delete Types.back(); |
| 36 | } |
| 37 | Types.pop_back(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void ASTContext::PrintStats() const { |
| 42 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 43 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 44 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
| 45 | unsigned NumVector = 0, NumComplex = 0; |
| 46 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
| 47 | |
| 48 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 49 | unsigned NumObjcInterfaces = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 50 | |
| 51 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 52 | Type *T = Types[i]; |
| 53 | if (isa<BuiltinType>(T)) |
| 54 | ++NumBuiltin; |
| 55 | else if (isa<PointerType>(T)) |
| 56 | ++NumPointer; |
| 57 | else if (isa<ReferenceType>(T)) |
| 58 | ++NumReference; |
| 59 | else if (isa<ComplexType>(T)) |
| 60 | ++NumComplex; |
| 61 | else if (isa<ArrayType>(T)) |
| 62 | ++NumArray; |
| 63 | else if (isa<VectorType>(T)) |
| 64 | ++NumVector; |
| 65 | else if (isa<FunctionTypeNoProto>(T)) |
| 66 | ++NumFunctionNP; |
| 67 | else if (isa<FunctionTypeProto>(T)) |
| 68 | ++NumFunctionP; |
| 69 | else if (isa<TypedefType>(T)) |
| 70 | ++NumTypeName; |
| 71 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 72 | ++NumTagged; |
| 73 | switch (TT->getDecl()->getKind()) { |
| 74 | default: assert(0 && "Unknown tagged type!"); |
| 75 | case Decl::Struct: ++NumTagStruct; break; |
| 76 | case Decl::Union: ++NumTagUnion; break; |
| 77 | case Decl::Class: ++NumTagClass; break; |
| 78 | case Decl::Enum: ++NumTagEnum; break; |
| 79 | } |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 80 | } else if (isa<ObjcInterfaceType>(T)) |
| 81 | ++NumObjcInterfaces; |
| 82 | else { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 83 | assert(0 && "Unknown type!"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 88 | fprintf(stderr, " %d pointer types\n", NumPointer); |
| 89 | fprintf(stderr, " %d reference types\n", NumReference); |
| 90 | fprintf(stderr, " %d complex types\n", NumComplex); |
| 91 | fprintf(stderr, " %d array types\n", NumArray); |
| 92 | fprintf(stderr, " %d vector types\n", NumVector); |
| 93 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 94 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 95 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 96 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 97 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 98 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 99 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 100 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 101 | fprintf(stderr, " %d interface types\n", NumObjcInterfaces); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 102 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 103 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
| 104 | NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ |
| 105 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 106 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
| 107 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType))); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 112 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
| 113 | } |
| 114 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 115 | void ASTContext::InitBuiltinTypes() { |
| 116 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 117 | |
| 118 | // C99 6.2.5p19. |
| 119 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 120 | |
| 121 | // C99 6.2.5p2. |
| 122 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 123 | // C99 6.2.5p3. |
| 124 | if (Target.isCharSigned(SourceLocation())) |
| 125 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 126 | else |
| 127 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 128 | // C99 6.2.5p4. |
| 129 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 130 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 131 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 132 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 133 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 134 | |
| 135 | // C99 6.2.5p6. |
| 136 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 137 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 138 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 139 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 140 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 141 | |
| 142 | // C99 6.2.5p10. |
| 143 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 144 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 145 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
| 146 | |
| 147 | // C99 6.2.5p11. |
| 148 | FloatComplexTy = getComplexType(FloatTy); |
| 149 | DoubleComplexTy = getComplexType(DoubleTy); |
| 150 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 151 | |
| 152 | BuiltinVaListType = QualType(); |
| 153 | ObjcIdType = QualType(); |
| 154 | IdStructType = 0; |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 155 | ObjcConstantStringType = QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | // Type Sizing and Analysis |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | |
| 162 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 163 | /// does not work on incomplete types. |
| 164 | std::pair<uint64_t, unsigned> |
| 165 | ASTContext::getTypeInfo(QualType T, SourceLocation L) { |
| 166 | T = T.getCanonicalType(); |
| 167 | uint64_t Size; |
| 168 | unsigned Align; |
| 169 | switch (T->getTypeClass()) { |
| 170 | case Type::TypeName: assert(0 && "Not a canonical type!"); |
| 171 | case Type::FunctionNoProto: |
| 172 | case Type::FunctionProto: |
| 173 | default: |
| 174 | assert(0 && "Incomplete types have no size!"); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 175 | case Type::VariableArray: |
| 176 | assert(0 && "VLAs not implemented yet!"); |
| 177 | case Type::ConstantArray: { |
| 178 | ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
| 179 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 180 | std::pair<uint64_t, unsigned> EltInfo = |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 181 | getTypeInfo(CAT->getElementType(), L); |
| 182 | Size = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 183 | Align = EltInfo.second; |
| 184 | break; |
| 185 | } |
| 186 | case Type::Vector: { |
| 187 | std::pair<uint64_t, unsigned> EltInfo = |
| 188 | getTypeInfo(cast<VectorType>(T)->getElementType(), L); |
| 189 | Size = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
| 190 | // FIXME: Vector alignment is not the alignment of its elements. |
| 191 | Align = EltInfo.second; |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | case Type::Builtin: { |
| 196 | // FIXME: need to use TargetInfo to derive the target specific sizes. This |
| 197 | // implementation will suffice for play with vector support. |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 198 | const llvm::fltSemantics *F; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 199 | switch (cast<BuiltinType>(T)->getKind()) { |
| 200 | default: assert(0 && "Unknown builtin type!"); |
| 201 | case BuiltinType::Void: |
| 202 | assert(0 && "Incomplete types have no size!"); |
| 203 | case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break; |
| 204 | case BuiltinType::Char_S: |
| 205 | case BuiltinType::Char_U: |
| 206 | case BuiltinType::UChar: |
| 207 | case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break; |
| 208 | case BuiltinType::UShort: |
| 209 | case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break; |
| 210 | case BuiltinType::UInt: |
| 211 | case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break; |
| 212 | case BuiltinType::ULong: |
| 213 | case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break; |
| 214 | case BuiltinType::ULongLong: |
| 215 | case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break; |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 216 | case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break; |
| 217 | case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break; |
| 218 | case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 219 | } |
| 220 | break; |
| 221 | } |
| 222 | case Type::Pointer: Target.getPointerInfo(Size, Align, L); break; |
| 223 | case Type::Reference: |
| 224 | // "When applied to a reference or a reference type, the result is the size |
| 225 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
| 226 | // FIXME: This is wrong for struct layout! |
| 227 | return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L); |
| 228 | |
| 229 | case Type::Complex: { |
| 230 | // Complex types have the same alignment as their elements, but twice the |
| 231 | // size. |
| 232 | std::pair<uint64_t, unsigned> EltInfo = |
| 233 | getTypeInfo(cast<ComplexType>(T)->getElementType(), L); |
| 234 | Size = EltInfo.first*2; |
| 235 | Align = EltInfo.second; |
| 236 | break; |
| 237 | } |
| 238 | case Type::Tagged: |
Chris Lattner | eb56d29 | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 239 | TagType *TT = cast<TagType>(T); |
| 240 | if (RecordType *RT = dyn_cast<RecordType>(TT)) { |
| 241 | const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L); |
| 242 | Size = Layout.getSize(); |
| 243 | Align = Layout.getAlignment(); |
| 244 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) { |
Chris Lattner | 90a018d | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 245 | return getTypeInfo(ED->getIntegerType(), L); |
Chris Lattner | eb56d29 | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 246 | } else { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 247 | assert(0 && "Unimplemented type sizes!"); |
Chris Lattner | eb56d29 | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | |
| 252 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
| 253 | return std::make_pair(Size, Align); |
| 254 | } |
| 255 | |
| 256 | /// getRecordLayout - Get or compute information about the layout of the |
| 257 | /// specified record (struct/union/class), which indicates its size and field |
| 258 | /// position information. |
| 259 | const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D, |
| 260 | SourceLocation L) { |
| 261 | assert(D->isDefinition() && "Cannot get layout of forward declarations!"); |
| 262 | |
| 263 | // Look up this layout, if already laid out, return what we have. |
| 264 | const RecordLayout *&Entry = RecordLayoutInfo[D]; |
| 265 | if (Entry) return *Entry; |
| 266 | |
| 267 | // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can |
| 268 | // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into. |
| 269 | RecordLayout *NewEntry = new RecordLayout(); |
| 270 | Entry = NewEntry; |
| 271 | |
| 272 | uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()]; |
| 273 | uint64_t RecordSize = 0; |
| 274 | unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits. |
| 275 | |
| 276 | if (D->getKind() != Decl::Union) { |
| 277 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 278 | // the future, this will need to be tweakable by targets. |
| 279 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 280 | const FieldDecl *FD = D->getMember(i); |
| 281 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 282 | uint64_t FieldSize = FieldInfo.first; |
| 283 | unsigned FieldAlign = FieldInfo.second; |
| 284 | |
| 285 | // Round up the current record size to the field's alignment boundary. |
| 286 | RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1); |
| 287 | |
| 288 | // Place this field at the current location. |
| 289 | FieldOffsets[i] = RecordSize; |
| 290 | |
| 291 | // Reserve space for this field. |
| 292 | RecordSize += FieldSize; |
| 293 | |
| 294 | // Remember max struct/class alignment. |
| 295 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 296 | } |
| 297 | |
| 298 | // Finally, round the size of the total struct up to the alignment of the |
| 299 | // struct itself. |
| 300 | RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1); |
| 301 | } else { |
| 302 | // Union layout just puts each member at the start of the record. |
| 303 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 304 | const FieldDecl *FD = D->getMember(i); |
| 305 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 306 | uint64_t FieldSize = FieldInfo.first; |
| 307 | unsigned FieldAlign = FieldInfo.second; |
| 308 | |
| 309 | // Round up the current record size to the field's alignment boundary. |
| 310 | RecordSize = std::max(RecordSize, FieldSize); |
| 311 | |
| 312 | // Place this field at the start of the record. |
| 313 | FieldOffsets[i] = 0; |
| 314 | |
| 315 | // Remember max struct/class alignment. |
| 316 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets); |
| 321 | return *NewEntry; |
| 322 | } |
| 323 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 324 | //===----------------------------------------------------------------------===// |
| 325 | // Type creation/memoization methods |
| 326 | //===----------------------------------------------------------------------===// |
| 327 | |
| 328 | |
| 329 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 330 | /// number with the specified element type. |
| 331 | QualType ASTContext::getComplexType(QualType T) { |
| 332 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 333 | // structure. |
| 334 | llvm::FoldingSetNodeID ID; |
| 335 | ComplexType::Profile(ID, T); |
| 336 | |
| 337 | void *InsertPos = 0; |
| 338 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 339 | return QualType(CT, 0); |
| 340 | |
| 341 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 342 | // so fill in the canonical type field. |
| 343 | QualType Canonical; |
| 344 | if (!T->isCanonical()) { |
| 345 | Canonical = getComplexType(T.getCanonicalType()); |
| 346 | |
| 347 | // Get the new insert position for the node we care about. |
| 348 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 349 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 350 | } |
| 351 | ComplexType *New = new ComplexType(T, Canonical); |
| 352 | Types.push_back(New); |
| 353 | ComplexTypes.InsertNode(New, InsertPos); |
| 354 | return QualType(New, 0); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 359 | /// the specified type. |
| 360 | QualType ASTContext::getPointerType(QualType T) { |
| 361 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 362 | // structure. |
| 363 | llvm::FoldingSetNodeID ID; |
| 364 | PointerType::Profile(ID, T); |
| 365 | |
| 366 | void *InsertPos = 0; |
| 367 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 368 | return QualType(PT, 0); |
| 369 | |
| 370 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 371 | // so fill in the canonical type field. |
| 372 | QualType Canonical; |
| 373 | if (!T->isCanonical()) { |
| 374 | Canonical = getPointerType(T.getCanonicalType()); |
| 375 | |
| 376 | // Get the new insert position for the node we care about. |
| 377 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 378 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 379 | } |
| 380 | PointerType *New = new PointerType(T, Canonical); |
| 381 | Types.push_back(New); |
| 382 | PointerTypes.InsertNode(New, InsertPos); |
| 383 | return QualType(New, 0); |
| 384 | } |
| 385 | |
| 386 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 387 | /// to the specified type. |
| 388 | QualType ASTContext::getReferenceType(QualType T) { |
| 389 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 390 | // structure. |
| 391 | llvm::FoldingSetNodeID ID; |
| 392 | ReferenceType::Profile(ID, T); |
| 393 | |
| 394 | void *InsertPos = 0; |
| 395 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 396 | return QualType(RT, 0); |
| 397 | |
| 398 | // If the referencee type isn't canonical, this won't be a canonical type |
| 399 | // either, so fill in the canonical type field. |
| 400 | QualType Canonical; |
| 401 | if (!T->isCanonical()) { |
| 402 | Canonical = getReferenceType(T.getCanonicalType()); |
| 403 | |
| 404 | // Get the new insert position for the node we care about. |
| 405 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 406 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 407 | } |
| 408 | |
| 409 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 410 | Types.push_back(New); |
| 411 | ReferenceTypes.InsertNode(New, InsertPos); |
| 412 | return QualType(New, 0); |
| 413 | } |
| 414 | |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 415 | /// getConstantArrayType - Return the unique reference to the type for an |
| 416 | /// array of the specified element type. |
| 417 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 418 | const llvm::APInt &ArySize, |
| 419 | ArrayType::ArraySizeModifier ASM, |
| 420 | unsigned EltTypeQuals) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 421 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 422 | ConstantArrayType::Profile(ID, EltTy, ArySize); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 423 | |
| 424 | void *InsertPos = 0; |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 425 | if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 426 | return QualType(ATP, 0); |
| 427 | |
| 428 | // If the element type isn't canonical, this won't be a canonical type either, |
| 429 | // so fill in the canonical type field. |
| 430 | QualType Canonical; |
| 431 | if (!EltTy->isCanonical()) { |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 432 | Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize, |
| 433 | ASM, EltTypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 434 | // Get the new insert position for the node we care about. |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 435 | ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 436 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 437 | } |
| 438 | |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 439 | ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize, |
| 440 | ASM, EltTypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 441 | ArrayTypes.InsertNode(New, InsertPos); |
| 442 | Types.push_back(New); |
| 443 | return QualType(New, 0); |
| 444 | } |
| 445 | |
Steve Naroff | e2579e3 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 446 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 447 | /// variable array of the specified element type. |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 448 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
| 449 | ArrayType::ArraySizeModifier ASM, |
| 450 | unsigned EltTypeQuals) { |
| 451 | // Since we don't unique expressions, it isn't possible to unique VLA's. |
| 452 | ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 453 | ASM, EltTypeQuals); |
| 454 | Types.push_back(New); |
| 455 | return QualType(New, 0); |
Steve Naroff | 83c1301 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 458 | /// getVectorType - Return the unique reference to a vector type of |
| 459 | /// the specified element type and size. VectorType must be a built-in type. |
| 460 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
| 461 | BuiltinType *baseType; |
| 462 | |
| 463 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 464 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
| 465 | |
| 466 | // Check if we've already instantiated a vector of this type. |
| 467 | llvm::FoldingSetNodeID ID; |
| 468 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
| 469 | void *InsertPos = 0; |
| 470 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 471 | return QualType(VTP, 0); |
| 472 | |
| 473 | // If the element type isn't canonical, this won't be a canonical type either, |
| 474 | // so fill in the canonical type field. |
| 475 | QualType Canonical; |
| 476 | if (!vecType->isCanonical()) { |
| 477 | Canonical = getVectorType(vecType.getCanonicalType(), NumElts); |
| 478 | |
| 479 | // Get the new insert position for the node we care about. |
| 480 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 481 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 482 | } |
| 483 | VectorType *New = new VectorType(vecType, NumElts, Canonical); |
| 484 | VectorTypes.InsertNode(New, InsertPos); |
| 485 | Types.push_back(New); |
| 486 | return QualType(New, 0); |
| 487 | } |
| 488 | |
| 489 | /// getOCUVectorType - Return the unique reference to an OCU vector type of |
| 490 | /// the specified element type and size. VectorType must be a built-in type. |
| 491 | QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) { |
| 492 | BuiltinType *baseType; |
| 493 | |
| 494 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 495 | assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type"); |
| 496 | |
| 497 | // Check if we've already instantiated a vector of this type. |
| 498 | llvm::FoldingSetNodeID ID; |
| 499 | VectorType::Profile(ID, vecType, NumElts, Type::OCUVector); |
| 500 | void *InsertPos = 0; |
| 501 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 502 | return QualType(VTP, 0); |
| 503 | |
| 504 | // If the element type isn't canonical, this won't be a canonical type either, |
| 505 | // so fill in the canonical type field. |
| 506 | QualType Canonical; |
| 507 | if (!vecType->isCanonical()) { |
| 508 | Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts); |
| 509 | |
| 510 | // Get the new insert position for the node we care about. |
| 511 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 512 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 513 | } |
| 514 | OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical); |
| 515 | VectorTypes.InsertNode(New, InsertPos); |
| 516 | Types.push_back(New); |
| 517 | return QualType(New, 0); |
| 518 | } |
| 519 | |
| 520 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 521 | /// |
| 522 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 523 | // Unique functions, to guarantee there is only one function of a particular |
| 524 | // structure. |
| 525 | llvm::FoldingSetNodeID ID; |
| 526 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 527 | |
| 528 | void *InsertPos = 0; |
| 529 | if (FunctionTypeNoProto *FT = |
| 530 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 531 | return QualType(FT, 0); |
| 532 | |
| 533 | QualType Canonical; |
| 534 | if (!ResultTy->isCanonical()) { |
| 535 | Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType()); |
| 536 | |
| 537 | // Get the new insert position for the node we care about. |
| 538 | FunctionTypeNoProto *NewIP = |
| 539 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 540 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 541 | } |
| 542 | |
| 543 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 544 | Types.push_back(New); |
| 545 | FunctionTypeProtos.InsertNode(New, InsertPos); |
| 546 | return QualType(New, 0); |
| 547 | } |
| 548 | |
| 549 | /// getFunctionType - Return a normal function type with a typed argument |
| 550 | /// list. isVariadic indicates whether the argument list includes '...'. |
| 551 | QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray, |
| 552 | unsigned NumArgs, bool isVariadic) { |
| 553 | // Unique functions, to guarantee there is only one function of a particular |
| 554 | // structure. |
| 555 | llvm::FoldingSetNodeID ID; |
| 556 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic); |
| 557 | |
| 558 | void *InsertPos = 0; |
| 559 | if (FunctionTypeProto *FTP = |
| 560 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 561 | return QualType(FTP, 0); |
| 562 | |
| 563 | // Determine whether the type being created is already canonical or not. |
| 564 | bool isCanonical = ResultTy->isCanonical(); |
| 565 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 566 | if (!ArgArray[i]->isCanonical()) |
| 567 | isCanonical = false; |
| 568 | |
| 569 | // If this type isn't canonical, get the canonical version of it. |
| 570 | QualType Canonical; |
| 571 | if (!isCanonical) { |
| 572 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 573 | CanonicalArgs.reserve(NumArgs); |
| 574 | for (unsigned i = 0; i != NumArgs; ++i) |
| 575 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 576 | |
| 577 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 578 | &CanonicalArgs[0], NumArgs, |
| 579 | isVariadic); |
| 580 | |
| 581 | // Get the new insert position for the node we care about. |
| 582 | FunctionTypeProto *NewIP = |
| 583 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 584 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 585 | } |
| 586 | |
| 587 | // FunctionTypeProto objects are not allocated with new because they have a |
| 588 | // variable size array (for parameter types) at the end of them. |
| 589 | FunctionTypeProto *FTP = |
| 590 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
| 591 | NumArgs*sizeof(QualType)); |
| 592 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 593 | Canonical); |
| 594 | Types.push_back(FTP); |
| 595 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 596 | return QualType(FTP, 0); |
| 597 | } |
| 598 | |
| 599 | /// getTypedefType - Return the unique reference to the type for the |
| 600 | /// specified typename decl. |
| 601 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 602 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 603 | |
| 604 | QualType Canonical = Decl->getUnderlyingType().getCanonicalType(); |
| 605 | Decl->TypeForDecl = new TypedefType(Decl, Canonical); |
| 606 | Types.push_back(Decl->TypeForDecl); |
| 607 | return QualType(Decl->TypeForDecl, 0); |
| 608 | } |
| 609 | |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 610 | /// getObjcInterfaceType - Return the unique reference to the type for the |
| 611 | /// specified ObjC interface decl. |
| 612 | QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) { |
| 613 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 614 | |
| 615 | Decl->TypeForDecl = new ObjcInterfaceType(Decl); |
| 616 | Types.push_back(Decl->TypeForDecl); |
| 617 | return QualType(Decl->TypeForDecl, 0); |
| 618 | } |
| 619 | |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 620 | /// getObjcQualifiedInterfaceType - Return a |
| 621 | /// ObjcQualifiedInterfaceType type for the given interface decl and |
| 622 | /// the conforming protocol list. |
| 623 | QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl, |
| 624 | ObjcProtocolDecl **Protocols, unsigned NumProtocols) { |
| 625 | ObjcInterfaceType *IType = |
| 626 | cast<ObjcInterfaceType>(getObjcInterfaceType(Decl)); |
| 627 | |
| 628 | llvm::FoldingSetNodeID ID; |
| 629 | ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols); |
| 630 | |
| 631 | void *InsertPos = 0; |
| 632 | if (ObjcQualifiedInterfaceType *QT = |
| 633 | ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 634 | return QualType(QT, 0); |
| 635 | |
| 636 | // No Match; |
Chris Lattner | d855a6e | 2007-10-11 03:36:41 +0000 | [diff] [blame] | 637 | ObjcQualifiedInterfaceType *QType = |
| 638 | new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 639 | Types.push_back(QType); |
| 640 | ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
| 641 | return QualType(QType, 0); |
| 642 | } |
| 643 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 644 | /// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique |
| 645 | /// TypeOfExpr AST's (since expression's are never shared). For example, |
| 646 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 647 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 648 | /// on canonical type's (which are always unique). |
Steve Naroff | 11b649c | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 649 | QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 650 | QualType Canonical = tofExpr->getType().getCanonicalType(); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 651 | TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical); |
| 652 | Types.push_back(toe); |
| 653 | return QualType(toe, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 656 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 657 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 658 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 659 | /// an issue. This doesn't effect the type checker, since it operates |
| 660 | /// on canonical type's (which are always unique). |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 661 | QualType ASTContext::getTypeOfType(QualType tofType) { |
| 662 | QualType Canonical = tofType.getCanonicalType(); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 663 | TypeOfType *tot = new TypeOfType(tofType, Canonical); |
| 664 | Types.push_back(tot); |
| 665 | return QualType(tot, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 668 | /// getTagDeclType - Return the unique reference to the type for the |
| 669 | /// specified TagDecl (struct/union/class/enum) decl. |
| 670 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
| 671 | // The decl stores the type cache. |
| 672 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 673 | |
| 674 | Decl->TypeForDecl = new TagType(Decl, QualType()); |
| 675 | Types.push_back(Decl->TypeForDecl); |
| 676 | return QualType(Decl->TypeForDecl, 0); |
| 677 | } |
| 678 | |
| 679 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 680 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 681 | /// needs to agree with the definition in <stddef.h>. |
| 682 | QualType ASTContext::getSizeType() const { |
| 683 | // On Darwin, size_t is defined as a "long unsigned int". |
| 684 | // FIXME: should derive from "Target". |
| 685 | return UnsignedLongTy; |
| 686 | } |
| 687 | |
| 688 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 689 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 690 | QualType ASTContext::getPointerDiffType() const { |
| 691 | // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug... |
| 692 | // FIXME: should derive from "Target". |
| 693 | return IntTy; |
| 694 | } |
| 695 | |
| 696 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 697 | /// routine will assert if passed a built-in type that isn't an integer or enum. |
| 698 | static int getIntegerRank(QualType t) { |
| 699 | if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) { |
| 700 | assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum"); |
| 701 | return 4; |
| 702 | } |
| 703 | |
| 704 | const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType()); |
| 705 | switch (BT->getKind()) { |
| 706 | default: |
| 707 | assert(0 && "getIntegerRank(): not a built-in integer"); |
| 708 | case BuiltinType::Bool: |
| 709 | return 1; |
| 710 | case BuiltinType::Char_S: |
| 711 | case BuiltinType::Char_U: |
| 712 | case BuiltinType::SChar: |
| 713 | case BuiltinType::UChar: |
| 714 | return 2; |
| 715 | case BuiltinType::Short: |
| 716 | case BuiltinType::UShort: |
| 717 | return 3; |
| 718 | case BuiltinType::Int: |
| 719 | case BuiltinType::UInt: |
| 720 | return 4; |
| 721 | case BuiltinType::Long: |
| 722 | case BuiltinType::ULong: |
| 723 | return 5; |
| 724 | case BuiltinType::LongLong: |
| 725 | case BuiltinType::ULongLong: |
| 726 | return 6; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /// getFloatingRank - Return a relative rank for floating point types. |
| 731 | /// This routine will assert if passed a built-in type that isn't a float. |
| 732 | static int getFloatingRank(QualType T) { |
| 733 | T = T.getCanonicalType(); |
| 734 | if (ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 735 | return getFloatingRank(CT->getElementType()); |
| 736 | |
| 737 | switch (cast<BuiltinType>(T)->getKind()) { |
| 738 | default: assert(0 && "getFloatingPointRank(): not a floating type"); |
| 739 | case BuiltinType::Float: return FloatRank; |
| 740 | case BuiltinType::Double: return DoubleRank; |
| 741 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 742 | } |
| 743 | } |
| 744 | |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 745 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 746 | /// point or a complex type (based on typeDomain/typeSize). |
| 747 | /// 'typeDomain' is a real floating point or complex type. |
| 748 | /// 'typeSize' is a real floating point or complex type. |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 749 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain( |
| 750 | QualType typeSize, QualType typeDomain) const { |
| 751 | if (typeDomain->isComplexType()) { |
| 752 | switch (getFloatingRank(typeSize)) { |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 753 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 754 | case FloatRank: return FloatComplexTy; |
| 755 | case DoubleRank: return DoubleComplexTy; |
| 756 | case LongDoubleRank: return LongDoubleComplexTy; |
| 757 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 758 | } |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 759 | if (typeDomain->isRealFloatingType()) { |
| 760 | switch (getFloatingRank(typeSize)) { |
Steve Naroff | fa0c453 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 761 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 762 | case FloatRank: return FloatTy; |
| 763 | case DoubleRank: return DoubleTy; |
| 764 | case LongDoubleRank: return LongDoubleTy; |
| 765 | } |
| 766 | } |
| 767 | assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain"); |
Chris Lattner | 1d2b461 | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 768 | //an invalid return value, but the assert |
| 769 | //will ensure that this code is never reached. |
| 770 | return VoidTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 773 | /// compareFloatingType - Handles 3 different combos: |
| 774 | /// float/float, float/complex, complex/complex. |
| 775 | /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. |
| 776 | int ASTContext::compareFloatingType(QualType lt, QualType rt) { |
| 777 | if (getFloatingRank(lt) == getFloatingRank(rt)) |
| 778 | return 0; |
| 779 | if (getFloatingRank(lt) > getFloatingRank(rt)) |
| 780 | return 1; |
| 781 | return -1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | // maxIntegerType - Returns the highest ranked integer type. Handles 3 case: |
| 785 | // unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1. |
| 786 | QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) { |
| 787 | if (lhs == rhs) return lhs; |
| 788 | |
| 789 | bool t1Unsigned = lhs->isUnsignedIntegerType(); |
| 790 | bool t2Unsigned = rhs->isUnsignedIntegerType(); |
| 791 | |
| 792 | if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned)) |
| 793 | return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs; |
| 794 | |
| 795 | // We have two integer types with differing signs |
| 796 | QualType unsignedType = t1Unsigned ? lhs : rhs; |
| 797 | QualType signedType = t1Unsigned ? rhs : lhs; |
| 798 | |
| 799 | if (getIntegerRank(unsignedType) >= getIntegerRank(signedType)) |
| 800 | return unsignedType; |
| 801 | else { |
| 802 | // FIXME: Need to check if the signed type can represent all values of the |
| 803 | // unsigned type. If it can, then the result is the signed type. |
| 804 | // If it can't, then the result is the unsigned version of the signed type. |
| 805 | // Should probably add a helper that returns a signed integer type from |
| 806 | // an unsigned (and vice versa). C99 6.3.1.8. |
| 807 | return signedType; |
| 808 | } |
| 809 | } |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 810 | |
| 811 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 812 | QualType ASTContext::getCFConstantStringType() { |
| 813 | if (!CFConstantStringTypeDecl) { |
| 814 | CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(), |
| 815 | &Idents.get("__builtin_CFString"), |
| 816 | 0); |
| 817 | |
| 818 | QualType FieldTypes[4]; |
| 819 | |
| 820 | // const int *isa; |
| 821 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
| 822 | // int flags; |
| 823 | FieldTypes[1] = IntTy; |
| 824 | // const char *str; |
| 825 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
| 826 | // long length; |
| 827 | FieldTypes[3] = LongTy; |
| 828 | // Create fields |
| 829 | FieldDecl *FieldDecls[4]; |
| 830 | |
| 831 | for (unsigned i = 0; i < 4; ++i) |
Steve Naroff | dc1ad76 | 2007-09-14 02:20:46 +0000 | [diff] [blame] | 832 | FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]); |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 833 | |
| 834 | CFConstantStringTypeDecl->defineBody(FieldDecls, 4); |
| 835 | } |
| 836 | |
| 837 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 61ce98c | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 838 | } |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 839 | |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame^] | 840 | // This returns true if a type has been typedefed to BOOL: |
| 841 | // typedef <type> BOOL; |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 842 | static bool isTypeTypedefedAsBOOL(QualType T) |
| 843 | { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame^] | 844 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 845 | if (!strcmp(TT->getDecl()->getName(), "BOOL")) |
| 846 | return true; |
| 847 | |
| 848 | return false; |
| 849 | } |
| 850 | |
| 851 | void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const |
| 852 | { |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame^] | 853 | // FIXME: This currently doesn't encode: |
| 854 | // @ An object (whether statically typed or typed id) |
| 855 | // # A class object (Class) |
| 856 | // : A method selector (SEL) |
| 857 | // {name=type...} A structure |
| 858 | // (name=type...) A union |
| 859 | // bnum A bit field of num bits |
| 860 | |
| 861 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 862 | char encoding; |
| 863 | switch (BT->getKind()) { |
| 864 | case BuiltinType::Void: |
| 865 | encoding = 'v'; |
| 866 | break; |
| 867 | case BuiltinType::Bool: |
| 868 | encoding = 'B'; |
| 869 | break; |
| 870 | case BuiltinType::Char_U: |
| 871 | case BuiltinType::UChar: |
| 872 | encoding = 'C'; |
| 873 | break; |
| 874 | case BuiltinType::UShort: |
| 875 | encoding = 'S'; |
| 876 | break; |
| 877 | case BuiltinType::UInt: |
| 878 | encoding = 'I'; |
| 879 | break; |
| 880 | case BuiltinType::ULong: |
| 881 | encoding = 'L'; |
| 882 | break; |
| 883 | case BuiltinType::ULongLong: |
| 884 | encoding = 'Q'; |
| 885 | break; |
| 886 | case BuiltinType::Char_S: |
| 887 | case BuiltinType::SChar: |
| 888 | encoding = 'c'; |
| 889 | break; |
| 890 | case BuiltinType::Short: |
| 891 | encoding = 's'; |
| 892 | break; |
| 893 | case BuiltinType::Int: |
| 894 | encoding = 'i'; |
| 895 | break; |
| 896 | case BuiltinType::Long: |
| 897 | encoding = 'l'; |
| 898 | break; |
| 899 | case BuiltinType::LongLong: |
| 900 | encoding = 'q'; |
| 901 | break; |
| 902 | case BuiltinType::Float: |
| 903 | encoding = 'f'; |
| 904 | break; |
| 905 | case BuiltinType::Double: |
| 906 | encoding = 'd'; |
| 907 | break; |
| 908 | case BuiltinType::LongDouble: |
| 909 | encoding = 'd'; |
| 910 | break; |
| 911 | default: |
| 912 | assert(0 && "Unhandled builtin type kind"); |
| 913 | } |
| 914 | |
| 915 | S += encoding; |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame^] | 916 | } else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 917 | QualType PointeeTy = PT->getPointeeType(); |
| 918 | |
| 919 | if (PointeeTy->isCharType()) { |
| 920 | // char pointer types should be encoded as '*' unless it is a |
| 921 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame^] | 922 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 923 | S += '*'; |
| 924 | return; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | S += '^'; |
| 929 | getObjcEncodingForType(PT->getPointeeType(), S); |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame^] | 930 | } else if (const ArrayType *AT = T->getAsArrayType()) { |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 931 | S += '['; |
| 932 | |
| 933 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 934 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 935 | else |
| 936 | assert(0 && "Unhandled array type!"); |
| 937 | |
| 938 | getObjcEncodingForType(AT->getElementType(), S); |
| 939 | S += ']'; |
| 940 | } else |
Anders Carlsson | e3f0257 | 2007-10-29 06:33:42 +0000 | [diff] [blame^] | 941 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 944 | void ASTContext::setBuiltinVaListType(QualType T) |
| 945 | { |
| 946 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 947 | |
| 948 | BuiltinVaListType = T; |
| 949 | } |
| 950 | |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 951 | void ASTContext::setObjcIdType(TypedefDecl *TD) |
| 952 | { |
| 953 | assert(ObjcIdType.isNull() && "'id' type already set!"); |
| 954 | |
| 955 | ObjcIdType = getTypedefType(TD); |
| 956 | |
| 957 | // typedef struct objc_object *id; |
| 958 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 959 | assert(ptr && "'id' incorrectly typed"); |
| 960 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 961 | assert(rec && "'id' incorrectly typed"); |
| 962 | IdStructType = rec; |
| 963 | } |
| 964 | |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 965 | void ASTContext::setObjcSelType(TypedefDecl *TD) |
| 966 | { |
| 967 | assert(ObjcSelType.isNull() && "'SEL' type already set!"); |
| 968 | |
| 969 | ObjcSelType = getTypedefType(TD); |
| 970 | |
| 971 | // typedef struct objc_selector *SEL; |
| 972 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 973 | assert(ptr && "'SEL' incorrectly typed"); |
| 974 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 975 | assert(rec && "'SEL' incorrectly typed"); |
| 976 | SelStructType = rec; |
| 977 | } |
| 978 | |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 979 | void ASTContext::setObjcProtoType(TypedefDecl *TD) |
| 980 | { |
| 981 | assert(ObjcProtoType.isNull() && "'Protocol' type already set!"); |
| 982 | |
| 983 | // typedef struct Protocol Protocol; |
| 984 | ObjcProtoType = TD->getUnderlyingType(); |
| 985 | // Protocol * type |
| 986 | ObjcProtoType = getPointerType(ObjcProtoType); |
| 987 | ProtoStructType = TD->getUnderlyingType()->getAsStructureType(); |
| 988 | } |
| 989 | |
Steve Naroff | f2e3031 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 990 | void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) { |
| 991 | assert(ObjcConstantStringType.isNull() && |
| 992 | "'NSConstantString' type already set!"); |
| 993 | |
| 994 | ObjcConstantStringType = getObjcInterfaceType(Decl); |
| 995 | } |
| 996 | |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 997 | bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) { |
| 998 | const BuiltinType *lBuiltin = lhs->getAsBuiltinType(); |
| 999 | const BuiltinType *rBuiltin = rhs->getAsBuiltinType(); |
| 1000 | |
| 1001 | return lBuiltin->getKind() == rBuiltin->getKind(); |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1006 | if (lhs->isObjcInterfaceType() && isObjcIdType(rhs)) |
| 1007 | return true; |
| 1008 | else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType()) |
| 1009 | return true; |
| 1010 | return false; |
| 1011 | } |
| 1012 | |
| 1013 | bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1014 | return true; // FIXME: IMPLEMENT. |
| 1015 | } |
| 1016 | |
| 1017 | // C99 6.2.7p1: If both are complete types, then the following additional |
| 1018 | // requirements apply...FIXME (handle compatibility across source files). |
| 1019 | bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1020 | TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl(); |
| 1021 | TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl(); |
| 1022 | |
| 1023 | if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) { |
| 1024 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1025 | return true; |
| 1026 | } |
| 1027 | if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) { |
| 1028 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1029 | return true; |
| 1030 | } |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1035 | // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be |
| 1036 | // identically qualified and both shall be pointers to compatible types. |
| 1037 | if (lhs.getQualifiers() != rhs.getQualifiers()) |
| 1038 | return false; |
| 1039 | |
| 1040 | QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType(); |
| 1041 | QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType(); |
| 1042 | |
| 1043 | return typesAreCompatible(ltype, rtype); |
| 1044 | } |
| 1045 | |
| 1046 | // C++ 5.17p6: When the left opperand of an assignment operator denotes a |
| 1047 | // reference to T, the operation assigns to the object of type T denoted by the |
| 1048 | // reference. |
| 1049 | bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1050 | QualType ltype = lhs; |
| 1051 | |
| 1052 | if (lhs->isReferenceType()) |
| 1053 | ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType(); |
| 1054 | |
| 1055 | QualType rtype = rhs; |
| 1056 | |
| 1057 | if (rhs->isReferenceType()) |
| 1058 | rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType(); |
| 1059 | |
| 1060 | return typesAreCompatible(ltype, rtype); |
| 1061 | } |
| 1062 | |
| 1063 | bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1064 | const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType()); |
| 1065 | const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType()); |
| 1066 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 1067 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 1068 | |
| 1069 | // first check the return types (common between C99 and K&R). |
| 1070 | if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType())) |
| 1071 | return false; |
| 1072 | |
| 1073 | if (lproto && rproto) { // two C99 style function prototypes |
| 1074 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 1075 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 1076 | |
| 1077 | if (lproto_nargs != rproto_nargs) |
| 1078 | return false; |
| 1079 | |
| 1080 | // both prototypes have the same number of arguments. |
| 1081 | if ((lproto->isVariadic() && !rproto->isVariadic()) || |
| 1082 | (rproto->isVariadic() && !lproto->isVariadic())) |
| 1083 | return false; |
| 1084 | |
| 1085 | // The use of ellipsis agree...now check the argument types. |
| 1086 | for (unsigned i = 0; i < lproto_nargs; i++) |
| 1087 | if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i))) |
| 1088 | return false; |
| 1089 | return true; |
| 1090 | } |
| 1091 | if (!lproto && !rproto) // two K&R style function decls, nothing to do. |
| 1092 | return true; |
| 1093 | |
| 1094 | // we have a mixture of K&R style with C99 prototypes |
| 1095 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 1096 | |
| 1097 | if (proto->isVariadic()) |
| 1098 | return false; |
| 1099 | |
| 1100 | // FIXME: Each parameter type T in the prototype must be compatible with the |
| 1101 | // type resulting from applying the usual argument conversions to T. |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
| 1105 | bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1106 | QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType(); |
| 1107 | QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType(); |
| 1108 | |
| 1109 | if (!typesAreCompatible(ltype, rtype)) |
| 1110 | return false; |
| 1111 | |
| 1112 | // FIXME: If both types specify constant sizes, then the sizes must also be |
| 1113 | // the same. Even if the sizes are the same, GCC produces an error. |
| 1114 | return true; |
| 1115 | } |
| 1116 | |
| 1117 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 1118 | /// both shall have the identically qualified version of a compatible type. |
| 1119 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 1120 | /// same. See 6.7.[2,3,5] for additional rules. |
| 1121 | bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) { |
| 1122 | QualType lcanon = lhs.getCanonicalType(); |
| 1123 | QualType rcanon = rhs.getCanonicalType(); |
| 1124 | |
| 1125 | // If two types are identical, they are are compatible |
| 1126 | if (lcanon == rcanon) |
| 1127 | return true; |
| 1128 | |
| 1129 | // If the canonical type classes don't match, they can't be compatible |
| 1130 | if (lcanon->getTypeClass() != rcanon->getTypeClass()) { |
| 1131 | // For Objective-C, it is possible for two types to be compatible |
| 1132 | // when their classes don't match (when dealing with "id"). If either type |
| 1133 | // is an interface, we defer to objcTypesAreCompatible(). |
| 1134 | if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType()) |
| 1135 | return objcTypesAreCompatible(lcanon, rcanon); |
| 1136 | return false; |
| 1137 | } |
| 1138 | switch (lcanon->getTypeClass()) { |
| 1139 | case Type::Pointer: |
| 1140 | return pointerTypesAreCompatible(lcanon, rcanon); |
| 1141 | case Type::Reference: |
| 1142 | return referenceTypesAreCompatible(lcanon, rcanon); |
| 1143 | case Type::ConstantArray: |
| 1144 | case Type::VariableArray: |
| 1145 | return arrayTypesAreCompatible(lcanon, rcanon); |
| 1146 | case Type::FunctionNoProto: |
| 1147 | case Type::FunctionProto: |
| 1148 | return functionTypesAreCompatible(lcanon, rcanon); |
| 1149 | case Type::Tagged: // handle structures, unions |
| 1150 | return tagTypesAreCompatible(lcanon, rcanon); |
| 1151 | case Type::Builtin: |
| 1152 | return builtinTypesAreCompatible(lcanon, rcanon); |
| 1153 | case Type::ObjcInterface: |
| 1154 | return interfaceTypesAreCompatible(lcanon, rcanon); |
| 1155 | default: |
| 1156 | assert(0 && "unexpected type"); |
| 1157 | } |
| 1158 | return true; // should never get here... |
| 1159 | } |