1 //===- Ownership.h - Parser ownership helpers -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains classes for managing ownership of Stmt and Expr nodes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SEMA_OWNERSHIP_H
15 #define LLVM_CLANG_SEMA_OWNERSHIP_H
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"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
32 class CXXBaseSpecifier;
33 class CXXCtorInitializer;
36 class ParsedTemplateArgument;
40 class TemplateParameterList;
42 /// Wrapper for void* pointer.
43 /// \tparam PtrTy Either a pointer type like 'T*' or a type that behaves like
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>
54 explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {}
56 using Traits = llvm::PointerLikeTypeTraits<PtrTy>;
59 OpaquePtr(std::nullptr_t = nullptr) {}
61 static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; }
63 /// Returns plain pointer to the entity pointed by this wrapper.
64 /// \tparam PointeeT Type of pointed entity.
66 /// It is identical to getPtrAs<PointeeT*>.
67 template <typename PointeeT> PointeeT* getPtrTo() const {
71 /// Returns pointer converted to the specified type.
72 /// \tparam PtrT Result pointer type. There must be implicit conversion
73 /// from PtrTy to PtrT.
75 /// In contrast to getPtrTo, this method allows the return type to be
77 template <typename PtrT> PtrT getPtrAs() const {
82 return Traits::getFromVoidPointer(Ptr);
86 Ptr = Traits::getAsVoidPointer(P);
89 explicit operator bool() const { return Ptr != nullptr; }
91 void *getAsOpaquePtr() const { return Ptr; }
92 static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); }
95 /// UnionOpaquePtr - A version of OpaquePtr suitable for membership
97 template <class T> struct UnionOpaquePtr {
100 static UnionOpaquePtr make(OpaquePtr<T> P) {
101 UnionOpaquePtr OP = { P.getAsOpaquePtr() };
105 OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); }
106 operator OpaquePtr<T>() const { return get(); }
108 UnionOpaquePtr &operator=(OpaquePtr<T> P) {
109 Ptr = P.getAsOpaquePtr();
119 struct PointerLikeTypeTraits<clang::OpaquePtr<T>> {
120 enum { NumLowBitsAvailable = 0 };
122 static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) {
123 // FIXME: Doesn't work? return P.getAs< void >();
124 return P.getAsOpaquePtr();
127 static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
128 return clang::OpaquePtr<T>::getFromOpaquePtr(P);
133 struct isPodLike<clang::OpaquePtr<T>> { static const bool value = true; };
140 class DiagnosticBuilder;
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.
146 struct IsResultPtrLowBitFree {
147 static const bool value = false;
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>
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) {}
166 // These two overloads prevent void* -> bool conversions.
167 ActionResult(const void *) = delete;
168 ActionResult(volatile void *) = delete;
170 bool isInvalid() const { return Invalid; }
171 bool isUsable() const { return !Invalid && Val; }
172 bool isUnset() const { return !Invalid && !Val; }
174 PtrTy get() const { return Val; }
175 template <typename T> T *getAs() { return static_cast<T*>(get()); }
177 void set(PtrTy V) { Val = V; }
179 const ActionResult &operator=(PtrTy RHS) {
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
192 uintptr_t PtrWithInvalid;
194 using PtrTraits = llvm::PointerLikeTypeTraits<PtrTy>;
197 ActionResult(bool Invalid = false)
198 : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) {}
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");
206 ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) {}
208 // These two overloads prevent void* -> bool conversions.
209 ActionResult(const void *) = delete;
210 ActionResult(volatile void *) = delete;
212 bool isInvalid() const { return PtrWithInvalid & 0x01; }
213 bool isUsable() const { return PtrWithInvalid > 0x01; }
214 bool isUnset() const { return PtrWithInvalid == 0; }
217 void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
218 return PtrTraits::getFromVoidPointer(VP);
221 template <typename T> T *getAs() { return static_cast<T*>(get()); }
224 void *VP = PtrTraits::getAsVoidPointer(V);
225 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
226 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
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");
236 // For types where we can fit a flag in with the pointer, provide
237 // conversions to/from pointer type.
238 static ActionResult getFromOpaquePointer(void *P) {
240 Result.PtrWithInvalid = (uintptr_t)P;
243 void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; }
246 /// An opaque type for threading parsed type information through the
248 using ParsedType = OpaquePtr<QualType>;
249 using UnionParsedType = UnionOpaquePtr<QualType>;
251 // We can re-use the low bit of expression, statement, base, and
252 // member-initializer pointers for the "invalid" flag of
254 template<> struct IsResultPtrLowBitFree<Expr*> {
255 static const bool value = true;
257 template<> struct IsResultPtrLowBitFree<Stmt*> {
258 static const bool value = true;
260 template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> {
261 static const bool value = true;
263 template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> {
264 static const bool value = true;
267 using ExprResult = ActionResult<Expr *>;
268 using StmtResult = ActionResult<Stmt *>;
269 using TypeResult = ActionResult<ParsedType>;
270 using BaseResult = ActionResult<CXXBaseSpecifier *>;
271 using MemInitResult = ActionResult<CXXCtorInitializer *>;
273 using DeclResult = ActionResult<Decl *>;
274 using ParsedTemplateTy = OpaquePtr<TemplateName>;
275 using UnionParsedTemplateTy = UnionOpaquePtr<TemplateName>;
277 using MultiExprArg = MutableArrayRef<Expr *>;
278 using MultiStmtArg = MutableArrayRef<Stmt *>;
279 using ASTTemplateArgsPtr = MutableArrayRef<ParsedTemplateArgument>;
280 using MultiTypeArg = MutableArrayRef<ParsedType>;
281 using MultiTemplateParamsArg = MutableArrayRef<TemplateParameterList *>;
283 inline ExprResult ExprError() { return ExprResult(true); }
284 inline StmtResult StmtError() { return StmtResult(true); }
286 inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
287 inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
289 inline ExprResult ExprEmpty() { return ExprResult(false); }
290 inline StmtResult StmtEmpty() { return StmtResult(false); }
292 inline Expr *AssertSuccess(ExprResult R) {
293 assert(!R.isInvalid() && "operation was asserted to never fail!");
297 inline Stmt *AssertSuccess(StmtResult R) {
298 assert(!R.isInvalid() && "operation was asserted to never fail!");
304 #endif // LLVM_CLANG_SEMA_OWNERSHIP_H