Decl.h revision e6d2a534851a649485cb087e9dfcaf8a65886858
1//===--- Decl.h - Classes for representing declarations ---------*- C++ -*-===//
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 defines the Decl subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_DECL_H
15#define LLVM_CLANG_AST_DECL_H
16
17#include "clang/AST/APValue.h"
18#include "clang/AST/DeclBase.h"
19#include "clang/AST/Redeclarable.h"
20#include "clang/AST/DeclarationName.h"
21#include "clang/AST/ExternalASTSource.h"
22
23namespace clang {
24class CXXTemporary;
25class Expr;
26class FunctionTemplateDecl;
27class Stmt;
28class CompoundStmt;
29class StringLiteral;
30class TemplateArgumentList;
31class MemberSpecializationInfo;
32class FunctionTemplateSpecializationInfo;
33class TypeLoc;
34
35/// \brief A container of type source information.
36///
37/// A client can read the relevant info using TypeLoc wrappers, e.g:
38/// @code
39/// TypeLoc TL = TypeSourceInfo->getTypeLoc();
40/// if (PointerLoc *PL = dyn_cast<PointerLoc>(&TL))
41///   PL->getStarLoc().print(OS, SrcMgr);
42/// @endcode
43///
44class TypeSourceInfo {
45  QualType Ty;
46  // Contains a memory block after the class, used for type source information,
47  // allocated by ASTContext.
48  friend class ASTContext;
49  TypeSourceInfo(QualType ty) : Ty(ty) { }
50public:
51  /// \brief Return the type wrapped by this type source info.
52  QualType getType() const { return Ty; }
53
54  /// \brief Return the TypeLoc wrapper for the type source info.
55  TypeLoc getTypeLoc() const;
56};
57
58/// TranslationUnitDecl - The top declaration context.
59class TranslationUnitDecl : public Decl, public DeclContext {
60  ASTContext &Ctx;
61
62  /// The (most recently entered) anonymous namespace for this
63  /// translation unit, if one has been created.
64  NamespaceDecl *AnonymousNamespace;
65
66  explicit TranslationUnitDecl(ASTContext &ctx)
67    : Decl(TranslationUnit, 0, SourceLocation()),
68      DeclContext(TranslationUnit),
69      Ctx(ctx), AnonymousNamespace(0) {}
70public:
71  ASTContext &getASTContext() const { return Ctx; }
72
73  NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; }
74  void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; }
75
76  static TranslationUnitDecl *Create(ASTContext &C);
77  // Implement isa/cast/dyncast/etc.
78  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
79  static bool classof(const TranslationUnitDecl *D) { return true; }
80  static bool classofKind(Kind K) { return K == TranslationUnit; }
81  static DeclContext *castToDeclContext(const TranslationUnitDecl *D) {
82    return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D));
83  }
84  static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
85    return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC));
86  }
87};
88
89/// NamedDecl - This represents a decl with a name.  Many decls have names such
90/// as ObjCMethodDecl, but not @class, etc.
91class NamedDecl : public Decl {
92  /// Name - The name of this declaration, which is typically a normal
93  /// identifier but may also be a special kind of name (C++
94  /// constructor, Objective-C selector, etc.)
95  DeclarationName Name;
96
97protected:
98  NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N)
99    : Decl(DK, DC, L), Name(N) { }
100
101public:
102  /// getIdentifier - Get the identifier that names this declaration,
103  /// if there is one. This will return NULL if this declaration has
104  /// no name (e.g., for an unnamed class) or if the name is a special
105  /// name (C++ constructor, Objective-C selector, etc.).
106  IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); }
107
108  /// getName - Get the name of identifier for this declaration as a StringRef.
109  /// This requires that the declaration have a name and that it be a simple
110  /// identifier.
111  llvm::StringRef getName() const {
112    assert(Name.isIdentifier() && "Name is not a simple identifier");
113    return getIdentifier() ? getIdentifier()->getName() : "";
114  }
115
116  /// getNameAsCString - Get the name of identifier for this declaration as a
117  /// C string (const char*).  This requires that the declaration have a name
118  /// and that it be a simple identifier.
119  //
120  // FIXME: Deprecated, move clients to getName().
121  const char *getNameAsCString() const {
122    assert(Name.isIdentifier() && "Name is not a simple identifier");
123    return getIdentifier() ? getIdentifier()->getNameStart() : "";
124  }
125
126  /// getNameAsString - Get a human-readable name for the declaration, even if
127  /// it is one of the special kinds of names (C++ constructor, Objective-C
128  /// selector, etc).  Creating this name requires expensive string
129  /// manipulation, so it should be called only when performance doesn't matter.
130  /// For simple declarations, getNameAsCString() should suffice.
131  //
132  // FIXME: This function should be renamed to indicate that it is not just an
133  // alternate form of getName(), and clients should move as appropriate.
134  //
135  // FIXME: Deprecated, move clients to getName().
136  std::string getNameAsString() const { return Name.getAsString(); }
137
138  /// getDeclName - Get the actual, stored name of the declaration,
139  /// which may be a special name.
140  DeclarationName getDeclName() const { return Name; }
141
142  /// \brief Set the name of this declaration.
143  void setDeclName(DeclarationName N) { Name = N; }
144
145  /// getQualifiedNameAsString - Returns human-readable qualified name for
146  /// declaration, like A::B::i, for i being member of namespace A::B.
147  /// If declaration is not member of context which can be named (record,
148  /// namespace), it will return same result as getNameAsString().
149  /// Creating this name is expensive, so it should be called only when
150  /// performance doesn't matter.
151  std::string getQualifiedNameAsString() const;
152  std::string getQualifiedNameAsString(const PrintingPolicy &Policy) const;
153
154  /// getNameForDiagnostic - Appends a human-readable name for this
155  /// declaration into the given string.
156  ///
157  /// This is the method invoked by Sema when displaying a NamedDecl
158  /// in a diagnostic.  It does not necessarily produce the same
159  /// result as getNameAsString(); for example, class template
160  /// specializations are printed with their template arguments.
161  ///
162  /// TODO: use an API that doesn't require so many temporary strings
163  virtual void getNameForDiagnostic(std::string &S,
164                                    const PrintingPolicy &Policy,
165                                    bool Qualified) const {
166    if (Qualified)
167      S += getQualifiedNameAsString(Policy);
168    else
169      S += getNameAsString();
170  }
171
172  /// declarationReplaces - Determine whether this declaration, if
173  /// known to be well-formed within its context, will replace the
174  /// declaration OldD if introduced into scope. A declaration will
175  /// replace another declaration if, for example, it is a
176  /// redeclaration of the same variable or function, but not if it is
177  /// a declaration of a different kind (function vs. class) or an
178  /// overloaded function.
179  bool declarationReplaces(NamedDecl *OldD) const;
180
181  /// \brief Determine whether this declaration has linkage.
182  bool hasLinkage() const;
183
184  /// \brief Determine whether this declaration is a C++ class member.
185  bool isCXXClassMember() const {
186    const DeclContext *DC = getDeclContext();
187
188    // C++0x [class.mem]p1:
189    //   The enumerators of an unscoped enumeration defined in
190    //   the class are members of the class.
191    // FIXME: support C++0x scoped enumerations.
192    if (isa<EnumDecl>(DC))
193      DC = DC->getParent();
194
195    return DC->isRecord();
196  }
197
198  /// \brief Describes the different kinds of linkage
199  /// (C++ [basic.link], C99 6.2.2) that an entity may have.
200  enum Linkage {
201    /// \brief No linkage, which means that the entity is unique and
202    /// can only be referred to from within its scope.
203    NoLinkage = 0,
204
205    /// \brief Internal linkage, which indicates that the entity can
206    /// be referred to from within the translation unit (but not other
207    /// translation units).
208    InternalLinkage,
209
210    /// \brief External linkage, which indicates that the entity can
211    /// be referred to from other translation units.
212    ExternalLinkage
213  };
214
215  /// \brief Determine what kind of linkage this entity has.
216  Linkage getLinkage() const;
217
218  /// \brief Looks through UsingDecls and ObjCCompatibleAliasDecls for
219  /// the underlying named decl.
220  NamedDecl *getUnderlyingDecl();
221  const NamedDecl *getUnderlyingDecl() const {
222    return const_cast<NamedDecl*>(this)->getUnderlyingDecl();
223  }
224
225  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
226  static bool classof(const NamedDecl *D) { return true; }
227  static bool classofKind(Kind K) { return K >= NamedFirst && K <= NamedLast; }
228};
229
230/// NamespaceDecl - Represent a C++ namespace.
231class NamespaceDecl : public NamedDecl, public DeclContext {
232  SourceLocation LBracLoc, RBracLoc;
233
234  // For extended namespace definitions:
235  //
236  // namespace A { int x; }
237  // namespace A { int y; }
238  //
239  // there will be one NamespaceDecl for each declaration.
240  // NextNamespace points to the next extended declaration.
241  // OrigNamespace points to the original namespace declaration.
242  // OrigNamespace of the first namespace decl points to itself.
243  NamespaceDecl *OrigNamespace, *NextNamespace;
244
245  // The (most recently entered) anonymous namespace inside this
246  // namespace.
247  NamespaceDecl *AnonymousNamespace;
248
249  NamespaceDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id)
250    : NamedDecl(Namespace, DC, L, Id), DeclContext(Namespace) {
251    OrigNamespace = this;
252    NextNamespace = 0;
253    AnonymousNamespace = 0;
254  }
255public:
256  static NamespaceDecl *Create(ASTContext &C, DeclContext *DC,
257                               SourceLocation L, IdentifierInfo *Id);
258
259  virtual void Destroy(ASTContext& C);
260
261  // \brief Returns true if this is an anonymous namespace declaration.
262  //
263  // For example:
264  //   namespace {
265  //     ...
266  //   };
267  // q.v. C++ [namespace.unnamed]
268  bool isAnonymousNamespace() const {
269    return !getIdentifier();
270  }
271
272  NamespaceDecl *getNextNamespace() { return NextNamespace; }
273  const NamespaceDecl *getNextNamespace() const { return NextNamespace; }
274  void setNextNamespace(NamespaceDecl *ND) { NextNamespace = ND; }
275
276  NamespaceDecl *getOriginalNamespace() const {
277    return OrigNamespace;
278  }
279  void setOriginalNamespace(NamespaceDecl *ND) { OrigNamespace = ND; }
280
281  NamespaceDecl *getAnonymousNamespace() const {
282    return AnonymousNamespace;
283  }
284
285  void setAnonymousNamespace(NamespaceDecl *D) {
286    assert(D->isAnonymousNamespace());
287    assert(D->getParent() == this);
288    AnonymousNamespace = D;
289  }
290
291  virtual NamespaceDecl *getCanonicalDecl() { return OrigNamespace; }
292  const NamespaceDecl *getCanonicalDecl() const { return OrigNamespace; }
293
294  virtual SourceRange getSourceRange() const {
295    return SourceRange(getLocation(), RBracLoc);
296  }
297
298  SourceLocation getLBracLoc() const { return LBracLoc; }
299  SourceLocation getRBracLoc() const { return RBracLoc; }
300  void setLBracLoc(SourceLocation LBrace) { LBracLoc = LBrace; }
301  void setRBracLoc(SourceLocation RBrace) { RBracLoc = RBrace; }
302
303  // Implement isa/cast/dyncast/etc.
304  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
305  static bool classof(const NamespaceDecl *D) { return true; }
306  static bool classofKind(Kind K) { return K == Namespace; }
307  static DeclContext *castToDeclContext(const NamespaceDecl *D) {
308    return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D));
309  }
310  static NamespaceDecl *castFromDeclContext(const DeclContext *DC) {
311    return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC));
312  }
313};
314
315/// ValueDecl - Represent the declaration of a variable (in which case it is
316/// an lvalue) a function (in which case it is a function designator) or
317/// an enum constant.
318class ValueDecl : public NamedDecl {
319  QualType DeclType;
320
321protected:
322  ValueDecl(Kind DK, DeclContext *DC, SourceLocation L,
323            DeclarationName N, QualType T)
324    : NamedDecl(DK, DC, L, N), DeclType(T) {}
325public:
326  QualType getType() const { return DeclType; }
327  void setType(QualType newType) { DeclType = newType; }
328
329  // Implement isa/cast/dyncast/etc.
330  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
331  static bool classof(const ValueDecl *D) { return true; }
332  static bool classofKind(Kind K) { return K >= ValueFirst && K <= ValueLast; }
333};
334
335/// \brief Represents a ValueDecl that came out of a declarator.
336/// Contains type source information through TypeSourceInfo.
337class DeclaratorDecl : public ValueDecl {
338  TypeSourceInfo *DeclInfo;
339
340protected:
341  DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L,
342                 DeclarationName N, QualType T, TypeSourceInfo *TInfo)
343    : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo) {}
344
345public:
346  TypeSourceInfo *getTypeSourceInfo() const { return DeclInfo; }
347  void setTypeSourceInfo(TypeSourceInfo *TInfo) { DeclInfo = TInfo; }
348
349  SourceLocation getTypeSpecStartLoc() const;
350
351  // Implement isa/cast/dyncast/etc.
352  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
353  static bool classof(const DeclaratorDecl *D) { return true; }
354  static bool classofKind(Kind K) {
355    return K >= DeclaratorFirst && K <= DeclaratorLast;
356  }
357};
358
359/// \brief Structure used to store a statement, the constant value to
360/// which it was evaluated (if any), and whether or not the statement
361/// is an integral constant expression (if known).
362struct EvaluatedStmt {
363  EvaluatedStmt() : WasEvaluated(false), IsEvaluating(false), CheckedICE(false),
364                    CheckingICE(false), IsICE(false) { }
365
366  /// \brief Whether this statement was already evaluated.
367  bool WasEvaluated : 1;
368
369  /// \brief Whether this statement is being evaluated.
370  bool IsEvaluating : 1;
371
372  /// \brief Whether we already checked whether this statement was an
373  /// integral constant expression.
374  bool CheckedICE : 1;
375
376  /// \brief Whether we are checking whether this statement is an
377  /// integral constant expression.
378  bool CheckingICE : 1;
379
380  /// \brief Whether this statement is an integral constant
381  /// expression. Only valid if CheckedICE is true.
382  bool IsICE : 1;
383
384  Stmt *Value;
385  APValue Evaluated;
386};
387
388// \brief Describes the kind of template specialization that a
389// particular template specialization declaration represents.
390enum TemplateSpecializationKind {
391  /// This template specialization was formed from a template-id but
392  /// has not yet been declared, defined, or instantiated.
393  TSK_Undeclared = 0,
394  /// This template specialization was implicitly instantiated from a
395  /// template. (C++ [temp.inst]).
396  TSK_ImplicitInstantiation,
397  /// This template specialization was declared or defined by an
398  /// explicit specialization (C++ [temp.expl.spec]) or partial
399  /// specialization (C++ [temp.class.spec]).
400  TSK_ExplicitSpecialization,
401  /// This template specialization was instantiated from a template
402  /// due to an explicit instantiation declaration request
403  /// (C++0x [temp.explicit]).
404  TSK_ExplicitInstantiationDeclaration,
405  /// This template specialization was instantiated from a template
406  /// due to an explicit instantiation definition request
407  /// (C++ [temp.explicit]).
408  TSK_ExplicitInstantiationDefinition
409};
410
411/// VarDecl - An instance of this class is created to represent a variable
412/// declaration or definition.
413class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
414public:
415  enum StorageClass {
416    None, Auto, Register, Extern, Static, PrivateExtern
417  };
418
419  /// getStorageClassSpecifierString - Return the string used to
420  /// specify the storage class \arg SC.
421  ///
422  /// It is illegal to call this function with SC == None.
423  static const char *getStorageClassSpecifierString(StorageClass SC);
424
425protected:
426  /// \brief Placeholder type used in Init to denote an unparsed C++ default
427  /// argument.
428  struct UnparsedDefaultArgument;
429
430  /// \brief Placeholder type used in Init to denote an uninstantiated C++
431  /// default argument.
432  struct UninstantiatedDefaultArgument;
433
434  typedef llvm::PointerUnion4<Stmt *, EvaluatedStmt *,
435                              UnparsedDefaultArgument *,
436                              UninstantiatedDefaultArgument *> InitType;
437
438  /// \brief The initializer for this variable or, for a ParmVarDecl, the
439  /// C++ default argument.
440  mutable InitType Init;
441
442private:
443  // FIXME: This can be packed into the bitfields in Decl.
444  unsigned SClass : 3;
445  bool ThreadSpecified : 1;
446  bool HasCXXDirectInit : 1;
447
448  /// DeclaredInCondition - Whether this variable was declared in a
449  /// condition, e.g., if (int x = foo()) { ... }.
450  bool DeclaredInCondition : 1;
451
452  friend class StmtIteratorBase;
453protected:
454  VarDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
455          QualType T, TypeSourceInfo *TInfo, StorageClass SC)
456    : DeclaratorDecl(DK, DC, L, Id, T, TInfo), Init(),
457      ThreadSpecified(false), HasCXXDirectInit(false),
458      DeclaredInCondition(false) {
459    SClass = SC;
460  }
461
462  typedef Redeclarable<VarDecl> redeclarable_base;
463  virtual VarDecl *getNextRedeclaration() { return RedeclLink.getNext(); }
464
465public:
466  typedef redeclarable_base::redecl_iterator redecl_iterator;
467  redecl_iterator redecls_begin() const {
468    return redeclarable_base::redecls_begin();
469  }
470  redecl_iterator redecls_end() const {
471    return redeclarable_base::redecls_end();
472  }
473
474  static VarDecl *Create(ASTContext &C, DeclContext *DC,
475                         SourceLocation L, IdentifierInfo *Id,
476                         QualType T, TypeSourceInfo *TInfo, StorageClass S);
477
478  virtual void Destroy(ASTContext& C);
479  virtual ~VarDecl();
480
481  virtual SourceRange getSourceRange() const;
482
483  StorageClass getStorageClass() const { return (StorageClass)SClass; }
484  void setStorageClass(StorageClass SC) { SClass = SC; }
485
486  void setThreadSpecified(bool T) { ThreadSpecified = T; }
487  bool isThreadSpecified() const {
488    return ThreadSpecified;
489  }
490
491  /// hasLocalStorage - Returns true if a variable with function scope
492  ///  is a non-static local variable.
493  bool hasLocalStorage() const {
494    if (getStorageClass() == None)
495      return !isFileVarDecl();
496
497    // Return true for:  Auto, Register.
498    // Return false for: Extern, Static, PrivateExtern.
499
500    return getStorageClass() <= Register;
501  }
502
503  /// hasExternStorage - Returns true if a variable has extern or
504  /// __private_extern__ storage.
505  bool hasExternalStorage() const {
506    return getStorageClass() == Extern || getStorageClass() == PrivateExtern;
507  }
508
509  /// hasGlobalStorage - Returns true for all variables that do not
510  ///  have local storage.  This includs all global variables as well
511  ///  as static variables declared within a function.
512  bool hasGlobalStorage() const { return !hasLocalStorage(); }
513
514  /// \brief Determines whether this variable is a variable with
515  /// external, C linkage.
516  bool isExternC() const;
517
518  /// isBlockVarDecl - Returns true for local variable declarations.  Note that
519  /// this includes static variables inside of functions.
520  ///
521  ///   void foo() { int x; static int y; extern int z; }
522  ///
523  bool isBlockVarDecl() const {
524    if (getKind() != Decl::Var)
525      return false;
526    if (const DeclContext *DC = getDeclContext())
527      return DC->getLookupContext()->isFunctionOrMethod();
528    return false;
529  }
530
531  /// \brief Determines whether this is a static data member.
532  ///
533  /// This will only be true in C++, and applies to, e.g., the
534  /// variable 'x' in:
535  /// \code
536  /// struct S {
537  ///   static int x;
538  /// };
539  /// \endcode
540  bool isStaticDataMember() const {
541    return getDeclContext()->isRecord();
542  }
543
544  virtual VarDecl *getCanonicalDecl();
545  const VarDecl *getCanonicalDecl() const {
546    return const_cast<VarDecl*>(this)->getCanonicalDecl();
547  }
548
549  /// \brief Retrieve the definition of this variable, which may come
550  /// from a previous declaration. Def will be set to the VarDecl that
551  /// contains the initializer, and the result will be that
552  /// initializer.
553  const Expr *getDefinition(const VarDecl *&Def) const;
554
555  const Expr *getDefinition() const {
556    const VarDecl* Definition;
557    return getDefinition(Definition);
558  }
559
560  /// \brief Determine whether this is or was instantiated from an out-of-line
561  /// definition of a static data member.
562  bool isOutOfLine() const;
563
564  /// \brief If this is a static data member, find its out-of-line definition.
565  VarDecl *getOutOfLineDefinition();
566
567  /// isFileVarDecl - Returns true for file scoped variable declaration.
568  bool isFileVarDecl() const {
569    if (getKind() != Decl::Var)
570      return false;
571    if (const DeclContext *Ctx = getDeclContext()) {
572      Ctx = Ctx->getLookupContext();
573      if (isa<TranslationUnitDecl>(Ctx) || isa<NamespaceDecl>(Ctx) )
574        return true;
575    }
576    if (isStaticDataMember())
577      return true;
578
579    return false;
580  }
581
582  /// \brief Determine whether this is a tentative definition of a
583  /// variable in C.
584  bool isTentativeDefinition(ASTContext &Context) const;
585
586  const Expr *getInit() const {
587    if (Init.isNull())
588      return 0;
589
590    const Stmt *S = Init.dyn_cast<Stmt *>();
591    if (!S) {
592      if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
593        S = ES->Value;
594    }
595    return (const Expr*) S;
596  }
597  Expr *getInit() {
598    if (Init.isNull())
599      return 0;
600
601    Stmt *S = Init.dyn_cast<Stmt *>();
602    if (!S) {
603      if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
604        S = ES->Value;
605    }
606
607    return (Expr*) S;
608  }
609
610  /// \brief Retrieve the address of the initializer expression.
611  Stmt **getInitAddress() {
612    if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
613      return &ES->Value;
614
615    // This union hack tip-toes around strict-aliasing rules.
616    union {
617      InitType *InitPtr;
618      Stmt **StmtPtr;
619    };
620
621    InitPtr = &Init;
622    return StmtPtr;
623  }
624
625  void setInit(ASTContext &C, Expr *I);
626
627  EvaluatedStmt *EnsureEvaluatedStmt() const {
628    EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
629    if (!Eval) {
630      Stmt *S = Init.get<Stmt *>();
631      Eval = new (getASTContext()) EvaluatedStmt;
632      Eval->Value = S;
633      Init = Eval;
634    }
635    return Eval;
636  }
637
638  /// \brief Check whether we are in the process of checking whether the
639  /// initializer can be evaluated.
640  bool isEvaluatingValue() const {
641    if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
642      return Eval->IsEvaluating;
643
644    return false;
645  }
646
647  /// \brief Note that we now are checking whether the initializer can be
648  /// evaluated.
649  void setEvaluatingValue() const {
650    EvaluatedStmt *Eval = EnsureEvaluatedStmt();
651    Eval->IsEvaluating = true;
652  }
653
654  /// \brief Note that constant evaluation has computed the given
655  /// value for this variable's initializer.
656  void setEvaluatedValue(const APValue &Value) const {
657    EvaluatedStmt *Eval = EnsureEvaluatedStmt();
658    Eval->IsEvaluating = false;
659    Eval->WasEvaluated = true;
660    Eval->Evaluated = Value;
661  }
662
663  /// \brief Return the already-evaluated value of this variable's
664  /// initializer, or NULL if the value is not yet known. Returns pointer
665  /// to untyped APValue if the value could not be evaluated.
666  APValue *getEvaluatedValue() const {
667    if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
668      if (Eval->WasEvaluated)
669        return &Eval->Evaluated;
670
671    return 0;
672  }
673
674  /// \brief Determines whether it is already known whether the
675  /// initializer is an integral constant expression or not.
676  bool isInitKnownICE() const {
677    if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
678      return Eval->CheckedICE;
679
680    return false;
681  }
682
683  /// \brief Determines whether the initializer is an integral
684  /// constant expression.
685  ///
686  /// \pre isInitKnownICE()
687  bool isInitICE() const {
688    assert(isInitKnownICE() &&
689           "Check whether we already know that the initializer is an ICE");
690    return Init.get<EvaluatedStmt *>()->IsICE;
691  }
692
693  /// \brief Check whether we are in the process of checking the initializer
694  /// is an integral constant expression.
695  bool isCheckingICE() const {
696    if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
697      return Eval->CheckingICE;
698
699    return false;
700  }
701
702  /// \brief Note that we now are checking whether the initializer is an
703  /// integral constant expression.
704  void setCheckingICE() const {
705    EvaluatedStmt *Eval = EnsureEvaluatedStmt();
706    Eval->CheckingICE = true;
707  }
708
709  /// \brief Note that we now know whether the initializer is an
710  /// integral constant expression.
711  void setInitKnownICE(bool IsICE) const {
712    EvaluatedStmt *Eval = EnsureEvaluatedStmt();
713    Eval->CheckingICE = false;
714    Eval->CheckedICE = true;
715    Eval->IsICE = IsICE;
716  }
717
718  void setCXXDirectInitializer(bool T) { HasCXXDirectInit = T; }
719
720  /// hasCXXDirectInitializer - If true, the initializer was a direct
721  /// initializer, e.g: "int x(1);". The Init expression will be the expression
722  /// inside the parens or a "ClassType(a,b,c)" class constructor expression for
723  /// class types. Clients can distinguish between "int x(1);" and "int x=1;"
724  /// by checking hasCXXDirectInitializer.
725  ///
726  bool hasCXXDirectInitializer() const {
727    return HasCXXDirectInit;
728  }
729
730  /// isDeclaredInCondition - Whether this variable was declared as
731  /// part of a condition in an if/switch/while statement, e.g.,
732  /// @code
733  /// if (int x = foo()) { ... }
734  /// @endcode
735  bool isDeclaredInCondition() const {
736    return DeclaredInCondition;
737  }
738  void setDeclaredInCondition(bool InCondition) {
739    DeclaredInCondition = InCondition;
740  }
741
742  /// \brief If this variable is an instantiated static data member of a
743  /// class template specialization, returns the templated static data member
744  /// from which it was instantiated.
745  VarDecl *getInstantiatedFromStaticDataMember() const;
746
747  /// \brief If this variable is a static data member, determine what kind of
748  /// template specialization or instantiation this is.
749  TemplateSpecializationKind getTemplateSpecializationKind() const;
750
751  /// \brief If this variable is an instantiation of a static data member of a
752  /// class template specialization, retrieves the member specialization
753  /// information.
754  MemberSpecializationInfo *getMemberSpecializationInfo() const;
755
756  /// \brief For a static data member that was instantiated from a static
757  /// data member of a class template, set the template specialiation kind.
758  void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
759                        SourceLocation PointOfInstantiation = SourceLocation());
760
761  // Implement isa/cast/dyncast/etc.
762  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
763  static bool classof(const VarDecl *D) { return true; }
764  static bool classofKind(Kind K) { return K >= VarFirst && K <= VarLast; }
765};
766
767class ImplicitParamDecl : public VarDecl {
768protected:
769  ImplicitParamDecl(Kind DK, DeclContext *DC, SourceLocation L,
770                    IdentifierInfo *Id, QualType Tw)
771    : VarDecl(DK, DC, L, Id, Tw, /*TInfo=*/0, VarDecl::None) {}
772public:
773  static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC,
774                                   SourceLocation L, IdentifierInfo *Id,
775                                   QualType T);
776  // Implement isa/cast/dyncast/etc.
777  static bool classof(const ImplicitParamDecl *D) { return true; }
778  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
779  static bool classofKind(Kind K) { return K == ImplicitParam; }
780};
781
782/// ParmVarDecl - Represent a parameter to a function.
783class ParmVarDecl : public VarDecl {
784  // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
785  /// FIXME: Also can be paced into the bitfields in Decl.
786  /// in, inout, etc.
787  unsigned objcDeclQualifier : 6;
788
789protected:
790  ParmVarDecl(Kind DK, DeclContext *DC, SourceLocation L,
791              IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
792              StorageClass S, Expr *DefArg)
793  : VarDecl(DK, DC, L, Id, T, TInfo, S), objcDeclQualifier(OBJC_TQ_None) {
794    setDefaultArg(DefArg);
795  }
796
797public:
798  static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
799                             SourceLocation L,IdentifierInfo *Id,
800                             QualType T, TypeSourceInfo *TInfo,
801                             StorageClass S, Expr *DefArg);
802
803  ObjCDeclQualifier getObjCDeclQualifier() const {
804    return ObjCDeclQualifier(objcDeclQualifier);
805  }
806  void setObjCDeclQualifier(ObjCDeclQualifier QTVal) {
807    objcDeclQualifier = QTVal;
808  }
809
810  Expr *getDefaultArg();
811  const Expr *getDefaultArg() const {
812    return const_cast<ParmVarDecl *>(this)->getDefaultArg();
813  }
814
815  void setDefaultArg(Expr *defarg) {
816    Init = reinterpret_cast<Stmt *>(defarg);
817  }
818
819  unsigned getNumDefaultArgTemporaries() const;
820  CXXTemporary *getDefaultArgTemporary(unsigned i);
821  const CXXTemporary *getDefaultArgTemporary(unsigned i) const {
822    return const_cast<ParmVarDecl *>(this)->getDefaultArgTemporary(i);
823  }
824
825  /// \brief Retrieve the source range that covers the entire default
826  /// argument.
827  SourceRange getDefaultArgRange() const;
828  void setUninstantiatedDefaultArg(Expr *arg) {
829    Init = reinterpret_cast<UninstantiatedDefaultArgument *>(arg);
830  }
831  Expr *getUninstantiatedDefaultArg() {
832    return (Expr *)Init.get<UninstantiatedDefaultArgument *>();
833  }
834  const Expr *getUninstantiatedDefaultArg() const {
835    return (const Expr *)Init.get<UninstantiatedDefaultArgument *>();
836  }
837
838  /// hasDefaultArg - Determines whether this parameter has a default argument,
839  /// either parsed or not.
840  bool hasDefaultArg() const {
841    return getInit() || hasUnparsedDefaultArg() ||
842      hasUninstantiatedDefaultArg();
843  }
844
845  /// hasUnparsedDefaultArg - Determines whether this parameter has a
846  /// default argument that has not yet been parsed. This will occur
847  /// during the processing of a C++ class whose member functions have
848  /// default arguments, e.g.,
849  /// @code
850  ///   class X {
851  ///   public:
852  ///     void f(int x = 17); // x has an unparsed default argument now
853  ///   }; // x has a regular default argument now
854  /// @endcode
855  bool hasUnparsedDefaultArg() const {
856    return Init.is<UnparsedDefaultArgument*>();
857  }
858
859  bool hasUninstantiatedDefaultArg() const {
860    return Init.is<UninstantiatedDefaultArgument*>();
861  }
862
863  /// setUnparsedDefaultArg - Specify that this parameter has an
864  /// unparsed default argument. The argument will be replaced with a
865  /// real default argument via setDefaultArg when the class
866  /// definition enclosing the function declaration that owns this
867  /// default argument is completed.
868  void setUnparsedDefaultArg() {
869    Init = (UnparsedDefaultArgument *)0;
870  }
871
872  QualType getOriginalType() const {
873    if (getTypeSourceInfo())
874      return getTypeSourceInfo()->getType();
875    return getType();
876  }
877
878  /// setOwningFunction - Sets the function declaration that owns this
879  /// ParmVarDecl. Since ParmVarDecls are often created before the
880  /// FunctionDecls that own them, this routine is required to update
881  /// the DeclContext appropriately.
882  void setOwningFunction(DeclContext *FD) { setDeclContext(FD); }
883
884  // Implement isa/cast/dyncast/etc.
885  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
886  static bool classof(const ParmVarDecl *D) { return true; }
887  static bool classofKind(Kind K) { return K == ParmVar; }
888};
889
890/// FunctionDecl - An instance of this class is created to represent a
891/// function declaration or definition.
892///
893/// Since a given function can be declared several times in a program,
894/// there may be several FunctionDecls that correspond to that
895/// function. Only one of those FunctionDecls will be found when
896/// traversing the list of declarations in the context of the
897/// FunctionDecl (e.g., the translation unit); this FunctionDecl
898/// contains all of the information known about the function. Other,
899/// previous declarations of the function are available via the
900/// getPreviousDeclaration() chain.
901class FunctionDecl : public DeclaratorDecl, public DeclContext,
902                     public Redeclarable<FunctionDecl> {
903public:
904  enum StorageClass {
905    None, Extern, Static, PrivateExtern
906  };
907
908private:
909  /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
910  /// parameters of this function.  This is null if a prototype or if there are
911  /// no formals.
912  ParmVarDecl **ParamInfo;
913
914  LazyDeclStmtPtr Body;
915
916  // FIXME: This can be packed into the bitfields in Decl.
917  // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum
918  unsigned SClass : 2;
919  bool IsInline : 1;
920  bool IsVirtualAsWritten : 1;
921  bool IsPure : 1;
922  bool HasInheritedPrototype : 1;
923  bool HasWrittenPrototype : 1;
924  bool IsDeleted : 1;
925  bool IsTrivial : 1; // sunk from CXXMethodDecl
926  bool IsCopyAssignment : 1;  // sunk from CXXMethodDecl
927  bool HasImplicitReturnZero : 1;
928
929  /// \brief End part of this FunctionDecl's source range.
930  ///
931  /// We could compute the full range in getSourceRange(). However, when we're
932  /// dealing with a function definition deserialized from a PCH/AST file,
933  /// we can only compute the full range once the function body has been
934  /// de-serialized, so it's far better to have the (sometimes-redundant)
935  /// EndRangeLoc.
936  SourceLocation EndRangeLoc;
937
938  /// \brief The template or declaration that this declaration
939  /// describes or was instantiated from, respectively.
940  ///
941  /// For non-templates, this value will be NULL. For function
942  /// declarations that describe a function template, this will be a
943  /// pointer to a FunctionTemplateDecl. For member functions
944  /// of class template specializations, this will be a MemberSpecializationInfo
945  /// pointer containing information about the specialization.
946  /// For function template specializations, this will be a
947  /// FunctionTemplateSpecializationInfo, which contains information about
948  /// the template being specialized and the template arguments involved in
949  /// that specialization.
950  llvm::PointerUnion3<FunctionTemplateDecl *,
951                      MemberSpecializationInfo *,
952                      FunctionTemplateSpecializationInfo *>
953    TemplateOrSpecialization;
954
955protected:
956  FunctionDecl(Kind DK, DeclContext *DC, SourceLocation L,
957               DeclarationName N, QualType T, TypeSourceInfo *TInfo,
958               StorageClass S, bool isInline)
959    : DeclaratorDecl(DK, DC, L, N, T, TInfo),
960      DeclContext(DK),
961      ParamInfo(0), Body(),
962      SClass(S), IsInline(isInline),
963      IsVirtualAsWritten(false), IsPure(false), HasInheritedPrototype(false),
964      HasWrittenPrototype(true), IsDeleted(false), IsTrivial(false),
965      IsCopyAssignment(false),
966      HasImplicitReturnZero(false),
967      EndRangeLoc(L), TemplateOrSpecialization() {}
968
969  virtual ~FunctionDecl() {}
970  virtual void Destroy(ASTContext& C);
971
972  typedef Redeclarable<FunctionDecl> redeclarable_base;
973  virtual FunctionDecl *getNextRedeclaration() { return RedeclLink.getNext(); }
974
975public:
976  typedef redeclarable_base::redecl_iterator redecl_iterator;
977  redecl_iterator redecls_begin() const {
978    return redeclarable_base::redecls_begin();
979  }
980  redecl_iterator redecls_end() const {
981    return redeclarable_base::redecls_end();
982  }
983
984  static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
985                              DeclarationName N, QualType T,
986                              TypeSourceInfo *TInfo,
987                              StorageClass S = None, bool isInline = false,
988                              bool hasWrittenPrototype = true);
989
990  virtual void getNameForDiagnostic(std::string &S,
991                                    const PrintingPolicy &Policy,
992                                    bool Qualified) const;
993
994  virtual SourceRange getSourceRange() const {
995    return SourceRange(getLocation(), EndRangeLoc);
996  }
997  void setLocEnd(SourceLocation E) {
998    EndRangeLoc = E;
999  }
1000
1001  /// getBody - Retrieve the body (definition) of the function. The
1002  /// function body might be in any of the (re-)declarations of this
1003  /// function. The variant that accepts a FunctionDecl pointer will
1004  /// set that function declaration to the actual declaration
1005  /// containing the body (if there is one).
1006  Stmt *getBody(const FunctionDecl *&Definition) const;
1007
1008  virtual Stmt *getBody() const {
1009    const FunctionDecl* Definition;
1010    return getBody(Definition);
1011  }
1012
1013  /// isThisDeclarationADefinition - Returns whether this specific
1014  /// declaration of the function is also a definition. This does not
1015  /// determine whether the function has been defined (e.g., in a
1016  /// previous definition); for that information, use getBody.
1017  /// FIXME: Should return true if function is deleted or defaulted. However,
1018  /// CodeGenModule.cpp uses it, and I don't know if this would break it.
1019  bool isThisDeclarationADefinition() const { return Body; }
1020
1021  void setBody(Stmt *B);
1022  void setLazyBody(uint64_t Offset) { Body = Offset; }
1023
1024  /// Whether this function is marked as virtual explicitly.
1025  bool isVirtualAsWritten() const { return IsVirtualAsWritten; }
1026  void setVirtualAsWritten(bool V) { IsVirtualAsWritten = V; }
1027
1028  /// Whether this virtual function is pure, i.e. makes the containing class
1029  /// abstract.
1030  bool isPure() const { return IsPure; }
1031  void setPure(bool P = true) { IsPure = P; }
1032
1033  /// Whether this function is "trivial" in some specialized C++ senses.
1034  /// Can only be true for default constructors, copy constructors,
1035  /// copy assignment operators, and destructors.  Not meaningful until
1036  /// the class has been fully built by Sema.
1037  bool isTrivial() const { return IsTrivial; }
1038  void setTrivial(bool IT) { IsTrivial = IT; }
1039
1040  bool isCopyAssignment() const { return IsCopyAssignment; }
1041  void setCopyAssignment(bool CA) { IsCopyAssignment = CA; }
1042
1043  /// Whether falling off this function implicitly returns null/zero.
1044  /// If a more specific implicit return value is required, front-ends
1045  /// should synthesize the appropriate return statements.
1046  bool hasImplicitReturnZero() const { return HasImplicitReturnZero; }
1047  void setHasImplicitReturnZero(bool IRZ) { HasImplicitReturnZero = IRZ; }
1048
1049  /// \brief Whether this function has a prototype, either because one
1050  /// was explicitly written or because it was "inherited" by merging
1051  /// a declaration without a prototype with a declaration that has a
1052  /// prototype.
1053  bool hasPrototype() const {
1054    return HasWrittenPrototype || HasInheritedPrototype;
1055  }
1056
1057  bool hasWrittenPrototype() const { return HasWrittenPrototype; }
1058  void setHasWrittenPrototype(bool P) { HasWrittenPrototype = P; }
1059
1060  /// \brief Whether this function inherited its prototype from a
1061  /// previous declaration.
1062  bool hasInheritedPrototype() const { return HasInheritedPrototype; }
1063  void setHasInheritedPrototype(bool P = true) { HasInheritedPrototype = P; }
1064
1065  /// \brief Whether this function has been deleted.
1066  ///
1067  /// A function that is "deleted" (via the C++0x "= delete" syntax)
1068  /// acts like a normal function, except that it cannot actually be
1069  /// called or have its address taken. Deleted functions are
1070  /// typically used in C++ overload resolution to attract arguments
1071  /// whose type or lvalue/rvalue-ness would permit the use of a
1072  /// different overload that would behave incorrectly. For example,
1073  /// one might use deleted functions to ban implicit conversion from
1074  /// a floating-point number to an Integer type:
1075  ///
1076  /// @code
1077  /// struct Integer {
1078  ///   Integer(long); // construct from a long
1079  ///   Integer(double) = delete; // no construction from float or double
1080  ///   Integer(long double) = delete; // no construction from long double
1081  /// };
1082  /// @endcode
1083  bool isDeleted() const { return IsDeleted; }
1084  void setDeleted(bool D = true) { IsDeleted = D; }
1085
1086  /// \brief Determines whether this is a function "main", which is
1087  /// the entry point into an executable program.
1088  bool isMain() const;
1089
1090  /// \brief Determines whether this function is a function with
1091  /// external, C linkage.
1092  bool isExternC() const;
1093
1094  /// \brief Determines whether this is a global function.
1095  bool isGlobal() const;
1096
1097  void setPreviousDeclaration(FunctionDecl * PrevDecl);
1098
1099  virtual const FunctionDecl *getCanonicalDecl() const;
1100  virtual FunctionDecl *getCanonicalDecl();
1101
1102  unsigned getBuiltinID() const;
1103
1104  // Iterator access to formal parameters.
1105  unsigned param_size() const { return getNumParams(); }
1106  typedef ParmVarDecl **param_iterator;
1107  typedef ParmVarDecl * const *param_const_iterator;
1108
1109  param_iterator param_begin() { return ParamInfo; }
1110  param_iterator param_end()   { return ParamInfo+param_size(); }
1111
1112  param_const_iterator param_begin() const { return ParamInfo; }
1113  param_const_iterator param_end() const   { return ParamInfo+param_size(); }
1114
1115  /// getNumParams - Return the number of parameters this function must have
1116  /// based on its FunctionType.  This is the length of the ParamInfo array
1117  /// after it has been created.
1118  unsigned getNumParams() const;
1119
1120  const ParmVarDecl *getParamDecl(unsigned i) const {
1121    assert(i < getNumParams() && "Illegal param #");
1122    return ParamInfo[i];
1123  }
1124  ParmVarDecl *getParamDecl(unsigned i) {
1125    assert(i < getNumParams() && "Illegal param #");
1126    return ParamInfo[i];
1127  }
1128  void setParams(ASTContext& C, ParmVarDecl **NewParamInfo, unsigned NumParams);
1129
1130  /// getMinRequiredArguments - Returns the minimum number of arguments
1131  /// needed to call this function. This may be fewer than the number of
1132  /// function parameters, if some of the parameters have default
1133  /// arguments (in C++).
1134  unsigned getMinRequiredArguments() const;
1135
1136  QualType getResultType() const {
1137    return getType()->getAs<FunctionType>()->getResultType();
1138  }
1139  StorageClass getStorageClass() const { return StorageClass(SClass); }
1140  void setStorageClass(StorageClass SC) { SClass = SC; }
1141
1142  /// \brief Determine whether the "inline" keyword was specified for this
1143  /// function.
1144  bool isInlineSpecified() const { return IsInline; }
1145
1146  /// Set whether the "inline" keyword was specified for this function.
1147  void setInlineSpecified(bool I) { IsInline = I; }
1148
1149  /// \brief Determine whether this function should be inlined, because it is
1150  /// either marked "inline" or is a member function of a C++ class that
1151  /// was defined in the class body.
1152  bool isInlined() const;
1153
1154  bool isInlineDefinitionExternallyVisible() const;
1155
1156  /// isOverloadedOperator - Whether this function declaration
1157  /// represents an C++ overloaded operator, e.g., "operator+".
1158  bool isOverloadedOperator() const {
1159    return getOverloadedOperator() != OO_None;
1160  }
1161
1162  OverloadedOperatorKind getOverloadedOperator() const;
1163
1164  const IdentifierInfo *getLiteralIdentifier() const;
1165
1166  /// \brief If this function is an instantiation of a member function
1167  /// of a class template specialization, retrieves the function from
1168  /// which it was instantiated.
1169  ///
1170  /// This routine will return non-NULL for (non-templated) member
1171  /// functions of class templates and for instantiations of function
1172  /// templates. For example, given:
1173  ///
1174  /// \code
1175  /// template<typename T>
1176  /// struct X {
1177  ///   void f(T);
1178  /// };
1179  /// \endcode
1180  ///
1181  /// The declaration for X<int>::f is a (non-templated) FunctionDecl
1182  /// whose parent is the class template specialization X<int>. For
1183  /// this declaration, getInstantiatedFromFunction() will return
1184  /// the FunctionDecl X<T>::A. When a complete definition of
1185  /// X<int>::A is required, it will be instantiated from the
1186  /// declaration returned by getInstantiatedFromMemberFunction().
1187  FunctionDecl *getInstantiatedFromMemberFunction() const;
1188
1189  /// \brief If this function is an instantiation of a member function of a
1190  /// class template specialization, retrieves the member specialization
1191  /// information.
1192  MemberSpecializationInfo *getMemberSpecializationInfo() const;
1193
1194  /// \brief Specify that this record is an instantiation of the
1195  /// member function FD.
1196  void setInstantiationOfMemberFunction(FunctionDecl *FD,
1197                                        TemplateSpecializationKind TSK);
1198
1199  /// \brief Retrieves the function template that is described by this
1200  /// function declaration.
1201  ///
1202  /// Every function template is represented as a FunctionTemplateDecl
1203  /// and a FunctionDecl (or something derived from FunctionDecl). The
1204  /// former contains template properties (such as the template
1205  /// parameter lists) while the latter contains the actual
1206  /// description of the template's
1207  /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the
1208  /// FunctionDecl that describes the function template,
1209  /// getDescribedFunctionTemplate() retrieves the
1210  /// FunctionTemplateDecl from a FunctionDecl.
1211  FunctionTemplateDecl *getDescribedFunctionTemplate() const {
1212    return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl*>();
1213  }
1214
1215  void setDescribedFunctionTemplate(FunctionTemplateDecl *Template) {
1216    TemplateOrSpecialization = Template;
1217  }
1218
1219  /// \brief Determine whether this function is a function template
1220  /// specialization.
1221  bool isFunctionTemplateSpecialization() const {
1222    return getPrimaryTemplate() != 0;
1223  }
1224
1225  /// \brief If this function is actually a function template specialization,
1226  /// retrieve information about this function template specialization.
1227  /// Otherwise, returns NULL.
1228  FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const {
1229    return TemplateOrSpecialization.
1230             dyn_cast<FunctionTemplateSpecializationInfo*>();
1231  }
1232
1233  /// \brief Determines whether this function is a function template
1234  /// specialization or a member of a class template specialization that can
1235  /// be implicitly instantiated.
1236  bool isImplicitlyInstantiable() const;
1237
1238  /// \brief Retrieve the function declaration from which this function could
1239  /// be instantiated, if it is an instantiation (rather than a non-template
1240  /// or a specialization, for example).
1241  FunctionDecl *getTemplateInstantiationPattern() const;
1242
1243  /// \brief Retrieve the primary template that this function template
1244  /// specialization either specializes or was instantiated from.
1245  ///
1246  /// If this function declaration is not a function template specialization,
1247  /// returns NULL.
1248  FunctionTemplateDecl *getPrimaryTemplate() const;
1249
1250  /// \brief Retrieve the template arguments used to produce this function
1251  /// template specialization from the primary template.
1252  ///
1253  /// If this function declaration is not a function template specialization,
1254  /// returns NULL.
1255  const TemplateArgumentList *getTemplateSpecializationArgs() const;
1256
1257  /// \brief Specify that this function declaration is actually a function
1258  /// template specialization.
1259  ///
1260  /// \param Context the AST context in which this function resides.
1261  ///
1262  /// \param Template the function template that this function template
1263  /// specialization specializes.
1264  ///
1265  /// \param TemplateArgs the template arguments that produced this
1266  /// function template specialization from the template.
1267  ///
1268  /// \param InsertPos If non-NULL, the position in the function template
1269  /// specialization set where the function template specialization data will
1270  /// be inserted.
1271  ///
1272  /// \param TSK the kind of template specialization this is.
1273  void setFunctionTemplateSpecialization(ASTContext &Context,
1274                                         FunctionTemplateDecl *Template,
1275                                      const TemplateArgumentList *TemplateArgs,
1276                                         void *InsertPos,
1277                    TemplateSpecializationKind TSK = TSK_ImplicitInstantiation);
1278
1279  /// \brief Determine what kind of template instantiation this function
1280  /// represents.
1281  TemplateSpecializationKind getTemplateSpecializationKind() const;
1282
1283  /// \brief Determine what kind of template instantiation this function
1284  /// represents.
1285  void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1286                        SourceLocation PointOfInstantiation = SourceLocation());
1287
1288  /// \brief Retrieve the (first) point of instantiation of a function template
1289  /// specialization or a member of a class template specialization.
1290  ///
1291  /// \returns the first point of instantiation, if this function was
1292  /// instantiated from a template; otherwie, returns an invalid source
1293  /// location.
1294  SourceLocation getPointOfInstantiation() const;
1295
1296  /// \brief Determine whether this is or was instantiated from an out-of-line
1297  /// definition of a member function.
1298  bool isOutOfLine() const;
1299
1300  // Implement isa/cast/dyncast/etc.
1301  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1302  static bool classof(const FunctionDecl *D) { return true; }
1303  static bool classofKind(Kind K) {
1304    return K >= FunctionFirst && K <= FunctionLast;
1305  }
1306  static DeclContext *castToDeclContext(const FunctionDecl *D) {
1307    return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D));
1308  }
1309  static FunctionDecl *castFromDeclContext(const DeclContext *DC) {
1310    return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC));
1311  }
1312};
1313
1314
1315/// FieldDecl - An instance of this class is created by Sema::ActOnField to
1316/// represent a member of a struct/union/class.
1317class FieldDecl : public DeclaratorDecl {
1318  // FIXME: This can be packed into the bitfields in Decl.
1319  bool Mutable : 1;
1320  Expr *BitWidth;
1321protected:
1322  FieldDecl(Kind DK, DeclContext *DC, SourceLocation L,
1323            IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
1324            Expr *BW, bool Mutable)
1325    : DeclaratorDecl(DK, DC, L, Id, T, TInfo), Mutable(Mutable), BitWidth(BW) {
1326  }
1327
1328public:
1329  static FieldDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1330                           IdentifierInfo *Id, QualType T,
1331                           TypeSourceInfo *TInfo, Expr *BW, bool Mutable);
1332
1333  /// isMutable - Determines whether this field is mutable (C++ only).
1334  bool isMutable() const { return Mutable; }
1335
1336  /// \brief Set whether this field is mutable (C++ only).
1337  void setMutable(bool M) { Mutable = M; }
1338
1339  /// isBitfield - Determines whether this field is a bitfield.
1340  bool isBitField() const { return BitWidth != NULL; }
1341
1342  /// @brief Determines whether this is an unnamed bitfield.
1343  bool isUnnamedBitfield() const { return BitWidth != NULL && !getDeclName(); }
1344
1345  /// isAnonymousStructOrUnion - Determines whether this field is a
1346  /// representative for an anonymous struct or union. Such fields are
1347  /// unnamed and are implicitly generated by the implementation to
1348  /// store the data for the anonymous union or struct.
1349  bool isAnonymousStructOrUnion() const;
1350
1351  Expr *getBitWidth() const { return BitWidth; }
1352  void setBitWidth(Expr *BW) { BitWidth = BW; }
1353
1354  /// getParent - Returns the parent of this field declaration, which
1355  /// is the struct in which this method is defined.
1356  const RecordDecl *getParent() const {
1357    return cast<RecordDecl>(getDeclContext());
1358  }
1359
1360  RecordDecl *getParent() {
1361    return cast<RecordDecl>(getDeclContext());
1362  }
1363
1364  // Implement isa/cast/dyncast/etc.
1365  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1366  static bool classof(const FieldDecl *D) { return true; }
1367  static bool classofKind(Kind K) { return K >= FieldFirst && K <= FieldLast; }
1368};
1369
1370/// EnumConstantDecl - An instance of this object exists for each enum constant
1371/// that is defined.  For example, in "enum X {a,b}", each of a/b are
1372/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
1373/// TagType for the X EnumDecl.
1374class EnumConstantDecl : public ValueDecl {
1375  Stmt *Init; // an integer constant expression
1376  llvm::APSInt Val; // The value.
1377protected:
1378  EnumConstantDecl(DeclContext *DC, SourceLocation L,
1379                   IdentifierInfo *Id, QualType T, Expr *E,
1380                   const llvm::APSInt &V)
1381    : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {}
1382
1383  virtual ~EnumConstantDecl() {}
1384public:
1385
1386  static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC,
1387                                  SourceLocation L, IdentifierInfo *Id,
1388                                  QualType T, Expr *E,
1389                                  const llvm::APSInt &V);
1390
1391  virtual void Destroy(ASTContext& C);
1392
1393  const Expr *getInitExpr() const { return (const Expr*) Init; }
1394  Expr *getInitExpr() { return (Expr*) Init; }
1395  const llvm::APSInt &getInitVal() const { return Val; }
1396
1397  void setInitExpr(Expr *E) { Init = (Stmt*) E; }
1398  void setInitVal(const llvm::APSInt &V) { Val = V; }
1399
1400  // Implement isa/cast/dyncast/etc.
1401  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1402  static bool classof(const EnumConstantDecl *D) { return true; }
1403  static bool classofKind(Kind K) { return K == EnumConstant; }
1404
1405  friend class StmtIteratorBase;
1406};
1407
1408
1409/// TypeDecl - Represents a declaration of a type.
1410///
1411class TypeDecl : public NamedDecl {
1412  /// TypeForDecl - This indicates the Type object that represents
1413  /// this TypeDecl.  It is a cache maintained by
1414  /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and
1415  /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl.
1416  mutable Type *TypeForDecl;
1417  friend class ASTContext;
1418  friend class DeclContext;
1419  friend class TagDecl;
1420  friend class TemplateTypeParmDecl;
1421  friend class ClassTemplateSpecializationDecl;
1422  friend class TagType;
1423
1424protected:
1425  TypeDecl(Kind DK, DeclContext *DC, SourceLocation L,
1426           IdentifierInfo *Id)
1427    : NamedDecl(DK, DC, L, Id), TypeForDecl(0) {}
1428
1429public:
1430  // Low-level accessor
1431  Type *getTypeForDecl() const { return TypeForDecl; }
1432  void setTypeForDecl(Type *TD) { TypeForDecl = TD; }
1433
1434  // Implement isa/cast/dyncast/etc.
1435  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1436  static bool classof(const TypeDecl *D) { return true; }
1437  static bool classofKind(Kind K) { return K >= TypeFirst && K <= TypeLast; }
1438};
1439
1440
1441class TypedefDecl : public TypeDecl, public Redeclarable<TypedefDecl> {
1442  /// UnderlyingType - This is the type the typedef is set to.
1443  TypeSourceInfo *TInfo;
1444
1445  TypedefDecl(DeclContext *DC, SourceLocation L,
1446              IdentifierInfo *Id, TypeSourceInfo *TInfo)
1447    : TypeDecl(Typedef, DC, L, Id), TInfo(TInfo) {}
1448
1449  virtual ~TypedefDecl();
1450public:
1451
1452  static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
1453                             SourceLocation L, IdentifierInfo *Id,
1454                             TypeSourceInfo *TInfo);
1455
1456  TypeSourceInfo *getTypeSourceInfo() const {
1457    return TInfo;
1458  }
1459
1460  /// Retrieves the canonical declaration of this typedef.
1461  TypedefDecl *getCanonicalDecl() {
1462    return getFirstDeclaration();
1463  }
1464  const TypedefDecl *getCanonicalDecl() const {
1465    return getFirstDeclaration();
1466  }
1467
1468  QualType getUnderlyingType() const {
1469    return TInfo->getType();
1470  }
1471  void setTypeSourceInfo(TypeSourceInfo *newType) {
1472    TInfo = newType;
1473  }
1474
1475  // Implement isa/cast/dyncast/etc.
1476  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1477  static bool classof(const TypedefDecl *D) { return true; }
1478  static bool classofKind(Kind K) { return K == Typedef; }
1479};
1480
1481class TypedefDecl;
1482
1483/// TagDecl - Represents the declaration of a struct/union/class/enum.
1484class TagDecl
1485  : public TypeDecl, public DeclContext, public Redeclarable<TagDecl> {
1486public:
1487  // This is really ugly.
1488  typedef ElaboratedType::TagKind TagKind;
1489  static const TagKind TK_struct = ElaboratedType::TK_struct;
1490  static const TagKind TK_union = ElaboratedType::TK_union;
1491  static const TagKind TK_class = ElaboratedType::TK_class;
1492  static const TagKind TK_enum = ElaboratedType::TK_enum;
1493
1494private:
1495  // FIXME: This can be packed into the bitfields in Decl.
1496  /// TagDeclKind - The TagKind enum.
1497  unsigned TagDeclKind : 2;
1498
1499  /// IsDefinition - True if this is a definition ("struct foo {};"), false if
1500  /// it is a declaration ("struct foo;").
1501  bool IsDefinition : 1;
1502
1503  /// TypedefForAnonDecl - If a TagDecl is anonymous and part of a typedef,
1504  /// this points to the TypedefDecl. Used for mangling.
1505  TypedefDecl *TypedefForAnonDecl;
1506
1507  SourceLocation TagKeywordLoc;
1508  SourceLocation RBraceLoc;
1509
1510protected:
1511  TagDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
1512          IdentifierInfo *Id, TagDecl *PrevDecl,
1513          SourceLocation TKL = SourceLocation())
1514    : TypeDecl(DK, DC, L, Id), DeclContext(DK), TypedefForAnonDecl(0),
1515      TagKeywordLoc(TKL) {
1516    assert((DK != Enum || TK == TK_enum) &&"EnumDecl not matched with TK_enum");
1517    TagDeclKind = TK;
1518    IsDefinition = false;
1519    setPreviousDeclaration(PrevDecl);
1520  }
1521
1522  typedef Redeclarable<TagDecl> redeclarable_base;
1523  virtual TagDecl *getNextRedeclaration() { return RedeclLink.getNext(); }
1524
1525public:
1526  typedef redeclarable_base::redecl_iterator redecl_iterator;
1527  redecl_iterator redecls_begin() const {
1528    return redeclarable_base::redecls_begin();
1529  }
1530  redecl_iterator redecls_end() const {
1531    return redeclarable_base::redecls_end();
1532  }
1533
1534  SourceLocation getRBraceLoc() const { return RBraceLoc; }
1535  void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
1536
1537  SourceLocation getTagKeywordLoc() const { return TagKeywordLoc; }
1538  void setTagKeywordLoc(SourceLocation TKL) { TagKeywordLoc = TKL; }
1539
1540  virtual SourceRange getSourceRange() const;
1541
1542  virtual TagDecl* getCanonicalDecl();
1543  const TagDecl* getCanonicalDecl() const {
1544    return const_cast<TagDecl*>(this)->getCanonicalDecl();
1545  }
1546
1547  /// isDefinition - Return true if this decl has its body specified.
1548  bool isDefinition() const {
1549    return IsDefinition;
1550  }
1551
1552  /// \brief Whether this declaration declares a type that is
1553  /// dependent, i.e., a type that somehow depends on template
1554  /// parameters.
1555  bool isDependentType() const { return isDependentContext(); }
1556
1557  /// @brief Starts the definition of this tag declaration.
1558  ///
1559  /// This method should be invoked at the beginning of the definition
1560  /// of this tag declaration. It will set the tag type into a state
1561  /// where it is in the process of being defined.
1562  void startDefinition();
1563
1564  /// @brief Completes the definition of this tag declaration.
1565  void completeDefinition();
1566
1567  /// getDefinition - Returns the TagDecl that actually defines this
1568  ///  struct/union/class/enum.  When determining whether or not a
1569  ///  struct/union/class/enum is completely defined, one should use this method
1570  ///  as opposed to 'isDefinition'.  'isDefinition' indicates whether or not a
1571  ///  specific TagDecl is defining declaration, not whether or not the
1572  ///  struct/union/class/enum type is defined.  This method returns NULL if
1573  ///  there is no TagDecl that defines the struct/union/class/enum.
1574  TagDecl* getDefinition(ASTContext& C) const;
1575
1576  void setDefinition(bool V) { IsDefinition = V; }
1577
1578  const char *getKindName() const {
1579    return ElaboratedType::getNameForTagKind(getTagKind());
1580  }
1581
1582  /// getTagKindForTypeSpec - Converts a type specifier (DeclSpec::TST)
1583  /// into a tag kind.  It is an error to provide a type specifier
1584  /// which *isn't* a tag kind here.
1585  static TagKind getTagKindForTypeSpec(unsigned TypeSpec);
1586
1587  TagKind getTagKind() const {
1588    return TagKind(TagDeclKind);
1589  }
1590
1591  void setTagKind(TagKind TK) { TagDeclKind = TK; }
1592
1593  bool isStruct() const { return getTagKind() == TK_struct; }
1594  bool isClass()  const { return getTagKind() == TK_class; }
1595  bool isUnion()  const { return getTagKind() == TK_union; }
1596  bool isEnum()   const { return getTagKind() == TK_enum; }
1597
1598  TypedefDecl *getTypedefForAnonDecl() const { return TypedefForAnonDecl; }
1599  void setTypedefForAnonDecl(TypedefDecl *TDD) { TypedefForAnonDecl = TDD; }
1600
1601  // Implement isa/cast/dyncast/etc.
1602  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1603  static bool classof(const TagDecl *D) { return true; }
1604  static bool classofKind(Kind K) { return K >= TagFirst && K <= TagLast; }
1605
1606  static DeclContext *castToDeclContext(const TagDecl *D) {
1607    return static_cast<DeclContext *>(const_cast<TagDecl*>(D));
1608  }
1609  static TagDecl *castFromDeclContext(const DeclContext *DC) {
1610    return static_cast<TagDecl *>(const_cast<DeclContext*>(DC));
1611  }
1612};
1613
1614/// EnumDecl - Represents an enum.  As an extension, we allow forward-declared
1615/// enums.
1616class EnumDecl : public TagDecl {
1617  /// IntegerType - This represent the integer type that the enum corresponds
1618  /// to for code generation purposes.  Note that the enumerator constants may
1619  /// have a different type than this does.
1620  QualType IntegerType;
1621
1622  /// PromotionType - The integer type that values of this type should
1623  /// promote to.  In C, enumerators are generally of an integer type
1624  /// directly, but gcc-style large enumerators (and all enumerators
1625  /// in C++) are of the enum type instead.
1626  QualType PromotionType;
1627
1628  /// \brief If the enumeration was instantiated from an enumeration
1629  /// within a class or function template, this pointer refers to the
1630  /// enumeration declared within the template.
1631  EnumDecl *InstantiatedFrom;
1632
1633  EnumDecl(DeclContext *DC, SourceLocation L,
1634           IdentifierInfo *Id, EnumDecl *PrevDecl, SourceLocation TKL)
1635    : TagDecl(Enum, TK_enum, DC, L, Id, PrevDecl, TKL), InstantiatedFrom(0) {
1636      IntegerType = QualType();
1637    }
1638public:
1639  EnumDecl *getCanonicalDecl() {
1640    return cast<EnumDecl>(TagDecl::getCanonicalDecl());
1641  }
1642  const EnumDecl *getCanonicalDecl() const {
1643    return cast<EnumDecl>(TagDecl::getCanonicalDecl());
1644  }
1645
1646  static EnumDecl *Create(ASTContext &C, DeclContext *DC,
1647                          SourceLocation L, IdentifierInfo *Id,
1648                          SourceLocation TKL, EnumDecl *PrevDecl);
1649
1650  virtual void Destroy(ASTContext& C);
1651
1652  /// completeDefinition - When created, the EnumDecl corresponds to a
1653  /// forward-declared enum. This method is used to mark the
1654  /// declaration as being defined; it's enumerators have already been
1655  /// added (via DeclContext::addDecl). NewType is the new underlying
1656  /// type of the enumeration type.
1657  void completeDefinition(ASTContext &C, QualType NewType,
1658                          QualType PromotionType);
1659
1660  // enumerator_iterator - Iterates through the enumerators of this
1661  // enumeration.
1662  typedef specific_decl_iterator<EnumConstantDecl> enumerator_iterator;
1663
1664  enumerator_iterator enumerator_begin() const {
1665    return enumerator_iterator(this->decls_begin());
1666  }
1667
1668  enumerator_iterator enumerator_end() const {
1669    return enumerator_iterator(this->decls_end());
1670  }
1671
1672  /// getPromotionType - Return the integer type that enumerators
1673  /// should promote to.
1674  QualType getPromotionType() const { return PromotionType; }
1675
1676  /// \brief Set the promotion type.
1677  void setPromotionType(QualType T) { PromotionType = T; }
1678
1679  /// getIntegerType - Return the integer type this enum decl corresponds to.
1680  /// This returns a null qualtype for an enum forward definition.
1681  QualType getIntegerType() const { return IntegerType; }
1682
1683  /// \brief Set the underlying integer type.
1684  void setIntegerType(QualType T) { IntegerType = T; }
1685
1686  /// \brief Returns the enumeration (declared within the template)
1687  /// from which this enumeration type was instantiated, or NULL if
1688  /// this enumeration was not instantiated from any template.
1689  EnumDecl *getInstantiatedFromMemberEnum() const {
1690    return InstantiatedFrom;
1691  }
1692
1693  void setInstantiationOfMemberEnum(EnumDecl *IF) { InstantiatedFrom = IF; }
1694
1695  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1696  static bool classof(const EnumDecl *D) { return true; }
1697  static bool classofKind(Kind K) { return K == Enum; }
1698};
1699
1700
1701/// RecordDecl - Represents a struct/union/class.  For example:
1702///   struct X;                  // Forward declaration, no "body".
1703///   union Y { int A, B; };     // Has body with members A and B (FieldDecls).
1704/// This decl will be marked invalid if *any* members are invalid.
1705///
1706class RecordDecl : public TagDecl {
1707  // FIXME: This can be packed into the bitfields in Decl.
1708  /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
1709  /// array member (e.g. int X[]) or if this union contains a struct that does.
1710  /// If so, this cannot be contained in arrays or other structs as a member.
1711  bool HasFlexibleArrayMember : 1;
1712
1713  /// AnonymousStructOrUnion - Whether this is the type of an
1714  /// anonymous struct or union.
1715  bool AnonymousStructOrUnion : 1;
1716
1717  /// HasObjectMember - This is true if this struct has at least one
1718  /// member containing an object
1719  bool HasObjectMember : 1;
1720
1721protected:
1722  RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
1723             SourceLocation L, IdentifierInfo *Id,
1724             RecordDecl *PrevDecl, SourceLocation TKL);
1725  virtual ~RecordDecl();
1726
1727public:
1728  static RecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
1729                            SourceLocation L, IdentifierInfo *Id,
1730                            SourceLocation TKL = SourceLocation(),
1731                            RecordDecl* PrevDecl = 0);
1732
1733  virtual void Destroy(ASTContext& C);
1734
1735  bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
1736  void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
1737
1738  /// isAnonymousStructOrUnion - Whether this is an anonymous struct
1739  /// or union. To be an anonymous struct or union, it must have been
1740  /// declared without a name and there must be no objects of this
1741  /// type declared, e.g.,
1742  /// @code
1743  ///   union { int i; float f; };
1744  /// @endcode
1745  /// is an anonymous union but neither of the following are:
1746  /// @code
1747  ///  union X { int i; float f; };
1748  ///  union { int i; float f; } obj;
1749  /// @endcode
1750  bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion; }
1751  void setAnonymousStructOrUnion(bool Anon) {
1752    AnonymousStructOrUnion = Anon;
1753  }
1754
1755  bool hasObjectMember() const { return HasObjectMember; }
1756  void setHasObjectMember (bool val) { HasObjectMember = val; }
1757
1758  /// \brief Determines whether this declaration represents the
1759  /// injected class name.
1760  ///
1761  /// The injected class name in C++ is the name of the class that
1762  /// appears inside the class itself. For example:
1763  ///
1764  /// \code
1765  /// struct C {
1766  ///   // C is implicitly declared here as a synonym for the class name.
1767  /// };
1768  ///
1769  /// C::C c; // same as "C c;"
1770  /// \endcode
1771  bool isInjectedClassName() const;
1772
1773  /// getDefinition - Returns the RecordDecl that actually defines this
1774  ///  struct/union/class.  When determining whether or not a struct/union/class
1775  ///  is completely defined, one should use this method as opposed to
1776  ///  'isDefinition'.  'isDefinition' indicates whether or not a specific
1777  ///  RecordDecl is defining declaration, not whether or not the record
1778  ///  type is defined.  This method returns NULL if there is no RecordDecl
1779  ///  that defines the struct/union/tag.
1780  RecordDecl* getDefinition(ASTContext& C) const {
1781    return cast_or_null<RecordDecl>(TagDecl::getDefinition(C));
1782  }
1783
1784  // Iterator access to field members. The field iterator only visits
1785  // the non-static data members of this class, ignoring any static
1786  // data members, functions, constructors, destructors, etc.
1787  typedef specific_decl_iterator<FieldDecl> field_iterator;
1788
1789  field_iterator field_begin() const {
1790    return field_iterator(decls_begin());
1791  }
1792  field_iterator field_end() const {
1793    return field_iterator(decls_end());
1794  }
1795
1796  // field_empty - Whether there are any fields (non-static data
1797  // members) in this record.
1798  bool field_empty() const {
1799    return field_begin() == field_end();
1800  }
1801
1802  /// completeDefinition - Notes that the definition of this type is
1803  /// now complete.
1804  void completeDefinition(ASTContext& C);
1805
1806  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1807  static bool classof(const RecordDecl *D) { return true; }
1808  static bool classofKind(Kind K) {
1809    return K >= RecordFirst && K <= RecordLast;
1810  }
1811};
1812
1813class FileScopeAsmDecl : public Decl {
1814  StringLiteral *AsmString;
1815  FileScopeAsmDecl(DeclContext *DC, SourceLocation L, StringLiteral *asmstring)
1816    : Decl(FileScopeAsm, DC, L), AsmString(asmstring) {}
1817public:
1818  static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC,
1819                                  SourceLocation L, StringLiteral *Str);
1820
1821  const StringLiteral *getAsmString() const { return AsmString; }
1822  StringLiteral *getAsmString() { return AsmString; }
1823  void setAsmString(StringLiteral *Asm) { AsmString = Asm; }
1824
1825  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1826  static bool classof(const FileScopeAsmDecl *D) { return true; }
1827  static bool classofKind(Kind K) { return K == FileScopeAsm; }
1828};
1829
1830/// BlockDecl - This represents a block literal declaration, which is like an
1831/// unnamed FunctionDecl.  For example:
1832/// ^{ statement-body }   or   ^(int arg1, float arg2){ statement-body }
1833///
1834class BlockDecl : public Decl, public DeclContext {
1835  // FIXME: This can be packed into the bitfields in Decl.
1836  bool isVariadic : 1;
1837  /// ParamInfo - new[]'d array of pointers to ParmVarDecls for the formal
1838  /// parameters of this function.  This is null if a prototype or if there are
1839  /// no formals.
1840  ParmVarDecl **ParamInfo;
1841  unsigned NumParams;
1842
1843  Stmt *Body;
1844
1845protected:
1846  BlockDecl(DeclContext *DC, SourceLocation CaretLoc)
1847    : Decl(Block, DC, CaretLoc), DeclContext(Block),
1848      isVariadic(false), ParamInfo(0), NumParams(0), Body(0) {}
1849
1850  virtual ~BlockDecl();
1851  virtual void Destroy(ASTContext& C);
1852
1853public:
1854  static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L);
1855
1856  SourceLocation getCaretLocation() const { return getLocation(); }
1857
1858  bool IsVariadic() const { return isVariadic; }
1859  void setIsVariadic(bool value) { isVariadic = value; }
1860
1861  CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; }
1862  Stmt *getBody() const { return (Stmt*) Body; }
1863  void setBody(CompoundStmt *B) { Body = (Stmt*) B; }
1864
1865  // Iterator access to formal parameters.
1866  unsigned param_size() const { return getNumParams(); }
1867  typedef ParmVarDecl **param_iterator;
1868  typedef ParmVarDecl * const *param_const_iterator;
1869
1870  bool param_empty() const { return NumParams == 0; }
1871  param_iterator param_begin()  { return ParamInfo; }
1872  param_iterator param_end()   { return ParamInfo+param_size(); }
1873
1874  param_const_iterator param_begin() const { return ParamInfo; }
1875  param_const_iterator param_end() const   { return ParamInfo+param_size(); }
1876
1877  unsigned getNumParams() const;
1878  const ParmVarDecl *getParamDecl(unsigned i) const {
1879    assert(i < getNumParams() && "Illegal param #");
1880    return ParamInfo[i];
1881  }
1882  ParmVarDecl *getParamDecl(unsigned i) {
1883    assert(i < getNumParams() && "Illegal param #");
1884    return ParamInfo[i];
1885  }
1886  void setParams(ASTContext& C, ParmVarDecl **NewParamInfo, unsigned NumParams);
1887
1888  // Implement isa/cast/dyncast/etc.
1889  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1890  static bool classof(const BlockDecl *D) { return true; }
1891  static bool classofKind(Kind K) { return K == Block; }
1892  static DeclContext *castToDeclContext(const BlockDecl *D) {
1893    return static_cast<DeclContext *>(const_cast<BlockDecl*>(D));
1894  }
1895  static BlockDecl *castFromDeclContext(const DeclContext *DC) {
1896    return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC));
1897  }
1898};
1899
1900/// Insertion operator for diagnostics.  This allows sending NamedDecl's
1901/// into a diagnostic with <<.
1902inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1903                                           NamedDecl* ND) {
1904  DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND), Diagnostic::ak_nameddecl);
1905  return DB;
1906}
1907
1908}  // end namespace clang
1909
1910#endif
1911