Ownership.h revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
1//===--- Ownership.h - Parser ownership helpers -----------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains classes for managing ownership of Stmt and Expr nodes. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_SEMA_OWNERSHIP_H 15#define LLVM_CLANG_SEMA_OWNERSHIP_H 16 17#include "clang/Basic/LLVM.h" 18#include "llvm/ADT/ArrayRef.h" 19#include "llvm/ADT/PointerIntPair.h" 20 21//===----------------------------------------------------------------------===// 22// OpaquePtr 23//===----------------------------------------------------------------------===// 24 25namespace clang { 26 class CXXCtorInitializer; 27 class CXXBaseSpecifier; 28 class Decl; 29 class Expr; 30 class ParsedTemplateArgument; 31 class QualType; 32 class Stmt; 33 class TemplateName; 34 class TemplateParameterList; 35 36 /// \brief Wrapper for void* pointer. 37 /// \tparam PtrTy Either a pointer type like 'T*' or a type that behaves like 38 /// a pointer. 39 /// 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> 45 class OpaquePtr { 46 void *Ptr; 47 explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {} 48 49 typedef llvm::PointerLikeTypeTraits<PtrTy> Traits; 50 51 public: 52 OpaquePtr() : Ptr(nullptr) {} 53 54 static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; } 55 56 /// \brief Returns plain pointer to the entity pointed by this wrapper. 57 /// \tparam PointeeT Type of pointed entity. 58 /// 59 /// It is identical to getPtrAs<PointeeT*>. 60 template <typename PointeeT> PointeeT* getPtrTo() const { 61 return get(); 62 } 63 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. 67 /// 68 /// In contrast to getPtrTo, this method allows the return type to be 69 /// a smart pointer. 70 template <typename PtrT> PtrT getPtrAs() const { 71 return get(); 72 } 73 74 PtrTy get() const { 75 return Traits::getFromVoidPointer(Ptr); 76 } 77 78 void set(PtrTy P) { 79 Ptr = Traits::getAsVoidPointer(P); 80 } 81 82 LLVM_EXPLICIT operator bool() const { return Ptr != nullptr; } 83 84 void *getAsOpaquePtr() const { return Ptr; } 85 static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); } 86 }; 87 88 /// UnionOpaquePtr - A version of OpaquePtr suitable for membership 89 /// in a union. 90 template <class T> struct UnionOpaquePtr { 91 void *Ptr; 92 93 static UnionOpaquePtr make(OpaquePtr<T> P) { 94 UnionOpaquePtr OP = { P.getAsOpaquePtr() }; 95 return OP; 96 } 97 98 OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); } 99 operator OpaquePtr<T>() const { return get(); } 100 101 UnionOpaquePtr &operator=(OpaquePtr<T> P) { 102 Ptr = P.getAsOpaquePtr(); 103 return *this; 104 } 105 }; 106} 107 108namespace llvm { 109 template <class T> 110 class PointerLikeTypeTraits<clang::OpaquePtr<T> > { 111 public: 112 static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) { 113 // FIXME: Doesn't work? return P.getAs< void >(); 114 return P.getAsOpaquePtr(); 115 } 116 static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) { 117 return clang::OpaquePtr<T>::getFromOpaquePtr(P); 118 } 119 enum { NumLowBitsAvailable = 0 }; 120 }; 121 122 template <class T> 123 struct isPodLike<clang::OpaquePtr<T> > { static const bool value = true; }; 124} 125 126namespace clang { 127 // Basic 128 class DiagnosticBuilder; 129 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. 133 template<class Ptr> 134 struct IsResultPtrLowBitFree { 135 static const bool value = false; 136 }; 137 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> 145 class ActionResult { 146 PtrTy Val; 147 bool Invalid; 148 149 public: 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) {} 154 155 // These two overloads prevent void* -> bool conversions. 156 ActionResult(const void *); 157 ActionResult(volatile void *); 158 159 bool isInvalid() const { return Invalid; } 160 bool isUsable() const { return !Invalid && Val; } 161 bool isUnset() const { return !Invalid && !Val; } 162 163 PtrTy get() const { return Val; } 164 // FIXME: Replace with get. 165 PtrTy release() const { return Val; } 166 PtrTy take() const { return Val; } 167 template <typename T> T *takeAs() { return static_cast<T*>(get()); } 168 169 void set(PtrTy V) { Val = V; } 170 171 const ActionResult &operator=(PtrTy RHS) { 172 Val = RHS; 173 Invalid = false; 174 return *this; 175 } 176 }; 177 178 // This ActionResult partial specialization places the "invalid" 179 // flag into the low bit of the pointer. 180 template<typename PtrTy> 181 class ActionResult<PtrTy, true> { 182 // A pointer whose low bit is 1 if this result is invalid, 0 183 // otherwise. 184 uintptr_t PtrWithInvalid; 185 typedef llvm::PointerLikeTypeTraits<PtrTy> PtrTraits; 186 public: 187 ActionResult(bool Invalid = false) 188 : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) { } 189 190 ActionResult(PtrTy V) { 191 void *VP = PtrTraits::getAsVoidPointer(V); 192 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); 193 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer"); 194 } 195 ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) { } 196 197 // These two overloads prevent void* -> bool conversions. 198 ActionResult(const void *); 199 ActionResult(volatile void *); 200 201 bool isInvalid() const { return PtrWithInvalid & 0x01; } 202 bool isUsable() const { return PtrWithInvalid > 0x01; } 203 bool isUnset() const { return PtrWithInvalid == 0; } 204 205 PtrTy get() const { 206 void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01); 207 return PtrTraits::getFromVoidPointer(VP); 208 } 209 // FIXME: Replace with get. 210 PtrTy take() const { return get(); } 211 PtrTy release() const { return get(); } 212 template <typename T> T *takeAs() { return static_cast<T*>(get()); } 213 214 void set(PtrTy V) { 215 void *VP = PtrTraits::getAsVoidPointer(V); 216 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); 217 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer"); 218 } 219 220 const ActionResult &operator=(PtrTy RHS) { 221 void *VP = PtrTraits::getAsVoidPointer(RHS); 222 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); 223 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer"); 224 return *this; 225 } 226 227 // For types where we can fit a flag in with the pointer, provide 228 // conversions to/from pointer type. 229 static ActionResult getFromOpaquePointer(void *P) { 230 ActionResult Result; 231 Result.PtrWithInvalid = (uintptr_t)P; 232 return Result; 233 } 234 void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; } 235 }; 236 237 /// An opaque type for threading parsed type information through the 238 /// parser. 239 typedef OpaquePtr<QualType> ParsedType; 240 typedef UnionOpaquePtr<QualType> UnionParsedType; 241 242 // We can re-use the low bit of expression, statement, base, and 243 // member-initializer pointers for the "invalid" flag of 244 // ActionResult. 245 template<> struct IsResultPtrLowBitFree<Expr*> { 246 static const bool value = true; 247 }; 248 template<> struct IsResultPtrLowBitFree<Stmt*> { 249 static const bool value = true; 250 }; 251 template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> { 252 static const bool value = true; 253 }; 254 template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> { 255 static const bool value = true; 256 }; 257 258 typedef ActionResult<Expr*> ExprResult; 259 typedef ActionResult<Stmt*> StmtResult; 260 typedef ActionResult<ParsedType> TypeResult; 261 typedef ActionResult<CXXBaseSpecifier*> BaseResult; 262 typedef ActionResult<CXXCtorInitializer*> MemInitResult; 263 264 typedef ActionResult<Decl*> DeclResult; 265 typedef OpaquePtr<TemplateName> ParsedTemplateTy; 266 267 typedef llvm::MutableArrayRef<Expr*> MultiExprArg; 268 typedef llvm::MutableArrayRef<Stmt*> MultiStmtArg; 269 typedef llvm::MutableArrayRef<ParsedTemplateArgument> ASTTemplateArgsPtr; 270 typedef llvm::MutableArrayRef<ParsedType> MultiTypeArg; 271 typedef llvm::MutableArrayRef<TemplateParameterList*> MultiTemplateParamsArg; 272 273 inline ExprResult ExprError() { return ExprResult(true); } 274 inline StmtResult StmtError() { return StmtResult(true); } 275 276 inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); } 277 inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); } 278 279 inline ExprResult ExprEmpty() { return ExprResult(false); } 280 inline StmtResult StmtEmpty() { return StmtResult(false); } 281 282 inline Expr *AssertSuccess(ExprResult R) { 283 assert(!R.isInvalid() && "operation was asserted to never fail!"); 284 return R.get(); 285 } 286 287 inline Stmt *AssertSuccess(StmtResult R) { 288 assert(!R.isInvalid() && "operation was asserted to never fail!"); 289 return R.get(); 290 } 291} 292 293#endif 294