Deleted Added
full compact
Decl.cpp (200583) Decl.cpp (201361)
1//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/TypeLoc.h"
20#include "clang/AST/Stmt.h"
21#include "clang/AST/Expr.h"
1//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/TypeLoc.h"
20#include "clang/AST/Stmt.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
22#include "clang/AST/PrettyPrinter.h"
23#include "clang/Basic/Builtins.h"
24#include "clang/Basic/IdentifierTable.h"
25#include "clang/Parse/DeclSpec.h"
26#include "llvm/Support/ErrorHandling.h"
27#include <vector>
28
29using namespace clang;
30
31void Attr::Destroy(ASTContext &C) {
32 if (Next) {
33 Next->Destroy(C);
34 Next = 0;
35 }
36 this->~Attr();
37 C.Deallocate((void*)this);
38}
39
40/// \brief Return the TypeLoc wrapper for the type source info.
41TypeLoc TypeSourceInfo::getTypeLoc() const {
42 return TypeLoc(Ty, (void*)(this + 1));
43}
44
45//===----------------------------------------------------------------------===//
46// Decl Allocation/Deallocation Method Implementations
47//===----------------------------------------------------------------------===//
48
49
50TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
51 return new (C) TranslationUnitDecl(C);
52}
53
54NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
55 SourceLocation L, IdentifierInfo *Id) {
56 return new (C) NamespaceDecl(DC, L, Id);
57}
58
59void NamespaceDecl::Destroy(ASTContext& C) {
60 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
61 // together. They are all top-level Decls.
62
63 this->~NamespaceDecl();
64 C.Deallocate((void *)this);
65}
66
67
68ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
69 SourceLocation L, IdentifierInfo *Id, QualType T) {
70 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
71}
72
73const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
74 switch (SC) {
75 case VarDecl::None: break;
76 case VarDecl::Auto: return "auto"; break;
77 case VarDecl::Extern: return "extern"; break;
78 case VarDecl::PrivateExtern: return "__private_extern__"; break;
79 case VarDecl::Register: return "register"; break;
80 case VarDecl::Static: return "static"; break;
81 }
82
83 assert(0 && "Invalid storage class");
84 return 0;
85}
86
87ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
88 SourceLocation L, IdentifierInfo *Id,
89 QualType T, TypeSourceInfo *TInfo,
90 StorageClass S, Expr *DefArg) {
91 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
92}
93
23#include "clang/AST/PrettyPrinter.h"
24#include "clang/Basic/Builtins.h"
25#include "clang/Basic/IdentifierTable.h"
26#include "clang/Parse/DeclSpec.h"
27#include "llvm/Support/ErrorHandling.h"
28#include <vector>
29
30using namespace clang;
31
32void Attr::Destroy(ASTContext &C) {
33 if (Next) {
34 Next->Destroy(C);
35 Next = 0;
36 }
37 this->~Attr();
38 C.Deallocate((void*)this);
39}
40
41/// \brief Return the TypeLoc wrapper for the type source info.
42TypeLoc TypeSourceInfo::getTypeLoc() const {
43 return TypeLoc(Ty, (void*)(this + 1));
44}
45
46//===----------------------------------------------------------------------===//
47// Decl Allocation/Deallocation Method Implementations
48//===----------------------------------------------------------------------===//
49
50
51TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
52 return new (C) TranslationUnitDecl(C);
53}
54
55NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
56 SourceLocation L, IdentifierInfo *Id) {
57 return new (C) NamespaceDecl(DC, L, Id);
58}
59
60void NamespaceDecl::Destroy(ASTContext& C) {
61 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
62 // together. They are all top-level Decls.
63
64 this->~NamespaceDecl();
65 C.Deallocate((void *)this);
66}
67
68
69ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
70 SourceLocation L, IdentifierInfo *Id, QualType T) {
71 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
72}
73
74const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
75 switch (SC) {
76 case VarDecl::None: break;
77 case VarDecl::Auto: return "auto"; break;
78 case VarDecl::Extern: return "extern"; break;
79 case VarDecl::PrivateExtern: return "__private_extern__"; break;
80 case VarDecl::Register: return "register"; break;
81 case VarDecl::Static: return "static"; break;
82 }
83
84 assert(0 && "Invalid storage class");
85 return 0;
86}
87
88ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
89 SourceLocation L, IdentifierInfo *Id,
90 QualType T, TypeSourceInfo *TInfo,
91 StorageClass S, Expr *DefArg) {
92 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
93}
94
95Expr *ParmVarDecl::getDefaultArg() {
96 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
97 assert(!hasUninstantiatedDefaultArg() &&
98 "Default argument is not yet instantiated!");
99
100 Expr *Arg = getInit();
101 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
102 return E->getSubExpr();
103
104 return Arg;
105}
106
107unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
108 if (const CXXExprWithTemporaries *E =
109 dyn_cast<CXXExprWithTemporaries>(getInit()))
110 return E->getNumTemporaries();
111
112 return 0;
113}
114
115CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
116 assert(getNumDefaultArgTemporaries() &&
117 "Default arguments does not have any temporaries!");
118
119 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
120 return E->getTemporary(i);
121}
122
94SourceRange ParmVarDecl::getDefaultArgRange() const {
95 if (const Expr *E = getInit())
96 return E->getSourceRange();
97
98 if (hasUninstantiatedDefaultArg())
99 return getUninstantiatedDefaultArg()->getSourceRange();
100
101 return SourceRange();
102}
103
104void VarDecl::setInit(ASTContext &C, Expr *I) {
105 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
106 Eval->~EvaluatedStmt();
107 C.Deallocate(Eval);
108 }
109
110 Init = I;
111}
112
113bool VarDecl::isExternC() const {
114 ASTContext &Context = getASTContext();
115 if (!Context.getLangOptions().CPlusPlus)
116 return (getDeclContext()->isTranslationUnit() &&
117 getStorageClass() != Static) ||
118 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
119
120 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
121 DC = DC->getParent()) {
122 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
123 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
124 return getStorageClass() != Static;
125
126 break;
127 }
128
129 if (DC->isFunctionOrMethod())
130 return false;
131 }
132
133 return false;
134}
135
136FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
137 SourceLocation L,
138 DeclarationName N, QualType T,
139 TypeSourceInfo *TInfo,
140 StorageClass S, bool isInline,
141 bool hasWrittenPrototype) {
142 FunctionDecl *New
143 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
144 New->HasWrittenPrototype = hasWrittenPrototype;
145 return New;
146}
147
148BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
149 return new (C) BlockDecl(DC, L);
150}
151
152FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
153 IdentifierInfo *Id, QualType T,
154 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
155 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
156}
157
158bool FieldDecl::isAnonymousStructOrUnion() const {
159 if (!isImplicit() || getDeclName())
160 return false;
161
162 if (const RecordType *Record = getType()->getAs<RecordType>())
163 return Record->getDecl()->isAnonymousStructOrUnion();
164
165 return false;
166}
167
168EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
169 SourceLocation L,
170 IdentifierInfo *Id, QualType T,
171 Expr *E, const llvm::APSInt &V) {
172 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
173}
174
175void EnumConstantDecl::Destroy(ASTContext& C) {
176 if (Init) Init->Destroy(C);
177 Decl::Destroy(C);
178}
179
180TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
181 SourceLocation L, IdentifierInfo *Id,
182 TypeSourceInfo *TInfo) {
183 return new (C) TypedefDecl(DC, L, Id, TInfo);
184}
185
123SourceRange ParmVarDecl::getDefaultArgRange() const {
124 if (const Expr *E = getInit())
125 return E->getSourceRange();
126
127 if (hasUninstantiatedDefaultArg())
128 return getUninstantiatedDefaultArg()->getSourceRange();
129
130 return SourceRange();
131}
132
133void VarDecl::setInit(ASTContext &C, Expr *I) {
134 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
135 Eval->~EvaluatedStmt();
136 C.Deallocate(Eval);
137 }
138
139 Init = I;
140}
141
142bool VarDecl::isExternC() const {
143 ASTContext &Context = getASTContext();
144 if (!Context.getLangOptions().CPlusPlus)
145 return (getDeclContext()->isTranslationUnit() &&
146 getStorageClass() != Static) ||
147 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
148
149 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
150 DC = DC->getParent()) {
151 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
152 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
153 return getStorageClass() != Static;
154
155 break;
156 }
157
158 if (DC->isFunctionOrMethod())
159 return false;
160 }
161
162 return false;
163}
164
165FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
166 SourceLocation L,
167 DeclarationName N, QualType T,
168 TypeSourceInfo *TInfo,
169 StorageClass S, bool isInline,
170 bool hasWrittenPrototype) {
171 FunctionDecl *New
172 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
173 New->HasWrittenPrototype = hasWrittenPrototype;
174 return New;
175}
176
177BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
178 return new (C) BlockDecl(DC, L);
179}
180
181FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
182 IdentifierInfo *Id, QualType T,
183 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
184 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
185}
186
187bool FieldDecl::isAnonymousStructOrUnion() const {
188 if (!isImplicit() || getDeclName())
189 return false;
190
191 if (const RecordType *Record = getType()->getAs<RecordType>())
192 return Record->getDecl()->isAnonymousStructOrUnion();
193
194 return false;
195}
196
197EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
198 SourceLocation L,
199 IdentifierInfo *Id, QualType T,
200 Expr *E, const llvm::APSInt &V) {
201 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
202}
203
204void EnumConstantDecl::Destroy(ASTContext& C) {
205 if (Init) Init->Destroy(C);
206 Decl::Destroy(C);
207}
208
209TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
210 SourceLocation L, IdentifierInfo *Id,
211 TypeSourceInfo *TInfo) {
212 return new (C) TypedefDecl(DC, L, Id, TInfo);
213}
214
215// Anchor TypedefDecl's vtable here.
216TypedefDecl::~TypedefDecl() {}
217
186EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
187 IdentifierInfo *Id, SourceLocation TKL,
188 EnumDecl *PrevDecl) {
189 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
190 C.getTypeDeclType(Enum, PrevDecl);
191 return Enum;
192}
193
194void EnumDecl::Destroy(ASTContext& C) {
195 Decl::Destroy(C);
196}
197
198void EnumDecl::completeDefinition(ASTContext &C,
199 QualType NewType,
200 QualType NewPromotionType) {
201 assert(!isDefinition() && "Cannot redefine enums!");
202 IntegerType = NewType;
203 PromotionType = NewPromotionType;
204 TagDecl::completeDefinition();
205}
206
207FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
208 SourceLocation L,
209 StringLiteral *Str) {
210 return new (C) FileScopeAsmDecl(DC, L, Str);
211}
212
213//===----------------------------------------------------------------------===//
214// NamedDecl Implementation
215//===----------------------------------------------------------------------===//
216
217static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
218 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
219 "Not a name having namespace scope");
220 ASTContext &Context = D->getASTContext();
221
222 // C++ [basic.link]p3:
223 // A name having namespace scope (3.3.6) has internal linkage if it
224 // is the name of
225 // - an object, reference, function or function template that is
226 // explicitly declared static; or,
227 // (This bullet corresponds to C99 6.2.2p3.)
228 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
229 // Explicitly declared static.
230 if (Var->getStorageClass() == VarDecl::Static)
231 return NamedDecl::InternalLinkage;
232
233 // - an object or reference that is explicitly declared const
234 // and neither explicitly declared extern nor previously
235 // declared to have external linkage; or
236 // (there is no equivalent in C99)
237 if (Context.getLangOptions().CPlusPlus &&
238 Var->getType().isConstant(Context) &&
239 Var->getStorageClass() != VarDecl::Extern &&
240 Var->getStorageClass() != VarDecl::PrivateExtern) {
241 bool FoundExtern = false;
242 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
243 PrevVar && !FoundExtern;
244 PrevVar = PrevVar->getPreviousDeclaration())
245 if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage)
246 FoundExtern = true;
247
248 if (!FoundExtern)
249 return NamedDecl::InternalLinkage;
250 }
251 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
252 const FunctionDecl *Function = 0;
253 if (const FunctionTemplateDecl *FunTmpl
254 = dyn_cast<FunctionTemplateDecl>(D))
255 Function = FunTmpl->getTemplatedDecl();
256 else
257 Function = cast<FunctionDecl>(D);
258
259 // Explicitly declared static.
260 if (Function->getStorageClass() == FunctionDecl::Static)
261 return NamedDecl::InternalLinkage;
262 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
263 // - a data member of an anonymous union.
264 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
265 return NamedDecl::InternalLinkage;
266 }
267
268 // C++ [basic.link]p4:
269
270 // A name having namespace scope has external linkage if it is the
271 // name of
272 //
273 // - an object or reference, unless it has internal linkage; or
274 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
275 if (!Context.getLangOptions().CPlusPlus &&
276 (Var->getStorageClass() == VarDecl::Extern ||
277 Var->getStorageClass() == VarDecl::PrivateExtern)) {
278 // C99 6.2.2p4:
279 // For an identifier declared with the storage-class specifier
280 // extern in a scope in which a prior declaration of that
281 // identifier is visible, if the prior declaration specifies
282 // internal or external linkage, the linkage of the identifier
283 // at the later declaration is the same as the linkage
284 // specified at the prior declaration. If no prior declaration
285 // is visible, or if the prior declaration specifies no
286 // linkage, then the identifier has external linkage.
287 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
288 if (NamedDecl::Linkage L = PrevVar->getLinkage())
289 return L;
290 }
291 }
292
293 // C99 6.2.2p5:
294 // If the declaration of an identifier for an object has file
295 // scope and no storage-class specifier, its linkage is
296 // external.
297 return NamedDecl::ExternalLinkage;
298 }
299
300 // - a function, unless it has internal linkage; or
301 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
302 // C99 6.2.2p5:
303 // If the declaration of an identifier for a function has no
304 // storage-class specifier, its linkage is determined exactly
305 // as if it were declared with the storage-class specifier
306 // extern.
307 if (!Context.getLangOptions().CPlusPlus &&
308 (Function->getStorageClass() == FunctionDecl::Extern ||
309 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
310 Function->getStorageClass() == FunctionDecl::None)) {
311 // C99 6.2.2p4:
312 // For an identifier declared with the storage-class specifier
313 // extern in a scope in which a prior declaration of that
314 // identifier is visible, if the prior declaration specifies
315 // internal or external linkage, the linkage of the identifier
316 // at the later declaration is the same as the linkage
317 // specified at the prior declaration. If no prior declaration
318 // is visible, or if the prior declaration specifies no
319 // linkage, then the identifier has external linkage.
320 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
321 if (NamedDecl::Linkage L = PrevFunc->getLinkage())
322 return L;
323 }
324 }
325
326 return NamedDecl::ExternalLinkage;
327 }
328
329 // - a named class (Clause 9), or an unnamed class defined in a
330 // typedef declaration in which the class has the typedef name
331 // for linkage purposes (7.1.3); or
332 // - a named enumeration (7.2), or an unnamed enumeration
333 // defined in a typedef declaration in which the enumeration
334 // has the typedef name for linkage purposes (7.1.3); or
335 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
336 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl())
337 return NamedDecl::ExternalLinkage;
338
339 // - an enumerator belonging to an enumeration with external linkage;
340 if (isa<EnumConstantDecl>(D))
341 if (cast<NamedDecl>(D->getDeclContext())->getLinkage()
342 == NamedDecl::ExternalLinkage)
343 return NamedDecl::ExternalLinkage;
344
345 // - a template, unless it is a function template that has
346 // internal linkage (Clause 14);
347 if (isa<TemplateDecl>(D))
348 return NamedDecl::ExternalLinkage;
349
350 // - a namespace (7.3), unless it is declared within an unnamed
351 // namespace.
352 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
353 return NamedDecl::ExternalLinkage;
354
355 return NamedDecl::NoLinkage;
356}
357
358NamedDecl::Linkage NamedDecl::getLinkage() const {
359 // Handle linkage for namespace-scope names.
360 if (getDeclContext()->getLookupContext()->isFileContext())
361 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
362 return L;
363
364 // C++ [basic.link]p5:
365 // In addition, a member function, static data member, a named
366 // class or enumeration of class scope, or an unnamed class or
367 // enumeration defined in a class-scope typedef declaration such
368 // that the class or enumeration has the typedef name for linkage
369 // purposes (7.1.3), has external linkage if the name of the class
370 // has external linkage.
371 if (getDeclContext()->isRecord() &&
372 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
373 (isa<TagDecl>(this) &&
374 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) &&
375 cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage)
376 return ExternalLinkage;
377
378 // C++ [basic.link]p6:
379 // The name of a function declared in block scope and the name of
380 // an object declared by a block scope extern declaration have
381 // linkage. If there is a visible declaration of an entity with
382 // linkage having the same name and type, ignoring entities
383 // declared outside the innermost enclosing namespace scope, the
384 // block scope declaration declares that same entity and receives
385 // the linkage of the previous declaration. If there is more than
386 // one such matching entity, the program is ill-formed. Otherwise,
387 // if no matching entity is found, the block scope entity receives
388 // external linkage.
389 if (getLexicalDeclContext()->isFunctionOrMethod()) {
390 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
391 if (Function->getPreviousDeclaration())
392 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
393 return L;
394
395 return ExternalLinkage;
396 }
397
398 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
399 if (Var->getStorageClass() == VarDecl::Extern ||
400 Var->getStorageClass() == VarDecl::PrivateExtern) {
401 if (Var->getPreviousDeclaration())
402 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
403 return L;
404
405 return ExternalLinkage;
406 }
407 }
408
409 // C++ [basic.link]p6:
410 // Names not covered by these rules have no linkage.
411 return NoLinkage;
412}
413
414std::string NamedDecl::getQualifiedNameAsString() const {
415 return getQualifiedNameAsString(getASTContext().getLangOptions());
416}
417
418std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
419 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
420 // std::string thrashing.
421 std::vector<std::string> Names;
422 std::string QualName;
423 const DeclContext *Ctx = getDeclContext();
424
425 if (Ctx->isFunctionOrMethod())
426 return getNameAsString();
427
428 while (Ctx) {
218EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
219 IdentifierInfo *Id, SourceLocation TKL,
220 EnumDecl *PrevDecl) {
221 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
222 C.getTypeDeclType(Enum, PrevDecl);
223 return Enum;
224}
225
226void EnumDecl::Destroy(ASTContext& C) {
227 Decl::Destroy(C);
228}
229
230void EnumDecl::completeDefinition(ASTContext &C,
231 QualType NewType,
232 QualType NewPromotionType) {
233 assert(!isDefinition() && "Cannot redefine enums!");
234 IntegerType = NewType;
235 PromotionType = NewPromotionType;
236 TagDecl::completeDefinition();
237}
238
239FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
240 SourceLocation L,
241 StringLiteral *Str) {
242 return new (C) FileScopeAsmDecl(DC, L, Str);
243}
244
245//===----------------------------------------------------------------------===//
246// NamedDecl Implementation
247//===----------------------------------------------------------------------===//
248
249static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
250 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
251 "Not a name having namespace scope");
252 ASTContext &Context = D->getASTContext();
253
254 // C++ [basic.link]p3:
255 // A name having namespace scope (3.3.6) has internal linkage if it
256 // is the name of
257 // - an object, reference, function or function template that is
258 // explicitly declared static; or,
259 // (This bullet corresponds to C99 6.2.2p3.)
260 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
261 // Explicitly declared static.
262 if (Var->getStorageClass() == VarDecl::Static)
263 return NamedDecl::InternalLinkage;
264
265 // - an object or reference that is explicitly declared const
266 // and neither explicitly declared extern nor previously
267 // declared to have external linkage; or
268 // (there is no equivalent in C99)
269 if (Context.getLangOptions().CPlusPlus &&
270 Var->getType().isConstant(Context) &&
271 Var->getStorageClass() != VarDecl::Extern &&
272 Var->getStorageClass() != VarDecl::PrivateExtern) {
273 bool FoundExtern = false;
274 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
275 PrevVar && !FoundExtern;
276 PrevVar = PrevVar->getPreviousDeclaration())
277 if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage)
278 FoundExtern = true;
279
280 if (!FoundExtern)
281 return NamedDecl::InternalLinkage;
282 }
283 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
284 const FunctionDecl *Function = 0;
285 if (const FunctionTemplateDecl *FunTmpl
286 = dyn_cast<FunctionTemplateDecl>(D))
287 Function = FunTmpl->getTemplatedDecl();
288 else
289 Function = cast<FunctionDecl>(D);
290
291 // Explicitly declared static.
292 if (Function->getStorageClass() == FunctionDecl::Static)
293 return NamedDecl::InternalLinkage;
294 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
295 // - a data member of an anonymous union.
296 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
297 return NamedDecl::InternalLinkage;
298 }
299
300 // C++ [basic.link]p4:
301
302 // A name having namespace scope has external linkage if it is the
303 // name of
304 //
305 // - an object or reference, unless it has internal linkage; or
306 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
307 if (!Context.getLangOptions().CPlusPlus &&
308 (Var->getStorageClass() == VarDecl::Extern ||
309 Var->getStorageClass() == VarDecl::PrivateExtern)) {
310 // C99 6.2.2p4:
311 // For an identifier declared with the storage-class specifier
312 // extern in a scope in which a prior declaration of that
313 // identifier is visible, if the prior declaration specifies
314 // internal or external linkage, the linkage of the identifier
315 // at the later declaration is the same as the linkage
316 // specified at the prior declaration. If no prior declaration
317 // is visible, or if the prior declaration specifies no
318 // linkage, then the identifier has external linkage.
319 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
320 if (NamedDecl::Linkage L = PrevVar->getLinkage())
321 return L;
322 }
323 }
324
325 // C99 6.2.2p5:
326 // If the declaration of an identifier for an object has file
327 // scope and no storage-class specifier, its linkage is
328 // external.
329 return NamedDecl::ExternalLinkage;
330 }
331
332 // - a function, unless it has internal linkage; or
333 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
334 // C99 6.2.2p5:
335 // If the declaration of an identifier for a function has no
336 // storage-class specifier, its linkage is determined exactly
337 // as if it were declared with the storage-class specifier
338 // extern.
339 if (!Context.getLangOptions().CPlusPlus &&
340 (Function->getStorageClass() == FunctionDecl::Extern ||
341 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
342 Function->getStorageClass() == FunctionDecl::None)) {
343 // C99 6.2.2p4:
344 // For an identifier declared with the storage-class specifier
345 // extern in a scope in which a prior declaration of that
346 // identifier is visible, if the prior declaration specifies
347 // internal or external linkage, the linkage of the identifier
348 // at the later declaration is the same as the linkage
349 // specified at the prior declaration. If no prior declaration
350 // is visible, or if the prior declaration specifies no
351 // linkage, then the identifier has external linkage.
352 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
353 if (NamedDecl::Linkage L = PrevFunc->getLinkage())
354 return L;
355 }
356 }
357
358 return NamedDecl::ExternalLinkage;
359 }
360
361 // - a named class (Clause 9), or an unnamed class defined in a
362 // typedef declaration in which the class has the typedef name
363 // for linkage purposes (7.1.3); or
364 // - a named enumeration (7.2), or an unnamed enumeration
365 // defined in a typedef declaration in which the enumeration
366 // has the typedef name for linkage purposes (7.1.3); or
367 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
368 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl())
369 return NamedDecl::ExternalLinkage;
370
371 // - an enumerator belonging to an enumeration with external linkage;
372 if (isa<EnumConstantDecl>(D))
373 if (cast<NamedDecl>(D->getDeclContext())->getLinkage()
374 == NamedDecl::ExternalLinkage)
375 return NamedDecl::ExternalLinkage;
376
377 // - a template, unless it is a function template that has
378 // internal linkage (Clause 14);
379 if (isa<TemplateDecl>(D))
380 return NamedDecl::ExternalLinkage;
381
382 // - a namespace (7.3), unless it is declared within an unnamed
383 // namespace.
384 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
385 return NamedDecl::ExternalLinkage;
386
387 return NamedDecl::NoLinkage;
388}
389
390NamedDecl::Linkage NamedDecl::getLinkage() const {
391 // Handle linkage for namespace-scope names.
392 if (getDeclContext()->getLookupContext()->isFileContext())
393 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
394 return L;
395
396 // C++ [basic.link]p5:
397 // In addition, a member function, static data member, a named
398 // class or enumeration of class scope, or an unnamed class or
399 // enumeration defined in a class-scope typedef declaration such
400 // that the class or enumeration has the typedef name for linkage
401 // purposes (7.1.3), has external linkage if the name of the class
402 // has external linkage.
403 if (getDeclContext()->isRecord() &&
404 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
405 (isa<TagDecl>(this) &&
406 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) &&
407 cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage)
408 return ExternalLinkage;
409
410 // C++ [basic.link]p6:
411 // The name of a function declared in block scope and the name of
412 // an object declared by a block scope extern declaration have
413 // linkage. If there is a visible declaration of an entity with
414 // linkage having the same name and type, ignoring entities
415 // declared outside the innermost enclosing namespace scope, the
416 // block scope declaration declares that same entity and receives
417 // the linkage of the previous declaration. If there is more than
418 // one such matching entity, the program is ill-formed. Otherwise,
419 // if no matching entity is found, the block scope entity receives
420 // external linkage.
421 if (getLexicalDeclContext()->isFunctionOrMethod()) {
422 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
423 if (Function->getPreviousDeclaration())
424 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
425 return L;
426
427 return ExternalLinkage;
428 }
429
430 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
431 if (Var->getStorageClass() == VarDecl::Extern ||
432 Var->getStorageClass() == VarDecl::PrivateExtern) {
433 if (Var->getPreviousDeclaration())
434 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
435 return L;
436
437 return ExternalLinkage;
438 }
439 }
440
441 // C++ [basic.link]p6:
442 // Names not covered by these rules have no linkage.
443 return NoLinkage;
444}
445
446std::string NamedDecl::getQualifiedNameAsString() const {
447 return getQualifiedNameAsString(getASTContext().getLangOptions());
448}
449
450std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
451 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
452 // std::string thrashing.
453 std::vector<std::string> Names;
454 std::string QualName;
455 const DeclContext *Ctx = getDeclContext();
456
457 if (Ctx->isFunctionOrMethod())
458 return getNameAsString();
459
460 while (Ctx) {
429 if (Ctx->isFunctionOrMethod())
430 // FIXME: That probably will happen, when D was member of local
431 // scope class/struct/union. How do we handle this case?
432 break;
433
434 if (const ClassTemplateSpecializationDecl *Spec
435 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
436 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
437 std::string TemplateArgsStr
438 = TemplateSpecializationType::PrintTemplateArgumentList(
439 TemplateArgs.getFlatArgumentList(),
440 TemplateArgs.flat_size(),
441 P);
442 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
461 if (const ClassTemplateSpecializationDecl *Spec
462 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
463 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
464 std::string TemplateArgsStr
465 = TemplateSpecializationType::PrintTemplateArgumentList(
466 TemplateArgs.getFlatArgumentList(),
467 TemplateArgs.flat_size(),
468 P);
469 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
470 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
471 if (ND->isAnonymousNamespace())
472 Names.push_back("<anonymous namespace>");
473 else
474 Names.push_back(ND->getNameAsString());
475 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
476 if (!RD->getIdentifier()) {
477 std::string RecordString = "<anonymous ";
478 RecordString += RD->getKindName();
479 RecordString += ">";
480 Names.push_back(RecordString);
481 } else {
482 Names.push_back(RD->getNameAsString());
483 }
484 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
485 std::string Proto = FD->getNameAsString();
486
487 const FunctionProtoType *FT = 0;
488 if (FD->hasWrittenPrototype())
489 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
490
491 Proto += "(";
492 if (FT) {
493 llvm::raw_string_ostream POut(Proto);
494 unsigned NumParams = FD->getNumParams();
495 for (unsigned i = 0; i < NumParams; ++i) {
496 if (i)
497 POut << ", ";
498 std::string Param;
499 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
500 POut << Param;
501 }
502
503 if (FT->isVariadic()) {
504 if (NumParams > 0)
505 POut << ", ";
506 POut << "...";
507 }
508 }
509 Proto += ")";
510
511 Names.push_back(Proto);
443 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
444 Names.push_back(ND->getNameAsString());
445 else
446 break;
447
448 Ctx = Ctx->getParent();
449 }
450
451 std::vector<std::string>::reverse_iterator
452 I = Names.rbegin(),
453 End = Names.rend();
454
455 for (; I!=End; ++I)
456 QualName += *I + "::";
457
458 QualName += getNameAsString();
459
460 return QualName;
461}
462
463bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
464 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
465
466 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
467 // We want to keep it, unless it nominates same namespace.
468 if (getKind() == Decl::UsingDirective) {
469 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
470 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
471 }
472
473 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
474 // For function declarations, we keep track of redeclarations.
475 return FD->getPreviousDeclaration() == OldD;
476
477 // For function templates, the underlying function declarations are linked.
478 if (const FunctionTemplateDecl *FunctionTemplate
479 = dyn_cast<FunctionTemplateDecl>(this))
480 if (const FunctionTemplateDecl *OldFunctionTemplate
481 = dyn_cast<FunctionTemplateDecl>(OldD))
482 return FunctionTemplate->getTemplatedDecl()
483 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
484
485 // For method declarations, we keep track of redeclarations.
486 if (isa<ObjCMethodDecl>(this))
487 return false;
488
489 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
490 return true;
491
492 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
493 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
494 cast<UsingShadowDecl>(OldD)->getTargetDecl();
495
496 // For non-function declarations, if the declarations are of the
497 // same kind then this must be a redeclaration, or semantic analysis
498 // would not have given us the new declaration.
499 return this->getKind() == OldD->getKind();
500}
501
502bool NamedDecl::hasLinkage() const {
503 return getLinkage() != NoLinkage;
504}
505
506NamedDecl *NamedDecl::getUnderlyingDecl() {
507 NamedDecl *ND = this;
508 while (true) {
509 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
510 ND = UD->getTargetDecl();
511 else if (ObjCCompatibleAliasDecl *AD
512 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
513 return AD->getClassInterface();
514 else
515 return ND;
516 }
517}
518
519//===----------------------------------------------------------------------===//
520// DeclaratorDecl Implementation
521//===----------------------------------------------------------------------===//
522
523SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
524 if (DeclInfo) {
525 TypeLoc TL = DeclInfo->getTypeLoc();
526 while (true) {
527 TypeLoc NextTL = TL.getNextTypeLoc();
528 if (!NextTL)
529 return TL.getSourceRange().getBegin();
530 TL = NextTL;
531 }
532 }
533 return SourceLocation();
534}
535
536//===----------------------------------------------------------------------===//
537// VarDecl Implementation
538//===----------------------------------------------------------------------===//
539
540VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
541 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
542 StorageClass S) {
543 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
544}
545
546void VarDecl::Destroy(ASTContext& C) {
547 Expr *Init = getInit();
548 if (Init) {
549 Init->Destroy(C);
550 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
551 Eval->~EvaluatedStmt();
552 C.Deallocate(Eval);
553 }
554 }
555 this->~VarDecl();
556 C.Deallocate((void *)this);
557}
558
559VarDecl::~VarDecl() {
560}
561
562SourceRange VarDecl::getSourceRange() const {
563 if (getInit())
564 return SourceRange(getLocation(), getInit()->getLocEnd());
565 return SourceRange(getLocation(), getLocation());
566}
567
568bool VarDecl::isOutOfLine() const {
569 if (!isStaticDataMember())
570 return false;
571
572 if (Decl::isOutOfLine())
573 return true;
574
575 // If this static data member was instantiated from a static data member of
576 // a class template, check whether that static data member was defined
577 // out-of-line.
578 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
579 return VD->isOutOfLine();
580
581 return false;
582}
583
584VarDecl *VarDecl::getOutOfLineDefinition() {
585 if (!isStaticDataMember())
586 return 0;
587
588 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
589 RD != RDEnd; ++RD) {
590 if (RD->getLexicalDeclContext()->isFileContext())
591 return *RD;
592 }
593
594 return 0;
595}
596
597VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
598 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
599 return cast<VarDecl>(MSI->getInstantiatedFrom());
600
601 return 0;
602}
603
604TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
605 if (MemberSpecializationInfo *MSI
606 = getASTContext().getInstantiatedFromStaticDataMember(this))
607 return MSI->getTemplateSpecializationKind();
608
609 return TSK_Undeclared;
610}
611
612MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
613 return getASTContext().getInstantiatedFromStaticDataMember(this);
614}
615
616void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
617 SourceLocation PointOfInstantiation) {
618 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
619 assert(MSI && "Not an instantiated static data member?");
620 MSI->setTemplateSpecializationKind(TSK);
621 if (TSK != TSK_ExplicitSpecialization &&
622 PointOfInstantiation.isValid() &&
623 MSI->getPointOfInstantiation().isInvalid())
624 MSI->setPointOfInstantiation(PointOfInstantiation);
625}
626
627bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
628 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
629 return false;
630
631 const VarDecl *Def = 0;
632 return (!getDefinition(Def) &&
633 (getStorageClass() == None || getStorageClass() == Static));
634}
635
636const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
637 redecl_iterator I = redecls_begin(), E = redecls_end();
638 while (I != E && !I->getInit())
639 ++I;
640
641 if (I != E) {
642 Def = *I;
643 return I->getInit();
644 }
645 return 0;
646}
647
648VarDecl *VarDecl::getCanonicalDecl() {
649 return getFirstDeclaration();
650}
651
652//===----------------------------------------------------------------------===//
653// FunctionDecl Implementation
654//===----------------------------------------------------------------------===//
655
656void FunctionDecl::Destroy(ASTContext& C) {
657 if (Body && Body.isOffset())
658 Body.get(C.getExternalSource())->Destroy(C);
659
660 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
661 (*I)->Destroy(C);
662
663 FunctionTemplateSpecializationInfo *FTSInfo
664 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
665 if (FTSInfo)
666 C.Deallocate(FTSInfo);
667
668 MemberSpecializationInfo *MSInfo
669 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
670 if (MSInfo)
671 C.Deallocate(MSInfo);
672
673 C.Deallocate(ParamInfo);
674
675 Decl::Destroy(C);
676}
677
678void FunctionDecl::getNameForDiagnostic(std::string &S,
679 const PrintingPolicy &Policy,
680 bool Qualified) const {
681 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
682 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
683 if (TemplateArgs)
684 S += TemplateSpecializationType::PrintTemplateArgumentList(
685 TemplateArgs->getFlatArgumentList(),
686 TemplateArgs->flat_size(),
687 Policy);
688
689}
690
691Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
692 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
693 if (I->Body) {
694 Definition = *I;
695 return I->Body.get(getASTContext().getExternalSource());
696 }
697 }
698
699 return 0;
700}
701
702void FunctionDecl::setBody(Stmt *B) {
703 Body = B;
704 if (B)
705 EndRangeLoc = B->getLocEnd();
706}
707
708bool FunctionDecl::isMain() const {
709 ASTContext &Context = getASTContext();
710 return !Context.getLangOptions().Freestanding &&
711 getDeclContext()->getLookupContext()->isTranslationUnit() &&
712 getIdentifier() && getIdentifier()->isStr("main");
713}
714
715bool FunctionDecl::isExternC() const {
716 ASTContext &Context = getASTContext();
717 // In C, any non-static, non-overloadable function has external
718 // linkage.
719 if (!Context.getLangOptions().CPlusPlus)
720 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
721
722 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
723 DC = DC->getParent()) {
724 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
725 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
726 return getStorageClass() != Static &&
727 !getAttr<OverloadableAttr>();
728
729 break;
730 }
731 }
732
733 return false;
734}
735
736bool FunctionDecl::isGlobal() const {
737 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
738 return Method->isStatic();
739
740 if (getStorageClass() == Static)
741 return false;
742
743 for (const DeclContext *DC = getDeclContext();
744 DC->isNamespace();
745 DC = DC->getParent()) {
746 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
747 if (!Namespace->getDeclName())
748 return false;
749 break;
750 }
751 }
752
753 return true;
754}
755
756/// \brief Returns a value indicating whether this function
757/// corresponds to a builtin function.
758///
759/// The function corresponds to a built-in function if it is
760/// declared at translation scope or within an extern "C" block and
761/// its name matches with the name of a builtin. The returned value
762/// will be 0 for functions that do not correspond to a builtin, a
763/// value of type \c Builtin::ID if in the target-independent range
764/// \c [1,Builtin::First), or a target-specific builtin value.
765unsigned FunctionDecl::getBuiltinID() const {
766 ASTContext &Context = getASTContext();
767 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
768 return 0;
769
770 unsigned BuiltinID = getIdentifier()->getBuiltinID();
771 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
772 return BuiltinID;
773
774 // This function has the name of a known C library
775 // function. Determine whether it actually refers to the C library
776 // function or whether it just has the same name.
777
778 // If this is a static function, it's not a builtin.
779 if (getStorageClass() == Static)
780 return 0;
781
782 // If this function is at translation-unit scope and we're not in
783 // C++, it refers to the C library function.
784 if (!Context.getLangOptions().CPlusPlus &&
785 getDeclContext()->isTranslationUnit())
786 return BuiltinID;
787
788 // If the function is in an extern "C" linkage specification and is
789 // not marked "overloadable", it's the real function.
790 if (isa<LinkageSpecDecl>(getDeclContext()) &&
791 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
792 == LinkageSpecDecl::lang_c &&
793 !getAttr<OverloadableAttr>())
794 return BuiltinID;
795
796 // Not a builtin
797 return 0;
798}
799
800
801/// getNumParams - Return the number of parameters this function must have
802/// based on its FunctionType. This is the length of the PararmInfo array
803/// after it has been created.
804unsigned FunctionDecl::getNumParams() const {
805 const FunctionType *FT = getType()->getAs<FunctionType>();
806 if (isa<FunctionNoProtoType>(FT))
807 return 0;
808 return cast<FunctionProtoType>(FT)->getNumArgs();
809
810}
811
812void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
813 unsigned NumParams) {
814 assert(ParamInfo == 0 && "Already has param info!");
815 assert(NumParams == getNumParams() && "Parameter count mismatch!");
816
817 // Zero params -> null pointer.
818 if (NumParams) {
819 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
820 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
821 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
822
823 // Update source range. The check below allows us to set EndRangeLoc before
824 // setting the parameters.
825 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
826 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
827 }
828}
829
830/// getMinRequiredArguments - Returns the minimum number of arguments
831/// needed to call this function. This may be fewer than the number of
832/// function parameters, if some of the parameters have default
833/// arguments (in C++).
834unsigned FunctionDecl::getMinRequiredArguments() const {
835 unsigned NumRequiredArgs = getNumParams();
836 while (NumRequiredArgs > 0
837 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
838 --NumRequiredArgs;
839
840 return NumRequiredArgs;
841}
842
843bool FunctionDecl::isInlined() const {
844 // FIXME: This is not enough. Consider:
845 //
846 // inline void f();
847 // void f() { }
848 //
849 // f is inlined, but does not have inline specified.
850 // To fix this we should add an 'inline' flag to FunctionDecl.
851 if (isInlineSpecified())
852 return true;
853
854 if (isa<CXXMethodDecl>(this)) {
855 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
856 return true;
857 }
858
859 switch (getTemplateSpecializationKind()) {
860 case TSK_Undeclared:
861 case TSK_ExplicitSpecialization:
862 return false;
863
864 case TSK_ImplicitInstantiation:
865 case TSK_ExplicitInstantiationDeclaration:
866 case TSK_ExplicitInstantiationDefinition:
867 // Handle below.
868 break;
869 }
870
871 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
872 Stmt *Pattern = 0;
873 if (PatternDecl)
874 Pattern = PatternDecl->getBody(PatternDecl);
875
876 if (Pattern && PatternDecl)
877 return PatternDecl->isInlined();
878
879 return false;
880}
881
882/// \brief For an inline function definition in C or C++, determine whether the
883/// definition will be externally visible.
884///
885/// Inline function definitions are always available for inlining optimizations.
886/// However, depending on the language dialect, declaration specifiers, and
887/// attributes, the definition of an inline function may or may not be
888/// "externally" visible to other translation units in the program.
889///
890/// In C99, inline definitions are not externally visible by default. However,
891/// if even one of the globa-scope declarations is marked "extern inline", the
892/// inline definition becomes externally visible (C99 6.7.4p6).
893///
894/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
895/// definition, we use the GNU semantics for inline, which are nearly the
896/// opposite of C99 semantics. In particular, "inline" by itself will create
897/// an externally visible symbol, but "extern inline" will not create an
898/// externally visible symbol.
899bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
900 assert(isThisDeclarationADefinition() && "Must have the function definition");
901 assert(isInlined() && "Function must be inline");
902 ASTContext &Context = getASTContext();
903
904 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
905 // GNU inline semantics. Based on a number of examples, we came up with the
906 // following heuristic: if the "inline" keyword is present on a
907 // declaration of the function but "extern" is not present on that
908 // declaration, then the symbol is externally visible. Otherwise, the GNU
909 // "extern inline" semantics applies and the symbol is not externally
910 // visible.
911 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
912 Redecl != RedeclEnd;
913 ++Redecl) {
914 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
915 return true;
916 }
917
918 // GNU "extern inline" semantics; no externally visible symbol.
919 return false;
920 }
921
922 // C99 6.7.4p6:
923 // [...] If all of the file scope declarations for a function in a
924 // translation unit include the inline function specifier without extern,
925 // then the definition in that translation unit is an inline definition.
926 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
927 Redecl != RedeclEnd;
928 ++Redecl) {
929 // Only consider file-scope declarations in this test.
930 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
931 continue;
932
933 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
934 return true; // Not an inline definition
935 }
936
937 // C99 6.7.4p6:
938 // An inline definition does not provide an external definition for the
939 // function, and does not forbid an external definition in another
940 // translation unit.
941 return false;
942}
943
944void
945FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
946 redeclarable_base::setPreviousDeclaration(PrevDecl);
947
948 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
949 FunctionTemplateDecl *PrevFunTmpl
950 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
951 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
952 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
953 }
954}
955
956const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
957 return getFirstDeclaration();
958}
959
960FunctionDecl *FunctionDecl::getCanonicalDecl() {
961 return getFirstDeclaration();
962}
963
964/// getOverloadedOperator - Which C++ overloaded operator this
965/// function represents, if any.
966OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
967 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
968 return getDeclName().getCXXOverloadedOperator();
969 else
970 return OO_None;
971}
972
973FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
974 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
975 return cast<FunctionDecl>(Info->getInstantiatedFrom());
976
977 return 0;
978}
979
980MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
981 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
982}
983
984void
985FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
986 TemplateSpecializationKind TSK) {
987 assert(TemplateOrSpecialization.isNull() &&
988 "Member function is already a specialization");
989 MemberSpecializationInfo *Info
990 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
991 TemplateOrSpecialization = Info;
992}
993
994bool FunctionDecl::isImplicitlyInstantiable() const {
995 // If this function already has a definition or is invalid, it can't be
996 // implicitly instantiated.
997 if (isInvalidDecl() || getBody())
998 return false;
999
1000 switch (getTemplateSpecializationKind()) {
1001 case TSK_Undeclared:
1002 case TSK_ExplicitSpecialization:
1003 case TSK_ExplicitInstantiationDefinition:
1004 return false;
1005
1006 case TSK_ImplicitInstantiation:
1007 return true;
1008
1009 case TSK_ExplicitInstantiationDeclaration:
1010 // Handled below.
1011 break;
1012 }
1013
1014 // Find the actual template from which we will instantiate.
1015 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1016 Stmt *Pattern = 0;
1017 if (PatternDecl)
1018 Pattern = PatternDecl->getBody(PatternDecl);
1019
1020 // C++0x [temp.explicit]p9:
1021 // Except for inline functions, other explicit instantiation declarations
1022 // have the effect of suppressing the implicit instantiation of the entity
1023 // to which they refer.
1024 if (!Pattern || !PatternDecl)
1025 return true;
1026
1027 return PatternDecl->isInlined();
1028}
1029
1030FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1031 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1032 while (Primary->getInstantiatedFromMemberTemplate()) {
1033 // If we have hit a point where the user provided a specialization of
1034 // this template, we're done looking.
1035 if (Primary->isMemberSpecialization())
1036 break;
1037
1038 Primary = Primary->getInstantiatedFromMemberTemplate();
1039 }
1040
1041 return Primary->getTemplatedDecl();
1042 }
1043
1044 return getInstantiatedFromMemberFunction();
1045}
1046
1047FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
1048 if (FunctionTemplateSpecializationInfo *Info
1049 = TemplateOrSpecialization
1050 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1051 return Info->Template.getPointer();
1052 }
1053 return 0;
1054}
1055
1056const TemplateArgumentList *
1057FunctionDecl::getTemplateSpecializationArgs() const {
1058 if (FunctionTemplateSpecializationInfo *Info
1059 = TemplateOrSpecialization
1060 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1061 return Info->TemplateArguments;
1062 }
1063 return 0;
1064}
1065
1066void
1067FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1068 FunctionTemplateDecl *Template,
1069 const TemplateArgumentList *TemplateArgs,
1070 void *InsertPos,
1071 TemplateSpecializationKind TSK) {
1072 assert(TSK != TSK_Undeclared &&
1073 "Must specify the type of function template specialization");
1074 FunctionTemplateSpecializationInfo *Info
1075 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
1076 if (!Info)
1077 Info = new (Context) FunctionTemplateSpecializationInfo;
1078
1079 Info->Function = this;
1080 Info->Template.setPointer(Template);
1081 Info->Template.setInt(TSK - 1);
1082 Info->TemplateArguments = TemplateArgs;
1083 TemplateOrSpecialization = Info;
1084
1085 // Insert this function template specialization into the set of known
1086 // function template specializations.
1087 if (InsertPos)
1088 Template->getSpecializations().InsertNode(Info, InsertPos);
1089 else {
1090 // Try to insert the new node. If there is an existing node, remove it
1091 // first.
1092 FunctionTemplateSpecializationInfo *Existing
1093 = Template->getSpecializations().GetOrInsertNode(Info);
1094 if (Existing) {
1095 Template->getSpecializations().RemoveNode(Existing);
1096 Template->getSpecializations().GetOrInsertNode(Info);
1097 }
1098 }
1099}
1100
1101TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
1102 // For a function template specialization, query the specialization
1103 // information object.
1104 FunctionTemplateSpecializationInfo *FTSInfo
1105 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
1106 if (FTSInfo)
1107 return FTSInfo->getTemplateSpecializationKind();
1108
1109 MemberSpecializationInfo *MSInfo
1110 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1111 if (MSInfo)
1112 return MSInfo->getTemplateSpecializationKind();
1113
1114 return TSK_Undeclared;
1115}
1116
1117void
1118FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1119 SourceLocation PointOfInstantiation) {
1120 if (FunctionTemplateSpecializationInfo *FTSInfo
1121 = TemplateOrSpecialization.dyn_cast<
1122 FunctionTemplateSpecializationInfo*>()) {
1123 FTSInfo->setTemplateSpecializationKind(TSK);
1124 if (TSK != TSK_ExplicitSpecialization &&
1125 PointOfInstantiation.isValid() &&
1126 FTSInfo->getPointOfInstantiation().isInvalid())
1127 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1128 } else if (MemberSpecializationInfo *MSInfo
1129 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1130 MSInfo->setTemplateSpecializationKind(TSK);
1131 if (TSK != TSK_ExplicitSpecialization &&
1132 PointOfInstantiation.isValid() &&
1133 MSInfo->getPointOfInstantiation().isInvalid())
1134 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1135 } else
1136 assert(false && "Function cannot have a template specialization kind");
1137}
1138
1139SourceLocation FunctionDecl::getPointOfInstantiation() const {
1140 if (FunctionTemplateSpecializationInfo *FTSInfo
1141 = TemplateOrSpecialization.dyn_cast<
1142 FunctionTemplateSpecializationInfo*>())
1143 return FTSInfo->getPointOfInstantiation();
1144 else if (MemberSpecializationInfo *MSInfo
1145 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
1146 return MSInfo->getPointOfInstantiation();
1147
1148 return SourceLocation();
1149}
1150
1151bool FunctionDecl::isOutOfLine() const {
1152 if (Decl::isOutOfLine())
1153 return true;
1154
1155 // If this function was instantiated from a member function of a
1156 // class template, check whether that member function was defined out-of-line.
1157 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1158 const FunctionDecl *Definition;
1159 if (FD->getBody(Definition))
1160 return Definition->isOutOfLine();
1161 }
1162
1163 // If this function was instantiated from a function template,
1164 // check whether that function template was defined out-of-line.
1165 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1166 const FunctionDecl *Definition;
1167 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1168 return Definition->isOutOfLine();
1169 }
1170
1171 return false;
1172}
1173
1174//===----------------------------------------------------------------------===//
1175// TagDecl Implementation
1176//===----------------------------------------------------------------------===//
1177
1178SourceRange TagDecl::getSourceRange() const {
1179 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
1180 return SourceRange(TagKeywordLoc, E);
1181}
1182
1183TagDecl* TagDecl::getCanonicalDecl() {
1184 return getFirstDeclaration();
1185}
1186
1187void TagDecl::startDefinition() {
1188 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1189 TagT->decl.setPointer(this);
1190 TagT->decl.setInt(1);
1191 }
1192}
1193
1194void TagDecl::completeDefinition() {
1195 IsDefinition = true;
1196 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1197 assert(TagT->decl.getPointer() == this &&
1198 "Attempt to redefine a tag definition?");
1199 TagT->decl.setInt(0);
1200 }
1201}
1202
1203TagDecl* TagDecl::getDefinition(ASTContext& C) const {
1204 if (isDefinition())
1205 return const_cast<TagDecl *>(this);
1206
1207 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
1208 R != REnd; ++R)
1209 if (R->isDefinition())
1210 return *R;
1211
1212 return 0;
1213}
1214
1215TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1216 switch (TypeSpec) {
1217 default: llvm_unreachable("unexpected type specifier");
1218 case DeclSpec::TST_struct: return TK_struct;
1219 case DeclSpec::TST_class: return TK_class;
1220 case DeclSpec::TST_union: return TK_union;
1221 case DeclSpec::TST_enum: return TK_enum;
1222 }
1223}
1224
1225//===----------------------------------------------------------------------===//
1226// RecordDecl Implementation
1227//===----------------------------------------------------------------------===//
1228
1229RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
1230 IdentifierInfo *Id, RecordDecl *PrevDecl,
1231 SourceLocation TKL)
1232 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
1233 HasFlexibleArrayMember = false;
1234 AnonymousStructOrUnion = false;
1235 HasObjectMember = false;
1236 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
1237}
1238
1239RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
1240 SourceLocation L, IdentifierInfo *Id,
1241 SourceLocation TKL, RecordDecl* PrevDecl) {
1242
1243 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
1244 C.getTypeDeclType(R, PrevDecl);
1245 return R;
1246}
1247
1248RecordDecl::~RecordDecl() {
1249}
1250
1251void RecordDecl::Destroy(ASTContext& C) {
1252 TagDecl::Destroy(C);
1253}
1254
1255bool RecordDecl::isInjectedClassName() const {
1256 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1257 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1258}
1259
1260/// completeDefinition - Notes that the definition of this type is now
1261/// complete.
1262void RecordDecl::completeDefinition(ASTContext& C) {
1263 assert(!isDefinition() && "Cannot redefine record!");
1264 TagDecl::completeDefinition();
1265}
1266
1267//===----------------------------------------------------------------------===//
1268// BlockDecl Implementation
1269//===----------------------------------------------------------------------===//
1270
1271BlockDecl::~BlockDecl() {
1272}
1273
1274void BlockDecl::Destroy(ASTContext& C) {
1275 if (Body)
1276 Body->Destroy(C);
1277
1278 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1279 (*I)->Destroy(C);
1280
1281 C.Deallocate(ParamInfo);
1282 Decl::Destroy(C);
1283}
1284
1285void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1286 unsigned NParms) {
1287 assert(ParamInfo == 0 && "Already has param info!");
1288
1289 // Zero params -> null pointer.
1290 if (NParms) {
1291 NumParams = NParms;
1292 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1293 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1294 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1295 }
1296}
1297
1298unsigned BlockDecl::getNumParams() const {
1299 return NumParams;
1300}
512 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
513 Names.push_back(ND->getNameAsString());
514 else
515 break;
516
517 Ctx = Ctx->getParent();
518 }
519
520 std::vector<std::string>::reverse_iterator
521 I = Names.rbegin(),
522 End = Names.rend();
523
524 for (; I!=End; ++I)
525 QualName += *I + "::";
526
527 QualName += getNameAsString();
528
529 return QualName;
530}
531
532bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
533 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
534
535 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
536 // We want to keep it, unless it nominates same namespace.
537 if (getKind() == Decl::UsingDirective) {
538 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
539 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
540 }
541
542 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
543 // For function declarations, we keep track of redeclarations.
544 return FD->getPreviousDeclaration() == OldD;
545
546 // For function templates, the underlying function declarations are linked.
547 if (const FunctionTemplateDecl *FunctionTemplate
548 = dyn_cast<FunctionTemplateDecl>(this))
549 if (const FunctionTemplateDecl *OldFunctionTemplate
550 = dyn_cast<FunctionTemplateDecl>(OldD))
551 return FunctionTemplate->getTemplatedDecl()
552 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
553
554 // For method declarations, we keep track of redeclarations.
555 if (isa<ObjCMethodDecl>(this))
556 return false;
557
558 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
559 return true;
560
561 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
562 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
563 cast<UsingShadowDecl>(OldD)->getTargetDecl();
564
565 // For non-function declarations, if the declarations are of the
566 // same kind then this must be a redeclaration, or semantic analysis
567 // would not have given us the new declaration.
568 return this->getKind() == OldD->getKind();
569}
570
571bool NamedDecl::hasLinkage() const {
572 return getLinkage() != NoLinkage;
573}
574
575NamedDecl *NamedDecl::getUnderlyingDecl() {
576 NamedDecl *ND = this;
577 while (true) {
578 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
579 ND = UD->getTargetDecl();
580 else if (ObjCCompatibleAliasDecl *AD
581 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
582 return AD->getClassInterface();
583 else
584 return ND;
585 }
586}
587
588//===----------------------------------------------------------------------===//
589// DeclaratorDecl Implementation
590//===----------------------------------------------------------------------===//
591
592SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
593 if (DeclInfo) {
594 TypeLoc TL = DeclInfo->getTypeLoc();
595 while (true) {
596 TypeLoc NextTL = TL.getNextTypeLoc();
597 if (!NextTL)
598 return TL.getSourceRange().getBegin();
599 TL = NextTL;
600 }
601 }
602 return SourceLocation();
603}
604
605//===----------------------------------------------------------------------===//
606// VarDecl Implementation
607//===----------------------------------------------------------------------===//
608
609VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
610 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
611 StorageClass S) {
612 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
613}
614
615void VarDecl::Destroy(ASTContext& C) {
616 Expr *Init = getInit();
617 if (Init) {
618 Init->Destroy(C);
619 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
620 Eval->~EvaluatedStmt();
621 C.Deallocate(Eval);
622 }
623 }
624 this->~VarDecl();
625 C.Deallocate((void *)this);
626}
627
628VarDecl::~VarDecl() {
629}
630
631SourceRange VarDecl::getSourceRange() const {
632 if (getInit())
633 return SourceRange(getLocation(), getInit()->getLocEnd());
634 return SourceRange(getLocation(), getLocation());
635}
636
637bool VarDecl::isOutOfLine() const {
638 if (!isStaticDataMember())
639 return false;
640
641 if (Decl::isOutOfLine())
642 return true;
643
644 // If this static data member was instantiated from a static data member of
645 // a class template, check whether that static data member was defined
646 // out-of-line.
647 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
648 return VD->isOutOfLine();
649
650 return false;
651}
652
653VarDecl *VarDecl::getOutOfLineDefinition() {
654 if (!isStaticDataMember())
655 return 0;
656
657 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
658 RD != RDEnd; ++RD) {
659 if (RD->getLexicalDeclContext()->isFileContext())
660 return *RD;
661 }
662
663 return 0;
664}
665
666VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
667 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
668 return cast<VarDecl>(MSI->getInstantiatedFrom());
669
670 return 0;
671}
672
673TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
674 if (MemberSpecializationInfo *MSI
675 = getASTContext().getInstantiatedFromStaticDataMember(this))
676 return MSI->getTemplateSpecializationKind();
677
678 return TSK_Undeclared;
679}
680
681MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
682 return getASTContext().getInstantiatedFromStaticDataMember(this);
683}
684
685void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
686 SourceLocation PointOfInstantiation) {
687 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
688 assert(MSI && "Not an instantiated static data member?");
689 MSI->setTemplateSpecializationKind(TSK);
690 if (TSK != TSK_ExplicitSpecialization &&
691 PointOfInstantiation.isValid() &&
692 MSI->getPointOfInstantiation().isInvalid())
693 MSI->setPointOfInstantiation(PointOfInstantiation);
694}
695
696bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
697 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
698 return false;
699
700 const VarDecl *Def = 0;
701 return (!getDefinition(Def) &&
702 (getStorageClass() == None || getStorageClass() == Static));
703}
704
705const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
706 redecl_iterator I = redecls_begin(), E = redecls_end();
707 while (I != E && !I->getInit())
708 ++I;
709
710 if (I != E) {
711 Def = *I;
712 return I->getInit();
713 }
714 return 0;
715}
716
717VarDecl *VarDecl::getCanonicalDecl() {
718 return getFirstDeclaration();
719}
720
721//===----------------------------------------------------------------------===//
722// FunctionDecl Implementation
723//===----------------------------------------------------------------------===//
724
725void FunctionDecl::Destroy(ASTContext& C) {
726 if (Body && Body.isOffset())
727 Body.get(C.getExternalSource())->Destroy(C);
728
729 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
730 (*I)->Destroy(C);
731
732 FunctionTemplateSpecializationInfo *FTSInfo
733 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
734 if (FTSInfo)
735 C.Deallocate(FTSInfo);
736
737 MemberSpecializationInfo *MSInfo
738 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
739 if (MSInfo)
740 C.Deallocate(MSInfo);
741
742 C.Deallocate(ParamInfo);
743
744 Decl::Destroy(C);
745}
746
747void FunctionDecl::getNameForDiagnostic(std::string &S,
748 const PrintingPolicy &Policy,
749 bool Qualified) const {
750 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
751 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
752 if (TemplateArgs)
753 S += TemplateSpecializationType::PrintTemplateArgumentList(
754 TemplateArgs->getFlatArgumentList(),
755 TemplateArgs->flat_size(),
756 Policy);
757
758}
759
760Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
761 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
762 if (I->Body) {
763 Definition = *I;
764 return I->Body.get(getASTContext().getExternalSource());
765 }
766 }
767
768 return 0;
769}
770
771void FunctionDecl::setBody(Stmt *B) {
772 Body = B;
773 if (B)
774 EndRangeLoc = B->getLocEnd();
775}
776
777bool FunctionDecl::isMain() const {
778 ASTContext &Context = getASTContext();
779 return !Context.getLangOptions().Freestanding &&
780 getDeclContext()->getLookupContext()->isTranslationUnit() &&
781 getIdentifier() && getIdentifier()->isStr("main");
782}
783
784bool FunctionDecl::isExternC() const {
785 ASTContext &Context = getASTContext();
786 // In C, any non-static, non-overloadable function has external
787 // linkage.
788 if (!Context.getLangOptions().CPlusPlus)
789 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
790
791 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
792 DC = DC->getParent()) {
793 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
794 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
795 return getStorageClass() != Static &&
796 !getAttr<OverloadableAttr>();
797
798 break;
799 }
800 }
801
802 return false;
803}
804
805bool FunctionDecl::isGlobal() const {
806 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
807 return Method->isStatic();
808
809 if (getStorageClass() == Static)
810 return false;
811
812 for (const DeclContext *DC = getDeclContext();
813 DC->isNamespace();
814 DC = DC->getParent()) {
815 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
816 if (!Namespace->getDeclName())
817 return false;
818 break;
819 }
820 }
821
822 return true;
823}
824
825/// \brief Returns a value indicating whether this function
826/// corresponds to a builtin function.
827///
828/// The function corresponds to a built-in function if it is
829/// declared at translation scope or within an extern "C" block and
830/// its name matches with the name of a builtin. The returned value
831/// will be 0 for functions that do not correspond to a builtin, a
832/// value of type \c Builtin::ID if in the target-independent range
833/// \c [1,Builtin::First), or a target-specific builtin value.
834unsigned FunctionDecl::getBuiltinID() const {
835 ASTContext &Context = getASTContext();
836 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
837 return 0;
838
839 unsigned BuiltinID = getIdentifier()->getBuiltinID();
840 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
841 return BuiltinID;
842
843 // This function has the name of a known C library
844 // function. Determine whether it actually refers to the C library
845 // function or whether it just has the same name.
846
847 // If this is a static function, it's not a builtin.
848 if (getStorageClass() == Static)
849 return 0;
850
851 // If this function is at translation-unit scope and we're not in
852 // C++, it refers to the C library function.
853 if (!Context.getLangOptions().CPlusPlus &&
854 getDeclContext()->isTranslationUnit())
855 return BuiltinID;
856
857 // If the function is in an extern "C" linkage specification and is
858 // not marked "overloadable", it's the real function.
859 if (isa<LinkageSpecDecl>(getDeclContext()) &&
860 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
861 == LinkageSpecDecl::lang_c &&
862 !getAttr<OverloadableAttr>())
863 return BuiltinID;
864
865 // Not a builtin
866 return 0;
867}
868
869
870/// getNumParams - Return the number of parameters this function must have
871/// based on its FunctionType. This is the length of the PararmInfo array
872/// after it has been created.
873unsigned FunctionDecl::getNumParams() const {
874 const FunctionType *FT = getType()->getAs<FunctionType>();
875 if (isa<FunctionNoProtoType>(FT))
876 return 0;
877 return cast<FunctionProtoType>(FT)->getNumArgs();
878
879}
880
881void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
882 unsigned NumParams) {
883 assert(ParamInfo == 0 && "Already has param info!");
884 assert(NumParams == getNumParams() && "Parameter count mismatch!");
885
886 // Zero params -> null pointer.
887 if (NumParams) {
888 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
889 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
890 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
891
892 // Update source range. The check below allows us to set EndRangeLoc before
893 // setting the parameters.
894 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
895 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
896 }
897}
898
899/// getMinRequiredArguments - Returns the minimum number of arguments
900/// needed to call this function. This may be fewer than the number of
901/// function parameters, if some of the parameters have default
902/// arguments (in C++).
903unsigned FunctionDecl::getMinRequiredArguments() const {
904 unsigned NumRequiredArgs = getNumParams();
905 while (NumRequiredArgs > 0
906 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
907 --NumRequiredArgs;
908
909 return NumRequiredArgs;
910}
911
912bool FunctionDecl::isInlined() const {
913 // FIXME: This is not enough. Consider:
914 //
915 // inline void f();
916 // void f() { }
917 //
918 // f is inlined, but does not have inline specified.
919 // To fix this we should add an 'inline' flag to FunctionDecl.
920 if (isInlineSpecified())
921 return true;
922
923 if (isa<CXXMethodDecl>(this)) {
924 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
925 return true;
926 }
927
928 switch (getTemplateSpecializationKind()) {
929 case TSK_Undeclared:
930 case TSK_ExplicitSpecialization:
931 return false;
932
933 case TSK_ImplicitInstantiation:
934 case TSK_ExplicitInstantiationDeclaration:
935 case TSK_ExplicitInstantiationDefinition:
936 // Handle below.
937 break;
938 }
939
940 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
941 Stmt *Pattern = 0;
942 if (PatternDecl)
943 Pattern = PatternDecl->getBody(PatternDecl);
944
945 if (Pattern && PatternDecl)
946 return PatternDecl->isInlined();
947
948 return false;
949}
950
951/// \brief For an inline function definition in C or C++, determine whether the
952/// definition will be externally visible.
953///
954/// Inline function definitions are always available for inlining optimizations.
955/// However, depending on the language dialect, declaration specifiers, and
956/// attributes, the definition of an inline function may or may not be
957/// "externally" visible to other translation units in the program.
958///
959/// In C99, inline definitions are not externally visible by default. However,
960/// if even one of the globa-scope declarations is marked "extern inline", the
961/// inline definition becomes externally visible (C99 6.7.4p6).
962///
963/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
964/// definition, we use the GNU semantics for inline, which are nearly the
965/// opposite of C99 semantics. In particular, "inline" by itself will create
966/// an externally visible symbol, but "extern inline" will not create an
967/// externally visible symbol.
968bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
969 assert(isThisDeclarationADefinition() && "Must have the function definition");
970 assert(isInlined() && "Function must be inline");
971 ASTContext &Context = getASTContext();
972
973 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
974 // GNU inline semantics. Based on a number of examples, we came up with the
975 // following heuristic: if the "inline" keyword is present on a
976 // declaration of the function but "extern" is not present on that
977 // declaration, then the symbol is externally visible. Otherwise, the GNU
978 // "extern inline" semantics applies and the symbol is not externally
979 // visible.
980 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
981 Redecl != RedeclEnd;
982 ++Redecl) {
983 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
984 return true;
985 }
986
987 // GNU "extern inline" semantics; no externally visible symbol.
988 return false;
989 }
990
991 // C99 6.7.4p6:
992 // [...] If all of the file scope declarations for a function in a
993 // translation unit include the inline function specifier without extern,
994 // then the definition in that translation unit is an inline definition.
995 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
996 Redecl != RedeclEnd;
997 ++Redecl) {
998 // Only consider file-scope declarations in this test.
999 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1000 continue;
1001
1002 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
1003 return true; // Not an inline definition
1004 }
1005
1006 // C99 6.7.4p6:
1007 // An inline definition does not provide an external definition for the
1008 // function, and does not forbid an external definition in another
1009 // translation unit.
1010 return false;
1011}
1012
1013void
1014FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1015 redeclarable_base::setPreviousDeclaration(PrevDecl);
1016
1017 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1018 FunctionTemplateDecl *PrevFunTmpl
1019 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1020 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1021 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1022 }
1023}
1024
1025const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1026 return getFirstDeclaration();
1027}
1028
1029FunctionDecl *FunctionDecl::getCanonicalDecl() {
1030 return getFirstDeclaration();
1031}
1032
1033/// getOverloadedOperator - Which C++ overloaded operator this
1034/// function represents, if any.
1035OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1036 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1037 return getDeclName().getCXXOverloadedOperator();
1038 else
1039 return OO_None;
1040}
1041
1042FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1043 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
1044 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1045
1046 return 0;
1047}
1048
1049MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1050 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1051}
1052
1053void
1054FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1055 TemplateSpecializationKind TSK) {
1056 assert(TemplateOrSpecialization.isNull() &&
1057 "Member function is already a specialization");
1058 MemberSpecializationInfo *Info
1059 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1060 TemplateOrSpecialization = Info;
1061}
1062
1063bool FunctionDecl::isImplicitlyInstantiable() const {
1064 // If this function already has a definition or is invalid, it can't be
1065 // implicitly instantiated.
1066 if (isInvalidDecl() || getBody())
1067 return false;
1068
1069 switch (getTemplateSpecializationKind()) {
1070 case TSK_Undeclared:
1071 case TSK_ExplicitSpecialization:
1072 case TSK_ExplicitInstantiationDefinition:
1073 return false;
1074
1075 case TSK_ImplicitInstantiation:
1076 return true;
1077
1078 case TSK_ExplicitInstantiationDeclaration:
1079 // Handled below.
1080 break;
1081 }
1082
1083 // Find the actual template from which we will instantiate.
1084 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1085 Stmt *Pattern = 0;
1086 if (PatternDecl)
1087 Pattern = PatternDecl->getBody(PatternDecl);
1088
1089 // C++0x [temp.explicit]p9:
1090 // Except for inline functions, other explicit instantiation declarations
1091 // have the effect of suppressing the implicit instantiation of the entity
1092 // to which they refer.
1093 if (!Pattern || !PatternDecl)
1094 return true;
1095
1096 return PatternDecl->isInlined();
1097}
1098
1099FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1100 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1101 while (Primary->getInstantiatedFromMemberTemplate()) {
1102 // If we have hit a point where the user provided a specialization of
1103 // this template, we're done looking.
1104 if (Primary->isMemberSpecialization())
1105 break;
1106
1107 Primary = Primary->getInstantiatedFromMemberTemplate();
1108 }
1109
1110 return Primary->getTemplatedDecl();
1111 }
1112
1113 return getInstantiatedFromMemberFunction();
1114}
1115
1116FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
1117 if (FunctionTemplateSpecializationInfo *Info
1118 = TemplateOrSpecialization
1119 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1120 return Info->Template.getPointer();
1121 }
1122 return 0;
1123}
1124
1125const TemplateArgumentList *
1126FunctionDecl::getTemplateSpecializationArgs() const {
1127 if (FunctionTemplateSpecializationInfo *Info
1128 = TemplateOrSpecialization
1129 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1130 return Info->TemplateArguments;
1131 }
1132 return 0;
1133}
1134
1135void
1136FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1137 FunctionTemplateDecl *Template,
1138 const TemplateArgumentList *TemplateArgs,
1139 void *InsertPos,
1140 TemplateSpecializationKind TSK) {
1141 assert(TSK != TSK_Undeclared &&
1142 "Must specify the type of function template specialization");
1143 FunctionTemplateSpecializationInfo *Info
1144 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
1145 if (!Info)
1146 Info = new (Context) FunctionTemplateSpecializationInfo;
1147
1148 Info->Function = this;
1149 Info->Template.setPointer(Template);
1150 Info->Template.setInt(TSK - 1);
1151 Info->TemplateArguments = TemplateArgs;
1152 TemplateOrSpecialization = Info;
1153
1154 // Insert this function template specialization into the set of known
1155 // function template specializations.
1156 if (InsertPos)
1157 Template->getSpecializations().InsertNode(Info, InsertPos);
1158 else {
1159 // Try to insert the new node. If there is an existing node, remove it
1160 // first.
1161 FunctionTemplateSpecializationInfo *Existing
1162 = Template->getSpecializations().GetOrInsertNode(Info);
1163 if (Existing) {
1164 Template->getSpecializations().RemoveNode(Existing);
1165 Template->getSpecializations().GetOrInsertNode(Info);
1166 }
1167 }
1168}
1169
1170TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
1171 // For a function template specialization, query the specialization
1172 // information object.
1173 FunctionTemplateSpecializationInfo *FTSInfo
1174 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
1175 if (FTSInfo)
1176 return FTSInfo->getTemplateSpecializationKind();
1177
1178 MemberSpecializationInfo *MSInfo
1179 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1180 if (MSInfo)
1181 return MSInfo->getTemplateSpecializationKind();
1182
1183 return TSK_Undeclared;
1184}
1185
1186void
1187FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1188 SourceLocation PointOfInstantiation) {
1189 if (FunctionTemplateSpecializationInfo *FTSInfo
1190 = TemplateOrSpecialization.dyn_cast<
1191 FunctionTemplateSpecializationInfo*>()) {
1192 FTSInfo->setTemplateSpecializationKind(TSK);
1193 if (TSK != TSK_ExplicitSpecialization &&
1194 PointOfInstantiation.isValid() &&
1195 FTSInfo->getPointOfInstantiation().isInvalid())
1196 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1197 } else if (MemberSpecializationInfo *MSInfo
1198 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1199 MSInfo->setTemplateSpecializationKind(TSK);
1200 if (TSK != TSK_ExplicitSpecialization &&
1201 PointOfInstantiation.isValid() &&
1202 MSInfo->getPointOfInstantiation().isInvalid())
1203 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1204 } else
1205 assert(false && "Function cannot have a template specialization kind");
1206}
1207
1208SourceLocation FunctionDecl::getPointOfInstantiation() const {
1209 if (FunctionTemplateSpecializationInfo *FTSInfo
1210 = TemplateOrSpecialization.dyn_cast<
1211 FunctionTemplateSpecializationInfo*>())
1212 return FTSInfo->getPointOfInstantiation();
1213 else if (MemberSpecializationInfo *MSInfo
1214 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
1215 return MSInfo->getPointOfInstantiation();
1216
1217 return SourceLocation();
1218}
1219
1220bool FunctionDecl::isOutOfLine() const {
1221 if (Decl::isOutOfLine())
1222 return true;
1223
1224 // If this function was instantiated from a member function of a
1225 // class template, check whether that member function was defined out-of-line.
1226 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1227 const FunctionDecl *Definition;
1228 if (FD->getBody(Definition))
1229 return Definition->isOutOfLine();
1230 }
1231
1232 // If this function was instantiated from a function template,
1233 // check whether that function template was defined out-of-line.
1234 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1235 const FunctionDecl *Definition;
1236 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1237 return Definition->isOutOfLine();
1238 }
1239
1240 return false;
1241}
1242
1243//===----------------------------------------------------------------------===//
1244// TagDecl Implementation
1245//===----------------------------------------------------------------------===//
1246
1247SourceRange TagDecl::getSourceRange() const {
1248 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
1249 return SourceRange(TagKeywordLoc, E);
1250}
1251
1252TagDecl* TagDecl::getCanonicalDecl() {
1253 return getFirstDeclaration();
1254}
1255
1256void TagDecl::startDefinition() {
1257 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1258 TagT->decl.setPointer(this);
1259 TagT->decl.setInt(1);
1260 }
1261}
1262
1263void TagDecl::completeDefinition() {
1264 IsDefinition = true;
1265 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1266 assert(TagT->decl.getPointer() == this &&
1267 "Attempt to redefine a tag definition?");
1268 TagT->decl.setInt(0);
1269 }
1270}
1271
1272TagDecl* TagDecl::getDefinition(ASTContext& C) const {
1273 if (isDefinition())
1274 return const_cast<TagDecl *>(this);
1275
1276 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
1277 R != REnd; ++R)
1278 if (R->isDefinition())
1279 return *R;
1280
1281 return 0;
1282}
1283
1284TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1285 switch (TypeSpec) {
1286 default: llvm_unreachable("unexpected type specifier");
1287 case DeclSpec::TST_struct: return TK_struct;
1288 case DeclSpec::TST_class: return TK_class;
1289 case DeclSpec::TST_union: return TK_union;
1290 case DeclSpec::TST_enum: return TK_enum;
1291 }
1292}
1293
1294//===----------------------------------------------------------------------===//
1295// RecordDecl Implementation
1296//===----------------------------------------------------------------------===//
1297
1298RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
1299 IdentifierInfo *Id, RecordDecl *PrevDecl,
1300 SourceLocation TKL)
1301 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
1302 HasFlexibleArrayMember = false;
1303 AnonymousStructOrUnion = false;
1304 HasObjectMember = false;
1305 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
1306}
1307
1308RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
1309 SourceLocation L, IdentifierInfo *Id,
1310 SourceLocation TKL, RecordDecl* PrevDecl) {
1311
1312 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
1313 C.getTypeDeclType(R, PrevDecl);
1314 return R;
1315}
1316
1317RecordDecl::~RecordDecl() {
1318}
1319
1320void RecordDecl::Destroy(ASTContext& C) {
1321 TagDecl::Destroy(C);
1322}
1323
1324bool RecordDecl::isInjectedClassName() const {
1325 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1326 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1327}
1328
1329/// completeDefinition - Notes that the definition of this type is now
1330/// complete.
1331void RecordDecl::completeDefinition(ASTContext& C) {
1332 assert(!isDefinition() && "Cannot redefine record!");
1333 TagDecl::completeDefinition();
1334}
1335
1336//===----------------------------------------------------------------------===//
1337// BlockDecl Implementation
1338//===----------------------------------------------------------------------===//
1339
1340BlockDecl::~BlockDecl() {
1341}
1342
1343void BlockDecl::Destroy(ASTContext& C) {
1344 if (Body)
1345 Body->Destroy(C);
1346
1347 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1348 (*I)->Destroy(C);
1349
1350 C.Deallocate(ParamInfo);
1351 Decl::Destroy(C);
1352}
1353
1354void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1355 unsigned NParms) {
1356 assert(ParamInfo == 0 && "Already has param info!");
1357
1358 // Zero params -> null pointer.
1359 if (NParms) {
1360 NumParams = NParms;
1361 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1362 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1363 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1364 }
1365}
1366
1367unsigned BlockDecl::getNumParams() const {
1368 return NumParams;
1369}