119510856727e0e14a3696b2a72c35163bff2a71fJohn McCall//===--- Ownership.h - Parser ownership helpers -----------------*- C++ -*-===//
27f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//
37f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//                     The LLVM Compiler Infrastructure
47f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//
57f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl// This file is distributed under the University of Illinois Open Source
67f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl// License. See LICENSE.TXT for details.
77f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//
87f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//===----------------------------------------------------------------------===//
97f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//
107f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//  This file contains classes for managing ownership of Stmt and Expr nodes.
117f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//
127f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl//===----------------------------------------------------------------------===//
137f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl
1419510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#ifndef LLVM_CLANG_SEMA_OWNERSHIP_H
1519510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#define LLVM_CLANG_SEMA_OWNERSHIP_H
167f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl
17686775deca8b8685eb90801495880e3abdd844c2Chris Lattner#include "clang/Basic/LLVM.h"
185354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer#include "llvm/ADT/ArrayRef.h"
19ff9b446dd1db0ed3a89403373df83b0da81dc6f5Chris Lattner#include "llvm/ADT/PointerIntPair.h"
20d51a183ffd7de05374ef20977e17ab4444bd633cChris Lattner
21b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner//===----------------------------------------------------------------------===//
22b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner// OpaquePtr
23b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner//===----------------------------------------------------------------------===//
24b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner
25b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnernamespace clang {
26ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  class Attr;
27cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  class CXXCtorInitializer;
28ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  class CXXBaseSpecifier;
29d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  class Decl;
302b5289b6fd7e3d9899868410a498c081c9595662John McCall  class DeclGroupRef;
31ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  class Expr;
32ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  class NestedNameSpecifier;
333fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  class ParsedTemplateArgument;
34b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  class QualType;
35ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  class Stmt;
362b5289b6fd7e3d9899868410a498c081c9595662John McCall  class TemplateName;
37ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  class TemplateParameterList;
381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  /// OpaquePtr - This is a very simple POD type that wraps a pointer that the
40b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  /// Parser doesn't know about but that Sema or another client does.  The UID
41b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  /// template argument is used to make sure that "Decl" pointers are not
42b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  /// compatible with "Type" pointers for example.
432b5289b6fd7e3d9899868410a498c081c9595662John McCall  template <class PtrTy>
44b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  class OpaquePtr {
45b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    void *Ptr;
462b5289b6fd7e3d9899868410a498c081c9595662John McCall    explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {}
472b5289b6fd7e3d9899868410a498c081c9595662John McCall
48b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    typedef llvm::PointerLikeTypeTraits<PtrTy> Traits;
49b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
50b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  public:
51b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    OpaquePtr() : Ptr(0) {}
521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; }
541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
552b5289b6fd7e3d9899868410a498c081c9595662John McCall    template <typename T> T* getAs() const {
562b5289b6fd7e3d9899868410a498c081c9595662John McCall      return get();
57d51a183ffd7de05374ef20977e17ab4444bd633cChris Lattner    }
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
592b5289b6fd7e3d9899868410a498c081c9595662John McCall    template <typename T> T getAsVal() const {
602b5289b6fd7e3d9899868410a498c081c9595662John McCall      return get();
612b5289b6fd7e3d9899868410a498c081c9595662John McCall    }
621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
632b5289b6fd7e3d9899868410a498c081c9595662John McCall    PtrTy get() const {
64b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return Traits::getFromVoidPointer(Ptr);
65d51a183ffd7de05374ef20977e17ab4444bd633cChris Lattner    }
661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
672b5289b6fd7e3d9899868410a498c081c9595662John McCall    void set(PtrTy P) {
68b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Ptr = Traits::getAsVoidPointer(P);
69d51a183ffd7de05374ef20977e17ab4444bd633cChris Lattner    }
701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    operator bool() const { return Ptr != 0; }
722b5289b6fd7e3d9899868410a498c081c9595662John McCall
732b5289b6fd7e3d9899868410a498c081c9595662John McCall    void *getAsOpaquePtr() const { return Ptr; }
742b5289b6fd7e3d9899868410a498c081c9595662John McCall    static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); }
75b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  };
76b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
77b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  /// UnionOpaquePtr - A version of OpaquePtr suitable for membership
78b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  /// in a union.
79b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  template <class T> struct UnionOpaquePtr {
80b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    void *Ptr;
81b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
82b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    static UnionOpaquePtr make(OpaquePtr<T> P) {
83b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      UnionOpaquePtr OP = { P.getAsOpaquePtr() };
84b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return OP;
85b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    }
86b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
87b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); }
88b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    operator OpaquePtr<T>() const { return get(); }
89b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
90b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    UnionOpaquePtr &operator=(OpaquePtr<T> P) {
91b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Ptr = P.getAsOpaquePtr();
92b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return *this;
93b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    }
94b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  };
95b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner}
96b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner
97b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnernamespace llvm {
982b5289b6fd7e3d9899868410a498c081c9595662John McCall  template <class T>
992b5289b6fd7e3d9899868410a498c081c9595662John McCall  class PointerLikeTypeTraits<clang::OpaquePtr<T> > {
100b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  public:
1012b5289b6fd7e3d9899868410a498c081c9595662John McCall    static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) {
102b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      // FIXME: Doesn't work? return P.getAs< void >();
1032b5289b6fd7e3d9899868410a498c081c9595662John McCall      return P.getAsOpaquePtr();
104b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    }
1052b5289b6fd7e3d9899868410a498c081c9595662John McCall    static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
1062b5289b6fd7e3d9899868410a498c081c9595662John McCall      return clang::OpaquePtr<T>::getFromOpaquePtr(P);
107b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    }
1082b5289b6fd7e3d9899868410a498c081c9595662John McCall    enum { NumLowBitsAvailable = 0 };
109b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  };
110f564289d3b117f2d268ca34e1e3b7d98857e1978Benjamin Kramer
111f564289d3b117f2d268ca34e1e3b7d98857e1978Benjamin Kramer  template <class T>
112f564289d3b117f2d268ca34e1e3b7d98857e1978Benjamin Kramer  struct isPodLike<clang::OpaquePtr<T> > { static const bool value = true; };
113b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner}
114b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner
115ff9b446dd1db0ed3a89403373df83b0da81dc6f5Chris Lattnernamespace clang {
11615faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  // Basic
1177f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl  class DiagnosticBuilder;
1187f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl
1195ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  // Determines whether the low bit of the result pointer for the
1205ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  // given UID is always zero. If so, ActionResult will use that bit
1215ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  // for it's "invalid" flag.
122ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  template<class Ptr>
1235ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  struct IsResultPtrLowBitFree {
1245ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor    static const bool value = false;
1255ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor  };
1265ac8aff3d7431dc7e4d64d960574a10c9f7e0078Douglas Gregor
127b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  /// ActionResult - This structure is used while parsing/acting on
128b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  /// expressions, stmts, etc.  It encapsulates both the object returned by
129b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  /// the action, plus a sense of whether or not it is valid.
130b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  /// When CompressInvalid is true, the "invalid" flag will be
131b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  /// stored in the low bit of the Val pointer.
132b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  template<class PtrTy,
133b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall           bool CompressInvalid = IsResultPtrLowBitFree<PtrTy>::value>
134b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  class ActionResult {
135b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    PtrTy Val;
136b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    bool Invalid;
137b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
138b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  public:
139182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    ActionResult(bool Invalid = false)
140182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall      : Val(PtrTy()), Invalid(Invalid) {}
141b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ActionResult(PtrTy val) : Val(val), Invalid(false) {}
142b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ActionResult(const DiagnosticBuilder &) : Val(PtrTy()), Invalid(true) {}
143b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
144b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    // These two overloads prevent void* -> bool conversions.
145b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ActionResult(const void *);
146b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ActionResult(volatile void *);
147b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
148182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    bool isInvalid() const { return Invalid; }
149182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    bool isUsable() const { return !Invalid && Val; }
150182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall
151b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    PtrTy get() const { return Val; }
1523fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    // FIXME: Replace with get.
153182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    PtrTy release() const { return Val; }
154182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    PtrTy take() const { return Val; }
155182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    template <typename T> T *takeAs() { return static_cast<T*>(get()); }
156182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall
157b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    void set(PtrTy V) { Val = V; }
158b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
159b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    const ActionResult &operator=(PtrTy RHS) {
160b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Val = RHS;
161b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Invalid = false;
162b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return *this;
163b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    }
164b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  };
165b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
166b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // This ActionResult partial specialization places the "invalid"
167b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // flag into the low bit of the pointer.
168b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  template<typename PtrTy>
169b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  class ActionResult<PtrTy, true> {
170b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    // A pointer whose low bit is 1 if this result is invalid, 0
171b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    // otherwise.
172b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    uintptr_t PtrWithInvalid;
173b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    typedef llvm::PointerLikeTypeTraits<PtrTy> PtrTraits;
1747f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl  public:
175b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ActionResult(bool Invalid = false)
176b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) { }
177b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
178b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ActionResult(PtrTy V) {
179b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      void *VP = PtrTraits::getAsVoidPointer(V);
180b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
181b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
182b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    }
183182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) { }
184b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
185b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    // These two overloads prevent void* -> bool conversions.
186b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ActionResult(const void *);
187b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ActionResult(volatile void *);
188b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
189182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    bool isInvalid() const { return PtrWithInvalid & 0x01; }
190182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    bool isUsable() const { return PtrWithInvalid > 0x01; }
191b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
192b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    PtrTy get() const {
193b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
194b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return PtrTraits::getFromVoidPointer(VP);
195b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    }
1963fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    // FIXME: Replace with get.
197182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    PtrTy take() const { return get(); }
198182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    PtrTy release() const { return get(); }
199182f7093dd9dabbcf6df44b4ae004ead38804b48John McCall    template <typename T> T *takeAs() { return static_cast<T*>(get()); }
200b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
201b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    void set(PtrTy V) {
202b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      void *VP = PtrTraits::getAsVoidPointer(V);
203b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
204b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
205b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    }
206b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall
207b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    const ActionResult &operator=(PtrTy RHS) {
208b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      void *VP = PtrTraits::getAsVoidPointer(RHS);
209b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
210b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
211b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return *this;
212b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    }
2137f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl  };
2147f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl
215f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  /// An opaque type for threading parsed type information through the
216f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  /// parser.
217f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  typedef OpaquePtr<QualType> ParsedType;
218f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  typedef UnionOpaquePtr<QualType> UnionParsedType;
219f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
220d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  // We can re-use the low bit of expression, statement, base, and
221d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  // member-initializer pointers for the "invalid" flag of
222d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  // ActionResult.
223ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  template<> struct IsResultPtrLowBitFree<Expr*> {
224ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    static const bool value = true;
225ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  };
226ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  template<> struct IsResultPtrLowBitFree<Stmt*> {
227ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    static const bool value = true;
228ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  };
229ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> {
230ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    static const bool value = true;
231ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  };
232cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> {
233ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    static const bool value = true;
234ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  };
235ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall
236b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  typedef ActionResult<Expr*> ExprResult;
237b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  typedef ActionResult<Stmt*> StmtResult;
238b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  typedef ActionResult<ParsedType> TypeResult;
239b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  typedef ActionResult<CXXBaseSpecifier*> BaseResult;
240cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  typedef ActionResult<CXXCtorInitializer*> MemInitResult;
241ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall
242b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  typedef ActionResult<Decl*> DeclResult;
2432b5289b6fd7e3d9899868410a498c081c9595662John McCall  typedef OpaquePtr<TemplateName> ParsedTemplateTy;
2449ae2f076ca5ab1feb3ba95629099ec2319833701John McCall
2455354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  typedef llvm::MutableArrayRef<Expr*> MultiExprArg;
2465354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  typedef llvm::MutableArrayRef<Stmt*> MultiStmtArg;
2475354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  typedef llvm::MutableArrayRef<ParsedTemplateArgument> ASTTemplateArgsPtr;
2485354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  typedef llvm::MutableArrayRef<ParsedType> MultiTypeArg;
2495354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  typedef llvm::MutableArrayRef<TemplateParameterList*> MultiTemplateParamsArg;
2509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall
251f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  inline ExprResult ExprError() { return ExprResult(true); }
252f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  inline StmtResult StmtError() { return StmtResult(true); }
253f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
254f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
255f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
256f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
257f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  inline ExprResult ExprEmpty() { return ExprResult(false); }
258f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  inline StmtResult StmtEmpty() { return StmtResult(false); }
259f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
26060d7b3a319d84d688752be3870615ac0f111fb16John McCall  inline Expr *AssertSuccess(ExprResult R) {
2619ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    assert(!R.isInvalid() && "operation was asserted to never fail!");
2629ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return R.get();
2639ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  }
2649ae2f076ca5ab1feb3ba95629099ec2319833701John McCall
26560d7b3a319d84d688752be3870615ac0f111fb16John McCall  inline Stmt *AssertSuccess(StmtResult R) {
2669ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    assert(!R.isInvalid() && "operation was asserted to never fail!");
2679ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return R.get();
2689ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  }
2697f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl}
2707f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl
2717f792faf0cee47fa1ea304bd7a7f502c0e5bbc9aSebastian Redl#endif
272