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"
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
26 class CXXCtorInitializer;
27 class CXXBaseSpecifier;
30 class ParsedTemplateArgument;
34 class TemplateParameterList;
36 /// \brief Wrapper for void* pointer.
37 /// \tparam PtrTy Either a pointer type like 'T*' or a type that behaves like
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>
47 explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {}
49 typedef llvm::PointerLikeTypeTraits<PtrTy> Traits;
52 OpaquePtr(std::nullptr_t = nullptr) {}
54 static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; }
56 /// \brief Returns plain pointer to the entity pointed by this wrapper.
57 /// \tparam PointeeT Type of pointed entity.
59 /// It is identical to getPtrAs<PointeeT*>.
60 template <typename PointeeT> PointeeT* getPtrTo() const {
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.
68 /// In contrast to getPtrTo, this method allows the return type to be
70 template <typename PtrT> PtrT getPtrAs() const {
75 return Traits::getFromVoidPointer(Ptr);
79 Ptr = Traits::getAsVoidPointer(P);
82 explicit operator bool() const { return Ptr != nullptr; }
84 void *getAsOpaquePtr() const { return Ptr; }
85 static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); }
88 /// UnionOpaquePtr - A version of OpaquePtr suitable for membership
90 template <class T> struct UnionOpaquePtr {
93 static UnionOpaquePtr make(OpaquePtr<T> P) {
94 UnionOpaquePtr OP = { P.getAsOpaquePtr() };
98 OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); }
99 operator OpaquePtr<T>() const { return get(); }
101 UnionOpaquePtr &operator=(OpaquePtr<T> P) {
102 Ptr = P.getAsOpaquePtr();
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();
115 static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
116 return clang::OpaquePtr<T>::getFromOpaquePtr(P);
118 enum { NumLowBitsAvailable = 0 };
122 struct isPodLike<clang::OpaquePtr<T> > { static const bool value = true; };
127 class DiagnosticBuilder;
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.
133 struct IsResultPtrLowBitFree {
134 static const bool value = false;
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>
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) {}
154 // These two overloads prevent void* -> bool conversions.
155 ActionResult(const void *) = delete;
156 ActionResult(volatile void *) = delete;
158 bool isInvalid() const { return Invalid; }
159 bool isUsable() const { return !Invalid && Val; }
160 bool isUnset() const { return !Invalid && !Val; }
162 PtrTy get() const { return Val; }
163 template <typename T> T *getAs() { return static_cast<T*>(get()); }
165 void set(PtrTy V) { Val = V; }
167 const ActionResult &operator=(PtrTy RHS) {
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
180 uintptr_t PtrWithInvalid;
181 typedef llvm::PointerLikeTypeTraits<PtrTy> PtrTraits;
183 ActionResult(bool Invalid = false)
184 : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) { }
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");
191 ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) { }
193 // These two overloads prevent void* -> bool conversions.
194 ActionResult(const void *) = delete;
195 ActionResult(volatile void *) = delete;
197 bool isInvalid() const { return PtrWithInvalid & 0x01; }
198 bool isUsable() const { return PtrWithInvalid > 0x01; }
199 bool isUnset() const { return PtrWithInvalid == 0; }
202 void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
203 return PtrTraits::getFromVoidPointer(VP);
205 template <typename T> T *getAs() { return static_cast<T*>(get()); }
208 void *VP = PtrTraits::getAsVoidPointer(V);
209 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
210 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
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");
220 // For types where we can fit a flag in with the pointer, provide
221 // conversions to/from pointer type.
222 static ActionResult getFromOpaquePointer(void *P) {
224 Result.PtrWithInvalid = (uintptr_t)P;
227 void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; }
230 /// An opaque type for threading parsed type information through the
232 typedef OpaquePtr<QualType> ParsedType;
233 typedef UnionOpaquePtr<QualType> UnionParsedType;
235 // We can re-use the low bit of expression, statement, base, and
236 // member-initializer pointers for the "invalid" flag of
238 template<> struct IsResultPtrLowBitFree<Expr*> {
239 static const bool value = true;
241 template<> struct IsResultPtrLowBitFree<Stmt*> {
242 static const bool value = true;
244 template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> {
245 static const bool value = true;
247 template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> {
248 static const bool value = true;
251 typedef ActionResult<Expr*> ExprResult;
252 typedef ActionResult<Stmt*> StmtResult;
253 typedef ActionResult<ParsedType> TypeResult;
254 typedef ActionResult<CXXBaseSpecifier*> BaseResult;
255 typedef ActionResult<CXXCtorInitializer*> MemInitResult;
257 typedef ActionResult<Decl*> DeclResult;
258 typedef OpaquePtr<TemplateName> ParsedTemplateTy;
259 typedef UnionOpaquePtr<TemplateName> UnionParsedTemplateTy;
261 typedef MutableArrayRef<Expr*> MultiExprArg;
262 typedef MutableArrayRef<Stmt*> MultiStmtArg;
263 typedef MutableArrayRef<ParsedTemplateArgument> ASTTemplateArgsPtr;
264 typedef MutableArrayRef<ParsedType> MultiTypeArg;
265 typedef MutableArrayRef<TemplateParameterList*> MultiTemplateParamsArg;
267 inline ExprResult ExprError() { return ExprResult(true); }
268 inline StmtResult StmtError() { return StmtResult(true); }
270 inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
271 inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
273 inline ExprResult ExprEmpty() { return ExprResult(false); }
274 inline StmtResult StmtEmpty() { return StmtResult(false); }
276 inline Expr *AssertSuccess(ExprResult R) {
277 assert(!R.isInvalid() && "operation was asserted to never fail!");
281 inline Stmt *AssertSuccess(StmtResult R) {
282 assert(!R.isInvalid() && "operation was asserted to never fail!");