Decl.h revision d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8
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/DeclBase.h"
18#include "clang/Parse/AccessSpecifier.h"
19
20namespace clang {
21class Expr;
22class Stmt;
23class StringLiteral;
24class IdentifierInfo;
25
26/// TranslationUnitDecl - The top declaration context.
27/// FIXME: The TranslationUnit class should probably be modified to serve as
28/// the top decl context. It would have ownership of the top decls so that the
29/// AST is self-contained and easily de/serializable.
30class TranslationUnitDecl : public Decl, public DeclContext {
31  TranslationUnitDecl()
32    : Decl(TranslationUnit, SourceLocation()),
33      DeclContext(TranslationUnit) {}
34public:
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
40protected:
41  /// EmitImpl - Serialize this TranslationUnitDecl. Called by Decl::Emit.
42  virtual void EmitImpl(llvm::Serializer& S) const;
43
44  /// CreateImpl - Deserialize a TranslationUnitDecl.  Called by Decl::Create.
45  static TranslationUnitDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
46
47  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
48};
49
50/// NamedDecl - This represents a decl with an identifier for a name.  Many
51/// decls have names, but not ObjCMethodDecl, @class, etc.
52class NamedDecl : public Decl {
53  /// Identifier - The identifier for this declaration (e.g. the name for the
54  /// variable, the tag for a struct).
55  IdentifierInfo *Identifier;
56public:
57  NamedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id)
58   : Decl(DK, L), Identifier(Id) {}
59
60  IdentifierInfo *getIdentifier() const { return Identifier; }
61  const char *getName() const;
62
63  static bool classof(const Decl *D) {
64    return D->getKind() >= NamedFirst && D->getKind() <= NamedLast;
65  }
66  static bool classof(const NamedDecl *D) { return true; }
67
68protected:
69  void EmitInRec(llvm::Serializer& S) const;
70  void ReadInRec(llvm::Deserializer& D, ASTContext& C);
71};
72
73/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's
74/// and TypeDecl's.
75class ScopedDecl : public NamedDecl {
76  /// NextDeclarator - If this decl was part of a multi-declarator declaration,
77  /// such as "int X, Y, *Z;" this indicates Decl for the next declarator.
78  ScopedDecl *NextDeclarator;
79
80  /// When this decl is in scope while parsing, the Next field contains a
81  /// pointer to the shadowed decl of the same name.  When the scope is popped,
82  /// Decls are relinked onto a containing decl object.
83  ///
84  ScopedDecl *Next;
85
86  DeclContext *DeclCtx;
87
88protected:
89  ScopedDecl(Kind DK, DeclContext *DC, SourceLocation L,
90             IdentifierInfo *Id, ScopedDecl *PrevDecl)
91    : NamedDecl(DK, L, Id), NextDeclarator(PrevDecl), Next(0), DeclCtx(DC) {}
92
93public:
94  DeclContext *getDeclContext() const { return DeclCtx; }
95
96  ScopedDecl *getNext() const { return Next; }
97  void setNext(ScopedDecl *N) { Next = N; }
98
99  /// getNextDeclarator - If this decl was part of a multi-declarator
100  /// declaration, such as "int X, Y, *Z;" this returns the decl for the next
101  /// declarator.  Otherwise it returns null.
102  ScopedDecl *getNextDeclarator() { return NextDeclarator; }
103  const ScopedDecl *getNextDeclarator() const { return NextDeclarator; }
104  void setNextDeclarator(ScopedDecl *N) { NextDeclarator = N; }
105
106  // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
107  // scoped decl is defined outside the current function or method.  This is
108  // roughly global variables and functions, but also handles enums (which could
109  // be defined inside or outside a function etc).
110  bool isDefinedOutsideFunctionOrMethod() const {
111    if (getDeclContext())
112      return !getDeclContext()->isFunctionOrMethod();
113    else
114      return true;
115  }
116
117  // Implement isa/cast/dyncast/etc.
118  static bool classof(const Decl *D) {
119    return D->getKind() >= ScopedFirst && D->getKind() <= ScopedLast;
120  }
121  static bool classof(const ScopedDecl *D) { return true; }
122
123protected:
124  void EmitInRec(llvm::Serializer& S) const;
125  void ReadInRec(llvm::Deserializer& D, ASTContext& C);
126
127  void EmitOutRec(llvm::Serializer& S) const;
128  void ReadOutRec(llvm::Deserializer& D, ASTContext& C);
129
130  friend void Decl::Destroy(ASTContext& C);
131};
132
133/// NamespaceDecl - Represent a C++ namespace.
134class NamespaceDecl : public ScopedDecl, public DeclContext {
135  SourceLocation LBracLoc, RBracLoc;
136
137  // For extended namespace definitions:
138  //
139  // namespace A { int x; }
140  // namespace A { int y; }
141  //
142  // there will be one NamespaceDecl for each declaration.
143  // NextDeclarator points to the next extended declaration.
144  // OrigNamespace points to the original namespace declaration.
145  // OrigNamespace of the first namespace decl points to itself.
146
147  NamespaceDecl *OrigNamespace;
148
149  NamespaceDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id)
150    : ScopedDecl(Namespace, DC, L, Id, 0), DeclContext(Namespace) {
151      OrigNamespace = this;
152  }
153public:
154  static NamespaceDecl *Create(ASTContext &C, DeclContext *DC,
155                               SourceLocation L, IdentifierInfo *Id);
156
157  virtual void Destroy(ASTContext& C);
158
159  NamespaceDecl *getNextNamespace() {
160    return cast_or_null<NamespaceDecl>(getNextDeclarator());
161  }
162  const NamespaceDecl *getNextNamespace() const {
163    return cast_or_null<NamespaceDecl>(getNextDeclarator());
164  }
165  void setNextNamespace(NamespaceDecl *ND) { setNextDeclarator(ND); }
166
167  NamespaceDecl *getOriginalNamespace() const {
168    return OrigNamespace;
169  }
170  void setOriginalNamespace(NamespaceDecl *ND) { OrigNamespace = ND; }
171
172  SourceRange getSourceRange() const {
173    return SourceRange(LBracLoc, RBracLoc);
174  }
175
176  SourceLocation getLBracLoc() const { return LBracLoc; }
177  SourceLocation getRBracLoc() const { return RBracLoc; }
178  void setLBracLoc(SourceLocation LBrace) { LBracLoc = LBrace; }
179  void setRBracLoc(SourceLocation RBrace) { RBracLoc = RBrace; }
180
181  // Implement isa/cast/dyncast/etc.
182  static bool classof(const Decl *D) { return D->getKind() == Namespace; }
183  static bool classof(const NamespaceDecl *D) { return true; }
184
185protected:
186  /// EmitImpl - Serialize this NamespaceDecl. Called by Decl::Emit.
187  virtual void EmitImpl(llvm::Serializer& S) const;
188
189  /// CreateImpl - Deserialize a NamespaceDecl.  Called by Decl::Create.
190  static NamespaceDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
191
192  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
193};
194
195/// ValueDecl - Represent the declaration of a variable (in which case it is
196/// an lvalue) a function (in which case it is a function designator) or
197/// an enum constant.
198class ValueDecl : public ScopedDecl {
199  QualType DeclType;
200
201protected:
202  ValueDecl(Kind DK, DeclContext *DC, SourceLocation L,
203            IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl)
204    : ScopedDecl(DK, DC, L, Id, PrevDecl), DeclType(T) {}
205public:
206  QualType getType() const { return DeclType; }
207  void setType(QualType newType) { DeclType = newType; }
208
209  // Implement isa/cast/dyncast/etc.
210  static bool classof(const Decl *D) {
211    return D->getKind() >= ValueFirst && D->getKind() <= ValueLast;
212  }
213  static bool classof(const ValueDecl *D) { return true; }
214
215protected:
216  void EmitInRec(llvm::Serializer& S) const;
217  void ReadInRec(llvm::Deserializer& D, ASTContext& C);
218};
219
220/// VarDecl - An instance of this class is created to represent a variable
221/// declaration or definition.
222class VarDecl : public ValueDecl {
223public:
224  enum StorageClass {
225    None, Auto, Register, Extern, Static, PrivateExtern
226  };
227private:
228  Stmt *Init;
229  // FIXME: This can be packed into the bitfields in Decl.
230  unsigned SClass : 3;
231
232  friend class StmtIteratorBase;
233protected:
234  VarDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
235          QualType T, StorageClass SC, ScopedDecl *PrevDecl)
236    : ValueDecl(DK, DC, L, Id, T, PrevDecl), Init(0) { SClass = SC; }
237public:
238  static VarDecl *Create(ASTContext &C, DeclContext *DC,
239                         SourceLocation L, IdentifierInfo *Id,
240                         QualType T, StorageClass S, ScopedDecl *PrevDecl);
241
242  StorageClass getStorageClass() const { return (StorageClass)SClass; }
243
244  const Expr *getInit() const { return (const Expr*) Init; }
245  Expr *getInit() { return (Expr*) Init; }
246  void setInit(Expr *I) { Init = (Stmt*) I; }
247
248  /// hasLocalStorage - Returns true if a variable with function scope
249  ///  is a non-static local variable.
250  bool hasLocalStorage() const {
251    if (getStorageClass() == None)
252      return !isFileVarDecl();
253
254    // Return true for:  Auto, Register.
255    // Return false for: Extern, Static, PrivateExtern.
256
257    return getStorageClass() <= Register;
258  }
259
260  /// hasGlobalStorage - Returns true for all variables that do not
261  ///  have local storage.  This includs all global variables as well
262  ///  as static variables declared within a function.
263  bool hasGlobalStorage() const { return !hasLocalStorage(); }
264
265  /// isBlockVarDecl - Returns true for local variable declarations.  Note that
266  /// this includes static variables inside of functions.
267  ///
268  ///   void foo() { int x; static int y; extern int z; }
269  ///
270  bool isBlockVarDecl() const {
271    if (getKind() != Decl::Var)
272      return false;
273    if (DeclContext *DC = getDeclContext())
274      return DC->isFunctionOrMethod();
275    return false;
276  }
277
278  /// isFileVarDecl - Returns true for file scoped variable declaration.
279  bool isFileVarDecl() const {
280    if (getKind() != Decl::Var)
281      return false;
282    if (isa<TranslationUnitDecl>(getDeclContext()) ||
283        isa<NamespaceDecl>(getDeclContext()) )
284      return true;
285    return false;
286  }
287
288  // Implement isa/cast/dyncast/etc.
289  static bool classof(const Decl *D) {
290    return D->getKind() >= VarFirst && D->getKind() <= VarLast;
291  }
292  static bool classof(const VarDecl *D) { return true; }
293
294protected:
295  void EmitInRec(llvm::Serializer& S) const;
296  void ReadInRec(llvm::Deserializer& D, ASTContext& C);
297
298  void EmitOutRec(llvm::Serializer& S) const;
299  void ReadOutRec(llvm::Deserializer& D, ASTContext& C);
300
301  /// EmitImpl - Serialize this VarDecl. Called by Decl::Emit.
302  virtual void EmitImpl(llvm::Serializer& S) const;
303
304  /// ReadImpl - Deserialize this VarDecl. Called by subclasses.
305  virtual void ReadImpl(llvm::Deserializer& D, ASTContext& C);
306
307  /// CreateImpl - Deserialize a VarDecl.  Called by Decl::Create.
308  static VarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
309};
310
311/// ParmVarDecl - Represent a parameter to a function.
312class ParmVarDecl : public VarDecl {
313  // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
314  /// FIXME: Also can be paced into the bitfields in Decl.
315  /// in, inout, etc.
316  unsigned objcDeclQualifier : 6;
317
318  /// Default argument, if any.  [C++ Only]
319  Expr *DefaultArg;
320
321  ParmVarDecl(DeclContext *DC, SourceLocation L,
322              IdentifierInfo *Id, QualType T, StorageClass S,
323              Expr *DefArg, ScopedDecl *PrevDecl)
324    : VarDecl(ParmVar, DC, L, Id, T, S, PrevDecl),
325      objcDeclQualifier(OBJC_TQ_None), DefaultArg(DefArg) {}
326
327public:
328  static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
329                             SourceLocation L,IdentifierInfo *Id,
330                             QualType T, StorageClass S, Expr *DefArg,
331                             ScopedDecl *PrevDecl);
332
333  ObjCDeclQualifier getObjCDeclQualifier() const {
334    return ObjCDeclQualifier(objcDeclQualifier);
335  }
336  void setObjCDeclQualifier(ObjCDeclQualifier QTVal)
337  { objcDeclQualifier = QTVal; }
338
339  const Expr *getDefaultArg() const { return DefaultArg; }
340  Expr *getDefaultArg() { return DefaultArg; }
341  void setDefaultArg(Expr *defarg) { DefaultArg = defarg; }
342
343  // Implement isa/cast/dyncast/etc.
344  static bool classof(const Decl *D) { return D->getKind() == ParmVar; }
345  static bool classof(const ParmVarDecl *D) { return true; }
346
347protected:
348  /// EmitImpl - Serialize this ParmVarDecl. Called by Decl::Emit.
349  virtual void EmitImpl(llvm::Serializer& S) const;
350
351  /// CreateImpl - Deserialize a ParmVarDecl.  Called by Decl::Create.
352  static ParmVarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
353
354  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
355};
356
357/// FunctionDecl - An instance of this class is created to represent a
358/// function declaration or definition.
359///
360/// Since a given function can be declared several times in a program,
361/// there may be several FunctionDecls that correspond to that
362/// function. Only one of those FunctionDecls will be found when
363/// traversing the list of declarations in the context of the
364/// FunctionDecl (e.g., the translation unit); this FunctionDecl
365/// contains all of the information known about the function. Other,
366/// previous declarations of the function are available via the
367/// getPreviousDeclaration() chain.
368class FunctionDecl : public ValueDecl, public DeclContext {
369public:
370  enum StorageClass {
371    None, Extern, Static, PrivateExtern
372  };
373private:
374  /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
375  /// parameters of this function.  This is null if a prototype or if there are
376  /// no formals.  TODO: we could allocate this space immediately after the
377  /// FunctionDecl object to save an allocation like FunctionType does.
378  ParmVarDecl **ParamInfo;
379
380  Stmt *Body;  // Null if a prototype.
381
382  /// DeclChain - Linked list of declarations that are defined inside this
383  /// function.
384  ScopedDecl *DeclChain;
385
386  /// PreviousDeclaration - A link to the previous declaration of this
387  /// same function, NULL if this is the first declaration. For
388  /// example, in the following code, the PreviousDeclaration can be
389  /// traversed several times to see all three declarations of the
390  /// function "f", the last of which is also a definition.
391  ///
392  ///   int f(int x, int y = 1);
393  ///   int f(int x = 0, int y);
394  ///   int f(int x, int y) { return x + y; }
395  FunctionDecl *PreviousDeclaration;
396
397  // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum
398  unsigned SClass : 2;
399  bool IsInline : 1;
400  bool IsImplicit : 1;
401
402protected:
403  FunctionDecl(Kind DK, DeclContext *DC, SourceLocation L,
404               IdentifierInfo *Id, QualType T,
405               StorageClass S, bool isInline, ScopedDecl *PrevDecl)
406    : ValueDecl(DK, DC, L, Id, T, PrevDecl),
407      DeclContext(DK),
408      ParamInfo(0), Body(0), DeclChain(0), PreviousDeclaration(0),
409      SClass(S), IsInline(isInline), IsImplicit(0) {}
410
411  virtual ~FunctionDecl();
412  virtual void Destroy(ASTContext& C);
413
414public:
415  static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
416                              IdentifierInfo *Id, QualType T,
417                              StorageClass S = None, bool isInline = false,
418                              ScopedDecl *PrevDecl = 0);
419
420  /// getBody - Retrieve the body (definition) of the function. The
421  /// function body might be in any of the (re-)declarations of this
422  /// function. The variant that accepts a FunctionDecl pointer will
423  /// set that function declaration to the actual declaration
424  /// containing the body (if there is one).
425  Stmt *getBody(const FunctionDecl *&Definition) const;
426  Stmt *getBody() const {
427    const FunctionDecl* Definition;
428    return getBody(Definition);
429  }
430
431  /// isThisDeclarationADefinition - Returns whether this specific
432  /// declaration of the function is also a definition. This does not
433  /// determine whether the function has been defined (e.g., in a
434  /// previous definition); for that information, use getBody.
435  bool isThisDeclarationADefinition() const { return Body != 0; }
436
437  void setBody(Stmt *B) { Body = B; }
438
439  bool isImplicit() { return IsImplicit; }
440  void setImplicit() { IsImplicit = true; }
441
442  ScopedDecl *getDeclChain() const { return DeclChain; }
443  void setDeclChain(ScopedDecl *D) { DeclChain = D; }
444
445  /// getPreviousDeclaration - Return the previous declaration of this
446  /// function.
447  const FunctionDecl *getPreviousDeclaration() const {
448    return PreviousDeclaration;
449  }
450
451  void setPreviousDeclaration(FunctionDecl * PrevDecl) {
452    PreviousDeclaration = PrevDecl;
453  }
454
455  // Iterator access to formal parameters.
456  unsigned param_size() const { return getNumParams(); }
457  typedef ParmVarDecl **param_iterator;
458  typedef ParmVarDecl * const *param_const_iterator;
459
460  param_iterator param_begin() { return ParamInfo; }
461  param_iterator param_end() {
462
463    // Special-case for handling typedefs:
464    //
465    //  typedef void func_t(int x);
466    //  func_t a;
467    //
468    // In the case of the FunctionDecl for "a", there are no ParmVarDecls.
469
470    return ParamInfo ? ParamInfo+param_size() : 0x0;
471  }
472
473  param_const_iterator param_begin() const { return ParamInfo; }
474
475  param_const_iterator param_end() const {
476    return ParamInfo ? ParamInfo+param_size() : 0x0;
477  }
478
479  unsigned getNumParams() const;
480  const ParmVarDecl *getParamDecl(unsigned i) const {
481    assert(i < getNumParams() && "Illegal param #");
482    return ParamInfo[i];
483  }
484  ParmVarDecl *getParamDecl(unsigned i) {
485    assert(i < getNumParams() && "Illegal param #");
486    return ParamInfo[i];
487  }
488  void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams);
489
490  /// getMinRequiredArguments - Returns the minimum number of arguments
491  /// needed to call this function. This may be fewer than the number of
492  /// function parameters, if some of the parameters have default
493  /// arguments (in C++).
494  unsigned getMinRequiredArguments() const;
495
496  QualType getResultType() const {
497    return getType()->getAsFunctionType()->getResultType();
498  }
499  StorageClass getStorageClass() const { return StorageClass(SClass); }
500  bool isInline() const { return IsInline; }
501
502  // Implement isa/cast/dyncast/etc.
503  static bool classof(const Decl *D) {
504    return D->getKind() >= FunctionFirst && D->getKind() <= FunctionLast;
505  }
506  static bool classof(const FunctionDecl *D) { return true; }
507
508protected:
509  /// EmitImpl - Serialize this FunctionDecl.  Called by Decl::Emit.
510  virtual void EmitImpl(llvm::Serializer& S) const;
511
512  /// CreateImpl - Deserialize a FunctionDecl.  Called by Decl::Create.
513  static FunctionDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
514
515  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
516};
517
518
519/// FieldDecl - An instance of this class is created by Sema::ActOnField to
520/// represent a member of a struct/union/class.
521class FieldDecl : public NamedDecl {
522  QualType DeclType;
523  Expr *BitWidth;
524protected:
525  FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
526            Expr *BW = NULL)
527    : NamedDecl(DK, L, Id), DeclType(T), BitWidth(BW) {}
528  FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *BW)
529    : NamedDecl(Field, L, Id), DeclType(T), BitWidth(BW) {}
530public:
531  static FieldDecl *Create(ASTContext &C, SourceLocation L, IdentifierInfo *Id,
532                           QualType T, Expr *BW = NULL);
533
534  QualType getType() const { return DeclType; }
535  QualType getCanonicalType() const { return DeclType.getCanonicalType(); }
536
537  bool isBitField() const { return BitWidth != NULL; }
538  Expr *getBitWidth() const { return BitWidth; }
539  // Implement isa/cast/dyncast/etc.
540  static bool classof(const Decl *D) {
541    return D->getKind() >= FieldFirst && D->getKind() <= FieldLast;
542  }
543  static bool classof(const FieldDecl *D) { return true; }
544
545protected:
546  /// EmitImpl - Serialize this FieldDecl.  Called by Decl::Emit.
547  virtual void EmitImpl(llvm::Serializer& S) const;
548
549  /// CreateImpl - Deserialize a FieldDecl.  Called by Decl::Create.
550  static FieldDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
551
552  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
553};
554
555/// EnumConstantDecl - An instance of this object exists for each enum constant
556/// that is defined.  For example, in "enum X {a,b}", each of a/b are
557/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
558/// TagType for the X EnumDecl.
559class EnumConstantDecl : public ValueDecl {
560  Stmt *Init; // an integer constant expression
561  llvm::APSInt Val; // The value.
562protected:
563  EnumConstantDecl(DeclContext *DC, SourceLocation L,
564                   IdentifierInfo *Id, QualType T, Expr *E,
565                   const llvm::APSInt &V, ScopedDecl *PrevDecl)
566    : ValueDecl(EnumConstant, DC, L, Id, T, PrevDecl), Init((Stmt*)E), Val(V) {}
567
568  virtual ~EnumConstantDecl() {}
569public:
570
571  static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC,
572                                  SourceLocation L, IdentifierInfo *Id,
573                                  QualType T, Expr *E,
574                                  const llvm::APSInt &V, ScopedDecl *PrevDecl);
575
576  virtual void Destroy(ASTContext& C);
577
578  const Expr *getInitExpr() const { return (const Expr*) Init; }
579  Expr *getInitExpr() { return (Expr*) Init; }
580  const llvm::APSInt &getInitVal() const { return Val; }
581
582  void setInitExpr(Expr *E) { Init = (Stmt*) E; }
583  void setInitVal(const llvm::APSInt &V) { Val = V; }
584
585  // Implement isa/cast/dyncast/etc.
586  static bool classof(const Decl *D) { return D->getKind() == EnumConstant; }
587  static bool classof(const EnumConstantDecl *D) { return true; }
588
589  friend class StmtIteratorBase;
590
591protected:
592  /// EmitImpl - Serialize this EnumConstantDecl.  Called by Decl::Emit.
593  virtual void EmitImpl(llvm::Serializer& S) const;
594
595  /// CreateImpl - Deserialize a EnumConstantDecl.  Called by Decl::Create.
596  static EnumConstantDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
597
598  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
599};
600
601
602/// TypeDecl - Represents a declaration of a type.
603///
604class TypeDecl : public ScopedDecl {
605  /// TypeForDecl - This indicates the Type object that represents this
606  /// TypeDecl.  It is a cache maintained by ASTContext::getTypedefType and
607  /// ASTContext::getTagDeclType.
608  Type *TypeForDecl;
609  friend class ASTContext;
610protected:
611  TypeDecl(Kind DK, DeclContext *DC, SourceLocation L,
612           IdentifierInfo *Id, ScopedDecl *PrevDecl)
613    : ScopedDecl(DK, DC, L, Id, PrevDecl), TypeForDecl(0) {}
614public:
615  void setAccess(AccessSpecifier AS) { Access = AS; }
616  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
617
618  // Implement isa/cast/dyncast/etc.
619  static bool classof(const Decl *D) {
620    return D->getKind() >= TypeFirst && D->getKind() <= TypeLast;
621  }
622  static bool classof(const TypeDecl *D) { return true; }
623};
624
625
626class TypedefDecl : public TypeDecl {
627  /// UnderlyingType - This is the type the typedef is set to.
628  QualType UnderlyingType;
629  TypedefDecl(DeclContext *DC, SourceLocation L,
630              IdentifierInfo *Id, QualType T, ScopedDecl *PD)
631    : TypeDecl(Typedef, DC, L, Id, PD), UnderlyingType(T) {}
632
633  virtual ~TypedefDecl() {}
634public:
635
636  static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
637                             SourceLocation L,IdentifierInfo *Id,
638                             QualType T, ScopedDecl *PD);
639
640  QualType getUnderlyingType() const { return UnderlyingType; }
641  void setUnderlyingType(QualType newType) { UnderlyingType = newType; }
642
643  // Implement isa/cast/dyncast/etc.
644  static bool classof(const Decl *D) { return D->getKind() == Typedef; }
645  static bool classof(const TypedefDecl *D) { return true; }
646
647protected:
648  /// EmitImpl - Serialize this TypedefDecl.  Called by Decl::Emit.
649  virtual void EmitImpl(llvm::Serializer& S) const;
650
651  /// CreateImpl - Deserialize a TypedefDecl.  Called by Decl::Create.
652  static TypedefDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
653
654  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
655
656};
657
658
659/// TagDecl - Represents the declaration of a struct/union/class/enum.
660class TagDecl : public TypeDecl {
661  /// IsDefinition - True if this is a definition ("struct foo {};"), false if
662  /// it is a declaration ("struct foo;").
663  bool IsDefinition : 1;
664protected:
665  TagDecl(Kind DK, DeclContext *DC, SourceLocation L,
666          IdentifierInfo *Id, ScopedDecl *PrevDecl)
667    : TypeDecl(DK, DC, L, Id, PrevDecl) {
668    IsDefinition = false;
669  }
670public:
671
672  /// isDefinition - Return true if this decl has its body specified.
673  bool isDefinition() const {
674    return IsDefinition;
675  }
676
677  const char *getKindName() const {
678    switch (getKind()) {
679    default: assert(0 && "Unknown TagDecl!");
680    case Struct: return "struct";
681    case Union:  return "union";
682    case Class:  return "class";
683    case Enum:   return "enum";
684    }
685  }
686
687  // Implement isa/cast/dyncast/etc.
688  static bool classof(const Decl *D) {
689    return D->getKind() >= TagFirst && D->getKind() <= TagLast;
690  }
691  static bool classof(const TagDecl *D) { return true; }
692protected:
693  void setDefinition(bool V) { IsDefinition = V; }
694};
695
696/// EnumDecl - Represents an enum.  As an extension, we allow forward-declared
697/// enums.
698class EnumDecl : public TagDecl, public DeclContext {
699  /// ElementList - this is a linked list of EnumConstantDecl's which are linked
700  /// together through their getNextDeclarator pointers.
701  EnumConstantDecl *ElementList;
702
703  /// IntegerType - This represent the integer type that the enum corresponds
704  /// to for code generation purposes.  Note that the enumerator constants may
705  /// have a different type than this does.
706  QualType IntegerType;
707
708  EnumDecl(DeclContext *DC, SourceLocation L,
709           IdentifierInfo *Id, ScopedDecl *PrevDecl)
710    : TagDecl(Enum, DC, L, Id, PrevDecl), DeclContext(Enum) {
711      ElementList = 0;
712      IntegerType = QualType();
713    }
714public:
715  static EnumDecl *Create(ASTContext &C, DeclContext *DC,
716                          SourceLocation L, IdentifierInfo *Id,
717                          ScopedDecl *PrevDecl);
718
719  virtual void Destroy(ASTContext& C);
720
721  /// defineElements - When created, EnumDecl correspond to a forward declared
722  /// enum.  This method is used to mark the decl as being defined, with the
723  /// specified list of enums.
724  void defineElements(EnumConstantDecl *ListHead, QualType NewType) {
725    assert(!isDefinition() && "Cannot redefine enums!");
726    ElementList = ListHead;
727    setDefinition(true);
728
729    IntegerType = NewType;
730  }
731
732  /// getIntegerType - Return the integer type this enum decl corresponds to.
733  /// This returns a null qualtype for an enum forward definition.
734  QualType getIntegerType() const { return IntegerType; }
735
736  /// getEnumConstantList - Return the first EnumConstantDecl in the enum.
737  ///
738  EnumConstantDecl *getEnumConstantList() { return ElementList; }
739  const EnumConstantDecl *getEnumConstantList() const { return ElementList; }
740
741  static bool classof(const Decl *D) { return D->getKind() == Enum; }
742  static bool classof(const EnumDecl *D) { return true; }
743
744protected:
745  /// EmitImpl - Serialize this EnumDecl.  Called by Decl::Emit.
746  virtual void EmitImpl(llvm::Serializer& S) const;
747
748  /// CreateImpl - Deserialize a EnumDecl.  Called by Decl::Create.
749  static EnumDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
750
751  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
752};
753
754
755/// RecordDecl - Represents a struct/union/class.  For example:
756///   struct X;                  // Forward declaration, no "body".
757///   union Y { int A, B; };     // Has body with members A and B (FieldDecls).
758/// This decl will be marked invalid if *any* members are invalid.
759///
760class RecordDecl : public TagDecl {
761  /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
762  /// array member (e.g. int X[]) or if this union contains a struct that does.
763  /// If so, this cannot be contained in arrays or other structs as a member.
764  bool HasFlexibleArrayMember : 1;
765
766  /// Members/NumMembers - This is a new[]'d array of pointers to Decls.
767  FieldDecl **Members;   // Null if not defined.
768  int NumMembers;   // -1 if not defined.
769
770protected:
771  RecordDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
772             ScopedDecl *PrevDecl) : TagDecl(DK, DC, L, Id, PrevDecl) {
773    HasFlexibleArrayMember = false;
774    assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
775    Members = 0;
776    NumMembers = -1;
777  }
778public:
779
780  static RecordDecl *Create(ASTContext &C, Kind DK, DeclContext *DC,
781                            SourceLocation L, IdentifierInfo *Id,
782                            ScopedDecl *PrevDecl);
783
784  bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
785  void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
786
787  /// getNumMembers - Return the number of members, or -1 if this is a forward
788  /// definition.
789  int getNumMembers() const { return NumMembers; }
790  const FieldDecl *getMember(unsigned i) const { return Members[i]; }
791  FieldDecl *getMember(unsigned i) { return Members[i]; }
792
793  /// defineBody - When created, RecordDecl's correspond to a forward declared
794  /// record.  This method is used to mark the decl as being defined, with the
795  /// specified contents.
796  void defineBody(FieldDecl **Members, unsigned numMembers);
797
798  /// getMember - If the member doesn't exist, or there are no members, this
799  /// function will return 0;
800  FieldDecl *getMember(IdentifierInfo *name);
801
802  static bool classof(const Decl *D) {
803    return D->getKind() >= RecordFirst && D->getKind() <= RecordLast;
804  }
805  static bool classof(const RecordDecl *D) { return true; }
806
807protected:
808  /// EmitImpl - Serialize this RecordDecl.  Called by Decl::Emit.
809  virtual void EmitImpl(llvm::Serializer& S) const;
810
811  /// CreateImpl - Deserialize a RecordDecl.  Called by Decl::Create.
812  static RecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D, ASTContext& C);
813
814  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
815};
816
817class FileScopeAsmDecl : public Decl {
818  StringLiteral *AsmString;
819  FileScopeAsmDecl(SourceLocation L, StringLiteral *asmstring)
820    : Decl(FileScopeAsm, L), AsmString(asmstring) {}
821public:
822  static FileScopeAsmDecl *Create(ASTContext &C, SourceLocation L,
823                                  StringLiteral *Str);
824
825  const StringLiteral *getAsmString() const { return AsmString; }
826  StringLiteral *getAsmString() { return AsmString; }
827  static bool classof(const Decl *D) {
828    return D->getKind() == FileScopeAsm;
829  }
830  static bool classof(const FileScopeAsmDecl *D) { return true; }
831protected:
832  /// EmitImpl - Serialize this FileScopeAsmDecl. Called by Decl::Emit.
833  virtual void EmitImpl(llvm::Serializer& S) const;
834
835  /// CreateImpl - Deserialize a FileScopeAsmDecl.  Called by Decl::Create.
836  static FileScopeAsmDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
837
838  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
839};
840
841/// LinkageSpecDecl - This represents a linkage specification.  For example:
842///   extern "C" void foo();
843///
844class LinkageSpecDecl : public Decl {
845public:
846  /// LanguageIDs - Used to represent the language in a linkage
847  /// specification.  The values are part of the serialization abi for
848  /// ASTs and cannot be changed without altering that abi.  To help
849  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
850  /// from the dwarf standard.
851  enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002,
852                     lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 };
853private:
854  /// Language - The language for this linkage specification.
855  LanguageIDs Language;
856  /// D - This is the Decl of the linkage specification.
857  Decl *D;
858
859  LinkageSpecDecl(SourceLocation L, LanguageIDs lang, Decl *d)
860   : Decl(LinkageSpec, L), Language(lang), D(d) {}
861public:
862  static LinkageSpecDecl *Create(ASTContext &C, SourceLocation L,
863                                 LanguageIDs Lang, Decl *D);
864
865  LanguageIDs getLanguage() const { return Language; }
866  const Decl *getDecl() const { return D; }
867  Decl *getDecl() { return D; }
868
869  static bool classof(const Decl *D) {
870    return D->getKind() == LinkageSpec;
871  }
872  static bool classof(const LinkageSpecDecl *D) { return true; }
873
874protected:
875  void EmitInRec(llvm::Serializer& S) const;
876  void ReadInRec(llvm::Deserializer& D, ASTContext& C);
877};
878
879}  // end namespace clang
880
881#endif
882