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