clang  8.0.0svn
Ownership.h
Go to the documentation of this file.
1 //===- Ownership.h - Parser ownership helpers -------------------*- 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 contains classes for managing ownership of Stmt and Expr nodes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_SEMA_OWNERSHIP_H
15 #define LLVM_CLANG_SEMA_OWNERSHIP_H
16 
17 #include "clang/AST/Expr.h"
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/Support/PointerLikeTypeTraits.h"
21 #include "llvm/Support/type_traits.h"
22 #include <cassert>
23 #include <cstddef>
24 #include <cstdint>
25 
26 //===----------------------------------------------------------------------===//
27 // OpaquePtr
28 //===----------------------------------------------------------------------===//
29 
30 namespace clang {
31 
32 class CXXBaseSpecifier;
33 class CXXCtorInitializer;
34 class Decl;
35 class Expr;
36 class ParsedTemplateArgument;
37 class QualType;
38 class Stmt;
39 class TemplateName;
40 class TemplateParameterList;
41 
42  /// Wrapper for void* pointer.
43  /// \tparam PtrTy Either a pointer type like 'T*' or a type that behaves like
44  /// a pointer.
45  ///
46  /// This is a very simple POD type that wraps a pointer that the Parser
47  /// doesn't know about but that Sema or another client does. The PtrTy
48  /// template argument is used to make sure that "Decl" pointers are not
49  /// compatible with "Type" pointers for example.
50  template <class PtrTy>
51  class OpaquePtr {
52  void *Ptr = nullptr;
53 
54  explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {}
55 
57 
58  public:
59  OpaquePtr(std::nullptr_t = nullptr) {}
60 
61  static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; }
62 
63  /// Returns plain pointer to the entity pointed by this wrapper.
64  /// \tparam PointeeT Type of pointed entity.
65  ///
66  /// It is identical to getPtrAs<PointeeT*>.
67  template <typename PointeeT> PointeeT* getPtrTo() const {
68  return get();
69  }
70 
71  /// Returns pointer converted to the specified type.
72  /// \tparam PtrT Result pointer type. There must be implicit conversion
73  /// from PtrTy to PtrT.
74  ///
75  /// In contrast to getPtrTo, this method allows the return type to be
76  /// a smart pointer.
77  template <typename PtrT> PtrT getPtrAs() const {
78  return get();
79  }
80 
81  PtrTy get() const {
82  return Traits::getFromVoidPointer(Ptr);
83  }
84 
85  void set(PtrTy P) {
86  Ptr = Traits::getAsVoidPointer(P);
87  }
88 
89  explicit operator bool() const { return Ptr != nullptr; }
90 
91  void *getAsOpaquePtr() const { return Ptr; }
92  static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); }
93  };
94 
95  /// UnionOpaquePtr - A version of OpaquePtr suitable for membership
96  /// in a union.
97  template <class T> struct UnionOpaquePtr {
98  void *Ptr;
99 
101  UnionOpaquePtr OP = { P.getAsOpaquePtr() };
102  return OP;
103  }
104 
105  OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); }
106  operator OpaquePtr<T>() const { return get(); }
107 
109  Ptr = P.getAsOpaquePtr();
110  return *this;
111  }
112  };
113 
114 } // namespace clang
115 
116 namespace llvm {
117 
118  template <class T>
119  struct PointerLikeTypeTraits<clang::OpaquePtr<T>> {
120  enum { NumLowBitsAvailable = 0 };
121 
122  static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) {
123  // FIXME: Doesn't work? return P.getAs< void >();
124  return P.getAsOpaquePtr();
125  }
126 
127  static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
129  }
130  };
131 
132  template <class T>
133  struct isPodLike<clang::OpaquePtr<T>> { static const bool value = true; };
134 
135 } // namespace llvm
136 
137 namespace clang {
138 
139  // Basic
140  class DiagnosticBuilder;
141 
142  // Determines whether the low bit of the result pointer for the
143  // given UID is always zero. If so, ActionResult will use that bit
144  // for it's "invalid" flag.
145  template<class Ptr>
147  static const bool value = false;
148  };
149 
150  /// ActionResult - This structure is used while parsing/acting on
151  /// expressions, stmts, etc. It encapsulates both the object returned by
152  /// the action, plus a sense of whether or not it is valid.
153  /// When CompressInvalid is true, the "invalid" flag will be
154  /// stored in the low bit of the Val pointer.
155  template<class PtrTy,
156  bool CompressInvalid = IsResultPtrLowBitFree<PtrTy>::value>
157  class ActionResult {
158  PtrTy Val;
159  bool Invalid;
160 
161  public:
162  ActionResult(bool Invalid = false) : Val(PtrTy()), Invalid(Invalid) {}
163  ActionResult(PtrTy val) : Val(val), Invalid(false) {}
164  ActionResult(const DiagnosticBuilder &) : Val(PtrTy()), Invalid(true) {}
165 
166  // These two overloads prevent void* -> bool conversions.
167  ActionResult(const void *) = delete;
168  ActionResult(volatile void *) = delete;
169 
170  bool isInvalid() const { return Invalid; }
171  bool isUsable() const { return !Invalid && Val; }
172  bool isUnset() const { return !Invalid && !Val; }
173 
174  PtrTy get() const { return Val; }
175  template <typename T> T *getAs() { return static_cast<T*>(get()); }
176 
177  void set(PtrTy V) { Val = V; }
178 
179  const ActionResult &operator=(PtrTy RHS) {
180  Val = RHS;
181  Invalid = false;
182  return *this;
183  }
184  };
185 
186  // This ActionResult partial specialization places the "invalid"
187  // flag into the low bit of the pointer.
188  template<typename PtrTy>
189  class ActionResult<PtrTy, true> {
190  // A pointer whose low bit is 1 if this result is invalid, 0
191  // otherwise.
192  uintptr_t PtrWithInvalid;
193 
195 
196  public:
197  ActionResult(bool Invalid = false)
198  : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) {}
199 
200  ActionResult(PtrTy V) {
201  void *VP = PtrTraits::getAsVoidPointer(V);
202  PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
203  assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
204  }
205 
206  ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) {}
207 
208  // These two overloads prevent void* -> bool conversions.
209  ActionResult(const void *) = delete;
210  ActionResult(volatile void *) = delete;
211 
212  bool isInvalid() const { return PtrWithInvalid & 0x01; }
213  bool isUsable() const { return PtrWithInvalid > 0x01; }
214  bool isUnset() const { return PtrWithInvalid == 0; }
215 
216  PtrTy get() const {
217  void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
218  return PtrTraits::getFromVoidPointer(VP);
219  }
220 
221  template <typename T> T *getAs() { return static_cast<T*>(get()); }
222 
223  void set(PtrTy V) {
224  void *VP = PtrTraits::getAsVoidPointer(V);
225  PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
226  assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
227  }
228 
229  const ActionResult &operator=(PtrTy RHS) {
230  void *VP = PtrTraits::getAsVoidPointer(RHS);
231  PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
232  assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
233  return *this;
234  }
235 
236  // For types where we can fit a flag in with the pointer, provide
237  // conversions to/from pointer type.
240  Result.PtrWithInvalid = (uintptr_t)P;
241  return Result;
242  }
243  void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; }
244  };
245 
246  /// An opaque type for threading parsed type information through the
247  /// parser.
250 
251  // We can re-use the low bit of expression, statement, base, and
252  // member-initializer pointers for the "invalid" flag of
253  // ActionResult.
254  template<> struct IsResultPtrLowBitFree<Expr*> {
255  static const bool value = true;
256  };
257  template<> struct IsResultPtrLowBitFree<Stmt*> {
258  static const bool value = true;
259  };
261  static const bool value = true;
262  };
264  static const bool value = true;
265  };
266 
272 
276 
282 
283  inline ExprResult ExprError() { return ExprResult(true); }
284  inline StmtResult StmtError() { return StmtResult(true); }
285 
286  inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
287  inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
288 
289  inline ExprResult ExprEmpty() { return ExprResult(false); }
290  inline StmtResult StmtEmpty() { return StmtResult(false); }
291 
293  assert(!R.isInvalid() && "operation was asserted to never fail!");
294  return R.get();
295  }
296 
298  assert(!R.isInvalid() && "operation was asserted to never fail!");
299  return R.get();
300  }
301 
302 } // namespace clang
303 
304 #endif // LLVM_CLANG_SEMA_OWNERSHIP_H
OpaquePtr(std::nullptr_t=nullptr)
Definition: Ownership.h:59
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
PointeeT * getPtrTo() const
Returns plain pointer to the entity pointed by this wrapper.
Definition: Ownership.h:67
Stmt - This represents one statement.
Definition: Stmt.h:66
const ActionResult & operator=(PtrTy RHS)
Definition: Ownership.h:179
StringRef P
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Wrapper for void* pointer.
Definition: Ownership.h:51
static ActionResult getFromOpaquePointer(void *P)
Definition: Ownership.h:238
ActionResult< Stmt * > StmtResult
Definition: Ownership.h:268
bool isUnset() const
Definition: Ownership.h:172
ExprResult ExprEmpty()
Definition: Ownership.h:289
PtrT getPtrAs() const
Returns pointer converted to the specified type.
Definition: Ownership.h:77
void * getAsOpaquePtr() const
Definition: Ownership.h:91
PtrTy get() const
Definition: Ownership.h:174
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
StmtResult StmtError()
Definition: Ownership.h:284
ActionResult(const DiagnosticBuilder &)
Definition: Ownership.h:206
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1040
ActionResult(PtrTy val)
Definition: Ownership.h:163
This represents one expression.
Definition: Expr.h:106
StmtResult StmtEmpty()
Definition: Ownership.h:290
#define bool
Definition: stdbool.h:31
bool isInvalid() const
Definition: Ownership.h:170
bool isUsable() const
Definition: Ownership.h:171
The result type of a method or function.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:90
#define false
Definition: stdbool.h:33
ActionResult - This structure is used while parsing/acting on expressions, stmts, etc...
Definition: Ownership.h:157
ActionResult(bool Invalid=false)
Definition: Ownership.h:162
static UnionOpaquePtr make(OpaquePtr< T > P)
Definition: Ownership.h:100
UnionOpaquePtr - A version of OpaquePtr suitable for membership in a union.
Definition: Ownership.h:97
Dataflow Directional Tag Classes.
static void * getAsVoidPointer(clang::OpaquePtr< T > P)
Definition: Ownership.h:122
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2253
UnionOpaquePtr & operator=(OpaquePtr< T > P)
Definition: Ownership.h:108
static clang::OpaquePtr< T > getFromVoidPointer(void *P)
Definition: Ownership.h:127
Represents a base class of a C++ class.
Definition: DeclCXX.h:192
ActionResult< Expr * > ExprResult
Definition: Ownership.h:267
ActionResult(bool Invalid=false)
Definition: Ownership.h:197
ActionResult(const DiagnosticBuilder &)
Definition: Ownership.h:164
ExprResult ExprError()
Definition: Ownership.h:283
static OpaquePtr make(PtrTy P)
Definition: Ownership.h:61
#define true
Definition: stdbool.h:32
Expr * AssertSuccess(ExprResult R)
Definition: Ownership.h:292
static OpaquePtr getFromOpaquePtr(void *P)
Definition: Ownership.h:92
void set(PtrTy P)
Definition: Ownership.h:85
const ActionResult & operator=(PtrTy RHS)
Definition: Ownership.h:229