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