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