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 /// OpaquePtr - This is a very simple POD type that wraps a pointer that the
37 /// Parser doesn't know about but that Sema or another client does. The UID
38 /// template argument is used to make sure that "Decl" pointers are not
39 /// compatible with "Type" pointers for example.
40 template <class PtrTy>
43 explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {}
45 typedef llvm::PointerLikeTypeTraits<PtrTy> Traits;
48 OpaquePtr() : Ptr(0) {}
50 static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; }
52 template <typename T> T* getAs() const {
56 template <typename T> T getAsVal() const {
61 return Traits::getFromVoidPointer(Ptr);
65 Ptr = Traits::getAsVoidPointer(P);
68 operator bool() const { return Ptr != 0; }
70 void *getAsOpaquePtr() const { return Ptr; }
71 static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); }
74 /// UnionOpaquePtr - A version of OpaquePtr suitable for membership
76 template <class T> struct UnionOpaquePtr {
79 static UnionOpaquePtr make(OpaquePtr<T> P) {
80 UnionOpaquePtr OP = { P.getAsOpaquePtr() };
84 OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); }
85 operator OpaquePtr<T>() const { return get(); }
87 UnionOpaquePtr &operator=(OpaquePtr<T> P) {
88 Ptr = P.getAsOpaquePtr();
96 class PointerLikeTypeTraits<clang::OpaquePtr<T> > {
98 static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) {
99 // FIXME: Doesn't work? return P.getAs< void >();
100 return P.getAsOpaquePtr();
102 static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
103 return clang::OpaquePtr<T>::getFromOpaquePtr(P);
105 enum { NumLowBitsAvailable = 0 };
109 struct isPodLike<clang::OpaquePtr<T> > { static const bool value = true; };
114 class DiagnosticBuilder;
116 // Determines whether the low bit of the result pointer for the
117 // given UID is always zero. If so, ActionResult will use that bit
118 // for it's "invalid" flag.
120 struct IsResultPtrLowBitFree {
121 static const bool value = false;
124 /// ActionResult - This structure is used while parsing/acting on
125 /// expressions, stmts, etc. It encapsulates both the object returned by
126 /// the action, plus a sense of whether or not it is valid.
127 /// When CompressInvalid is true, the "invalid" flag will be
128 /// stored in the low bit of the Val pointer.
129 template<class PtrTy,
130 bool CompressInvalid = IsResultPtrLowBitFree<PtrTy>::value>
136 ActionResult(bool Invalid = false)
137 : Val(PtrTy()), Invalid(Invalid) {}
138 ActionResult(PtrTy val) : Val(val), Invalid(false) {}
139 ActionResult(const DiagnosticBuilder &) : Val(PtrTy()), Invalid(true) {}
141 // These two overloads prevent void* -> bool conversions.
142 ActionResult(const void *);
143 ActionResult(volatile void *);
145 bool isInvalid() const { return Invalid; }
146 bool isUsable() const { return !Invalid && Val; }
148 PtrTy get() const { return Val; }
149 // FIXME: Replace with get.
150 PtrTy release() const { return Val; }
151 PtrTy take() const { return Val; }
152 template <typename T> T *takeAs() { return static_cast<T*>(get()); }
154 void set(PtrTy V) { Val = V; }
156 const ActionResult &operator=(PtrTy RHS) {
163 // This ActionResult partial specialization places the "invalid"
164 // flag into the low bit of the pointer.
165 template<typename PtrTy>
166 class ActionResult<PtrTy, true> {
167 // A pointer whose low bit is 1 if this result is invalid, 0
169 uintptr_t PtrWithInvalid;
170 typedef llvm::PointerLikeTypeTraits<PtrTy> PtrTraits;
172 ActionResult(bool Invalid = false)
173 : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) { }
175 ActionResult(PtrTy V) {
176 void *VP = PtrTraits::getAsVoidPointer(V);
177 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
178 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
180 ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) { }
182 // These two overloads prevent void* -> bool conversions.
183 ActionResult(const void *);
184 ActionResult(volatile void *);
186 bool isInvalid() const { return PtrWithInvalid & 0x01; }
187 bool isUsable() const { return PtrWithInvalid > 0x01; }
190 void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
191 return PtrTraits::getFromVoidPointer(VP);
193 // FIXME: Replace with get.
194 PtrTy take() const { return get(); }
195 PtrTy release() const { return get(); }
196 template <typename T> T *takeAs() { return static_cast<T*>(get()); }
199 void *VP = PtrTraits::getAsVoidPointer(V);
200 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
201 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
204 const ActionResult &operator=(PtrTy RHS) {
205 void *VP = PtrTraits::getAsVoidPointer(RHS);
206 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
207 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
211 // For types where we can fit a flag in with the pointer, provide
212 // conversions to/from pointer type.
213 static ActionResult getFromOpaquePointer(void *P) {
215 Result.PtrWithInvalid = (uintptr_t)P;
218 void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; }
221 /// An opaque type for threading parsed type information through the
223 typedef OpaquePtr<QualType> ParsedType;
224 typedef UnionOpaquePtr<QualType> UnionParsedType;
226 // We can re-use the low bit of expression, statement, base, and
227 // member-initializer pointers for the "invalid" flag of
229 template<> struct IsResultPtrLowBitFree<Expr*> {
230 static const bool value = true;
232 template<> struct IsResultPtrLowBitFree<Stmt*> {
233 static const bool value = true;
235 template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> {
236 static const bool value = true;
238 template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> {
239 static const bool value = true;
242 typedef ActionResult<Expr*> ExprResult;
243 typedef ActionResult<Stmt*> StmtResult;
244 typedef ActionResult<ParsedType> TypeResult;
245 typedef ActionResult<CXXBaseSpecifier*> BaseResult;
246 typedef ActionResult<CXXCtorInitializer*> MemInitResult;
248 typedef ActionResult<Decl*> DeclResult;
249 typedef OpaquePtr<TemplateName> ParsedTemplateTy;
251 typedef llvm::MutableArrayRef<Expr*> MultiExprArg;
252 typedef llvm::MutableArrayRef<Stmt*> MultiStmtArg;
253 typedef llvm::MutableArrayRef<ParsedTemplateArgument> ASTTemplateArgsPtr;
254 typedef llvm::MutableArrayRef<ParsedType> MultiTypeArg;
255 typedef llvm::MutableArrayRef<TemplateParameterList*> MultiTemplateParamsArg;
257 inline ExprResult ExprError() { return ExprResult(true); }
258 inline StmtResult StmtError() { return StmtResult(true); }
260 inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
261 inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
263 inline ExprResult ExprEmpty() { return ExprResult(false); }
264 inline StmtResult StmtEmpty() { return StmtResult(false); }
266 inline Expr *AssertSuccess(ExprResult R) {
267 assert(!R.isInvalid() && "operation was asserted to never fail!");
271 inline Stmt *AssertSuccess(StmtResult R) {
272 assert(!R.isInvalid() && "operation was asserted to never fail!");