blob: 8471494055f29b42eb74e6e851d3af135efd5dbe [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- 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 Lattnerd0342e52006-11-20 04:02:15 +000015#include "clang/AST/Decl.h"
Chris Lattnerddc135e2006-11-10 06:34:16 +000016#include "clang/Lex/Preprocessor.h"
Chris Lattnerc6ad8132006-12-02 07:52:18 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattnerddc135e2006-11-10 06:34:16 +000018using namespace llvm;
19using namespace clang;
20
21ASTContext::ASTContext(Preprocessor &pp)
22 : PP(pp), Target(pp.getTargetInfo()) {
Chris Lattner970e54e2006-11-12 00:37:36 +000023 InitBuiltinTypes();
Chris Lattner10a5b382007-01-29 05:24:35 +000024 BuiltinInfo.InitializeBuiltins(PP.getIdentifierTable(), Target);
Chris Lattner970e54e2006-11-12 00:37:36 +000025}
26
Chris Lattnerd5973eb2006-11-12 00:53:46 +000027ASTContext::~ASTContext() {
28 // Deallocate all the types.
29 while (!Types.empty()) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +000030 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 Lattnerd5973eb2006-11-12 00:53:46 +000037 Types.pop_back();
38 }
39}
40
Chris Lattner4eb445d2007-01-26 01:27:23 +000041void 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;
Chris Lattner32d920b2007-01-26 02:01:53 +000061 else if (isa<TypedefType>(T))
Chris Lattner4eb445d2007-01-26 01:27:23 +000062 ++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);
Chris Lattner4eb445d2007-01-26 01:27:23 +000088}
89
90
Chris Lattner726f97b2006-12-03 02:57:32 +000091void ASTContext::InitBuiltinType(TypeRef &R, BuiltinType::Kind K) {
92 Types.push_back((R = new BuiltinType(K)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +000093}
94
95
Chris Lattner970e54e2006-11-12 00:37:36 +000096void ASTContext::InitBuiltinTypes() {
97 assert(VoidTy.isNull() && "Context reinitialized?");
98
99 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000100 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000101
102 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000103 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000104 // C99 6.2.5p3.
Chris Lattner726f97b2006-12-03 02:57:32 +0000105 InitBuiltinType(CharTy, BuiltinType::Char);
Chris Lattner970e54e2006-11-12 00:37:36 +0000106 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000107 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
108 InitBuiltinType(ShortTy, BuiltinType::Short);
109 InitBuiltinType(IntTy, BuiltinType::Int);
110 InitBuiltinType(LongTy, BuiltinType::Long);
111 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000112
113 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000114 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
115 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
116 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
117 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
118 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000119
120 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000121 InitBuiltinType(FloatTy, BuiltinType::Float);
122 InitBuiltinType(DoubleTy, BuiltinType::Double);
123 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000124
125 // C99 6.2.5p11.
Chris Lattner726f97b2006-12-03 02:57:32 +0000126 InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex);
127 InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex);
128 InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex);
Chris Lattner970e54e2006-11-12 00:37:36 +0000129}
130
131/// getPointerType - Return the uniqued reference to the type for a pointer to
132/// the specified type.
Chris Lattner7ccecb92006-11-12 08:50:50 +0000133TypeRef ASTContext::getPointerType(TypeRef T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000134 // Unique pointers, to guarantee there is only one pointer of a particular
135 // structure.
Chris Lattner67521df2007-01-27 01:29:36 +0000136 FoldingSetNodeID ID;
137 PointerType::Profile(ID, T);
138
139 void *InsertPos = 0;
140 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
141 return PT;
Chris Lattner970e54e2006-11-12 00:37:36 +0000142
Chris Lattner7ccecb92006-11-12 08:50:50 +0000143 // If the pointee type isn't canonical, this won't be a canonical type either,
144 // so fill in the canonical type field.
Chris Lattner970e54e2006-11-12 00:37:36 +0000145 Type *Canonical = 0;
Chris Lattner67521df2007-01-27 01:29:36 +0000146 if (!T->isCanonical()) {
Chris Lattner970e54e2006-11-12 00:37:36 +0000147 Canonical = getPointerType(T.getCanonicalType()).getTypePtr();
Chris Lattner67521df2007-01-27 01:29:36 +0000148
149 // Get the new insert position for the node we care about.
150 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
151 assert(NewIP == 0 && "Shouldn't be in the map!");
152 }
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000153
Chris Lattner67521df2007-01-27 01:29:36 +0000154 PointerType *New = new PointerType(T, Canonical);
155 Types.push_back(New);
156 PointerTypes.InsertNode(New, InsertPos);
157 return New;
Chris Lattnerddc135e2006-11-10 06:34:16 +0000158}
159
Chris Lattner7ccecb92006-11-12 08:50:50 +0000160/// getArrayType - Return the unique reference to the type for an array of the
161/// specified element type.
162TypeRef ASTContext::getArrayType(TypeRef EltTy,ArrayType::ArraySizeModifier ASM,
Steve Naroff408451b2007-02-26 22:17:12 +0000163 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000164#warning "IGNORING SIZE"
165
Chris Lattner36f8e652007-01-27 08:31:04 +0000166 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000167 // structure.
Chris Lattner36f8e652007-01-27 08:31:04 +0000168 FoldingSetNodeID ID;
169 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy);
Steve Naroff408451b2007-02-26 22:17:12 +0000170
Chris Lattner36f8e652007-01-27 08:31:04 +0000171 void *InsertPos = 0;
172 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
173 return ATP;
Chris Lattner7ccecb92006-11-12 08:50:50 +0000174
175 // If the element type isn't canonical, this won't be a canonical type either,
176 // so fill in the canonical type field.
177 Type *Canonical = 0;
Chris Lattner36f8e652007-01-27 08:31:04 +0000178 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000179 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
180 NumElts).getTypePtr();
Chris Lattner36f8e652007-01-27 08:31:04 +0000181
182 // Get the new insert position for the node we care about.
183 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
184 assert(NewIP == 0 && "Shouldn't be in the map!");
185 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000186
Chris Lattner36f8e652007-01-27 08:31:04 +0000187 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical);
188 ArrayTypes.InsertNode(New, InsertPos);
189 Types.push_back(New);
190 return New;
Chris Lattner7ccecb92006-11-12 08:50:50 +0000191}
192
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000193/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
194///
195TypeRef ASTContext::getFunctionTypeNoProto(TypeRef ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000196 // Unique functions, to guarantee there is only one function of a particular
197 // structure.
Chris Lattner47955de2007-01-27 08:37:20 +0000198 FoldingSetNodeID ID;
199 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000200
Chris Lattner47955de2007-01-27 08:37:20 +0000201 void *InsertPos = 0;
202 if (FunctionTypeNoProto *FT =
203 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
204 return FT;
205
206 Type *Canonical = 0;
207 if (!ResultTy->isCanonical()) {
208 Canonical =getFunctionTypeNoProto(ResultTy.getCanonicalType()).getTypePtr();
209
210 // Get the new insert position for the node we care about.
211 FunctionTypeNoProto *NewIP =
212 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
213 assert(NewIP == 0 && "Shouldn't be in the map!");
214 }
215
216 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
217 Types.push_back(New);
218 FunctionTypeProtos.InsertNode(New, InsertPos);
219 return New;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000220}
221
222/// getFunctionType - Return a normal function type with a typed argument
223/// list. isVariadic indicates whether the argument list includes '...'.
224TypeRef ASTContext::getFunctionType(TypeRef ResultTy, TypeRef *ArgArray,
225 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000226 // Unique functions, to guarantee there is only one function of a particular
227 // structure.
Chris Lattnerfd4de792007-01-27 01:15:32 +0000228 FoldingSetNodeID ID;
229 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
230
231 void *InsertPos = 0;
232 if (FunctionTypeProto *FTP =
233 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
234 return FTP;
235
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000236 // Determine whether the type being created is already canonical or not.
237 bool isCanonical = ResultTy->isCanonical();
238 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
239 if (!ArgArray[i]->isCanonical())
240 isCanonical = false;
241
242 // If this type isn't canonical, get the canonical version of it.
243 Type *Canonical = 0;
244 if (!isCanonical) {
245 SmallVector<TypeRef, 16> CanonicalArgs;
246 CanonicalArgs.reserve(NumArgs);
247 for (unsigned i = 0; i != NumArgs; ++i)
248 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
249
250 Canonical = getFunctionType(ResultTy.getCanonicalType(),
251 &CanonicalArgs[0], NumArgs,
252 isVariadic).getTypePtr();
Chris Lattnerfd4de792007-01-27 01:15:32 +0000253
254 // Get the new insert position for the node we care about.
255 FunctionTypeProto *NewIP =
256 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
257 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000258 }
259
260 // FunctionTypeProto objects are not allocated with new because they have a
261 // variable size array (for parameter types) at the end of them.
262 FunctionTypeProto *FTP =
263 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
264 (NumArgs-1)*sizeof(TypeRef));
265 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
266 Canonical);
267
268 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000269 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000270 return FTP;
271}
Chris Lattneref51c202006-11-10 07:17:23 +0000272
Chris Lattner32d920b2007-01-26 02:01:53 +0000273/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000274/// specified typename decl.
Chris Lattner32d920b2007-01-26 02:01:53 +0000275TypeRef ASTContext::getTypedefType(TypedefDecl *Decl) {
Chris Lattner6668fc62007-01-26 02:07:07 +0000276 if (Decl->TypeForDecl) return Decl->TypeForDecl;
Chris Lattnerd0342e52006-11-20 04:02:15 +0000277
Chris Lattner6668fc62007-01-26 02:07:07 +0000278 // FIXME: does this lose qualifiers from the typedef??
Chris Lattnerd0ee4022007-01-22 07:39:30 +0000279 Type *Canonical = Decl->getUnderlyingType().getTypePtr();
Chris Lattner6668fc62007-01-26 02:07:07 +0000280 Types.push_back(Decl->TypeForDecl = new TypedefType(Decl, Canonical));
Chris Lattnerd0342e52006-11-20 04:02:15 +0000281 return Types.back();
282}
283
Chris Lattnerfb072462007-01-23 05:45:31 +0000284/// getTagDeclType - Return the unique reference to the type for the
285/// specified TagDecl (struct/union/class/enum) decl.
286TypeRef ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000287 // The decl stores the type cache.
288 if (Decl->TypeForDecl) return Decl->TypeForDecl;
Chris Lattnerfb072462007-01-23 05:45:31 +0000289
Chris Lattner733067d2007-01-26 01:42:24 +0000290 Types.push_back(Decl->TypeForDecl = new TaggedType(Decl, 0));
Chris Lattnerfb072462007-01-23 05:45:31 +0000291 return Types.back();
292}
293
294