Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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 NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
| 44 | |
| 45 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
| 46 | |
| 47 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 48 | Type *T = Types[i]; |
| 49 | if (isa<BuiltinType>(T)) |
| 50 | ++NumBuiltin; |
| 51 | else if (isa<PointerType>(T)) |
| 52 | ++NumPointer; |
| 53 | else if (isa<ReferenceType>(T)) |
| 54 | ++NumReference; |
| 55 | else if (isa<ArrayType>(T)) |
| 56 | ++NumArray; |
| 57 | else if (isa<FunctionTypeNoProto>(T)) |
| 58 | ++NumFunctionNP; |
| 59 | else if (isa<FunctionTypeProto>(T)) |
| 60 | ++NumFunctionP; |
| 61 | else if (isa<TypedefType>(T)) |
| 62 | ++NumTypeName; |
| 63 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 64 | ++NumTagged; |
| 65 | switch (TT->getDecl()->getKind()) { |
| 66 | default: assert(0 && "Unknown tagged type!"); |
| 67 | case Decl::Struct: ++NumTagStruct; break; |
| 68 | case Decl::Union: ++NumTagUnion; break; |
| 69 | case Decl::Class: ++NumTagClass; break; |
| 70 | case Decl::Enum: ++NumTagEnum; break; |
| 71 | } |
| 72 | } else { |
| 73 | assert(0 && "Unknown type!"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 78 | fprintf(stderr, " %d pointer types\n", NumPointer); |
| 79 | fprintf(stderr, " %d reference types\n", NumReference); |
| 80 | fprintf(stderr, " %d array types\n", NumArray); |
| 81 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 82 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 83 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 84 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 85 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 86 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 87 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 88 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
| 89 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 90 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
| 91 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 92 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
| 93 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType))); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 98 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | void ASTContext::InitBuiltinTypes() { |
| 103 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 104 | |
| 105 | // C99 6.2.5p19. |
| 106 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 107 | |
| 108 | // C99 6.2.5p2. |
| 109 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 110 | // C99 6.2.5p3. |
| 111 | if (Target.isCharSigned(SourceLocation())) |
| 112 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 113 | else |
| 114 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 115 | // C99 6.2.5p4. |
| 116 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 117 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 118 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 119 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 120 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 121 | |
| 122 | // C99 6.2.5p6. |
| 123 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 124 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 125 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 126 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 127 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 128 | |
| 129 | // C99 6.2.5p10. |
| 130 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 131 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 132 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
| 133 | |
| 134 | // C99 6.2.5p11. |
| 135 | FloatComplexTy = getComplexType(FloatTy); |
| 136 | DoubleComplexTy = getComplexType(DoubleTy); |
| 137 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
| 138 | } |
| 139 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 140 | |
| 141 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 142 | /// does not work on incomplete types. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 143 | std::pair<uint64_t, unsigned> |
| 144 | ASTContext::getTypeInfo(QualType T, SourceLocation L) { |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 145 | T = T.getCanonicalType(); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 146 | uint64_t Size; |
| 147 | unsigned Align; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 148 | switch (T->getTypeClass()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 149 | default: |
| 150 | case Type::Complex: |
| 151 | case Type::Array: |
| 152 | case Type::Vector: |
| 153 | case Type::TypeName: |
| 154 | case Type::Tagged: |
| 155 | assert(0 && "Unimplemented type sizes!"); |
| 156 | case Type::FunctionNoProto: |
| 157 | case Type::FunctionProto: |
| 158 | assert(0 && "Incomplete types have no size!"); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 159 | case Type::Builtin: { |
| 160 | // FIXME: need to use TargetInfo to derive the target specific sizes. This |
| 161 | // implementation will suffice for play with vector support. |
| 162 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 163 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 164 | case BuiltinType::Void: |
| 165 | assert(0 && "Incomplete types have no size!"); |
| 166 | case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 167 | case BuiltinType::Char_S: |
| 168 | case BuiltinType::Char_U: |
| 169 | case BuiltinType::UChar: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 170 | case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 171 | case BuiltinType::UShort: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 172 | case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 173 | case BuiltinType::UInt: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 174 | case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 175 | case BuiltinType::ULong: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 176 | case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 177 | case BuiltinType::ULongLong: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 178 | case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break; |
| 179 | case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break; |
| 180 | case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break; |
| 181 | case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 184 | case Type::Pointer: Target.getPointerInfo(Size, Align, L); break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 185 | case Type::Reference: |
Chris Lattner | 7ab2ed8 | 2007-07-13 22:16:13 +0000 | [diff] [blame] | 186 | // "When applied to a reference or a reference type, the result is the size |
| 187 | // of the referenced type." C++98 5.3.3p2: expr.sizeof |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 188 | return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 190 | |
| 191 | return std::make_pair(Size, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | // Type creation/memoization methods |
| 196 | //===----------------------------------------------------------------------===// |
| 197 | |
| 198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 200 | /// number with the specified element type. |
| 201 | QualType ASTContext::getComplexType(QualType T) { |
| 202 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 203 | // structure. |
| 204 | llvm::FoldingSetNodeID ID; |
| 205 | ComplexType::Profile(ID, T); |
| 206 | |
| 207 | void *InsertPos = 0; |
| 208 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 209 | return QualType(CT, 0); |
| 210 | |
| 211 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 212 | // so fill in the canonical type field. |
| 213 | QualType Canonical; |
| 214 | if (!T->isCanonical()) { |
| 215 | Canonical = getComplexType(T.getCanonicalType()); |
| 216 | |
| 217 | // Get the new insert position for the node we care about. |
| 218 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 219 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 220 | } |
| 221 | ComplexType *New = new ComplexType(T, Canonical); |
| 222 | Types.push_back(New); |
| 223 | ComplexTypes.InsertNode(New, InsertPos); |
| 224 | return QualType(New, 0); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 229 | /// the specified type. |
| 230 | QualType ASTContext::getPointerType(QualType T) { |
| 231 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 232 | // structure. |
| 233 | llvm::FoldingSetNodeID ID; |
| 234 | PointerType::Profile(ID, T); |
| 235 | |
| 236 | void *InsertPos = 0; |
| 237 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 238 | return QualType(PT, 0); |
| 239 | |
| 240 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 241 | // so fill in the canonical type field. |
| 242 | QualType Canonical; |
| 243 | if (!T->isCanonical()) { |
| 244 | Canonical = getPointerType(T.getCanonicalType()); |
| 245 | |
| 246 | // Get the new insert position for the node we care about. |
| 247 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 248 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 249 | } |
| 250 | PointerType *New = new PointerType(T, Canonical); |
| 251 | Types.push_back(New); |
| 252 | PointerTypes.InsertNode(New, InsertPos); |
| 253 | return QualType(New, 0); |
| 254 | } |
| 255 | |
| 256 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 257 | /// to the specified type. |
| 258 | QualType ASTContext::getReferenceType(QualType T) { |
| 259 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 260 | // structure. |
| 261 | llvm::FoldingSetNodeID ID; |
| 262 | ReferenceType::Profile(ID, T); |
| 263 | |
| 264 | void *InsertPos = 0; |
| 265 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 266 | return QualType(RT, 0); |
| 267 | |
| 268 | // If the referencee type isn't canonical, this won't be a canonical type |
| 269 | // either, so fill in the canonical type field. |
| 270 | QualType Canonical; |
| 271 | if (!T->isCanonical()) { |
| 272 | Canonical = getReferenceType(T.getCanonicalType()); |
| 273 | |
| 274 | // Get the new insert position for the node we care about. |
| 275 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 276 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 277 | } |
| 278 | |
| 279 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 280 | Types.push_back(New); |
| 281 | ReferenceTypes.InsertNode(New, InsertPos); |
| 282 | return QualType(New, 0); |
| 283 | } |
| 284 | |
| 285 | /// getArrayType - Return the unique reference to the type for an array of the |
| 286 | /// specified element type. |
| 287 | QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM, |
| 288 | unsigned EltTypeQuals, Expr *NumElts) { |
| 289 | // Unique array types, to guarantee there is only one array of a particular |
| 290 | // structure. |
| 291 | llvm::FoldingSetNodeID ID; |
| 292 | ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts); |
| 293 | |
| 294 | void *InsertPos = 0; |
| 295 | if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 296 | return QualType(ATP, 0); |
| 297 | |
| 298 | // If the element type isn't canonical, this won't be a canonical type either, |
| 299 | // so fill in the canonical type field. |
| 300 | QualType Canonical; |
| 301 | if (!EltTy->isCanonical()) { |
| 302 | Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals, |
| 303 | NumElts); |
| 304 | |
| 305 | // Get the new insert position for the node we care about. |
| 306 | ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 307 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 308 | } |
| 309 | |
| 310 | ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts); |
| 311 | ArrayTypes.InsertNode(New, InsertPos); |
| 312 | Types.push_back(New); |
| 313 | return QualType(New, 0); |
| 314 | } |
| 315 | |
| 316 | /// convertToVectorType - Return the unique reference to a vector type of |
| 317 | /// the specified element type and size. VectorType can be a pointer, array, |
| 318 | /// function, or built-in type (i.e. _Bool, integer, or float). |
| 319 | QualType ASTContext::convertToVectorType(QualType vecType, unsigned NumElts) { |
| 320 | BuiltinType *baseType; |
| 321 | |
| 322 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 323 | assert(baseType != 0 && |
| 324 | "convertToVectorType(): Complex vector types unimplemented"); |
| 325 | |
| 326 | // Check if we've already instantiated a vector of this type. |
| 327 | llvm::FoldingSetNodeID ID; |
| 328 | VectorType::Profile(ID, vecType, NumElts); |
| 329 | void *InsertPos = 0; |
| 330 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 331 | return QualType(VTP, 0); |
| 332 | |
| 333 | // If the element type isn't canonical, this won't be a canonical type either, |
| 334 | // so fill in the canonical type field. |
| 335 | QualType Canonical; |
| 336 | if (!vecType->isCanonical()) { |
| 337 | Canonical = convertToVectorType(vecType.getCanonicalType(), NumElts); |
| 338 | |
| 339 | // Get the new insert position for the node we care about. |
| 340 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 341 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 342 | } |
| 343 | VectorType *New = new VectorType(vecType, NumElts, Canonical); |
| 344 | VectorTypes.InsertNode(New, InsertPos); |
| 345 | Types.push_back(New); |
| 346 | return QualType(New, 0); |
| 347 | } |
| 348 | |
| 349 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 350 | /// |
| 351 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 352 | // Unique functions, to guarantee there is only one function of a particular |
| 353 | // structure. |
| 354 | llvm::FoldingSetNodeID ID; |
| 355 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 356 | |
| 357 | void *InsertPos = 0; |
| 358 | if (FunctionTypeNoProto *FT = |
| 359 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 360 | return QualType(FT, 0); |
| 361 | |
| 362 | QualType Canonical; |
| 363 | if (!ResultTy->isCanonical()) { |
| 364 | Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType()); |
| 365 | |
| 366 | // Get the new insert position for the node we care about. |
| 367 | FunctionTypeNoProto *NewIP = |
| 368 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 369 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 370 | } |
| 371 | |
| 372 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 373 | Types.push_back(New); |
| 374 | FunctionTypeProtos.InsertNode(New, InsertPos); |
| 375 | return QualType(New, 0); |
| 376 | } |
| 377 | |
| 378 | /// getFunctionType - Return a normal function type with a typed argument |
| 379 | /// list. isVariadic indicates whether the argument list includes '...'. |
| 380 | QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray, |
| 381 | unsigned NumArgs, bool isVariadic) { |
| 382 | // Unique functions, to guarantee there is only one function of a particular |
| 383 | // structure. |
| 384 | llvm::FoldingSetNodeID ID; |
| 385 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic); |
| 386 | |
| 387 | void *InsertPos = 0; |
| 388 | if (FunctionTypeProto *FTP = |
| 389 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 390 | return QualType(FTP, 0); |
| 391 | |
| 392 | // Determine whether the type being created is already canonical or not. |
| 393 | bool isCanonical = ResultTy->isCanonical(); |
| 394 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 395 | if (!ArgArray[i]->isCanonical()) |
| 396 | isCanonical = false; |
| 397 | |
| 398 | // If this type isn't canonical, get the canonical version of it. |
| 399 | QualType Canonical; |
| 400 | if (!isCanonical) { |
| 401 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 402 | CanonicalArgs.reserve(NumArgs); |
| 403 | for (unsigned i = 0; i != NumArgs; ++i) |
| 404 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 405 | |
| 406 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 407 | &CanonicalArgs[0], NumArgs, |
| 408 | isVariadic); |
| 409 | |
| 410 | // Get the new insert position for the node we care about. |
| 411 | FunctionTypeProto *NewIP = |
| 412 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 413 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 414 | } |
| 415 | |
| 416 | // FunctionTypeProto objects are not allocated with new because they have a |
| 417 | // variable size array (for parameter types) at the end of them. |
| 418 | FunctionTypeProto *FTP = |
| 419 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
| 420 | (NumArgs-1)*sizeof(QualType)); |
| 421 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 422 | Canonical); |
| 423 | Types.push_back(FTP); |
| 424 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 425 | return QualType(FTP, 0); |
| 426 | } |
| 427 | |
| 428 | /// getTypedefType - Return the unique reference to the type for the |
| 429 | /// specified typename decl. |
| 430 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 431 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 432 | |
| 433 | QualType Canonical = Decl->getUnderlyingType().getCanonicalType(); |
| 434 | Decl->TypeForDecl = new TypedefType(Decl, Canonical); |
| 435 | Types.push_back(Decl->TypeForDecl); |
| 436 | return QualType(Decl->TypeForDecl, 0); |
| 437 | } |
| 438 | |
| 439 | /// getTagDeclType - Return the unique reference to the type for the |
| 440 | /// specified TagDecl (struct/union/class/enum) decl. |
| 441 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
| 442 | // The decl stores the type cache. |
| 443 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 444 | |
| 445 | Decl->TypeForDecl = new TagType(Decl, QualType()); |
| 446 | Types.push_back(Decl->TypeForDecl); |
| 447 | return QualType(Decl->TypeForDecl, 0); |
| 448 | } |
| 449 | |
| 450 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 451 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 452 | /// needs to agree with the definition in <stddef.h>. |
| 453 | QualType ASTContext::getSizeType() const { |
| 454 | // On Darwin, size_t is defined as a "long unsigned int". |
| 455 | // FIXME: should derive from "Target". |
| 456 | return UnsignedLongTy; |
| 457 | } |
| 458 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 459 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 460 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 461 | QualType ASTContext::getPointerDiffType() const { |
| 462 | // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug... |
| 463 | // FIXME: should derive from "Target". |
| 464 | return IntTy; |
| 465 | } |
| 466 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 468 | /// routine will assert if passed a built-in type that isn't an integer or enum. |
| 469 | static int getIntegerRank(QualType t) { |
| 470 | if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) { |
| 471 | assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum"); |
| 472 | return 4; |
| 473 | } |
| 474 | |
| 475 | const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType()); |
| 476 | switch (BT->getKind()) { |
| 477 | default: |
| 478 | assert(0 && "getIntegerRank(): not a built-in integer"); |
| 479 | case BuiltinType::Bool: |
| 480 | return 1; |
| 481 | case BuiltinType::Char_S: |
| 482 | case BuiltinType::Char_U: |
| 483 | case BuiltinType::SChar: |
| 484 | case BuiltinType::UChar: |
| 485 | return 2; |
| 486 | case BuiltinType::Short: |
| 487 | case BuiltinType::UShort: |
| 488 | return 3; |
| 489 | case BuiltinType::Int: |
| 490 | case BuiltinType::UInt: |
| 491 | return 4; |
| 492 | case BuiltinType::Long: |
| 493 | case BuiltinType::ULong: |
| 494 | return 5; |
| 495 | case BuiltinType::LongLong: |
| 496 | case BuiltinType::ULongLong: |
| 497 | return 6; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /// getFloatingRank - Return a relative rank for floating point types. |
| 502 | /// This routine will assert if passed a built-in type that isn't a float. |
| 503 | static int getFloatingRank(QualType T) { |
| 504 | T = T.getCanonicalType(); |
| 505 | if (ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 506 | return getFloatingRank(CT->getElementType()); |
| 507 | |
| 508 | switch (cast<BuiltinType>(T)->getKind()) { |
| 509 | default: assert(0 && "getFloatingPointRank(): not a floating type"); |
| 510 | case BuiltinType::Float: return FloatRank; |
| 511 | case BuiltinType::Double: return DoubleRank; |
| 512 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // maxComplexType - the following code handles 3 different combinations: |
| 517 | // complex/complex, complex/float, float/complex. |
| 518 | // When both operands are complex, the shorter operand is converted to the |
| 519 | // type of the longer, and that is the type of the result. This corresponds |
| 520 | // to what is done when combining two real floating-point operands. |
| 521 | // The fun begins when size promotion occur across type domains. g |
| 522 | // getFloatingRank & convertFloatingRankToComplexType handle this without |
| 523 | // enumerating all permutations. |
| 524 | // It also allows us to add new types without breakage. |
| 525 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 526 | // floating-point type, the less precise type is converted, within it's |
| 527 | // real or complex domain, to the precision of the other type. For example, |
| 528 | // when combining a "long double" with a "double _Complex", the |
| 529 | // "double _Complex" is promoted to "long double _Complex". |
| 530 | |
| 531 | QualType ASTContext::maxComplexType(QualType lt, QualType rt) const { |
| 532 | switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) { |
| 533 | default: assert(0 && "convertRankToComplex(): illegal value for rank"); |
| 534 | case FloatRank: return FloatComplexTy; |
| 535 | case DoubleRank: return DoubleComplexTy; |
| 536 | case LongDoubleRank: return LongDoubleComplexTy; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // maxFloatingType - handles the simple case, both operands are floats. |
| 541 | QualType ASTContext::maxFloatingType(QualType lt, QualType rt) { |
| 542 | return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt; |
| 543 | } |
| 544 | |
| 545 | // maxIntegerType - Returns the highest ranked integer type. Handles 3 case: |
| 546 | // unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1. |
| 547 | QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) { |
| 548 | if (lhs == rhs) return lhs; |
| 549 | |
| 550 | bool t1Unsigned = lhs->isUnsignedIntegerType(); |
| 551 | bool t2Unsigned = rhs->isUnsignedIntegerType(); |
| 552 | |
| 553 | if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned)) |
| 554 | return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs; |
| 555 | |
| 556 | // We have two integer types with differing signs |
| 557 | QualType unsignedType = t1Unsigned ? lhs : rhs; |
| 558 | QualType signedType = t1Unsigned ? rhs : lhs; |
| 559 | |
| 560 | if (getIntegerRank(unsignedType) >= getIntegerRank(signedType)) |
| 561 | return unsignedType; |
| 562 | else { |
| 563 | // FIXME: Need to check if the signed type can represent all values of the |
| 564 | // unsigned type. If it can, then the result is the signed type. |
| 565 | // If it can't, then the result is the unsigned version of the signed type. |
| 566 | // Should probably add a helper that returns a signed integer type from |
| 567 | // an unsigned (and vice versa). C99 6.3.1.8. |
| 568 | return signedType; |
| 569 | } |
| 570 | } |