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" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | enum FloatingRank { |
| 22 | FloatRank, DoubleRank, LongDoubleRank |
| 23 | }; |
| 24 | |
| 25 | ASTContext::~ASTContext() { |
| 26 | // Deallocate all the types. |
| 27 | while (!Types.empty()) { |
| 28 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) { |
| 29 | // Destroy the object, but don't call delete. These are malloc'd. |
| 30 | FT->~FunctionTypeProto(); |
| 31 | free(FT); |
| 32 | } else { |
| 33 | delete Types.back(); |
| 34 | } |
| 35 | Types.pop_back(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void ASTContext::PrintStats() const { |
| 40 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 41 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 42 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
| 43 | unsigned NumVector = 0, NumComplex = 0; |
| 44 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
| 45 | |
| 46 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
| 47 | |
| 48 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 49 | Type *T = Types[i]; |
| 50 | if (isa<BuiltinType>(T)) |
| 51 | ++NumBuiltin; |
| 52 | else if (isa<PointerType>(T)) |
| 53 | ++NumPointer; |
| 54 | else if (isa<ReferenceType>(T)) |
| 55 | ++NumReference; |
| 56 | else if (isa<ComplexType>(T)) |
| 57 | ++NumComplex; |
| 58 | else if (isa<ArrayType>(T)) |
| 59 | ++NumArray; |
| 60 | else if (isa<VectorType>(T)) |
| 61 | ++NumVector; |
| 62 | else if (isa<FunctionTypeNoProto>(T)) |
| 63 | ++NumFunctionNP; |
| 64 | else if (isa<FunctionTypeProto>(T)) |
| 65 | ++NumFunctionP; |
| 66 | else if (isa<TypedefType>(T)) |
| 67 | ++NumTypeName; |
| 68 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 69 | ++NumTagged; |
| 70 | switch (TT->getDecl()->getKind()) { |
| 71 | default: assert(0 && "Unknown tagged type!"); |
| 72 | case Decl::Struct: ++NumTagStruct; break; |
| 73 | case Decl::Union: ++NumTagUnion; break; |
| 74 | case Decl::Class: ++NumTagClass; break; |
| 75 | case Decl::Enum: ++NumTagEnum; break; |
| 76 | } |
| 77 | } else { |
| 78 | assert(0 && "Unknown type!"); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 83 | fprintf(stderr, " %d pointer types\n", NumPointer); |
| 84 | fprintf(stderr, " %d reference types\n", NumReference); |
| 85 | fprintf(stderr, " %d complex types\n", NumComplex); |
| 86 | fprintf(stderr, " %d array types\n", NumArray); |
| 87 | fprintf(stderr, " %d vector types\n", NumVector); |
| 88 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 89 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 90 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 91 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 92 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 93 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 94 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 95 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
| 96 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 97 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
| 98 | NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ |
| 99 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 100 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
| 101 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType))); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 106 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void ASTContext::InitBuiltinTypes() { |
| 111 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 112 | |
| 113 | // C99 6.2.5p19. |
| 114 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 115 | |
| 116 | // C99 6.2.5p2. |
| 117 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 118 | // C99 6.2.5p3. |
| 119 | if (Target.isCharSigned(SourceLocation())) |
| 120 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 121 | else |
| 122 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 123 | // C99 6.2.5p4. |
| 124 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 125 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 126 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 127 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 128 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 129 | |
| 130 | // C99 6.2.5p6. |
| 131 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 132 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 133 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 134 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 135 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 136 | |
| 137 | // C99 6.2.5p10. |
| 138 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 139 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 140 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
| 141 | |
| 142 | // C99 6.2.5p11. |
| 143 | FloatComplexTy = getComplexType(FloatTy); |
| 144 | DoubleComplexTy = getComplexType(DoubleTy); |
| 145 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
| 146 | } |
| 147 | |
| 148 | //===----------------------------------------------------------------------===// |
| 149 | // Type Sizing and Analysis |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | |
| 152 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 153 | /// does not work on incomplete types. |
| 154 | std::pair<uint64_t, unsigned> |
| 155 | ASTContext::getTypeInfo(QualType T, SourceLocation L) { |
| 156 | T = T.getCanonicalType(); |
| 157 | uint64_t Size; |
| 158 | unsigned Align; |
| 159 | switch (T->getTypeClass()) { |
| 160 | case Type::TypeName: assert(0 && "Not a canonical type!"); |
| 161 | case Type::FunctionNoProto: |
| 162 | case Type::FunctionProto: |
| 163 | default: |
| 164 | assert(0 && "Incomplete types have no size!"); |
| 165 | case Type::Array: { |
| 166 | std::pair<uint64_t, unsigned> EltInfo = |
| 167 | getTypeInfo(cast<ArrayType>(T)->getElementType(), L); |
| 168 | |
| 169 | // Get the size of the array. |
| 170 | llvm::APSInt Sz(32); |
| 171 | if (!cast<ArrayType>(T)->getSizeExpr()->isIntegerConstantExpr(Sz, *this)) |
| 172 | assert(0 && "VLAs not implemented yet!"); |
| 173 | |
| 174 | Size = EltInfo.first*Sz.getZExtValue(); |
| 175 | Align = EltInfo.second; |
| 176 | break; |
| 177 | } |
| 178 | case Type::Vector: { |
| 179 | std::pair<uint64_t, unsigned> EltInfo = |
| 180 | getTypeInfo(cast<VectorType>(T)->getElementType(), L); |
| 181 | Size = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
| 182 | // FIXME: Vector alignment is not the alignment of its elements. |
| 183 | Align = EltInfo.second; |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | case Type::Builtin: { |
| 188 | // FIXME: need to use TargetInfo to derive the target specific sizes. This |
| 189 | // implementation will suffice for play with vector support. |
| 190 | switch (cast<BuiltinType>(T)->getKind()) { |
| 191 | default: assert(0 && "Unknown builtin type!"); |
| 192 | case BuiltinType::Void: |
| 193 | assert(0 && "Incomplete types have no size!"); |
| 194 | case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break; |
| 195 | case BuiltinType::Char_S: |
| 196 | case BuiltinType::Char_U: |
| 197 | case BuiltinType::UChar: |
| 198 | case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break; |
| 199 | case BuiltinType::UShort: |
| 200 | case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break; |
| 201 | case BuiltinType::UInt: |
| 202 | case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break; |
| 203 | case BuiltinType::ULong: |
| 204 | case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break; |
| 205 | case BuiltinType::ULongLong: |
| 206 | case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break; |
| 207 | case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break; |
| 208 | case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break; |
| 209 | case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break; |
| 210 | } |
| 211 | break; |
| 212 | } |
| 213 | case Type::Pointer: Target.getPointerInfo(Size, Align, L); break; |
| 214 | case Type::Reference: |
| 215 | // "When applied to a reference or a reference type, the result is the size |
| 216 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
| 217 | // FIXME: This is wrong for struct layout! |
| 218 | return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L); |
| 219 | |
| 220 | case Type::Complex: { |
| 221 | // Complex types have the same alignment as their elements, but twice the |
| 222 | // size. |
| 223 | std::pair<uint64_t, unsigned> EltInfo = |
| 224 | getTypeInfo(cast<ComplexType>(T)->getElementType(), L); |
| 225 | Size = EltInfo.first*2; |
| 226 | Align = EltInfo.second; |
| 227 | break; |
| 228 | } |
| 229 | case Type::Tagged: |
| 230 | RecordType *RT = dyn_cast<RecordType>(cast<TagType>(T)); |
| 231 | if (!RT) |
| 232 | // FIXME: Handle enums. |
| 233 | assert(0 && "Unimplemented type sizes!"); |
| 234 | const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L); |
| 235 | Size = Layout.getSize(); |
| 236 | Align = Layout.getAlignment(); |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
| 241 | return std::make_pair(Size, Align); |
| 242 | } |
| 243 | |
| 244 | /// getRecordLayout - Get or compute information about the layout of the |
| 245 | /// specified record (struct/union/class), which indicates its size and field |
| 246 | /// position information. |
| 247 | const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D, |
| 248 | SourceLocation L) { |
| 249 | assert(D->isDefinition() && "Cannot get layout of forward declarations!"); |
| 250 | |
| 251 | // Look up this layout, if already laid out, return what we have. |
| 252 | const RecordLayout *&Entry = RecordLayoutInfo[D]; |
| 253 | if (Entry) return *Entry; |
| 254 | |
| 255 | // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can |
| 256 | // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into. |
| 257 | RecordLayout *NewEntry = new RecordLayout(); |
| 258 | Entry = NewEntry; |
| 259 | |
| 260 | uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()]; |
| 261 | uint64_t RecordSize = 0; |
| 262 | unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits. |
| 263 | |
| 264 | if (D->getKind() != Decl::Union) { |
| 265 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 266 | // the future, this will need to be tweakable by targets. |
| 267 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 268 | const FieldDecl *FD = D->getMember(i); |
| 269 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 270 | uint64_t FieldSize = FieldInfo.first; |
| 271 | unsigned FieldAlign = FieldInfo.second; |
| 272 | |
| 273 | // Round up the current record size to the field's alignment boundary. |
| 274 | RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1); |
| 275 | |
| 276 | // Place this field at the current location. |
| 277 | FieldOffsets[i] = RecordSize; |
| 278 | |
| 279 | // Reserve space for this field. |
| 280 | RecordSize += FieldSize; |
| 281 | |
| 282 | // Remember max struct/class alignment. |
| 283 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 284 | } |
| 285 | |
| 286 | // Finally, round the size of the total struct up to the alignment of the |
| 287 | // struct itself. |
| 288 | RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1); |
| 289 | } else { |
| 290 | // Union layout just puts each member at the start of the record. |
| 291 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 292 | const FieldDecl *FD = D->getMember(i); |
| 293 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 294 | uint64_t FieldSize = FieldInfo.first; |
| 295 | unsigned FieldAlign = FieldInfo.second; |
| 296 | |
| 297 | // Round up the current record size to the field's alignment boundary. |
| 298 | RecordSize = std::max(RecordSize, FieldSize); |
| 299 | |
| 300 | // Place this field at the start of the record. |
| 301 | FieldOffsets[i] = 0; |
| 302 | |
| 303 | // Remember max struct/class alignment. |
| 304 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets); |
| 309 | return *NewEntry; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | // Type creation/memoization methods |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | |
| 317 | |
| 318 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 319 | /// number with the specified element type. |
| 320 | QualType ASTContext::getComplexType(QualType T) { |
| 321 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 322 | // structure. |
| 323 | llvm::FoldingSetNodeID ID; |
| 324 | ComplexType::Profile(ID, T); |
| 325 | |
| 326 | void *InsertPos = 0; |
| 327 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 328 | return QualType(CT, 0); |
| 329 | |
| 330 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 331 | // so fill in the canonical type field. |
| 332 | QualType Canonical; |
| 333 | if (!T->isCanonical()) { |
| 334 | Canonical = getComplexType(T.getCanonicalType()); |
| 335 | |
| 336 | // Get the new insert position for the node we care about. |
| 337 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 338 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 339 | } |
| 340 | ComplexType *New = new ComplexType(T, Canonical); |
| 341 | Types.push_back(New); |
| 342 | ComplexTypes.InsertNode(New, InsertPos); |
| 343 | return QualType(New, 0); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 348 | /// the specified type. |
| 349 | QualType ASTContext::getPointerType(QualType T) { |
| 350 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 351 | // structure. |
| 352 | llvm::FoldingSetNodeID ID; |
| 353 | PointerType::Profile(ID, T); |
| 354 | |
| 355 | void *InsertPos = 0; |
| 356 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 357 | return QualType(PT, 0); |
| 358 | |
| 359 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 360 | // so fill in the canonical type field. |
| 361 | QualType Canonical; |
| 362 | if (!T->isCanonical()) { |
| 363 | Canonical = getPointerType(T.getCanonicalType()); |
| 364 | |
| 365 | // Get the new insert position for the node we care about. |
| 366 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 367 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 368 | } |
| 369 | PointerType *New = new PointerType(T, Canonical); |
| 370 | Types.push_back(New); |
| 371 | PointerTypes.InsertNode(New, InsertPos); |
| 372 | return QualType(New, 0); |
| 373 | } |
| 374 | |
| 375 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 376 | /// to the specified type. |
| 377 | QualType ASTContext::getReferenceType(QualType T) { |
| 378 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 379 | // structure. |
| 380 | llvm::FoldingSetNodeID ID; |
| 381 | ReferenceType::Profile(ID, T); |
| 382 | |
| 383 | void *InsertPos = 0; |
| 384 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 385 | return QualType(RT, 0); |
| 386 | |
| 387 | // If the referencee type isn't canonical, this won't be a canonical type |
| 388 | // either, so fill in the canonical type field. |
| 389 | QualType Canonical; |
| 390 | if (!T->isCanonical()) { |
| 391 | Canonical = getReferenceType(T.getCanonicalType()); |
| 392 | |
| 393 | // Get the new insert position for the node we care about. |
| 394 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 395 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 396 | } |
| 397 | |
| 398 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 399 | Types.push_back(New); |
| 400 | ReferenceTypes.InsertNode(New, InsertPos); |
| 401 | return QualType(New, 0); |
| 402 | } |
| 403 | |
| 404 | /// getArrayType - Return the unique reference to the type for an array of the |
| 405 | /// specified element type. |
| 406 | QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM, |
| 407 | unsigned EltTypeQuals, Expr *NumElts) { |
| 408 | // Unique array types, to guarantee there is only one array of a particular |
| 409 | // structure. |
| 410 | llvm::FoldingSetNodeID ID; |
| 411 | ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts); |
| 412 | |
| 413 | void *InsertPos = 0; |
| 414 | if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 415 | return QualType(ATP, 0); |
| 416 | |
| 417 | // If the element type isn't canonical, this won't be a canonical type either, |
| 418 | // so fill in the canonical type field. |
| 419 | QualType Canonical; |
| 420 | if (!EltTy->isCanonical()) { |
| 421 | Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals, |
| 422 | NumElts); |
| 423 | |
| 424 | // Get the new insert position for the node we care about. |
| 425 | ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 426 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 427 | } |
| 428 | |
| 429 | ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts); |
| 430 | ArrayTypes.InsertNode(New, InsertPos); |
| 431 | Types.push_back(New); |
| 432 | return QualType(New, 0); |
| 433 | } |
| 434 | |
| 435 | /// getVectorType - Return the unique reference to a vector type of |
| 436 | /// the specified element type and size. VectorType must be a built-in type. |
| 437 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
| 438 | BuiltinType *baseType; |
| 439 | |
| 440 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 441 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
| 442 | |
| 443 | // Check if we've already instantiated a vector of this type. |
| 444 | llvm::FoldingSetNodeID ID; |
| 445 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
| 446 | void *InsertPos = 0; |
| 447 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 448 | return QualType(VTP, 0); |
| 449 | |
| 450 | // If the element type isn't canonical, this won't be a canonical type either, |
| 451 | // so fill in the canonical type field. |
| 452 | QualType Canonical; |
| 453 | if (!vecType->isCanonical()) { |
| 454 | Canonical = getVectorType(vecType.getCanonicalType(), NumElts); |
| 455 | |
| 456 | // Get the new insert position for the node we care about. |
| 457 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 458 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 459 | } |
| 460 | VectorType *New = new VectorType(vecType, NumElts, Canonical); |
| 461 | VectorTypes.InsertNode(New, InsertPos); |
| 462 | Types.push_back(New); |
| 463 | return QualType(New, 0); |
| 464 | } |
| 465 | |
| 466 | /// getOCUVectorType - Return the unique reference to an OCU vector type of |
| 467 | /// the specified element type and size. VectorType must be a built-in type. |
| 468 | QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) { |
| 469 | BuiltinType *baseType; |
| 470 | |
| 471 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 472 | assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type"); |
| 473 | |
| 474 | // Check if we've already instantiated a vector of this type. |
| 475 | llvm::FoldingSetNodeID ID; |
| 476 | VectorType::Profile(ID, vecType, NumElts, Type::OCUVector); |
| 477 | void *InsertPos = 0; |
| 478 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 479 | return QualType(VTP, 0); |
| 480 | |
| 481 | // If the element type isn't canonical, this won't be a canonical type either, |
| 482 | // so fill in the canonical type field. |
| 483 | QualType Canonical; |
| 484 | if (!vecType->isCanonical()) { |
| 485 | Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts); |
| 486 | |
| 487 | // Get the new insert position for the node we care about. |
| 488 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 489 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 490 | } |
| 491 | OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical); |
| 492 | VectorTypes.InsertNode(New, InsertPos); |
| 493 | Types.push_back(New); |
| 494 | return QualType(New, 0); |
| 495 | } |
| 496 | |
| 497 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 498 | /// |
| 499 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 500 | // Unique functions, to guarantee there is only one function of a particular |
| 501 | // structure. |
| 502 | llvm::FoldingSetNodeID ID; |
| 503 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 504 | |
| 505 | void *InsertPos = 0; |
| 506 | if (FunctionTypeNoProto *FT = |
| 507 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 508 | return QualType(FT, 0); |
| 509 | |
| 510 | QualType Canonical; |
| 511 | if (!ResultTy->isCanonical()) { |
| 512 | Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType()); |
| 513 | |
| 514 | // Get the new insert position for the node we care about. |
| 515 | FunctionTypeNoProto *NewIP = |
| 516 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 517 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 518 | } |
| 519 | |
| 520 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 521 | Types.push_back(New); |
| 522 | FunctionTypeProtos.InsertNode(New, InsertPos); |
| 523 | return QualType(New, 0); |
| 524 | } |
| 525 | |
| 526 | /// getFunctionType - Return a normal function type with a typed argument |
| 527 | /// list. isVariadic indicates whether the argument list includes '...'. |
| 528 | QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray, |
| 529 | unsigned NumArgs, bool isVariadic) { |
| 530 | // Unique functions, to guarantee there is only one function of a particular |
| 531 | // structure. |
| 532 | llvm::FoldingSetNodeID ID; |
| 533 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic); |
| 534 | |
| 535 | void *InsertPos = 0; |
| 536 | if (FunctionTypeProto *FTP = |
| 537 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 538 | return QualType(FTP, 0); |
| 539 | |
| 540 | // Determine whether the type being created is already canonical or not. |
| 541 | bool isCanonical = ResultTy->isCanonical(); |
| 542 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 543 | if (!ArgArray[i]->isCanonical()) |
| 544 | isCanonical = false; |
| 545 | |
| 546 | // If this type isn't canonical, get the canonical version of it. |
| 547 | QualType Canonical; |
| 548 | if (!isCanonical) { |
| 549 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 550 | CanonicalArgs.reserve(NumArgs); |
| 551 | for (unsigned i = 0; i != NumArgs; ++i) |
| 552 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 553 | |
| 554 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 555 | &CanonicalArgs[0], NumArgs, |
| 556 | isVariadic); |
| 557 | |
| 558 | // Get the new insert position for the node we care about. |
| 559 | FunctionTypeProto *NewIP = |
| 560 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 561 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 562 | } |
| 563 | |
| 564 | // FunctionTypeProto objects are not allocated with new because they have a |
| 565 | // variable size array (for parameter types) at the end of them. |
| 566 | FunctionTypeProto *FTP = |
| 567 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
| 568 | NumArgs*sizeof(QualType)); |
| 569 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 570 | Canonical); |
| 571 | Types.push_back(FTP); |
| 572 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 573 | return QualType(FTP, 0); |
| 574 | } |
| 575 | |
| 576 | /// getTypedefType - Return the unique reference to the type for the |
| 577 | /// specified typename decl. |
| 578 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 579 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 580 | |
| 581 | QualType Canonical = Decl->getUnderlyingType().getCanonicalType(); |
| 582 | Decl->TypeForDecl = new TypedefType(Decl, Canonical); |
| 583 | Types.push_back(Decl->TypeForDecl); |
| 584 | return QualType(Decl->TypeForDecl, 0); |
| 585 | } |
| 586 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 587 | /// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique |
| 588 | /// TypeOfExpr AST's (since expression's are never shared). For example, |
| 589 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 590 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 591 | /// on canonical type's (which are always unique). |
Steve Naroff | 11b649c | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 592 | QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 593 | QualType Canonical = tofExpr->getType().getCanonicalType(); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 594 | TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical); |
| 595 | Types.push_back(toe); |
| 596 | return QualType(toe, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 599 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 600 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 601 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 602 | /// an issue. This doesn't effect the type checker, since it operates |
| 603 | /// on canonical type's (which are always unique). |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 604 | QualType ASTContext::getTypeOfType(QualType tofType) { |
| 605 | QualType Canonical = tofType.getCanonicalType(); |
Steve Naroff | 0604dd9 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 606 | TypeOfType *tot = new TypeOfType(tofType, Canonical); |
| 607 | Types.push_back(tot); |
| 608 | return QualType(tot, 0); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 611 | /// getTagDeclType - Return the unique reference to the type for the |
| 612 | /// specified TagDecl (struct/union/class/enum) decl. |
| 613 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
| 614 | // The decl stores the type cache. |
| 615 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 616 | |
| 617 | Decl->TypeForDecl = new TagType(Decl, QualType()); |
| 618 | Types.push_back(Decl->TypeForDecl); |
| 619 | return QualType(Decl->TypeForDecl, 0); |
| 620 | } |
| 621 | |
| 622 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 623 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 624 | /// needs to agree with the definition in <stddef.h>. |
| 625 | QualType ASTContext::getSizeType() const { |
| 626 | // On Darwin, size_t is defined as a "long unsigned int". |
| 627 | // FIXME: should derive from "Target". |
| 628 | return UnsignedLongTy; |
| 629 | } |
| 630 | |
| 631 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 632 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 633 | QualType ASTContext::getPointerDiffType() const { |
| 634 | // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug... |
| 635 | // FIXME: should derive from "Target". |
| 636 | return IntTy; |
| 637 | } |
| 638 | |
| 639 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 640 | /// routine will assert if passed a built-in type that isn't an integer or enum. |
| 641 | static int getIntegerRank(QualType t) { |
| 642 | if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) { |
| 643 | assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum"); |
| 644 | return 4; |
| 645 | } |
| 646 | |
| 647 | const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType()); |
| 648 | switch (BT->getKind()) { |
| 649 | default: |
| 650 | assert(0 && "getIntegerRank(): not a built-in integer"); |
| 651 | case BuiltinType::Bool: |
| 652 | return 1; |
| 653 | case BuiltinType::Char_S: |
| 654 | case BuiltinType::Char_U: |
| 655 | case BuiltinType::SChar: |
| 656 | case BuiltinType::UChar: |
| 657 | return 2; |
| 658 | case BuiltinType::Short: |
| 659 | case BuiltinType::UShort: |
| 660 | return 3; |
| 661 | case BuiltinType::Int: |
| 662 | case BuiltinType::UInt: |
| 663 | return 4; |
| 664 | case BuiltinType::Long: |
| 665 | case BuiltinType::ULong: |
| 666 | return 5; |
| 667 | case BuiltinType::LongLong: |
| 668 | case BuiltinType::ULongLong: |
| 669 | return 6; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /// getFloatingRank - Return a relative rank for floating point types. |
| 674 | /// This routine will assert if passed a built-in type that isn't a float. |
| 675 | static int getFloatingRank(QualType T) { |
| 676 | T = T.getCanonicalType(); |
| 677 | if (ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 678 | return getFloatingRank(CT->getElementType()); |
| 679 | |
| 680 | switch (cast<BuiltinType>(T)->getKind()) { |
| 681 | default: assert(0 && "getFloatingPointRank(): not a floating type"); |
| 682 | case BuiltinType::Float: return FloatRank; |
| 683 | case BuiltinType::Double: return DoubleRank; |
| 684 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 685 | } |
| 686 | } |
| 687 | |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame^] | 688 | /// getFloatingTypeOfSizeWithinDomain - Returns the either a real floating |
| 689 | /// point type or a complex type (based on typeDomain) of typeSize. |
| 690 | /// typeSize is expected to be a floating point type (real or complex). |
| 691 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain( |
| 692 | QualType typeSize, QualType typeDomain) const { |
| 693 | if (typeDomain->isComplexType()) { |
| 694 | switch (getFloatingRank(typeSize)) { |
| 695 | default: assert(0 && "convertRankToComplex(): illegal value for rank"); |
| 696 | case FloatRank: return FloatComplexTy; |
| 697 | case DoubleRank: return DoubleComplexTy; |
| 698 | case LongDoubleRank: return LongDoubleComplexTy; |
| 699 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 700 | } |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame^] | 701 | if (typeDomain->isRealFloatingType()) { |
| 702 | switch (getFloatingRank(typeSize)) { |
| 703 | default: assert(0 && "convertRankToComplex(): illegal value for rank"); |
| 704 | case FloatRank: return FloatTy; |
| 705 | case DoubleRank: return DoubleTy; |
| 706 | case LongDoubleRank: return LongDoubleTy; |
| 707 | } |
| 708 | } |
| 709 | assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | // maxFloatingType - handles the simple case, both operands are floats. |
| 713 | QualType ASTContext::maxFloatingType(QualType lt, QualType rt) { |
| 714 | return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt; |
| 715 | } |
| 716 | |
| 717 | // maxIntegerType - Returns the highest ranked integer type. Handles 3 case: |
| 718 | // unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1. |
| 719 | QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) { |
| 720 | if (lhs == rhs) return lhs; |
| 721 | |
| 722 | bool t1Unsigned = lhs->isUnsignedIntegerType(); |
| 723 | bool t2Unsigned = rhs->isUnsignedIntegerType(); |
| 724 | |
| 725 | if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned)) |
| 726 | return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs; |
| 727 | |
| 728 | // We have two integer types with differing signs |
| 729 | QualType unsignedType = t1Unsigned ? lhs : rhs; |
| 730 | QualType signedType = t1Unsigned ? rhs : lhs; |
| 731 | |
| 732 | if (getIntegerRank(unsignedType) >= getIntegerRank(signedType)) |
| 733 | return unsignedType; |
| 734 | else { |
| 735 | // FIXME: Need to check if the signed type can represent all values of the |
| 736 | // unsigned type. If it can, then the result is the signed type. |
| 737 | // If it can't, then the result is the unsigned version of the signed type. |
| 738 | // Should probably add a helper that returns a signed integer type from |
| 739 | // an unsigned (and vice versa). C99 6.3.1.8. |
| 740 | return signedType; |
| 741 | } |
| 742 | } |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 743 | |
| 744 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 745 | QualType ASTContext::getCFConstantStringType() { |
| 746 | if (!CFConstantStringTypeDecl) { |
| 747 | CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(), |
| 748 | &Idents.get("__builtin_CFString"), |
| 749 | 0); |
| 750 | |
| 751 | QualType FieldTypes[4]; |
| 752 | |
| 753 | // const int *isa; |
| 754 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
| 755 | // int flags; |
| 756 | FieldTypes[1] = IntTy; |
| 757 | // const char *str; |
| 758 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
| 759 | // long length; |
| 760 | FieldTypes[3] = LongTy; |
| 761 | // Create fields |
| 762 | FieldDecl *FieldDecls[4]; |
| 763 | |
| 764 | for (unsigned i = 0; i < 4; ++i) |
| 765 | FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i], 0); |
| 766 | |
| 767 | CFConstantStringTypeDecl->defineBody(FieldDecls, 4); |
| 768 | } |
| 769 | |
| 770 | return getTagDeclType(CFConstantStringTypeDecl); |
| 771 | } |