| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #ifndef LLVM_CLANG_SEMA_OWNERSHIP_H |
| 14 | #define LLVM_CLANG_SEMA_OWNERSHIP_H |
| 15 | |
| 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/Basic/LLVM.h" |
| 18 | #include "llvm/ADT/ArrayRef.h" |
| 19 | #include "llvm/Support/PointerLikeTypeTraits.h" |
| 20 | #include "llvm/Support/type_traits.h" |
| 21 | #include <cassert> |
| 22 | #include <cstddef> |
| 23 | #include <cstdint> |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | namespace clang { |
| 30 | |
| 31 | class CXXBaseSpecifier; |
| 32 | class CXXCtorInitializer; |
| 33 | class Decl; |
| 34 | class Expr; |
| 35 | class ParsedTemplateArgument; |
| 36 | class QualType; |
| 37 | class Stmt; |
| 38 | class TemplateName; |
| 39 | class TemplateParameterList; |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | template <class PtrTy> |
| 50 | class OpaquePtr { |
| 51 | void *Ptr = nullptr; |
| 52 | |
| 53 | explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {} |
| 54 | |
| 55 | using Traits = llvm::PointerLikeTypeTraits<PtrTy>; |
| 56 | |
| 57 | public: |
| 58 | OpaquePtr(std::nullptr_t = nullptr) {} |
| 59 | |
| 60 | static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; } |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | template <typename PointeeT> PointeeT* getPtrTo() const { |
| 67 | return get(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | template <typename PtrT> PtrT getPtrAs() const { |
| 77 | return get(); |
| 78 | } |
| 79 | |
| 80 | PtrTy get() const { |
| 81 | return Traits::getFromVoidPointer(Ptr); |
| 82 | } |
| 83 | |
| 84 | void set(PtrTy P) { |
| 85 | Ptr = Traits::getAsVoidPointer(P); |
| 86 | } |
| 87 | |
| 88 | explicit operator bool() const { return Ptr != nullptr; } |
| 89 | |
| 90 | void *getAsOpaquePtr() const { return Ptr; } |
| 91 | static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); } |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | |
| 96 | template <class T> struct UnionOpaquePtr { |
| 97 | void *Ptr; |
| 98 | |
| 99 | static UnionOpaquePtr make(OpaquePtr<T> P) { |
| 100 | UnionOpaquePtr OP = { P.getAsOpaquePtr() }; |
| 101 | return OP; |
| 102 | } |
| 103 | |
| 104 | OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); } |
| 105 | operator OpaquePtr<T>() const { return get(); } |
| 106 | |
| 107 | UnionOpaquePtr &operator=(OpaquePtr<T> P) { |
| 108 | Ptr = P.getAsOpaquePtr(); |
| 109 | return *this; |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | namespace llvm { |
| 116 | |
| 117 | template <class T> |
| 118 | struct PointerLikeTypeTraits<clang::OpaquePtr<T>> { |
| 119 | enum { NumLowBitsAvailable = 0 }; |
| 120 | |
| 121 | static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) { |
| 122 | |
| 123 | return P.getAsOpaquePtr(); |
| 124 | } |
| 125 | |
| 126 | static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) { |
| 127 | return clang::OpaquePtr<T>::getFromOpaquePtr(P); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | namespace clang { |
| 134 | |
| 135 | |
| 136 | class DiagnosticBuilder; |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | template<class Ptr> |
| 142 | struct IsResultPtrLowBitFree { |
| 143 | static const bool value = false; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | template<class PtrTy, |
| 152 | bool CompressInvalid = IsResultPtrLowBitFree<PtrTy>::value> |
| 153 | class ActionResult { |
| 154 | PtrTy Val; |
| 155 | bool Invalid; |
| 156 | |
| 157 | public: |
| 158 | ActionResult(bool Invalid = false) : Val(PtrTy()), Invalid(Invalid) {} |
| 159 | ActionResult(PtrTy val) : Val(val), Invalid(false) {} |
| 160 | ActionResult(const DiagnosticBuilder &) : Val(PtrTy()), Invalid(true) {} |
| 161 | |
| 162 | |
| 163 | ActionResult(const void *) = delete; |
| 164 | ActionResult(volatile void *) = delete; |
| 165 | |
| 166 | bool isInvalid() const { return Invalid; } |
| 167 | bool isUsable() const { return !Invalid && Val; } |
| 168 | bool isUnset() const { return !Invalid && !Val; } |
| 169 | |
| 170 | PtrTy get() const { return Val; } |
| 171 | template <typename T> T *getAs() { return static_cast<T*>(get()); } |
| 172 | |
| 173 | void set(PtrTy V) { Val = V; } |
| 174 | |
| 175 | const ActionResult &operator=(PtrTy RHS) { |
| 176 | Val = RHS; |
| 177 | Invalid = false; |
| 178 | return *this; |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | |
| 184 | template<typename PtrTy> |
| 185 | class ActionResult<PtrTy, true> { |
| 186 | |
| 187 | |
| 188 | uintptr_t PtrWithInvalid; |
| 189 | |
| 190 | using PtrTraits = llvm::PointerLikeTypeTraits<PtrTy>; |
| 191 | |
| 192 | public: |
| 193 | ActionResult(bool Invalid = false) |
| 194 | : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) {} |
| 195 | |
| 196 | ActionResult(PtrTy V) { |
| 197 | void *VP = PtrTraits::getAsVoidPointer(V); |
| 198 | PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); |
| 199 | (0) . __assert_fail ("(PtrWithInvalid & 0x01) == 0 && \"Badly aligned pointer\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Sema/Ownership.h", 199, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer"); |
| 200 | } |
| 201 | |
| 202 | ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) {} |
| 203 | |
| 204 | |
| 205 | ActionResult(const void *) = delete; |
| 206 | ActionResult(volatile void *) = delete; |
| 207 | |
| 208 | bool isInvalid() const { return PtrWithInvalid & 0x01; } |
| 209 | bool isUsable() const { return PtrWithInvalid > 0x01; } |
| 210 | bool isUnset() const { return PtrWithInvalid == 0; } |
| 211 | |
| 212 | PtrTy get() const { |
| 213 | void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01); |
| 214 | return PtrTraits::getFromVoidPointer(VP); |
| 215 | } |
| 216 | |
| 217 | template <typename T> T *getAs() { return static_cast<T*>(get()); } |
| 218 | |
| 219 | void set(PtrTy V) { |
| 220 | void *VP = PtrTraits::getAsVoidPointer(V); |
| 221 | PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); |
| 222 | (0) . __assert_fail ("(PtrWithInvalid & 0x01) == 0 && \"Badly aligned pointer\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Sema/Ownership.h", 222, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer"); |
| 223 | } |
| 224 | |
| 225 | const ActionResult &operator=(PtrTy RHS) { |
| 226 | void *VP = PtrTraits::getAsVoidPointer(RHS); |
| 227 | PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); |
| 228 | (0) . __assert_fail ("(PtrWithInvalid & 0x01) == 0 && \"Badly aligned pointer\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Sema/Ownership.h", 228, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer"); |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | |
| 234 | static ActionResult getFromOpaquePointer(void *P) { |
| 235 | ActionResult Result; |
| 236 | Result.PtrWithInvalid = (uintptr_t)P; |
| 237 | return Result; |
| 238 | } |
| 239 | void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; } |
| 240 | }; |
| 241 | |
| 242 | |
| 243 | |
| 244 | using ParsedType = OpaquePtr<QualType>; |
| 245 | using UnionParsedType = UnionOpaquePtr<QualType>; |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | template<> struct IsResultPtrLowBitFree<Expr*> { |
| 251 | static const bool value = true; |
| 252 | }; |
| 253 | template<> struct IsResultPtrLowBitFree<Stmt*> { |
| 254 | static const bool value = true; |
| 255 | }; |
| 256 | template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> { |
| 257 | static const bool value = true; |
| 258 | }; |
| 259 | template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> { |
| 260 | static const bool value = true; |
| 261 | }; |
| 262 | |
| 263 | using ExprResult = ActionResult<Expr *>; |
| 264 | using StmtResult = ActionResult<Stmt *>; |
| 265 | using TypeResult = ActionResult<ParsedType>; |
| 266 | using BaseResult = ActionResult<CXXBaseSpecifier *>; |
| 267 | using MemInitResult = ActionResult<CXXCtorInitializer *>; |
| 268 | |
| 269 | using DeclResult = ActionResult<Decl *>; |
| 270 | using ParsedTemplateTy = OpaquePtr<TemplateName>; |
| 271 | using UnionParsedTemplateTy = UnionOpaquePtr<TemplateName>; |
| 272 | |
| 273 | using MultiExprArg = MutableArrayRef<Expr *>; |
| 274 | using MultiStmtArg = MutableArrayRef<Stmt *>; |
| 275 | using ASTTemplateArgsPtr = MutableArrayRef<ParsedTemplateArgument>; |
| 276 | using MultiTypeArg = MutableArrayRef<ParsedType>; |
| 277 | using MultiTemplateParamsArg = MutableArrayRef<TemplateParameterList *>; |
| 278 | |
| 279 | inline ExprResult ExprError() { return ExprResult(true); } |
| 280 | inline StmtResult StmtError() { return StmtResult(true); } |
| 281 | |
| 282 | inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); } |
| 283 | inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); } |
| 284 | |
| 285 | inline ExprResult ExprEmpty() { return ExprResult(false); } |
| 286 | inline StmtResult StmtEmpty() { return StmtResult(false); } |
| 287 | |
| 288 | inline Expr *AssertSuccess(ExprResult R) { |
| 289 | (0) . __assert_fail ("!R.isInvalid() && \"operation was asserted to never fail!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Sema/Ownership.h", 289, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!R.isInvalid() && "operation was asserted to never fail!"); |
| 290 | return R.get(); |
| 291 | } |
| 292 | |
| 293 | inline Stmt *AssertSuccess(StmtResult R) { |
| 294 | (0) . __assert_fail ("!R.isInvalid() && \"operation was asserted to never fail!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Sema/Ownership.h", 294, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!R.isInvalid() && "operation was asserted to never fail!"); |
| 295 | return R.get(); |
| 296 | } |
| 297 | |
| 298 | } |
| 299 | |
| 300 | #endif |
| 301 | |