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