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