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