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