Chris Lattner | ddc135e | 2006-11-10 06:34:16 +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" |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | using namespace clang; |
| 20 | |
| 21 | ASTContext::ASTContext(Preprocessor &pp) |
| 22 | : PP(pp), Target(pp.getTargetInfo()) { |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame^] | 23 | NumSlowLookups = 0; |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 24 | InitBuiltinTypes(); |
| 25 | } |
| 26 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 27 | ASTContext::~ASTContext() { |
| 28 | // Deallocate all the types. |
| 29 | while (!Types.empty()) { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 30 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) { |
| 31 | // Destroy the object, but don't call delete. These are malloc'd. |
| 32 | FT->~FunctionTypeProto(); |
| 33 | free(FT); |
| 34 | } else { |
| 35 | delete Types.back(); |
| 36 | } |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 37 | Types.pop_back(); |
| 38 | } |
| 39 | } |
| 40 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame^] | 41 | void ASTContext::PrintStats() const { |
| 42 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 43 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 44 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
| 45 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0; |
| 46 | |
| 47 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
| 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<ArrayType>(T)) |
| 56 | ++NumArray; |
| 57 | else if (isa<FunctionTypeNoProto>(T)) |
| 58 | ++NumFunctionNP; |
| 59 | else if (isa<FunctionTypeProto>(T)) |
| 60 | ++NumFunctionP; |
| 61 | else if (isa<TypeNameType>(T)) |
| 62 | ++NumTypeName; |
| 63 | else if (TaggedType *TT = dyn_cast<TaggedType>(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 array types\n", NumArray); |
| 80 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 81 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 82 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 83 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 84 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 85 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 86 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 87 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
| 88 | fprintf(stderr, " %d slow type lookups\n", NumSlowLookups); |
| 89 | } |
| 90 | |
| 91 | |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 92 | void ASTContext::InitBuiltinType(TypeRef &R, BuiltinType::Kind K) { |
| 93 | Types.push_back((R = new BuiltinType(K)).getTypePtr()); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 97 | void ASTContext::InitBuiltinTypes() { |
| 98 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 99 | |
| 100 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 101 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 102 | |
| 103 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 104 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 105 | // C99 6.2.5p3. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 106 | InitBuiltinType(CharTy, BuiltinType::Char); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 107 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 108 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 109 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 110 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 111 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 112 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 113 | |
| 114 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 115 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 116 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 117 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 118 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 119 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 120 | |
| 121 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 122 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 123 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 124 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 125 | |
| 126 | // C99 6.2.5p11. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 127 | InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex); |
| 128 | InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex); |
| 129 | InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 133 | /// the specified type. |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 134 | TypeRef ASTContext::getPointerType(TypeRef T) { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 135 | // FIXME: This is obviously braindead! |
| 136 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 137 | // structure. |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame^] | 138 | ++NumSlowLookups; |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 139 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
Chris Lattner | 4781466 | 2006-11-12 00:56:20 +0000 | [diff] [blame] | 140 | if (PointerType *PTy = dyn_cast<PointerType>(Types[i])) |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 141 | if (PTy->getPointeeType() == T) |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 142 | return Types[i]; |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 144 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 145 | // so fill in the canonical type field. |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 146 | Type *Canonical = 0; |
| 147 | if (!T->isCanonical()) |
| 148 | Canonical = getPointerType(T.getCanonicalType()).getTypePtr(); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 149 | |
| 150 | Types.push_back(new PointerType(T, Canonical)); |
| 151 | return Types.back(); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 154 | /// getArrayType - Return the unique reference to the type for an array of the |
| 155 | /// specified element type. |
| 156 | TypeRef ASTContext::getArrayType(TypeRef EltTy,ArrayType::ArraySizeModifier ASM, |
| 157 | unsigned EltTypeQuals, void *NumElts) { |
| 158 | #warning "IGNORING SIZE" |
| 159 | |
| 160 | // FIXME: This is obviously braindead! |
| 161 | // Unique array, to guarantee there is only one array of a particular |
| 162 | // structure. |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame^] | 163 | ++NumSlowLookups; |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 164 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 165 | if (ArrayType *ATy = dyn_cast<ArrayType>(Types[i])) |
| 166 | if (ATy->getElementType() == EltTy && |
| 167 | ATy->getSizeModifier() == ASM && |
| 168 | ATy->getIndexTypeQualifier() == EltTypeQuals) |
| 169 | return Types[i]; |
| 170 | |
| 171 | // If the element type isn't canonical, this won't be a canonical type either, |
| 172 | // so fill in the canonical type field. |
| 173 | Type *Canonical = 0; |
| 174 | if (!EltTy->isCanonical()) |
| 175 | Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals, |
| 176 | NumElts).getTypePtr(); |
| 177 | |
| 178 | Types.push_back(new ArrayType(EltTy, ASM, EltTypeQuals, Canonical)); |
| 179 | return Types.back(); |
| 180 | } |
| 181 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 182 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 183 | /// |
| 184 | TypeRef ASTContext::getFunctionTypeNoProto(TypeRef ResultTy) { |
| 185 | // FIXME: This is obviously braindead! |
| 186 | // Unique functions, to guarantee there is only one function of a particular |
| 187 | // structure. |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame^] | 188 | ++NumSlowLookups; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 189 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 190 | if (FunctionTypeNoProto *FTy = dyn_cast<FunctionTypeNoProto>(Types[i])) |
| 191 | if (FTy->getResultType() == ResultTy) |
| 192 | return Types[i]; |
| 193 | |
| 194 | Type *Canonical = 0; |
| 195 | if (!ResultTy->isCanonical()) |
| 196 | Canonical =getFunctionTypeNoProto(ResultTy.getCanonicalType()).getTypePtr(); |
| 197 | |
| 198 | Types.push_back(new FunctionTypeNoProto(ResultTy, Canonical)); |
| 199 | return Types.back(); |
| 200 | } |
| 201 | |
| 202 | /// getFunctionType - Return a normal function type with a typed argument |
| 203 | /// list. isVariadic indicates whether the argument list includes '...'. |
| 204 | TypeRef ASTContext::getFunctionType(TypeRef ResultTy, TypeRef *ArgArray, |
| 205 | unsigned NumArgs, bool isVariadic) { |
| 206 | // FIXME: This is obviously braindead! |
| 207 | // Unique functions, to guarantee there is only one function of a particular |
| 208 | // structure. |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame^] | 209 | ++NumSlowLookups; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 210 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 211 | if (FunctionTypeProto *FTy = dyn_cast<FunctionTypeProto>(Types[i])) |
| 212 | if (FTy->getResultType() == ResultTy && |
| 213 | FTy->getNumArgs() == NumArgs && |
| 214 | FTy->isVariadic() == isVariadic) { |
| 215 | bool Match = true; |
| 216 | for (unsigned arg = 0; arg != NumArgs; ++arg) { |
| 217 | if (FTy->getArgType(arg) != ArgArray[arg]) { |
| 218 | Match = false; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | if (Match) |
| 223 | return Types[i]; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Determine whether the type being created is already canonical or not. |
| 228 | bool isCanonical = ResultTy->isCanonical(); |
| 229 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 230 | if (!ArgArray[i]->isCanonical()) |
| 231 | isCanonical = false; |
| 232 | |
| 233 | // If this type isn't canonical, get the canonical version of it. |
| 234 | Type *Canonical = 0; |
| 235 | if (!isCanonical) { |
| 236 | SmallVector<TypeRef, 16> CanonicalArgs; |
| 237 | CanonicalArgs.reserve(NumArgs); |
| 238 | for (unsigned i = 0; i != NumArgs; ++i) |
| 239 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 240 | |
| 241 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 242 | &CanonicalArgs[0], NumArgs, |
| 243 | isVariadic).getTypePtr(); |
| 244 | } |
| 245 | |
| 246 | // FunctionTypeProto objects are not allocated with new because they have a |
| 247 | // variable size array (for parameter types) at the end of them. |
| 248 | FunctionTypeProto *FTP = |
| 249 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
| 250 | (NumArgs-1)*sizeof(TypeRef)); |
| 251 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 252 | Canonical); |
| 253 | |
| 254 | Types.push_back(FTP); |
| 255 | return FTP; |
| 256 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 257 | |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 258 | /// getTypeDeclType - Return the unique reference to the type for the |
| 259 | /// specified typename decl. |
| 260 | TypeRef ASTContext::getTypeDeclType(TypeDecl *Decl) { |
| 261 | // FIXME: This is obviously braindead! |
| 262 | // Unique TypeDecl, to guarantee there is only one TypeDeclType. |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame^] | 263 | ++NumSlowLookups; |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 264 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 265 | if (TypeNameType *Ty = dyn_cast<TypeNameType>(Types[i])) |
| 266 | if (Ty->getDecl() == Decl) |
| 267 | return Types[i]; |
| 268 | |
| 269 | // FIXME: does this lose qualifiers from the typedef?? |
| 270 | |
Chris Lattner | d0ee402 | 2007-01-22 07:39:30 +0000 | [diff] [blame] | 271 | Type *Canonical = Decl->getUnderlyingType().getTypePtr(); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 272 | Types.push_back(new TypeNameType(Decl, Canonical)); |
| 273 | return Types.back(); |
| 274 | } |
| 275 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 276 | /// getTagDeclType - Return the unique reference to the type for the |
| 277 | /// specified TagDecl (struct/union/class/enum) decl. |
| 278 | TypeRef ASTContext::getTagDeclType(TagDecl *Decl) { |
| 279 | // FIXME: This is obviously braindead! |
| 280 | // Unique TypeDecl, to guarantee there is only one TaggedType. |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame^] | 281 | ++NumSlowLookups; |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 282 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 283 | if (TaggedType *Ty = dyn_cast<TaggedType>(Types[i])) |
| 284 | if (Ty->getDecl() == Decl) |
| 285 | return Types[i]; |
| 286 | |
| 287 | // FIXME: does this lose qualifiers from the typedef?? |
| 288 | |
| 289 | Types.push_back(new TaggedType(Decl, 0)); |
| 290 | return Types.back(); |
| 291 | } |
| 292 | |
| 293 | |