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