]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Sema/Ownership.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Sema / Ownership.h
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/AST/Expr.h"
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/Support/PointerLikeTypeTraits.h"
21 #include "llvm/Support/type_traits.h"
22 #include <cassert>
23 #include <cstddef>
24 #include <cstdint>
25
26 //===----------------------------------------------------------------------===//
27 // OpaquePtr
28 //===----------------------------------------------------------------------===//
29
30 namespace clang {
31
32 class CXXBaseSpecifier;
33 class CXXCtorInitializer;
34 class Decl;
35 class Expr;
36 class ParsedTemplateArgument;
37 class QualType;
38 class Stmt;
39 class TemplateName;
40 class TemplateParameterList;
41
42   /// Wrapper for void* pointer.
43   /// \tparam PtrTy Either a pointer type like 'T*' or a type that behaves like
44   ///               a pointer.
45   ///
46   /// This is a very simple POD type that wraps a pointer that the Parser
47   /// doesn't know about but that Sema or another client does.  The PtrTy
48   /// template argument is used to make sure that "Decl" pointers are not
49   /// compatible with "Type" pointers for example.
50   template <class PtrTy>
51   class OpaquePtr {
52     void *Ptr = nullptr;
53
54     explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {}
55
56     using Traits = llvm::PointerLikeTypeTraits<PtrTy>;
57
58   public:
59     OpaquePtr(std::nullptr_t = nullptr) {}
60
61     static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; }
62
63     /// Returns plain pointer to the entity pointed by this wrapper.
64     /// \tparam PointeeT Type of pointed entity.
65     ///
66     /// It is identical to getPtrAs<PointeeT*>.
67     template <typename PointeeT> PointeeT* getPtrTo() const {
68       return get();
69     }
70
71     /// Returns pointer converted to the specified type.
72     /// \tparam PtrT Result pointer type.  There must be implicit conversion
73     ///              from PtrTy to PtrT.
74     ///
75     /// In contrast to getPtrTo, this method allows the return type to be
76     /// a smart pointer.
77     template <typename PtrT> PtrT getPtrAs() const {
78       return get();
79     }
80
81     PtrTy get() const {
82       return Traits::getFromVoidPointer(Ptr);
83     }
84
85     void set(PtrTy P) {
86       Ptr = Traits::getAsVoidPointer(P);
87     }
88
89     explicit operator bool() const { return Ptr != nullptr; }
90
91     void *getAsOpaquePtr() const { return Ptr; }
92     static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); }
93   };
94
95   /// UnionOpaquePtr - A version of OpaquePtr suitable for membership
96   /// in a union.
97   template <class T> struct UnionOpaquePtr {
98     void *Ptr;
99
100     static UnionOpaquePtr make(OpaquePtr<T> P) {
101       UnionOpaquePtr OP = { P.getAsOpaquePtr() };
102       return OP;
103     }
104
105     OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); }
106     operator OpaquePtr<T>() const { return get(); }
107
108     UnionOpaquePtr &operator=(OpaquePtr<T> P) {
109       Ptr = P.getAsOpaquePtr();
110       return *this;
111     }
112   };
113
114 } // namespace clang
115
116 namespace llvm {
117
118   template <class T>
119   struct PointerLikeTypeTraits<clang::OpaquePtr<T>> {
120     enum { NumLowBitsAvailable = 0 };
121
122     static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) {
123       // FIXME: Doesn't work? return P.getAs< void >();
124       return P.getAsOpaquePtr();
125     }
126
127     static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
128       return clang::OpaquePtr<T>::getFromOpaquePtr(P);
129     }
130   };
131
132   template <class T>
133   struct isPodLike<clang::OpaquePtr<T>> { static const bool value = true; };
134
135 } // namespace llvm
136
137 namespace clang {
138
139   // Basic
140   class DiagnosticBuilder;
141
142   // Determines whether the low bit of the result pointer for the
143   // given UID is always zero. If so, ActionResult will use that bit
144   // for it's "invalid" flag.
145   template<class Ptr>
146   struct IsResultPtrLowBitFree {
147     static const bool value = false;
148   };
149
150   /// ActionResult - This structure is used while parsing/acting on
151   /// expressions, stmts, etc.  It encapsulates both the object returned by
152   /// the action, plus a sense of whether or not it is valid.
153   /// When CompressInvalid is true, the "invalid" flag will be
154   /// stored in the low bit of the Val pointer.
155   template<class PtrTy,
156            bool CompressInvalid = IsResultPtrLowBitFree<PtrTy>::value>
157   class ActionResult {
158     PtrTy Val;
159     bool Invalid;
160
161   public:
162     ActionResult(bool Invalid = false) : Val(PtrTy()), Invalid(Invalid) {}
163     ActionResult(PtrTy val) : Val(val), Invalid(false) {}
164     ActionResult(const DiagnosticBuilder &) : Val(PtrTy()), Invalid(true) {}
165
166     // These two overloads prevent void* -> bool conversions.
167     ActionResult(const void *) = delete;
168     ActionResult(volatile void *) = delete;
169
170     bool isInvalid() const { return Invalid; }
171     bool isUsable() const { return !Invalid && Val; }
172     bool isUnset() const { return !Invalid && !Val; }
173
174     PtrTy get() const { return Val; }
175     template <typename T> T *getAs() { return static_cast<T*>(get()); }
176
177     void set(PtrTy V) { Val = V; }
178
179     const ActionResult &operator=(PtrTy RHS) {
180       Val = RHS;
181       Invalid = false;
182       return *this;
183     }
184   };
185
186   // This ActionResult partial specialization places the "invalid"
187   // flag into the low bit of the pointer.
188   template<typename PtrTy>
189   class ActionResult<PtrTy, true> {
190     // A pointer whose low bit is 1 if this result is invalid, 0
191     // otherwise.
192     uintptr_t PtrWithInvalid;
193
194     using PtrTraits = llvm::PointerLikeTypeTraits<PtrTy>;
195
196   public:
197     ActionResult(bool Invalid = false)
198         : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) {}
199
200     ActionResult(PtrTy V) {
201       void *VP = PtrTraits::getAsVoidPointer(V);
202       PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
203       assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
204     }
205
206     ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) {}
207
208     // These two overloads prevent void* -> bool conversions.
209     ActionResult(const void *) = delete;
210     ActionResult(volatile void *) = delete;
211
212     bool isInvalid() const { return PtrWithInvalid & 0x01; }
213     bool isUsable() const { return PtrWithInvalid > 0x01; }
214     bool isUnset() const { return PtrWithInvalid == 0; }
215
216     PtrTy get() const {
217       void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
218       return PtrTraits::getFromVoidPointer(VP);
219     }
220
221     template <typename T> T *getAs() { return static_cast<T*>(get()); }
222
223     void set(PtrTy V) {
224       void *VP = PtrTraits::getAsVoidPointer(V);
225       PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
226       assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
227     }
228
229     const ActionResult &operator=(PtrTy RHS) {
230       void *VP = PtrTraits::getAsVoidPointer(RHS);
231       PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
232       assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
233       return *this;
234     }
235
236     // For types where we can fit a flag in with the pointer, provide
237     // conversions to/from pointer type.
238     static ActionResult getFromOpaquePointer(void *P) {
239       ActionResult Result;
240       Result.PtrWithInvalid = (uintptr_t)P;
241       return Result;
242     }
243     void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; }
244   };
245
246   /// An opaque type for threading parsed type information through the
247   /// parser.
248   using ParsedType = OpaquePtr<QualType>;
249   using UnionParsedType = UnionOpaquePtr<QualType>;
250
251   // We can re-use the low bit of expression, statement, base, and
252   // member-initializer pointers for the "invalid" flag of
253   // ActionResult.
254   template<> struct IsResultPtrLowBitFree<Expr*> {
255     static const bool value = true;
256   };
257   template<> struct IsResultPtrLowBitFree<Stmt*> {
258     static const bool value = true;
259   };
260   template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> {
261     static const bool value = true;
262   };
263   template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> {
264     static const bool value = true;
265   };
266
267   using ExprResult = ActionResult<Expr *>;
268   using StmtResult = ActionResult<Stmt *>;
269   using TypeResult = ActionResult<ParsedType>;
270   using BaseResult = ActionResult<CXXBaseSpecifier *>;
271   using MemInitResult = ActionResult<CXXCtorInitializer *>;
272
273   using DeclResult = ActionResult<Decl *>;
274   using ParsedTemplateTy = OpaquePtr<TemplateName>;
275   using UnionParsedTemplateTy = UnionOpaquePtr<TemplateName>;
276
277   using MultiExprArg = MutableArrayRef<Expr *>;
278   using MultiStmtArg = MutableArrayRef<Stmt *>;
279   using ASTTemplateArgsPtr = MutableArrayRef<ParsedTemplateArgument>;
280   using MultiTypeArg = MutableArrayRef<ParsedType>;
281   using MultiTemplateParamsArg = MutableArrayRef<TemplateParameterList *>;
282
283   inline ExprResult ExprError() { return ExprResult(true); }
284   inline StmtResult StmtError() { return StmtResult(true); }
285
286   inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
287   inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
288
289   inline ExprResult ExprEmpty() { return ExprResult(false); }
290   inline StmtResult StmtEmpty() { return StmtResult(false); }
291
292   inline Expr *AssertSuccess(ExprResult R) {
293     assert(!R.isInvalid() && "operation was asserted to never fail!");
294     return R.get();
295   }
296
297   inline Stmt *AssertSuccess(StmtResult R) {
298     assert(!R.isInvalid() && "operation was asserted to never fail!");
299     return R.get();
300   }
301
302 } // namespace clang
303
304 #endif // LLVM_CLANG_SEMA_OWNERSHIP_H