blob: 7ce736c9c9d4c29f03d10ff7cee8240974acea30 [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"
Steve Naroff6fbf0dc2007-03-16 00:33:25 +000018
Chris Lattnerddc135e2006-11-10 06:34:16 +000019using namespace llvm;
20using namespace clang;
21
Chris Lattnerd5973eb2006-11-12 00:53:46 +000022ASTContext::~ASTContext() {
23 // Deallocate all the types.
24 while (!Types.empty()) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +000025 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
26 // Destroy the object, but don't call delete. These are malloc'd.
27 FT->~FunctionTypeProto();
28 free(FT);
29 } else {
30 delete Types.back();
31 }
Chris Lattnerd5973eb2006-11-12 00:53:46 +000032 Types.pop_back();
33 }
34}
35
Chris Lattner4eb445d2007-01-26 01:27:23 +000036void ASTContext::PrintStats() const {
37 fprintf(stderr, "*** AST Context Stats:\n");
38 fprintf(stderr, " %d types total.\n", (int)Types.size());
39 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
40 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
41
42 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
43
44 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
45 Type *T = Types[i];
46 if (isa<BuiltinType>(T))
47 ++NumBuiltin;
48 else if (isa<PointerType>(T))
49 ++NumPointer;
50 else if (isa<ArrayType>(T))
51 ++NumArray;
52 else if (isa<FunctionTypeNoProto>(T))
53 ++NumFunctionNP;
54 else if (isa<FunctionTypeProto>(T))
55 ++NumFunctionP;
Chris Lattner32d920b2007-01-26 02:01:53 +000056 else if (isa<TypedefType>(T))
Chris Lattner4eb445d2007-01-26 01:27:23 +000057 ++NumTypeName;
Steve Narofff1e53692007-03-23 22:27:02 +000058 else if (TagType *TT = dyn_cast<TagType>(T)) {
Chris Lattner4eb445d2007-01-26 01:27:23 +000059 ++NumTagged;
60 switch (TT->getDecl()->getKind()) {
61 default: assert(0 && "Unknown tagged type!");
62 case Decl::Struct: ++NumTagStruct; break;
63 case Decl::Union: ++NumTagUnion; break;
64 case Decl::Class: ++NumTagClass; break;
65 case Decl::Enum: ++NumTagEnum; break;
66 }
67 } else {
68 assert(0 && "Unknown type!");
69 }
70 }
71
72 fprintf(stderr, " %d builtin types\n", NumBuiltin);
73 fprintf(stderr, " %d pointer types\n", NumPointer);
74 fprintf(stderr, " %d array types\n", NumArray);
75 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
76 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
77 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
78 fprintf(stderr, " %d tagged types\n", NumTagged);
79 fprintf(stderr, " %d struct types\n", NumTagStruct);
80 fprintf(stderr, " %d union types\n", NumTagUnion);
81 fprintf(stderr, " %d class types\n", NumTagClass);
82 fprintf(stderr, " %d enum types\n", NumTagEnum);
Chris Lattner4eb445d2007-01-26 01:27:23 +000083}
84
85
Steve Naroffe5aa9be2007-04-05 22:36:20 +000086void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
87 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +000088}
89
90
Chris Lattner970e54e2006-11-12 00:37:36 +000091void ASTContext::InitBuiltinTypes() {
92 assert(VoidTy.isNull() && "Context reinitialized?");
93
94 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +000095 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +000096
97 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +000098 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +000099 // C99 6.2.5p3.
Chris Lattner726f97b2006-12-03 02:57:32 +0000100 InitBuiltinType(CharTy, BuiltinType::Char);
Chris Lattner970e54e2006-11-12 00:37:36 +0000101 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000102 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
103 InitBuiltinType(ShortTy, BuiltinType::Short);
104 InitBuiltinType(IntTy, BuiltinType::Int);
105 InitBuiltinType(LongTy, BuiltinType::Long);
106 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000107
108 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000109 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
110 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
111 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
112 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
113 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000114
115 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000116 InitBuiltinType(FloatTy, BuiltinType::Float);
117 InitBuiltinType(DoubleTy, BuiltinType::Double);
118 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000119
120 // C99 6.2.5p11.
Chris Lattner726f97b2006-12-03 02:57:32 +0000121 InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex);
122 InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex);
123 InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex);
Chris Lattner970e54e2006-11-12 00:37:36 +0000124}
125
126/// getPointerType - Return the uniqued reference to the type for a pointer to
127/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000128QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000129 // Unique pointers, to guarantee there is only one pointer of a particular
130 // structure.
Chris Lattner67521df2007-01-27 01:29:36 +0000131 FoldingSetNodeID ID;
132 PointerType::Profile(ID, T);
133
134 void *InsertPos = 0;
135 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000136 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000137
Chris Lattner7ccecb92006-11-12 08:50:50 +0000138 // If the pointee type isn't canonical, this won't be a canonical type either,
139 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000140 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000141 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000142 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000143
144 // Get the new insert position for the node we care about.
145 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
146 assert(NewIP == 0 && "Shouldn't be in the map!");
147 }
Chris Lattner67521df2007-01-27 01:29:36 +0000148 PointerType *New = new PointerType(T, Canonical);
149 Types.push_back(New);
150 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000151 return QualType(New, 0);
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.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000156QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
157 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner36f8e652007-01-27 08:31:04 +0000158 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000159 // structure.
Chris Lattner36f8e652007-01-27 08:31:04 +0000160 FoldingSetNodeID ID;
Steve Naroffb7d49242007-03-14 19:55:17 +0000161 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
Steve Naroff408451b2007-02-26 22:17:12 +0000162
Chris Lattner36f8e652007-01-27 08:31:04 +0000163 void *InsertPos = 0;
164 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000165 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000166
167 // If the element type isn't canonical, this won't be a canonical type either,
168 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000169 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000170 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000171 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000172 NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000173
174 // Get the new insert position for the node we care about.
175 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
176 assert(NewIP == 0 && "Shouldn't be in the map!");
177 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000178
Steve Naroffb7d49242007-03-14 19:55:17 +0000179 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000180 ArrayTypes.InsertNode(New, InsertPos);
181 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000182 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000183}
184
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000185/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
186///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000187QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000188 // Unique functions, to guarantee there is only one function of a particular
189 // structure.
Chris Lattner47955de2007-01-27 08:37:20 +0000190 FoldingSetNodeID ID;
191 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000192
Chris Lattner47955de2007-01-27 08:37:20 +0000193 void *InsertPos = 0;
194 if (FunctionTypeNoProto *FT =
195 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000196 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000197
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000198 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000199 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000200 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000201
202 // Get the new insert position for the node we care about.
203 FunctionTypeNoProto *NewIP =
204 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
205 assert(NewIP == 0 && "Shouldn't be in the map!");
206 }
207
208 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
209 Types.push_back(New);
210 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000211 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000212}
213
214/// getFunctionType - Return a normal function type with a typed argument
215/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000216QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
217 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000218 // Unique functions, to guarantee there is only one function of a particular
219 // structure.
Chris Lattnerfd4de792007-01-27 01:15:32 +0000220 FoldingSetNodeID ID;
221 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
222
223 void *InsertPos = 0;
224 if (FunctionTypeProto *FTP =
225 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000226 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000227
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000228 // Determine whether the type being created is already canonical or not.
229 bool isCanonical = ResultTy->isCanonical();
230 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
231 if (!ArgArray[i]->isCanonical())
232 isCanonical = false;
233
234 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000235 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000236 if (!isCanonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000237 SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000238 CanonicalArgs.reserve(NumArgs);
239 for (unsigned i = 0; i != NumArgs; ++i)
240 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
241
242 Canonical = getFunctionType(ResultTy.getCanonicalType(),
243 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000244 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000245
246 // Get the new insert position for the node we care about.
247 FunctionTypeProto *NewIP =
248 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
249 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000250 }
251
252 // FunctionTypeProto objects are not allocated with new because they have a
253 // variable size array (for parameter types) at the end of them.
254 FunctionTypeProto *FTP =
255 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000256 (NumArgs-1)*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000257 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
258 Canonical);
259
260 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000261 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000262 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000263}
Chris Lattneref51c202006-11-10 07:17:23 +0000264
Chris Lattner32d920b2007-01-26 02:01:53 +0000265/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000266/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000267QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
268 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000269
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000270 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000271 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
272 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000273 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000274}
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.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000278QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000279 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000280 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000281
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000282 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000283 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000284 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000285}
286
Steve Naroff92e30f82007-04-02 22:35:25 +0000287/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000288/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000289/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000290QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000291 // On Darwin, size_t is defined as a "long unsigned int".
292 // FIXME: should derive from "Target".
293 return UnsignedLongTy;
294}
Chris Lattnerfb072462007-01-23 05:45:31 +0000295