blob: 6b52a17a21303e20a523f3b6582cbbefa8112691 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000026#include "clang/Basic/Specifiers.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000027#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000028
Chris Lattner6d9a6852006-10-25 05:11:20 +000029using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000030
Chris Lattner88f70d62008-03-15 05:43:15 +000031//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000032// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000033//===----------------------------------------------------------------------===//
34
Douglas Gregor7dc5c172010-02-03 09:33:45 +000035/// \brief Get the most restrictive linkage for the types in the given
36/// template parameter list.
37static Linkage
38getLinkageForTemplateParameterList(const TemplateParameterList *Params) {
39 Linkage L = ExternalLinkage;
40 for (TemplateParameterList::const_iterator P = Params->begin(),
41 PEnd = Params->end();
42 P != PEnd; ++P) {
43 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
44 if (!NTTP->getType()->isDependentType()) {
45 L = minLinkage(L, NTTP->getType()->getLinkage());
46 continue;
47 }
48
49 if (TemplateTemplateParmDecl *TTP
50 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
51 L = minLinkage(L,
52 getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
53 }
54 }
55
56 return L;
57}
58
59/// \brief Get the most restrictive linkage for the types and
60/// declarations in the given template argument list.
61static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
62 unsigned NumArgs) {
63 Linkage L = ExternalLinkage;
64
65 for (unsigned I = 0; I != NumArgs; ++I) {
66 switch (Args[I].getKind()) {
67 case TemplateArgument::Null:
68 case TemplateArgument::Integral:
69 case TemplateArgument::Expression:
70 break;
71
72 case TemplateArgument::Type:
73 L = minLinkage(L, Args[I].getAsType()->getLinkage());
74 break;
75
76 case TemplateArgument::Declaration:
77 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
78 L = minLinkage(L, ND->getLinkage());
79 if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
80 L = minLinkage(L, VD->getType()->getLinkage());
81 break;
82
83 case TemplateArgument::Template:
84 if (TemplateDecl *Template
85 = Args[I].getAsTemplate().getAsTemplateDecl())
86 L = minLinkage(L, Template->getLinkage());
87 break;
88
89 case TemplateArgument::Pack:
90 L = minLinkage(L,
91 getLinkageForTemplateArgumentList(Args[I].pack_begin(),
92 Args[I].pack_size()));
93 break;
94 }
95 }
96
97 return L;
98}
99
100static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000101 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
102 "Not a name having namespace scope");
103 ASTContext &Context = D->getASTContext();
104
105 // C++ [basic.link]p3:
106 // A name having namespace scope (3.3.6) has internal linkage if it
107 // is the name of
108 // - an object, reference, function or function template that is
109 // explicitly declared static; or,
110 // (This bullet corresponds to C99 6.2.2p3.)
111 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
112 // Explicitly declared static.
113 if (Var->getStorageClass() == VarDecl::Static)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000114 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000115
116 // - an object or reference that is explicitly declared const
117 // and neither explicitly declared extern nor previously
118 // declared to have external linkage; or
119 // (there is no equivalent in C99)
120 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000121 Var->getType().isConstant(Context) &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000122 Var->getStorageClass() != VarDecl::Extern &&
123 Var->getStorageClass() != VarDecl::PrivateExtern) {
124 bool FoundExtern = false;
125 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
126 PrevVar && !FoundExtern;
127 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000128 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000129 FoundExtern = true;
130
131 if (!FoundExtern)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000132 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000133 }
134 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000135 // C++ [temp]p4:
136 // A non-member function template can have internal linkage; any
137 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000138 const FunctionDecl *Function = 0;
139 if (const FunctionTemplateDecl *FunTmpl
140 = dyn_cast<FunctionTemplateDecl>(D))
141 Function = FunTmpl->getTemplatedDecl();
142 else
143 Function = cast<FunctionDecl>(D);
144
145 // Explicitly declared static.
146 if (Function->getStorageClass() == FunctionDecl::Static)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000147 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000148 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
149 // - a data member of an anonymous union.
150 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000151 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000152 }
153
154 // C++ [basic.link]p4:
155
156 // A name having namespace scope has external linkage if it is the
157 // name of
158 //
159 // - an object or reference, unless it has internal linkage; or
160 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
161 if (!Context.getLangOptions().CPlusPlus &&
162 (Var->getStorageClass() == VarDecl::Extern ||
163 Var->getStorageClass() == VarDecl::PrivateExtern)) {
164 // C99 6.2.2p4:
165 // For an identifier declared with the storage-class specifier
166 // extern in a scope in which a prior declaration of that
167 // identifier is visible, if the prior declaration specifies
168 // internal or external linkage, the linkage of the identifier
169 // at the later declaration is the same as the linkage
170 // specified at the prior declaration. If no prior declaration
171 // is visible, or if the prior declaration specifies no
172 // linkage, then the identifier has external linkage.
173 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000174 if (Linkage L = PrevVar->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000175 return L;
176 }
177 }
178
179 // C99 6.2.2p5:
180 // If the declaration of an identifier for an object has file
181 // scope and no storage-class specifier, its linkage is
182 // external.
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000183 if (Var->isInAnonymousNamespace())
184 return UniqueExternalLinkage;
185
186 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000187 }
188
189 // - a function, unless it has internal linkage; or
190 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
191 // C99 6.2.2p5:
192 // If the declaration of an identifier for a function has no
193 // storage-class specifier, its linkage is determined exactly
194 // as if it were declared with the storage-class specifier
195 // extern.
196 if (!Context.getLangOptions().CPlusPlus &&
197 (Function->getStorageClass() == FunctionDecl::Extern ||
198 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
199 Function->getStorageClass() == FunctionDecl::None)) {
200 // C99 6.2.2p4:
201 // For an identifier declared with the storage-class specifier
202 // extern in a scope in which a prior declaration of that
203 // identifier is visible, if the prior declaration specifies
204 // internal or external linkage, the linkage of the identifier
205 // at the later declaration is the same as the linkage
206 // specified at the prior declaration. If no prior declaration
207 // is visible, or if the prior declaration specifies no
208 // linkage, then the identifier has external linkage.
209 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000210 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000211 return L;
212 }
213 }
214
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000215 if (Function->isInAnonymousNamespace())
216 return UniqueExternalLinkage;
217
218 if (FunctionTemplateSpecializationInfo *SpecInfo
219 = Function->getTemplateSpecializationInfo()) {
220 Linkage L = SpecInfo->getTemplate()->getLinkage();
221 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
222 L = minLinkage(L,
223 getLinkageForTemplateArgumentList(
224 TemplateArgs.getFlatArgumentList(),
225 TemplateArgs.flat_size()));
226 return L;
227 }
228
229 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 }
231
232 // - a named class (Clause 9), or an unnamed class defined in a
233 // typedef declaration in which the class has the typedef name
234 // for linkage purposes (7.1.3); or
235 // - a named enumeration (7.2), or an unnamed enumeration
236 // defined in a typedef declaration in which the enumeration
237 // has the typedef name for linkage purposes (7.1.3); or
238 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000239 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
240 if (Tag->isInAnonymousNamespace())
241 return UniqueExternalLinkage;
242
243 // If this is a class template specialization, consider the
244 // linkage of the template and template arguments.
245 if (const ClassTemplateSpecializationDecl *Spec
246 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
247 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
248 Linkage L = getLinkageForTemplateArgumentList(
249 TemplateArgs.getFlatArgumentList(),
250 TemplateArgs.flat_size());
251 return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
252 }
253
254 return ExternalLinkage;
255 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000256
257 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000258 if (isa<EnumConstantDecl>(D)) {
259 Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
260 if (isExternalLinkage(L))
261 return L;
262 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000263
264 // - a template, unless it is a function template that has
265 // internal linkage (Clause 14);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000266 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
267 if (D->isInAnonymousNamespace())
268 return UniqueExternalLinkage;
269
270 return getLinkageForTemplateParameterList(
271 Template->getTemplateParameters());
272 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000273
274 // - a namespace (7.3), unless it is declared within an unnamed
275 // namespace.
276 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000277 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000278
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000279 return NoLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000280}
281
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000282Linkage NamedDecl::getLinkage() const {
Ted Kremenek926d8602010-04-20 23:15:35 +0000283
284 // Objective-C: treat all Objective-C declarations as having external
285 // linkage.
286 switch (getKind()) {
287 default:
288 break;
289 case Decl::ObjCAtDefsField:
290 case Decl::ObjCCategory:
291 case Decl::ObjCCategoryImpl:
292 case Decl::ObjCClass:
293 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000294 case Decl::ObjCForwardProtocol:
295 case Decl::ObjCImplementation:
296 case Decl::ObjCInterface:
297 case Decl::ObjCIvar:
298 case Decl::ObjCMethod:
299 case Decl::ObjCProperty:
300 case Decl::ObjCPropertyImpl:
301 case Decl::ObjCProtocol:
302 return ExternalLinkage;
303 }
304
Douglas Gregorf73b2822009-11-25 22:24:25 +0000305 // Handle linkage for namespace-scope names.
306 if (getDeclContext()->getLookupContext()->isFileContext())
307 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
308 return L;
309
310 // C++ [basic.link]p5:
311 // In addition, a member function, static data member, a named
312 // class or enumeration of class scope, or an unnamed class or
313 // enumeration defined in a class-scope typedef declaration such
314 // that the class or enumeration has the typedef name for linkage
315 // purposes (7.1.3), has external linkage if the name of the class
316 // has external linkage.
317 if (getDeclContext()->isRecord() &&
318 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
319 (isa<TagDecl>(this) &&
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000320 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
321 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
322 if (isExternalLinkage(L))
323 return L;
324 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000325
326 // C++ [basic.link]p6:
327 // The name of a function declared in block scope and the name of
328 // an object declared by a block scope extern declaration have
329 // linkage. If there is a visible declaration of an entity with
330 // linkage having the same name and type, ignoring entities
331 // declared outside the innermost enclosing namespace scope, the
332 // block scope declaration declares that same entity and receives
333 // the linkage of the previous declaration. If there is more than
334 // one such matching entity, the program is ill-formed. Otherwise,
335 // if no matching entity is found, the block scope entity receives
336 // external linkage.
337 if (getLexicalDeclContext()->isFunctionOrMethod()) {
338 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
339 if (Function->getPreviousDeclaration())
340 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
341 return L;
342
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000343 if (Function->isInAnonymousNamespace())
344 return UniqueExternalLinkage;
345
Douglas Gregorf73b2822009-11-25 22:24:25 +0000346 return ExternalLinkage;
347 }
348
349 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
350 if (Var->getStorageClass() == VarDecl::Extern ||
351 Var->getStorageClass() == VarDecl::PrivateExtern) {
352 if (Var->getPreviousDeclaration())
353 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
354 return L;
355
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000356 if (Var->isInAnonymousNamespace())
357 return UniqueExternalLinkage;
358
Douglas Gregorf73b2822009-11-25 22:24:25 +0000359 return ExternalLinkage;
360 }
361 }
362
363 // C++ [basic.link]p6:
364 // Names not covered by these rules have no linkage.
365 return NoLinkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000366 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000367
Douglas Gregor2ada0482009-02-04 17:27:36 +0000368std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000369 return getQualifiedNameAsString(getASTContext().getLangOptions());
370}
371
372std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000373 const DeclContext *Ctx = getDeclContext();
374
375 if (Ctx->isFunctionOrMethod())
376 return getNameAsString();
377
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000378 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
379 ContextsTy Contexts;
380
381 // Collect contexts.
382 while (Ctx && isa<NamedDecl>(Ctx)) {
383 Contexts.push_back(Ctx);
384 Ctx = Ctx->getParent();
385 };
386
387 std::string QualName;
388 llvm::raw_string_ostream OS(QualName);
389
390 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
391 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000392 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000393 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000394 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
395 std::string TemplateArgsStr
396 = TemplateSpecializationType::PrintTemplateArgumentList(
397 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000398 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000399 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000400 OS << Spec->getName() << TemplateArgsStr;
401 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000402 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000403 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000404 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000405 OS << ND;
406 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
407 if (!RD->getIdentifier())
408 OS << "<anonymous " << RD->getKindName() << '>';
409 else
410 OS << RD;
411 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000412 const FunctionProtoType *FT = 0;
413 if (FD->hasWrittenPrototype())
414 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
415
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000416 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000417 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000418 unsigned NumParams = FD->getNumParams();
419 for (unsigned i = 0; i < NumParams; ++i) {
420 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000421 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000422 std::string Param;
423 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000424 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000425 }
426
427 if (FT->isVariadic()) {
428 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000429 OS << ", ";
430 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000431 }
432 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000433 OS << ')';
434 } else {
435 OS << cast<NamedDecl>(*I);
436 }
437 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000438 }
439
John McCalla2a3f7d2010-03-16 21:48:18 +0000440 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000441 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000442 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000443 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000444
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000445 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000446}
447
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000448bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000449 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
450
Douglas Gregor889ceb72009-02-03 19:21:40 +0000451 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
452 // We want to keep it, unless it nominates same namespace.
453 if (getKind() == Decl::UsingDirective) {
454 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
455 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
459 // For function declarations, we keep track of redeclarations.
460 return FD->getPreviousDeclaration() == OldD;
461
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000462 // For function templates, the underlying function declarations are linked.
463 if (const FunctionTemplateDecl *FunctionTemplate
464 = dyn_cast<FunctionTemplateDecl>(this))
465 if (const FunctionTemplateDecl *OldFunctionTemplate
466 = dyn_cast<FunctionTemplateDecl>(OldD))
467 return FunctionTemplate->getTemplatedDecl()
468 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000469
Steve Naroffc4173fa2009-02-22 19:35:57 +0000470 // For method declarations, we keep track of redeclarations.
471 if (isa<ObjCMethodDecl>(this))
472 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000473
John McCall9f3059a2009-10-09 21:13:30 +0000474 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
475 return true;
476
John McCall3f746822009-11-17 05:59:44 +0000477 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
478 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
479 cast<UsingShadowDecl>(OldD)->getTargetDecl();
480
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000481 // For non-function declarations, if the declarations are of the
482 // same kind then this must be a redeclaration, or semantic analysis
483 // would not have given us the new declaration.
484 return this->getKind() == OldD->getKind();
485}
486
Douglas Gregoreddf4332009-02-24 20:03:32 +0000487bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000488 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000489}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000490
Anders Carlsson6915bf62009-06-26 06:29:23 +0000491NamedDecl *NamedDecl::getUnderlyingDecl() {
492 NamedDecl *ND = this;
493 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000494 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000495 ND = UD->getTargetDecl();
496 else if (ObjCCompatibleAliasDecl *AD
497 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
498 return AD->getClassInterface();
499 else
500 return ND;
501 }
502}
503
John McCalla8ae2222010-04-06 21:38:20 +0000504bool NamedDecl::isCXXInstanceMember() const {
505 assert(isCXXClassMember() &&
506 "checking whether non-member is instance member");
507
508 const NamedDecl *D = this;
509 if (isa<UsingShadowDecl>(D))
510 D = cast<UsingShadowDecl>(D)->getTargetDecl();
511
512 if (isa<FieldDecl>(D))
513 return true;
514 if (isa<CXXMethodDecl>(D))
515 return cast<CXXMethodDecl>(D)->isInstance();
516 if (isa<FunctionTemplateDecl>(D))
517 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
518 ->getTemplatedDecl())->isInstance();
519 return false;
520}
521
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000522//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000523// DeclaratorDecl Implementation
524//===----------------------------------------------------------------------===//
525
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000526template <typename DeclT>
527static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
528 if (decl->getNumTemplateParameterLists() > 0)
529 return decl->getTemplateParameterList(0)->getTemplateLoc();
530 else
531 return decl->getInnerLocStart();
532}
533
John McCall3e11ebe2010-03-15 10:12:16 +0000534DeclaratorDecl::~DeclaratorDecl() {}
535void DeclaratorDecl::Destroy(ASTContext &C) {
536 if (hasExtInfo())
537 C.Deallocate(getExtInfo());
538 ValueDecl::Destroy(C);
539}
540
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000541SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000542 TypeSourceInfo *TSI = getTypeSourceInfo();
543 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000544 return SourceLocation();
545}
546
John McCall3e11ebe2010-03-15 10:12:16 +0000547void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
548 SourceRange QualifierRange) {
549 if (Qualifier) {
550 // Make sure the extended decl info is allocated.
551 if (!hasExtInfo()) {
552 // Save (non-extended) type source info pointer.
553 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
554 // Allocate external info struct.
555 DeclInfo = new (getASTContext()) ExtInfo;
556 // Restore savedTInfo into (extended) decl info.
557 getExtInfo()->TInfo = savedTInfo;
558 }
559 // Set qualifier info.
560 getExtInfo()->NNS = Qualifier;
561 getExtInfo()->NNSRange = QualifierRange;
562 }
563 else {
564 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
565 assert(QualifierRange.isInvalid());
566 if (hasExtInfo()) {
567 // Save type source info pointer.
568 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
569 // Deallocate the extended decl info.
570 getASTContext().Deallocate(getExtInfo());
571 // Restore savedTInfo into (non-extended) decl info.
572 DeclInfo = savedTInfo;
573 }
574 }
575}
576
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000577SourceLocation DeclaratorDecl::getOuterLocStart() const {
578 return getTemplateOrInnerLocStart(this);
579}
580
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000581void
Douglas Gregor20527e22010-06-15 17:44:38 +0000582QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
583 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000584 TemplateParameterList **TPLists) {
585 assert((NumTPLists == 0 || TPLists != 0) &&
586 "Empty array of template parameters with positive size!");
587 assert((NumTPLists == 0 || NNS) &&
588 "Nonempty array of template parameters with no qualifier!");
589
590 // Free previous template parameters (if any).
591 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000592 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000593 TemplParamLists = 0;
594 NumTemplParamLists = 0;
595 }
596 // Set info on matched template parameter lists (if any).
597 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000598 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000599 NumTemplParamLists = NumTPLists;
600 for (unsigned i = NumTPLists; i-- > 0; )
601 TemplParamLists[i] = TPLists[i];
602 }
603}
604
Douglas Gregor20527e22010-06-15 17:44:38 +0000605void QualifierInfo::Destroy(ASTContext &Context) {
606 // FIXME: Deallocate template parameter lists themselves!
607 if (TemplParamLists)
608 Context.Deallocate(TemplParamLists);
609}
610
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000611//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000612// VarDecl Implementation
613//===----------------------------------------------------------------------===//
614
Sebastian Redl833ef452010-01-26 22:01:41 +0000615const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
616 switch (SC) {
617 case VarDecl::None: break;
618 case VarDecl::Auto: return "auto"; break;
619 case VarDecl::Extern: return "extern"; break;
620 case VarDecl::PrivateExtern: return "__private_extern__"; break;
621 case VarDecl::Register: return "register"; break;
622 case VarDecl::Static: return "static"; break;
623 }
624
625 assert(0 && "Invalid storage class");
626 return 0;
627}
628
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000629VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000630 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000631 StorageClass S, StorageClass SCAsWritten) {
632 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000633}
634
635void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000636 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000637 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000638 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000639 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
640 Eval->~EvaluatedStmt();
641 C.Deallocate(Eval);
642 }
643 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000644 this->~VarDecl();
John McCall3e11ebe2010-03-15 10:12:16 +0000645 DeclaratorDecl::Destroy(C);
Nuno Lopes394ec982008-12-17 23:39:55 +0000646}
647
648VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000649}
650
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000651SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000652 SourceLocation Start = getTypeSpecStartLoc();
653 if (Start.isInvalid())
654 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000655 return Start;
656}
657
658SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000659 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000660 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
661 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000662}
663
Sebastian Redl833ef452010-01-26 22:01:41 +0000664bool VarDecl::isExternC() const {
665 ASTContext &Context = getASTContext();
666 if (!Context.getLangOptions().CPlusPlus)
667 return (getDeclContext()->isTranslationUnit() &&
668 getStorageClass() != Static) ||
669 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
670
671 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
672 DC = DC->getParent()) {
673 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
674 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
675 return getStorageClass() != Static;
676
677 break;
678 }
679
680 if (DC->isFunctionOrMethod())
681 return false;
682 }
683
684 return false;
685}
686
687VarDecl *VarDecl::getCanonicalDecl() {
688 return getFirstDeclaration();
689}
690
Sebastian Redl35351a92010-01-31 22:27:38 +0000691VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
692 // C++ [basic.def]p2:
693 // A declaration is a definition unless [...] it contains the 'extern'
694 // specifier or a linkage-specification and neither an initializer [...],
695 // it declares a static data member in a class declaration [...].
696 // C++ [temp.expl.spec]p15:
697 // An explicit specialization of a static data member of a template is a
698 // definition if the declaration includes an initializer; otherwise, it is
699 // a declaration.
700 if (isStaticDataMember()) {
701 if (isOutOfLine() && (hasInit() ||
702 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
703 return Definition;
704 else
705 return DeclarationOnly;
706 }
707 // C99 6.7p5:
708 // A definition of an identifier is a declaration for that identifier that
709 // [...] causes storage to be reserved for that object.
710 // Note: that applies for all non-file-scope objects.
711 // C99 6.9.2p1:
712 // If the declaration of an identifier for an object has file scope and an
713 // initializer, the declaration is an external definition for the identifier
714 if (hasInit())
715 return Definition;
716 // AST for 'extern "C" int foo;' is annotated with 'extern'.
717 if (hasExternalStorage())
718 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000719
720 if (getStorageClassAsWritten() == Extern ||
721 getStorageClassAsWritten() == PrivateExtern) {
722 for (const VarDecl *PrevVar = getPreviousDeclaration();
723 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
724 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
725 return DeclarationOnly;
726 }
727 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000728 // C99 6.9.2p2:
729 // A declaration of an object that has file scope without an initializer,
730 // and without a storage class specifier or the scs 'static', constitutes
731 // a tentative definition.
732 // No such thing in C++.
733 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
734 return TentativeDefinition;
735
736 // What's left is (in C, block-scope) declarations without initializers or
737 // external storage. These are definitions.
738 return Definition;
739}
740
Sebastian Redl35351a92010-01-31 22:27:38 +0000741VarDecl *VarDecl::getActingDefinition() {
742 DefinitionKind Kind = isThisDeclarationADefinition();
743 if (Kind != TentativeDefinition)
744 return 0;
745
Chris Lattner48eb14d2010-06-14 18:31:46 +0000746 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000747 VarDecl *First = getFirstDeclaration();
748 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
749 I != E; ++I) {
750 Kind = (*I)->isThisDeclarationADefinition();
751 if (Kind == Definition)
752 return 0;
753 else if (Kind == TentativeDefinition)
754 LastTentative = *I;
755 }
756 return LastTentative;
757}
758
759bool VarDecl::isTentativeDefinitionNow() const {
760 DefinitionKind Kind = isThisDeclarationADefinition();
761 if (Kind != TentativeDefinition)
762 return false;
763
764 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
765 if ((*I)->isThisDeclarationADefinition() == Definition)
766 return false;
767 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000768 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000769}
770
Sebastian Redl5ca79842010-02-01 20:16:42 +0000771VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000772 VarDecl *First = getFirstDeclaration();
773 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
774 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000775 if ((*I)->isThisDeclarationADefinition() == Definition)
776 return *I;
777 }
778 return 0;
779}
780
781const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000782 redecl_iterator I = redecls_begin(), E = redecls_end();
783 while (I != E && !I->getInit())
784 ++I;
785
786 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000787 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000788 return I->getInit();
789 }
790 return 0;
791}
792
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000793bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000794 if (Decl::isOutOfLine())
795 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000796
797 if (!isStaticDataMember())
798 return false;
799
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000800 // If this static data member was instantiated from a static data member of
801 // a class template, check whether that static data member was defined
802 // out-of-line.
803 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
804 return VD->isOutOfLine();
805
806 return false;
807}
808
Douglas Gregor1d957a32009-10-27 18:42:08 +0000809VarDecl *VarDecl::getOutOfLineDefinition() {
810 if (!isStaticDataMember())
811 return 0;
812
813 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
814 RD != RDEnd; ++RD) {
815 if (RD->getLexicalDeclContext()->isFileContext())
816 return *RD;
817 }
818
819 return 0;
820}
821
Douglas Gregord5058122010-02-11 01:19:42 +0000822void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000823 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
824 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000825 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000826 }
827
828 Init = I;
829}
830
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000831VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000832 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000833 return cast<VarDecl>(MSI->getInstantiatedFrom());
834
835 return 0;
836}
837
Douglas Gregor3c74d412009-10-14 20:14:33 +0000838TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000839 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000840 return MSI->getTemplateSpecializationKind();
841
842 return TSK_Undeclared;
843}
844
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000845MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000846 return getASTContext().getInstantiatedFromStaticDataMember(this);
847}
848
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000849void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
850 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000851 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000852 assert(MSI && "Not an instantiated static data member?");
853 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000854 if (TSK != TSK_ExplicitSpecialization &&
855 PointOfInstantiation.isValid() &&
856 MSI->getPointOfInstantiation().isInvalid())
857 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000858}
859
Sebastian Redl833ef452010-01-26 22:01:41 +0000860//===----------------------------------------------------------------------===//
861// ParmVarDecl Implementation
862//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000863
Sebastian Redl833ef452010-01-26 22:01:41 +0000864ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
865 SourceLocation L, IdentifierInfo *Id,
866 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000867 StorageClass S, StorageClass SCAsWritten,
868 Expr *DefArg) {
869 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
870 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000871}
872
Sebastian Redl833ef452010-01-26 22:01:41 +0000873Expr *ParmVarDecl::getDefaultArg() {
874 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
875 assert(!hasUninstantiatedDefaultArg() &&
876 "Default argument is not yet instantiated!");
877
878 Expr *Arg = getInit();
879 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
880 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000881
Sebastian Redl833ef452010-01-26 22:01:41 +0000882 return Arg;
883}
884
885unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
886 if (const CXXExprWithTemporaries *E =
887 dyn_cast<CXXExprWithTemporaries>(getInit()))
888 return E->getNumTemporaries();
889
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000890 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000891}
892
Sebastian Redl833ef452010-01-26 22:01:41 +0000893CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
894 assert(getNumDefaultArgTemporaries() &&
895 "Default arguments does not have any temporaries!");
896
897 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
898 return E->getTemporary(i);
899}
900
901SourceRange ParmVarDecl::getDefaultArgRange() const {
902 if (const Expr *E = getInit())
903 return E->getSourceRange();
904
905 if (hasUninstantiatedDefaultArg())
906 return getUninstantiatedDefaultArg()->getSourceRange();
907
908 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000909}
910
Nuno Lopes394ec982008-12-17 23:39:55 +0000911//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000912// FunctionDecl Implementation
913//===----------------------------------------------------------------------===//
914
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000915void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000916 if (Body && Body.isOffset())
917 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000918
919 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
920 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000921
Douglas Gregord801b062009-10-07 23:56:10 +0000922 FunctionTemplateSpecializationInfo *FTSInfo
923 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
924 if (FTSInfo)
925 C.Deallocate(FTSInfo);
926
927 MemberSpecializationInfo *MSInfo
928 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
929 if (MSInfo)
930 C.Deallocate(MSInfo);
931
Steve Naroff13ae6f42009-01-27 21:25:57 +0000932 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000933
John McCall3e11ebe2010-03-15 10:12:16 +0000934 DeclaratorDecl::Destroy(C);
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000935}
936
John McCalle1f2ec22009-09-11 06:45:03 +0000937void FunctionDecl::getNameForDiagnostic(std::string &S,
938 const PrintingPolicy &Policy,
939 bool Qualified) const {
940 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
941 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
942 if (TemplateArgs)
943 S += TemplateSpecializationType::PrintTemplateArgumentList(
944 TemplateArgs->getFlatArgumentList(),
945 TemplateArgs->flat_size(),
946 Policy);
947
948}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000949
Ted Kremenek186a0742010-04-29 16:49:01 +0000950bool FunctionDecl::isVariadic() const {
951 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
952 return FT->isVariadic();
953 return false;
954}
955
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000956Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000957 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
958 if (I->Body) {
959 Definition = *I;
960 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000961 }
962 }
963
964 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000965}
966
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000967void FunctionDecl::setBody(Stmt *B) {
968 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000969 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000970 EndRangeLoc = B->getLocEnd();
971}
972
Douglas Gregor16618f22009-09-12 00:17:51 +0000973bool FunctionDecl::isMain() const {
974 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000975 return !Context.getLangOptions().Freestanding &&
976 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000977 getIdentifier() && getIdentifier()->isStr("main");
978}
979
Douglas Gregor16618f22009-09-12 00:17:51 +0000980bool FunctionDecl::isExternC() const {
981 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000982 // In C, any non-static, non-overloadable function has external
983 // linkage.
984 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000985 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000986
Mike Stump11289f42009-09-09 15:08:12 +0000987 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000988 DC = DC->getParent()) {
989 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
990 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000991 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000992 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000993
994 break;
995 }
996 }
997
998 return false;
999}
1000
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001001bool FunctionDecl::isGlobal() const {
1002 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1003 return Method->isStatic();
1004
1005 if (getStorageClass() == Static)
1006 return false;
1007
Mike Stump11289f42009-09-09 15:08:12 +00001008 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001009 DC->isNamespace();
1010 DC = DC->getParent()) {
1011 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1012 if (!Namespace->getDeclName())
1013 return false;
1014 break;
1015 }
1016 }
1017
1018 return true;
1019}
1020
Sebastian Redl833ef452010-01-26 22:01:41 +00001021void
1022FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1023 redeclarable_base::setPreviousDeclaration(PrevDecl);
1024
1025 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1026 FunctionTemplateDecl *PrevFunTmpl
1027 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1028 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1029 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1030 }
1031}
1032
1033const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1034 return getFirstDeclaration();
1035}
1036
1037FunctionDecl *FunctionDecl::getCanonicalDecl() {
1038 return getFirstDeclaration();
1039}
1040
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001041/// \brief Returns a value indicating whether this function
1042/// corresponds to a builtin function.
1043///
1044/// The function corresponds to a built-in function if it is
1045/// declared at translation scope or within an extern "C" block and
1046/// its name matches with the name of a builtin. The returned value
1047/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001048/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001049/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001050unsigned FunctionDecl::getBuiltinID() const {
1051 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001052 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1053 return 0;
1054
1055 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1056 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1057 return BuiltinID;
1058
1059 // This function has the name of a known C library
1060 // function. Determine whether it actually refers to the C library
1061 // function or whether it just has the same name.
1062
Douglas Gregora908e7f2009-02-17 03:23:10 +00001063 // If this is a static function, it's not a builtin.
1064 if (getStorageClass() == Static)
1065 return 0;
1066
Douglas Gregore711f702009-02-14 18:57:46 +00001067 // If this function is at translation-unit scope and we're not in
1068 // C++, it refers to the C library function.
1069 if (!Context.getLangOptions().CPlusPlus &&
1070 getDeclContext()->isTranslationUnit())
1071 return BuiltinID;
1072
1073 // If the function is in an extern "C" linkage specification and is
1074 // not marked "overloadable", it's the real function.
1075 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001076 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001077 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001078 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001079 return BuiltinID;
1080
1081 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001082 return 0;
1083}
1084
1085
Chris Lattner47c0d002009-04-25 06:03:53 +00001086/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001087/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001088/// after it has been created.
1089unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001090 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001091 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001092 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001093 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001095}
1096
Douglas Gregord5058122010-02-11 01:19:42 +00001097void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001098 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001099 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001101 // Zero params -> null pointer.
1102 if (NumParams) {
Douglas Gregord5058122010-02-11 01:19:42 +00001103 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001104 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001105 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001106
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001107 // Update source range. The check below allows us to set EndRangeLoc before
1108 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001109 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001110 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001111 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001112}
Chris Lattner41943152007-01-25 04:52:46 +00001113
Chris Lattner58258242008-04-10 02:22:51 +00001114/// getMinRequiredArguments - Returns the minimum number of arguments
1115/// needed to call this function. This may be fewer than the number of
1116/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001117/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001118unsigned FunctionDecl::getMinRequiredArguments() const {
1119 unsigned NumRequiredArgs = getNumParams();
1120 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001121 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001122 --NumRequiredArgs;
1123
1124 return NumRequiredArgs;
1125}
1126
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001127bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001128 // FIXME: This is not enough. Consider:
1129 //
1130 // inline void f();
1131 // void f() { }
1132 //
1133 // f is inlined, but does not have inline specified.
1134 // To fix this we should add an 'inline' flag to FunctionDecl.
1135 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001136 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001137
1138 if (isa<CXXMethodDecl>(this)) {
1139 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1140 return true;
1141 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001142
1143 switch (getTemplateSpecializationKind()) {
1144 case TSK_Undeclared:
1145 case TSK_ExplicitSpecialization:
1146 return false;
1147
1148 case TSK_ImplicitInstantiation:
1149 case TSK_ExplicitInstantiationDeclaration:
1150 case TSK_ExplicitInstantiationDefinition:
1151 // Handle below.
1152 break;
1153 }
1154
1155 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1156 Stmt *Pattern = 0;
1157 if (PatternDecl)
1158 Pattern = PatternDecl->getBody(PatternDecl);
1159
1160 if (Pattern && PatternDecl)
1161 return PatternDecl->isInlined();
1162
1163 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001164}
1165
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001166/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001167/// definition will be externally visible.
1168///
1169/// Inline function definitions are always available for inlining optimizations.
1170/// However, depending on the language dialect, declaration specifiers, and
1171/// attributes, the definition of an inline function may or may not be
1172/// "externally" visible to other translation units in the program.
1173///
1174/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001175/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001176/// inline definition becomes externally visible (C99 6.7.4p6).
1177///
1178/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1179/// definition, we use the GNU semantics for inline, which are nearly the
1180/// opposite of C99 semantics. In particular, "inline" by itself will create
1181/// an externally visible symbol, but "extern inline" will not create an
1182/// externally visible symbol.
1183bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1184 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001185 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001186 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001187
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001188 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001189 // GNU inline semantics. Based on a number of examples, we came up with the
1190 // following heuristic: if the "inline" keyword is present on a
1191 // declaration of the function but "extern" is not present on that
1192 // declaration, then the symbol is externally visible. Otherwise, the GNU
1193 // "extern inline" semantics applies and the symbol is not externally
1194 // visible.
1195 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1196 Redecl != RedeclEnd;
1197 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +00001198 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001199 return true;
1200 }
1201
1202 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001203 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001204 }
1205
1206 // C99 6.7.4p6:
1207 // [...] If all of the file scope declarations for a function in a
1208 // translation unit include the inline function specifier without extern,
1209 // then the definition in that translation unit is an inline definition.
1210 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1211 Redecl != RedeclEnd;
1212 ++Redecl) {
1213 // Only consider file-scope declarations in this test.
1214 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1215 continue;
1216
Douglas Gregor35b57532009-10-27 21:01:01 +00001217 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001218 return true; // Not an inline definition
1219 }
1220
1221 // C99 6.7.4p6:
1222 // An inline definition does not provide an external definition for the
1223 // function, and does not forbid an external definition in another
1224 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001225 return false;
1226}
1227
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001228/// getOverloadedOperator - Which C++ overloaded operator this
1229/// function represents, if any.
1230OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001231 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1232 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001233 else
1234 return OO_None;
1235}
1236
Alexis Huntc88db062010-01-13 09:01:02 +00001237/// getLiteralIdentifier - The literal suffix identifier this function
1238/// represents, if any.
1239const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1240 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1241 return getDeclName().getCXXLiteralIdentifier();
1242 else
1243 return 0;
1244}
1245
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001246FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1247 if (TemplateOrSpecialization.isNull())
1248 return TK_NonTemplate;
1249 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1250 return TK_FunctionTemplate;
1251 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1252 return TK_MemberSpecialization;
1253 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1254 return TK_FunctionTemplateSpecialization;
1255 if (TemplateOrSpecialization.is
1256 <DependentFunctionTemplateSpecializationInfo*>())
1257 return TK_DependentFunctionTemplateSpecialization;
1258
1259 assert(false && "Did we miss a TemplateOrSpecialization type?");
1260 return TK_NonTemplate;
1261}
1262
Douglas Gregord801b062009-10-07 23:56:10 +00001263FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001264 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001265 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1266
1267 return 0;
1268}
1269
Douglas Gregor06db9f52009-10-12 20:18:28 +00001270MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1271 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1272}
1273
Douglas Gregord801b062009-10-07 23:56:10 +00001274void
1275FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1276 TemplateSpecializationKind TSK) {
1277 assert(TemplateOrSpecialization.isNull() &&
1278 "Member function is already a specialization");
1279 MemberSpecializationInfo *Info
1280 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1281 TemplateOrSpecialization = Info;
1282}
1283
Douglas Gregorafca3b42009-10-27 20:53:28 +00001284bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001285 // If the function is invalid, it can't be implicitly instantiated.
1286 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001287 return false;
1288
1289 switch (getTemplateSpecializationKind()) {
1290 case TSK_Undeclared:
1291 case TSK_ExplicitSpecialization:
1292 case TSK_ExplicitInstantiationDefinition:
1293 return false;
1294
1295 case TSK_ImplicitInstantiation:
1296 return true;
1297
1298 case TSK_ExplicitInstantiationDeclaration:
1299 // Handled below.
1300 break;
1301 }
1302
1303 // Find the actual template from which we will instantiate.
1304 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1305 Stmt *Pattern = 0;
1306 if (PatternDecl)
1307 Pattern = PatternDecl->getBody(PatternDecl);
1308
1309 // C++0x [temp.explicit]p9:
1310 // Except for inline functions, other explicit instantiation declarations
1311 // have the effect of suppressing the implicit instantiation of the entity
1312 // to which they refer.
1313 if (!Pattern || !PatternDecl)
1314 return true;
1315
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001316 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001317}
1318
1319FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1320 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1321 while (Primary->getInstantiatedFromMemberTemplate()) {
1322 // If we have hit a point where the user provided a specialization of
1323 // this template, we're done looking.
1324 if (Primary->isMemberSpecialization())
1325 break;
1326
1327 Primary = Primary->getInstantiatedFromMemberTemplate();
1328 }
1329
1330 return Primary->getTemplatedDecl();
1331 }
1332
1333 return getInstantiatedFromMemberFunction();
1334}
1335
Douglas Gregor70d83e22009-06-29 17:30:29 +00001336FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001337 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001338 = TemplateOrSpecialization
1339 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001340 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001341 }
1342 return 0;
1343}
1344
1345const TemplateArgumentList *
1346FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001347 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001348 = TemplateOrSpecialization
1349 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001350 return Info->TemplateArguments;
1351 }
1352 return 0;
1353}
1354
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001355const TemplateArgumentListInfo *
1356FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1357 if (FunctionTemplateSpecializationInfo *Info
1358 = TemplateOrSpecialization
1359 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1360 return Info->TemplateArgumentsAsWritten;
1361 }
1362 return 0;
1363}
1364
Mike Stump11289f42009-09-09 15:08:12 +00001365void
Douglas Gregord5058122010-02-11 01:19:42 +00001366FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001367 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001368 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001369 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001370 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1371 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001372 assert(TSK != TSK_Undeclared &&
1373 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001374 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001375 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001376 if (!Info)
Douglas Gregord5058122010-02-11 01:19:42 +00001377 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001379 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001380 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001381 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001382 Info->TemplateArguments = TemplateArgs;
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001383 Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten;
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001384 Info->PointOfInstantiation = PointOfInstantiation;
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001385 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001386
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001387 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001388 // function template specializations.
1389 if (InsertPos)
1390 Template->getSpecializations().InsertNode(Info, InsertPos);
1391 else {
1392 // Try to insert the new node. If there is an existing node, remove it
1393 // first.
1394 FunctionTemplateSpecializationInfo *Existing
1395 = Template->getSpecializations().GetOrInsertNode(Info);
1396 if (Existing) {
1397 Template->getSpecializations().RemoveNode(Existing);
1398 Template->getSpecializations().GetOrInsertNode(Info);
1399 }
1400 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001401}
1402
John McCallb9c78482010-04-08 09:05:18 +00001403void
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001404FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1405 unsigned NumTemplateArgs,
1406 const TemplateArgument *TemplateArgs,
1407 TemplateSpecializationKind TSK,
1408 unsigned NumTemplateArgsAsWritten,
1409 TemplateArgumentLoc *TemplateArgsAsWritten,
1410 SourceLocation LAngleLoc,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001411 SourceLocation RAngleLoc,
1412 SourceLocation PointOfInstantiation) {
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001413 ASTContext &Ctx = getASTContext();
1414 TemplateArgumentList *TemplArgs
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +00001415 = new (Ctx) TemplateArgumentList(Ctx, TemplateArgs, NumTemplateArgs);
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001416 TemplateArgumentListInfo *TemplArgsInfo
1417 = new (Ctx) TemplateArgumentListInfo(LAngleLoc, RAngleLoc);
1418 for (unsigned i=0; i != NumTemplateArgsAsWritten; ++i)
1419 TemplArgsInfo->addArgument(TemplateArgsAsWritten[i]);
1420
1421 setFunctionTemplateSpecialization(Template, TemplArgs, /*InsertPos=*/0, TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001422 TemplArgsInfo, PointOfInstantiation);
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001423}
1424
1425void
John McCallb9c78482010-04-08 09:05:18 +00001426FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1427 const UnresolvedSetImpl &Templates,
1428 const TemplateArgumentListInfo &TemplateArgs) {
1429 assert(TemplateOrSpecialization.isNull());
1430 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1431 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001432 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001433 void *Buffer = Context.Allocate(Size);
1434 DependentFunctionTemplateSpecializationInfo *Info =
1435 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1436 TemplateArgs);
1437 TemplateOrSpecialization = Info;
1438}
1439
1440DependentFunctionTemplateSpecializationInfo::
1441DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1442 const TemplateArgumentListInfo &TArgs)
1443 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1444
1445 d.NumTemplates = Ts.size();
1446 d.NumArgs = TArgs.size();
1447
1448 FunctionTemplateDecl **TsArray =
1449 const_cast<FunctionTemplateDecl**>(getTemplates());
1450 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1451 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1452
1453 TemplateArgumentLoc *ArgsArray =
1454 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1455 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1456 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1457}
1458
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001459TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001460 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001461 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001462 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001463 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001464 if (FTSInfo)
1465 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001466
Douglas Gregord801b062009-10-07 23:56:10 +00001467 MemberSpecializationInfo *MSInfo
1468 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1469 if (MSInfo)
1470 return MSInfo->getTemplateSpecializationKind();
1471
1472 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001473}
1474
Mike Stump11289f42009-09-09 15:08:12 +00001475void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001476FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1477 SourceLocation PointOfInstantiation) {
1478 if (FunctionTemplateSpecializationInfo *FTSInfo
1479 = TemplateOrSpecialization.dyn_cast<
1480 FunctionTemplateSpecializationInfo*>()) {
1481 FTSInfo->setTemplateSpecializationKind(TSK);
1482 if (TSK != TSK_ExplicitSpecialization &&
1483 PointOfInstantiation.isValid() &&
1484 FTSInfo->getPointOfInstantiation().isInvalid())
1485 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1486 } else if (MemberSpecializationInfo *MSInfo
1487 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1488 MSInfo->setTemplateSpecializationKind(TSK);
1489 if (TSK != TSK_ExplicitSpecialization &&
1490 PointOfInstantiation.isValid() &&
1491 MSInfo->getPointOfInstantiation().isInvalid())
1492 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1493 } else
1494 assert(false && "Function cannot have a template specialization kind");
1495}
1496
1497SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001498 if (FunctionTemplateSpecializationInfo *FTSInfo
1499 = TemplateOrSpecialization.dyn_cast<
1500 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001501 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001502 else if (MemberSpecializationInfo *MSInfo
1503 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001504 return MSInfo->getPointOfInstantiation();
1505
1506 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001507}
1508
Douglas Gregor6411b922009-09-11 20:15:17 +00001509bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001510 if (Decl::isOutOfLine())
1511 return true;
1512
1513 // If this function was instantiated from a member function of a
1514 // class template, check whether that member function was defined out-of-line.
1515 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1516 const FunctionDecl *Definition;
1517 if (FD->getBody(Definition))
1518 return Definition->isOutOfLine();
1519 }
1520
1521 // If this function was instantiated from a function template,
1522 // check whether that function template was defined out-of-line.
1523 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1524 const FunctionDecl *Definition;
1525 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1526 return Definition->isOutOfLine();
1527 }
1528
1529 return false;
1530}
1531
Chris Lattner59a25942008-03-31 00:36:02 +00001532//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001533// FieldDecl Implementation
1534//===----------------------------------------------------------------------===//
1535
1536FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1537 IdentifierInfo *Id, QualType T,
1538 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1539 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1540}
1541
1542bool FieldDecl::isAnonymousStructOrUnion() const {
1543 if (!isImplicit() || getDeclName())
1544 return false;
1545
1546 if (const RecordType *Record = getType()->getAs<RecordType>())
1547 return Record->getDecl()->isAnonymousStructOrUnion();
1548
1549 return false;
1550}
1551
1552//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001553// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001554//===----------------------------------------------------------------------===//
1555
John McCall3e11ebe2010-03-15 10:12:16 +00001556void TagDecl::Destroy(ASTContext &C) {
1557 if (hasExtInfo())
1558 C.Deallocate(getExtInfo());
1559 TypeDecl::Destroy(C);
1560}
1561
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001562SourceLocation TagDecl::getOuterLocStart() const {
1563 return getTemplateOrInnerLocStart(this);
1564}
1565
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001566SourceRange TagDecl::getSourceRange() const {
1567 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001568 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001569}
1570
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001571TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001572 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001573}
1574
Douglas Gregora72a4e32010-05-19 18:39:18 +00001575void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1576 TypedefDeclOrQualifier = TDD;
1577 if (TypeForDecl)
1578 TypeForDecl->ClearLinkageCache();
1579}
1580
Douglas Gregordee1be82009-01-17 00:42:38 +00001581void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001582 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1583 TagT->decl.setPointer(this);
1584 TagT->decl.setInt(1);
Douglas Gregor1e13c5a2010-04-30 04:39:27 +00001585 } else if (InjectedClassNameType *Injected
1586 = const_cast<InjectedClassNameType *>(
1587 TypeForDecl->getAs<InjectedClassNameType>())) {
1588 Injected->Decl = cast<CXXRecordDecl>(this);
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001589 }
John McCall67da35c2010-02-04 22:26:26 +00001590
1591 if (isa<CXXRecordDecl>(this)) {
1592 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1593 struct CXXRecordDecl::DefinitionData *Data =
1594 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001595 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1596 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001597 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001598}
1599
1600void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001601 assert((!isa<CXXRecordDecl>(this) ||
1602 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1603 "definition completed but not started");
1604
Douglas Gregordee1be82009-01-17 00:42:38 +00001605 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001606 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1607 assert(TagT->decl.getPointer() == this &&
1608 "Attempt to redefine a tag definition?");
1609 TagT->decl.setInt(0);
Douglas Gregor1e13c5a2010-04-30 04:39:27 +00001610 } else if (InjectedClassNameType *Injected
1611 = const_cast<InjectedClassNameType *>(
1612 TypeForDecl->getAs<InjectedClassNameType>())) {
1613 assert(Injected->Decl == this &&
1614 "Attempt to redefine a class template definition?");
Chandler Carruth2998b542010-05-06 05:28:42 +00001615 (void)Injected;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001616 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001617}
1618
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001619TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001620 if (isDefinition())
1621 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001622
1623 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001624 R != REnd; ++R)
1625 if (R->isDefinition())
1626 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001627
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001628 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001629}
1630
John McCall3e11ebe2010-03-15 10:12:16 +00001631void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1632 SourceRange QualifierRange) {
1633 if (Qualifier) {
1634 // Make sure the extended qualifier info is allocated.
1635 if (!hasExtInfo())
1636 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1637 // Set qualifier info.
1638 getExtInfo()->NNS = Qualifier;
1639 getExtInfo()->NNSRange = QualifierRange;
1640 }
1641 else {
1642 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1643 assert(QualifierRange.isInvalid());
1644 if (hasExtInfo()) {
1645 getASTContext().Deallocate(getExtInfo());
1646 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1647 }
1648 }
1649}
1650
Ted Kremenek21475702008-09-05 17:16:31 +00001651//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001652// EnumDecl Implementation
1653//===----------------------------------------------------------------------===//
1654
1655EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1656 IdentifierInfo *Id, SourceLocation TKL,
1657 EnumDecl *PrevDecl) {
1658 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1659 C.getTypeDeclType(Enum, PrevDecl);
1660 return Enum;
1661}
1662
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001663EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
1664 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation());
1665}
1666
Sebastian Redl833ef452010-01-26 22:01:41 +00001667void EnumDecl::Destroy(ASTContext& C) {
John McCall3e11ebe2010-03-15 10:12:16 +00001668 TagDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001669}
1670
Douglas Gregord5058122010-02-11 01:19:42 +00001671void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001672 QualType NewPromotionType,
1673 unsigned NumPositiveBits,
1674 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001675 assert(!isDefinition() && "Cannot redefine enums!");
1676 IntegerType = NewType;
1677 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001678 setNumPositiveBits(NumPositiveBits);
1679 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001680 TagDecl::completeDefinition();
1681}
1682
1683//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001684// RecordDecl Implementation
1685//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001686
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001687RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001688 IdentifierInfo *Id, RecordDecl *PrevDecl,
1689 SourceLocation TKL)
1690 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001691 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001692 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001693 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001694 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001695}
1696
1697RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001698 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001699 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001700
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001701 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001702 C.getTypeDeclType(R, PrevDecl);
1703 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001704}
1705
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001706RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1707 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1708 SourceLocation());
1709}
1710
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001711RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001712}
1713
1714void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001715 TagDecl::Destroy(C);
1716}
1717
Douglas Gregordfcad112009-03-25 15:59:44 +00001718bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001719 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001720 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1721}
1722
Douglas Gregor91f84212008-12-11 16:49:14 +00001723/// completeDefinition - Notes that the definition of this type is now
1724/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001725void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001726 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001727 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001728}
Steve Naroffcc321422007-03-26 23:09:51 +00001729
John McCall61925b02010-05-21 01:17:40 +00001730ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1731 // Force the decl chain to come into existence properly.
1732 if (!getNextDeclInContext()) getParent()->decls_begin();
1733
1734 assert(isAnonymousStructOrUnion());
1735 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1736 assert(D->getType()->isRecordType());
1737 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1738 return D;
1739}
1740
Steve Naroff415d3d52008-10-08 17:01:13 +00001741//===----------------------------------------------------------------------===//
1742// BlockDecl Implementation
1743//===----------------------------------------------------------------------===//
1744
1745BlockDecl::~BlockDecl() {
1746}
1747
1748void BlockDecl::Destroy(ASTContext& C) {
1749 if (Body)
1750 Body->Destroy(C);
1751
1752 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1753 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001754
1755 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001756 Decl::Destroy(C);
1757}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001758
Douglas Gregord5058122010-02-11 01:19:42 +00001759void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001760 unsigned NParms) {
1761 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001762
Steve Naroffc4b30e52009-03-13 16:56:44 +00001763 // Zero params -> null pointer.
1764 if (NParms) {
1765 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001766 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001767 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1768 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1769 }
1770}
1771
1772unsigned BlockDecl::getNumParams() const {
1773 return NumParams;
1774}
Sebastian Redl833ef452010-01-26 22:01:41 +00001775
1776
1777//===----------------------------------------------------------------------===//
1778// Other Decl Allocation/Deallocation Method Implementations
1779//===----------------------------------------------------------------------===//
1780
1781TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1782 return new (C) TranslationUnitDecl(C);
1783}
1784
1785NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1786 SourceLocation L, IdentifierInfo *Id) {
1787 return new (C) NamespaceDecl(DC, L, Id);
1788}
1789
1790void NamespaceDecl::Destroy(ASTContext& C) {
1791 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1792 // together. They are all top-level Decls.
1793
1794 this->~NamespaceDecl();
John McCall3e11ebe2010-03-15 10:12:16 +00001795 Decl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001796}
1797
1798
1799ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1800 SourceLocation L, IdentifierInfo *Id, QualType T) {
1801 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1802}
1803
1804FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1805 SourceLocation L,
1806 DeclarationName N, QualType T,
1807 TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001808 StorageClass S, StorageClass SCAsWritten,
1809 bool isInline, bool hasWrittenPrototype) {
1810 FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
1811 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001812 New->HasWrittenPrototype = hasWrittenPrototype;
1813 return New;
1814}
1815
1816BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1817 return new (C) BlockDecl(DC, L);
1818}
1819
1820EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1821 SourceLocation L,
1822 IdentifierInfo *Id, QualType T,
1823 Expr *E, const llvm::APSInt &V) {
1824 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1825}
1826
1827void EnumConstantDecl::Destroy(ASTContext& C) {
1828 if (Init) Init->Destroy(C);
John McCall3e11ebe2010-03-15 10:12:16 +00001829 ValueDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001830}
1831
1832TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1833 SourceLocation L, IdentifierInfo *Id,
1834 TypeSourceInfo *TInfo) {
1835 return new (C) TypedefDecl(DC, L, Id, TInfo);
1836}
1837
1838// Anchor TypedefDecl's vtable here.
1839TypedefDecl::~TypedefDecl() {}
1840
1841FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1842 SourceLocation L,
1843 StringLiteral *Str) {
1844 return new (C) FileScopeAsmDecl(DC, L, Str);
1845}