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/Basic/LLVM.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/PointerIntPair.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() : Ptr(0) {}
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 LLVM_EXPLICIT operator bool() const { return Ptr != 0; }
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 class PointerLikeTypeTraits<clang::OpaquePtr<T> > {
112 static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) {
113 // FIXME: Doesn't work? return P.getAs< void >();
114 return P.getAsOpaquePtr();
116 static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
117 return clang::OpaquePtr<T>::getFromOpaquePtr(P);
119 enum { NumLowBitsAvailable = 0 };
123 struct isPodLike<clang::OpaquePtr<T> > { static const bool value = true; };
128 class DiagnosticBuilder;
130 // Determines whether the low bit of the result pointer for the
131 // given UID is always zero. If so, ActionResult will use that bit
132 // for it's "invalid" flag.
134 struct IsResultPtrLowBitFree {
135 static const bool value = false;
138 /// ActionResult - This structure is used while parsing/acting on
139 /// expressions, stmts, etc. It encapsulates both the object returned by
140 /// the action, plus a sense of whether or not it is valid.
141 /// When CompressInvalid is true, the "invalid" flag will be
142 /// stored in the low bit of the Val pointer.
143 template<class PtrTy,
144 bool CompressInvalid = IsResultPtrLowBitFree<PtrTy>::value>
150 ActionResult(bool Invalid = false)
151 : Val(PtrTy()), Invalid(Invalid) {}
152 ActionResult(PtrTy val) : Val(val), Invalid(false) {}
153 ActionResult(const DiagnosticBuilder &) : Val(PtrTy()), Invalid(true) {}
155 // These two overloads prevent void* -> bool conversions.
156 ActionResult(const void *);
157 ActionResult(volatile void *);
159 bool isInvalid() const { return Invalid; }
160 bool isUsable() const { return !Invalid && Val; }
162 PtrTy get() const { return Val; }
163 // FIXME: Replace with get.
164 PtrTy release() const { return Val; }
165 PtrTy take() const { return Val; }
166 template <typename T> T *takeAs() { return static_cast<T*>(get()); }
168 void set(PtrTy V) { Val = V; }
170 const ActionResult &operator=(PtrTy RHS) {
177 // This ActionResult partial specialization places the "invalid"
178 // flag into the low bit of the pointer.
179 template<typename PtrTy>
180 class ActionResult<PtrTy, true> {
181 // A pointer whose low bit is 1 if this result is invalid, 0
183 uintptr_t PtrWithInvalid;
184 typedef llvm::PointerLikeTypeTraits<PtrTy> PtrTraits;
186 ActionResult(bool Invalid = false)
187 : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) { }
189 ActionResult(PtrTy V) {
190 void *VP = PtrTraits::getAsVoidPointer(V);
191 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
192 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
194 ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) { }
196 // These two overloads prevent void* -> bool conversions.
197 ActionResult(const void *);
198 ActionResult(volatile void *);
200 bool isInvalid() const { return PtrWithInvalid & 0x01; }
201 bool isUsable() const { return PtrWithInvalid > 0x01; }
204 void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
205 return PtrTraits::getFromVoidPointer(VP);
207 // FIXME: Replace with get.
208 PtrTy take() const { return get(); }
209 PtrTy release() const { return get(); }
210 template <typename T> T *takeAs() { return static_cast<T*>(get()); }
213 void *VP = PtrTraits::getAsVoidPointer(V);
214 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
215 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
218 const ActionResult &operator=(PtrTy RHS) {
219 void *VP = PtrTraits::getAsVoidPointer(RHS);
220 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
221 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
225 // For types where we can fit a flag in with the pointer, provide
226 // conversions to/from pointer type.
227 static ActionResult getFromOpaquePointer(void *P) {
229 Result.PtrWithInvalid = (uintptr_t)P;
232 void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; }
235 /// An opaque type for threading parsed type information through the
237 typedef OpaquePtr<QualType> ParsedType;
238 typedef UnionOpaquePtr<QualType> UnionParsedType;
240 // We can re-use the low bit of expression, statement, base, and
241 // member-initializer pointers for the "invalid" flag of
243 template<> struct IsResultPtrLowBitFree<Expr*> {
244 static const bool value = true;
246 template<> struct IsResultPtrLowBitFree<Stmt*> {
247 static const bool value = true;
249 template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> {
250 static const bool value = true;
252 template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> {
253 static const bool value = true;
256 typedef ActionResult<Expr*> ExprResult;
257 typedef ActionResult<Stmt*> StmtResult;
258 typedef ActionResult<ParsedType> TypeResult;
259 typedef ActionResult<CXXBaseSpecifier*> BaseResult;
260 typedef ActionResult<CXXCtorInitializer*> MemInitResult;
262 typedef ActionResult<Decl*> DeclResult;
263 typedef OpaquePtr<TemplateName> ParsedTemplateTy;
265 typedef llvm::MutableArrayRef<Expr*> MultiExprArg;
266 typedef llvm::MutableArrayRef<Stmt*> MultiStmtArg;
267 typedef llvm::MutableArrayRef<ParsedTemplateArgument> ASTTemplateArgsPtr;
268 typedef llvm::MutableArrayRef<ParsedType> MultiTypeArg;
269 typedef llvm::MutableArrayRef<TemplateParameterList*> MultiTemplateParamsArg;
271 inline ExprResult ExprError() { return ExprResult(true); }
272 inline StmtResult StmtError() { return StmtResult(true); }
274 inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
275 inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
277 inline ExprResult ExprEmpty() { return ExprResult(false); }
278 inline StmtResult StmtEmpty() { return StmtResult(false); }
280 inline Expr *AssertSuccess(ExprResult R) {
281 assert(!R.isInvalid() && "operation was asserted to never fail!");
285 inline Stmt *AssertSuccess(StmtResult R) {
286 assert(!R.isInvalid() && "operation was asserted to never fail!");