blob: 0170b08cd7edfd74ce7e16bedfddab915455e8ff [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
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000534SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000535 TypeSourceInfo *TSI = getTypeSourceInfo();
536 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000537 return SourceLocation();
538}
539
John McCall3e11ebe2010-03-15 10:12:16 +0000540void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
541 SourceRange QualifierRange) {
542 if (Qualifier) {
543 // Make sure the extended decl info is allocated.
544 if (!hasExtInfo()) {
545 // Save (non-extended) type source info pointer.
546 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
547 // Allocate external info struct.
548 DeclInfo = new (getASTContext()) ExtInfo;
549 // Restore savedTInfo into (extended) decl info.
550 getExtInfo()->TInfo = savedTInfo;
551 }
552 // Set qualifier info.
553 getExtInfo()->NNS = Qualifier;
554 getExtInfo()->NNSRange = QualifierRange;
555 }
556 else {
557 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
558 assert(QualifierRange.isInvalid());
559 if (hasExtInfo()) {
560 // Save type source info pointer.
561 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
562 // Deallocate the extended decl info.
563 getASTContext().Deallocate(getExtInfo());
564 // Restore savedTInfo into (non-extended) decl info.
565 DeclInfo = savedTInfo;
566 }
567 }
568}
569
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000570SourceLocation DeclaratorDecl::getOuterLocStart() const {
571 return getTemplateOrInnerLocStart(this);
572}
573
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000574void
Douglas Gregor20527e22010-06-15 17:44:38 +0000575QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
576 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000577 TemplateParameterList **TPLists) {
578 assert((NumTPLists == 0 || TPLists != 0) &&
579 "Empty array of template parameters with positive size!");
580 assert((NumTPLists == 0 || NNS) &&
581 "Nonempty array of template parameters with no qualifier!");
582
583 // Free previous template parameters (if any).
584 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000585 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000586 TemplParamLists = 0;
587 NumTemplParamLists = 0;
588 }
589 // Set info on matched template parameter lists (if any).
590 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000591 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000592 NumTemplParamLists = NumTPLists;
593 for (unsigned i = NumTPLists; i-- > 0; )
594 TemplParamLists[i] = TPLists[i];
595 }
596}
597
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000598//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000599// VarDecl Implementation
600//===----------------------------------------------------------------------===//
601
Sebastian Redl833ef452010-01-26 22:01:41 +0000602const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
603 switch (SC) {
604 case VarDecl::None: break;
605 case VarDecl::Auto: return "auto"; break;
606 case VarDecl::Extern: return "extern"; break;
607 case VarDecl::PrivateExtern: return "__private_extern__"; break;
608 case VarDecl::Register: return "register"; break;
609 case VarDecl::Static: return "static"; break;
610 }
611
612 assert(0 && "Invalid storage class");
613 return 0;
614}
615
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000616VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000617 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000618 StorageClass S, StorageClass SCAsWritten) {
619 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000620}
621
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000622SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000623 SourceLocation Start = getTypeSpecStartLoc();
624 if (Start.isInvalid())
625 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000626 return Start;
627}
628
629SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000630 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000631 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
632 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000633}
634
Sebastian Redl833ef452010-01-26 22:01:41 +0000635bool VarDecl::isExternC() const {
636 ASTContext &Context = getASTContext();
637 if (!Context.getLangOptions().CPlusPlus)
638 return (getDeclContext()->isTranslationUnit() &&
639 getStorageClass() != Static) ||
640 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
641
642 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
643 DC = DC->getParent()) {
644 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
645 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
646 return getStorageClass() != Static;
647
648 break;
649 }
650
651 if (DC->isFunctionOrMethod())
652 return false;
653 }
654
655 return false;
656}
657
658VarDecl *VarDecl::getCanonicalDecl() {
659 return getFirstDeclaration();
660}
661
Sebastian Redl35351a92010-01-31 22:27:38 +0000662VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
663 // C++ [basic.def]p2:
664 // A declaration is a definition unless [...] it contains the 'extern'
665 // specifier or a linkage-specification and neither an initializer [...],
666 // it declares a static data member in a class declaration [...].
667 // C++ [temp.expl.spec]p15:
668 // An explicit specialization of a static data member of a template is a
669 // definition if the declaration includes an initializer; otherwise, it is
670 // a declaration.
671 if (isStaticDataMember()) {
672 if (isOutOfLine() && (hasInit() ||
673 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
674 return Definition;
675 else
676 return DeclarationOnly;
677 }
678 // C99 6.7p5:
679 // A definition of an identifier is a declaration for that identifier that
680 // [...] causes storage to be reserved for that object.
681 // Note: that applies for all non-file-scope objects.
682 // C99 6.9.2p1:
683 // If the declaration of an identifier for an object has file scope and an
684 // initializer, the declaration is an external definition for the identifier
685 if (hasInit())
686 return Definition;
687 // AST for 'extern "C" int foo;' is annotated with 'extern'.
688 if (hasExternalStorage())
689 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000690
691 if (getStorageClassAsWritten() == Extern ||
692 getStorageClassAsWritten() == PrivateExtern) {
693 for (const VarDecl *PrevVar = getPreviousDeclaration();
694 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
695 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
696 return DeclarationOnly;
697 }
698 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000699 // C99 6.9.2p2:
700 // A declaration of an object that has file scope without an initializer,
701 // and without a storage class specifier or the scs 'static', constitutes
702 // a tentative definition.
703 // No such thing in C++.
704 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
705 return TentativeDefinition;
706
707 // What's left is (in C, block-scope) declarations without initializers or
708 // external storage. These are definitions.
709 return Definition;
710}
711
Sebastian Redl35351a92010-01-31 22:27:38 +0000712VarDecl *VarDecl::getActingDefinition() {
713 DefinitionKind Kind = isThisDeclarationADefinition();
714 if (Kind != TentativeDefinition)
715 return 0;
716
Chris Lattner48eb14d2010-06-14 18:31:46 +0000717 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000718 VarDecl *First = getFirstDeclaration();
719 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
720 I != E; ++I) {
721 Kind = (*I)->isThisDeclarationADefinition();
722 if (Kind == Definition)
723 return 0;
724 else if (Kind == TentativeDefinition)
725 LastTentative = *I;
726 }
727 return LastTentative;
728}
729
730bool VarDecl::isTentativeDefinitionNow() const {
731 DefinitionKind Kind = isThisDeclarationADefinition();
732 if (Kind != TentativeDefinition)
733 return false;
734
735 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
736 if ((*I)->isThisDeclarationADefinition() == Definition)
737 return false;
738 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000739 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000740}
741
Sebastian Redl5ca79842010-02-01 20:16:42 +0000742VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000743 VarDecl *First = getFirstDeclaration();
744 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
745 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000746 if ((*I)->isThisDeclarationADefinition() == Definition)
747 return *I;
748 }
749 return 0;
750}
751
752const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000753 redecl_iterator I = redecls_begin(), E = redecls_end();
754 while (I != E && !I->getInit())
755 ++I;
756
757 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000758 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000759 return I->getInit();
760 }
761 return 0;
762}
763
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000764bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000765 if (Decl::isOutOfLine())
766 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000767
768 if (!isStaticDataMember())
769 return false;
770
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000771 // If this static data member was instantiated from a static data member of
772 // a class template, check whether that static data member was defined
773 // out-of-line.
774 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
775 return VD->isOutOfLine();
776
777 return false;
778}
779
Douglas Gregor1d957a32009-10-27 18:42:08 +0000780VarDecl *VarDecl::getOutOfLineDefinition() {
781 if (!isStaticDataMember())
782 return 0;
783
784 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
785 RD != RDEnd; ++RD) {
786 if (RD->getLexicalDeclContext()->isFileContext())
787 return *RD;
788 }
789
790 return 0;
791}
792
Douglas Gregord5058122010-02-11 01:19:42 +0000793void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000794 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
795 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000796 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000797 }
798
799 Init = I;
800}
801
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000802VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000803 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000804 return cast<VarDecl>(MSI->getInstantiatedFrom());
805
806 return 0;
807}
808
Douglas Gregor3c74d412009-10-14 20:14:33 +0000809TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000810 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000811 return MSI->getTemplateSpecializationKind();
812
813 return TSK_Undeclared;
814}
815
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000816MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000817 return getASTContext().getInstantiatedFromStaticDataMember(this);
818}
819
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000820void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
821 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000822 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000823 assert(MSI && "Not an instantiated static data member?");
824 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000825 if (TSK != TSK_ExplicitSpecialization &&
826 PointOfInstantiation.isValid() &&
827 MSI->getPointOfInstantiation().isInvalid())
828 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000829}
830
Sebastian Redl833ef452010-01-26 22:01:41 +0000831//===----------------------------------------------------------------------===//
832// ParmVarDecl Implementation
833//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000834
Sebastian Redl833ef452010-01-26 22:01:41 +0000835ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
836 SourceLocation L, IdentifierInfo *Id,
837 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000838 StorageClass S, StorageClass SCAsWritten,
839 Expr *DefArg) {
840 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
841 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000842}
843
Sebastian Redl833ef452010-01-26 22:01:41 +0000844Expr *ParmVarDecl::getDefaultArg() {
845 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
846 assert(!hasUninstantiatedDefaultArg() &&
847 "Default argument is not yet instantiated!");
848
849 Expr *Arg = getInit();
850 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
851 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000852
Sebastian Redl833ef452010-01-26 22:01:41 +0000853 return Arg;
854}
855
856unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
857 if (const CXXExprWithTemporaries *E =
858 dyn_cast<CXXExprWithTemporaries>(getInit()))
859 return E->getNumTemporaries();
860
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000861 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000862}
863
Sebastian Redl833ef452010-01-26 22:01:41 +0000864CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
865 assert(getNumDefaultArgTemporaries() &&
866 "Default arguments does not have any temporaries!");
867
868 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
869 return E->getTemporary(i);
870}
871
872SourceRange ParmVarDecl::getDefaultArgRange() const {
873 if (const Expr *E = getInit())
874 return E->getSourceRange();
875
876 if (hasUninstantiatedDefaultArg())
877 return getUninstantiatedDefaultArg()->getSourceRange();
878
879 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000880}
881
Nuno Lopes394ec982008-12-17 23:39:55 +0000882//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000883// FunctionDecl Implementation
884//===----------------------------------------------------------------------===//
885
John McCalle1f2ec22009-09-11 06:45:03 +0000886void FunctionDecl::getNameForDiagnostic(std::string &S,
887 const PrintingPolicy &Policy,
888 bool Qualified) const {
889 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
890 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
891 if (TemplateArgs)
892 S += TemplateSpecializationType::PrintTemplateArgumentList(
893 TemplateArgs->getFlatArgumentList(),
894 TemplateArgs->flat_size(),
895 Policy);
896
897}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000898
Ted Kremenek186a0742010-04-29 16:49:01 +0000899bool FunctionDecl::isVariadic() const {
900 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
901 return FT->isVariadic();
902 return false;
903}
904
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000905bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
906 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
907 if (I->Body) {
908 Definition = *I;
909 return true;
910 }
911 }
912
913 return false;
914}
915
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000916Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000917 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
918 if (I->Body) {
919 Definition = *I;
920 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000921 }
922 }
923
924 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000925}
926
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000927void FunctionDecl::setBody(Stmt *B) {
928 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000929 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000930 EndRangeLoc = B->getLocEnd();
931}
932
Douglas Gregor16618f22009-09-12 00:17:51 +0000933bool FunctionDecl::isMain() const {
934 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000935 return !Context.getLangOptions().Freestanding &&
936 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000937 getIdentifier() && getIdentifier()->isStr("main");
938}
939
Douglas Gregor16618f22009-09-12 00:17:51 +0000940bool FunctionDecl::isExternC() const {
941 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000942 // In C, any non-static, non-overloadable function has external
943 // linkage.
944 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000945 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000946
Mike Stump11289f42009-09-09 15:08:12 +0000947 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000948 DC = DC->getParent()) {
949 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
950 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000951 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000952 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000953
954 break;
955 }
956 }
957
958 return false;
959}
960
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000961bool FunctionDecl::isGlobal() const {
962 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
963 return Method->isStatic();
964
965 if (getStorageClass() == Static)
966 return false;
967
Mike Stump11289f42009-09-09 15:08:12 +0000968 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000969 DC->isNamespace();
970 DC = DC->getParent()) {
971 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
972 if (!Namespace->getDeclName())
973 return false;
974 break;
975 }
976 }
977
978 return true;
979}
980
Sebastian Redl833ef452010-01-26 22:01:41 +0000981void
982FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
983 redeclarable_base::setPreviousDeclaration(PrevDecl);
984
985 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
986 FunctionTemplateDecl *PrevFunTmpl
987 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
988 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
989 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
990 }
991}
992
993const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
994 return getFirstDeclaration();
995}
996
997FunctionDecl *FunctionDecl::getCanonicalDecl() {
998 return getFirstDeclaration();
999}
1000
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001001/// \brief Returns a value indicating whether this function
1002/// corresponds to a builtin function.
1003///
1004/// The function corresponds to a built-in function if it is
1005/// declared at translation scope or within an extern "C" block and
1006/// its name matches with the name of a builtin. The returned value
1007/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001008/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001009/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001010unsigned FunctionDecl::getBuiltinID() const {
1011 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001012 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1013 return 0;
1014
1015 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1016 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1017 return BuiltinID;
1018
1019 // This function has the name of a known C library
1020 // function. Determine whether it actually refers to the C library
1021 // function or whether it just has the same name.
1022
Douglas Gregora908e7f2009-02-17 03:23:10 +00001023 // If this is a static function, it's not a builtin.
1024 if (getStorageClass() == Static)
1025 return 0;
1026
Douglas Gregore711f702009-02-14 18:57:46 +00001027 // If this function is at translation-unit scope and we're not in
1028 // C++, it refers to the C library function.
1029 if (!Context.getLangOptions().CPlusPlus &&
1030 getDeclContext()->isTranslationUnit())
1031 return BuiltinID;
1032
1033 // If the function is in an extern "C" linkage specification and is
1034 // not marked "overloadable", it's the real function.
1035 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001036 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001037 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001038 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001039 return BuiltinID;
1040
1041 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001042 return 0;
1043}
1044
1045
Chris Lattner47c0d002009-04-25 06:03:53 +00001046/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001047/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001048/// after it has been created.
1049unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001050 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001051 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001052 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001053 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001054
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001055}
1056
Douglas Gregord5058122010-02-11 01:19:42 +00001057void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001058 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001059 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001060
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001061 // Zero params -> null pointer.
1062 if (NumParams) {
Douglas Gregord5058122010-02-11 01:19:42 +00001063 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001064 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001065 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001066
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001067 // Update source range. The check below allows us to set EndRangeLoc before
1068 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001069 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001070 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001071 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001072}
Chris Lattner41943152007-01-25 04:52:46 +00001073
Chris Lattner58258242008-04-10 02:22:51 +00001074/// getMinRequiredArguments - Returns the minimum number of arguments
1075/// needed to call this function. This may be fewer than the number of
1076/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001077/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001078unsigned FunctionDecl::getMinRequiredArguments() const {
1079 unsigned NumRequiredArgs = getNumParams();
1080 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001081 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001082 --NumRequiredArgs;
1083
1084 return NumRequiredArgs;
1085}
1086
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001087bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001088 // FIXME: This is not enough. Consider:
1089 //
1090 // inline void f();
1091 // void f() { }
1092 //
1093 // f is inlined, but does not have inline specified.
1094 // To fix this we should add an 'inline' flag to FunctionDecl.
1095 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001096 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001097
1098 if (isa<CXXMethodDecl>(this)) {
1099 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1100 return true;
1101 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001102
1103 switch (getTemplateSpecializationKind()) {
1104 case TSK_Undeclared:
1105 case TSK_ExplicitSpecialization:
1106 return false;
1107
1108 case TSK_ImplicitInstantiation:
1109 case TSK_ExplicitInstantiationDeclaration:
1110 case TSK_ExplicitInstantiationDefinition:
1111 // Handle below.
1112 break;
1113 }
1114
1115 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001116 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001117 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001118 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001119
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001120 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001121 return PatternDecl->isInlined();
1122
1123 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001124}
1125
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001126/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001127/// definition will be externally visible.
1128///
1129/// Inline function definitions are always available for inlining optimizations.
1130/// However, depending on the language dialect, declaration specifiers, and
1131/// attributes, the definition of an inline function may or may not be
1132/// "externally" visible to other translation units in the program.
1133///
1134/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001135/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001136/// inline definition becomes externally visible (C99 6.7.4p6).
1137///
1138/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1139/// definition, we use the GNU semantics for inline, which are nearly the
1140/// opposite of C99 semantics. In particular, "inline" by itself will create
1141/// an externally visible symbol, but "extern inline" will not create an
1142/// externally visible symbol.
1143bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1144 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001145 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001146 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001147
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001148 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001149 // GNU inline semantics. Based on a number of examples, we came up with the
1150 // following heuristic: if the "inline" keyword is present on a
1151 // declaration of the function but "extern" is not present on that
1152 // declaration, then the symbol is externally visible. Otherwise, the GNU
1153 // "extern inline" semantics applies and the symbol is not externally
1154 // visible.
1155 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1156 Redecl != RedeclEnd;
1157 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +00001158 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001159 return true;
1160 }
1161
1162 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001163 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001164 }
1165
1166 // C99 6.7.4p6:
1167 // [...] If all of the file scope declarations for a function in a
1168 // translation unit include the inline function specifier without extern,
1169 // then the definition in that translation unit is an inline definition.
1170 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1171 Redecl != RedeclEnd;
1172 ++Redecl) {
1173 // Only consider file-scope declarations in this test.
1174 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1175 continue;
1176
Douglas Gregor35b57532009-10-27 21:01:01 +00001177 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001178 return true; // Not an inline definition
1179 }
1180
1181 // C99 6.7.4p6:
1182 // An inline definition does not provide an external definition for the
1183 // function, and does not forbid an external definition in another
1184 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001185 return false;
1186}
1187
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001188/// getOverloadedOperator - Which C++ overloaded operator this
1189/// function represents, if any.
1190OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001191 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1192 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001193 else
1194 return OO_None;
1195}
1196
Alexis Huntc88db062010-01-13 09:01:02 +00001197/// getLiteralIdentifier - The literal suffix identifier this function
1198/// represents, if any.
1199const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1200 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1201 return getDeclName().getCXXLiteralIdentifier();
1202 else
1203 return 0;
1204}
1205
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001206FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1207 if (TemplateOrSpecialization.isNull())
1208 return TK_NonTemplate;
1209 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1210 return TK_FunctionTemplate;
1211 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1212 return TK_MemberSpecialization;
1213 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1214 return TK_FunctionTemplateSpecialization;
1215 if (TemplateOrSpecialization.is
1216 <DependentFunctionTemplateSpecializationInfo*>())
1217 return TK_DependentFunctionTemplateSpecialization;
1218
1219 assert(false && "Did we miss a TemplateOrSpecialization type?");
1220 return TK_NonTemplate;
1221}
1222
Douglas Gregord801b062009-10-07 23:56:10 +00001223FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001224 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001225 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1226
1227 return 0;
1228}
1229
Douglas Gregor06db9f52009-10-12 20:18:28 +00001230MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1231 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1232}
1233
Douglas Gregord801b062009-10-07 23:56:10 +00001234void
1235FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1236 TemplateSpecializationKind TSK) {
1237 assert(TemplateOrSpecialization.isNull() &&
1238 "Member function is already a specialization");
1239 MemberSpecializationInfo *Info
1240 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1241 TemplateOrSpecialization = Info;
1242}
1243
Douglas Gregorafca3b42009-10-27 20:53:28 +00001244bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001245 // If the function is invalid, it can't be implicitly instantiated.
1246 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001247 return false;
1248
1249 switch (getTemplateSpecializationKind()) {
1250 case TSK_Undeclared:
1251 case TSK_ExplicitSpecialization:
1252 case TSK_ExplicitInstantiationDefinition:
1253 return false;
1254
1255 case TSK_ImplicitInstantiation:
1256 return true;
1257
1258 case TSK_ExplicitInstantiationDeclaration:
1259 // Handled below.
1260 break;
1261 }
1262
1263 // Find the actual template from which we will instantiate.
1264 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001265 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001266 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001267 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001268
1269 // C++0x [temp.explicit]p9:
1270 // Except for inline functions, other explicit instantiation declarations
1271 // have the effect of suppressing the implicit instantiation of the entity
1272 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001273 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001274 return true;
1275
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001276 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001277}
1278
1279FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1280 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1281 while (Primary->getInstantiatedFromMemberTemplate()) {
1282 // If we have hit a point where the user provided a specialization of
1283 // this template, we're done looking.
1284 if (Primary->isMemberSpecialization())
1285 break;
1286
1287 Primary = Primary->getInstantiatedFromMemberTemplate();
1288 }
1289
1290 return Primary->getTemplatedDecl();
1291 }
1292
1293 return getInstantiatedFromMemberFunction();
1294}
1295
Douglas Gregor70d83e22009-06-29 17:30:29 +00001296FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001297 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001298 = TemplateOrSpecialization
1299 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001300 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001301 }
1302 return 0;
1303}
1304
1305const TemplateArgumentList *
1306FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001307 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001308 = TemplateOrSpecialization
1309 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001310 return Info->TemplateArguments;
1311 }
1312 return 0;
1313}
1314
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001315const TemplateArgumentListInfo *
1316FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1317 if (FunctionTemplateSpecializationInfo *Info
1318 = TemplateOrSpecialization
1319 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1320 return Info->TemplateArgumentsAsWritten;
1321 }
1322 return 0;
1323}
1324
Mike Stump11289f42009-09-09 15:08:12 +00001325void
Douglas Gregord5058122010-02-11 01:19:42 +00001326FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001327 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001328 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001329 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001330 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1331 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001332 assert(TSK != TSK_Undeclared &&
1333 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001334 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001335 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001336 if (!Info)
Douglas Gregord5058122010-02-11 01:19:42 +00001337 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001338
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001339 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001340 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001341 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001342 Info->TemplateArguments = TemplateArgs;
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001343 Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten;
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001344 Info->PointOfInstantiation = PointOfInstantiation;
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001345 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001346
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001347 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001348 // function template specializations.
1349 if (InsertPos)
1350 Template->getSpecializations().InsertNode(Info, InsertPos);
1351 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001352 // Try to insert the new node. If there is an existing node, leave it, the
1353 // set will contain the canonical decls while
1354 // FunctionTemplateDecl::findSpecialization will return
1355 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001356 FunctionTemplateSpecializationInfo *Existing
1357 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001358 (void)Existing;
1359 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1360 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001361 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001362}
1363
John McCallb9c78482010-04-08 09:05:18 +00001364void
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001365FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1366 unsigned NumTemplateArgs,
1367 const TemplateArgument *TemplateArgs,
1368 TemplateSpecializationKind TSK,
1369 unsigned NumTemplateArgsAsWritten,
1370 TemplateArgumentLoc *TemplateArgsAsWritten,
1371 SourceLocation LAngleLoc,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001372 SourceLocation RAngleLoc,
1373 SourceLocation PointOfInstantiation) {
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001374 ASTContext &Ctx = getASTContext();
1375 TemplateArgumentList *TemplArgs
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +00001376 = new (Ctx) TemplateArgumentList(Ctx, TemplateArgs, NumTemplateArgs);
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001377 TemplateArgumentListInfo *TemplArgsInfo
1378 = new (Ctx) TemplateArgumentListInfo(LAngleLoc, RAngleLoc);
1379 for (unsigned i=0; i != NumTemplateArgsAsWritten; ++i)
1380 TemplArgsInfo->addArgument(TemplateArgsAsWritten[i]);
1381
1382 setFunctionTemplateSpecialization(Template, TemplArgs, /*InsertPos=*/0, TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001383 TemplArgsInfo, PointOfInstantiation);
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001384}
1385
1386void
John McCallb9c78482010-04-08 09:05:18 +00001387FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1388 const UnresolvedSetImpl &Templates,
1389 const TemplateArgumentListInfo &TemplateArgs) {
1390 assert(TemplateOrSpecialization.isNull());
1391 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1392 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001393 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001394 void *Buffer = Context.Allocate(Size);
1395 DependentFunctionTemplateSpecializationInfo *Info =
1396 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1397 TemplateArgs);
1398 TemplateOrSpecialization = Info;
1399}
1400
1401DependentFunctionTemplateSpecializationInfo::
1402DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1403 const TemplateArgumentListInfo &TArgs)
1404 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1405
1406 d.NumTemplates = Ts.size();
1407 d.NumArgs = TArgs.size();
1408
1409 FunctionTemplateDecl **TsArray =
1410 const_cast<FunctionTemplateDecl**>(getTemplates());
1411 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1412 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1413
1414 TemplateArgumentLoc *ArgsArray =
1415 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1416 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1417 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1418}
1419
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001420TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001421 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001422 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001423 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001424 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001425 if (FTSInfo)
1426 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001427
Douglas Gregord801b062009-10-07 23:56:10 +00001428 MemberSpecializationInfo *MSInfo
1429 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1430 if (MSInfo)
1431 return MSInfo->getTemplateSpecializationKind();
1432
1433 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001434}
1435
Mike Stump11289f42009-09-09 15:08:12 +00001436void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001437FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1438 SourceLocation PointOfInstantiation) {
1439 if (FunctionTemplateSpecializationInfo *FTSInfo
1440 = TemplateOrSpecialization.dyn_cast<
1441 FunctionTemplateSpecializationInfo*>()) {
1442 FTSInfo->setTemplateSpecializationKind(TSK);
1443 if (TSK != TSK_ExplicitSpecialization &&
1444 PointOfInstantiation.isValid() &&
1445 FTSInfo->getPointOfInstantiation().isInvalid())
1446 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1447 } else if (MemberSpecializationInfo *MSInfo
1448 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1449 MSInfo->setTemplateSpecializationKind(TSK);
1450 if (TSK != TSK_ExplicitSpecialization &&
1451 PointOfInstantiation.isValid() &&
1452 MSInfo->getPointOfInstantiation().isInvalid())
1453 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1454 } else
1455 assert(false && "Function cannot have a template specialization kind");
1456}
1457
1458SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001459 if (FunctionTemplateSpecializationInfo *FTSInfo
1460 = TemplateOrSpecialization.dyn_cast<
1461 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001462 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001463 else if (MemberSpecializationInfo *MSInfo
1464 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001465 return MSInfo->getPointOfInstantiation();
1466
1467 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001468}
1469
Douglas Gregor6411b922009-09-11 20:15:17 +00001470bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001471 if (Decl::isOutOfLine())
1472 return true;
1473
1474 // If this function was instantiated from a member function of a
1475 // class template, check whether that member function was defined out-of-line.
1476 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1477 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001478 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001479 return Definition->isOutOfLine();
1480 }
1481
1482 // If this function was instantiated from a function template,
1483 // check whether that function template was defined out-of-line.
1484 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1485 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001486 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001487 return Definition->isOutOfLine();
1488 }
1489
1490 return false;
1491}
1492
Chris Lattner59a25942008-03-31 00:36:02 +00001493//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001494// FieldDecl Implementation
1495//===----------------------------------------------------------------------===//
1496
1497FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1498 IdentifierInfo *Id, QualType T,
1499 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1500 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1501}
1502
1503bool FieldDecl::isAnonymousStructOrUnion() const {
1504 if (!isImplicit() || getDeclName())
1505 return false;
1506
1507 if (const RecordType *Record = getType()->getAs<RecordType>())
1508 return Record->getDecl()->isAnonymousStructOrUnion();
1509
1510 return false;
1511}
1512
1513//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001514// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001515//===----------------------------------------------------------------------===//
1516
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001517SourceLocation TagDecl::getOuterLocStart() const {
1518 return getTemplateOrInnerLocStart(this);
1519}
1520
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001521SourceRange TagDecl::getSourceRange() const {
1522 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001523 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001524}
1525
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001526TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001527 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001528}
1529
Douglas Gregora72a4e32010-05-19 18:39:18 +00001530void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1531 TypedefDeclOrQualifier = TDD;
1532 if (TypeForDecl)
1533 TypeForDecl->ClearLinkageCache();
1534}
1535
Douglas Gregordee1be82009-01-17 00:42:38 +00001536void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001537 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001538
1539 if (isa<CXXRecordDecl>(this)) {
1540 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1541 struct CXXRecordDecl::DefinitionData *Data =
1542 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001543 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1544 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001545 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001546}
1547
1548void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001549 assert((!isa<CXXRecordDecl>(this) ||
1550 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1551 "definition completed but not started");
1552
Douglas Gregordee1be82009-01-17 00:42:38 +00001553 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001554 IsBeingDefined = false;
Douglas Gregordee1be82009-01-17 00:42:38 +00001555}
1556
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001557TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001558 if (isDefinition())
1559 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001560
1561 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001562 R != REnd; ++R)
1563 if (R->isDefinition())
1564 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001565
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001566 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001567}
1568
John McCall3e11ebe2010-03-15 10:12:16 +00001569void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1570 SourceRange QualifierRange) {
1571 if (Qualifier) {
1572 // Make sure the extended qualifier info is allocated.
1573 if (!hasExtInfo())
1574 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1575 // Set qualifier info.
1576 getExtInfo()->NNS = Qualifier;
1577 getExtInfo()->NNSRange = QualifierRange;
1578 }
1579 else {
1580 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1581 assert(QualifierRange.isInvalid());
1582 if (hasExtInfo()) {
1583 getASTContext().Deallocate(getExtInfo());
1584 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1585 }
1586 }
1587}
1588
Ted Kremenek21475702008-09-05 17:16:31 +00001589//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001590// EnumDecl Implementation
1591//===----------------------------------------------------------------------===//
1592
1593EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1594 IdentifierInfo *Id, SourceLocation TKL,
1595 EnumDecl *PrevDecl) {
1596 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1597 C.getTypeDeclType(Enum, PrevDecl);
1598 return Enum;
1599}
1600
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001601EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
1602 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation());
1603}
1604
Douglas Gregord5058122010-02-11 01:19:42 +00001605void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001606 QualType NewPromotionType,
1607 unsigned NumPositiveBits,
1608 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001609 assert(!isDefinition() && "Cannot redefine enums!");
1610 IntegerType = NewType;
1611 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001612 setNumPositiveBits(NumPositiveBits);
1613 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001614 TagDecl::completeDefinition();
1615}
1616
1617//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001618// RecordDecl Implementation
1619//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001620
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001621RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001622 IdentifierInfo *Id, RecordDecl *PrevDecl,
1623 SourceLocation TKL)
1624 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001625 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001626 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001627 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001628 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001629}
1630
1631RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001632 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001633 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001634
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001635 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001636 C.getTypeDeclType(R, PrevDecl);
1637 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001638}
1639
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001640RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1641 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1642 SourceLocation());
1643}
1644
Douglas Gregordfcad112009-03-25 15:59:44 +00001645bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001646 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001647 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1648}
1649
Douglas Gregor91f84212008-12-11 16:49:14 +00001650/// completeDefinition - Notes that the definition of this type is now
1651/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001652void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001653 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001654 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001655}
Steve Naroffcc321422007-03-26 23:09:51 +00001656
John McCall61925b02010-05-21 01:17:40 +00001657ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1658 // Force the decl chain to come into existence properly.
1659 if (!getNextDeclInContext()) getParent()->decls_begin();
1660
1661 assert(isAnonymousStructOrUnion());
1662 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1663 assert(D->getType()->isRecordType());
1664 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1665 return D;
1666}
1667
Steve Naroff415d3d52008-10-08 17:01:13 +00001668//===----------------------------------------------------------------------===//
1669// BlockDecl Implementation
1670//===----------------------------------------------------------------------===//
1671
Douglas Gregord5058122010-02-11 01:19:42 +00001672void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001673 unsigned NParms) {
1674 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001675
Steve Naroffc4b30e52009-03-13 16:56:44 +00001676 // Zero params -> null pointer.
1677 if (NParms) {
1678 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001679 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001680 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1681 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1682 }
1683}
1684
1685unsigned BlockDecl::getNumParams() const {
1686 return NumParams;
1687}
Sebastian Redl833ef452010-01-26 22:01:41 +00001688
1689
1690//===----------------------------------------------------------------------===//
1691// Other Decl Allocation/Deallocation Method Implementations
1692//===----------------------------------------------------------------------===//
1693
1694TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1695 return new (C) TranslationUnitDecl(C);
1696}
1697
1698NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1699 SourceLocation L, IdentifierInfo *Id) {
1700 return new (C) NamespaceDecl(DC, L, Id);
1701}
1702
Sebastian Redl833ef452010-01-26 22:01:41 +00001703ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1704 SourceLocation L, IdentifierInfo *Id, QualType T) {
1705 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1706}
1707
1708FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001709 const DeclarationNameInfo &NameInfo,
1710 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001711 StorageClass S, StorageClass SCAsWritten,
1712 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001713 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001714 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001715 New->HasWrittenPrototype = hasWrittenPrototype;
1716 return New;
1717}
1718
1719BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1720 return new (C) BlockDecl(DC, L);
1721}
1722
1723EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1724 SourceLocation L,
1725 IdentifierInfo *Id, QualType T,
1726 Expr *E, const llvm::APSInt &V) {
1727 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1728}
1729
Sebastian Redl833ef452010-01-26 22:01:41 +00001730TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1731 SourceLocation L, IdentifierInfo *Id,
1732 TypeSourceInfo *TInfo) {
1733 return new (C) TypedefDecl(DC, L, Id, TInfo);
1734}
1735
Sebastian Redl833ef452010-01-26 22:01:41 +00001736FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1737 SourceLocation L,
1738 StringLiteral *Str) {
1739 return new (C) FileScopeAsmDecl(DC, L, Str);
1740}