blob: d19cc0e54270d514059f0af883e633c4d6e31803 [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 Lattner4eb445d2007-01-26 01:27:23 +000023 NumSlowLookups = 0;
Chris Lattner970e54e2006-11-12 00:37:36 +000024 InitBuiltinTypes();
25}
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;
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 Lattner726f97b2006-12-03 02:57:32 +000092void ASTContext::InitBuiltinType(TypeRef &R, BuiltinType::Kind K) {
93 Types.push_back((R = new BuiltinType(K)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +000094}
95
96
Chris Lattner970e54e2006-11-12 00:37:36 +000097void ASTContext::InitBuiltinTypes() {
98 assert(VoidTy.isNull() && "Context reinitialized?");
99
100 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000101 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000102
103 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000104 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000105 // C99 6.2.5p3.
Chris Lattner726f97b2006-12-03 02:57:32 +0000106 InitBuiltinType(CharTy, BuiltinType::Char);
Chris Lattner970e54e2006-11-12 00:37:36 +0000107 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000108 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 Lattner970e54e2006-11-12 00:37:36 +0000113
114 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000115 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 Lattner970e54e2006-11-12 00:37:36 +0000120
121 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000122 InitBuiltinType(FloatTy, BuiltinType::Float);
123 InitBuiltinType(DoubleTy, BuiltinType::Double);
124 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000125
126 // C99 6.2.5p11.
Chris Lattner726f97b2006-12-03 02:57:32 +0000127 InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex);
128 InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex);
129 InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex);
Chris Lattner970e54e2006-11-12 00:37:36 +0000130}
131
132/// getPointerType - Return the uniqued reference to the type for a pointer to
133/// the specified type.
Chris Lattner7ccecb92006-11-12 08:50:50 +0000134TypeRef ASTContext::getPointerType(TypeRef T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000135 // FIXME: This is obviously braindead!
136 // Unique pointers, to guarantee there is only one pointer of a particular
137 // structure.
Chris Lattner4eb445d2007-01-26 01:27:23 +0000138 ++NumSlowLookups;
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000139 for (unsigned i = 0, e = Types.size(); i != e; ++i)
Chris Lattner47814662006-11-12 00:56:20 +0000140 if (PointerType *PTy = dyn_cast<PointerType>(Types[i]))
Chris Lattner7ccecb92006-11-12 08:50:50 +0000141 if (PTy->getPointeeType() == T)
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000142 return Types[i];
Chris Lattner970e54e2006-11-12 00:37:36 +0000143
Chris Lattner7ccecb92006-11-12 08:50:50 +0000144 // If the pointee type isn't canonical, this won't be a canonical type either,
145 // so fill in the canonical type field.
Chris Lattner970e54e2006-11-12 00:37:36 +0000146 Type *Canonical = 0;
147 if (!T->isCanonical())
148 Canonical = getPointerType(T.getCanonicalType()).getTypePtr();
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000149
150 Types.push_back(new PointerType(T, Canonical));
151 return Types.back();
Chris Lattnerddc135e2006-11-10 06:34:16 +0000152}
153
Chris Lattner7ccecb92006-11-12 08:50:50 +0000154/// getArrayType - Return the unique reference to the type for an array of the
155/// specified element type.
156TypeRef 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 Lattner4eb445d2007-01-26 01:27:23 +0000163 ++NumSlowLookups;
Chris Lattner7ccecb92006-11-12 08:50:50 +0000164 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 Lattnerc6ad8132006-12-02 07:52:18 +0000182/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
183///
184TypeRef 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 Lattner4eb445d2007-01-26 01:27:23 +0000188 ++NumSlowLookups;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000189 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 '...'.
204TypeRef 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 Lattner4eb445d2007-01-26 01:27:23 +0000209 ++NumSlowLookups;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000210 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 Lattneref51c202006-11-10 07:17:23 +0000257
Chris Lattnerd0342e52006-11-20 04:02:15 +0000258/// getTypeDeclType - Return the unique reference to the type for the
259/// specified typename decl.
260TypeRef ASTContext::getTypeDeclType(TypeDecl *Decl) {
261 // FIXME: This is obviously braindead!
262 // Unique TypeDecl, to guarantee there is only one TypeDeclType.
Chris Lattner4eb445d2007-01-26 01:27:23 +0000263 ++NumSlowLookups;
Chris Lattnerd0342e52006-11-20 04:02:15 +0000264 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 Lattnerd0ee4022007-01-22 07:39:30 +0000271 Type *Canonical = Decl->getUnderlyingType().getTypePtr();
Chris Lattnerd0342e52006-11-20 04:02:15 +0000272 Types.push_back(new TypeNameType(Decl, Canonical));
273 return Types.back();
274}
275
Chris Lattnerfb072462007-01-23 05:45:31 +0000276/// getTagDeclType - Return the unique reference to the type for the
277/// specified TagDecl (struct/union/class/enum) decl.
278TypeRef ASTContext::getTagDeclType(TagDecl *Decl) {
279 // FIXME: This is obviously braindead!
280 // Unique TypeDecl, to guarantee there is only one TaggedType.
Chris Lattner4eb445d2007-01-26 01:27:23 +0000281 ++NumSlowLookups;
Chris Lattnerfb072462007-01-23 05:45:31 +0000282 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