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