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/ADT/PointerIntPair.h"
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
27 class CXXCtorInitializer;
28 class CXXBaseSpecifier;
31 class ParsedTemplateArgument;
35 class TemplateParameterList;
37 /// \brief Wrapper for void* pointer.
38 /// \tparam PtrTy Either a pointer type like 'T*' or a type that behaves like
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>
48 explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {}
50 typedef llvm::PointerLikeTypeTraits<PtrTy> Traits;
53 OpaquePtr(std::nullptr_t = nullptr) {}
55 static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; }
57 /// \brief Returns plain pointer to the entity pointed by this wrapper.
58 /// \tparam PointeeT Type of pointed entity.
60 /// It is identical to getPtrAs<PointeeT*>.
61 template <typename PointeeT> PointeeT* getPtrTo() const {
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.
69 /// In contrast to getPtrTo, this method allows the return type to be
71 template <typename PtrT> PtrT getPtrAs() const {
76 return Traits::getFromVoidPointer(Ptr);
80 Ptr = Traits::getAsVoidPointer(P);
83 explicit operator bool() const { return Ptr != nullptr; }
85 void *getAsOpaquePtr() const { return Ptr; }
86 static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); }
89 /// UnionOpaquePtr - A version of OpaquePtr suitable for membership
91 template <class T> struct UnionOpaquePtr {
94 static UnionOpaquePtr make(OpaquePtr<T> P) {
95 UnionOpaquePtr OP = { P.getAsOpaquePtr() };
99 OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); }
100 operator OpaquePtr<T>() const { return get(); }
102 UnionOpaquePtr &operator=(OpaquePtr<T> P) {
103 Ptr = P.getAsOpaquePtr();
111 class PointerLikeTypeTraits<clang::OpaquePtr<T> > {
113 static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) {
114 // FIXME: Doesn't work? return P.getAs< void >();
115 return P.getAsOpaquePtr();
117 static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
118 return clang::OpaquePtr<T>::getFromOpaquePtr(P);
120 enum { NumLowBitsAvailable = 0 };
124 struct isPodLike<clang::OpaquePtr<T> > { static const bool value = true; };
129 class DiagnosticBuilder;
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.
135 struct IsResultPtrLowBitFree {
136 static const bool value = false;
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>
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) {}
156 // These two overloads prevent void* -> bool conversions.
157 ActionResult(const void *);
158 ActionResult(volatile void *);
160 bool isInvalid() const { return Invalid; }
161 bool isUsable() const { return !Invalid && Val; }
162 bool isUnset() const { return !Invalid && !Val; }
164 PtrTy get() const { return Val; }
165 template <typename T> T *getAs() { return static_cast<T*>(get()); }
167 void set(PtrTy V) { Val = V; }
169 const ActionResult &operator=(PtrTy RHS) {
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
182 uintptr_t PtrWithInvalid;
183 typedef llvm::PointerLikeTypeTraits<PtrTy> PtrTraits;
185 ActionResult(bool Invalid = false)
186 : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) { }
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");
193 ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) { }
195 // These two overloads prevent void* -> bool conversions.
196 ActionResult(const void *);
197 ActionResult(volatile void *);
199 bool isInvalid() const { return PtrWithInvalid & 0x01; }
200 bool isUsable() const { return PtrWithInvalid > 0x01; }
201 bool isUnset() const { return PtrWithInvalid == 0; }
204 void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
205 return PtrTraits::getFromVoidPointer(VP);
207 template <typename T> T *getAs() { return static_cast<T*>(get()); }
210 void *VP = PtrTraits::getAsVoidPointer(V);
211 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
212 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
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");
222 // For types where we can fit a flag in with the pointer, provide
223 // conversions to/from pointer type.
224 static ActionResult getFromOpaquePointer(void *P) {
226 Result.PtrWithInvalid = (uintptr_t)P;
229 void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; }
232 /// An opaque type for threading parsed type information through the
234 typedef OpaquePtr<QualType> ParsedType;
235 typedef UnionOpaquePtr<QualType> UnionParsedType;
237 // We can re-use the low bit of expression, statement, base, and
238 // member-initializer pointers for the "invalid" flag of
240 template<> struct IsResultPtrLowBitFree<Expr*> {
241 static const bool value = true;
243 template<> struct IsResultPtrLowBitFree<Stmt*> {
244 static const bool value = true;
246 template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> {
247 static const bool value = true;
249 template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> {
250 static const bool value = true;
253 typedef ActionResult<Expr*> ExprResult;
254 typedef ActionResult<Stmt*> StmtResult;
255 typedef ActionResult<ParsedType> TypeResult;
256 typedef ActionResult<CXXBaseSpecifier*> BaseResult;
257 typedef ActionResult<CXXCtorInitializer*> MemInitResult;
259 typedef ActionResult<Decl*> DeclResult;
260 typedef OpaquePtr<TemplateName> ParsedTemplateTy;
262 typedef MutableArrayRef<Expr*> MultiExprArg;
263 typedef MutableArrayRef<Stmt*> MultiStmtArg;
264 typedef MutableArrayRef<ParsedTemplateArgument> ASTTemplateArgsPtr;
265 typedef MutableArrayRef<ParsedType> MultiTypeArg;
266 typedef MutableArrayRef<TemplateParameterList*> MultiTemplateParamsArg;
268 inline ExprResult ExprError() { return ExprResult(true); }
269 inline StmtResult StmtError() { return StmtResult(true); }
271 inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
272 inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
274 inline ExprResult ExprEmpty() { return ExprResult(false); }
275 inline StmtResult StmtEmpty() { return StmtResult(false); }
277 inline Expr *AssertSuccess(ExprResult R) {
278 assert(!R.isInvalid() && "operation was asserted to never fail!");
282 inline Stmt *AssertSuccess(StmtResult R) {
283 assert(!R.isInvalid() && "operation was asserted to never fail!");