DeclBase.h revision 232200866d298066c4a22e7d92587f3cd856bd7c
1//===-- DeclBase.h - Base 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 and DeclContext interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_DECLBASE_H
15#define LLVM_CLANG_AST_DECLBASE_H
16
17#include "clang/AST/Attr.h"
18#include "clang/AST/Type.h"
19#include "clang/Basic/SourceLocation.h"
20
21namespace clang {
22class TranslationUnitDecl;
23class NamespaceDecl;
24class ScopedDecl;
25class FunctionDecl;
26class CXXRecordDecl;
27class EnumDecl;
28class ObjCMethodDecl;
29class ObjCInterfaceDecl;
30
31/// Decl - This represents one declaration (or definition), e.g. a variable,
32/// typedef, function, struct, etc.
33///
34class Decl {
35public:
36  enum Kind {
37    // This lists the concrete classes of Decl in order of the inheritance
38    // hierarchy.  This allows us to do efficient classof tests based on the
39    // enums below.   The commented out names are abstract class names.
40    // [DeclContext] indicates that the class also inherits from DeclContext.
41
42    // Decl
43         TranslationUnit,  // [DeclContext]
44    //   NamedDecl
45           Field,
46             CXXField,
47             ObjCIvar,
48           ObjCCategory,
49           ObjCCategoryImpl,
50           ObjCImplementation,
51           ObjCProtocol,
52           ObjCProperty,
53    //     ScopedDecl
54             Namespace,  // [DeclContext]
55    //       TypeDecl
56               Typedef,
57    //         TagDecl
58                 Enum,  // [DeclContext]
59    //           RecordDecl
60                   Struct,
61                   Union,
62                   Class,
63    //             CXXRecordDecl  [DeclContext]
64                     CXXStruct,
65                     CXXUnion,
66                     CXXClass,
67    //       ValueDecl
68               EnumConstant,
69               Function,  // [DeclContext]
70                 CXXMethod,
71               Var,
72                 ImplicitParam,
73                 CXXClassVar,
74                 ParmVar,
75           ObjCInterface,  // [DeclContext]
76           ObjCCompatibleAlias,
77           ObjCMethod,  // [DeclContext]
78           ObjCClass,
79           ObjCForwardProtocol,
80           ObjCPropertyImpl,
81         LinkageSpec,
82         FileScopeAsm,
83
84    // For each non-leaf class, we now define a mapping to the first/last member
85    // of the class, to allow efficient classof.
86    NamedFirst     = Field        , NamedLast     = ParmVar,
87    FieldFirst     = Field        , FieldLast     = ObjCIvar,
88    ScopedFirst    = Namespace    , ScopedLast    = ParmVar,
89    TypeFirst      = Typedef      , TypeLast      = CXXClass,
90    TagFirst       = Enum         , TagLast       = CXXClass,
91    RecordFirst    = Struct       , RecordLast    = CXXClass,
92    CXXRecordFirst = CXXStruct    , CXXRecordLast = CXXClass,
93    ValueFirst     = EnumConstant , ValueLast     = ParmVar,
94    FunctionFirst  = Function     , FunctionLast  = CXXMethod,
95    VarFirst       = Var          , VarLast       = ParmVar
96  };
97
98  /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
99  /// labels, tags, members and ordinary identifiers. These are meant
100  /// as bitmasks, so that searches in C++ can look into the "tag" namespace
101  /// during ordinary lookup.
102  enum IdentifierNamespace {
103    IDNS_Label = 0x1,
104    IDNS_Tag = 0x2,
105    IDNS_Member = 0x4,
106    IDNS_Ordinary = 0x8
107  };
108
109  /// ObjCDeclQualifier - Qualifier used on types in method declarations
110  /// for remote messaging. They are meant for the arguments though and
111  /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
112  enum ObjCDeclQualifier {
113    OBJC_TQ_None = 0x0,
114    OBJC_TQ_In = 0x1,
115    OBJC_TQ_Inout = 0x2,
116    OBJC_TQ_Out = 0x4,
117    OBJC_TQ_Bycopy = 0x8,
118    OBJC_TQ_Byref = 0x10,
119    OBJC_TQ_Oneway = 0x20
120  };
121
122private:
123  /// Loc - The location that this decl.
124  SourceLocation Loc;
125
126  /// DeclKind - This indicates which class this is.
127  Kind DeclKind   :  8;
128
129  /// InvalidDecl - This indicates a semantic error occurred.
130  unsigned int InvalidDecl :  1;
131
132  /// HasAttrs - This indicates whether the decl has attributes or not.
133  unsigned int HasAttrs : 1;
134
135 protected:
136  /// Access - Used by C++ decls for the access specifier.
137  // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
138  unsigned Access : 2;
139  friend class CXXClassMemberWrapper;
140
141  Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0),
142    HasAttrs(false) {
143    if (Decl::CollectingStats()) addDeclKind(DK);
144  }
145
146  virtual ~Decl();
147
148public:
149  SourceLocation getLocation() const { return Loc; }
150  void setLocation(SourceLocation L) { Loc = L; }
151
152  Kind getKind() const { return DeclKind; }
153  const char *getDeclKindName() const;
154
155  void addAttr(Attr *attr);
156  const Attr *getAttrs() const;
157  void swapAttrs(Decl *D);
158  void invalidateAttrs();
159
160  template<typename T> const T *getAttr() const {
161    for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
162      if (const T *V = dyn_cast<T>(attr))
163        return V;
164
165    return 0;
166  }
167
168  /// setInvalidDecl - Indicates the Decl had a semantic error. This
169  /// allows for graceful error recovery.
170  void setInvalidDecl() { InvalidDecl = 1; }
171  bool isInvalidDecl() const { return (bool) InvalidDecl; }
172
173  IdentifierNamespace getIdentifierNamespace() const {
174    switch (DeclKind) {
175    default: assert(0 && "Unknown decl kind!");
176    case ImplicitParam:
177    case Typedef:
178    case Function:
179    case Var:
180    case ParmVar:
181    case EnumConstant:
182    case ObjCInterface:
183    case ObjCCompatibleAlias:
184    case CXXField:
185    case CXXMethod:
186    case CXXClassVar:
187      return IDNS_Ordinary;
188    case Struct:
189    case Union:
190    case Class:
191    case CXXStruct:
192    case CXXUnion:
193    case CXXClass:
194    case Enum:
195      return IDNS_Tag;
196    case Namespace:
197      return IdentifierNamespace(IDNS_Tag | IDNS_Ordinary);
198    }
199  }
200
201  // getBody - If this Decl represents a declaration for a body of code,
202  //  such as a function or method definition, this method returns the top-level
203  //  Stmt* of that body.  Otherwise this method returns null.
204  virtual Stmt* getBody() const { return 0; }
205
206  // global temp stats (until we have a per-module visitor)
207  static void addDeclKind(Kind k);
208  static bool CollectingStats(bool Enable = false);
209  static void PrintStats();
210
211  // Implement isa/cast/dyncast/etc.
212  static bool classof(const Decl *) { return true; }
213
214  /// Emit - Serialize this Decl to Bitcode.
215  void Emit(llvm::Serializer& S) const;
216
217  /// Create - Deserialize a Decl from Bitcode.
218  static Decl* Create(llvm::Deserializer& D, ASTContext& C);
219
220  /// Destroy - Call destructors and release memory.
221  virtual void Destroy(ASTContext& C);
222
223protected:
224  /// EmitImpl - Provides the subclass-specific serialization logic for
225  ///   serializing out a decl.
226  virtual void EmitImpl(llvm::Serializer& S) const {
227    // FIXME: This will eventually be a pure virtual function.
228    assert (false && "Not implemented.");
229  }
230
231  void EmitInRec(llvm::Serializer& S) const;
232  void ReadInRec(llvm::Deserializer& D, ASTContext& C);
233};
234
235/// DeclContext - This is used only as base class of specific decl types that
236/// can act as declaration contexts. These decls are:
237///
238///   TranslationUnitDecl
239///   NamespaceDecl
240///   FunctionDecl
241///   CXXRecordDecl
242///   EnumDecl
243///   ObjCMethodDecl
244///   ObjCInterfaceDecl
245///
246class DeclContext {
247  /// DeclKind - This indicates which class this is.
248  Decl::Kind DeclKind   :  8;
249
250  /// DeclChain - Linked list of declarations that are defined inside this
251  /// declaration context.
252  ScopedDecl *DeclChain;
253
254  // Used in the CastTo template to get the DeclKind
255  // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method
256  // to avoid 'ambiguous access' compiler errors.
257  template<typename T> struct KindTrait {
258    static Decl::Kind getKind(const T *D) { return D->getKind(); }
259  };
260
261  // Used only by the ToDecl and FromDecl methods
262  template<typename To, typename From>
263  static To *CastTo(const From *D) {
264    Decl::Kind DK = KindTrait<From>::getKind(D);
265    switch(DK) {
266      case Decl::TranslationUnit:
267        return static_cast<TranslationUnitDecl*>(const_cast<From*>(D));
268      case Decl::Namespace:
269        return static_cast<NamespaceDecl*>(const_cast<From*>(D));
270      case Decl::Enum:
271        return static_cast<EnumDecl*>(const_cast<From*>(D));
272      case Decl::ObjCMethod:
273        return static_cast<ObjCMethodDecl*>(const_cast<From*>(D));
274      case Decl::ObjCInterface:
275        return static_cast<ObjCInterfaceDecl*>(const_cast<From*>(D));
276      default:
277        if (DK >= Decl::FunctionFirst && DK <= Decl::FunctionLast)
278          return static_cast<FunctionDecl*>(const_cast<From*>(D));
279        if (DK >= Decl::CXXRecordFirst && DK <= Decl::CXXRecordLast)
280          return static_cast<CXXRecordDecl*>(const_cast<From*>(D));
281
282        assert(false && "a decl that inherits DeclContext isn't handled");
283        return 0;
284    }
285  }
286
287protected:
288  DeclContext(Decl::Kind K) : DeclKind(K), DeclChain(0) {}
289
290public:
291  /// getParent - Returns the containing DeclContext if this is a ScopedDecl,
292  /// else returns NULL.
293  DeclContext *getParent() const;
294
295  bool isFunctionOrMethod() const {
296    switch (DeclKind) {
297      case Decl::Function:
298      case Decl::CXXMethod:
299      case Decl::ObjCMethod:
300        return true;
301      default:
302        return false;
303    }
304  }
305
306  ScopedDecl *getDeclChain() const { return DeclChain; }
307  void setDeclChain(ScopedDecl *D) { DeclChain = D; }
308
309  /// ToDecl and FromDecl make Decl <-> DeclContext castings.
310  /// They are intended to be used by the simplify_type and cast_convert_val
311  /// templates.
312  static Decl        *ToDecl   (const DeclContext *D);
313  static DeclContext *FromDecl (const Decl *D);
314
315  static bool classof(const Decl *D) {
316    switch (D->getKind()) {
317      case Decl::TranslationUnit:
318      case Decl::Namespace:
319      case Decl::Enum:
320      case Decl::ObjCMethod:
321      case Decl::ObjCInterface:
322        return true;
323      default:
324        if (D->getKind() >= Decl::FunctionFirst &&
325            D->getKind() <= Decl::FunctionLast)
326          return true;
327        if (D->getKind() >= Decl::CXXRecordFirst &&
328            D->getKind() <= Decl::CXXRecordLast)
329          return true;
330        return false;
331    }
332  }
333  static bool classof(const DeclContext *D) { return true; }
334  static bool classof(const TranslationUnitDecl *D) { return true; }
335  static bool classof(const NamespaceDecl *D) { return true; }
336  static bool classof(const FunctionDecl *D) { return true; }
337  static bool classof(const CXXRecordDecl *D) { return true; }
338  static bool classof(const EnumDecl *D) { return true; }
339  static bool classof(const ObjCMethodDecl *D) { return true; }
340  static bool classof(const ObjCInterfaceDecl *D) { return true; }
341
342private:
343  void EmitOutRec(llvm::Serializer& S) const;
344  void ReadOutRec(llvm::Deserializer& D, ASTContext& C);
345
346  friend class Decl;
347};
348
349template<> struct DeclContext::KindTrait<DeclContext> {
350  static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; }
351};
352
353} // end clang.
354
355namespace llvm {
356/// Implement simplify_type for DeclContext, so that we can dyn_cast from
357/// DeclContext to a specific Decl class.
358  template<> struct simplify_type<const ::clang::DeclContext*> {
359  typedef ::clang::Decl* SimpleType;
360  static SimpleType getSimplifiedValue(const ::clang::DeclContext *Val) {
361    return ::clang::DeclContext::ToDecl(Val);
362  }
363};
364template<> struct simplify_type< ::clang::DeclContext*>
365  : public simplify_type<const ::clang::DeclContext*> {};
366
367template<> struct simplify_type<const ::clang::DeclContext> {
368  typedef ::clang::Decl SimpleType;
369  static SimpleType &getSimplifiedValue(const ::clang::DeclContext &Val) {
370    return *::clang::DeclContext::ToDecl(&Val);
371  }
372};
373template<> struct simplify_type< ::clang::DeclContext>
374  : public simplify_type<const ::clang::DeclContext> {};
375
376/// Implement cast_convert_val for DeclContext, so that we can dyn_cast from
377/// a Decl class to DeclContext.
378template<class FromTy>
379struct cast_convert_val< ::clang::DeclContext,const FromTy,const FromTy> {
380  static ::clang::DeclContext &doit(const FromTy &Val) {
381    return *::clang::DeclContext::FromDecl(&Val);
382  }
383};
384template<class FromTy>
385struct cast_convert_val< ::clang::DeclContext,FromTy,FromTy>
386  : public cast_convert_val< ::clang::DeclContext,const FromTy,const FromTy>
387    {};
388
389template<class FromTy>
390struct cast_convert_val< ::clang::DeclContext,const FromTy*,const FromTy*> {
391  static ::clang::DeclContext *doit(const FromTy *Val) {
392    return ::clang::DeclContext::FromDecl(Val);
393  }
394};
395template<class FromTy>
396struct cast_convert_val< ::clang::DeclContext,FromTy*,FromTy*>
397  : public cast_convert_val< ::clang::DeclContext,const FromTy*,const FromTy*>
398    {};
399
400} // end namespace llvm
401
402#endif
403