Decl.cpp revision fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240
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/Stmt.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/PrettyPrinter.h"
22#include "clang/Basic/Builtins.h"
23#include "clang/Basic/IdentifierTable.h"
24#include <vector>
25
26using namespace clang;
27
28void Attr::Destroy(ASTContext &C) {
29  if (Next) {
30    Next->Destroy(C);
31    Next = 0;
32  }
33  this->~Attr();
34  C.Deallocate((void*)this);
35}
36
37
38//===----------------------------------------------------------------------===//
39// Decl Allocation/Deallocation Method Implementations
40//===----------------------------------------------------------------------===//
41
42
43TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
44  return new (C) TranslationUnitDecl(C);
45}
46
47NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
48                                     SourceLocation L, IdentifierInfo *Id) {
49  return new (C) NamespaceDecl(DC, L, Id);
50}
51
52void NamespaceDecl::Destroy(ASTContext& C) {
53  // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
54  // together. They are all top-level Decls.
55
56  this->~NamespaceDecl();
57  C.Deallocate((void *)this);
58}
59
60
61ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
62    SourceLocation L, IdentifierInfo *Id, QualType T) {
63  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
64}
65
66const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
67  switch (SC) {
68  case VarDecl::None:          break;
69  case VarDecl::Auto:          return "auto"; break;
70  case VarDecl::Extern:        return "extern"; break;
71  case VarDecl::PrivateExtern: return "__private_extern__"; break;
72  case VarDecl::Register:      return "register"; break;
73  case VarDecl::Static:        return "static"; break;
74  }
75
76  assert(0 && "Invalid storage class");
77  return 0;
78}
79
80ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
81                                 SourceLocation L, IdentifierInfo *Id,
82                                 QualType T, StorageClass S,
83                                 Expr *DefArg) {
84  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
85}
86
87QualType ParmVarDecl::getOriginalType() const {
88  if (const OriginalParmVarDecl *PVD =
89      dyn_cast<OriginalParmVarDecl>(this))
90    return PVD->OriginalType;
91  return getType();
92}
93
94void VarDecl::setInit(ASTContext &C, Expr *I) {
95    if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
96      Eval->~EvaluatedStmt();
97      C.Deallocate(Eval);
98    }
99
100    Init = I;
101  }
102
103bool VarDecl::isExternC(ASTContext &Context) const {
104  if (!Context.getLangOptions().CPlusPlus)
105    return (getDeclContext()->isTranslationUnit() &&
106            getStorageClass() != Static) ||
107      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
108
109  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
110       DC = DC->getParent()) {
111    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
112      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
113        return getStorageClass() != Static;
114
115      break;
116    }
117
118    if (DC->isFunctionOrMethod())
119      return false;
120  }
121
122  return false;
123}
124
125OriginalParmVarDecl *OriginalParmVarDecl::Create(
126                                 ASTContext &C, DeclContext *DC,
127                                 SourceLocation L, IdentifierInfo *Id,
128                                 QualType T, QualType OT, StorageClass S,
129                                 Expr *DefArg) {
130  return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
131}
132
133FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
134                                   SourceLocation L,
135                                   DeclarationName N, QualType T,
136                                   StorageClass S, bool isInline,
137                                   bool hasWrittenPrototype,
138                                   SourceLocation TypeSpecStartLoc) {
139  FunctionDecl *New
140    = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
141                           TypeSpecStartLoc);
142  New->HasWrittenPrototype = hasWrittenPrototype;
143  return New;
144}
145
146BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
147  return new (C) BlockDecl(DC, L);
148}
149
150FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
151                             IdentifierInfo *Id, QualType T, Expr *BW,
152                             bool Mutable) {
153  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
154}
155
156bool FieldDecl::isAnonymousStructOrUnion() const {
157  if (!isImplicit() || getDeclName())
158    return false;
159
160  if (const RecordType *Record = getType()->getAsRecordType())
161    return Record->getDecl()->isAnonymousStructOrUnion();
162
163  return false;
164}
165
166EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
167                                           SourceLocation L,
168                                           IdentifierInfo *Id, QualType T,
169                                           Expr *E, const llvm::APSInt &V) {
170  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
171}
172
173void EnumConstantDecl::Destroy(ASTContext& C) {
174  if (Init) Init->Destroy(C);
175  Decl::Destroy(C);
176}
177
178TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
179                                 SourceLocation L,
180                                 IdentifierInfo *Id, QualType T) {
181  return new (C) TypedefDecl(DC, L, Id, T);
182}
183
184EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
185                           IdentifierInfo *Id,
186                           EnumDecl *PrevDecl) {
187  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
188  C.getTypeDeclType(Enum, PrevDecl);
189  return Enum;
190}
191
192void EnumDecl::Destroy(ASTContext& C) {
193  Decl::Destroy(C);
194}
195
196void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
197  assert(!isDefinition() && "Cannot redefine enums!");
198  IntegerType = NewType;
199  TagDecl::completeDefinition();
200}
201
202FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
203                                           SourceLocation L,
204                                           StringLiteral *Str) {
205  return new (C) FileScopeAsmDecl(DC, L, Str);
206}
207
208//===----------------------------------------------------------------------===//
209// NamedDecl Implementation
210//===----------------------------------------------------------------------===//
211
212std::string NamedDecl::getQualifiedNameAsString() const {
213  std::vector<std::string> Names;
214  std::string QualName;
215  const DeclContext *Ctx = getDeclContext();
216
217  if (Ctx->isFunctionOrMethod())
218    return getNameAsString();
219
220  while (Ctx) {
221    if (Ctx->isFunctionOrMethod())
222      // FIXME: That probably will happen, when D was member of local
223      // scope class/struct/union. How do we handle this case?
224      break;
225
226    if (const ClassTemplateSpecializationDecl *Spec
227          = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
228      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
229      PrintingPolicy Policy(getASTContext().getLangOptions());
230      std::string TemplateArgsStr
231        = TemplateSpecializationType::PrintTemplateArgumentList(
232                                           TemplateArgs.getFlatArgumentList(),
233                                           TemplateArgs.flat_size(),
234                                           Policy);
235      Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
236    } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
237      Names.push_back(ND->getNameAsString());
238    else
239      break;
240
241    Ctx = Ctx->getParent();
242  }
243
244  std::vector<std::string>::reverse_iterator
245    I = Names.rbegin(),
246    End = Names.rend();
247
248  for (; I!=End; ++I)
249    QualName += *I + "::";
250
251  QualName += getNameAsString();
252
253  return QualName;
254}
255
256
257bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
258  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
259
260  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
261  // We want to keep it, unless it nominates same namespace.
262  if (getKind() == Decl::UsingDirective) {
263    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
264           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
265  }
266
267  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
268    // For function declarations, we keep track of redeclarations.
269    return FD->getPreviousDeclaration() == OldD;
270
271  // For function templates, the underlying function declarations are linked.
272  if (const FunctionTemplateDecl *FunctionTemplate
273        = dyn_cast<FunctionTemplateDecl>(this))
274    if (const FunctionTemplateDecl *OldFunctionTemplate
275          = dyn_cast<FunctionTemplateDecl>(OldD))
276      return FunctionTemplate->getTemplatedDecl()
277               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
278
279  // For method declarations, we keep track of redeclarations.
280  if (isa<ObjCMethodDecl>(this))
281    return false;
282
283  // For non-function declarations, if the declarations are of the
284  // same kind then this must be a redeclaration, or semantic analysis
285  // would not have given us the new declaration.
286  return this->getKind() == OldD->getKind();
287}
288
289bool NamedDecl::hasLinkage() const {
290  if (const VarDecl *VD = dyn_cast<VarDecl>(this))
291    return VD->hasExternalStorage() || VD->isFileVarDecl();
292
293  if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
294    return true;
295
296  return false;
297}
298
299NamedDecl *NamedDecl::getUnderlyingDecl() {
300  NamedDecl *ND = this;
301  while (true) {
302    if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
303      ND = UD->getTargetDecl();
304    else if (ObjCCompatibleAliasDecl *AD
305              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
306      return AD->getClassInterface();
307    else
308      return ND;
309  }
310}
311
312//===----------------------------------------------------------------------===//
313// VarDecl Implementation
314//===----------------------------------------------------------------------===//
315
316VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
317                         IdentifierInfo *Id, QualType T, StorageClass S,
318                         SourceLocation TypeSpecStartLoc) {
319  return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
320}
321
322void VarDecl::Destroy(ASTContext& C) {
323  Expr *Init = getInit();
324  if (Init) {
325    Init->Destroy(C);
326    if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
327      Eval->~EvaluatedStmt();
328      C.Deallocate(Eval);
329    }
330  }
331  this->~VarDecl();
332  C.Deallocate((void *)this);
333}
334
335VarDecl::~VarDecl() {
336}
337
338SourceRange VarDecl::getSourceRange() const {
339  if (getInit())
340    return SourceRange(getLocation(), getInit()->getLocEnd());
341  return SourceRange(getLocation(), getLocation());
342}
343
344bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
345  if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
346    return false;
347
348  const VarDecl *Def = 0;
349  return (!getDefinition(Def) &&
350          (getStorageClass() == None || getStorageClass() == Static));
351}
352
353const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
354  Def = this;
355  while (Def && !Def->getInit())
356    Def = Def->getPreviousDeclaration();
357
358  return Def? Def->getInit() : 0;
359}
360
361Decl *VarDecl::getPrimaryDecl() const {
362  const VarDecl *Prim = this;
363  while (Prim->getPreviousDeclaration())
364    Prim = Prim->getPreviousDeclaration();
365
366  return const_cast<VarDecl *>(Prim);
367}
368
369//===----------------------------------------------------------------------===//
370// FunctionDecl Implementation
371//===----------------------------------------------------------------------===//
372
373void FunctionDecl::Destroy(ASTContext& C) {
374  if (Body && Body.isOffset())
375    Body.get(C.getExternalSource())->Destroy(C);
376
377  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
378    (*I)->Destroy(C);
379
380  C.Deallocate(ParamInfo);
381
382  Decl::Destroy(C);
383}
384
385
386Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
387  for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
388    if (FD->Body) {
389      Definition = FD;
390      return FD->Body.get(getASTContext().getExternalSource());
391    }
392  }
393
394  return 0;
395}
396
397Stmt *FunctionDecl::getBodyIfAvailable() const {
398  for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
399    if (FD->Body && !FD->Body.isOffset()) {
400      return FD->Body.get(0);
401    }
402  }
403
404  return 0;
405}
406
407void FunctionDecl::setBody(Stmt *B) {
408  Body = B;
409  if (B)
410    EndRangeLoc = B->getLocEnd();
411}
412
413bool FunctionDecl::isMain() const {
414  return getDeclContext()->getLookupContext()->isTranslationUnit() &&
415    getIdentifier() && getIdentifier()->isStr("main");
416}
417
418bool FunctionDecl::isExternC(ASTContext &Context) const {
419  // In C, any non-static, non-overloadable function has external
420  // linkage.
421  if (!Context.getLangOptions().CPlusPlus)
422    return getStorageClass() != Static && !getAttr<OverloadableAttr>();
423
424  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
425       DC = DC->getParent()) {
426    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
427      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
428        return getStorageClass() != Static &&
429               !getAttr<OverloadableAttr>();
430
431      break;
432    }
433  }
434
435  return false;
436}
437
438bool FunctionDecl::isGlobal() const {
439  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
440    return Method->isStatic();
441
442  if (getStorageClass() == Static)
443    return false;
444
445  for (const DeclContext *DC = getDeclContext();
446       DC->isNamespace();
447       DC = DC->getParent()) {
448    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
449      if (!Namespace->getDeclName())
450        return false;
451      break;
452    }
453  }
454
455  return true;
456}
457
458/// \brief Returns a value indicating whether this function
459/// corresponds to a builtin function.
460///
461/// The function corresponds to a built-in function if it is
462/// declared at translation scope or within an extern "C" block and
463/// its name matches with the name of a builtin. The returned value
464/// will be 0 for functions that do not correspond to a builtin, a
465/// value of type \c Builtin::ID if in the target-independent range
466/// \c [1,Builtin::First), or a target-specific builtin value.
467unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
468  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
469    return 0;
470
471  unsigned BuiltinID = getIdentifier()->getBuiltinID();
472  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
473    return BuiltinID;
474
475  // This function has the name of a known C library
476  // function. Determine whether it actually refers to the C library
477  // function or whether it just has the same name.
478
479  // If this is a static function, it's not a builtin.
480  if (getStorageClass() == Static)
481    return 0;
482
483  // If this function is at translation-unit scope and we're not in
484  // C++, it refers to the C library function.
485  if (!Context.getLangOptions().CPlusPlus &&
486      getDeclContext()->isTranslationUnit())
487    return BuiltinID;
488
489  // If the function is in an extern "C" linkage specification and is
490  // not marked "overloadable", it's the real function.
491  if (isa<LinkageSpecDecl>(getDeclContext()) &&
492      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
493        == LinkageSpecDecl::lang_c &&
494      !getAttr<OverloadableAttr>())
495    return BuiltinID;
496
497  // Not a builtin
498  return 0;
499}
500
501
502/// getNumParams - Return the number of parameters this function must have
503/// based on its FunctionType.  This is the length of the PararmInfo array
504/// after it has been created.
505unsigned FunctionDecl::getNumParams() const {
506  const FunctionType *FT = getType()->getAsFunctionType();
507  if (isa<FunctionNoProtoType>(FT))
508    return 0;
509  return cast<FunctionProtoType>(FT)->getNumArgs();
510
511}
512
513void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
514                             unsigned NumParams) {
515  assert(ParamInfo == 0 && "Already has param info!");
516  assert(NumParams == getNumParams() && "Parameter count mismatch!");
517
518  // Zero params -> null pointer.
519  if (NumParams) {
520    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
521    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
522    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
523
524    // Update source range. The check below allows us to set EndRangeLoc before
525    // setting the parameters.
526    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
527      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
528  }
529}
530
531/// getMinRequiredArguments - Returns the minimum number of arguments
532/// needed to call this function. This may be fewer than the number of
533/// function parameters, if some of the parameters have default
534/// arguments (in C++).
535unsigned FunctionDecl::getMinRequiredArguments() const {
536  unsigned NumRequiredArgs = getNumParams();
537  while (NumRequiredArgs > 0
538         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
539    --NumRequiredArgs;
540
541  return NumRequiredArgs;
542}
543
544bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
545  if (!isInline() || !hasAttr<GNUInlineAttr>())
546    return false;
547
548  for (const FunctionDecl *FD = getPreviousDeclaration(); FD;
549       FD = FD->getPreviousDeclaration()) {
550    if (FD->isInline() && !FD->hasAttr<GNUInlineAttr>())
551      return false;
552  }
553
554  return true;
555}
556
557bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
558  if (!hasActiveGNUInlineAttribute(Context))
559    return false;
560
561  for (const FunctionDecl *FD = this; FD; FD = FD->getPreviousDeclaration())
562    if (FD->getStorageClass() == Extern && FD->hasAttr<GNUInlineAttr>())
563      return true;
564
565  return false;
566}
567
568void
569FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
570  PreviousDeclaration = PrevDecl;
571
572  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
573    FunctionTemplateDecl *PrevFunTmpl
574      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
575    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
576    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
577  }
578}
579
580Decl *FunctionDecl::getPrimaryDecl() const {
581  const FunctionDecl *Prim = this;
582  while (Prim->getPreviousDeclaration())
583    Prim = Prim->getPreviousDeclaration();
584
585  return const_cast<FunctionDecl *>(Prim);
586}
587
588/// getOverloadedOperator - Which C++ overloaded operator this
589/// function represents, if any.
590OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
591  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
592    return getDeclName().getCXXOverloadedOperator();
593  else
594    return OO_None;
595}
596
597FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
598  if (FunctionTemplateSpecializationInfo *Info
599        = TemplateOrSpecialization
600            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
601    return Info->Template.getPointer();
602  }
603  return 0;
604}
605
606const TemplateArgumentList *
607FunctionDecl::getTemplateSpecializationArgs() const {
608  if (FunctionTemplateSpecializationInfo *Info
609      = TemplateOrSpecialization
610      .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
611    return Info->TemplateArguments;
612  }
613  return 0;
614}
615
616void
617FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
618                                                FunctionTemplateDecl *Template,
619                                     const TemplateArgumentList *TemplateArgs,
620                                                void *InsertPos) {
621  FunctionTemplateSpecializationInfo *Info
622    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
623  if (!Info)
624    Info = new (Context) FunctionTemplateSpecializationInfo;
625
626  Info->Function = this;
627  Info->Template.setPointer(Template);
628  Info->Template.setInt(0); // Implicit instantiation, unless told otherwise
629  Info->TemplateArguments = TemplateArgs;
630  TemplateOrSpecialization = Info;
631
632  // Insert this function template specialization into the set of known
633  // function template specialiations.
634  Template->getSpecializations().InsertNode(Info, InsertPos);
635}
636
637bool FunctionDecl::isExplicitSpecialization() const {
638  // FIXME: check this property for explicit specializations of member
639  // functions of class templates.
640  FunctionTemplateSpecializationInfo *Info
641    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
642  if (!Info)
643    return false;
644
645  return Info->isExplicitSpecialization();
646}
647
648void FunctionDecl::setExplicitSpecialization(bool ES) {
649  // FIXME: set this property for explicit specializations of member functions
650  // of class templates.
651  FunctionTemplateSpecializationInfo *Info
652    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
653  if (Info)
654    Info->setExplicitSpecialization(ES);
655}
656
657//===----------------------------------------------------------------------===//
658// TagDecl Implementation
659//===----------------------------------------------------------------------===//
660
661void TagDecl::startDefinition() {
662  TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
663  TagT->decl.setPointer(this);
664  TagT->getAsTagType()->decl.setInt(1);
665}
666
667void TagDecl::completeDefinition() {
668  assert((!TypeForDecl ||
669          TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
670         "Attempt to redefine a tag definition?");
671  IsDefinition = true;
672  TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
673  TagT->decl.setPointer(this);
674  TagT->decl.setInt(0);
675}
676
677TagDecl* TagDecl::getDefinition(ASTContext& C) const {
678  QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
679  TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
680  return D->isDefinition() ? D : 0;
681}
682
683//===----------------------------------------------------------------------===//
684// RecordDecl Implementation
685//===----------------------------------------------------------------------===//
686
687RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
688                       IdentifierInfo *Id)
689  : TagDecl(DK, TK, DC, L, Id) {
690  HasFlexibleArrayMember = false;
691  AnonymousStructOrUnion = false;
692  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
693}
694
695RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
696                               SourceLocation L, IdentifierInfo *Id,
697                               RecordDecl* PrevDecl) {
698
699  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
700  C.getTypeDeclType(R, PrevDecl);
701  return R;
702}
703
704RecordDecl::~RecordDecl() {
705}
706
707void RecordDecl::Destroy(ASTContext& C) {
708  TagDecl::Destroy(C);
709}
710
711bool RecordDecl::isInjectedClassName() const {
712  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
713    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
714}
715
716/// completeDefinition - Notes that the definition of this type is now
717/// complete.
718void RecordDecl::completeDefinition(ASTContext& C) {
719  assert(!isDefinition() && "Cannot redefine record!");
720  TagDecl::completeDefinition();
721}
722
723//===----------------------------------------------------------------------===//
724// BlockDecl Implementation
725//===----------------------------------------------------------------------===//
726
727BlockDecl::~BlockDecl() {
728}
729
730void BlockDecl::Destroy(ASTContext& C) {
731  if (Body)
732    Body->Destroy(C);
733
734  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
735    (*I)->Destroy(C);
736
737  C.Deallocate(ParamInfo);
738  Decl::Destroy(C);
739}
740
741void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
742                          unsigned NParms) {
743  assert(ParamInfo == 0 && "Already has param info!");
744
745  // Zero params -> null pointer.
746  if (NParms) {
747    NumParams = NParms;
748    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
749    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
750    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
751  }
752}
753
754unsigned BlockDecl::getNumParams() const {
755  return NumParams;
756}
757