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