Decl.h revision c63e660882ff93841fa234d70ef6757038302b92
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(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, Auto, Register, Extern, Static, PrivateExtern
296  };
297private:
298  Expr *Init;
299  // FIXME: This can be packed into the bitfields in Decl.
300  unsigned SClass : 3;
301
302  friend class StmtIteratorBase;
303protected:
304  VarDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
305          StorageClass SC, ScopedDecl *PrevDecl)
306    : ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; }
307public:
308  StorageClass getStorageClass() const { return (StorageClass)SClass; }
309
310  const Expr *getInit() const { return Init; }
311  Expr *getInit() { return Init; }
312  void setInit(Expr *I) { Init = I; }
313
314  /// hasLocalStorage - Returns true if a variable with function scope
315  ///  is a non-static local variable.
316  bool hasLocalStorage() const {
317    if (getStorageClass() == None)
318      return getKind() != FileVar;
319
320    // Return true for:  Auto, Register.
321    // Return false for: Extern, Static, PrivateExtern.
322
323    return getStorageClass() <= Register;
324  }
325
326  /// hasGlobalStorage - Returns true for all variables that do not
327  ///  have local storage.  This includs all global variables as well
328  ///  as static variables declared within a function.
329  bool hasGlobalStorage() const { return !hasLocalStorage(); }
330
331  // Implement isa/cast/dyncast/etc.
332  static bool classof(const Decl *D) {
333    return D->getKind() >= VarFirst && D->getKind() <= VarLast;
334  }
335  static bool classof(const VarDecl *D) { return true; }
336
337protected:
338  void EmitInRec(llvm::Serializer& S) const;
339  void ReadInRec(llvm::Deserializer& D);
340
341  void EmitOutRec(llvm::Serializer& S) const;
342  void ReadOutRec(llvm::Deserializer& D);
343
344  /// EmitImpl - Serialize this VarDecl. Called by Decl::Emit.
345  virtual void EmitImpl(llvm::Serializer& S) const;
346
347  /// ReadImpl - Deserialize this VarDecl. Called by subclasses.
348  virtual void ReadImpl(llvm::Deserializer& S);
349};
350
351/// BlockVarDecl - Represent a local variable declaration.
352class BlockVarDecl : public VarDecl {
353  BlockVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
354               ScopedDecl *PrevDecl)
355    : VarDecl(BlockVar, L, Id, T, S, PrevDecl) {}
356public:
357  static BlockVarDecl *Create(ASTContext &C, SourceLocation L,
358                              IdentifierInfo *Id, QualType T, StorageClass S,
359                              ScopedDecl *PrevDecl);
360  // Implement isa/cast/dyncast/etc.
361  static bool classof(const Decl *D) { return D->getKind() == BlockVar; }
362  static bool classof(const BlockVarDecl *D) { return true; }
363
364protected:
365  /// CreateImpl - Deserialize a BlockVarDecl.  Called by Decl::Create.
366  static BlockVarDecl* CreateImpl(llvm::Deserializer& D);
367
368  friend Decl* Decl::Create(llvm::Deserializer& D);
369};
370
371/// FileVarDecl - Represent a file scoped variable declaration. This
372/// will allow us to reason about external variable declarations and tentative
373/// definitions (C99 6.9.2p2) using our type system (without storing a
374/// pointer to the decl's scope, which is transient).
375class FileVarDecl : public VarDecl {
376  FileVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
377              ScopedDecl *PrevDecl)
378    : VarDecl(FileVar, L, Id, T, S, PrevDecl) {}
379public:
380  static FileVarDecl *Create(ASTContext &C, SourceLocation L,IdentifierInfo *Id,
381                             QualType T, StorageClass S, ScopedDecl *PrevDecl);
382
383  // Implement isa/cast/dyncast/etc.
384  static bool classof(const Decl *D) { return D->getKind() == FileVar; }
385  static bool classof(const FileVarDecl *D) { return true; }
386
387protected:
388  /// CreateImpl - Deserialize a FileVarDecl.  Called by Decl::Create.
389  static FileVarDecl* CreateImpl(llvm::Deserializer& D);
390
391  friend Decl* Decl::Create(llvm::Deserializer& D);
392};
393
394/// ParmVarDecl - Represent a parameter to a function.
395class ParmVarDecl : public VarDecl {
396  // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
397  /// FIXME: Also can be paced into the bitfields in Decl.
398  /// in, inout, etc.
399  unsigned objcDeclQualifier : 6;
400
401  ParmVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
402              ScopedDecl *PrevDecl)
403    : VarDecl(ParmVar, L, Id, T, S, PrevDecl),
404    objcDeclQualifier(OBJC_TQ_None) {}
405public:
406  static ParmVarDecl *Create(ASTContext &C, SourceLocation L,IdentifierInfo *Id,
407                             QualType T, StorageClass S, ScopedDecl *PrevDecl);
408
409  ObjCDeclQualifier getObjCDeclQualifier() const {
410    return ObjCDeclQualifier(objcDeclQualifier);
411  }
412  void setObjCDeclQualifier(ObjCDeclQualifier QTVal)
413  { objcDeclQualifier = QTVal; }
414
415  // Implement isa/cast/dyncast/etc.
416  static bool classof(const Decl *D) { return D->getKind() == ParmVar; }
417  static bool classof(const ParmVarDecl *D) { return true; }
418
419protected:
420  /// EmitImpl - Serialize this ParmVarDecl. Called by Decl::Emit.
421  virtual void EmitImpl(llvm::Serializer& S) const;
422
423  /// CreateImpl - Deserialize a ParmVarDecl.  Called by Decl::Create.
424  static ParmVarDecl* CreateImpl(llvm::Deserializer& D);
425
426  friend Decl* Decl::Create(llvm::Deserializer& D);
427};
428
429/// FunctionDecl - An instance of this class is created to represent a function
430/// declaration or definition.
431class FunctionDecl : public ValueDecl {
432public:
433  enum StorageClass {
434    None, Extern, Static, PrivateExtern
435  };
436private:
437  FunctionDecl(SourceLocation L, IdentifierInfo *Id, QualType T,
438               StorageClass S, bool isInline, ScopedDecl *PrevDecl)
439    : ValueDecl(Function, L, Id, T, PrevDecl),
440      ParamInfo(0), Body(0), DeclChain(0), SClass(S), IsInline(isInline) {}
441  virtual ~FunctionDecl();
442public:
443  static FunctionDecl *Create(ASTContext &C, SourceLocation L,
444                              IdentifierInfo *Id, QualType T,
445                              StorageClass S = None, bool isInline = false,
446                              ScopedDecl *PrevDecl = 0);
447
448  Stmt *getBody() const { return Body; }
449  void setBody(Stmt *B) { Body = B; }
450
451  ScopedDecl *getDeclChain() const { return DeclChain; }
452  void setDeclChain(ScopedDecl *D) { DeclChain = D; }
453
454  // Iterator access to formal parameters.
455  unsigned param_size() const { return getNumParams(); }
456  typedef ParmVarDecl **param_iterator;
457  typedef ParmVarDecl * const *param_const_iterator;
458  param_iterator param_begin() { return ParamInfo; }
459  param_iterator param_end() { return ParamInfo+param_size(); }
460  param_const_iterator param_begin() const { return ParamInfo; }
461  param_const_iterator param_end() const { return ParamInfo+param_size(); }
462
463  unsigned getNumParams() const;
464  const ParmVarDecl *getParamDecl(unsigned i) const {
465    assert(i < getNumParams() && "Illegal param #");
466    return ParamInfo[i];
467  }
468  ParmVarDecl *getParamDecl(unsigned i) {
469    assert(i < getNumParams() && "Illegal param #");
470    return ParamInfo[i];
471  }
472  void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams);
473
474  QualType getResultType() const {
475    return cast<FunctionType>(getType())->getResultType();
476  }
477  StorageClass getStorageClass() const { return StorageClass(SClass); }
478  bool isInline() const { return IsInline; }
479
480  // Implement isa/cast/dyncast/etc.
481  static bool classof(const Decl *D) { return D->getKind() == Function; }
482  static bool classof(const FunctionDecl *D) { return true; }
483
484private:
485  /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
486  /// parameters of this function.  This is null if a prototype or if there are
487  /// no formals.  TODO: we could allocate this space immediately after the
488  /// FunctionDecl object to save an allocation like FunctionType does.
489  ParmVarDecl **ParamInfo;
490
491  Stmt *Body;  // Null if a prototype.
492
493  /// DeclChain - Linked list of declarations that are defined inside this
494  /// function.
495  ScopedDecl *DeclChain;
496
497  // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum
498  unsigned SClass : 2;
499  bool IsInline : 1;
500
501protected:
502  /// EmitImpl - Serialize this FunctionDecl.  Called by Decl::Emit.
503  virtual void EmitImpl(llvm::Serializer& S) const;
504
505  /// CreateImpl - Deserialize a FunctionDecl.  Called by Decl::Create.
506  static FunctionDecl* CreateImpl(llvm::Deserializer& D);
507
508  friend Decl* Decl::Create(llvm::Deserializer& D);
509};
510
511
512/// FieldDecl - An instance of this class is created by Sema::ActOnField to
513/// represent a member of a struct/union/class.
514class FieldDecl : public NamedDecl {
515  QualType DeclType;
516  Expr *BitWidth;
517public:
518  FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T,
519            Expr *BW = NULL)
520    : NamedDecl(Field, L, Id), DeclType(T), BitWidth(BW) {}
521  FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
522            Expr *BW = NULL)
523    : NamedDecl(DK, L, Id), DeclType(T), BitWidth(BW) {}
524
525  QualType getType() const { return DeclType; }
526  QualType getCanonicalType() const { return DeclType.getCanonicalType(); }
527
528  bool isBitField() const { return BitWidth != NULL; }
529  Expr *getBitWidth() const { return BitWidth; }
530  // Implement isa/cast/dyncast/etc.
531  static bool classof(const Decl *D) {
532    return D->getKind() >= FieldFirst && D->getKind() <= FieldLast;
533  }
534  static bool classof(const FieldDecl *D) { return true; }
535
536protected:
537  /// EmitImpl - Serialize this FieldDecl.  Called by Decl::Emit.
538  virtual void EmitImpl(llvm::Serializer& S) const;
539
540  /// CreateImpl - Deserialize a FieldDecl.  Called by Decl::Create.
541  static FieldDecl* CreateImpl(llvm::Deserializer& D);
542
543  friend Decl* Decl::Create(llvm::Deserializer& D);
544};
545
546/// EnumConstantDecl - An instance of this object exists for each enum constant
547/// that is defined.  For example, in "enum X {a,b}", each of a/b are
548/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
549/// TagType for the X EnumDecl.
550class EnumConstantDecl : public ValueDecl {
551  Expr *Init; // an integer constant expression
552  llvm::APSInt Val; // The value.
553protected:
554  EnumConstantDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *E,
555                   const llvm::APSInt &V, ScopedDecl *PrevDecl)
556    : ValueDecl(EnumConstant, L, Id, T, PrevDecl), Init(E), Val(V) {}
557  ~EnumConstantDecl() {}
558public:
559
560  static EnumConstantDecl *Create(ASTContext &C, SourceLocation L,
561                                  IdentifierInfo *Id, QualType T, Expr *E,
562                                  const llvm::APSInt &V, ScopedDecl *PrevDecl);
563
564  const Expr *getInitExpr() const { return Init; }
565  Expr *getInitExpr() { return Init; }
566  const llvm::APSInt &getInitVal() const { return Val; }
567
568  void setInitExpr(Expr *E) { Init = E; }
569  void setInitVal(const llvm::APSInt &V) { Val = V; }
570
571  // Implement isa/cast/dyncast/etc.
572  static bool classof(const Decl *D) { return D->getKind() == EnumConstant; }
573  static bool classof(const EnumConstantDecl *D) { return true; }
574
575  friend class StmtIteratorBase;
576
577protected:
578  /// EmitImpl - Serialize this EnumConstantDecl.  Called by Decl::Emit.
579  virtual void EmitImpl(llvm::Serializer& S) const;
580
581  /// CreateImpl - Deserialize a EnumConstantDecl.  Called by Decl::Create.
582  static EnumConstantDecl* CreateImpl(llvm::Deserializer& D);
583
584  friend Decl* Decl::Create(llvm::Deserializer& D);
585};
586
587
588/// TypeDecl - Represents a declaration of a type.
589///
590class TypeDecl : public ScopedDecl {
591  /// TypeForDecl - This indicates the Type object that represents this
592  /// TypeDecl.  It is a cache maintained by ASTContext::getTypedefType and
593  /// ASTContext::getTagDeclType.
594  Type *TypeForDecl;
595  friend class ASTContext;
596protected:
597  TypeDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
598    : ScopedDecl(DK, L, Id, PrevDecl), TypeForDecl(0) {}
599public:
600  // Implement isa/cast/dyncast/etc.
601  static bool classof(const Decl *D) {
602    return D->getKind() >= TypeFirst && D->getKind() <= TypeLast;
603  }
604  static bool classof(const TypeDecl *D) { return true; }
605};
606
607
608class TypedefDecl : public TypeDecl {
609  /// UnderlyingType - This is the type the typedef is set to.
610  QualType UnderlyingType;
611  TypedefDecl(SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PD)
612    : TypeDecl(Typedef, L, Id, PD), UnderlyingType(T) {}
613  ~TypedefDecl() {}
614public:
615
616  static TypedefDecl *Create(ASTContext &C, SourceLocation L,IdentifierInfo *Id,
617                             QualType T, ScopedDecl *PD);
618
619  QualType getUnderlyingType() const { return UnderlyingType; }
620  void setUnderlyingType(QualType newType) { UnderlyingType = newType; }
621
622  // Implement isa/cast/dyncast/etc.
623  static bool classof(const Decl *D) { return D->getKind() == Typedef; }
624  static bool classof(const TypedefDecl *D) { return true; }
625
626protected:
627  /// EmitImpl - Serialize this TypedefDecl.  Called by Decl::Emit.
628  virtual void EmitImpl(llvm::Serializer& S) const;
629
630  /// CreateImpl - Deserialize a TypedefDecl.  Called by Decl::Create.
631  static TypedefDecl* CreateImpl(llvm::Deserializer& D);
632
633  friend Decl* Decl::Create(llvm::Deserializer& D);
634};
635
636
637/// TagDecl - Represents the declaration of a struct/union/class/enum.
638class TagDecl : public TypeDecl {
639  /// IsDefinition - True if this is a definition ("struct foo {};"), false if
640  /// it is a declaration ("struct foo;").
641  bool IsDefinition : 1;
642protected:
643  TagDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
644    : TypeDecl(DK, L, Id, PrevDecl) {
645    IsDefinition = false;
646  }
647public:
648
649  /// isDefinition - Return true if this decl has its body specified.
650  bool isDefinition() const {
651    return IsDefinition;
652  }
653
654  const char *getKindName() const {
655    switch (getKind()) {
656    default: assert(0 && "Unknown TagDecl!");
657    case Struct: return "struct";
658    case Union:  return "union";
659    case Class:  return "class";
660    case Enum:   return "enum";
661    }
662  }
663
664  // Implement isa/cast/dyncast/etc.
665  static bool classof(const Decl *D) {
666    return D->getKind() >= TagFirst && D->getKind() <= TagLast;
667  }
668  static bool classof(const TagDecl *D) { return true; }
669protected:
670  void setDefinition(bool V) { IsDefinition = V; }
671};
672
673/// EnumDecl - Represents an enum.  As an extension, we allow forward-declared
674/// enums.
675class EnumDecl : public TagDecl {
676  /// ElementList - this is a linked list of EnumConstantDecl's which are linked
677  /// together through their getNextDeclarator pointers.
678  EnumConstantDecl *ElementList;
679
680  /// IntegerType - This represent the integer type that the enum corresponds
681  /// to for code generation purposes.  Note that the enumerator constants may
682  /// have a different type than this does.
683  QualType IntegerType;
684
685  EnumDecl(SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
686    : TagDecl(Enum, L, Id, PrevDecl) {
687      ElementList = 0;
688      IntegerType = QualType();
689    }
690  ~EnumDecl() {}
691public:
692  static EnumDecl *Create(ASTContext &C, SourceLocation L, IdentifierInfo *Id,
693                          ScopedDecl *PrevDecl);
694
695  /// defineElements - When created, EnumDecl correspond to a forward declared
696  /// enum.  This method is used to mark the decl as being defined, with the
697  /// specified list of enums.
698  void defineElements(EnumConstantDecl *ListHead, QualType NewType) {
699    assert(!isDefinition() && "Cannot redefine enums!");
700    ElementList = ListHead;
701    setDefinition(true);
702
703    IntegerType = NewType;
704  }
705
706  /// getIntegerType - Return the integer type this enum decl corresponds to.
707  /// This returns a null qualtype for an enum forward definition.
708  QualType getIntegerType() const { return IntegerType; }
709
710  /// getEnumConstantList - Return the first EnumConstantDecl in the enum.
711  ///
712  EnumConstantDecl *getEnumConstantList() { return ElementList; }
713  const EnumConstantDecl *getEnumConstantList() const { return ElementList; }
714
715  static bool classof(const Decl *D) { return D->getKind() == Enum; }
716  static bool classof(const EnumDecl *D) { return true; }
717
718protected:
719  /// EmitImpl - Serialize this EnumDecl.  Called by Decl::Emit.
720  virtual void EmitImpl(llvm::Serializer& S) const;
721
722  /// CreateImpl - Deserialize a EnumDecl.  Called by Decl::Create.
723  static EnumDecl* CreateImpl(llvm::Deserializer& D);
724
725  friend Decl* Decl::Create(llvm::Deserializer& D);
726};
727
728
729/// RecordDecl - Represents a struct/union/class.  For example:
730///   struct X;                  // Forward declaration, no "body".
731///   union Y { int A, B; };     // Has body with members A and B (FieldDecls).
732/// This decl will be marked invalid if *any* members are invalid.
733///
734class RecordDecl : public TagDecl {
735  /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
736  /// array member (e.g. int X[]) or if this union contains a struct that does.
737  /// If so, this cannot be contained in arrays or other structs as a member.
738  bool HasFlexibleArrayMember : 1;
739
740  /// Members/NumMembers - This is a new[]'d array of pointers to Decls.
741  FieldDecl **Members;   // Null if not defined.
742  int NumMembers;   // -1 if not defined.
743
744  RecordDecl(Kind DK, SourceLocation L, IdentifierInfo *Id,
745             ScopedDecl *PrevDecl) : TagDecl(DK, L, Id, PrevDecl) {
746    HasFlexibleArrayMember = false;
747    assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
748    Members = 0;
749    NumMembers = -1;
750  }
751
752  ~RecordDecl() {}
753public:
754
755  static RecordDecl *Create(ASTContext &C, Kind DK, SourceLocation L,
756                            IdentifierInfo *Id, ScopedDecl *PrevDecl);
757
758  bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
759  void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
760
761  /// getNumMembers - Return the number of members, or -1 if this is a forward
762  /// definition.
763  int getNumMembers() const { return NumMembers; }
764  const FieldDecl *getMember(unsigned i) const { return Members[i]; }
765  FieldDecl *getMember(unsigned i) { return Members[i]; }
766
767  /// defineBody - When created, RecordDecl's correspond to a forward declared
768  /// record.  This method is used to mark the decl as being defined, with the
769  /// specified contents.
770  void defineBody(FieldDecl **Members, unsigned numMembers);
771
772  /// getMember - If the member doesn't exist, or there are no members, this
773  /// function will return 0;
774  FieldDecl *getMember(IdentifierInfo *name);
775
776  static bool classof(const Decl *D) {
777    return D->getKind() >= RecordFirst && D->getKind() <= RecordLast;
778  }
779  static bool classof(const RecordDecl *D) { return true; }
780
781protected:
782  /// EmitImpl - Serialize this RecordDecl.  Called by Decl::Emit.
783  virtual void EmitImpl(llvm::Serializer& S) const;
784
785  /// CreateImpl - Deserialize a RecordDecl.  Called by Decl::Create.
786  static RecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D);
787
788  friend Decl* Decl::Create(llvm::Deserializer& D);
789};
790
791class FileScopeAsmDecl : public Decl {
792  StringLiteral *AsmString;
793public:
794  FileScopeAsmDecl(SourceLocation L, StringLiteral *asmstring)
795    : Decl(FileScopeAsm, L), AsmString(asmstring) {}
796
797  const StringLiteral *getAsmString() const { return AsmString; }
798  StringLiteral *getAsmString() { return AsmString; }
799  static bool classof(const Decl *D) {
800    return D->getKind() == FileScopeAsm;
801  }
802  static bool classof(const FileScopeAsmDecl *D) { return true; }
803protected:
804  /// EmitImpl - Serialize this FileScopeAsmDecl. Called by Decl::Emit.
805  virtual void EmitImpl(llvm::Serializer& S) const;
806
807  /// CreateImpl - Deserialize a FileScopeAsmDecl.  Called by Decl::Create.
808  static FileScopeAsmDecl* CreateImpl(llvm::Deserializer& D);
809
810  friend Decl* Decl::Create(llvm::Deserializer& D);
811};
812
813/// LinkageSpecDecl - This represents a linkage specification.  For example:
814///   extern "C" void foo();
815///
816class LinkageSpecDecl : public Decl {
817public:
818  /// LanguageIDs - Used to represent the language in a linkage
819  /// specification.  The values are part of the serialization abi for
820  /// ASTs and cannot be changed without altering that abi.  To help
821  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
822  /// from the dwarf standard.
823  enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002,
824                     lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 };
825private:
826  /// Language - The language for this linkage specification.
827  LanguageIDs Language;
828  /// D - This is the Decl of the linkage specification.
829  Decl *D;
830public:
831  LinkageSpecDecl(SourceLocation L, LanguageIDs lang, Decl *d)
832   : Decl(LinkageSpec, L), Language(lang), D(d) {}
833
834  LanguageIDs getLanguage() const { return Language; }
835  const Decl *getDecl() const { return D; }
836  Decl *getDecl() { return D; }
837
838  static bool classof(const Decl *D) {
839    return D->getKind() == LinkageSpec;
840  }
841  static bool classof(const LinkageSpecDecl *D) { return true; }
842
843protected:
844  void EmitInRec(llvm::Serializer& S) const;
845  void ReadInRec(llvm::Deserializer& D);
846};
847
848}  // end namespace clang
849
850#endif
851