DeclAccessPair.h revision 207619
192108Sphk//===--- DeclAccessPair.h - A decl bundled with its path access -*- C++ -*-===//
292108Sphk//
392108Sphk//                     The LLVM Compiler Infrastructure
492108Sphk//
592108Sphk// This file is distributed under the University of Illinois Open Source
692108Sphk// License. See LICENSE.TXT for details.
792108Sphk//
892108Sphk//===----------------------------------------------------------------------===//
992108Sphk//
1092108Sphk//  This file defines the DeclAccessPair class, which provides an
1192108Sphk//  efficient representation of a pair of a NamedDecl* and an
1292108Sphk//  AccessSpecifier.  Generally the access specifier gives the
1392108Sphk//  natural access of a declaration when named in a class, as
1492108Sphk//  defined in C++ [class.access.base]p1.
1592108Sphk//
1692108Sphk//===----------------------------------------------------------------------===//
1792108Sphk
1892108Sphk#ifndef LLVM_CLANG_AST_DECLACCESSPAIR_H
1992108Sphk#define LLVM_CLANG_AST_DECLACCESSPAIR_H
2092108Sphk
2192108Sphk#include "clang/Basic/Specifiers.h"
2292108Sphk
2392108Sphknamespace clang {
2492108Sphk
2592108Sphkclass NamedDecl;
2692108Sphk
2792108Sphk/// A POD class for pairing a NamedDecl* with an access specifier.
2892108Sphk/// Can be put into unions.
2992108Sphkclass DeclAccessPair {
3092108Sphk  NamedDecl *Ptr; // we'd use llvm::PointerUnion, but it isn't trivial
3192108Sphk
3292108Sphk  enum { Mask = 0x3 };
3392108Sphk
3492108Sphkpublic:
3592108Sphk  static DeclAccessPair make(NamedDecl *D, AccessSpecifier AS) {
3692108Sphk    DeclAccessPair p;
3792108Sphk    p.set(D, AS);
3892108Sphk    return p;
3992108Sphk  }
4092108Sphk
41116196Sobrien  NamedDecl *getDecl() const {
42116196Sobrien    return (NamedDecl*) (~Mask & (uintptr_t) Ptr);
43116196Sobrien  }
4492108Sphk  AccessSpecifier getAccess() const {
4592108Sphk    return AccessSpecifier(Mask & (uintptr_t) Ptr);
4692108Sphk  }
4792108Sphk
4892108Sphk  void setDecl(NamedDecl *D) {
4992108Sphk    set(D, getAccess());
50136837Sphk  }
5192108Sphk  void setAccess(AccessSpecifier AS) {
5292108Sphk    set(getDecl(), AS);
5392108Sphk  }
5493250Sphk  void set(NamedDecl *D, AccessSpecifier AS) {
5592108Sphk    Ptr = reinterpret_cast<NamedDecl*>(uintptr_t(AS) |
56136837Sphk                                       reinterpret_cast<uintptr_t>(D));
57136837Sphk  }
58113938Sphk
59113938Sphk  operator NamedDecl*() const { return getDecl(); }
6092108Sphk  NamedDecl *operator->() const { return getDecl(); }
61109170Sphk};
6292108Sphk}
63104053Sphk
64131820Sphk// Take a moment to tell SmallVector that DeclAccessPair is POD.
6592108Sphknamespace llvm {
66113938Sphktemplate<typename> struct isPodLike;
67113938Sphktemplate<> struct isPodLike<clang::DeclAccessPair> {
68113938Sphk   static const bool value = true;
69113938Sphk};
70113940Sphk}
71113938Sphk
72113940Sphk#endif
73113938Sphk