blob: d4cc945b1ba083c3f81a24a80e17079634c08e83 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
John McCallf1bbbb42009-09-04 01:14:41 +000026#include "clang/Parse/DeclSpec.h"
27#include "llvm/Support/ErrorHandling.h"
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000028#include <vector>
Ted Kremenek27f8a282008-05-20 00:43:19 +000029
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000032/// \brief Return the TypeLoc wrapper for the type source info.
John McCalla93c9342009-12-07 02:54:59 +000033TypeLoc TypeSourceInfo::getTypeLoc() const {
Argyrios Kyrtzidisb7354712009-09-29 19:40:20 +000034 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000035}
Chris Lattner0b2b6e12009-03-04 06:34:08 +000036
Chris Lattnerd3b90652008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000038// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000039//===----------------------------------------------------------------------===//
40
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000041/// \brief Get the most restrictive linkage for the types in the given
42/// template parameter list.
43static Linkage
44getLinkageForTemplateParameterList(const TemplateParameterList *Params) {
45 Linkage L = ExternalLinkage;
46 for (TemplateParameterList::const_iterator P = Params->begin(),
47 PEnd = Params->end();
48 P != PEnd; ++P) {
49 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
50 if (!NTTP->getType()->isDependentType()) {
51 L = minLinkage(L, NTTP->getType()->getLinkage());
52 continue;
53 }
54
55 if (TemplateTemplateParmDecl *TTP
56 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
57 L = minLinkage(L,
58 getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
59 }
60 }
61
62 return L;
63}
64
65/// \brief Get the most restrictive linkage for the types and
66/// declarations in the given template argument list.
67static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
68 unsigned NumArgs) {
69 Linkage L = ExternalLinkage;
70
71 for (unsigned I = 0; I != NumArgs; ++I) {
72 switch (Args[I].getKind()) {
73 case TemplateArgument::Null:
74 case TemplateArgument::Integral:
75 case TemplateArgument::Expression:
76 break;
77
78 case TemplateArgument::Type:
79 L = minLinkage(L, Args[I].getAsType()->getLinkage());
80 break;
81
82 case TemplateArgument::Declaration:
83 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
84 L = minLinkage(L, ND->getLinkage());
85 if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
86 L = minLinkage(L, VD->getType()->getLinkage());
87 break;
88
89 case TemplateArgument::Template:
90 if (TemplateDecl *Template
91 = Args[I].getAsTemplate().getAsTemplateDecl())
92 L = minLinkage(L, Template->getLinkage());
93 break;
94
95 case TemplateArgument::Pack:
96 L = minLinkage(L,
97 getLinkageForTemplateArgumentList(Args[I].pack_begin(),
98 Args[I].pack_size()));
99 break;
100 }
101 }
102
103 return L;
104}
105
106static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000107 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
108 "Not a name having namespace scope");
109 ASTContext &Context = D->getASTContext();
110
111 // C++ [basic.link]p3:
112 // A name having namespace scope (3.3.6) has internal linkage if it
113 // is the name of
114 // - an object, reference, function or function template that is
115 // explicitly declared static; or,
116 // (This bullet corresponds to C99 6.2.2p3.)
117 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
118 // Explicitly declared static.
119 if (Var->getStorageClass() == VarDecl::Static)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000120 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000121
122 // - an object or reference that is explicitly declared const
123 // and neither explicitly declared extern nor previously
124 // declared to have external linkage; or
125 // (there is no equivalent in C99)
126 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000127 Var->getType().isConstant(Context) &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000128 Var->getStorageClass() != VarDecl::Extern &&
129 Var->getStorageClass() != VarDecl::PrivateExtern) {
130 bool FoundExtern = false;
131 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
132 PrevVar && !FoundExtern;
133 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000134 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000135 FoundExtern = true;
136
137 if (!FoundExtern)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000138 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000139 }
140 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000141 // C++ [temp]p4:
142 // A non-member function template can have internal linkage; any
143 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000144 const FunctionDecl *Function = 0;
145 if (const FunctionTemplateDecl *FunTmpl
146 = dyn_cast<FunctionTemplateDecl>(D))
147 Function = FunTmpl->getTemplatedDecl();
148 else
149 Function = cast<FunctionDecl>(D);
150
151 // Explicitly declared static.
152 if (Function->getStorageClass() == FunctionDecl::Static)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000153 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000154 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
155 // - a data member of an anonymous union.
156 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000157 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000158 }
159
160 // C++ [basic.link]p4:
161
162 // A name having namespace scope has external linkage if it is the
163 // name of
164 //
165 // - an object or reference, unless it has internal linkage; or
166 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
167 if (!Context.getLangOptions().CPlusPlus &&
168 (Var->getStorageClass() == VarDecl::Extern ||
169 Var->getStorageClass() == VarDecl::PrivateExtern)) {
170 // C99 6.2.2p4:
171 // For an identifier declared with the storage-class specifier
172 // extern in a scope in which a prior declaration of that
173 // identifier is visible, if the prior declaration specifies
174 // internal or external linkage, the linkage of the identifier
175 // at the later declaration is the same as the linkage
176 // specified at the prior declaration. If no prior declaration
177 // is visible, or if the prior declaration specifies no
178 // linkage, then the identifier has external linkage.
179 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000180 if (Linkage L = PrevVar->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000181 return L;
182 }
183 }
184
185 // C99 6.2.2p5:
186 // If the declaration of an identifier for an object has file
187 // scope and no storage-class specifier, its linkage is
188 // external.
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000189 if (Var->isInAnonymousNamespace())
190 return UniqueExternalLinkage;
191
192 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000193 }
194
195 // - a function, unless it has internal linkage; or
196 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
197 // C99 6.2.2p5:
198 // If the declaration of an identifier for a function has no
199 // storage-class specifier, its linkage is determined exactly
200 // as if it were declared with the storage-class specifier
201 // extern.
202 if (!Context.getLangOptions().CPlusPlus &&
203 (Function->getStorageClass() == FunctionDecl::Extern ||
204 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
205 Function->getStorageClass() == FunctionDecl::None)) {
206 // C99 6.2.2p4:
207 // For an identifier declared with the storage-class specifier
208 // extern in a scope in which a prior declaration of that
209 // identifier is visible, if the prior declaration specifies
210 // internal or external linkage, the linkage of the identifier
211 // at the later declaration is the same as the linkage
212 // specified at the prior declaration. If no prior declaration
213 // is visible, or if the prior declaration specifies no
214 // linkage, then the identifier has external linkage.
215 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000216 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000217 return L;
218 }
219 }
220
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000221 if (Function->isInAnonymousNamespace())
222 return UniqueExternalLinkage;
223
224 if (FunctionTemplateSpecializationInfo *SpecInfo
225 = Function->getTemplateSpecializationInfo()) {
226 Linkage L = SpecInfo->getTemplate()->getLinkage();
227 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
228 L = minLinkage(L,
229 getLinkageForTemplateArgumentList(
230 TemplateArgs.getFlatArgumentList(),
231 TemplateArgs.flat_size()));
232 return L;
233 }
234
235 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000236 }
237
238 // - a named class (Clause 9), or an unnamed class defined in a
239 // typedef declaration in which the class has the typedef name
240 // for linkage purposes (7.1.3); or
241 // - a named enumeration (7.2), or an unnamed enumeration
242 // defined in a typedef declaration in which the enumeration
243 // has the typedef name for linkage purposes (7.1.3); or
244 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000245 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
246 if (Tag->isInAnonymousNamespace())
247 return UniqueExternalLinkage;
248
249 // If this is a class template specialization, consider the
250 // linkage of the template and template arguments.
251 if (const ClassTemplateSpecializationDecl *Spec
252 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
253 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
254 Linkage L = getLinkageForTemplateArgumentList(
255 TemplateArgs.getFlatArgumentList(),
256 TemplateArgs.flat_size());
257 return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
258 }
259
260 return ExternalLinkage;
261 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000262
263 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000264 if (isa<EnumConstantDecl>(D)) {
265 Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
266 if (isExternalLinkage(L))
267 return L;
268 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000269
270 // - a template, unless it is a function template that has
271 // internal linkage (Clause 14);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000272 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
273 if (D->isInAnonymousNamespace())
274 return UniqueExternalLinkage;
275
276 return getLinkageForTemplateParameterList(
277 Template->getTemplateParameters());
278 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000279
280 // - a namespace (7.3), unless it is declared within an unnamed
281 // namespace.
282 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000283 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000284
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000285 return NoLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000286}
287
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000288Linkage NamedDecl::getLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000289 // Handle linkage for namespace-scope names.
290 if (getDeclContext()->getLookupContext()->isFileContext())
291 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
292 return L;
293
294 // C++ [basic.link]p5:
295 // In addition, a member function, static data member, a named
296 // class or enumeration of class scope, or an unnamed class or
297 // enumeration defined in a class-scope typedef declaration such
298 // that the class or enumeration has the typedef name for linkage
299 // purposes (7.1.3), has external linkage if the name of the class
300 // has external linkage.
301 if (getDeclContext()->isRecord() &&
302 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
303 (isa<TagDecl>(this) &&
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000304 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
305 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
306 if (isExternalLinkage(L))
307 return L;
308 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000309
310 // C++ [basic.link]p6:
311 // The name of a function declared in block scope and the name of
312 // an object declared by a block scope extern declaration have
313 // linkage. If there is a visible declaration of an entity with
314 // linkage having the same name and type, ignoring entities
315 // declared outside the innermost enclosing namespace scope, the
316 // block scope declaration declares that same entity and receives
317 // the linkage of the previous declaration. If there is more than
318 // one such matching entity, the program is ill-formed. Otherwise,
319 // if no matching entity is found, the block scope entity receives
320 // external linkage.
321 if (getLexicalDeclContext()->isFunctionOrMethod()) {
322 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
323 if (Function->getPreviousDeclaration())
324 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
325 return L;
326
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000327 if (Function->isInAnonymousNamespace())
328 return UniqueExternalLinkage;
329
Douglas Gregord85b5b92009-11-25 22:24:25 +0000330 return ExternalLinkage;
331 }
332
333 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
334 if (Var->getStorageClass() == VarDecl::Extern ||
335 Var->getStorageClass() == VarDecl::PrivateExtern) {
336 if (Var->getPreviousDeclaration())
337 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
338 return L;
339
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000340 if (Var->isInAnonymousNamespace())
341 return UniqueExternalLinkage;
342
Douglas Gregord85b5b92009-11-25 22:24:25 +0000343 return ExternalLinkage;
344 }
345 }
346
347 // C++ [basic.link]p6:
348 // Names not covered by these rules have no linkage.
349 return NoLinkage;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000350 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000351
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000352std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000353 return getQualifiedNameAsString(getASTContext().getLangOptions());
354}
355
356std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000357 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
358 // std::string thrashing.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000359 std::vector<std::string> Names;
360 std::string QualName;
361 const DeclContext *Ctx = getDeclContext();
362
363 if (Ctx->isFunctionOrMethod())
364 return getNameAsString();
365
366 while (Ctx) {
Mike Stump1eb44332009-09-09 15:08:12 +0000367 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000368 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
369 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
370 std::string TemplateArgsStr
371 = TemplateSpecializationType::PrintTemplateArgumentList(
372 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000373 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000374 P);
Daniel Dunbare013d682009-10-18 20:26:12 +0000375 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig6be11202009-12-24 23:15:03 +0000376 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
377 if (ND->isAnonymousNamespace())
378 Names.push_back("<anonymous namespace>");
379 else
380 Names.push_back(ND->getNameAsString());
381 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
382 if (!RD->getIdentifier()) {
383 std::string RecordString = "<anonymous ";
384 RecordString += RD->getKindName();
385 RecordString += ">";
386 Names.push_back(RecordString);
387 } else {
388 Names.push_back(RD->getNameAsString());
389 }
Sam Weinig3521d012009-12-28 03:19:38 +0000390 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
391 std::string Proto = FD->getNameAsString();
392
393 const FunctionProtoType *FT = 0;
394 if (FD->hasWrittenPrototype())
395 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
396
397 Proto += "(";
398 if (FT) {
399 llvm::raw_string_ostream POut(Proto);
400 unsigned NumParams = FD->getNumParams();
401 for (unsigned i = 0; i < NumParams; ++i) {
402 if (i)
403 POut << ", ";
404 std::string Param;
405 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
406 POut << Param;
407 }
408
409 if (FT->isVariadic()) {
410 if (NumParams > 0)
411 POut << ", ";
412 POut << "...";
413 }
414 }
415 Proto += ")";
416
417 Names.push_back(Proto);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000418 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000419 Names.push_back(ND->getNameAsString());
420 else
421 break;
422
423 Ctx = Ctx->getParent();
424 }
425
426 std::vector<std::string>::reverse_iterator
427 I = Names.rbegin(),
428 End = Names.rend();
429
430 for (; I!=End; ++I)
431 QualName += *I + "::";
432
John McCall8472af42010-03-16 21:48:18 +0000433 if (getDeclName())
434 QualName += getNameAsString();
435 else
436 QualName += "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000437
438 return QualName;
439}
440
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000441bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000442 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
443
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000444 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
445 // We want to keep it, unless it nominates same namespace.
446 if (getKind() == Decl::UsingDirective) {
447 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
448 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000451 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
452 // For function declarations, we keep track of redeclarations.
453 return FD->getPreviousDeclaration() == OldD;
454
Douglas Gregore53060f2009-06-25 22:08:12 +0000455 // For function templates, the underlying function declarations are linked.
456 if (const FunctionTemplateDecl *FunctionTemplate
457 = dyn_cast<FunctionTemplateDecl>(this))
458 if (const FunctionTemplateDecl *OldFunctionTemplate
459 = dyn_cast<FunctionTemplateDecl>(OldD))
460 return FunctionTemplate->getTemplatedDecl()
461 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Steve Naroff0de21fd2009-02-22 19:35:57 +0000463 // For method declarations, we keep track of redeclarations.
464 if (isa<ObjCMethodDecl>(this))
465 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
John McCallf36e02d2009-10-09 21:13:30 +0000467 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
468 return true;
469
John McCall9488ea12009-11-17 05:59:44 +0000470 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
471 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
472 cast<UsingShadowDecl>(OldD)->getTargetDecl();
473
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000474 // For non-function declarations, if the declarations are of the
475 // same kind then this must be a redeclaration, or semantic analysis
476 // would not have given us the new declaration.
477 return this->getKind() == OldD->getKind();
478}
479
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000480bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000481 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000482}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000483
Anders Carlssone136e0e2009-06-26 06:29:23 +0000484NamedDecl *NamedDecl::getUnderlyingDecl() {
485 NamedDecl *ND = this;
486 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000487 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000488 ND = UD->getTargetDecl();
489 else if (ObjCCompatibleAliasDecl *AD
490 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
491 return AD->getClassInterface();
492 else
493 return ND;
494 }
495}
496
John McCall161755a2010-04-06 21:38:20 +0000497bool NamedDecl::isCXXInstanceMember() const {
498 assert(isCXXClassMember() &&
499 "checking whether non-member is instance member");
500
501 const NamedDecl *D = this;
502 if (isa<UsingShadowDecl>(D))
503 D = cast<UsingShadowDecl>(D)->getTargetDecl();
504
505 if (isa<FieldDecl>(D))
506 return true;
507 if (isa<CXXMethodDecl>(D))
508 return cast<CXXMethodDecl>(D)->isInstance();
509 if (isa<FunctionTemplateDecl>(D))
510 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
511 ->getTemplatedDecl())->isInstance();
512 return false;
513}
514
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000515//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000516// DeclaratorDecl Implementation
517//===----------------------------------------------------------------------===//
518
John McCallb6217662010-03-15 10:12:16 +0000519DeclaratorDecl::~DeclaratorDecl() {}
520void DeclaratorDecl::Destroy(ASTContext &C) {
521 if (hasExtInfo())
522 C.Deallocate(getExtInfo());
523 ValueDecl::Destroy(C);
524}
525
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000526SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000527 if (DeclInfo) {
John McCallb6217662010-03-15 10:12:16 +0000528 TypeLoc TL = getTypeSourceInfo()->getTypeLoc();
John McCall51bd8032009-10-18 01:05:36 +0000529 while (true) {
530 TypeLoc NextTL = TL.getNextTypeLoc();
531 if (!NextTL)
532 return TL.getSourceRange().getBegin();
533 TL = NextTL;
534 }
535 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000536 return SourceLocation();
537}
538
John McCallb6217662010-03-15 10:12:16 +0000539void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
540 SourceRange QualifierRange) {
541 if (Qualifier) {
542 // Make sure the extended decl info is allocated.
543 if (!hasExtInfo()) {
544 // Save (non-extended) type source info pointer.
545 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
546 // Allocate external info struct.
547 DeclInfo = new (getASTContext()) ExtInfo;
548 // Restore savedTInfo into (extended) decl info.
549 getExtInfo()->TInfo = savedTInfo;
550 }
551 // Set qualifier info.
552 getExtInfo()->NNS = Qualifier;
553 getExtInfo()->NNSRange = QualifierRange;
554 }
555 else {
556 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
557 assert(QualifierRange.isInvalid());
558 if (hasExtInfo()) {
559 // Save type source info pointer.
560 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
561 // Deallocate the extended decl info.
562 getASTContext().Deallocate(getExtInfo());
563 // Restore savedTInfo into (non-extended) decl info.
564 DeclInfo = savedTInfo;
565 }
566 }
567}
568
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000569//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000570// VarDecl Implementation
571//===----------------------------------------------------------------------===//
572
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000573const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
574 switch (SC) {
575 case VarDecl::None: break;
576 case VarDecl::Auto: return "auto"; break;
577 case VarDecl::Extern: return "extern"; break;
578 case VarDecl::PrivateExtern: return "__private_extern__"; break;
579 case VarDecl::Register: return "register"; break;
580 case VarDecl::Static: return "static"; break;
581 }
582
583 assert(0 && "Invalid storage class");
584 return 0;
585}
586
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000587VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000588 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000589 StorageClass S) {
John McCalla93c9342009-12-07 02:54:59 +0000590 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000591}
592
593void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000594 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000595 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000596 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000597 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
598 Eval->~EvaluatedStmt();
599 C.Deallocate(Eval);
600 }
601 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000602 this->~VarDecl();
John McCallb6217662010-03-15 10:12:16 +0000603 DeclaratorDecl::Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000604}
605
606VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000607}
608
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000609SourceRange VarDecl::getSourceRange() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000610 SourceLocation Start = getTypeSpecStartLoc();
611 if (Start.isInvalid())
612 Start = getLocation();
613
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000614 if (getInit())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000615 return SourceRange(Start, getInit()->getLocEnd());
616 return SourceRange(Start, getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000617}
618
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000619bool VarDecl::isExternC() const {
620 ASTContext &Context = getASTContext();
621 if (!Context.getLangOptions().CPlusPlus)
622 return (getDeclContext()->isTranslationUnit() &&
623 getStorageClass() != Static) ||
624 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
625
626 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
627 DC = DC->getParent()) {
628 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
629 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
630 return getStorageClass() != Static;
631
632 break;
633 }
634
635 if (DC->isFunctionOrMethod())
636 return false;
637 }
638
639 return false;
640}
641
642VarDecl *VarDecl::getCanonicalDecl() {
643 return getFirstDeclaration();
644}
645
Sebastian Redle9d12b62010-01-31 22:27:38 +0000646VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
647 // C++ [basic.def]p2:
648 // A declaration is a definition unless [...] it contains the 'extern'
649 // specifier or a linkage-specification and neither an initializer [...],
650 // it declares a static data member in a class declaration [...].
651 // C++ [temp.expl.spec]p15:
652 // An explicit specialization of a static data member of a template is a
653 // definition if the declaration includes an initializer; otherwise, it is
654 // a declaration.
655 if (isStaticDataMember()) {
656 if (isOutOfLine() && (hasInit() ||
657 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
658 return Definition;
659 else
660 return DeclarationOnly;
661 }
662 // C99 6.7p5:
663 // A definition of an identifier is a declaration for that identifier that
664 // [...] causes storage to be reserved for that object.
665 // Note: that applies for all non-file-scope objects.
666 // C99 6.9.2p1:
667 // If the declaration of an identifier for an object has file scope and an
668 // initializer, the declaration is an external definition for the identifier
669 if (hasInit())
670 return Definition;
671 // AST for 'extern "C" int foo;' is annotated with 'extern'.
672 if (hasExternalStorage())
673 return DeclarationOnly;
674
675 // C99 6.9.2p2:
676 // A declaration of an object that has file scope without an initializer,
677 // and without a storage class specifier or the scs 'static', constitutes
678 // a tentative definition.
679 // No such thing in C++.
680 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
681 return TentativeDefinition;
682
683 // What's left is (in C, block-scope) declarations without initializers or
684 // external storage. These are definitions.
685 return Definition;
686}
687
Sebastian Redle9d12b62010-01-31 22:27:38 +0000688VarDecl *VarDecl::getActingDefinition() {
689 DefinitionKind Kind = isThisDeclarationADefinition();
690 if (Kind != TentativeDefinition)
691 return 0;
692
693 VarDecl *LastTentative = false;
694 VarDecl *First = getFirstDeclaration();
695 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
696 I != E; ++I) {
697 Kind = (*I)->isThisDeclarationADefinition();
698 if (Kind == Definition)
699 return 0;
700 else if (Kind == TentativeDefinition)
701 LastTentative = *I;
702 }
703 return LastTentative;
704}
705
706bool VarDecl::isTentativeDefinitionNow() const {
707 DefinitionKind Kind = isThisDeclarationADefinition();
708 if (Kind != TentativeDefinition)
709 return false;
710
711 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
712 if ((*I)->isThisDeclarationADefinition() == Definition)
713 return false;
714 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000715 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000716}
717
Sebastian Redl31310a22010-02-01 20:16:42 +0000718VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +0000719 VarDecl *First = getFirstDeclaration();
720 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
721 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000722 if ((*I)->isThisDeclarationADefinition() == Definition)
723 return *I;
724 }
725 return 0;
726}
727
728const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000729 redecl_iterator I = redecls_begin(), E = redecls_end();
730 while (I != E && !I->getInit())
731 ++I;
732
733 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000734 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000735 return I->getInit();
736 }
737 return 0;
738}
739
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000740bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000741 if (Decl::isOutOfLine())
742 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +0000743
744 if (!isStaticDataMember())
745 return false;
746
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000747 // If this static data member was instantiated from a static data member of
748 // a class template, check whether that static data member was defined
749 // out-of-line.
750 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
751 return VD->isOutOfLine();
752
753 return false;
754}
755
Douglas Gregor0d035142009-10-27 18:42:08 +0000756VarDecl *VarDecl::getOutOfLineDefinition() {
757 if (!isStaticDataMember())
758 return 0;
759
760 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
761 RD != RDEnd; ++RD) {
762 if (RD->getLexicalDeclContext()->isFileContext())
763 return *RD;
764 }
765
766 return 0;
767}
768
Douglas Gregor838db382010-02-11 01:19:42 +0000769void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000770 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
771 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +0000772 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000773 }
774
775 Init = I;
776}
777
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000778VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000779 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000780 return cast<VarDecl>(MSI->getInstantiatedFrom());
781
782 return 0;
783}
784
Douglas Gregor663b5a02009-10-14 20:14:33 +0000785TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +0000786 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000787 return MSI->getTemplateSpecializationKind();
788
789 return TSK_Undeclared;
790}
791
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000792MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000793 return getASTContext().getInstantiatedFromStaticDataMember(this);
794}
795
Douglas Gregor0a897e32009-10-15 17:21:20 +0000796void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
797 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000798 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000799 assert(MSI && "Not an instantiated static data member?");
800 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000801 if (TSK != TSK_ExplicitSpecialization &&
802 PointOfInstantiation.isValid() &&
803 MSI->getPointOfInstantiation().isInvalid())
804 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000805}
806
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000807//===----------------------------------------------------------------------===//
808// ParmVarDecl Implementation
809//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +0000810
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000811ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
812 SourceLocation L, IdentifierInfo *Id,
813 QualType T, TypeSourceInfo *TInfo,
814 StorageClass S, Expr *DefArg) {
815 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +0000816}
817
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000818Expr *ParmVarDecl::getDefaultArg() {
819 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
820 assert(!hasUninstantiatedDefaultArg() &&
821 "Default argument is not yet instantiated!");
822
823 Expr *Arg = getInit();
824 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
825 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +0000826
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000827 return Arg;
828}
829
830unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
831 if (const CXXExprWithTemporaries *E =
832 dyn_cast<CXXExprWithTemporaries>(getInit()))
833 return E->getNumTemporaries();
834
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000835 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000836}
837
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000838CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
839 assert(getNumDefaultArgTemporaries() &&
840 "Default arguments does not have any temporaries!");
841
842 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
843 return E->getTemporary(i);
844}
845
846SourceRange ParmVarDecl::getDefaultArgRange() const {
847 if (const Expr *E = getInit())
848 return E->getSourceRange();
849
850 if (hasUninstantiatedDefaultArg())
851 return getUninstantiatedDefaultArg()->getSourceRange();
852
853 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000854}
855
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000856//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000857// FunctionDecl Implementation
858//===----------------------------------------------------------------------===//
859
Ted Kremenek27f8a282008-05-20 00:43:19 +0000860void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000861 if (Body && Body.isOffset())
862 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000863
864 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
865 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000866
Douglas Gregor2db32322009-10-07 23:56:10 +0000867 FunctionTemplateSpecializationInfo *FTSInfo
868 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
869 if (FTSInfo)
870 C.Deallocate(FTSInfo);
871
872 MemberSpecializationInfo *MSInfo
873 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
874 if (MSInfo)
875 C.Deallocate(MSInfo);
876
Steve Naroff3e970492009-01-27 21:25:57 +0000877 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000878
John McCallb6217662010-03-15 10:12:16 +0000879 DeclaratorDecl::Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +0000880}
881
John McCall136a6982009-09-11 06:45:03 +0000882void FunctionDecl::getNameForDiagnostic(std::string &S,
883 const PrintingPolicy &Policy,
884 bool Qualified) const {
885 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
886 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
887 if (TemplateArgs)
888 S += TemplateSpecializationType::PrintTemplateArgumentList(
889 TemplateArgs->getFlatArgumentList(),
890 TemplateArgs->flat_size(),
891 Policy);
892
893}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000894
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000895Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000896 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
897 if (I->Body) {
898 Definition = *I;
899 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000900 }
901 }
902
903 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000904}
905
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000906void FunctionDecl::setBody(Stmt *B) {
907 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000908 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000909 EndRangeLoc = B->getLocEnd();
910}
911
Douglas Gregor48a83b52009-09-12 00:17:51 +0000912bool FunctionDecl::isMain() const {
913 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000914 return !Context.getLangOptions().Freestanding &&
915 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000916 getIdentifier() && getIdentifier()->isStr("main");
917}
918
Douglas Gregor48a83b52009-09-12 00:17:51 +0000919bool FunctionDecl::isExternC() const {
920 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000921 // In C, any non-static, non-overloadable function has external
922 // linkage.
923 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000924 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000925
Mike Stump1eb44332009-09-09 15:08:12 +0000926 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000927 DC = DC->getParent()) {
928 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
929 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000930 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000931 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000932
933 break;
934 }
935 }
936
937 return false;
938}
939
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000940bool FunctionDecl::isGlobal() const {
941 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
942 return Method->isStatic();
943
944 if (getStorageClass() == Static)
945 return false;
946
Mike Stump1eb44332009-09-09 15:08:12 +0000947 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000948 DC->isNamespace();
949 DC = DC->getParent()) {
950 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
951 if (!Namespace->getDeclName())
952 return false;
953 break;
954 }
955 }
956
957 return true;
958}
959
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000960void
961FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
962 redeclarable_base::setPreviousDeclaration(PrevDecl);
963
964 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
965 FunctionTemplateDecl *PrevFunTmpl
966 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
967 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
968 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
969 }
970}
971
972const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
973 return getFirstDeclaration();
974}
975
976FunctionDecl *FunctionDecl::getCanonicalDecl() {
977 return getFirstDeclaration();
978}
979
Douglas Gregor3e41d602009-02-13 23:20:09 +0000980/// \brief Returns a value indicating whether this function
981/// corresponds to a builtin function.
982///
983/// The function corresponds to a built-in function if it is
984/// declared at translation scope or within an extern "C" block and
985/// its name matches with the name of a builtin. The returned value
986/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000987/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000988/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000989unsigned FunctionDecl::getBuiltinID() const {
990 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000991 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
992 return 0;
993
994 unsigned BuiltinID = getIdentifier()->getBuiltinID();
995 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
996 return BuiltinID;
997
998 // This function has the name of a known C library
999 // function. Determine whether it actually refers to the C library
1000 // function or whether it just has the same name.
1001
Douglas Gregor9add3172009-02-17 03:23:10 +00001002 // If this is a static function, it's not a builtin.
1003 if (getStorageClass() == Static)
1004 return 0;
1005
Douglas Gregor3c385e52009-02-14 18:57:46 +00001006 // If this function is at translation-unit scope and we're not in
1007 // C++, it refers to the C library function.
1008 if (!Context.getLangOptions().CPlusPlus &&
1009 getDeclContext()->isTranslationUnit())
1010 return BuiltinID;
1011
1012 // If the function is in an extern "C" linkage specification and is
1013 // not marked "overloadable", it's the real function.
1014 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001015 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001016 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001017 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001018 return BuiltinID;
1019
1020 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001021 return 0;
1022}
1023
1024
Chris Lattner1ad9b282009-04-25 06:03:53 +00001025/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001026/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001027/// after it has been created.
1028unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001029 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001030 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001031 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001032 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Reid Spencer5f016e22007-07-11 17:01:13 +00001034}
1035
Douglas Gregor838db382010-02-11 01:19:42 +00001036void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001038 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 // Zero params -> null pointer.
1041 if (NumParams) {
Douglas Gregor838db382010-02-11 01:19:42 +00001042 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001043 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001045
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001046 // Update source range. The check below allows us to set EndRangeLoc before
1047 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001048 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001049 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 }
1051}
1052
Chris Lattner8123a952008-04-10 02:22:51 +00001053/// getMinRequiredArguments - Returns the minimum number of arguments
1054/// needed to call this function. This may be fewer than the number of
1055/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001056/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001057unsigned FunctionDecl::getMinRequiredArguments() const {
1058 unsigned NumRequiredArgs = getNumParams();
1059 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001060 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001061 --NumRequiredArgs;
1062
1063 return NumRequiredArgs;
1064}
1065
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001066bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001067 // FIXME: This is not enough. Consider:
1068 //
1069 // inline void f();
1070 // void f() { }
1071 //
1072 // f is inlined, but does not have inline specified.
1073 // To fix this we should add an 'inline' flag to FunctionDecl.
1074 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001075 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001076
1077 if (isa<CXXMethodDecl>(this)) {
1078 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1079 return true;
1080 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001081
1082 switch (getTemplateSpecializationKind()) {
1083 case TSK_Undeclared:
1084 case TSK_ExplicitSpecialization:
1085 return false;
1086
1087 case TSK_ImplicitInstantiation:
1088 case TSK_ExplicitInstantiationDeclaration:
1089 case TSK_ExplicitInstantiationDefinition:
1090 // Handle below.
1091 break;
1092 }
1093
1094 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1095 Stmt *Pattern = 0;
1096 if (PatternDecl)
1097 Pattern = PatternDecl->getBody(PatternDecl);
1098
1099 if (Pattern && PatternDecl)
1100 return PatternDecl->isInlined();
1101
1102 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001103}
1104
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001105/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001106/// definition will be externally visible.
1107///
1108/// Inline function definitions are always available for inlining optimizations.
1109/// However, depending on the language dialect, declaration specifiers, and
1110/// attributes, the definition of an inline function may or may not be
1111/// "externally" visible to other translation units in the program.
1112///
1113/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001114/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001115/// inline definition becomes externally visible (C99 6.7.4p6).
1116///
1117/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1118/// definition, we use the GNU semantics for inline, which are nearly the
1119/// opposite of C99 semantics. In particular, "inline" by itself will create
1120/// an externally visible symbol, but "extern inline" will not create an
1121/// externally visible symbol.
1122bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1123 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001124 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001125 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001126
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001127 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001128 // GNU inline semantics. Based on a number of examples, we came up with the
1129 // following heuristic: if the "inline" keyword is present on a
1130 // declaration of the function but "extern" is not present on that
1131 // declaration, then the symbol is externally visible. Otherwise, the GNU
1132 // "extern inline" semantics applies and the symbol is not externally
1133 // visible.
1134 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1135 Redecl != RedeclEnd;
1136 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001137 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001138 return true;
1139 }
1140
1141 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001142 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001143 }
1144
1145 // C99 6.7.4p6:
1146 // [...] If all of the file scope declarations for a function in a
1147 // translation unit include the inline function specifier without extern,
1148 // then the definition in that translation unit is an inline definition.
1149 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1150 Redecl != RedeclEnd;
1151 ++Redecl) {
1152 // Only consider file-scope declarations in this test.
1153 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1154 continue;
1155
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001156 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001157 return true; // Not an inline definition
1158 }
1159
1160 // C99 6.7.4p6:
1161 // An inline definition does not provide an external definition for the
1162 // function, and does not forbid an external definition in another
1163 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001164 return false;
1165}
1166
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001167/// getOverloadedOperator - Which C++ overloaded operator this
1168/// function represents, if any.
1169OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001170 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1171 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001172 else
1173 return OO_None;
1174}
1175
Sean Hunta6c058d2010-01-13 09:01:02 +00001176/// getLiteralIdentifier - The literal suffix identifier this function
1177/// represents, if any.
1178const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1179 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1180 return getDeclName().getCXXLiteralIdentifier();
1181 else
1182 return 0;
1183}
1184
Douglas Gregor2db32322009-10-07 23:56:10 +00001185FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001186 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001187 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1188
1189 return 0;
1190}
1191
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001192MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1193 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1194}
1195
Douglas Gregor2db32322009-10-07 23:56:10 +00001196void
1197FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1198 TemplateSpecializationKind TSK) {
1199 assert(TemplateOrSpecialization.isNull() &&
1200 "Member function is already a specialization");
1201 MemberSpecializationInfo *Info
1202 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1203 TemplateOrSpecialization = Info;
1204}
1205
Douglas Gregor3b846b62009-10-27 20:53:28 +00001206bool FunctionDecl::isImplicitlyInstantiable() const {
1207 // If this function already has a definition or is invalid, it can't be
1208 // implicitly instantiated.
1209 if (isInvalidDecl() || getBody())
1210 return false;
1211
1212 switch (getTemplateSpecializationKind()) {
1213 case TSK_Undeclared:
1214 case TSK_ExplicitSpecialization:
1215 case TSK_ExplicitInstantiationDefinition:
1216 return false;
1217
1218 case TSK_ImplicitInstantiation:
1219 return true;
1220
1221 case TSK_ExplicitInstantiationDeclaration:
1222 // Handled below.
1223 break;
1224 }
1225
1226 // Find the actual template from which we will instantiate.
1227 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1228 Stmt *Pattern = 0;
1229 if (PatternDecl)
1230 Pattern = PatternDecl->getBody(PatternDecl);
1231
1232 // C++0x [temp.explicit]p9:
1233 // Except for inline functions, other explicit instantiation declarations
1234 // have the effect of suppressing the implicit instantiation of the entity
1235 // to which they refer.
1236 if (!Pattern || !PatternDecl)
1237 return true;
1238
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001239 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001240}
1241
1242FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1243 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1244 while (Primary->getInstantiatedFromMemberTemplate()) {
1245 // If we have hit a point where the user provided a specialization of
1246 // this template, we're done looking.
1247 if (Primary->isMemberSpecialization())
1248 break;
1249
1250 Primary = Primary->getInstantiatedFromMemberTemplate();
1251 }
1252
1253 return Primary->getTemplatedDecl();
1254 }
1255
1256 return getInstantiatedFromMemberFunction();
1257}
1258
Douglas Gregor16e8be22009-06-29 17:30:29 +00001259FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001260 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001261 = TemplateOrSpecialization
1262 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001263 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001264 }
1265 return 0;
1266}
1267
1268const TemplateArgumentList *
1269FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001270 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001271 = TemplateOrSpecialization
1272 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001273 return Info->TemplateArguments;
1274 }
1275 return 0;
1276}
1277
Mike Stump1eb44332009-09-09 15:08:12 +00001278void
Douglas Gregor838db382010-02-11 01:19:42 +00001279FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001280 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001281 void *InsertPos,
1282 TemplateSpecializationKind TSK) {
1283 assert(TSK != TSK_Undeclared &&
1284 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001285 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001286 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001287 if (!Info)
Douglas Gregor838db382010-02-11 01:19:42 +00001288 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregor127102b2009-06-29 20:59:39 +00001290 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001291 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001292 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001293 Info->TemplateArguments = TemplateArgs;
1294 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Douglas Gregor127102b2009-06-29 20:59:39 +00001296 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001297 // function template specializations.
1298 if (InsertPos)
1299 Template->getSpecializations().InsertNode(Info, InsertPos);
1300 else {
1301 // Try to insert the new node. If there is an existing node, remove it
1302 // first.
1303 FunctionTemplateSpecializationInfo *Existing
1304 = Template->getSpecializations().GetOrInsertNode(Info);
1305 if (Existing) {
1306 Template->getSpecializations().RemoveNode(Existing);
1307 Template->getSpecializations().GetOrInsertNode(Info);
1308 }
1309 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001310}
1311
John McCallaf2094e2010-04-08 09:05:18 +00001312void
1313FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1314 const UnresolvedSetImpl &Templates,
1315 const TemplateArgumentListInfo &TemplateArgs) {
1316 assert(TemplateOrSpecialization.isNull());
1317 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1318 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001319 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001320 void *Buffer = Context.Allocate(Size);
1321 DependentFunctionTemplateSpecializationInfo *Info =
1322 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1323 TemplateArgs);
1324 TemplateOrSpecialization = Info;
1325}
1326
1327DependentFunctionTemplateSpecializationInfo::
1328DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1329 const TemplateArgumentListInfo &TArgs)
1330 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1331
1332 d.NumTemplates = Ts.size();
1333 d.NumArgs = TArgs.size();
1334
1335 FunctionTemplateDecl **TsArray =
1336 const_cast<FunctionTemplateDecl**>(getTemplates());
1337 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1338 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1339
1340 TemplateArgumentLoc *ArgsArray =
1341 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1342 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1343 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1344}
1345
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001346TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001347 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001348 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001349 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001350 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001351 if (FTSInfo)
1352 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Douglas Gregor2db32322009-10-07 23:56:10 +00001354 MemberSpecializationInfo *MSInfo
1355 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1356 if (MSInfo)
1357 return MSInfo->getTemplateSpecializationKind();
1358
1359 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001360}
1361
Mike Stump1eb44332009-09-09 15:08:12 +00001362void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001363FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1364 SourceLocation PointOfInstantiation) {
1365 if (FunctionTemplateSpecializationInfo *FTSInfo
1366 = TemplateOrSpecialization.dyn_cast<
1367 FunctionTemplateSpecializationInfo*>()) {
1368 FTSInfo->setTemplateSpecializationKind(TSK);
1369 if (TSK != TSK_ExplicitSpecialization &&
1370 PointOfInstantiation.isValid() &&
1371 FTSInfo->getPointOfInstantiation().isInvalid())
1372 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1373 } else if (MemberSpecializationInfo *MSInfo
1374 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1375 MSInfo->setTemplateSpecializationKind(TSK);
1376 if (TSK != TSK_ExplicitSpecialization &&
1377 PointOfInstantiation.isValid() &&
1378 MSInfo->getPointOfInstantiation().isInvalid())
1379 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1380 } else
1381 assert(false && "Function cannot have a template specialization kind");
1382}
1383
1384SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001385 if (FunctionTemplateSpecializationInfo *FTSInfo
1386 = TemplateOrSpecialization.dyn_cast<
1387 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001388 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001389 else if (MemberSpecializationInfo *MSInfo
1390 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001391 return MSInfo->getPointOfInstantiation();
1392
1393 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001394}
1395
Douglas Gregor9f185072009-09-11 20:15:17 +00001396bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001397 if (Decl::isOutOfLine())
1398 return true;
1399
1400 // If this function was instantiated from a member function of a
1401 // class template, check whether that member function was defined out-of-line.
1402 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1403 const FunctionDecl *Definition;
1404 if (FD->getBody(Definition))
1405 return Definition->isOutOfLine();
1406 }
1407
1408 // If this function was instantiated from a function template,
1409 // check whether that function template was defined out-of-line.
1410 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1411 const FunctionDecl *Definition;
1412 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1413 return Definition->isOutOfLine();
1414 }
1415
1416 return false;
1417}
1418
Chris Lattner8a934232008-03-31 00:36:02 +00001419//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001420// FieldDecl Implementation
1421//===----------------------------------------------------------------------===//
1422
1423FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1424 IdentifierInfo *Id, QualType T,
1425 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1426 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1427}
1428
1429bool FieldDecl::isAnonymousStructOrUnion() const {
1430 if (!isImplicit() || getDeclName())
1431 return false;
1432
1433 if (const RecordType *Record = getType()->getAs<RecordType>())
1434 return Record->getDecl()->isAnonymousStructOrUnion();
1435
1436 return false;
1437}
1438
1439//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001440// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001441//===----------------------------------------------------------------------===//
1442
John McCallb6217662010-03-15 10:12:16 +00001443void TagDecl::Destroy(ASTContext &C) {
1444 if (hasExtInfo())
1445 C.Deallocate(getExtInfo());
1446 TypeDecl::Destroy(C);
1447}
1448
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001449SourceRange TagDecl::getSourceRange() const {
1450 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001451 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001452}
1453
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001454TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001455 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001456}
1457
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001458void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001459 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1460 TagT->decl.setPointer(this);
1461 TagT->decl.setInt(1);
1462 }
John McCall86ff3082010-02-04 22:26:26 +00001463
1464 if (isa<CXXRecordDecl>(this)) {
1465 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1466 struct CXXRecordDecl::DefinitionData *Data =
1467 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001468 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1469 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001470 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001471}
1472
1473void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001474 assert((!isa<CXXRecordDecl>(this) ||
1475 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1476 "definition completed but not started");
1477
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001478 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001479 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1480 assert(TagT->decl.getPointer() == this &&
1481 "Attempt to redefine a tag definition?");
1482 TagT->decl.setInt(0);
1483 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001484}
1485
Douglas Gregor952b0172010-02-11 01:04:33 +00001486TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001487 if (isDefinition())
1488 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001489
1490 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001491 R != REnd; ++R)
1492 if (R->isDefinition())
1493 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001495 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001496}
1497
John McCallf1bbbb42009-09-04 01:14:41 +00001498TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1499 switch (TypeSpec) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001500 default: llvm_unreachable("unexpected type specifier");
John McCallf1bbbb42009-09-04 01:14:41 +00001501 case DeclSpec::TST_struct: return TK_struct;
1502 case DeclSpec::TST_class: return TK_class;
1503 case DeclSpec::TST_union: return TK_union;
1504 case DeclSpec::TST_enum: return TK_enum;
1505 }
1506}
1507
John McCallb6217662010-03-15 10:12:16 +00001508void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1509 SourceRange QualifierRange) {
1510 if (Qualifier) {
1511 // Make sure the extended qualifier info is allocated.
1512 if (!hasExtInfo())
1513 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1514 // Set qualifier info.
1515 getExtInfo()->NNS = Qualifier;
1516 getExtInfo()->NNSRange = QualifierRange;
1517 }
1518 else {
1519 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1520 assert(QualifierRange.isInvalid());
1521 if (hasExtInfo()) {
1522 getASTContext().Deallocate(getExtInfo());
1523 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1524 }
1525 }
1526}
1527
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001528//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001529// EnumDecl Implementation
1530//===----------------------------------------------------------------------===//
1531
1532EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1533 IdentifierInfo *Id, SourceLocation TKL,
1534 EnumDecl *PrevDecl) {
1535 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1536 C.getTypeDeclType(Enum, PrevDecl);
1537 return Enum;
1538}
1539
1540void EnumDecl::Destroy(ASTContext& C) {
John McCallb6217662010-03-15 10:12:16 +00001541 TagDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001542}
1543
Douglas Gregor838db382010-02-11 01:19:42 +00001544void EnumDecl::completeDefinition(QualType NewType,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001545 QualType NewPromotionType) {
1546 assert(!isDefinition() && "Cannot redefine enums!");
1547 IntegerType = NewType;
1548 PromotionType = NewPromotionType;
1549 TagDecl::completeDefinition();
1550}
1551
1552//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001553// RecordDecl Implementation
1554//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001555
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001556RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001557 IdentifierInfo *Id, RecordDecl *PrevDecl,
1558 SourceLocation TKL)
1559 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001560 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001561 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001562 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001563 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001564}
1565
1566RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001567 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001568 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001570 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001571 C.getTypeDeclType(R, PrevDecl);
1572 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001573}
1574
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001575RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001576}
1577
1578void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001579 TagDecl::Destroy(C);
1580}
1581
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001582bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001583 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001584 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1585}
1586
Douglas Gregor44b43212008-12-11 16:49:14 +00001587/// completeDefinition - Notes that the definition of this type is now
1588/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001589void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001591 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001592}
1593
Steve Naroff56ee6892008-10-08 17:01:13 +00001594//===----------------------------------------------------------------------===//
1595// BlockDecl Implementation
1596//===----------------------------------------------------------------------===//
1597
1598BlockDecl::~BlockDecl() {
1599}
1600
1601void BlockDecl::Destroy(ASTContext& C) {
1602 if (Body)
1603 Body->Destroy(C);
1604
1605 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1606 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001607
1608 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001609 Decl::Destroy(C);
1610}
Steve Naroffe78b8092009-03-13 16:56:44 +00001611
Douglas Gregor838db382010-02-11 01:19:42 +00001612void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001613 unsigned NParms) {
1614 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Steve Naroffe78b8092009-03-13 16:56:44 +00001616 // Zero params -> null pointer.
1617 if (NParms) {
1618 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001619 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001620 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1621 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1622 }
1623}
1624
1625unsigned BlockDecl::getNumParams() const {
1626 return NumParams;
1627}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001628
1629
1630//===----------------------------------------------------------------------===//
1631// Other Decl Allocation/Deallocation Method Implementations
1632//===----------------------------------------------------------------------===//
1633
1634TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1635 return new (C) TranslationUnitDecl(C);
1636}
1637
1638NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1639 SourceLocation L, IdentifierInfo *Id) {
1640 return new (C) NamespaceDecl(DC, L, Id);
1641}
1642
1643void NamespaceDecl::Destroy(ASTContext& C) {
1644 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1645 // together. They are all top-level Decls.
1646
1647 this->~NamespaceDecl();
John McCallb6217662010-03-15 10:12:16 +00001648 Decl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001649}
1650
1651
1652ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1653 SourceLocation L, IdentifierInfo *Id, QualType T) {
1654 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1655}
1656
1657FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1658 SourceLocation L,
1659 DeclarationName N, QualType T,
1660 TypeSourceInfo *TInfo,
1661 StorageClass S, bool isInline,
1662 bool hasWrittenPrototype) {
1663 FunctionDecl *New
1664 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
1665 New->HasWrittenPrototype = hasWrittenPrototype;
1666 return New;
1667}
1668
1669BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1670 return new (C) BlockDecl(DC, L);
1671}
1672
1673EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1674 SourceLocation L,
1675 IdentifierInfo *Id, QualType T,
1676 Expr *E, const llvm::APSInt &V) {
1677 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1678}
1679
1680void EnumConstantDecl::Destroy(ASTContext& C) {
1681 if (Init) Init->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001682 ValueDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001683}
1684
1685TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1686 SourceLocation L, IdentifierInfo *Id,
1687 TypeSourceInfo *TInfo) {
1688 return new (C) TypedefDecl(DC, L, Id, TInfo);
1689}
1690
1691// Anchor TypedefDecl's vtable here.
1692TypedefDecl::~TypedefDecl() {}
1693
1694FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1695 SourceLocation L,
1696 StringLiteral *Str) {
1697 return new (C) FileScopeAsmDecl(DC, L, Str);
1698}