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 "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/PointerIntPair.h"
20 //===----------------------------------------------------------------------===//
22 //===----------------------------------------------------------------------===//
26 class CXXCtorInitializer;
27 class CXXBaseSpecifier;
31 class NestedNameSpecifier;
36 class TemplateParameterList;
38 /// OpaquePtr - This is a very simple POD type that wraps a pointer that the
39 /// Parser doesn't know about but that Sema or another client does. The UID
40 /// template argument is used to make sure that "Decl" pointers are not
41 /// compatible with "Type" pointers for example.
42 template <class PtrTy>
45 explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {}
47 typedef llvm::PointerLikeTypeTraits<PtrTy> Traits;
50 OpaquePtr() : Ptr(0) {}
52 static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; }
54 template <typename T> T* getAs() const {
58 template <typename T> T getAsVal() const {
63 return Traits::getFromVoidPointer(Ptr);
67 Ptr = Traits::getAsVoidPointer(P);
70 operator bool() const { return Ptr != 0; }
72 void *getAsOpaquePtr() const { return Ptr; }
73 static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); }
76 /// UnionOpaquePtr - A version of OpaquePtr suitable for membership
78 template <class T> struct UnionOpaquePtr {
81 static UnionOpaquePtr make(OpaquePtr<T> P) {
82 UnionOpaquePtr OP = { P.getAsOpaquePtr() };
86 OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); }
87 operator OpaquePtr<T>() const { return get(); }
89 UnionOpaquePtr &operator=(OpaquePtr<T> P) {
90 Ptr = P.getAsOpaquePtr();
98 class PointerLikeTypeTraits<clang::OpaquePtr<T> > {
100 static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) {
101 // FIXME: Doesn't work? return P.getAs< void >();
102 return P.getAsOpaquePtr();
104 static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) {
105 return clang::OpaquePtr<T>::getFromOpaquePtr(P);
107 enum { NumLowBitsAvailable = 0 };
111 struct isPodLike<clang::OpaquePtr<T> > { static const bool value = true; };
116 // -------------------------- About Move Emulation -------------------------- //
117 // The smart pointer classes in this file attempt to emulate move semantics
118 // as they appear in C++0x with rvalue references. Since C++03 doesn't have
119 // rvalue references, some tricks are needed to get similar results.
120 // Move semantics in C++0x have the following properties:
121 // 1) "Moving" means transferring the value of an object to another object,
122 // similar to copying, but without caring what happens to the old object.
123 // In particular, this means that the new object can steal the old object's
124 // resources instead of creating a copy.
125 // 2) Since moving can modify the source object, it must either be explicitly
126 // requested by the user, or the modifications must be unnoticeable.
127 // 3) As such, C++0x moving is only allowed in three contexts:
128 // * By explicitly using std::move() to request it.
129 // * From a temporary object, since that object cannot be accessed
130 // afterwards anyway, thus making the state unobservable.
131 // * On function return, since the object is not observable afterwards.
133 // To sum up: moving from a named object should only be possible with an
134 // explicit std::move(), or on function return. Moving from a temporary should
135 // be implicitly done. Moving from a const object is forbidden.
137 // The emulation is not perfect, and has the following shortcomings:
138 // * move() is not in namespace std.
139 // * move() is required on function return.
140 // * There are difficulties with implicit conversions.
141 // * Microsoft's compiler must be given the /Za switch to successfully compile.
143 // -------------------------- Implementation -------------------------------- //
144 // The move emulation relies on the peculiar reference binding semantics of
145 // C++03: as a rule, a non-const reference may not bind to a temporary object,
146 // except for the implicit object parameter in a member function call, which
147 // can refer to a temporary even when not being const.
148 // The moveable object has five important functions to facilitate moving:
149 // * A private, unimplemented constructor taking a non-const reference to its
150 // own class. This constructor serves a two-fold purpose.
151 // - It prevents the creation of a copy constructor that takes a const
152 // reference. Temporaries would be able to bind to the argument of such a
153 // constructor, and that would be bad.
154 // - Named objects will bind to the non-const reference, but since it's
155 // private, this will fail to compile. This prevents implicit moving from
157 // There's also a copy assignment operator for the same purpose.
158 // * An implicit, non-const conversion operator to a special mover type. This
159 // type represents the rvalue reference of C++0x. Being a non-const member,
160 // its implicit this parameter can bind to temporaries.
161 // * A constructor that takes an object of this mover type. This constructor
162 // performs the actual move operation. There is an equivalent assignment
164 // There is also a free move() function that takes a non-const reference to
165 // an object and returns a temporary. Internally, this function uses explicit
166 // constructor calls to move the value from the referenced object to the return
169 // There are now three possible scenarios of use.
170 // * Copying from a const object. Constructor overload resolution will find the
171 // non-const copy constructor, and the move constructor. The first is not
172 // viable because the const object cannot be bound to the non-const reference.
173 // The second fails because the conversion to the mover object is non-const.
174 // Moving from a const object fails as intended.
175 // * Copying from a named object. Constructor overload resolution will select
176 // the non-const copy constructor, but fail as intended, because this
177 // constructor is private.
178 // * Copying from a temporary. Constructor overload resolution cannot select
179 // the non-const copy constructor, because the temporary cannot be bound to
180 // the non-const reference. It thus selects the move constructor. The
181 // temporary can be bound to the implicit this parameter of the conversion
182 // operator, because of the special binding rule. Construction succeeds.
183 // Note that the Microsoft compiler, as an extension, allows binding
184 // temporaries against non-const references. The compiler thus selects the
185 // non-const copy constructor and fails, because the constructor is private.
186 // Passing /Za (disable extensions) disables this behaviour.
187 // The free move() function is used to move from a named object.
189 // Note that when passing an object of a different type (the classes below
190 // have OwningResult and OwningPtr, which should be mixable), you get a problem.
191 // Argument passing and function return use copy initialization rules. The
192 // effect of this is that, when the source object is not already of the target
193 // type, the compiler will first seek a way to convert the source object to the
194 // target type, and only then attempt to copy the resulting object. This means
195 // that when passing an OwningResult where an OwningPtr is expected, the
196 // compiler will first seek a conversion from OwningResult to OwningPtr, then
197 // copy the OwningPtr. The resulting conversion sequence is:
198 // OwningResult object -> ResultMover -> OwningResult argument to
199 // OwningPtr(OwningResult) -> OwningPtr -> PtrMover -> final OwningPtr
200 // This conversion sequence is too complex to be allowed. Thus the special
201 // move_* functions, which help the compiler out with some explicit
206 class DiagnosticBuilder;
208 // Determines whether the low bit of the result pointer for the
209 // given UID is always zero. If so, ActionResult will use that bit
210 // for it's "invalid" flag.
212 struct IsResultPtrLowBitFree {
213 static const bool value = false;
216 /// ActionResult - This structure is used while parsing/acting on
217 /// expressions, stmts, etc. It encapsulates both the object returned by
218 /// the action, plus a sense of whether or not it is valid.
219 /// When CompressInvalid is true, the "invalid" flag will be
220 /// stored in the low bit of the Val pointer.
221 template<class PtrTy,
222 bool CompressInvalid = IsResultPtrLowBitFree<PtrTy>::value>
228 ActionResult(bool Invalid = false)
229 : Val(PtrTy()), Invalid(Invalid) {}
230 ActionResult(PtrTy val) : Val(val), Invalid(false) {}
231 ActionResult(const DiagnosticBuilder &) : Val(PtrTy()), Invalid(true) {}
233 // These two overloads prevent void* -> bool conversions.
234 ActionResult(const void *);
235 ActionResult(volatile void *);
237 bool isInvalid() const { return Invalid; }
238 bool isUsable() const { return !Invalid && Val; }
240 PtrTy get() const { return Val; }
241 PtrTy release() const { return Val; }
242 PtrTy take() const { return Val; }
243 template <typename T> T *takeAs() { return static_cast<T*>(get()); }
245 void set(PtrTy V) { Val = V; }
247 const ActionResult &operator=(PtrTy RHS) {
254 // This ActionResult partial specialization places the "invalid"
255 // flag into the low bit of the pointer.
256 template<typename PtrTy>
257 class ActionResult<PtrTy, true> {
258 // A pointer whose low bit is 1 if this result is invalid, 0
260 uintptr_t PtrWithInvalid;
261 typedef llvm::PointerLikeTypeTraits<PtrTy> PtrTraits;
263 ActionResult(bool Invalid = false)
264 : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) { }
266 ActionResult(PtrTy V) {
267 void *VP = PtrTraits::getAsVoidPointer(V);
268 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
269 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
271 ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) { }
273 // These two overloads prevent void* -> bool conversions.
274 ActionResult(const void *);
275 ActionResult(volatile void *);
277 bool isInvalid() const { return PtrWithInvalid & 0x01; }
278 bool isUsable() const { return PtrWithInvalid > 0x01; }
281 void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01);
282 return PtrTraits::getFromVoidPointer(VP);
284 PtrTy take() const { return get(); }
285 PtrTy release() const { return get(); }
286 template <typename T> T *takeAs() { return static_cast<T*>(get()); }
289 void *VP = PtrTraits::getAsVoidPointer(V);
290 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
291 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
294 const ActionResult &operator=(PtrTy RHS) {
295 void *VP = PtrTraits::getAsVoidPointer(RHS);
296 PtrWithInvalid = reinterpret_cast<uintptr_t>(VP);
297 assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer");
302 /// ASTMultiPtr - A moveable smart pointer to multiple AST nodes. Only owns
303 /// the individual pointers, not the array holding them.
304 template <typename PtrTy> class ASTMultiPtr;
306 template <class PtrTy>
312 // Normal copying implicitly defined
313 ASTMultiPtr() : Nodes(0), Count(0) {}
314 explicit ASTMultiPtr(Sema &) : Nodes(0), Count(0) {}
315 ASTMultiPtr(Sema &, PtrTy *nodes, unsigned count)
316 : Nodes(nodes), Count(count) {}
317 // Fake mover in Parse/AstGuard.h needs this:
318 ASTMultiPtr(PtrTy *nodes, unsigned count) : Nodes(nodes), Count(count) {}
320 /// Access to the raw pointers.
321 PtrTy *get() const { return Nodes; }
323 /// Access to the count.
324 unsigned size() const { return Count; }
331 class ParsedTemplateArgument;
333 class ASTTemplateArgsPtr {
334 ParsedTemplateArgument *Args;
335 mutable unsigned Count;
338 ASTTemplateArgsPtr(Sema &actions, ParsedTemplateArgument *args,
340 Args(args), Count(count) { }
342 // FIXME: Lame, not-fully-type-safe emulation of 'move semantics'.
343 ASTTemplateArgsPtr(ASTTemplateArgsPtr &Other) :
344 Args(Other.Args), Count(Other.Count) {
347 // FIXME: Lame, not-fully-type-safe emulation of 'move semantics'.
348 ASTTemplateArgsPtr& operator=(ASTTemplateArgsPtr &Other) {
354 ParsedTemplateArgument *getArgs() const { return Args; }
355 unsigned size() const { return Count; }
357 void reset(ParsedTemplateArgument *args, unsigned count) {
362 const ParsedTemplateArgument &operator[](unsigned Arg) const;
364 ParsedTemplateArgument *release() const {
369 /// \brief A small vector that owns a set of AST nodes.
370 template <class PtrTy, unsigned N = 8>
371 class ASTOwningVector : public llvm::SmallVector<PtrTy, N> {
372 ASTOwningVector(ASTOwningVector &); // do not implement
373 ASTOwningVector &operator=(ASTOwningVector &); // do not implement
376 explicit ASTOwningVector(Sema &Actions)
380 return &this->front();
383 template<typename T> T **takeAs() { return reinterpret_cast<T**>(take()); }
386 /// An opaque type for threading parsed type information through the
388 typedef OpaquePtr<QualType> ParsedType;
389 typedef UnionOpaquePtr<QualType> UnionParsedType;
391 /// A SmallVector of statements, with stack size 32 (as that is the only one
393 typedef ASTOwningVector<Stmt*, 32> StmtVector;
394 /// A SmallVector of expressions, with stack size 12 (the maximum used.)
395 typedef ASTOwningVector<Expr*, 12> ExprVector;
396 /// A SmallVector of types.
397 typedef ASTOwningVector<ParsedType, 12> TypeVector;
399 template <class T, unsigned N> inline
400 ASTMultiPtr<T> move_arg(ASTOwningVector<T, N> &vec) {
401 return ASTMultiPtr<T>(vec.take(), vec.size());
404 // These versions are hopefully no-ops.
405 template <class T, bool C>
406 inline ActionResult<T,C> move(ActionResult<T,C> &ptr) {
410 template <class T> inline
411 ASTMultiPtr<T>& move(ASTMultiPtr<T> &ptr) {
415 // We can re-use the low bit of expression, statement, base, and
416 // member-initializer pointers for the "invalid" flag of
418 template<> struct IsResultPtrLowBitFree<Expr*> {
419 static const bool value = true;
421 template<> struct IsResultPtrLowBitFree<Stmt*> {
422 static const bool value = true;
424 template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> {
425 static const bool value = true;
427 template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> {
428 static const bool value = true;
431 typedef ActionResult<Expr*> ExprResult;
432 typedef ActionResult<Stmt*> StmtResult;
433 typedef ActionResult<ParsedType> TypeResult;
434 typedef ActionResult<CXXBaseSpecifier*> BaseResult;
435 typedef ActionResult<CXXCtorInitializer*> MemInitResult;
437 typedef ActionResult<Decl*> DeclResult;
438 typedef OpaquePtr<TemplateName> ParsedTemplateTy;
440 inline Expr *move(Expr *E) { return E; }
441 inline Stmt *move(Stmt *S) { return S; }
443 typedef ASTMultiPtr<Expr*> MultiExprArg;
444 typedef ASTMultiPtr<Stmt*> MultiStmtArg;
445 typedef ASTMultiPtr<ParsedType> MultiTypeArg;
446 typedef ASTMultiPtr<TemplateParameterList*> MultiTemplateParamsArg;
448 inline ExprResult ExprError() { return ExprResult(true); }
449 inline StmtResult StmtError() { return StmtResult(true); }
451 inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); }
452 inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); }
454 inline ExprResult ExprEmpty() { return ExprResult(false); }
455 inline StmtResult StmtEmpty() { return StmtResult(false); }
457 inline Expr *AssertSuccess(ExprResult R) {
458 assert(!R.isInvalid() && "operation was asserted to never fail!");
462 inline Stmt *AssertSuccess(StmtResult R) {
463 assert(!R.isInvalid() && "operation was asserted to never fail!");