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