]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/AST/Decl.h
Import Clang, at r72732.
[FreeBSD/FreeBSD.git] / include / clang / AST / Decl.h
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/DeclarationName.h"
20 #include "clang/AST/ExternalASTSource.h"
21
22 namespace clang {
23 class Expr;
24 class FunctionTemplateDecl;
25 class Stmt;
26 class CompoundStmt;
27 class StringLiteral;
28
29 /// TranslationUnitDecl - The top declaration context.
30 class TranslationUnitDecl : public Decl, public DeclContext {
31   TranslationUnitDecl()
32     : Decl(TranslationUnit, 0, SourceLocation()),
33       DeclContext(TranslationUnit) {}
34 public:
35   static TranslationUnitDecl *Create(ASTContext &C);
36   // Implement isa/cast/dyncast/etc.
37   static bool classof(const Decl *D) { return D->getKind() == TranslationUnit; }
38   static bool classof(const TranslationUnitDecl *D) { return true; }  
39   static DeclContext *castToDeclContext(const TranslationUnitDecl *D) {
40     return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D));
41   }
42   static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
43     return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC));
44   }
45 };
46
47 /// NamedDecl - This represents a decl with a name.  Many decls have names such
48 /// as ObjCMethodDecl, but not @class, etc.
49 class NamedDecl : public Decl {
50   /// Name - The name of this declaration, which is typically a normal
51   /// identifier but may also be a special kind of name (C++
52   /// constructor, Objective-C selector, etc.)
53   DeclarationName Name;
54
55 protected:
56   NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N)
57     : Decl(DK, DC, L), Name(N) { }
58
59 public:
60   /// getIdentifier - Get the identifier that names this declaration,
61   /// if there is one. This will return NULL if this declaration has
62   /// no name (e.g., for an unnamed class) or if the name is a special
63   /// name (C++ constructor, Objective-C selector, etc.).
64   IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); }
65
66   /// getNameAsCString - Get the name of identifier for this declaration as a
67   /// C string (const char*).  This requires that the declaration have a name
68   /// and that it be a simple identifier.
69   const char *getNameAsCString() const {
70     assert(getIdentifier() && "Name is not a simple identifier");
71     return getIdentifier()->getName();
72   }
73
74   /// getDeclName - Get the actual, stored name of the declaration,
75   /// which may be a special name.
76   DeclarationName getDeclName() const { return Name; }
77
78   /// \brief Set the name of this declaration.
79   void setDeclName(DeclarationName N) { Name = N; }
80
81   /// getNameAsString - Get a human-readable name for the declaration, even if
82   /// it is one of the special kinds of names (C++ constructor, Objective-C
83   /// selector, etc).  Creating this name requires expensive string
84   /// manipulation, so it should be called only when performance doesn't matter.
85   /// For simple declarations, getNameAsCString() should suffice.
86   std::string getNameAsString() const { return Name.getAsString(); }
87   
88   /// getQualifiedNameAsString - Returns human-readable qualified name for
89   /// declaration, like A::B::i, for i being member of namespace A::B.
90   /// If declaration is not member of context which can be named (record,
91   /// namespace), it will return same result as getNameAsString().
92   /// Creating this name is expensive, so it should be called only when
93   /// performance doesn't matter.
94   std::string getQualifiedNameAsString() const;
95
96   /// declarationReplaces - Determine whether this declaration, if
97   /// known to be well-formed within its context, will replace the
98   /// declaration OldD if introduced into scope. A declaration will
99   /// replace another declaration if, for example, it is a
100   /// redeclaration of the same variable or function, but not if it is
101   /// a declaration of a different kind (function vs. class) or an
102   /// overloaded function.
103   bool declarationReplaces(NamedDecl *OldD) const;
104
105   /// \brief Determine whether this declaration has linkage.
106   bool hasLinkage() const;
107
108   static bool classof(const Decl *D) {
109     return D->getKind() >= NamedFirst && D->getKind() <= NamedLast;
110   }
111   static bool classof(const NamedDecl *D) { return true; }
112 };
113
114 /// NamespaceDecl - Represent a C++ namespace.
115 class NamespaceDecl : public NamedDecl, public DeclContext {
116   SourceLocation LBracLoc, RBracLoc;
117   
118   // For extended namespace definitions:
119   //
120   // namespace A { int x; }
121   // namespace A { int y; }
122   //
123   // there will be one NamespaceDecl for each declaration.
124   // NextNamespace points to the next extended declaration.
125   // OrigNamespace points to the original namespace declaration.
126   // OrigNamespace of the first namespace decl points to itself.
127   NamespaceDecl *OrigNamespace, *NextNamespace;
128   
129   NamespaceDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id)
130     : NamedDecl(Namespace, DC, L, Id), DeclContext(Namespace) {
131     OrigNamespace = this;
132     NextNamespace = 0;
133   }
134 public:
135   static NamespaceDecl *Create(ASTContext &C, DeclContext *DC,
136                                SourceLocation L, IdentifierInfo *Id);
137   
138   virtual void Destroy(ASTContext& C);
139
140   NamespaceDecl *getNextNamespace() { return NextNamespace; }
141   const NamespaceDecl *getNextNamespace() const { return NextNamespace; }
142   void setNextNamespace(NamespaceDecl *ND) { NextNamespace = ND; }
143
144   NamespaceDecl *getOriginalNamespace() const {
145     return OrigNamespace;
146   }
147   void setOriginalNamespace(NamespaceDecl *ND) { OrigNamespace = ND; }
148   
149   SourceRange getSourceRange() const { 
150     return SourceRange(LBracLoc, RBracLoc); 
151   }
152
153   SourceLocation getLBracLoc() const { return LBracLoc; }
154   SourceLocation getRBracLoc() const { return RBracLoc; }
155   void setLBracLoc(SourceLocation LBrace) { LBracLoc = LBrace; }
156   void setRBracLoc(SourceLocation RBrace) { RBracLoc = RBrace; }
157   
158   // Implement isa/cast/dyncast/etc.
159   static bool classof(const Decl *D) { return D->getKind() == Namespace; }
160   static bool classof(const NamespaceDecl *D) { return true; }
161   static DeclContext *castToDeclContext(const NamespaceDecl *D) {
162     return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D));
163   }
164   static NamespaceDecl *castFromDeclContext(const DeclContext *DC) {
165     return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC));
166   }
167 };
168
169 /// ValueDecl - Represent the declaration of a variable (in which case it is 
170 /// an lvalue) a function (in which case it is a function designator) or
171 /// an enum constant. 
172 class ValueDecl : public NamedDecl {
173   QualType DeclType;
174
175 protected:
176   ValueDecl(Kind DK, DeclContext *DC, SourceLocation L,
177             DeclarationName N, QualType T) 
178     : NamedDecl(DK, DC, L, N), DeclType(T) {}
179 public:
180   QualType getType() const { return DeclType; }
181   void setType(QualType newType) { DeclType = newType; }
182   
183   // Implement isa/cast/dyncast/etc.
184   static bool classof(const Decl *D) {
185     return D->getKind() >= ValueFirst && D->getKind() <= ValueLast;
186   }
187   static bool classof(const ValueDecl *D) { return true; }
188 };
189
190 /// \brief Structure used to store a statement, the constant value to
191 /// which it was evaluated (if any), and whether or not the statement
192 /// is an integral constant expression (if known).
193 struct EvaluatedStmt {
194   EvaluatedStmt() : WasEvaluated(false), CheckedICE(false), IsICE(false) { }
195
196   /// \brief Whether this statement was already evaluated.
197   bool WasEvaluated : 1;
198
199   /// \brief Whether we already checked whether this statement was an
200   /// integral constant expression.
201   bool CheckedICE : 1;
202
203   /// \brief Whether this statement is an integral constant
204   /// expression. Only valid if CheckedICE is true.
205   bool IsICE : 1;
206
207   Stmt *Value;
208   APValue Evaluated;
209 };
210
211 /// VarDecl - An instance of this class is created to represent a variable
212 /// declaration or definition.
213 class VarDecl : public ValueDecl {
214 public:
215   enum StorageClass {
216     None, Auto, Register, Extern, Static, PrivateExtern
217   };
218
219   /// getStorageClassSpecifierString - Return the string used to
220   /// specify the storage class \arg SC.
221   ///
222   /// It is illegal to call this function with SC == None.
223   static const char *getStorageClassSpecifierString(StorageClass SC);
224
225 private:
226   mutable llvm::PointerUnion<Stmt *, EvaluatedStmt *> Init;
227   // FIXME: This can be packed into the bitfields in Decl.
228   unsigned SClass : 3;
229   bool ThreadSpecified : 1;
230   bool HasCXXDirectInit : 1; 
231
232   /// DeclaredInCondition - Whether this variable was declared in a
233   /// condition, e.g., if (int x = foo()) { ... }.
234   bool DeclaredInCondition : 1;
235
236   /// \brief The previous declaration of this variable.
237   VarDecl *PreviousDeclaration;
238
239   // Move to DeclGroup when it is implemented.
240   SourceLocation TypeSpecStartLoc;
241   friend class StmtIteratorBase;
242 protected:
243   VarDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
244           QualType T, StorageClass SC, SourceLocation TSSL = SourceLocation())
245     : ValueDecl(DK, DC, L, Id, T), Init(),
246       ThreadSpecified(false), HasCXXDirectInit(false),
247       DeclaredInCondition(false), PreviousDeclaration(0), 
248       TypeSpecStartLoc(TSSL) { 
249     SClass = SC; 
250   }
251 public:
252   static VarDecl *Create(ASTContext &C, DeclContext *DC,
253                          SourceLocation L, IdentifierInfo *Id,
254                          QualType T, StorageClass S,
255                          SourceLocation TypeSpecStartLoc = SourceLocation());
256
257   virtual ~VarDecl();
258   virtual void Destroy(ASTContext& C);
259
260   StorageClass getStorageClass() const { return (StorageClass)SClass; }
261   void setStorageClass(StorageClass SC) { SClass = SC; }
262
263   SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; }
264   void setTypeSpecStartLoc(SourceLocation SL) {
265     TypeSpecStartLoc = SL;
266   }
267
268   const Expr *getInit() const { 
269     if (Init.isNull())
270       return 0;
271
272     const Stmt *S = Init.dyn_cast<Stmt *>();
273     if (!S)
274       S = Init.get<EvaluatedStmt *>()->Value;
275
276     return (const Expr*) S; 
277   }
278   Expr *getInit() { 
279     if (Init.isNull())
280       return 0;
281
282     Stmt *S = Init.dyn_cast<Stmt *>();
283     if (!S)
284       S = Init.get<EvaluatedStmt *>()->Value;
285
286     return (Expr*) S; 
287   }
288
289   /// \brief Retrieve the address of the initializer expression.
290   Stmt **getInitAddress() {
291     if (Init.is<Stmt *>())
292       return reinterpret_cast<Stmt **>(&Init); // FIXME: ugly hack
293     return &Init.get<EvaluatedStmt *>()->Value;
294   }
295
296   void setInit(ASTContext &C, Expr *I);
297    
298   /// \brief Note that constant evaluation has computed the given
299   /// value for this variable's initializer.
300   void setEvaluatedValue(ASTContext &C, const APValue &Value) const {
301     EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
302     if (!Eval) {
303       Stmt *S = Init.get<Stmt *>();
304       Eval = new (C) EvaluatedStmt;
305       Eval->Value = S;
306       Init = Eval;
307     }
308
309     Eval->WasEvaluated = true;
310     Eval->Evaluated = Value;
311   }
312    
313   /// \brief Return the already-evaluated value of this variable's
314   /// initializer, or NULL if the value is not yet known.
315   APValue *getEvaluatedValue() const {
316     if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
317       if (Eval->WasEvaluated)
318         return &Eval->Evaluated;
319
320     return 0;
321   }
322
323   /// \brief Determines whether it is already known whether the
324   /// initializer is an integral constant expression or not.
325   bool isInitKnownICE() const {
326     if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
327       return Eval->CheckedICE;
328
329     return false;
330   }
331
332   /// \brief Determines whether the initializer is an integral
333   /// constant expression.
334   ///
335   /// \pre isInitKnownICE()
336   bool isInitICE() const {
337     assert(isInitKnownICE() && 
338            "Check whether we already know that the initializer is an ICE");
339     return Init.get<EvaluatedStmt *>()->IsICE;
340   }
341
342   /// \brief Note that we now know whether the initializer is an
343   /// integral constant expression.
344   void setInitKnownICE(ASTContext &C, bool IsICE) const {
345     EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
346     if (!Eval) {
347       Stmt *S = Init.get<Stmt *>();
348       Eval = new (C) EvaluatedStmt;
349       Eval->Value = S;
350       Init = Eval;
351     }
352
353     Eval->CheckedICE = true;
354     Eval->IsICE = IsICE;
355   }
356
357   /// \brief Retrieve the definition of this variable, which may come
358   /// from a previous declaration. Def will be set to the VarDecl that
359   /// contains the initializer, and the result will be that
360   /// initializer.
361   const Expr *getDefinition(const VarDecl *&Def) const;
362
363   void setThreadSpecified(bool T) { ThreadSpecified = T; }
364   bool isThreadSpecified() const {
365     return ThreadSpecified;
366   }
367
368   void setCXXDirectInitializer(bool T) { HasCXXDirectInit = T; }
369
370   /// hasCXXDirectInitializer - If true, the initializer was a direct
371   /// initializer, e.g: "int x(1);". The Init expression will be the expression
372   /// inside the parens or a "ClassType(a,b,c)" class constructor expression for
373   /// class types. Clients can distinguish between "int x(1);" and "int x=1;"
374   /// by checking hasCXXDirectInitializer.
375   ///
376   bool hasCXXDirectInitializer() const {
377     return HasCXXDirectInit;
378   }
379   
380   /// isDeclaredInCondition - Whether this variable was declared as
381   /// part of a condition in an if/switch/while statement, e.g.,
382   /// @code
383   /// if (int x = foo()) { ... }
384   /// @endcode
385   bool isDeclaredInCondition() const {
386     return DeclaredInCondition;
387   }
388   void setDeclaredInCondition(bool InCondition) { 
389     DeclaredInCondition = InCondition; 
390   }
391
392   /// getPreviousDeclaration - Return the previous declaration of this
393   /// variable.
394   const VarDecl *getPreviousDeclaration() const { return PreviousDeclaration; }
395
396   void setPreviousDeclaration(VarDecl *PrevDecl) {
397     PreviousDeclaration = PrevDecl;
398   }
399
400   /// hasLocalStorage - Returns true if a variable with function scope
401   ///  is a non-static local variable.
402   bool hasLocalStorage() const {
403     if (getStorageClass() == None)
404       return !isFileVarDecl();
405     
406     // Return true for:  Auto, Register.
407     // Return false for: Extern, Static, PrivateExtern.
408     
409     return getStorageClass() <= Register;
410   }
411
412   /// hasExternStorage - Returns true if a variable has extern or
413   /// __private_extern__ storage.
414   bool hasExternalStorage() const {
415     return getStorageClass() == Extern || getStorageClass() == PrivateExtern;
416   }
417
418   /// hasGlobalStorage - Returns true for all variables that do not
419   ///  have local storage.  This includs all global variables as well
420   ///  as static variables declared within a function.
421   bool hasGlobalStorage() const { return !hasLocalStorage(); }
422
423   /// isBlockVarDecl - Returns true for local variable declarations.  Note that
424   /// this includes static variables inside of functions.
425   ///
426   ///   void foo() { int x; static int y; extern int z; }
427   ///
428   bool isBlockVarDecl() const {
429     if (getKind() != Decl::Var)
430       return false;
431     if (const DeclContext *DC = getDeclContext())
432       return DC->getLookupContext()->isFunctionOrMethod();
433     return false;
434   }
435   
436   /// \brief Determines whether this is a static data member.
437   ///
438   /// This will only be true in C++, and applies to, e.g., the
439   /// variable 'x' in:
440   /// \code
441   /// struct S {
442   ///   static int x;
443   /// };
444   /// \endcode
445   bool isStaticDataMember() const {
446     return getDeclContext()->isRecord();
447   }
448
449   /// isFileVarDecl - Returns true for file scoped variable declaration.
450   bool isFileVarDecl() const {
451     if (getKind() != Decl::Var)
452       return false;
453     if (const DeclContext *Ctx = getDeclContext()) {
454       Ctx = Ctx->getLookupContext();
455       if (isa<TranslationUnitDecl>(Ctx) || isa<NamespaceDecl>(Ctx) )
456         return true;
457     }
458     return false;
459   }
460
461   /// \brief Determine whether this is a tentative definition of a
462   /// variable in C.
463   bool isTentativeDefinition(ASTContext &Context) const;
464   
465   /// \brief Determines whether this variable is a variable with
466   /// external, C linkage.
467   bool isExternC(ASTContext &Context) const;
468
469   // Implement isa/cast/dyncast/etc.
470   static bool classof(const Decl *D) {
471     return D->getKind() >= VarFirst && D->getKind() <= VarLast;
472   }
473   static bool classof(const VarDecl *D) { return true; }
474 };
475
476 class ImplicitParamDecl : public VarDecl {
477 protected:
478   ImplicitParamDecl(Kind DK, DeclContext *DC, SourceLocation L,
479             IdentifierInfo *Id, QualType Tw) 
480     : VarDecl(DK, DC, L, Id, Tw, VarDecl::None) {}
481 public:
482   static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC,
483                          SourceLocation L, IdentifierInfo *Id,
484                          QualType T);
485   // Implement isa/cast/dyncast/etc.
486   static bool classof(const ImplicitParamDecl *D) { return true; }
487   static bool classof(const Decl *D) { return D->getKind() == ImplicitParam; }
488 };
489
490 /// ParmVarDecl - Represent a parameter to a function.
491 class ParmVarDecl : public VarDecl {
492   // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
493   /// FIXME: Also can be paced into the bitfields in Decl.
494   /// in, inout, etc.
495   unsigned objcDeclQualifier : 6;
496   
497   /// Default argument, if any.  [C++ Only]
498   Expr *DefaultArg;
499 protected:
500   ParmVarDecl(Kind DK, DeclContext *DC, SourceLocation L,
501               IdentifierInfo *Id, QualType T, StorageClass S,
502               Expr *DefArg)
503     : VarDecl(DK, DC, L, Id, T, S), 
504       objcDeclQualifier(OBJC_TQ_None), DefaultArg(DefArg) {}
505
506 public:
507   static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
508                              SourceLocation L,IdentifierInfo *Id,
509                              QualType T, StorageClass S, Expr *DefArg);
510   
511   ObjCDeclQualifier getObjCDeclQualifier() const {
512     return ObjCDeclQualifier(objcDeclQualifier);
513   }
514   void setObjCDeclQualifier(ObjCDeclQualifier QTVal) {
515     objcDeclQualifier = QTVal;
516   }
517     
518   const Expr *getDefaultArg() const { return DefaultArg; }
519   Expr *getDefaultArg() { return DefaultArg; }
520   void setDefaultArg(Expr *defarg) { DefaultArg = defarg; }
521
522   /// hasUnparsedDefaultArg - Determines whether this parameter has a
523   /// default argument that has not yet been parsed. This will occur
524   /// during the processing of a C++ class whose member functions have
525   /// default arguments, e.g.,
526   /// @code
527   ///   class X {
528   ///   public:
529   ///     void f(int x = 17); // x has an unparsed default argument now
530   ///   }; // x has a regular default argument now
531   /// @endcode
532   bool hasUnparsedDefaultArg() const {
533     return DefaultArg == reinterpret_cast<Expr *>(-1);
534   }
535
536   /// setUnparsedDefaultArg - Specify that this parameter has an
537   /// unparsed default argument. The argument will be replaced with a
538   /// real default argument via setDefaultArg when the class
539   /// definition enclosing the function declaration that owns this
540   /// default argument is completed.
541   void setUnparsedDefaultArg() { DefaultArg = reinterpret_cast<Expr *>(-1); }
542
543   QualType getOriginalType() const;
544   
545   /// setOwningFunction - Sets the function declaration that owns this
546   /// ParmVarDecl. Since ParmVarDecls are often created before the
547   /// FunctionDecls that own them, this routine is required to update
548   /// the DeclContext appropriately.
549   void setOwningFunction(DeclContext *FD) { setDeclContext(FD); }
550
551   // Implement isa/cast/dyncast/etc.
552   static bool classof(const Decl *D) { 
553     return (D->getKind() == ParmVar ||
554             D->getKind() == OriginalParmVar); 
555   }
556   static bool classof(const ParmVarDecl *D) { return true; }
557 };
558
559 /// OriginalParmVarDecl - Represent a parameter to a function, when
560 /// the type of the parameter has been promoted. This node represents the
561 /// parameter to the function with its original type.
562 ///
563 class OriginalParmVarDecl : public ParmVarDecl {
564   friend class ParmVarDecl;
565 protected:
566   QualType OriginalType;
567 private:
568   OriginalParmVarDecl(DeclContext *DC, SourceLocation L,
569                               IdentifierInfo *Id, QualType T, 
570                               QualType OT, StorageClass S,
571                               Expr *DefArg)
572   : ParmVarDecl(OriginalParmVar, DC, L, Id, T, S, DefArg), OriginalType(OT) {}
573 public:
574   static OriginalParmVarDecl *Create(ASTContext &C, DeclContext *DC,
575                                      SourceLocation L,IdentifierInfo *Id,
576                                      QualType T, QualType OT,
577                                      StorageClass S, Expr *DefArg);
578
579   void setOriginalType(QualType T) { OriginalType = T; }
580
581   // Implement isa/cast/dyncast/etc.
582   static bool classof(const Decl *D) { return D->getKind() == OriginalParmVar; }
583   static bool classof(const OriginalParmVarDecl *D) { return true; }
584 };
585   
586 /// FunctionDecl - An instance of this class is created to represent a
587 /// function declaration or definition. 
588 ///
589 /// Since a given function can be declared several times in a program,
590 /// there may be several FunctionDecls that correspond to that
591 /// function. Only one of those FunctionDecls will be found when
592 /// traversing the list of declarations in the context of the
593 /// FunctionDecl (e.g., the translation unit); this FunctionDecl
594 /// contains all of the information known about the function. Other,
595 /// previous declarations of the function are available via the
596 /// getPreviousDeclaration() chain. 
597 class FunctionDecl : public ValueDecl, public DeclContext {
598 public:
599   enum StorageClass {
600     None, Extern, Static, PrivateExtern
601   };
602 private:
603   /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
604   /// parameters of this function.  This is null if a prototype or if there are
605   /// no formals.  TODO: we could allocate this space immediately after the
606   /// FunctionDecl object to save an allocation like FunctionType does.
607   ParmVarDecl **ParamInfo;
608   
609   LazyDeclStmtPtr Body;
610   
611   /// PreviousDeclaration - A link to the previous declaration of this
612   /// same function, NULL if this is the first declaration. For
613   /// example, in the following code, the PreviousDeclaration can be
614   /// traversed several times to see all three declarations of the
615   /// function "f", the last of which is also a definition.
616   ///
617   ///   int f(int x, int y = 1);
618   ///   int f(int x = 0, int y);
619   ///   int f(int x, int y) { return x + y; }
620   FunctionDecl *PreviousDeclaration;
621
622   // FIXME: This can be packed into the bitfields in Decl.
623   // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum
624   unsigned SClass : 2;
625   bool IsInline : 1;
626   bool C99InlineDefinition : 1;
627   bool IsVirtualAsWritten : 1;
628   bool IsPure : 1;
629   bool HasInheritedPrototype : 1;
630   bool HasWrittenPrototype : 1;
631   bool IsDeleted : 1;
632
633   // Move to DeclGroup when it is implemented.
634   SourceLocation TypeSpecStartLoc;
635
636   /// \brief The template or declaration that this declaration
637   /// describes or was instantiated from, respectively.
638   /// 
639   /// For non-templates, this value will be NULL. For function
640   /// declarations that describe a function template, this will be a
641   /// pointer to a FunctionTemplateDecl. For member functions
642   /// of class template specializations, this will be the
643   /// FunctionDecl from which the member function was instantiated.
644   llvm::PointerUnion<FunctionTemplateDecl*, FunctionDecl*>
645     TemplateOrInstantiation;
646
647 protected:
648   FunctionDecl(Kind DK, DeclContext *DC, SourceLocation L,
649                DeclarationName N, QualType T,
650                StorageClass S, bool isInline,
651                SourceLocation TSSL = SourceLocation())
652     : ValueDecl(DK, DC, L, N, T), 
653       DeclContext(DK),
654       ParamInfo(0), Body(), PreviousDeclaration(0),
655       SClass(S), IsInline(isInline), C99InlineDefinition(false), 
656       IsVirtualAsWritten(false), IsPure(false), HasInheritedPrototype(false), 
657       HasWrittenPrototype(true), IsDeleted(false), TypeSpecStartLoc(TSSL),
658       TemplateOrInstantiation() {}
659
660   virtual ~FunctionDecl() {}
661   virtual void Destroy(ASTContext& C);
662
663 public:
664   static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
665                               DeclarationName N, QualType T, 
666                               StorageClass S = None, bool isInline = false,
667                               bool hasWrittenPrototype = true,
668                               SourceLocation TSStartLoc = SourceLocation());  
669   
670   SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; }
671   void setTypeSpecStartLoc(SourceLocation TS) { TypeSpecStartLoc = TS; }
672
673   /// getBody - Retrieve the body (definition) of the function. The
674   /// function body might be in any of the (re-)declarations of this
675   /// function. The variant that accepts a FunctionDecl pointer will
676   /// set that function declaration to the actual declaration
677   /// containing the body (if there is one).
678   Stmt *getBody(ASTContext &Context, const FunctionDecl *&Definition) const;
679
680   virtual Stmt *getBody(ASTContext &Context) const {
681     const FunctionDecl* Definition;
682     return getBody(Context, Definition);
683   }
684
685   /// \brief If the function has a body that is immediately available,
686   /// return it.
687   Stmt *getBodyIfAvailable() const;
688
689   /// isThisDeclarationADefinition - Returns whether this specific
690   /// declaration of the function is also a definition. This does not
691   /// determine whether the function has been defined (e.g., in a
692   /// previous definition); for that information, use getBody.
693   /// FIXME: Should return true if function is deleted or defaulted. However,
694   /// CodeGenModule.cpp uses it, and I don't know if this would break it.
695   bool isThisDeclarationADefinition() const { return Body; }
696
697   void setBody(Stmt *B) { Body = B; }
698   void setLazyBody(uint64_t Offset) { Body = Offset; }
699
700   /// Whether this function is marked as virtual explicitly.
701   bool isVirtualAsWritten() const { return IsVirtualAsWritten; }
702   void setVirtualAsWritten(bool V) { IsVirtualAsWritten = V; }
703
704   /// Whether this virtual function is pure, i.e. makes the containing class
705   /// abstract.
706   bool isPure() const { return IsPure; }
707   void setPure(bool P = true) { IsPure = P; }
708
709   /// \brief Whether this function has a prototype, either because one
710   /// was explicitly written or because it was "inherited" by merging
711   /// a declaration without a prototype with a declaration that has a
712   /// prototype.
713   bool hasPrototype() const { 
714     return HasWrittenPrototype || HasInheritedPrototype; 
715   }
716   
717   bool hasWrittenPrototype() const { return HasWrittenPrototype; }
718   void setHasWrittenPrototype(bool P) { HasWrittenPrototype = P; }
719
720   /// \brief Whether this function inherited its prototype from a
721   /// previous declaration.
722   bool hasInheritedPrototype() const { return HasInheritedPrototype; }
723   void setHasInheritedPrototype(bool P = true) { HasInheritedPrototype = P; }
724
725   /// \brief Whether this function has been deleted.
726   ///
727   /// A function that is "deleted" (via the C++0x "= delete" syntax)
728   /// acts like a normal function, except that it cannot actually be
729   /// called or have its address taken. Deleted functions are
730   /// typically used in C++ overload resolution to attract arguments
731   /// whose type or lvalue/rvalue-ness would permit the use of a
732   /// different overload that would behave incorrectly. For example,
733   /// one might use deleted functions to ban implicit conversion from
734   /// a floating-point number to an Integer type:
735   ///
736   /// @code
737   /// struct Integer {
738   ///   Integer(long); // construct from a long
739   ///   Integer(double) = delete; // no construction from float or double
740   ///   Integer(long double) = delete; // no construction from long double
741   /// };
742   /// @endcode
743   bool isDeleted() const { return IsDeleted; }
744   void setDeleted(bool D = true) { IsDeleted = D; }
745
746   /// \brief Determines whether this is a function "main", which is
747   /// the entry point into an executable program.
748   bool isMain() const;
749
750   /// \brief Determines whether this function is a function with
751   /// external, C linkage.
752   bool isExternC(ASTContext &Context) const;
753
754   /// \brief Determines whether this is a global function.
755   bool isGlobal() const;
756
757   /// getPreviousDeclaration - Return the previous declaration of this
758   /// function.
759   const FunctionDecl *getPreviousDeclaration() const {
760     return PreviousDeclaration;
761   }
762
763   void setPreviousDeclaration(FunctionDecl * PrevDecl) {
764     PreviousDeclaration = PrevDecl;
765   }
766
767   unsigned getBuiltinID(ASTContext &Context) const;
768
769   unsigned getNumParmVarDeclsFromType() const;
770   
771   // Iterator access to formal parameters.
772   unsigned param_size() const { return getNumParams(); }
773   typedef ParmVarDecl **param_iterator;
774   typedef ParmVarDecl * const *param_const_iterator;
775   
776   param_iterator param_begin() { return ParamInfo; }
777   param_iterator param_end()   { return ParamInfo+param_size(); }
778   
779   param_const_iterator param_begin() const { return ParamInfo; }
780   param_const_iterator param_end() const   { return ParamInfo+param_size(); }
781   
782   /// getNumParams - Return the number of parameters this function must have
783   /// based on its functiontype.  This is the length of the PararmInfo array
784   /// after it has been created.
785   unsigned getNumParams() const;
786   
787   const ParmVarDecl *getParamDecl(unsigned i) const {
788     assert(i < getNumParams() && "Illegal param #");
789     return ParamInfo[i];
790   }
791   ParmVarDecl *getParamDecl(unsigned i) {
792     assert(i < getNumParams() && "Illegal param #");
793     return ParamInfo[i];
794   }
795   void setParams(ASTContext& C, ParmVarDecl **NewParamInfo, unsigned NumParams);
796
797   /// getMinRequiredArguments - Returns the minimum number of arguments
798   /// needed to call this function. This may be fewer than the number of
799   /// function parameters, if some of the parameters have default
800   /// arguments (in C++).
801   unsigned getMinRequiredArguments() const;
802
803   QualType getResultType() const { 
804     return getType()->getAsFunctionType()->getResultType();
805   }
806   StorageClass getStorageClass() const { return StorageClass(SClass); }
807   void setStorageClass(StorageClass SC) { SClass = SC; }
808
809   bool isInline() const { return IsInline; }
810   void setInline(bool I) { IsInline = I; }
811
812   /// \brief Whether this function is an "inline definition" as
813   /// defined by C99.
814   bool isC99InlineDefinition() const { return C99InlineDefinition; }
815   void setC99InlineDefinition(bool I) { C99InlineDefinition = I; }
816
817   /// \brief Determines whether this function has a gnu_inline
818   /// attribute that affects its semantics.
819   ///
820   /// The gnu_inline attribute only introduces GNU inline semantics
821   /// when all of the inline declarations of the function are marked
822   /// gnu_inline.
823   bool hasActiveGNUInlineAttribute() const;
824
825   /// \brief Determines whether this function is a GNU "extern
826   /// inline", which is roughly the opposite of a C99 "extern inline"
827   /// function.
828   bool isExternGNUInline() const;
829
830   /// isOverloadedOperator - Whether this function declaration
831   /// represents an C++ overloaded operator, e.g., "operator+".
832   bool isOverloadedOperator() const { 
833     return getOverloadedOperator() != OO_None;
834   };
835
836   OverloadedOperatorKind getOverloadedOperator() const;
837
838   /// \brief If this function is an instantiation of a member function
839   /// of a class template specialization, retrieves the function from
840   /// which it was instantiated.
841   ///
842   /// This routine will return non-NULL for (non-templated) member
843   /// functions of class templates and for instantiations of function
844   /// templates. For example, given:
845   ///
846   /// \code
847   /// template<typename T>
848   /// struct X {
849   ///   void f(T);
850   /// };
851   /// \endcode
852   ///
853   /// The declaration for X<int>::f is a (non-templated) FunctionDecl
854   /// whose parent is the class template specialization X<int>. For
855   /// this declaration, getInstantiatedFromFunction() will return
856   /// the FunctionDecl X<T>::A. When a complete definition of
857   /// X<int>::A is required, it will be instantiated from the
858   /// declaration returned by getInstantiatedFromMemberFunction().
859   FunctionDecl *getInstantiatedFromMemberFunction() const {
860     return TemplateOrInstantiation.dyn_cast<FunctionDecl*>();
861   }
862
863   /// \brief Specify that this record is an instantiation of the
864   /// member function RD.
865   void setInstantiationOfMemberFunction(FunctionDecl *RD) { 
866     TemplateOrInstantiation = RD;
867   }
868
869   /// \brief Retrieves the function template that is described by this
870   /// function declaration.
871   ///
872   /// Every function template is represented as a FunctionTemplateDecl
873   /// and a FunctionDecl (or something derived from FunctionDecl). The
874   /// former contains template properties (such as the template
875   /// parameter lists) while the latter contains the actual
876   /// description of the template's
877   /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the
878   /// FunctionDecl that describes the function template,
879   /// getDescribedFunctionTemplate() retrieves the
880   /// FunctionTemplateDecl from a FunctionDecl.
881   FunctionTemplateDecl *getDescribedFunctionTemplate() const {
882     return TemplateOrInstantiation.dyn_cast<FunctionTemplateDecl*>();
883   }
884
885   void setDescribedFunctionTemplate(FunctionTemplateDecl *Template) {
886     TemplateOrInstantiation = Template;
887   }
888
889   // Implement isa/cast/dyncast/etc.
890   static bool classof(const Decl *D) {
891     return D->getKind() >= FunctionFirst && D->getKind() <= FunctionLast;
892   }
893   static bool classof(const FunctionDecl *D) { return true; }
894   static DeclContext *castToDeclContext(const FunctionDecl *D) {
895     return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D));
896   }
897   static FunctionDecl *castFromDeclContext(const DeclContext *DC) {
898     return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC));
899   }
900 };
901
902
903 /// FieldDecl - An instance of this class is created by Sema::ActOnField to 
904 /// represent a member of a struct/union/class.
905 class FieldDecl : public ValueDecl {
906   // FIXME: This can be packed into the bitfields in Decl.
907   bool Mutable : 1;
908   Expr *BitWidth;
909 protected:
910   FieldDecl(Kind DK, DeclContext *DC, SourceLocation L, 
911             IdentifierInfo *Id, QualType T, Expr *BW, bool Mutable)
912     : ValueDecl(DK, DC, L, Id, T), Mutable(Mutable), BitWidth(BW)
913       { }
914
915 public:
916   static FieldDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L, 
917                            IdentifierInfo *Id, QualType T, Expr *BW, 
918                            bool Mutable);
919
920   /// isMutable - Determines whether this field is mutable (C++ only).
921   bool isMutable() const { return Mutable; }
922
923   /// \brief Set whether this field is mutable (C++ only).
924   void setMutable(bool M) { Mutable = M; }
925
926   /// isBitfield - Determines whether this field is a bitfield.
927   bool isBitField() const { return BitWidth != NULL; }
928
929   /// @brief Determines whether this is an unnamed bitfield.
930   bool isUnnamedBitfield() const { return BitWidth != NULL && !getDeclName(); }
931
932   /// isAnonymousStructOrUnion - Determines whether this field is a
933   /// representative for an anonymous struct or union. Such fields are
934   /// unnamed and are implicitly generated by the implementation to
935   /// store the data for the anonymous union or struct.
936   bool isAnonymousStructOrUnion() const;
937
938   Expr *getBitWidth() const { return BitWidth; }
939   void setBitWidth(Expr *BW) { BitWidth = BW; }
940
941   // Implement isa/cast/dyncast/etc.
942   static bool classof(const Decl *D) {
943     return D->getKind() >= FieldFirst && D->getKind() <= FieldLast;
944   }
945   static bool classof(const FieldDecl *D) { return true; }
946 };
947
948 /// EnumConstantDecl - An instance of this object exists for each enum constant
949 /// that is defined.  For example, in "enum X {a,b}", each of a/b are
950 /// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
951 /// TagType for the X EnumDecl.
952 class EnumConstantDecl : public ValueDecl {
953   Stmt *Init; // an integer constant expression
954   llvm::APSInt Val; // The value.
955 protected:
956   EnumConstantDecl(DeclContext *DC, SourceLocation L,
957                    IdentifierInfo *Id, QualType T, Expr *E,
958                    const llvm::APSInt &V)
959     : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {}
960
961   virtual ~EnumConstantDecl() {}
962 public:
963
964   static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC,
965                                   SourceLocation L, IdentifierInfo *Id,
966                                   QualType T, Expr *E,
967                                   const llvm::APSInt &V);
968   
969   virtual void Destroy(ASTContext& C);
970
971   const Expr *getInitExpr() const { return (const Expr*) Init; }
972   Expr *getInitExpr() { return (Expr*) Init; }
973   const llvm::APSInt &getInitVal() const { return Val; }
974
975   void setInitExpr(Expr *E) { Init = (Stmt*) E; }
976   void setInitVal(const llvm::APSInt &V) { Val = V; }
977   
978   // Implement isa/cast/dyncast/etc.
979   static bool classof(const Decl *D) { return D->getKind() == EnumConstant; }
980   static bool classof(const EnumConstantDecl *D) { return true; }
981   
982   friend class StmtIteratorBase;
983 };
984
985
986 /// TypeDecl - Represents a declaration of a type.
987 ///
988 class TypeDecl : public NamedDecl {
989   /// TypeForDecl - This indicates the Type object that represents
990   /// this TypeDecl.  It is a cache maintained by
991   /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and
992   /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl.
993   mutable Type *TypeForDecl;
994   friend class ASTContext;
995   friend class DeclContext;
996   friend class TagDecl;
997   friend class TemplateTypeParmDecl;
998   friend class ClassTemplateSpecializationDecl;
999   friend class TagType;
1000
1001 protected:
1002   TypeDecl(Kind DK, DeclContext *DC, SourceLocation L,
1003            IdentifierInfo *Id)
1004     : NamedDecl(DK, DC, L, Id), TypeForDecl(0) {}
1005
1006 public:
1007   // Low-level accessor
1008   Type *getTypeForDecl() const { return TypeForDecl; }
1009   void setTypeForDecl(Type *TD) { TypeForDecl = TD; }
1010
1011   // Implement isa/cast/dyncast/etc.
1012   static bool classof(const Decl *D) {
1013     return D->getKind() >= TypeFirst && D->getKind() <= TypeLast;
1014   }
1015   static bool classof(const TypeDecl *D) { return true; }
1016 };
1017
1018
1019 class TypedefDecl : public TypeDecl {
1020   /// UnderlyingType - This is the type the typedef is set to.
1021   QualType UnderlyingType;
1022   TypedefDecl(DeclContext *DC, SourceLocation L,
1023               IdentifierInfo *Id, QualType T) 
1024     : TypeDecl(Typedef, DC, L, Id), UnderlyingType(T) {}
1025
1026   virtual ~TypedefDecl() {}
1027 public:
1028   
1029   static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
1030                              SourceLocation L,IdentifierInfo *Id,
1031                              QualType T);
1032   
1033   QualType getUnderlyingType() const { return UnderlyingType; }
1034   void setUnderlyingType(QualType newType) { UnderlyingType = newType; }
1035
1036   // Implement isa/cast/dyncast/etc.
1037   static bool classof(const Decl *D) { return D->getKind() == Typedef; }
1038   static bool classof(const TypedefDecl *D) { return true; }
1039 };
1040
1041 class TypedefDecl;
1042   
1043 /// TagDecl - Represents the declaration of a struct/union/class/enum.
1044 class TagDecl : public TypeDecl, public DeclContext {
1045 public:
1046   enum TagKind {
1047     TK_struct,
1048     TK_union,
1049     TK_class,
1050     TK_enum
1051   };
1052
1053 private:
1054   // FIXME: This can be packed into the bitfields in Decl.
1055   /// TagDeclKind - The TagKind enum.
1056   unsigned TagDeclKind : 2;
1057
1058   /// IsDefinition - True if this is a definition ("struct foo {};"), false if
1059   /// it is a declaration ("struct foo;").
1060   bool IsDefinition : 1;
1061   
1062   /// TypedefForAnonDecl - If a TagDecl is anonymous and part of a typedef,
1063   /// this points to the TypedefDecl. Used for mangling.
1064   TypedefDecl *TypedefForAnonDecl;
1065   
1066 protected:
1067   TagDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
1068           IdentifierInfo *Id)
1069     : TypeDecl(DK, DC, L, Id), DeclContext(DK), TypedefForAnonDecl(0) {
1070     assert((DK != Enum || TK == TK_enum) &&"EnumDecl not matched with TK_enum");
1071     TagDeclKind = TK;
1072     IsDefinition = false;
1073   }
1074 public:
1075   
1076   /// isDefinition - Return true if this decl has its body specified.
1077   bool isDefinition() const {
1078     return IsDefinition;
1079   }
1080
1081   /// \brief Whether this declaration declares a type that is
1082   /// dependent, i.e., a type that somehow depends on template
1083   /// parameters.
1084   bool isDependentType() const { return isDependentContext(); }
1085
1086   /// @brief Starts the definition of this tag declaration.
1087   /// 
1088   /// This method should be invoked at the beginning of the definition
1089   /// of this tag declaration. It will set the tag type into a state
1090   /// where it is in the process of being defined.
1091   void startDefinition();
1092
1093   /// @brief Completes the definition of this tag declaration.
1094   void completeDefinition();
1095
1096   /// getDefinition - Returns the TagDecl that actually defines this 
1097   ///  struct/union/class/enum.  When determining whether or not a
1098   ///  struct/union/class/enum is completely defined, one should use this method
1099   ///  as opposed to 'isDefinition'.  'isDefinition' indicates whether or not a
1100   ///  specific TagDecl is defining declaration, not whether or not the
1101   ///  struct/union/class/enum type is defined.  This method returns NULL if
1102   ///  there is no TagDecl that defines the struct/union/class/enum.
1103   TagDecl* getDefinition(ASTContext& C) const;
1104   
1105   const char *getKindName() const {
1106     switch (getTagKind()) {
1107     default: assert(0 && "Unknown TagKind!");
1108     case TK_struct: return "struct";
1109     case TK_union:  return "union";
1110     case TK_class:  return "class";
1111     case TK_enum:   return "enum";
1112     }
1113   }
1114
1115   TagKind getTagKind() const {
1116     return TagKind(TagDeclKind);
1117   }
1118
1119   void setTagKind(TagKind TK) { TagDeclKind = TK; }
1120
1121   bool isStruct() const { return getTagKind() == TK_struct; }
1122   bool isClass()  const { return getTagKind() == TK_class; }
1123   bool isUnion()  const { return getTagKind() == TK_union; }
1124   bool isEnum()   const { return getTagKind() == TK_enum; }
1125   
1126   TypedefDecl *getTypedefForAnonDecl() const { return TypedefForAnonDecl; }
1127   void setTypedefForAnonDecl(TypedefDecl *TDD) { TypedefForAnonDecl = TDD; }
1128   
1129   // Implement isa/cast/dyncast/etc.
1130   static bool classof(const Decl *D) {
1131     return D->getKind() >= TagFirst && D->getKind() <= TagLast;
1132   }
1133   static bool classof(const TagDecl *D) { return true; }
1134
1135   static DeclContext *castToDeclContext(const TagDecl *D) {
1136     return static_cast<DeclContext *>(const_cast<TagDecl*>(D));
1137   }
1138   static TagDecl *castFromDeclContext(const DeclContext *DC) {
1139     return static_cast<TagDecl *>(const_cast<DeclContext*>(DC));
1140   }
1141
1142   void setDefinition(bool V) { IsDefinition = V; }
1143 };
1144
1145 /// EnumDecl - Represents an enum.  As an extension, we allow forward-declared
1146 /// enums.
1147 class EnumDecl : public TagDecl {
1148   /// IntegerType - This represent the integer type that the enum corresponds
1149   /// to for code generation purposes.  Note that the enumerator constants may
1150   /// have a different type than this does.
1151   QualType IntegerType;
1152
1153   /// \brief If the enumeration was instantiated from an enumeration
1154   /// within a class or function template, this pointer refers to the
1155   /// enumeration declared within the template.
1156   EnumDecl *InstantiatedFrom;
1157
1158   EnumDecl(DeclContext *DC, SourceLocation L,
1159            IdentifierInfo *Id)
1160     : TagDecl(Enum, TK_enum, DC, L, Id), InstantiatedFrom(0) {
1161       IntegerType = QualType();
1162     }
1163 public:
1164   static EnumDecl *Create(ASTContext &C, DeclContext *DC,
1165                           SourceLocation L, IdentifierInfo *Id,
1166                           EnumDecl *PrevDecl);
1167   
1168   virtual void Destroy(ASTContext& C);
1169
1170   /// completeDefinition - When created, the EnumDecl corresponds to a
1171   /// forward-declared enum. This method is used to mark the
1172   /// declaration as being defined; it's enumerators have already been
1173   /// added (via DeclContext::addDecl). NewType is the new underlying
1174   /// type of the enumeration type.
1175   void completeDefinition(ASTContext &C, QualType NewType);
1176   
1177   // enumerator_iterator - Iterates through the enumerators of this
1178   // enumeration.
1179   typedef specific_decl_iterator<EnumConstantDecl> enumerator_iterator;
1180
1181   enumerator_iterator enumerator_begin(ASTContext &Context) const { 
1182     return enumerator_iterator(this->decls_begin(Context));
1183   }
1184
1185   enumerator_iterator enumerator_end(ASTContext &Context) const { 
1186     return enumerator_iterator(this->decls_end(Context));
1187   }
1188
1189   /// getIntegerType - Return the integer type this enum decl corresponds to.
1190   /// This returns a null qualtype for an enum forward definition.
1191   QualType getIntegerType() const { return IntegerType; }
1192
1193   /// \brief Set the underlying integer type.
1194   void setIntegerType(QualType T) { IntegerType = T; }
1195
1196   /// \brief Returns the enumeration (declared within the template)
1197   /// from which this enumeration type was instantiated, or NULL if
1198   /// this enumeration was not instantiated from any template.
1199   EnumDecl *getInstantiatedFromMemberEnum() const {
1200     return InstantiatedFrom;
1201   }
1202
1203   void setInstantiationOfMemberEnum(EnumDecl *IF) { InstantiatedFrom = IF; }
1204
1205   static bool classof(const Decl *D) { return D->getKind() == Enum; }
1206   static bool classof(const EnumDecl *D) { return true; }
1207 };
1208
1209
1210 /// RecordDecl - Represents a struct/union/class.  For example:
1211 ///   struct X;                  // Forward declaration, no "body".
1212 ///   union Y { int A, B; };     // Has body with members A and B (FieldDecls).
1213 /// This decl will be marked invalid if *any* members are invalid.
1214 ///
1215 class RecordDecl : public TagDecl {
1216   // FIXME: This can be packed into the bitfields in Decl.
1217   /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
1218   /// array member (e.g. int X[]) or if this union contains a struct that does.
1219   /// If so, this cannot be contained in arrays or other structs as a member.
1220   bool HasFlexibleArrayMember : 1;
1221
1222   /// AnonymousStructOrUnion - Whether this is the type of an
1223   /// anonymous struct or union.
1224   bool AnonymousStructOrUnion : 1;
1225
1226 protected:
1227   RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
1228              SourceLocation L, IdentifierInfo *Id);
1229   virtual ~RecordDecl();
1230
1231 public:
1232   static RecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
1233                             SourceLocation L, IdentifierInfo *Id,
1234                             RecordDecl* PrevDecl = 0);
1235
1236   virtual void Destroy(ASTContext& C);
1237       
1238   bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
1239   void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
1240
1241   /// isAnonymousStructOrUnion - Whether this is an anonymous struct
1242   /// or union. To be an anonymous struct or union, it must have been
1243   /// declared without a name and there must be no objects of this
1244   /// type declared, e.g.,
1245   /// @code
1246   ///   union { int i; float f; };
1247   /// @endcode   
1248   /// is an anonymous union but neither of the following are:
1249   /// @code
1250   ///  union X { int i; float f; };
1251   ///  union { int i; float f; } obj;
1252   /// @endcode
1253   bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion; }
1254   void setAnonymousStructOrUnion(bool Anon) {
1255     AnonymousStructOrUnion = Anon;
1256   }
1257
1258   /// \brief Determines whether this declaration represents the
1259   /// injected class name.
1260   ///
1261   /// The injected class name in C++ is the name of the class that
1262   /// appears inside the class itself. For example:
1263   ///
1264   /// \code
1265   /// struct C {
1266   ///   // C is implicitly declared here as a synonym for the class name.
1267   /// };
1268   ///
1269   /// C::C c; // same as "C c;"
1270   /// \endcode
1271   bool isInjectedClassName() const;
1272
1273   /// getDefinition - Returns the RecordDecl that actually defines this 
1274   ///  struct/union/class.  When determining whether or not a struct/union/class
1275   ///  is completely defined, one should use this method as opposed to
1276   ///  'isDefinition'.  'isDefinition' indicates whether or not a specific
1277   ///  RecordDecl is defining declaration, not whether or not the record
1278   ///  type is defined.  This method returns NULL if there is no RecordDecl
1279   ///  that defines the struct/union/tag.
1280   RecordDecl* getDefinition(ASTContext& C) const {
1281     return cast_or_null<RecordDecl>(TagDecl::getDefinition(C));
1282   }
1283   
1284   // Iterator access to field members. The field iterator only visits
1285   // the non-static data members of this class, ignoring any static
1286   // data members, functions, constructors, destructors, etc.
1287   typedef specific_decl_iterator<FieldDecl> field_iterator;
1288
1289   field_iterator field_begin(ASTContext &Context) const {
1290     return field_iterator(decls_begin(Context));
1291   }
1292   field_iterator field_end(ASTContext &Context) const {
1293     return field_iterator(decls_end(Context));
1294   }
1295
1296   // field_empty - Whether there are any fields (non-static data
1297   // members) in this record.
1298   bool field_empty(ASTContext &Context) const { 
1299     return field_begin(Context) == field_end(Context);
1300   }
1301
1302   /// completeDefinition - Notes that the definition of this type is
1303   /// now complete.
1304   void completeDefinition(ASTContext& C);
1305
1306   static bool classof(const Decl *D) {
1307     return D->getKind() >= RecordFirst && D->getKind() <= RecordLast;
1308   }
1309   static bool classof(const RecordDecl *D) { return true; }
1310 };
1311
1312 class FileScopeAsmDecl : public Decl {
1313   StringLiteral *AsmString;
1314   FileScopeAsmDecl(DeclContext *DC, SourceLocation L, StringLiteral *asmstring)
1315     : Decl(FileScopeAsm, DC, L), AsmString(asmstring) {}
1316 public:
1317   static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC,
1318                                   SourceLocation L, StringLiteral *Str);
1319
1320   const StringLiteral *getAsmString() const { return AsmString; }
1321   StringLiteral *getAsmString() { return AsmString; }
1322   void setAsmString(StringLiteral *Asm) { AsmString = Asm; }
1323
1324   static bool classof(const Decl *D) {
1325     return D->getKind() == FileScopeAsm;
1326   }
1327   static bool classof(const FileScopeAsmDecl *D) { return true; }  
1328 };
1329
1330 /// BlockDecl - This represents a block literal declaration, which is like an
1331 /// unnamed FunctionDecl.  For example:
1332 /// ^{ statement-body }   or   ^(int arg1, float arg2){ statement-body }
1333 ///
1334 class BlockDecl : public Decl, public DeclContext {
1335   // FIXME: This can be packed into the bitfields in Decl.
1336   bool isVariadic : 1;
1337   /// ParamInfo - new[]'d array of pointers to ParmVarDecls for the formal
1338   /// parameters of this function.  This is null if a prototype or if there are
1339   /// no formals.
1340   ParmVarDecl **ParamInfo;
1341   unsigned NumParams;
1342   
1343   Stmt *Body;
1344   
1345 protected:
1346   BlockDecl(DeclContext *DC, SourceLocation CaretLoc)
1347     : Decl(Block, DC, CaretLoc), DeclContext(Block), 
1348       isVariadic(false), ParamInfo(0), NumParams(0), Body(0) {}
1349
1350   virtual ~BlockDecl();
1351   virtual void Destroy(ASTContext& C);
1352
1353 public:
1354   static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L);
1355
1356   SourceLocation getCaretLocation() const { return getLocation(); }
1357
1358   bool IsVariadic() const { return isVariadic; }
1359   void setIsVariadic(bool value) { isVariadic = value; }
1360   
1361   CompoundStmt *getBody() const { return (CompoundStmt*) Body; }
1362   Stmt *getBody(ASTContext &C) const { return (Stmt*) Body; }
1363   void setBody(CompoundStmt *B) { Body = (Stmt*) B; }
1364
1365   // Iterator access to formal parameters.
1366   unsigned param_size() const { return getNumParams(); }
1367   typedef ParmVarDecl **param_iterator;
1368   typedef ParmVarDecl * const *param_const_iterator;
1369   
1370   bool param_empty() const { return NumParams == 0; }
1371   param_iterator param_begin()  { return ParamInfo; }
1372   param_iterator param_end()   { return ParamInfo+param_size(); }
1373   
1374   param_const_iterator param_begin() const { return ParamInfo; }
1375   param_const_iterator param_end() const   { return ParamInfo+param_size(); }
1376   
1377   unsigned getNumParams() const;
1378   const ParmVarDecl *getParamDecl(unsigned i) const {
1379     assert(i < getNumParams() && "Illegal param #");
1380     return ParamInfo[i];
1381   }
1382   ParmVarDecl *getParamDecl(unsigned i) {
1383     assert(i < getNumParams() && "Illegal param #");
1384     return ParamInfo[i];
1385   }
1386   void setParams(ASTContext& C, ParmVarDecl **NewParamInfo, unsigned NumParams);
1387     
1388   // Implement isa/cast/dyncast/etc.
1389   static bool classof(const Decl *D) { return D->getKind() == Block; }
1390   static bool classof(const BlockDecl *D) { return true; }  
1391   static DeclContext *castToDeclContext(const BlockDecl *D) {
1392     return static_cast<DeclContext *>(const_cast<BlockDecl*>(D));
1393   }
1394   static BlockDecl *castFromDeclContext(const DeclContext *DC) {
1395     return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC));
1396   }
1397 };
1398
1399 /// Insertion operator for diagnostics.  This allows sending NamedDecl's
1400 /// into a diagnostic with <<.
1401 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1402                                            NamedDecl* ND) {
1403   DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND), Diagnostic::ak_nameddecl);
1404   return DB;
1405 }
1406
1407 }  // end namespace clang
1408
1409 #endif