Decl.h revision 2de4a9b1d6186f83d98975e02daeb71bd9605440
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/Attr.h"
18#include "clang/AST/Type.h"
19#include "clang/Basic/SourceLocation.h"
20#include "llvm/ADT/APSInt.h"
21#include "llvm/Bitcode/SerializationFwd.h"
22
23namespace clang {
24class Decl;
25}
26
27namespace clang {
28class Expr;
29class Stmt;
30class StringLiteral;
31class FunctionDecl;
32class IdentifierInfo;
33
34/// Decl - This represents one declaration (or definition), e.g. a variable,
35/// typedef, function, struct, etc.
36///
37class Decl {
38public:
39  enum Kind {
40    // This lists the concrete classes of Decl in order of the inheritance
41    // hierarchy.  This allows us to do efficient classof tests based on the
42    // enums below.   The commented out names are abstract class names.
43
44    // Decl
45    //   NamedDecl
46           Field,
47             ObjCIvar,
48           ObjCCategory,
49           ObjCCategoryImpl,
50           ObjCImplementation,
51           ObjCProtocol,
52           PropertyDecl,
53    //     ScopedDecl
54             CompatibleAlias,
55    //       TypeDecl
56               ObjCInterface,
57               Typedef,
58    //         TagDecl
59                 Enum,
60    //           RecordDecl,
61                   Struct,
62                   Union,
63                   Class,
64    //       ValueDecl
65               EnumConstant,
66               Function,
67    //         VarDecl
68                 BlockVar,
69                 FileVar,
70                 ParmVar,
71         ObjCMethod,
72         ObjCClass,
73         ObjCForwardProtocol,
74         LinkageSpec,
75   FileScopeAsm,
76
77    // For each non-leaf class, we now define a mapping to the first/last member
78    // of the class, to allow efficient classof.
79    NamedFirst  = Field,         NamedLast  = ParmVar,
80    FieldFirst  = Field,         FieldLast  = ObjCIvar,
81    ScopedFirst = CompatibleAlias, ScopedLast = ParmVar,
82    TypeFirst   = ObjCInterface, TypeLast   = Class,
83    TagFirst    = Enum         , TagLast    = Class,
84    RecordFirst = Struct       , RecordLast = Class,
85    ValueFirst  = EnumConstant , ValueLast  = ParmVar,
86    VarFirst    = BlockVar     , VarLast    = ParmVar
87  };
88
89  /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
90  /// labels, tags, members and ordinary identifiers.
91  enum IdentifierNamespace {
92    IDNS_Label,
93    IDNS_Tag,
94    IDNS_Member,
95    IDNS_Ordinary
96  };
97
98  /// ObjCDeclQualifier - Qualifier used on types in method declarations
99  /// for remote messaging. They are meant for the arguments though and
100  /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
101  enum ObjCDeclQualifier {
102    OBJC_TQ_None = 0x0,
103    OBJC_TQ_In = 0x1,
104    OBJC_TQ_Inout = 0x2,
105    OBJC_TQ_Out = 0x4,
106    OBJC_TQ_Bycopy = 0x8,
107    OBJC_TQ_Byref = 0x10,
108    OBJC_TQ_Oneway = 0x20
109  };
110
111private:
112  /// Loc - The location that this decl.
113  SourceLocation Loc;
114
115  /// DeclKind - This indicates which class this is.
116  Kind DeclKind   :  8;
117
118  /// InvalidDecl - This indicates a semantic error occurred.
119  unsigned int InvalidDecl :  1;
120
121  /// HasAttrs - This indicates whether the decl has attributes or not.
122  unsigned int HasAttrs : 1;
123protected:
124  Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0),
125    HasAttrs(false) {
126    if (Decl::CollectingStats()) addDeclKind(DK);
127  }
128
129  virtual ~Decl();
130
131public:
132  SourceLocation getLocation() const { return Loc; }
133  void setLocation(SourceLocation L) { Loc = L; }
134
135  Kind getKind() const { return DeclKind; }
136  const char *getDeclKindName() const;
137
138  void addAttr(Attr *attr);
139  const Attr *getAttrs() const;
140
141  template<typename T> const T *getAttr() const {
142    for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
143      if (const T *V = dyn_cast<T>(attr))
144        return V;
145
146    return 0;
147  }
148
149  /// setInvalidDecl - Indicates the Decl had a semantic error. This
150  /// allows for graceful error recovery.
151  void setInvalidDecl() { InvalidDecl = 1; }
152  bool isInvalidDecl() const { return (bool) InvalidDecl; }
153
154  IdentifierNamespace getIdentifierNamespace() const {
155    switch (DeclKind) {
156    default: assert(0 && "Unknown decl kind!");
157    case Typedef:
158    case Function:
159    case BlockVar:
160    case FileVar:
161    case ParmVar:
162    case EnumConstant:
163    case ObjCInterface:
164    case CompatibleAlias:
165      return IDNS_Ordinary;
166    case Struct:
167    case Union:
168    case Class:
169    case Enum:
170      return IDNS_Tag;
171    }
172  }
173  // global temp stats (until we have a per-module visitor)
174  static void addDeclKind(const Kind k);
175  static bool CollectingStats(bool enable=false);
176  static void PrintStats();
177
178  // Implement isa/cast/dyncast/etc.
179  static bool classof(const Decl *) { return true; }
180
181  /// Emit - Serialize this Decl to Bitcode.
182  void Emit(llvm::Serializer& S) const;
183
184  /// Create - Deserialize a Decl from Bitcode.
185  static Decl* Create(llvm::Deserializer& D);
186
187protected:
188  /// EmitImpl - Provides the subclass-specific serialization logic for
189  ///   serializing out a decl.
190  virtual void EmitImpl(llvm::Serializer& S) const {
191    // FIXME: This will eventually be a pure virtual function.
192    assert (false && "Not implemented.");
193  }
194
195  void EmitInRec(llvm::Serializer& S) const;
196  void ReadInRec(llvm::Deserializer& D);
197};
198
199/// NamedDecl - This represents a decl with an identifier for a name.  Many
200/// decls have names, but not ObjCMethodDecl, @class, etc.
201class NamedDecl : public Decl {
202  /// Identifier - The identifier for this declaration (e.g. the name for the
203  /// variable, the tag for a struct).
204  IdentifierInfo *Identifier;
205public:
206  NamedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id)
207   : Decl(DK, L), Identifier(Id) {}
208
209  IdentifierInfo *getIdentifier() const { return Identifier; }
210  const char *getName() const;
211
212  static bool classof(const Decl *D) {
213    return D->getKind() >= NamedFirst && D->getKind() <= NamedLast;
214  }
215  static bool classof(const NamedDecl *D) { return true; }
216
217protected:
218  void EmitInRec(llvm::Serializer& S) const;
219  void ReadInRec(llvm::Deserializer& D);
220};
221
222/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's
223/// and TypeDecl's.
224class ScopedDecl : public NamedDecl {
225  /// NextDeclarator - If this decl was part of a multi-declarator declaration,
226  /// such as "int X, Y, *Z;" this indicates Decl for the next declarator.
227  ScopedDecl *NextDeclarator;
228
229  /// When this decl is in scope while parsing, the Next field contains a
230  /// pointer to the shadowed decl of the same name.  When the scope is popped,
231  /// Decls are relinked onto a containing decl object.
232  ///
233  ScopedDecl *Next;
234
235protected:
236  ScopedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id,ScopedDecl *PrevDecl)
237    : NamedDecl(DK, L, Id), NextDeclarator(PrevDecl), Next(0) {}
238
239public:
240  ScopedDecl *getNext() const { return Next; }
241  void setNext(ScopedDecl *N) { Next = N; }
242
243  /// getNextDeclarator - If this decl was part of a multi-declarator
244  /// declaration, such as "int X, Y, *Z;" this returns the decl for the next
245  /// declarator.  Otherwise it returns null.
246  ScopedDecl *getNextDeclarator() { return NextDeclarator; }
247  const ScopedDecl *getNextDeclarator() const { return NextDeclarator; }
248  void setNextDeclarator(ScopedDecl *N) { NextDeclarator = N; }
249
250  // Implement isa/cast/dyncast/etc.
251  static bool classof(const Decl *D) {
252    return D->getKind() >= ScopedFirst && D->getKind() <= ScopedLast;
253  }
254  static bool classof(const ScopedDecl *D) { return true; }
255
256protected:
257  void EmitInRec(llvm::Serializer& S) const;
258  void ReadInRec(llvm::Deserializer& D);
259
260  void EmitOutRec(llvm::Serializer& S) const;
261  void ReadOutRec(llvm::Deserializer& D);
262};
263
264/// ValueDecl - Represent the declaration of a variable (in which case it is
265/// an lvalue) a function (in which case it is a function designator) or
266/// an enum constant.
267class ValueDecl : public ScopedDecl {
268  QualType DeclType;
269
270protected:
271  ValueDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
272            ScopedDecl *PrevDecl)
273    : ScopedDecl(DK, L, Id, PrevDecl), DeclType(T) {}
274public:
275  QualType getType() const { return DeclType; }
276  void setType(QualType newType) { DeclType = newType; }
277  QualType getCanonicalType() const { return DeclType.getCanonicalType(); }
278
279  // Implement isa/cast/dyncast/etc.
280  static bool classof(const Decl *D) {
281    return D->getKind() >= ValueFirst && D->getKind() <= ValueLast;
282  }
283  static bool classof(const ValueDecl *D) { return true; }
284
285protected:
286  void EmitInRec(llvm::Serializer& S) const;
287  void ReadInRec(llvm::Deserializer& D);
288};
289
290/// VarDecl - An instance of this class is created to represent a variable
291/// declaration or definition.
292class VarDecl : public ValueDecl {
293public:
294  enum StorageClass {
295    None, Extern, Static, Auto, Register, PrivateExtern
296  };
297  StorageClass getStorageClass() const { return (StorageClass)SClass; }
298
299  const Expr *getInit() const { return Init; }
300  Expr *getInit() { return Init; }
301  void setInit(Expr *I) { Init = I; }
302
303  /// hasAutoStorage - Returns true if either the implicit or explicit
304  ///  storage class of a variable is "auto."  In particular, variables
305  ///  declared within a function that lack a storage keyword are
306  ///  implicitly "auto", but are represented internally with a storage
307  ///  class of None.
308  bool hasAutoStorage() const {
309    return getStorageClass() == Auto ||
310          (getStorageClass() == None && getKind() != FileVar);
311  }
312
313  /// hasStaticStorage - Returns true if either the implicit or
314  ///  explicit storage class of a variable is "static."  In
315  ///  particular, variables declared within a file (outside of a
316  ///  function) that lack a storage keyword are implicitly "static,"
317  ///  but are represented internally with a storage class of "None".
318  bool hasStaticStorage() const {
319    if (getStorageClass() == Static) return true;
320    return getKind() == FileVar;
321  }
322
323  /// hasLocalStorage - Returns true if a variable with function scope
324  ///  is a non-static local variable.
325  bool hasLocalStorage() const {
326    return hasAutoStorage() || getStorageClass() == Register;
327  }
328
329  /// hasGlobalStorage - Returns true for all variables that do not
330  ///  have local storage.  This includs all global variables as well
331  ///  as static variables declared within a function.
332  bool hasGlobalStorage() const { return !hasAutoStorage(); }
333
334  // Implement isa/cast/dyncast/etc.
335  static bool classof(const Decl *D) {
336    return D->getKind() >= VarFirst && D->getKind() <= VarLast;
337  }
338  static bool classof(const VarDecl *D) { return true; }
339protected:
340  VarDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
341          StorageClass SC, ScopedDecl *PrevDecl)
342    : ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; }
343private:
344  Expr *Init;
345  // FIXME: This can be packed into the bitfields in Decl.
346  unsigned SClass : 3;
347
348  friend class StmtIteratorBase;
349
350protected:
351  void EmitInRec(llvm::Serializer& S) const;
352  void ReadInRec(llvm::Deserializer& D);
353
354  void EmitOutRec(llvm::Serializer& S) const;
355  void ReadOutRec(llvm::Deserializer& D);
356
357  /// EmitImpl - Serialize this VarDecl. Called by Decl::Emit.
358  virtual void EmitImpl(llvm::Serializer& S) const;
359
360  /// ReadImpl - Deserialize this VarDecl. Called by subclasses.
361  virtual void ReadImpl(llvm::Deserializer& S);
362};
363
364/// BlockVarDecl - Represent a local variable declaration.
365class BlockVarDecl : public VarDecl {
366public:
367  BlockVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
368               ScopedDecl *PrevDecl)
369    : VarDecl(BlockVar, L, Id, T, S, PrevDecl) {}
370
371  // Implement isa/cast/dyncast/etc.
372  static bool classof(const Decl *D) { return D->getKind() == BlockVar; }
373  static bool classof(const BlockVarDecl *D) { return true; }
374
375protected:
376  /// CreateImpl - Deserialize a BlockVarDecl.  Called by Decl::Create.
377  static BlockVarDecl* CreateImpl(llvm::Deserializer& D);
378
379  friend Decl* Decl::Create(llvm::Deserializer& D);
380};
381
382/// FileVarDecl - Represent a file scoped variable declaration. This
383/// will allow us to reason about external variable declarations and tentative
384/// definitions (C99 6.9.2p2) using our type system (without storing a
385/// pointer to the decl's scope, which is transient).
386class FileVarDecl : public VarDecl {
387public:
388  FileVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
389              ScopedDecl *PrevDecl)
390    : VarDecl(FileVar, L, Id, T, S, PrevDecl) {}
391
392  // Implement isa/cast/dyncast/etc.
393  static bool classof(const Decl *D) { return D->getKind() == FileVar; }
394  static bool classof(const FileVarDecl *D) { return true; }
395
396protected:
397  /// CreateImpl - Deserialize a FileVarDecl.  Called by Decl::Create.
398  static FileVarDecl* CreateImpl(llvm::Deserializer& D);
399
400  friend Decl* Decl::Create(llvm::Deserializer& D);
401};
402
403/// ParmVarDecl - Represent a parameter to a function.
404class ParmVarDecl : public VarDecl {
405public:
406  ParmVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
407              ScopedDecl *PrevDecl)
408    : VarDecl(ParmVar, L, Id, T, S, PrevDecl),
409    objcDeclQualifier(OBJC_TQ_None) {}
410
411  ObjCDeclQualifier getObjCDeclQualifier() const {
412    return ObjCDeclQualifier(objcDeclQualifier);
413  }
414  void setObjCDeclQualifier(ObjCDeclQualifier QTVal)
415  { objcDeclQualifier = QTVal; }
416
417  // Implement isa/cast/dyncast/etc.
418  static bool classof(const Decl *D) { return D->getKind() == ParmVar; }
419  static bool classof(const ParmVarDecl *D) { return true; }
420
421private:
422  // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
423  /// FIXME: Also can be paced into the bitfields in Decl.
424  /// in, inout, etc.
425  unsigned objcDeclQualifier : 6;
426
427protected:
428  /// EmitImpl - Serialize this ParmVarDecl. Called by Decl::Emit.
429  virtual void EmitImpl(llvm::Serializer& S) const;
430
431  /// CreateImpl - Deserialize a ParmVarDecl.  Called by Decl::Create.
432  static ParmVarDecl* CreateImpl(llvm::Deserializer& D);
433
434  friend Decl* Decl::Create(llvm::Deserializer& D);
435};
436
437/// FunctionDecl - An instance of this class is created to represent a function
438/// declaration or definition.
439class FunctionDecl : public ValueDecl {
440public:
441  enum StorageClass {
442    None, Extern, Static, PrivateExtern
443  };
444  FunctionDecl(SourceLocation L, IdentifierInfo *Id, QualType T,
445               StorageClass S = None, bool isInline = false,
446               ScopedDecl *PrevDecl = 0)
447    : ValueDecl(Function, L, Id, T, PrevDecl),
448      ParamInfo(0), Body(0), DeclChain(0), SClass(S), IsInline(isInline) {}
449  virtual ~FunctionDecl();
450
451  Stmt *getBody() const { return Body; }
452  void setBody(Stmt *B) { Body = B; }
453
454  ScopedDecl *getDeclChain() const { return DeclChain; }
455  void setDeclChain(ScopedDecl *D) { DeclChain = D; }
456
457  // Iterator access to formal parameters.
458  unsigned param_size() const { return getNumParams(); }
459  typedef ParmVarDecl **param_iterator;
460  typedef ParmVarDecl * const *param_const_iterator;
461  param_iterator param_begin() { return ParamInfo; }
462  param_iterator param_end() { return ParamInfo+param_size(); }
463  param_const_iterator param_begin() const { return ParamInfo; }
464  param_const_iterator param_end() const { return ParamInfo+param_size(); }
465
466  unsigned getNumParams() const;
467  const ParmVarDecl *getParamDecl(unsigned i) const {
468    assert(i < getNumParams() && "Illegal param #");
469    return ParamInfo[i];
470  }
471  ParmVarDecl *getParamDecl(unsigned i) {
472    assert(i < getNumParams() && "Illegal param #");
473    return ParamInfo[i];
474  }
475  void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams);
476
477  QualType getResultType() const {
478    return cast<FunctionType>(getType())->getResultType();
479  }
480  StorageClass getStorageClass() const { return StorageClass(SClass); }
481  bool isInline() const { return IsInline; }
482
483  // Implement isa/cast/dyncast/etc.
484  static bool classof(const Decl *D) { return D->getKind() == Function; }
485  static bool classof(const FunctionDecl *D) { return true; }
486
487private:
488  /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
489  /// parameters of this function.  This is null if a prototype or if there are
490  /// no formals.  TODO: we could allocate this space immediately after the
491  /// FunctionDecl object to save an allocation like FunctionType does.
492  ParmVarDecl **ParamInfo;
493
494  Stmt *Body;  // Null if a prototype.
495
496  /// DeclChain - Linked list of declarations that are defined inside this
497  /// function.
498  ScopedDecl *DeclChain;
499
500  // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum
501  unsigned SClass : 2;
502  bool IsInline : 1;
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);
510
511  friend Decl* Decl::Create(llvm::Deserializer& D);
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;
520public:
521  FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T,
522            Expr *BW = NULL)
523    : NamedDecl(Field, L, Id), DeclType(T), BitWidth(BW) {}
524  FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
525            Expr *BW = NULL)
526    : NamedDecl(DK, L, Id), DeclType(T), BitWidth(BW) {}
527
528  QualType getType() const { return DeclType; }
529  QualType getCanonicalType() const { return DeclType.getCanonicalType(); }
530
531  bool isBitField() const { return BitWidth != NULL; }
532  Expr *getBitWidth() const { return BitWidth; }
533  // Implement isa/cast/dyncast/etc.
534  static bool classof(const Decl *D) {
535    return D->getKind() >= FieldFirst && D->getKind() <= FieldLast;
536  }
537  static bool classof(const FieldDecl *D) { return true; }
538
539protected:
540  /// EmitImpl - Serialize this FieldDecl.  Called by Decl::Emit.
541  virtual void EmitImpl(llvm::Serializer& S) const;
542
543  /// CreateImpl - Deserialize a FieldDecl.  Called by Decl::Create.
544  static FieldDecl* CreateImpl(llvm::Deserializer& D);
545
546  friend Decl* Decl::Create(llvm::Deserializer& D);
547};
548
549/// EnumConstantDecl - An instance of this object exists for each enum constant
550/// that is defined.  For example, in "enum X {a,b}", each of a/b are
551/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
552/// TagType for the X EnumDecl.
553class EnumConstantDecl : public ValueDecl {
554  Expr *Init; // an integer constant expression
555  llvm::APSInt Val; // The value.
556public:
557  EnumConstantDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *E,
558                   const llvm::APSInt &V, ScopedDecl *PrevDecl)
559    : ValueDecl(EnumConstant, L, Id, T, PrevDecl), Init(E), Val(V) {}
560
561  const Expr *getInitExpr() const { return Init; }
562  Expr *getInitExpr() { return Init; }
563  const llvm::APSInt &getInitVal() const { return Val; }
564
565  void setInitExpr(Expr *E) { Init = E; }
566  void setInitVal(llvm::APSInt &V) { Val = V; }
567
568  // Implement isa/cast/dyncast/etc.
569  static bool classof(const Decl *D) { return D->getKind() == EnumConstant; }
570  static bool classof(const EnumConstantDecl *D) { return true; }
571
572  friend class StmtIteratorBase;
573
574protected:
575  /// EmitImpl - Serialize this EnumConstantDecl.  Called by Decl::Emit.
576  virtual void EmitImpl(llvm::Serializer& S) const;
577
578  /// CreateImpl - Deserialize a EnumConstantDecl.  Called by Decl::Create.
579  static EnumConstantDecl* CreateImpl(llvm::Deserializer& D);
580
581  friend Decl* Decl::Create(llvm::Deserializer& D);
582};
583
584
585/// TypeDecl - Represents a declaration of a type.
586///
587class TypeDecl : public ScopedDecl {
588  /// TypeForDecl - This indicates the Type object that represents this
589  /// TypeDecl.  It is a cache maintained by ASTContext::getTypedefType and
590  /// ASTContext::getTagDeclType.
591  Type *TypeForDecl;
592  friend class ASTContext;
593protected:
594  TypeDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
595    : ScopedDecl(DK, L, Id, PrevDecl), TypeForDecl(0) {}
596public:
597  // Implement isa/cast/dyncast/etc.
598  static bool classof(const Decl *D) {
599    return D->getKind() >= TypeFirst && D->getKind() <= TypeLast;
600  }
601  static bool classof(const TypeDecl *D) { return true; }
602};
603
604
605class TypedefDecl : public TypeDecl {
606  /// UnderlyingType - This is the type the typedef is set to.
607  QualType UnderlyingType;
608public:
609  TypedefDecl(SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PD)
610    : TypeDecl(Typedef, L, Id, PD), UnderlyingType(T) {}
611
612  QualType getUnderlyingType() const { return UnderlyingType; }
613  void setUnderlyingType(QualType newType) { UnderlyingType = newType; }
614
615  // Implement isa/cast/dyncast/etc.
616  static bool classof(const Decl *D) { return D->getKind() == Typedef; }
617  static bool classof(const TypedefDecl *D) { return true; }
618
619protected:
620  /// EmitImpl - Serialize this TypedefDecl.  Called by Decl::Emit.
621  virtual void EmitImpl(llvm::Serializer& S) const;
622
623  /// CreateImpl - Deserialize a TypedefDecl.  Called by Decl::Create.
624  static TypedefDecl* CreateImpl(llvm::Deserializer& D);
625
626  friend Decl* Decl::Create(llvm::Deserializer& D);
627};
628
629
630/// TagDecl - Represents the declaration of a struct/union/class/enum.
631class TagDecl : public TypeDecl {
632  /// IsDefinition - True if this is a definition ("struct foo {};"), false if
633  /// it is a declaration ("struct foo;").
634  bool IsDefinition : 1;
635protected:
636  TagDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
637    : TypeDecl(DK, L, Id, PrevDecl) {
638    IsDefinition = false;
639  }
640public:
641
642  /// isDefinition - Return true if this decl has its body specified.
643  bool isDefinition() const {
644    return IsDefinition;
645  }
646
647  const char *getKindName() const {
648    switch (getKind()) {
649    default: assert(0 && "Unknown TagDecl!");
650    case Struct: return "struct";
651    case Union:  return "union";
652    case Class:  return "class";
653    case Enum:   return "enum";
654    }
655  }
656
657  // Implement isa/cast/dyncast/etc.
658  static bool classof(const Decl *D) {
659    return D->getKind() >= TagFirst && D->getKind() <= TagLast;
660  }
661  static bool classof(const TagDecl *D) { return true; }
662protected:
663  void setDefinition(bool V) { IsDefinition = V; }
664};
665
666/// EnumDecl - Represents an enum.  As an extension, we allow forward-declared
667/// enums.
668class EnumDecl : public TagDecl {
669  /// ElementList - this is a linked list of EnumConstantDecl's which are linked
670  /// together through their getNextDeclarator pointers.
671  EnumConstantDecl *ElementList;
672
673  /// IntegerType - This represent the integer type that the enum corresponds
674  /// to for code generation purposes.  Note that the enumerator constants may
675  /// have a different type than this does.
676  QualType IntegerType;
677public:
678  EnumDecl(SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
679    : TagDecl(Enum, L, Id, PrevDecl) {
680    ElementList = 0;
681        IntegerType = QualType();
682  }
683
684  /// defineElements - When created, EnumDecl correspond to a forward declared
685  /// enum.  This method is used to mark the decl as being defined, with the
686  /// specified list of enums.
687  void defineElements(EnumConstantDecl *ListHead, QualType NewType) {
688    assert(!isDefinition() && "Cannot redefine enums!");
689    ElementList = ListHead;
690    setDefinition(true);
691
692    IntegerType = NewType;
693  }
694
695  /// getIntegerType - Return the integer type this enum decl corresponds to.
696  /// This returns a null qualtype for an enum forward definition.
697  QualType getIntegerType() const { return IntegerType; }
698
699  /// getEnumConstantList - Return the first EnumConstantDecl in the enum.
700  ///
701  EnumConstantDecl *getEnumConstantList() { return ElementList; }
702  const EnumConstantDecl *getEnumConstantList() const { return ElementList; }
703
704  static bool classof(const Decl *D) { return D->getKind() == Enum; }
705  static bool classof(const EnumDecl *D) { return true; }
706
707protected:
708  /// EmitImpl - Serialize this EnumDecl.  Called by Decl::Emit.
709  virtual void EmitImpl(llvm::Serializer& S) const;
710
711  /// CreateImpl - Deserialize a EnumDecl.  Called by Decl::Create.
712  static EnumDecl* CreateImpl(llvm::Deserializer& D);
713
714  friend Decl* Decl::Create(llvm::Deserializer& D);
715};
716
717
718/// RecordDecl - Represents a struct/union/class.  For example:
719///   struct X;                  // Forward declaration, no "body".
720///   union Y { int A, B; };     // Has body with members A and B (FieldDecls).
721/// This decl will be marked invalid if *any* members are invalid.
722///
723class RecordDecl : public TagDecl {
724  /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
725  /// array member (e.g. int X[]) or if this union contains a struct that does.
726  /// If so, this cannot be contained in arrays or other structs as a member.
727  bool HasFlexibleArrayMember : 1;
728
729  /// Members/NumMembers - This is a new[]'d array of pointers to Decls.
730  FieldDecl **Members;   // Null if not defined.
731  int NumMembers;   // -1 if not defined.
732public:
733  RecordDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl*PrevDecl)
734    : TagDecl(DK, L, Id, PrevDecl) {
735    HasFlexibleArrayMember = false;
736    assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
737    Members = 0;
738    NumMembers = -1;
739  }
740
741  bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
742  void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
743
744  /// getNumMembers - Return the number of members, or -1 if this is a forward
745  /// definition.
746  int getNumMembers() const { return NumMembers; }
747  const FieldDecl *getMember(unsigned i) const { return Members[i]; }
748  FieldDecl *getMember(unsigned i) { return Members[i]; }
749
750  /// defineBody - When created, RecordDecl's correspond to a forward declared
751  /// record.  This method is used to mark the decl as being defined, with the
752  /// specified contents.
753  void defineBody(FieldDecl **Members, unsigned numMembers);
754
755  /// getMember - If the member doesn't exist, or there are no members, this
756  /// function will return 0;
757  FieldDecl *getMember(IdentifierInfo *name);
758
759  static bool classof(const Decl *D) {
760    return D->getKind() >= RecordFirst && D->getKind() <= RecordLast;
761  }
762  static bool classof(const RecordDecl *D) { return true; }
763
764protected:
765  /// EmitImpl - Serialize this RecordDecl.  Called by Decl::Emit.
766  virtual void EmitImpl(llvm::Serializer& S) const;
767
768  /// CreateImpl - Deserialize a RecordDecl.  Called by Decl::Create.
769  static RecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D);
770
771  friend Decl* Decl::Create(llvm::Deserializer& D);
772};
773
774class FileScopeAsmDecl : public Decl {
775  StringLiteral *AsmString;
776public:
777  FileScopeAsmDecl(SourceLocation L, StringLiteral *asmstring)
778    : Decl(FileScopeAsm, L), AsmString(asmstring) {}
779
780  const StringLiteral *getAsmString() const { return AsmString; }
781  StringLiteral *getAsmString() { return AsmString; }
782  static bool classof(const Decl *D) {
783    return D->getKind() == FileScopeAsm;
784  }
785  static bool classof(const FileScopeAsmDecl *D) { return true; }
786protected:
787  /// EmitImpl - Serialize this FileScopeAsmDecl. Called by Decl::Emit.
788  virtual void EmitImpl(llvm::Serializer& S) const;
789
790  /// CreateImpl - Deserialize a FileScopeAsmDecl.  Called by Decl::Create.
791  static FileScopeAsmDecl* CreateImpl(llvm::Deserializer& D);
792
793  friend Decl* Decl::Create(llvm::Deserializer& D);
794};
795
796/// LinkageSpecDecl - This represents a linkage specification.  For example:
797///   extern "C" void foo();
798///
799class LinkageSpecDecl : public Decl {
800public:
801  /// LanguageIDs - Used to represent the language in a linkage
802  /// specification.  The values are part of the serialization abi for
803  /// ASTs and cannot be changed without altering that abi.  To help
804  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
805  /// from the dwarf standard.
806  enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002,
807                     lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 };
808private:
809  /// Language - The language for this linkage specification.
810  LanguageIDs Language;
811  /// D - This is the Decl of the linkage specification.
812  Decl *D;
813public:
814  LinkageSpecDecl(SourceLocation L, LanguageIDs lang, Decl *d)
815   : Decl(LinkageSpec, L), Language(lang), D(d) {}
816
817  LanguageIDs getLanguage() const { return Language; }
818  const Decl *getDecl() const { return D; }
819  Decl *getDecl() { return D; }
820
821  static bool classof(const Decl *D) {
822    return D->getKind() == LinkageSpec;
823  }
824  static bool classof(const LinkageSpecDecl *D) { return true; }
825
826protected:
827  void EmitInRec(llvm::Serializer& S) const;
828  void ReadInRec(llvm::Deserializer& D);
829};
830
831}  // end namespace clang
832
833#endif
834