blob: 23f5fba437a53f7d80cc5dd0e53cff0cf4b8388b [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"
John McCall06f6fe8d2009-09-04 01:14:41 +000026#include "clang/Parse/DeclSpec.h"
27#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2ada0482009-02-04 17:27:36 +000028#include <vector>
Ted Kremenekce20e8f2008-05-20 00:43:19 +000029
Chris Lattner6d9a6852006-10-25 05:11:20 +000030using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000031
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000032/// \brief Return the TypeLoc wrapper for the type source info.
John McCallbcd03502009-12-07 02:54:59 +000033TypeLoc TypeSourceInfo::getTypeLoc() const {
Argyrios Kyrtzidis1b7c4ca2009-09-29 19:40:20 +000034 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000035}
Chris Lattner9631e182009-03-04 06:34:08 +000036
Chris Lattner88f70d62008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000038// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000039//===----------------------------------------------------------------------===//
40
Douglas Gregor7dc5c172010-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 Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000120 return InternalLinkage;
Douglas Gregorf73b2822009-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 Friedmanf873c2f2009-11-26 03:04:01 +0000127 Var->getType().isConstant(Context) &&
Douglas Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000134 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000135 FoundExtern = true;
136
137 if (!FoundExtern)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000138 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000139 }
140 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-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 Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000153 return InternalLinkage;
Douglas Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000157 return InternalLinkage;
Douglas Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000180 if (Linkage L = PrevVar->getLinkage())
Douglas Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000189 if (Var->isInAnonymousNamespace())
190 return UniqueExternalLinkage;
191
192 return ExternalLinkage;
Douglas Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000216 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000217 return L;
218 }
219 }
220
Douglas Gregor7dc5c172010-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 Gregorf73b2822009-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 Gregor7dc5c172010-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 Gregorf73b2822009-11-25 22:24:25 +0000262
263 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor7dc5c172010-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 Gregorf73b2822009-11-25 22:24:25 +0000269
270 // - a template, unless it is a function template that has
271 // internal linkage (Clause 14);
Douglas Gregor7dc5c172010-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 Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000283 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000284
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000285 return NoLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000286}
287
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000288Linkage NamedDecl::getLinkage() const {
Douglas Gregorf73b2822009-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 Gregor7dc5c172010-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 Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000327 if (Function->isInAnonymousNamespace())
328 return UniqueExternalLinkage;
329
Douglas Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000340 if (Var->isInAnonymousNamespace())
341 return UniqueExternalLinkage;
342
Douglas Gregorf73b2822009-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 Gregor7dc5c172010-02-03 09:33:45 +0000350 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000351
Douglas Gregor2ada0482009-02-04 17:27:36 +0000352std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000353 return getQualifiedNameAsString(getASTContext().getLangOptions());
354}
355
356std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000357 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
358 // std::string thrashing.
Douglas Gregor2ada0482009-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 Stump11289f42009-09-09 15:08:12 +0000367 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-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 Gregor7de59662009-05-29 20:38:28 +0000373 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000374 P);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000375 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig07d211e2009-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 Weinigb999f682009-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 Gregor85673582009-05-18 17:01:57 +0000418 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-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
433 QualName += getNameAsString();
434
435 return QualName;
436}
437
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000438bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000439 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
440
Douglas Gregor889ceb72009-02-03 19:21:40 +0000441 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
442 // We want to keep it, unless it nominates same namespace.
443 if (getKind() == Decl::UsingDirective) {
444 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
445 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000448 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
449 // For function declarations, we keep track of redeclarations.
450 return FD->getPreviousDeclaration() == OldD;
451
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000452 // For function templates, the underlying function declarations are linked.
453 if (const FunctionTemplateDecl *FunctionTemplate
454 = dyn_cast<FunctionTemplateDecl>(this))
455 if (const FunctionTemplateDecl *OldFunctionTemplate
456 = dyn_cast<FunctionTemplateDecl>(OldD))
457 return FunctionTemplate->getTemplatedDecl()
458 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000459
Steve Naroffc4173fa2009-02-22 19:35:57 +0000460 // For method declarations, we keep track of redeclarations.
461 if (isa<ObjCMethodDecl>(this))
462 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000463
John McCall9f3059a2009-10-09 21:13:30 +0000464 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
465 return true;
466
John McCall3f746822009-11-17 05:59:44 +0000467 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
468 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
469 cast<UsingShadowDecl>(OldD)->getTargetDecl();
470
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000471 // For non-function declarations, if the declarations are of the
472 // same kind then this must be a redeclaration, or semantic analysis
473 // would not have given us the new declaration.
474 return this->getKind() == OldD->getKind();
475}
476
Douglas Gregoreddf4332009-02-24 20:03:32 +0000477bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000478 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000479}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000480
Anders Carlsson6915bf62009-06-26 06:29:23 +0000481NamedDecl *NamedDecl::getUnderlyingDecl() {
482 NamedDecl *ND = this;
483 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000484 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000485 ND = UD->getTargetDecl();
486 else if (ObjCCompatibleAliasDecl *AD
487 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
488 return AD->getClassInterface();
489 else
490 return ND;
491 }
492}
493
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000494//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000495// DeclaratorDecl Implementation
496//===----------------------------------------------------------------------===//
497
498SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-10-18 01:05:36 +0000499 if (DeclInfo) {
500 TypeLoc TL = DeclInfo->getTypeLoc();
501 while (true) {
502 TypeLoc NextTL = TL.getNextTypeLoc();
503 if (!NextTL)
504 return TL.getSourceRange().getBegin();
505 TL = NextTL;
506 }
507 }
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000508 return SourceLocation();
509}
510
511//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000512// VarDecl Implementation
513//===----------------------------------------------------------------------===//
514
Sebastian Redl833ef452010-01-26 22:01:41 +0000515const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
516 switch (SC) {
517 case VarDecl::None: break;
518 case VarDecl::Auto: return "auto"; break;
519 case VarDecl::Extern: return "extern"; break;
520 case VarDecl::PrivateExtern: return "__private_extern__"; break;
521 case VarDecl::Register: return "register"; break;
522 case VarDecl::Static: return "static"; break;
523 }
524
525 assert(0 && "Invalid storage class");
526 return 0;
527}
528
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000529VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000530 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000531 StorageClass S) {
John McCallbcd03502009-12-07 02:54:59 +0000532 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000533}
534
535void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000536 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000537 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000538 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000539 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
540 Eval->~EvaluatedStmt();
541 C.Deallocate(Eval);
542 }
543 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000544 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000545 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000546}
547
548VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000549}
550
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000551SourceRange VarDecl::getSourceRange() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000552 SourceLocation Start = getTypeSpecStartLoc();
553 if (Start.isInvalid())
554 Start = getLocation();
555
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000556 if (getInit())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000557 return SourceRange(Start, getInit()->getLocEnd());
558 return SourceRange(Start, getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000559}
560
Sebastian Redl833ef452010-01-26 22:01:41 +0000561bool VarDecl::isExternC() const {
562 ASTContext &Context = getASTContext();
563 if (!Context.getLangOptions().CPlusPlus)
564 return (getDeclContext()->isTranslationUnit() &&
565 getStorageClass() != Static) ||
566 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
567
568 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
569 DC = DC->getParent()) {
570 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
571 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
572 return getStorageClass() != Static;
573
574 break;
575 }
576
577 if (DC->isFunctionOrMethod())
578 return false;
579 }
580
581 return false;
582}
583
584VarDecl *VarDecl::getCanonicalDecl() {
585 return getFirstDeclaration();
586}
587
Sebastian Redl35351a92010-01-31 22:27:38 +0000588VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
589 // C++ [basic.def]p2:
590 // A declaration is a definition unless [...] it contains the 'extern'
591 // specifier or a linkage-specification and neither an initializer [...],
592 // it declares a static data member in a class declaration [...].
593 // C++ [temp.expl.spec]p15:
594 // An explicit specialization of a static data member of a template is a
595 // definition if the declaration includes an initializer; otherwise, it is
596 // a declaration.
597 if (isStaticDataMember()) {
598 if (isOutOfLine() && (hasInit() ||
599 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
600 return Definition;
601 else
602 return DeclarationOnly;
603 }
604 // C99 6.7p5:
605 // A definition of an identifier is a declaration for that identifier that
606 // [...] causes storage to be reserved for that object.
607 // Note: that applies for all non-file-scope objects.
608 // C99 6.9.2p1:
609 // If the declaration of an identifier for an object has file scope and an
610 // initializer, the declaration is an external definition for the identifier
611 if (hasInit())
612 return Definition;
613 // AST for 'extern "C" int foo;' is annotated with 'extern'.
614 if (hasExternalStorage())
615 return DeclarationOnly;
616
617 // C99 6.9.2p2:
618 // A declaration of an object that has file scope without an initializer,
619 // and without a storage class specifier or the scs 'static', constitutes
620 // a tentative definition.
621 // No such thing in C++.
622 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
623 return TentativeDefinition;
624
625 // What's left is (in C, block-scope) declarations without initializers or
626 // external storage. These are definitions.
627 return Definition;
628}
629
Sebastian Redl35351a92010-01-31 22:27:38 +0000630VarDecl *VarDecl::getActingDefinition() {
631 DefinitionKind Kind = isThisDeclarationADefinition();
632 if (Kind != TentativeDefinition)
633 return 0;
634
635 VarDecl *LastTentative = false;
636 VarDecl *First = getFirstDeclaration();
637 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
638 I != E; ++I) {
639 Kind = (*I)->isThisDeclarationADefinition();
640 if (Kind == Definition)
641 return 0;
642 else if (Kind == TentativeDefinition)
643 LastTentative = *I;
644 }
645 return LastTentative;
646}
647
648bool VarDecl::isTentativeDefinitionNow() const {
649 DefinitionKind Kind = isThisDeclarationADefinition();
650 if (Kind != TentativeDefinition)
651 return false;
652
653 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
654 if ((*I)->isThisDeclarationADefinition() == Definition)
655 return false;
656 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000657 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000658}
659
Sebastian Redl5ca79842010-02-01 20:16:42 +0000660VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000661 VarDecl *First = getFirstDeclaration();
662 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
663 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000664 if ((*I)->isThisDeclarationADefinition() == Definition)
665 return *I;
666 }
667 return 0;
668}
669
670const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000671 redecl_iterator I = redecls_begin(), E = redecls_end();
672 while (I != E && !I->getInit())
673 ++I;
674
675 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000676 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000677 return I->getInit();
678 }
679 return 0;
680}
681
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000682bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000683 if (Decl::isOutOfLine())
684 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000685
686 if (!isStaticDataMember())
687 return false;
688
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000689 // If this static data member was instantiated from a static data member of
690 // a class template, check whether that static data member was defined
691 // out-of-line.
692 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
693 return VD->isOutOfLine();
694
695 return false;
696}
697
Douglas Gregor1d957a32009-10-27 18:42:08 +0000698VarDecl *VarDecl::getOutOfLineDefinition() {
699 if (!isStaticDataMember())
700 return 0;
701
702 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
703 RD != RDEnd; ++RD) {
704 if (RD->getLexicalDeclContext()->isFileContext())
705 return *RD;
706 }
707
708 return 0;
709}
710
Douglas Gregord5058122010-02-11 01:19:42 +0000711void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000712 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
713 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000714 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000715 }
716
717 Init = I;
718}
719
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000720VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000721 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000722 return cast<VarDecl>(MSI->getInstantiatedFrom());
723
724 return 0;
725}
726
Douglas Gregor3c74d412009-10-14 20:14:33 +0000727TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000728 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000729 return MSI->getTemplateSpecializationKind();
730
731 return TSK_Undeclared;
732}
733
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000734MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000735 return getASTContext().getInstantiatedFromStaticDataMember(this);
736}
737
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000738void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
739 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000740 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000741 assert(MSI && "Not an instantiated static data member?");
742 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000743 if (TSK != TSK_ExplicitSpecialization &&
744 PointOfInstantiation.isValid() &&
745 MSI->getPointOfInstantiation().isInvalid())
746 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000747}
748
Sebastian Redl833ef452010-01-26 22:01:41 +0000749//===----------------------------------------------------------------------===//
750// ParmVarDecl Implementation
751//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000752
Sebastian Redl833ef452010-01-26 22:01:41 +0000753ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
754 SourceLocation L, IdentifierInfo *Id,
755 QualType T, TypeSourceInfo *TInfo,
756 StorageClass S, Expr *DefArg) {
757 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000758}
759
Sebastian Redl833ef452010-01-26 22:01:41 +0000760Expr *ParmVarDecl::getDefaultArg() {
761 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
762 assert(!hasUninstantiatedDefaultArg() &&
763 "Default argument is not yet instantiated!");
764
765 Expr *Arg = getInit();
766 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
767 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000768
Sebastian Redl833ef452010-01-26 22:01:41 +0000769 return Arg;
770}
771
772unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
773 if (const CXXExprWithTemporaries *E =
774 dyn_cast<CXXExprWithTemporaries>(getInit()))
775 return E->getNumTemporaries();
776
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000777 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000778}
779
Sebastian Redl833ef452010-01-26 22:01:41 +0000780CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
781 assert(getNumDefaultArgTemporaries() &&
782 "Default arguments does not have any temporaries!");
783
784 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
785 return E->getTemporary(i);
786}
787
788SourceRange ParmVarDecl::getDefaultArgRange() const {
789 if (const Expr *E = getInit())
790 return E->getSourceRange();
791
792 if (hasUninstantiatedDefaultArg())
793 return getUninstantiatedDefaultArg()->getSourceRange();
794
795 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000796}
797
Nuno Lopes394ec982008-12-17 23:39:55 +0000798//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000799// FunctionDecl Implementation
800//===----------------------------------------------------------------------===//
801
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000802void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000803 if (Body && Body.isOffset())
804 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000805
806 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
807 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000808
Douglas Gregord801b062009-10-07 23:56:10 +0000809 FunctionTemplateSpecializationInfo *FTSInfo
810 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
811 if (FTSInfo)
812 C.Deallocate(FTSInfo);
813
814 MemberSpecializationInfo *MSInfo
815 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
816 if (MSInfo)
817 C.Deallocate(MSInfo);
818
Steve Naroff13ae6f42009-01-27 21:25:57 +0000819 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000820
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000821 Decl::Destroy(C);
822}
823
John McCalle1f2ec22009-09-11 06:45:03 +0000824void FunctionDecl::getNameForDiagnostic(std::string &S,
825 const PrintingPolicy &Policy,
826 bool Qualified) const {
827 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
828 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
829 if (TemplateArgs)
830 S += TemplateSpecializationType::PrintTemplateArgumentList(
831 TemplateArgs->getFlatArgumentList(),
832 TemplateArgs->flat_size(),
833 Policy);
834
835}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000836
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000837Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000838 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
839 if (I->Body) {
840 Definition = *I;
841 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000842 }
843 }
844
845 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000846}
847
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000848void FunctionDecl::setBody(Stmt *B) {
849 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000850 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000851 EndRangeLoc = B->getLocEnd();
852}
853
Douglas Gregor16618f22009-09-12 00:17:51 +0000854bool FunctionDecl::isMain() const {
855 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000856 return !Context.getLangOptions().Freestanding &&
857 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000858 getIdentifier() && getIdentifier()->isStr("main");
859}
860
Douglas Gregor16618f22009-09-12 00:17:51 +0000861bool FunctionDecl::isExternC() const {
862 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000863 // In C, any non-static, non-overloadable function has external
864 // linkage.
865 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000866 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000867
Mike Stump11289f42009-09-09 15:08:12 +0000868 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000869 DC = DC->getParent()) {
870 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
871 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000872 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000873 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000874
875 break;
876 }
877 }
878
879 return false;
880}
881
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000882bool FunctionDecl::isGlobal() const {
883 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
884 return Method->isStatic();
885
886 if (getStorageClass() == Static)
887 return false;
888
Mike Stump11289f42009-09-09 15:08:12 +0000889 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000890 DC->isNamespace();
891 DC = DC->getParent()) {
892 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
893 if (!Namespace->getDeclName())
894 return false;
895 break;
896 }
897 }
898
899 return true;
900}
901
Sebastian Redl833ef452010-01-26 22:01:41 +0000902void
903FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
904 redeclarable_base::setPreviousDeclaration(PrevDecl);
905
906 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
907 FunctionTemplateDecl *PrevFunTmpl
908 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
909 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
910 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
911 }
912}
913
914const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
915 return getFirstDeclaration();
916}
917
918FunctionDecl *FunctionDecl::getCanonicalDecl() {
919 return getFirstDeclaration();
920}
921
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000922/// \brief Returns a value indicating whether this function
923/// corresponds to a builtin function.
924///
925/// The function corresponds to a built-in function if it is
926/// declared at translation scope or within an extern "C" block and
927/// its name matches with the name of a builtin. The returned value
928/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000929/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000930/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000931unsigned FunctionDecl::getBuiltinID() const {
932 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000933 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
934 return 0;
935
936 unsigned BuiltinID = getIdentifier()->getBuiltinID();
937 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
938 return BuiltinID;
939
940 // This function has the name of a known C library
941 // function. Determine whether it actually refers to the C library
942 // function or whether it just has the same name.
943
Douglas Gregora908e7f2009-02-17 03:23:10 +0000944 // If this is a static function, it's not a builtin.
945 if (getStorageClass() == Static)
946 return 0;
947
Douglas Gregore711f702009-02-14 18:57:46 +0000948 // If this function is at translation-unit scope and we're not in
949 // C++, it refers to the C library function.
950 if (!Context.getLangOptions().CPlusPlus &&
951 getDeclContext()->isTranslationUnit())
952 return BuiltinID;
953
954 // If the function is in an extern "C" linkage specification and is
955 // not marked "overloadable", it's the real function.
956 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000957 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000958 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000959 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000960 return BuiltinID;
961
962 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000963 return 0;
964}
965
966
Chris Lattner47c0d002009-04-25 06:03:53 +0000967/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000968/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000969/// after it has been created.
970unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000971 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000972 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000973 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000974 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000975
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000976}
977
Douglas Gregord5058122010-02-11 01:19:42 +0000978void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000979 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000980 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000981
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000982 // Zero params -> null pointer.
983 if (NumParams) {
Douglas Gregord5058122010-02-11 01:19:42 +0000984 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000985 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000986 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000987
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000988 // Update source range. The check below allows us to set EndRangeLoc before
989 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000990 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000991 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000992 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000993}
Chris Lattner41943152007-01-25 04:52:46 +0000994
Chris Lattner58258242008-04-10 02:22:51 +0000995/// getMinRequiredArguments - Returns the minimum number of arguments
996/// needed to call this function. This may be fewer than the number of
997/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000998/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000999unsigned FunctionDecl::getMinRequiredArguments() const {
1000 unsigned NumRequiredArgs = getNumParams();
1001 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001002 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001003 --NumRequiredArgs;
1004
1005 return NumRequiredArgs;
1006}
1007
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001008bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001009 // FIXME: This is not enough. Consider:
1010 //
1011 // inline void f();
1012 // void f() { }
1013 //
1014 // f is inlined, but does not have inline specified.
1015 // To fix this we should add an 'inline' flag to FunctionDecl.
1016 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001017 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001018
1019 if (isa<CXXMethodDecl>(this)) {
1020 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1021 return true;
1022 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001023
1024 switch (getTemplateSpecializationKind()) {
1025 case TSK_Undeclared:
1026 case TSK_ExplicitSpecialization:
1027 return false;
1028
1029 case TSK_ImplicitInstantiation:
1030 case TSK_ExplicitInstantiationDeclaration:
1031 case TSK_ExplicitInstantiationDefinition:
1032 // Handle below.
1033 break;
1034 }
1035
1036 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1037 Stmt *Pattern = 0;
1038 if (PatternDecl)
1039 Pattern = PatternDecl->getBody(PatternDecl);
1040
1041 if (Pattern && PatternDecl)
1042 return PatternDecl->isInlined();
1043
1044 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001045}
1046
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001047/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001048/// definition will be externally visible.
1049///
1050/// Inline function definitions are always available for inlining optimizations.
1051/// However, depending on the language dialect, declaration specifiers, and
1052/// attributes, the definition of an inline function may or may not be
1053/// "externally" visible to other translation units in the program.
1054///
1055/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001056/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001057/// inline definition becomes externally visible (C99 6.7.4p6).
1058///
1059/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1060/// definition, we use the GNU semantics for inline, which are nearly the
1061/// opposite of C99 semantics. In particular, "inline" by itself will create
1062/// an externally visible symbol, but "extern inline" will not create an
1063/// externally visible symbol.
1064bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1065 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001066 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001067 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001068
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001069 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001070 // GNU inline semantics. Based on a number of examples, we came up with the
1071 // following heuristic: if the "inline" keyword is present on a
1072 // declaration of the function but "extern" is not present on that
1073 // declaration, then the symbol is externally visible. Otherwise, the GNU
1074 // "extern inline" semantics applies and the symbol is not externally
1075 // visible.
1076 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1077 Redecl != RedeclEnd;
1078 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +00001079 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001080 return true;
1081 }
1082
1083 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001084 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001085 }
1086
1087 // C99 6.7.4p6:
1088 // [...] If all of the file scope declarations for a function in a
1089 // translation unit include the inline function specifier without extern,
1090 // then the definition in that translation unit is an inline definition.
1091 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1092 Redecl != RedeclEnd;
1093 ++Redecl) {
1094 // Only consider file-scope declarations in this test.
1095 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1096 continue;
1097
Douglas Gregor35b57532009-10-27 21:01:01 +00001098 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001099 return true; // Not an inline definition
1100 }
1101
1102 // C99 6.7.4p6:
1103 // An inline definition does not provide an external definition for the
1104 // function, and does not forbid an external definition in another
1105 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001106 return false;
1107}
1108
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001109/// getOverloadedOperator - Which C++ overloaded operator this
1110/// function represents, if any.
1111OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001112 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1113 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001114 else
1115 return OO_None;
1116}
1117
Alexis Huntc88db062010-01-13 09:01:02 +00001118/// getLiteralIdentifier - The literal suffix identifier this function
1119/// represents, if any.
1120const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1121 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1122 return getDeclName().getCXXLiteralIdentifier();
1123 else
1124 return 0;
1125}
1126
Douglas Gregord801b062009-10-07 23:56:10 +00001127FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001128 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001129 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1130
1131 return 0;
1132}
1133
Douglas Gregor06db9f52009-10-12 20:18:28 +00001134MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1135 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1136}
1137
Douglas Gregord801b062009-10-07 23:56:10 +00001138void
1139FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1140 TemplateSpecializationKind TSK) {
1141 assert(TemplateOrSpecialization.isNull() &&
1142 "Member function is already a specialization");
1143 MemberSpecializationInfo *Info
1144 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1145 TemplateOrSpecialization = Info;
1146}
1147
Douglas Gregorafca3b42009-10-27 20:53:28 +00001148bool FunctionDecl::isImplicitlyInstantiable() const {
1149 // If this function already has a definition or is invalid, it can't be
1150 // implicitly instantiated.
1151 if (isInvalidDecl() || getBody())
1152 return false;
1153
1154 switch (getTemplateSpecializationKind()) {
1155 case TSK_Undeclared:
1156 case TSK_ExplicitSpecialization:
1157 case TSK_ExplicitInstantiationDefinition:
1158 return false;
1159
1160 case TSK_ImplicitInstantiation:
1161 return true;
1162
1163 case TSK_ExplicitInstantiationDeclaration:
1164 // Handled below.
1165 break;
1166 }
1167
1168 // Find the actual template from which we will instantiate.
1169 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1170 Stmt *Pattern = 0;
1171 if (PatternDecl)
1172 Pattern = PatternDecl->getBody(PatternDecl);
1173
1174 // C++0x [temp.explicit]p9:
1175 // Except for inline functions, other explicit instantiation declarations
1176 // have the effect of suppressing the implicit instantiation of the entity
1177 // to which they refer.
1178 if (!Pattern || !PatternDecl)
1179 return true;
1180
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001181 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001182}
1183
1184FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1185 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1186 while (Primary->getInstantiatedFromMemberTemplate()) {
1187 // If we have hit a point where the user provided a specialization of
1188 // this template, we're done looking.
1189 if (Primary->isMemberSpecialization())
1190 break;
1191
1192 Primary = Primary->getInstantiatedFromMemberTemplate();
1193 }
1194
1195 return Primary->getTemplatedDecl();
1196 }
1197
1198 return getInstantiatedFromMemberFunction();
1199}
1200
Douglas Gregor70d83e22009-06-29 17:30:29 +00001201FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001202 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001203 = TemplateOrSpecialization
1204 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001205 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001206 }
1207 return 0;
1208}
1209
1210const TemplateArgumentList *
1211FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001212 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001213 = TemplateOrSpecialization
1214 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001215 return Info->TemplateArguments;
1216 }
1217 return 0;
1218}
1219
Mike Stump11289f42009-09-09 15:08:12 +00001220void
Douglas Gregord5058122010-02-11 01:19:42 +00001221FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001222 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001223 void *InsertPos,
1224 TemplateSpecializationKind TSK) {
1225 assert(TSK != TSK_Undeclared &&
1226 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001227 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001228 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001229 if (!Info)
Douglas Gregord5058122010-02-11 01:19:42 +00001230 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001231
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001232 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001233 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001234 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001235 Info->TemplateArguments = TemplateArgs;
1236 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001237
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001238 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001239 // function template specializations.
1240 if (InsertPos)
1241 Template->getSpecializations().InsertNode(Info, InsertPos);
1242 else {
1243 // Try to insert the new node. If there is an existing node, remove it
1244 // first.
1245 FunctionTemplateSpecializationInfo *Existing
1246 = Template->getSpecializations().GetOrInsertNode(Info);
1247 if (Existing) {
1248 Template->getSpecializations().RemoveNode(Existing);
1249 Template->getSpecializations().GetOrInsertNode(Info);
1250 }
1251 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001252}
1253
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001254TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001255 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001256 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001257 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001258 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001259 if (FTSInfo)
1260 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001261
Douglas Gregord801b062009-10-07 23:56:10 +00001262 MemberSpecializationInfo *MSInfo
1263 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1264 if (MSInfo)
1265 return MSInfo->getTemplateSpecializationKind();
1266
1267 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001268}
1269
Mike Stump11289f42009-09-09 15:08:12 +00001270void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001271FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1272 SourceLocation PointOfInstantiation) {
1273 if (FunctionTemplateSpecializationInfo *FTSInfo
1274 = TemplateOrSpecialization.dyn_cast<
1275 FunctionTemplateSpecializationInfo*>()) {
1276 FTSInfo->setTemplateSpecializationKind(TSK);
1277 if (TSK != TSK_ExplicitSpecialization &&
1278 PointOfInstantiation.isValid() &&
1279 FTSInfo->getPointOfInstantiation().isInvalid())
1280 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1281 } else if (MemberSpecializationInfo *MSInfo
1282 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1283 MSInfo->setTemplateSpecializationKind(TSK);
1284 if (TSK != TSK_ExplicitSpecialization &&
1285 PointOfInstantiation.isValid() &&
1286 MSInfo->getPointOfInstantiation().isInvalid())
1287 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1288 } else
1289 assert(false && "Function cannot have a template specialization kind");
1290}
1291
1292SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001293 if (FunctionTemplateSpecializationInfo *FTSInfo
1294 = TemplateOrSpecialization.dyn_cast<
1295 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001296 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001297 else if (MemberSpecializationInfo *MSInfo
1298 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001299 return MSInfo->getPointOfInstantiation();
1300
1301 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001302}
1303
Douglas Gregor6411b922009-09-11 20:15:17 +00001304bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001305 if (Decl::isOutOfLine())
1306 return true;
1307
1308 // If this function was instantiated from a member function of a
1309 // class template, check whether that member function was defined out-of-line.
1310 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1311 const FunctionDecl *Definition;
1312 if (FD->getBody(Definition))
1313 return Definition->isOutOfLine();
1314 }
1315
1316 // If this function was instantiated from a function template,
1317 // check whether that function template was defined out-of-line.
1318 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1319 const FunctionDecl *Definition;
1320 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1321 return Definition->isOutOfLine();
1322 }
1323
1324 return false;
1325}
1326
Chris Lattner59a25942008-03-31 00:36:02 +00001327//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001328// FieldDecl Implementation
1329//===----------------------------------------------------------------------===//
1330
1331FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1332 IdentifierInfo *Id, QualType T,
1333 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1334 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1335}
1336
1337bool FieldDecl::isAnonymousStructOrUnion() const {
1338 if (!isImplicit() || getDeclName())
1339 return false;
1340
1341 if (const RecordType *Record = getType()->getAs<RecordType>())
1342 return Record->getDecl()->isAnonymousStructOrUnion();
1343
1344 return false;
1345}
1346
1347//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001348// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001349//===----------------------------------------------------------------------===//
1350
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001351SourceRange TagDecl::getSourceRange() const {
1352 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001353 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001354}
1355
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001356TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001357 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001358}
1359
Douglas Gregordee1be82009-01-17 00:42:38 +00001360void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001361 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1362 TagT->decl.setPointer(this);
1363 TagT->decl.setInt(1);
1364 }
John McCall67da35c2010-02-04 22:26:26 +00001365
1366 if (isa<CXXRecordDecl>(this)) {
1367 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1368 struct CXXRecordDecl::DefinitionData *Data =
1369 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
1370 do {
1371 D->DefinitionData = Data;
1372 D = cast_or_null<CXXRecordDecl>(D->getPreviousDeclaration());
1373 } while (D);
1374 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001375}
1376
1377void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001378 assert((!isa<CXXRecordDecl>(this) ||
1379 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1380 "definition completed but not started");
1381
Douglas Gregordee1be82009-01-17 00:42:38 +00001382 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001383 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1384 assert(TagT->decl.getPointer() == this &&
1385 "Attempt to redefine a tag definition?");
1386 TagT->decl.setInt(0);
1387 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001388}
1389
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001390TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001391 if (isDefinition())
1392 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001393
1394 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001395 R != REnd; ++R)
1396 if (R->isDefinition())
1397 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001398
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001399 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001400}
1401
John McCall06f6fe8d2009-09-04 01:14:41 +00001402TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1403 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001404 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001405 case DeclSpec::TST_struct: return TK_struct;
1406 case DeclSpec::TST_class: return TK_class;
1407 case DeclSpec::TST_union: return TK_union;
1408 case DeclSpec::TST_enum: return TK_enum;
1409 }
1410}
1411
Ted Kremenek21475702008-09-05 17:16:31 +00001412//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001413// EnumDecl Implementation
1414//===----------------------------------------------------------------------===//
1415
1416EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1417 IdentifierInfo *Id, SourceLocation TKL,
1418 EnumDecl *PrevDecl) {
1419 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1420 C.getTypeDeclType(Enum, PrevDecl);
1421 return Enum;
1422}
1423
1424void EnumDecl::Destroy(ASTContext& C) {
1425 Decl::Destroy(C);
1426}
1427
Douglas Gregord5058122010-02-11 01:19:42 +00001428void EnumDecl::completeDefinition(QualType NewType,
Sebastian Redl833ef452010-01-26 22:01:41 +00001429 QualType NewPromotionType) {
1430 assert(!isDefinition() && "Cannot redefine enums!");
1431 IntegerType = NewType;
1432 PromotionType = NewPromotionType;
1433 TagDecl::completeDefinition();
1434}
1435
1436//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001437// RecordDecl Implementation
1438//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001439
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001440RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001441 IdentifierInfo *Id, RecordDecl *PrevDecl,
1442 SourceLocation TKL)
1443 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001444 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001445 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001446 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001447 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001448}
1449
1450RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001451 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001452 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001453
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001454 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001455 C.getTypeDeclType(R, PrevDecl);
1456 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001457}
1458
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001459RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001460}
1461
1462void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001463 TagDecl::Destroy(C);
1464}
1465
Douglas Gregordfcad112009-03-25 15:59:44 +00001466bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001467 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001468 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1469}
1470
Douglas Gregor91f84212008-12-11 16:49:14 +00001471/// completeDefinition - Notes that the definition of this type is now
1472/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001473void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001474 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001475 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001476}
Steve Naroffcc321422007-03-26 23:09:51 +00001477
Steve Naroff415d3d52008-10-08 17:01:13 +00001478//===----------------------------------------------------------------------===//
1479// BlockDecl Implementation
1480//===----------------------------------------------------------------------===//
1481
1482BlockDecl::~BlockDecl() {
1483}
1484
1485void BlockDecl::Destroy(ASTContext& C) {
1486 if (Body)
1487 Body->Destroy(C);
1488
1489 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1490 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001491
1492 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001493 Decl::Destroy(C);
1494}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001495
Douglas Gregord5058122010-02-11 01:19:42 +00001496void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001497 unsigned NParms) {
1498 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001499
Steve Naroffc4b30e52009-03-13 16:56:44 +00001500 // Zero params -> null pointer.
1501 if (NParms) {
1502 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001503 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001504 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1505 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1506 }
1507}
1508
1509unsigned BlockDecl::getNumParams() const {
1510 return NumParams;
1511}
Sebastian Redl833ef452010-01-26 22:01:41 +00001512
1513
1514//===----------------------------------------------------------------------===//
1515// Other Decl Allocation/Deallocation Method Implementations
1516//===----------------------------------------------------------------------===//
1517
1518TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1519 return new (C) TranslationUnitDecl(C);
1520}
1521
1522NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1523 SourceLocation L, IdentifierInfo *Id) {
1524 return new (C) NamespaceDecl(DC, L, Id);
1525}
1526
1527void NamespaceDecl::Destroy(ASTContext& C) {
1528 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1529 // together. They are all top-level Decls.
1530
1531 this->~NamespaceDecl();
1532 C.Deallocate((void *)this);
1533}
1534
1535
1536ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1537 SourceLocation L, IdentifierInfo *Id, QualType T) {
1538 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1539}
1540
1541FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1542 SourceLocation L,
1543 DeclarationName N, QualType T,
1544 TypeSourceInfo *TInfo,
1545 StorageClass S, bool isInline,
1546 bool hasWrittenPrototype) {
1547 FunctionDecl *New
1548 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
1549 New->HasWrittenPrototype = hasWrittenPrototype;
1550 return New;
1551}
1552
1553BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1554 return new (C) BlockDecl(DC, L);
1555}
1556
1557EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1558 SourceLocation L,
1559 IdentifierInfo *Id, QualType T,
1560 Expr *E, const llvm::APSInt &V) {
1561 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1562}
1563
1564void EnumConstantDecl::Destroy(ASTContext& C) {
1565 if (Init) Init->Destroy(C);
1566 Decl::Destroy(C);
1567}
1568
1569TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1570 SourceLocation L, IdentifierInfo *Id,
1571 TypeSourceInfo *TInfo) {
1572 return new (C) TypedefDecl(DC, L, Id, TInfo);
1573}
1574
1575// Anchor TypedefDecl's vtable here.
1576TypedefDecl::~TypedefDecl() {}
1577
1578FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1579 SourceLocation L,
1580 StringLiteral *Str) {
1581 return new (C) FileScopeAsmDecl(DC, L, Str);
1582}