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