blob: 3293f7468d8253bc1f045457e6398083bb0fac54 [file] [log] [blame]
Chris Lattner57ad3782011-02-17 20:34:02 +00001//===------- TreeTransform.h - Semantic Tree Transformation -----*- C++ -*-===//
Douglas Gregor577f75a2009-08-04 16:50:30 +00002//
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.
Chris Lattner57ad3782011-02-17 20:34:02 +00007//===----------------------------------------------------------------------===//
Douglas Gregor577f75a2009-08-04 16:50:30 +00008//
9// This file implements a semantic tree transformation that takes a given
10// AST and rebuilds it, possibly transforming some nodes in the process.
11//
Chris Lattner57ad3782011-02-17 20:34:02 +000012//===----------------------------------------------------------------------===//
13
Douglas Gregor577f75a2009-08-04 16:50:30 +000014#ifndef LLVM_CLANG_SEMA_TREETRANSFORM_H
15#define LLVM_CLANG_SEMA_TREETRANSFORM_H
16
John McCall2d887082010-08-25 22:03:47 +000017#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000018#include "clang/Sema/Lookup.h"
Douglas Gregor8491ffe2010-12-20 22:05:00 +000019#include "clang/Sema/ParsedTemplate.h"
Douglas Gregordcee1a12009-08-06 05:28:30 +000020#include "clang/Sema/SemaDiagnostic.h"
John McCall781472f2010-08-25 08:40:02 +000021#include "clang/Sema/ScopeInfo.h"
Douglas Gregorc68afe22009-09-03 21:38:09 +000022#include "clang/AST/Decl.h"
John McCall7cd088e2010-08-24 07:21:54 +000023#include "clang/AST/DeclObjC.h"
Richard Smith3e4c6c42011-05-05 21:57:07 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregor657c1ac2009-08-06 22:17:10 +000025#include "clang/AST/Expr.h"
Douglas Gregorb98b1992009-08-11 05:31:07 +000026#include "clang/AST/ExprCXX.h"
27#include "clang/AST/ExprObjC.h"
Douglas Gregor43959a92009-08-20 07:17:43 +000028#include "clang/AST/Stmt.h"
29#include "clang/AST/StmtCXX.h"
30#include "clang/AST/StmtObjC.h"
John McCall19510852010-08-20 18:27:03 +000031#include "clang/Sema/Ownership.h"
32#include "clang/Sema/Designator.h"
Douglas Gregorb98b1992009-08-11 05:31:07 +000033#include "clang/Lex/Preprocessor.h"
David Blaikiea71f9d02011-09-22 02:34:54 +000034#include "llvm/ADT/ArrayRef.h"
John McCalla2becad2009-10-21 00:40:46 +000035#include "llvm/Support/ErrorHandling.h"
Douglas Gregor7e44e3f2010-12-02 00:05:49 +000036#include "TypeLocBuilder.h"
Douglas Gregor577f75a2009-08-04 16:50:30 +000037#include <algorithm>
38
39namespace clang {
John McCall781472f2010-08-25 08:40:02 +000040using namespace sema;
Mike Stump1eb44332009-09-09 15:08:12 +000041
Douglas Gregor577f75a2009-08-04 16:50:30 +000042/// \brief A semantic tree transformation that allows one to transform one
43/// abstract syntax tree into another.
44///
Mike Stump1eb44332009-09-09 15:08:12 +000045/// A new tree transformation is defined by creating a new subclass \c X of
46/// \c TreeTransform<X> and then overriding certain operations to provide
47/// behavior specific to that transformation. For example, template
Douglas Gregor577f75a2009-08-04 16:50:30 +000048/// instantiation is implemented as a tree transformation where the
49/// transformation of TemplateTypeParmType nodes involves substituting the
50/// template arguments for their corresponding template parameters; a similar
51/// transformation is performed for non-type template parameters and
52/// template template parameters.
53///
54/// This tree-transformation template uses static polymorphism to allow
Mike Stump1eb44332009-09-09 15:08:12 +000055/// subclasses to customize any of its operations. Thus, a subclass can
Douglas Gregor577f75a2009-08-04 16:50:30 +000056/// override any of the transformation or rebuild operators by providing an
57/// operation with the same signature as the default implementation. The
58/// overridding function should not be virtual.
59///
60/// Semantic tree transformations are split into two stages, either of which
61/// can be replaced by a subclass. The "transform" step transforms an AST node
62/// or the parts of an AST node using the various transformation functions,
63/// then passes the pieces on to the "rebuild" step, which constructs a new AST
64/// node of the appropriate kind from the pieces. The default transformation
65/// routines recursively transform the operands to composite AST nodes (e.g.,
66/// the pointee type of a PointerType node) and, if any of those operand nodes
67/// were changed by the transformation, invokes the rebuild operation to create
68/// a new AST node.
69///
Mike Stump1eb44332009-09-09 15:08:12 +000070/// Subclasses can customize the transformation at various levels. The
Douglas Gregor670444e2009-08-04 22:27:00 +000071/// most coarse-grained transformations involve replacing TransformType(),
Douglas Gregor9151c112011-03-02 18:50:38 +000072/// TransformExpr(), TransformDecl(), TransformNestedNameSpecifierLoc(),
Douglas Gregor577f75a2009-08-04 16:50:30 +000073/// TransformTemplateName(), or TransformTemplateArgument() with entirely
74/// new implementations.
75///
76/// For more fine-grained transformations, subclasses can replace any of the
77/// \c TransformXXX functions (where XXX is the name of an AST node, e.g.,
Douglas Gregor43959a92009-08-20 07:17:43 +000078/// PointerType, StmtExpr) to alter the transformation. As mentioned previously,
Douglas Gregor577f75a2009-08-04 16:50:30 +000079/// replacing TransformTemplateTypeParmType() allows template instantiation
Mike Stump1eb44332009-09-09 15:08:12 +000080/// to substitute template arguments for their corresponding template
Douglas Gregor577f75a2009-08-04 16:50:30 +000081/// parameters. Additionally, subclasses can override the \c RebuildXXX
82/// functions to control how AST nodes are rebuilt when their operands change.
83/// By default, \c TreeTransform will invoke semantic analysis to rebuild
84/// AST nodes. However, certain other tree transformations (e.g, cloning) may
85/// be able to use more efficient rebuild steps.
86///
87/// There are a handful of other functions that can be overridden, allowing one
Mike Stump1eb44332009-09-09 15:08:12 +000088/// to avoid traversing nodes that don't need any transformation
Douglas Gregor577f75a2009-08-04 16:50:30 +000089/// (\c AlreadyTransformed()), force rebuilding AST nodes even when their
90/// operands have not changed (\c AlwaysRebuild()), and customize the
91/// default locations and entity names used for type-checking
92/// (\c getBaseLocation(), \c getBaseEntity()).
Douglas Gregor577f75a2009-08-04 16:50:30 +000093template<typename Derived>
94class TreeTransform {
Douglas Gregord3731192011-01-10 07:32:04 +000095 /// \brief Private RAII object that helps us forget and then re-remember
96 /// the template argument corresponding to a partially-substituted parameter
97 /// pack.
98 class ForgetPartiallySubstitutedPackRAII {
99 Derived &Self;
100 TemplateArgument Old;
101
102 public:
103 ForgetPartiallySubstitutedPackRAII(Derived &Self) : Self(Self) {
104 Old = Self.ForgetPartiallySubstitutedPack();
105 }
106
107 ~ForgetPartiallySubstitutedPackRAII() {
108 Self.RememberPartiallySubstitutedPack(Old);
109 }
110 };
111
Douglas Gregor577f75a2009-08-04 16:50:30 +0000112protected:
113 Sema &SemaRef;
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000114
Douglas Gregordfca6f52012-02-13 22:00:16 +0000115 /// \brief The set of local declarations that have been transformed, for
116 /// cases where we are forced to build new declarations within the transformer
117 /// rather than in the subclass (e.g., lambda closure types).
118 llvm::DenseMap<Decl *, Decl *> TransformedLocalDecls;
119
Mike Stump1eb44332009-09-09 15:08:12 +0000120public:
Douglas Gregor577f75a2009-08-04 16:50:30 +0000121 /// \brief Initializes a new tree transformer.
Douglas Gregorb99268b2010-12-21 00:52:54 +0000122 TreeTransform(Sema &SemaRef) : SemaRef(SemaRef) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Douglas Gregor577f75a2009-08-04 16:50:30 +0000124 /// \brief Retrieves a reference to the derived class.
125 Derived &getDerived() { return static_cast<Derived&>(*this); }
126
127 /// \brief Retrieves a reference to the derived class.
Mike Stump1eb44332009-09-09 15:08:12 +0000128 const Derived &getDerived() const {
129 return static_cast<const Derived&>(*this);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000130 }
131
John McCall60d7b3a2010-08-24 06:29:42 +0000132 static inline ExprResult Owned(Expr *E) { return E; }
133 static inline StmtResult Owned(Stmt *S) { return S; }
John McCall9ae2f072010-08-23 23:25:46 +0000134
Douglas Gregor577f75a2009-08-04 16:50:30 +0000135 /// \brief Retrieves a reference to the semantic analysis object used for
136 /// this tree transform.
137 Sema &getSema() const { return SemaRef; }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor577f75a2009-08-04 16:50:30 +0000139 /// \brief Whether the transformation should always rebuild AST nodes, even
140 /// if none of the children have changed.
141 ///
142 /// Subclasses may override this function to specify when the transformation
143 /// should rebuild all AST nodes.
144 bool AlwaysRebuild() { return false; }
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor577f75a2009-08-04 16:50:30 +0000146 /// \brief Returns the location of the entity being transformed, if that
147 /// information was not available elsewhere in the AST.
148 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000149 /// By default, returns no source-location information. Subclasses can
Douglas Gregor577f75a2009-08-04 16:50:30 +0000150 /// provide an alternative implementation that provides better location
151 /// information.
152 SourceLocation getBaseLocation() { return SourceLocation(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Douglas Gregor577f75a2009-08-04 16:50:30 +0000154 /// \brief Returns the name of the entity being transformed, if that
155 /// information was not available elsewhere in the AST.
156 ///
157 /// By default, returns an empty name. Subclasses can provide an alternative
158 /// implementation with a more precise name.
159 DeclarationName getBaseEntity() { return DeclarationName(); }
160
Douglas Gregorb98b1992009-08-11 05:31:07 +0000161 /// \brief Sets the "base" location and entity when that
162 /// information is known based on another transformation.
163 ///
164 /// By default, the source location and entity are ignored. Subclasses can
165 /// override this function to provide a customized implementation.
166 void setBase(SourceLocation Loc, DeclarationName Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregorb98b1992009-08-11 05:31:07 +0000168 /// \brief RAII object that temporarily sets the base location and entity
169 /// used for reporting diagnostics in types.
170 class TemporaryBase {
171 TreeTransform &Self;
172 SourceLocation OldLocation;
173 DeclarationName OldEntity;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Douglas Gregorb98b1992009-08-11 05:31:07 +0000175 public:
176 TemporaryBase(TreeTransform &Self, SourceLocation Location,
Mike Stump1eb44332009-09-09 15:08:12 +0000177 DeclarationName Entity) : Self(Self) {
Douglas Gregorb98b1992009-08-11 05:31:07 +0000178 OldLocation = Self.getDerived().getBaseLocation();
179 OldEntity = Self.getDerived().getBaseEntity();
Douglas Gregorae201f72011-01-25 17:51:48 +0000180
181 if (Location.isValid())
182 Self.getDerived().setBase(Location, Entity);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregorb98b1992009-08-11 05:31:07 +0000185 ~TemporaryBase() {
186 Self.getDerived().setBase(OldLocation, OldEntity);
187 }
188 };
Mike Stump1eb44332009-09-09 15:08:12 +0000189
190 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-08-04 16:50:30 +0000191 /// transformed.
192 ///
193 /// Subclasses can provide an alternative implementation of this routine
Mike Stump1eb44332009-09-09 15:08:12 +0000194 /// to short-circuit evaluation when it is known that a given type will
Douglas Gregor577f75a2009-08-04 16:50:30 +0000195 /// not change. For example, template instantiation need not traverse
196 /// non-dependent types.
197 bool AlreadyTransformed(QualType T) {
198 return T.isNull();
199 }
200
Douglas Gregor6eef5192009-12-14 19:27:10 +0000201 /// \brief Determine whether the given call argument should be dropped, e.g.,
202 /// because it is a default argument.
203 ///
204 /// Subclasses can provide an alternative implementation of this routine to
205 /// determine which kinds of call arguments get dropped. By default,
206 /// CXXDefaultArgument nodes are dropped (prior to transformation).
207 bool DropCallArgument(Expr *E) {
208 return E->isDefaultArgument();
209 }
Sean Huntc3021132010-05-05 15:23:54 +0000210
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000211 /// \brief Determine whether we should expand a pack expansion with the
212 /// given set of parameter packs into separate arguments by repeatedly
213 /// transforming the pattern.
214 ///
Douglas Gregorb99268b2010-12-21 00:52:54 +0000215 /// By default, the transformer never tries to expand pack expansions.
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000216 /// Subclasses can override this routine to provide different behavior.
217 ///
218 /// \param EllipsisLoc The location of the ellipsis that identifies the
219 /// pack expansion.
220 ///
221 /// \param PatternRange The source range that covers the entire pattern of
222 /// the pack expansion.
223 ///
224 /// \param Unexpanded The set of unexpanded parameter packs within the
225 /// pattern.
226 ///
227 /// \param NumUnexpanded The number of unexpanded parameter packs in
228 /// \p Unexpanded.
229 ///
230 /// \param ShouldExpand Will be set to \c true if the transformer should
231 /// expand the corresponding pack expansions into separate arguments. When
232 /// set, \c NumExpansions must also be set.
233 ///
Douglas Gregord3731192011-01-10 07:32:04 +0000234 /// \param RetainExpansion Whether the caller should add an unexpanded
235 /// pack expansion after all of the expanded arguments. This is used
236 /// when extending explicitly-specified template argument packs per
237 /// C++0x [temp.arg.explicit]p9.
238 ///
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000239 /// \param NumExpansions The number of separate arguments that will be in
Douglas Gregorcded4f62011-01-14 17:04:44 +0000240 /// the expanded form of the corresponding pack expansion. This is both an
241 /// input and an output parameter, which can be set by the caller if the
242 /// number of expansions is known a priori (e.g., due to a prior substitution)
243 /// and will be set by the callee when the number of expansions is known.
244 /// The callee must set this value when \c ShouldExpand is \c true; it may
245 /// set this value in other cases.
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000246 ///
247 /// \returns true if an error occurred (e.g., because the parameter packs
248 /// are to be instantiated with arguments of different lengths), false
249 /// otherwise. If false, \c ShouldExpand (and possibly \c NumExpansions)
250 /// must be set.
251 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
252 SourceRange PatternRange,
David Blaikiea71f9d02011-09-22 02:34:54 +0000253 llvm::ArrayRef<UnexpandedParameterPack> Unexpanded,
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000254 bool &ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000255 bool &RetainExpansion,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000256 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000257 ShouldExpand = false;
258 return false;
259 }
260
Douglas Gregord3731192011-01-10 07:32:04 +0000261 /// \brief "Forget" about the partially-substituted pack template argument,
262 /// when performing an instantiation that must preserve the parameter pack
263 /// use.
264 ///
265 /// This routine is meant to be overridden by the template instantiator.
266 TemplateArgument ForgetPartiallySubstitutedPack() {
267 return TemplateArgument();
268 }
269
270 /// \brief "Remember" the partially-substituted pack template argument
271 /// after performing an instantiation that must preserve the parameter pack
272 /// use.
273 ///
274 /// This routine is meant to be overridden by the template instantiator.
275 void RememberPartiallySubstitutedPack(TemplateArgument Arg) { }
276
Douglas Gregor12c9c002011-01-07 16:43:16 +0000277 /// \brief Note to the derived class when a function parameter pack is
278 /// being expanded.
279 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { }
280
Douglas Gregor577f75a2009-08-04 16:50:30 +0000281 /// \brief Transforms the given type into another type.
282 ///
John McCalla2becad2009-10-21 00:40:46 +0000283 /// By default, this routine transforms a type by creating a
John McCalla93c9342009-12-07 02:54:59 +0000284 /// TypeSourceInfo for it and delegating to the appropriate
John McCalla2becad2009-10-21 00:40:46 +0000285 /// function. This is expensive, but we don't mind, because
286 /// this method is deprecated anyway; all users should be
John McCalla93c9342009-12-07 02:54:59 +0000287 /// switched to storing TypeSourceInfos.
Douglas Gregor577f75a2009-08-04 16:50:30 +0000288 ///
289 /// \returns the transformed type.
John McCall43fed0d2010-11-12 08:19:04 +0000290 QualType TransformType(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
John McCalla2becad2009-10-21 00:40:46 +0000292 /// \brief Transforms the given type-with-location into a new
293 /// type-with-location.
Douglas Gregor577f75a2009-08-04 16:50:30 +0000294 ///
John McCalla2becad2009-10-21 00:40:46 +0000295 /// By default, this routine transforms a type by delegating to the
296 /// appropriate TransformXXXType to build a new type. Subclasses
297 /// may override this function (to take over all type
298 /// transformations) or some set of the TransformXXXType functions
299 /// to alter the transformation.
John McCall43fed0d2010-11-12 08:19:04 +0000300 TypeSourceInfo *TransformType(TypeSourceInfo *DI);
John McCalla2becad2009-10-21 00:40:46 +0000301
302 /// \brief Transform the given type-with-location into a new
303 /// type, collecting location information in the given builder
304 /// as necessary.
305 ///
John McCall43fed0d2010-11-12 08:19:04 +0000306 QualType TransformType(TypeLocBuilder &TLB, TypeLoc TL);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregor657c1ac2009-08-06 22:17:10 +0000308 /// \brief Transform the given statement.
Douglas Gregor577f75a2009-08-04 16:50:30 +0000309 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000310 /// By default, this routine transforms a statement by delegating to the
Douglas Gregor43959a92009-08-20 07:17:43 +0000311 /// appropriate TransformXXXStmt function to transform a specific kind of
312 /// statement or the TransformExpr() function to transform an expression.
313 /// Subclasses may override this function to transform statements using some
314 /// other mechanism.
315 ///
316 /// \returns the transformed statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000317 StmtResult TransformStmt(Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Douglas Gregor657c1ac2009-08-06 22:17:10 +0000319 /// \brief Transform the given expression.
320 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +0000321 /// By default, this routine transforms an expression by delegating to the
322 /// appropriate TransformXXXExpr function to build a new expression.
323 /// Subclasses may override this function to transform expressions using some
324 /// other mechanism.
325 ///
326 /// \returns the transformed expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000327 ExprResult TransformExpr(Expr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Douglas Gregoraa165f82011-01-03 19:04:46 +0000329 /// \brief Transform the given list of expressions.
330 ///
331 /// This routine transforms a list of expressions by invoking
332 /// \c TransformExpr() for each subexpression. However, it also provides
333 /// support for variadic templates by expanding any pack expansions (if the
334 /// derived class permits such expansion) along the way. When pack expansions
335 /// are present, the number of outputs may not equal the number of inputs.
336 ///
337 /// \param Inputs The set of expressions to be transformed.
338 ///
339 /// \param NumInputs The number of expressions in \c Inputs.
340 ///
341 /// \param IsCall If \c true, then this transform is being performed on
342 /// function-call arguments, and any arguments that should be dropped, will
343 /// be.
344 ///
345 /// \param Outputs The transformed input expressions will be added to this
346 /// vector.
347 ///
348 /// \param ArgChanged If non-NULL, will be set \c true if any argument changed
349 /// due to transformation.
350 ///
351 /// \returns true if an error occurred, false otherwise.
352 bool TransformExprs(Expr **Inputs, unsigned NumInputs, bool IsCall,
Chris Lattner686775d2011-07-20 06:58:45 +0000353 SmallVectorImpl<Expr *> &Outputs,
Douglas Gregoraa165f82011-01-03 19:04:46 +0000354 bool *ArgChanged = 0);
355
Douglas Gregor577f75a2009-08-04 16:50:30 +0000356 /// \brief Transform the given declaration, which is referenced from a type
357 /// or expression.
358 ///
Douglas Gregordfca6f52012-02-13 22:00:16 +0000359 /// By default, acts as the identity function on declarations, unless the
360 /// transformer has had to transform the declaration itself. Subclasses
Douglas Gregordcee1a12009-08-06 05:28:30 +0000361 /// may override this function to provide alternate behavior.
Douglas Gregordfca6f52012-02-13 22:00:16 +0000362 Decl *TransformDecl(SourceLocation Loc, Decl *D) {
363 llvm::DenseMap<Decl *, Decl *>::iterator Known
364 = TransformedLocalDecls.find(D);
365 if (Known != TransformedLocalDecls.end())
366 return Known->second;
367
368 return D;
369 }
Douglas Gregor43959a92009-08-20 07:17:43 +0000370
Douglas Gregordfca6f52012-02-13 22:00:16 +0000371 /// \brief Transform the attributes associated with the given declaration and
372 /// place them on the new declaration.
373 ///
374 /// By default, this operation does nothing. Subclasses may override this
375 /// behavior to transform attributes.
376 void transformAttrs(Decl *Old, Decl *New) { }
377
378 /// \brief Note that a local declaration has been transformed by this
379 /// transformer.
380 ///
381 /// Local declarations are typically transformed via a call to
382 /// TransformDefinition. However, in some cases (e.g., lambda expressions),
383 /// the transformer itself has to transform the declarations. This routine
384 /// can be overridden by a subclass that keeps track of such mappings.
385 void transformedLocalDecl(Decl *Old, Decl *New) {
386 TransformedLocalDecls[Old] = New;
387 }
388
Douglas Gregor43959a92009-08-20 07:17:43 +0000389 /// \brief Transform the definition of the given declaration.
390 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000391 /// By default, invokes TransformDecl() to transform the declaration.
Douglas Gregor43959a92009-08-20 07:17:43 +0000392 /// Subclasses may override this function to provide alternate behavior.
Sean Huntc3021132010-05-05 15:23:54 +0000393 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
394 return getDerived().TransformDecl(Loc, D);
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Douglas Gregor6cd21982009-10-20 05:58:46 +0000397 /// \brief Transform the given declaration, which was the first part of a
398 /// nested-name-specifier in a member access expression.
399 ///
Sean Huntc3021132010-05-05 15:23:54 +0000400 /// This specific declaration transformation only applies to the first
Douglas Gregor6cd21982009-10-20 05:58:46 +0000401 /// identifier in a nested-name-specifier of a member access expression, e.g.,
402 /// the \c T in \c x->T::member
403 ///
404 /// By default, invokes TransformDecl() to transform the declaration.
405 /// Subclasses may override this function to provide alternate behavior.
Sean Huntc3021132010-05-05 15:23:54 +0000406 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc) {
407 return cast_or_null<NamedDecl>(getDerived().TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000408 }
Sean Huntc3021132010-05-05 15:23:54 +0000409
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000410 /// \brief Transform the given nested-name-specifier with source-location
411 /// information.
412 ///
413 /// By default, transforms all of the types and declarations within the
414 /// nested-name-specifier. Subclasses may override this function to provide
415 /// alternate behavior.
416 NestedNameSpecifierLoc TransformNestedNameSpecifierLoc(
417 NestedNameSpecifierLoc NNS,
418 QualType ObjectType = QualType(),
419 NamedDecl *FirstQualifierInScope = 0);
420
Douglas Gregor81499bb2009-09-03 22:13:48 +0000421 /// \brief Transform the given declaration name.
422 ///
423 /// By default, transforms the types of conversion function, constructor,
424 /// and destructor names and then (if needed) rebuilds the declaration name.
425 /// Identifiers and selectors are returned unmodified. Sublcasses may
426 /// override this function to provide alternate behavior.
Abramo Bagnara25777432010-08-11 22:01:17 +0000427 DeclarationNameInfo
John McCall43fed0d2010-11-12 08:19:04 +0000428 TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregor577f75a2009-08-04 16:50:30 +0000430 /// \brief Transform the given template name.
Mike Stump1eb44332009-09-09 15:08:12 +0000431 ///
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000432 /// \param SS The nested-name-specifier that qualifies the template
433 /// name. This nested-name-specifier must already have been transformed.
434 ///
435 /// \param Name The template name to transform.
436 ///
437 /// \param NameLoc The source location of the template name.
438 ///
439 /// \param ObjectType If we're translating a template name within a member
440 /// access expression, this is the type of the object whose member template
441 /// is being referenced.
442 ///
443 /// \param FirstQualifierInScope If the first part of a nested-name-specifier
444 /// also refers to a name within the current (lexical) scope, this is the
445 /// declaration it refers to.
446 ///
447 /// By default, transforms the template name by transforming the declarations
448 /// and nested-name-specifiers that occur within the template name.
449 /// Subclasses may override this function to provide alternate behavior.
450 TemplateName TransformTemplateName(CXXScopeSpec &SS,
451 TemplateName Name,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000452 SourceLocation NameLoc,
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000453 QualType ObjectType = QualType(),
454 NamedDecl *FirstQualifierInScope = 0);
455
Douglas Gregor577f75a2009-08-04 16:50:30 +0000456 /// \brief Transform the given template argument.
457 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000458 /// By default, this operation transforms the type, expression, or
459 /// declaration stored within the template argument and constructs a
Douglas Gregor670444e2009-08-04 22:27:00 +0000460 /// new template argument from the transformed result. Subclasses may
461 /// override this function to provide alternate behavior.
John McCall833ca992009-10-29 08:12:44 +0000462 ///
463 /// Returns true if there was an error.
464 bool TransformTemplateArgument(const TemplateArgumentLoc &Input,
465 TemplateArgumentLoc &Output);
466
Douglas Gregorfcc12532010-12-20 17:31:10 +0000467 /// \brief Transform the given set of template arguments.
468 ///
469 /// By default, this operation transforms all of the template arguments
470 /// in the input set using \c TransformTemplateArgument(), and appends
471 /// the transformed arguments to the output list.
472 ///
Douglas Gregor7ca7ac42010-12-20 23:36:19 +0000473 /// Note that this overload of \c TransformTemplateArguments() is merely
474 /// a convenience function. Subclasses that wish to override this behavior
475 /// should override the iterator-based member template version.
476 ///
Douglas Gregorfcc12532010-12-20 17:31:10 +0000477 /// \param Inputs The set of template arguments to be transformed.
478 ///
479 /// \param NumInputs The number of template arguments in \p Inputs.
480 ///
481 /// \param Outputs The set of transformed template arguments output by this
482 /// routine.
483 ///
484 /// Returns true if an error occurred.
485 bool TransformTemplateArguments(const TemplateArgumentLoc *Inputs,
486 unsigned NumInputs,
Douglas Gregor7ca7ac42010-12-20 23:36:19 +0000487 TemplateArgumentListInfo &Outputs) {
488 return TransformTemplateArguments(Inputs, Inputs + NumInputs, Outputs);
489 }
Douglas Gregor7f61f2f2010-12-20 17:42:22 +0000490
491 /// \brief Transform the given set of template arguments.
492 ///
493 /// By default, this operation transforms all of the template arguments
494 /// in the input set using \c TransformTemplateArgument(), and appends
495 /// the transformed arguments to the output list.
496 ///
Douglas Gregor7ca7ac42010-12-20 23:36:19 +0000497 /// \param First An iterator to the first template argument.
498 ///
499 /// \param Last An iterator one step past the last template argument.
Douglas Gregor7f61f2f2010-12-20 17:42:22 +0000500 ///
501 /// \param Outputs The set of transformed template arguments output by this
502 /// routine.
503 ///
504 /// Returns true if an error occurred.
Douglas Gregor7ca7ac42010-12-20 23:36:19 +0000505 template<typename InputIterator>
506 bool TransformTemplateArguments(InputIterator First,
507 InputIterator Last,
508 TemplateArgumentListInfo &Outputs);
Douglas Gregor7f61f2f2010-12-20 17:42:22 +0000509
John McCall833ca992009-10-29 08:12:44 +0000510 /// \brief Fakes up a TemplateArgumentLoc for a given TemplateArgument.
511 void InventTemplateArgumentLoc(const TemplateArgument &Arg,
512 TemplateArgumentLoc &ArgLoc);
513
John McCalla93c9342009-12-07 02:54:59 +0000514 /// \brief Fakes up a TypeSourceInfo for a type.
515 TypeSourceInfo *InventTypeSourceInfo(QualType T) {
516 return SemaRef.Context.getTrivialTypeSourceInfo(T,
John McCall833ca992009-10-29 08:12:44 +0000517 getDerived().getBaseLocation());
518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
John McCalla2becad2009-10-21 00:40:46 +0000520#define ABSTRACT_TYPELOC(CLASS, PARENT)
521#define TYPELOC(CLASS, PARENT) \
John McCall43fed0d2010-11-12 08:19:04 +0000522 QualType Transform##CLASS##Type(TypeLocBuilder &TLB, CLASS##TypeLoc T);
John McCalla2becad2009-10-21 00:40:46 +0000523#include "clang/AST/TypeLocNodes.def"
Douglas Gregor577f75a2009-08-04 16:50:30 +0000524
John Wiegley28bbe4b2011-04-28 01:08:34 +0000525 StmtResult
526 TransformSEHHandler(Stmt *Handler);
527
John McCall43fed0d2010-11-12 08:19:04 +0000528 QualType
529 TransformTemplateSpecializationType(TypeLocBuilder &TLB,
530 TemplateSpecializationTypeLoc TL,
531 TemplateName Template);
532
533 QualType
534 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
535 DependentTemplateSpecializationTypeLoc TL,
Douglas Gregor087eb5a2011-03-04 18:53:13 +0000536 TemplateName Template,
537 CXXScopeSpec &SS);
Douglas Gregora88f09f2011-02-28 17:23:35 +0000538
539 QualType
540 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000541 DependentTemplateSpecializationTypeLoc TL,
542 NestedNameSpecifierLoc QualifierLoc);
543
John McCall21ef0fa2010-03-11 09:03:00 +0000544 /// \brief Transforms the parameters of a function type into the
545 /// given vectors.
546 ///
547 /// The result vectors should be kept in sync; null entries in the
548 /// variables vector are acceptable.
549 ///
550 /// Return true on error.
Douglas Gregora009b592011-01-07 00:20:55 +0000551 bool TransformFunctionTypeParams(SourceLocation Loc,
552 ParmVarDecl **Params, unsigned NumParams,
553 const QualType *ParamTypes,
Chris Lattner686775d2011-07-20 06:58:45 +0000554 SmallVectorImpl<QualType> &PTypes,
555 SmallVectorImpl<ParmVarDecl*> *PVars);
John McCall21ef0fa2010-03-11 09:03:00 +0000556
557 /// \brief Transforms a single function-type parameter. Return null
558 /// on error.
John McCallfb44de92011-05-01 22:35:37 +0000559 ///
560 /// \param indexAdjustment - A number to add to the parameter's
561 /// scope index; can be negative
Douglas Gregor6a24bfd2011-01-14 22:40:04 +0000562 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCallfb44de92011-05-01 22:35:37 +0000563 int indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +0000564 llvm::Optional<unsigned> NumExpansions,
565 bool ExpectParameterPack);
John McCall21ef0fa2010-03-11 09:03:00 +0000566
John McCall43fed0d2010-11-12 08:19:04 +0000567 QualType TransformReferenceType(TypeLocBuilder &TLB, ReferenceTypeLoc TL);
John McCall833ca992009-10-29 08:12:44 +0000568
John McCall60d7b3a2010-08-24 06:29:42 +0000569 StmtResult TransformCompoundStmt(CompoundStmt *S, bool IsStmtExpr);
570 ExprResult TransformCXXNamedCastExpr(CXXNamedCastExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Douglas Gregor43959a92009-08-20 07:17:43 +0000572#define STMT(Node, Parent) \
John McCall60d7b3a2010-08-24 06:29:42 +0000573 StmtResult Transform##Node(Node *S);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000574#define EXPR(Node, Parent) \
John McCall60d7b3a2010-08-24 06:29:42 +0000575 ExprResult Transform##Node(Node *E);
Sean Hunt7381d5c2010-05-18 06:22:21 +0000576#define ABSTRACT_STMT(Stmt)
Sean Hunt4bfe1962010-05-05 15:24:00 +0000577#include "clang/AST/StmtNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Douglas Gregor577f75a2009-08-04 16:50:30 +0000579 /// \brief Build a new pointer type given its pointee type.
580 ///
581 /// By default, performs semantic analysis when building the pointer type.
582 /// Subclasses may override this routine to provide different behavior.
John McCall85737a72009-10-30 00:06:24 +0000583 QualType RebuildPointerType(QualType PointeeType, SourceLocation Sigil);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000584
585 /// \brief Build a new block pointer type given its pointee type.
586 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000587 /// By default, performs semantic analysis when building the block pointer
Douglas Gregor577f75a2009-08-04 16:50:30 +0000588 /// type. Subclasses may override this routine to provide different behavior.
John McCall85737a72009-10-30 00:06:24 +0000589 QualType RebuildBlockPointerType(QualType PointeeType, SourceLocation Sigil);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000590
John McCall85737a72009-10-30 00:06:24 +0000591 /// \brief Build a new reference type given the type it references.
Douglas Gregor577f75a2009-08-04 16:50:30 +0000592 ///
John McCall85737a72009-10-30 00:06:24 +0000593 /// By default, performs semantic analysis when building the
594 /// reference type. Subclasses may override this routine to provide
595 /// different behavior.
Douglas Gregor577f75a2009-08-04 16:50:30 +0000596 ///
John McCall85737a72009-10-30 00:06:24 +0000597 /// \param LValue whether the type was written with an lvalue sigil
598 /// or an rvalue sigil.
599 QualType RebuildReferenceType(QualType ReferentType,
600 bool LValue,
601 SourceLocation Sigil);
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor577f75a2009-08-04 16:50:30 +0000603 /// \brief Build a new member pointer type given the pointee type and the
604 /// class type it refers into.
605 ///
606 /// By default, performs semantic analysis when building the member pointer
607 /// type. Subclasses may override this routine to provide different behavior.
John McCall85737a72009-10-30 00:06:24 +0000608 QualType RebuildMemberPointerType(QualType PointeeType, QualType ClassType,
609 SourceLocation Sigil);
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Douglas Gregor577f75a2009-08-04 16:50:30 +0000611 /// \brief Build a new array type given the element type, size
612 /// modifier, size of the array (if known), size expression, and index type
613 /// qualifiers.
614 ///
615 /// By default, performs semantic analysis when building the array type.
616 /// Subclasses may override this routine to provide different behavior.
Mike Stump1eb44332009-09-09 15:08:12 +0000617 /// Also by default, all of the other Rebuild*Array
Douglas Gregor577f75a2009-08-04 16:50:30 +0000618 QualType RebuildArrayType(QualType ElementType,
619 ArrayType::ArraySizeModifier SizeMod,
620 const llvm::APInt *Size,
621 Expr *SizeExpr,
622 unsigned IndexTypeQuals,
623 SourceRange BracketsRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Douglas Gregor577f75a2009-08-04 16:50:30 +0000625 /// \brief Build a new constant array type given the element type, size
626 /// modifier, (known) size of the array, and index type qualifiers.
627 ///
628 /// By default, performs semantic analysis when building the array type.
629 /// Subclasses may override this routine to provide different behavior.
Mike Stump1eb44332009-09-09 15:08:12 +0000630 QualType RebuildConstantArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000631 ArrayType::ArraySizeModifier SizeMod,
632 const llvm::APInt &Size,
John McCall85737a72009-10-30 00:06:24 +0000633 unsigned IndexTypeQuals,
634 SourceRange BracketsRange);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000635
Douglas Gregor577f75a2009-08-04 16:50:30 +0000636 /// \brief Build a new incomplete array type given the element type, size
637 /// modifier, and index type qualifiers.
638 ///
639 /// By default, performs semantic analysis when building the array type.
640 /// Subclasses may override this routine to provide different behavior.
Mike Stump1eb44332009-09-09 15:08:12 +0000641 QualType RebuildIncompleteArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000642 ArrayType::ArraySizeModifier SizeMod,
John McCall85737a72009-10-30 00:06:24 +0000643 unsigned IndexTypeQuals,
644 SourceRange BracketsRange);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000645
Mike Stump1eb44332009-09-09 15:08:12 +0000646 /// \brief Build a new variable-length array type given the element type,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000647 /// size modifier, size expression, and index type qualifiers.
648 ///
649 /// By default, performs semantic analysis when building the array type.
650 /// Subclasses may override this routine to provide different behavior.
Mike Stump1eb44332009-09-09 15:08:12 +0000651 QualType RebuildVariableArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000652 ArrayType::ArraySizeModifier SizeMod,
John McCall9ae2f072010-08-23 23:25:46 +0000653 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000654 unsigned IndexTypeQuals,
655 SourceRange BracketsRange);
656
Mike Stump1eb44332009-09-09 15:08:12 +0000657 /// \brief Build a new dependent-sized array type given the element type,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000658 /// size modifier, size expression, and index type qualifiers.
659 ///
660 /// By default, performs semantic analysis when building the array type.
661 /// Subclasses may override this routine to provide different behavior.
Mike Stump1eb44332009-09-09 15:08:12 +0000662 QualType RebuildDependentSizedArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000663 ArrayType::ArraySizeModifier SizeMod,
John McCall9ae2f072010-08-23 23:25:46 +0000664 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000665 unsigned IndexTypeQuals,
666 SourceRange BracketsRange);
667
668 /// \brief Build a new vector type given the element type and
669 /// number of elements.
670 ///
671 /// By default, performs semantic analysis when building the vector type.
672 /// Subclasses may override this routine to provide different behavior.
John Thompson82287d12010-02-05 00:12:22 +0000673 QualType RebuildVectorType(QualType ElementType, unsigned NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +0000674 VectorType::VectorKind VecKind);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregor577f75a2009-08-04 16:50:30 +0000676 /// \brief Build a new extended vector type given the element type and
677 /// number of elements.
678 ///
679 /// By default, performs semantic analysis when building the vector type.
680 /// Subclasses may override this routine to provide different behavior.
681 QualType RebuildExtVectorType(QualType ElementType, unsigned NumElements,
682 SourceLocation AttributeLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000683
684 /// \brief Build a new potentially dependently-sized extended vector type
Douglas Gregor577f75a2009-08-04 16:50:30 +0000685 /// given the element type and number of elements.
686 ///
687 /// By default, performs semantic analysis when building the vector type.
688 /// Subclasses may override this routine to provide different behavior.
Mike Stump1eb44332009-09-09 15:08:12 +0000689 QualType RebuildDependentSizedExtVectorType(QualType ElementType,
John McCall9ae2f072010-08-23 23:25:46 +0000690 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000691 SourceLocation AttributeLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregor577f75a2009-08-04 16:50:30 +0000693 /// \brief Build a new function type.
694 ///
695 /// By default, performs semantic analysis when building the function type.
696 /// Subclasses may override this routine to provide different behavior.
697 QualType RebuildFunctionProtoType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000698 QualType *ParamTypes,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000699 unsigned NumParamTypes,
Richard Smitheefb3d52012-02-10 09:58:53 +0000700 bool Variadic, bool HasTrailingReturn,
701 unsigned Quals,
Douglas Gregorc938c162011-01-26 05:01:58 +0000702 RefQualifierKind RefQualifier,
Eli Friedmanfa869542010-08-05 02:54:05 +0000703 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
John McCalla2becad2009-10-21 00:40:46 +0000705 /// \brief Build a new unprototyped function type.
706 QualType RebuildFunctionNoProtoType(QualType ResultType);
707
John McCalled976492009-12-04 22:46:56 +0000708 /// \brief Rebuild an unresolved typename type, given the decl that
709 /// the UnresolvedUsingTypenameDecl was transformed to.
710 QualType RebuildUnresolvedUsingType(Decl *D);
711
Douglas Gregor577f75a2009-08-04 16:50:30 +0000712 /// \brief Build a new typedef type.
Richard Smith162e1c12011-04-15 14:24:37 +0000713 QualType RebuildTypedefType(TypedefNameDecl *Typedef) {
Douglas Gregor577f75a2009-08-04 16:50:30 +0000714 return SemaRef.Context.getTypeDeclType(Typedef);
715 }
716
717 /// \brief Build a new class/struct/union type.
718 QualType RebuildRecordType(RecordDecl *Record) {
719 return SemaRef.Context.getTypeDeclType(Record);
720 }
721
722 /// \brief Build a new Enum type.
723 QualType RebuildEnumType(EnumDecl *Enum) {
724 return SemaRef.Context.getTypeDeclType(Enum);
725 }
John McCall7da24312009-09-05 00:15:47 +0000726
Mike Stump1eb44332009-09-09 15:08:12 +0000727 /// \brief Build a new typeof(expr) type.
Douglas Gregor577f75a2009-08-04 16:50:30 +0000728 ///
729 /// By default, performs semantic analysis when building the typeof type.
730 /// Subclasses may override this routine to provide different behavior.
John McCall2a984ca2010-10-12 00:20:44 +0000731 QualType RebuildTypeOfExprType(Expr *Underlying, SourceLocation Loc);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000732
Mike Stump1eb44332009-09-09 15:08:12 +0000733 /// \brief Build a new typeof(type) type.
Douglas Gregor577f75a2009-08-04 16:50:30 +0000734 ///
735 /// By default, builds a new TypeOfType with the given underlying type.
736 QualType RebuildTypeOfType(QualType Underlying);
737
Sean Huntca63c202011-05-24 22:41:36 +0000738 /// \brief Build a new unary transform type.
739 QualType RebuildUnaryTransformType(QualType BaseType,
740 UnaryTransformType::UTTKind UKind,
741 SourceLocation Loc);
742
Mike Stump1eb44332009-09-09 15:08:12 +0000743 /// \brief Build a new C++0x decltype type.
Douglas Gregor577f75a2009-08-04 16:50:30 +0000744 ///
745 /// By default, performs semantic analysis when building the decltype type.
746 /// Subclasses may override this routine to provide different behavior.
John McCall2a984ca2010-10-12 00:20:44 +0000747 QualType RebuildDecltypeType(Expr *Underlying, SourceLocation Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Richard Smith34b41d92011-02-20 03:19:35 +0000749 /// \brief Build a new C++0x auto type.
750 ///
751 /// By default, builds a new AutoType with the given deduced type.
752 QualType RebuildAutoType(QualType Deduced) {
753 return SemaRef.Context.getAutoType(Deduced);
754 }
755
Douglas Gregor577f75a2009-08-04 16:50:30 +0000756 /// \brief Build a new template specialization type.
757 ///
758 /// By default, performs semantic analysis when building the template
759 /// specialization type. Subclasses may override this routine to provide
760 /// different behavior.
761 QualType RebuildTemplateSpecializationType(TemplateName Template,
John McCall833ca992009-10-29 08:12:44 +0000762 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +0000763 TemplateArgumentListInfo &Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000765 /// \brief Build a new parenthesized type.
766 ///
767 /// By default, builds a new ParenType type from the inner type.
768 /// Subclasses may override this routine to provide different behavior.
769 QualType RebuildParenType(QualType InnerType) {
770 return SemaRef.Context.getParenType(InnerType);
771 }
772
Douglas Gregor577f75a2009-08-04 16:50:30 +0000773 /// \brief Build a new qualified name type.
774 ///
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000775 /// By default, builds a new ElaboratedType type from the keyword,
776 /// the nested-name-specifier and the named type.
777 /// Subclasses may override this routine to provide different behavior.
John McCall21e413f2010-11-04 19:04:38 +0000778 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
779 ElaboratedTypeKeyword Keyword,
Douglas Gregor9e876872011-03-01 18:12:44 +0000780 NestedNameSpecifierLoc QualifierLoc,
781 QualType Named) {
782 return SemaRef.Context.getElaboratedType(Keyword,
783 QualifierLoc.getNestedNameSpecifier(),
784 Named);
Mike Stump1eb44332009-09-09 15:08:12 +0000785 }
Douglas Gregor577f75a2009-08-04 16:50:30 +0000786
787 /// \brief Build a new typename type that refers to a template-id.
788 ///
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000789 /// By default, builds a new DependentNameType type from the
790 /// nested-name-specifier and the given type. Subclasses may override
791 /// this routine to provide different behavior.
John McCall33500952010-06-11 00:33:02 +0000792 QualType RebuildDependentTemplateSpecializationType(
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000793 ElaboratedTypeKeyword Keyword,
794 NestedNameSpecifierLoc QualifierLoc,
795 const IdentifierInfo *Name,
796 SourceLocation NameLoc,
Douglas Gregor67714232011-03-03 02:41:12 +0000797 TemplateArgumentListInfo &Args) {
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000798 // Rebuild the template name.
799 // TODO: avoid TemplateName abstraction
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000800 CXXScopeSpec SS;
801 SS.Adopt(QualifierLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000802 TemplateName InstName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000803 = getDerived().RebuildTemplateName(SS, *Name, NameLoc, QualType(), 0);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000804
805 if (InstName.isNull())
806 return QualType();
807
808 // If it's still dependent, make a dependent specialization.
809 if (InstName.getAsDependentTemplateName())
810 return SemaRef.Context.getDependentTemplateSpecializationType(Keyword,
811 QualifierLoc.getNestedNameSpecifier(),
812 Name,
813 Args);
814
815 // Otherwise, make an elaborated type wrapping a non-dependent
816 // specialization.
817 QualType T =
818 getDerived().RebuildTemplateSpecializationType(InstName, NameLoc, Args);
819 if (T.isNull()) return QualType();
820
821 if (Keyword == ETK_None && QualifierLoc.getNestedNameSpecifier() == 0)
822 return T;
823
824 return SemaRef.Context.getElaboratedType(Keyword,
825 QualifierLoc.getNestedNameSpecifier(),
826 T);
827 }
828
Douglas Gregor577f75a2009-08-04 16:50:30 +0000829 /// \brief Build a new typename type that refers to an identifier.
830 ///
831 /// By default, performs semantic analysis when building the typename type
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000832 /// (or elaborated type). Subclasses may override this routine to provide
Douglas Gregor577f75a2009-08-04 16:50:30 +0000833 /// different behavior.
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000834 QualType RebuildDependentNameType(ElaboratedTypeKeyword Keyword,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000835 SourceLocation KeywordLoc,
Douglas Gregor2494dd02011-03-01 01:34:45 +0000836 NestedNameSpecifierLoc QualifierLoc,
837 const IdentifierInfo *Id,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000838 SourceLocation IdLoc) {
Douglas Gregor40336422010-03-31 22:19:08 +0000839 CXXScopeSpec SS;
Douglas Gregor2494dd02011-03-01 01:34:45 +0000840 SS.Adopt(QualifierLoc);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000841
Douglas Gregor2494dd02011-03-01 01:34:45 +0000842 if (QualifierLoc.getNestedNameSpecifier()->isDependent()) {
Douglas Gregor40336422010-03-31 22:19:08 +0000843 // If the name is still dependent, just build a new dependent name type.
844 if (!SemaRef.computeDeclContext(SS))
Douglas Gregor2494dd02011-03-01 01:34:45 +0000845 return SemaRef.Context.getDependentNameType(Keyword,
846 QualifierLoc.getNestedNameSpecifier(),
847 Id);
Douglas Gregor40336422010-03-31 22:19:08 +0000848 }
849
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000850 if (Keyword == ETK_None || Keyword == ETK_Typename)
Douglas Gregor2494dd02011-03-01 01:34:45 +0000851 return SemaRef.CheckTypenameType(Keyword, KeywordLoc, QualifierLoc,
Douglas Gregore29425b2011-02-28 22:42:13 +0000852 *Id, IdLoc);
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000853
854 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
855
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000856 // We had a dependent elaborated-type-specifier that has been transformed
Douglas Gregor40336422010-03-31 22:19:08 +0000857 // into a non-dependent elaborated-type-specifier. Find the tag we're
858 // referring to.
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000859 LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
Douglas Gregor40336422010-03-31 22:19:08 +0000860 DeclContext *DC = SemaRef.computeDeclContext(SS, false);
861 if (!DC)
862 return QualType();
863
John McCall56138762010-05-27 06:40:31 +0000864 if (SemaRef.RequireCompleteDeclContext(SS, DC))
865 return QualType();
866
Douglas Gregor40336422010-03-31 22:19:08 +0000867 TagDecl *Tag = 0;
868 SemaRef.LookupQualifiedName(Result, DC);
869 switch (Result.getResultKind()) {
870 case LookupResult::NotFound:
871 case LookupResult::NotFoundInCurrentInstantiation:
872 break;
Sean Huntc3021132010-05-05 15:23:54 +0000873
Douglas Gregor40336422010-03-31 22:19:08 +0000874 case LookupResult::Found:
875 Tag = Result.getAsSingle<TagDecl>();
876 break;
Sean Huntc3021132010-05-05 15:23:54 +0000877
Douglas Gregor40336422010-03-31 22:19:08 +0000878 case LookupResult::FoundOverloaded:
879 case LookupResult::FoundUnresolvedValue:
880 llvm_unreachable("Tag lookup cannot find non-tags");
881 return QualType();
Sean Huntc3021132010-05-05 15:23:54 +0000882
Douglas Gregor40336422010-03-31 22:19:08 +0000883 case LookupResult::Ambiguous:
884 // Let the LookupResult structure handle ambiguities.
885 return QualType();
886 }
887
888 if (!Tag) {
Nick Lewycky446e4022011-01-24 19:01:04 +0000889 // Check where the name exists but isn't a tag type and use that to emit
890 // better diagnostics.
891 LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
892 SemaRef.LookupQualifiedName(Result, DC);
893 switch (Result.getResultKind()) {
894 case LookupResult::Found:
895 case LookupResult::FoundOverloaded:
896 case LookupResult::FoundUnresolvedValue: {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000897 NamedDecl *SomeDecl = Result.getRepresentativeDecl();
Nick Lewycky446e4022011-01-24 19:01:04 +0000898 unsigned Kind = 0;
899 if (isa<TypedefDecl>(SomeDecl)) Kind = 1;
Richard Smith162e1c12011-04-15 14:24:37 +0000900 else if (isa<TypeAliasDecl>(SomeDecl)) Kind = 2;
901 else if (isa<ClassTemplateDecl>(SomeDecl)) Kind = 3;
Nick Lewycky446e4022011-01-24 19:01:04 +0000902 SemaRef.Diag(IdLoc, diag::err_tag_reference_non_tag) << Kind;
903 SemaRef.Diag(SomeDecl->getLocation(), diag::note_declared_at);
904 break;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000905 }
Nick Lewycky446e4022011-01-24 19:01:04 +0000906 default:
907 // FIXME: Would be nice to highlight just the source range.
908 SemaRef.Diag(IdLoc, diag::err_not_tag_in_scope)
909 << Kind << Id << DC;
910 break;
911 }
Douglas Gregor40336422010-03-31 22:19:08 +0000912 return QualType();
913 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000914
Richard Trieubbf34c02011-06-10 03:11:26 +0000915 if (!SemaRef.isAcceptableTagRedeclaration(Tag, Kind, /*isDefinition*/false,
916 IdLoc, *Id)) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000917 SemaRef.Diag(KeywordLoc, diag::err_use_with_wrong_tag) << Id;
Douglas Gregor40336422010-03-31 22:19:08 +0000918 SemaRef.Diag(Tag->getLocation(), diag::note_previous_use);
919 return QualType();
920 }
921
922 // Build the elaborated-type-specifier type.
923 QualType T = SemaRef.Context.getTypeDeclType(Tag);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000924 return SemaRef.Context.getElaboratedType(Keyword,
925 QualifierLoc.getNestedNameSpecifier(),
926 T);
Douglas Gregordcee1a12009-08-06 05:28:30 +0000927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000929 /// \brief Build a new pack expansion type.
930 ///
931 /// By default, builds a new PackExpansionType type from the given pattern.
932 /// Subclasses may override this routine to provide different behavior.
933 QualType RebuildPackExpansionType(QualType Pattern,
934 SourceRange PatternRange,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000935 SourceLocation EllipsisLoc,
936 llvm::Optional<unsigned> NumExpansions) {
937 return getSema().CheckPackExpansion(Pattern, PatternRange, EllipsisLoc,
938 NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000939 }
940
Eli Friedmanb001de72011-10-06 23:00:33 +0000941 /// \brief Build a new atomic type given its value type.
942 ///
943 /// By default, performs semantic analysis when building the atomic type.
944 /// Subclasses may override this routine to provide different behavior.
945 QualType RebuildAtomicType(QualType ValueType, SourceLocation KWLoc);
946
Douglas Gregord1067e52009-08-06 06:41:21 +0000947 /// \brief Build a new template name given a nested name specifier, a flag
948 /// indicating whether the "template" keyword was provided, and the template
949 /// that the template name refers to.
950 ///
951 /// By default, builds the new template name directly. Subclasses may override
952 /// this routine to provide different behavior.
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000953 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
Douglas Gregord1067e52009-08-06 06:41:21 +0000954 bool TemplateKW,
955 TemplateDecl *Template);
956
Douglas Gregord1067e52009-08-06 06:41:21 +0000957 /// \brief Build a new template name given a nested name specifier and the
958 /// name that is referred to as a template.
959 ///
960 /// By default, performs semantic analysis to determine whether the name can
961 /// be resolved to a specific template, then builds the appropriate kind of
962 /// template name. Subclasses may override this routine to provide different
963 /// behavior.
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000964 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
965 const IdentifierInfo &Name,
966 SourceLocation NameLoc,
John McCall43fed0d2010-11-12 08:19:04 +0000967 QualType ObjectType,
968 NamedDecl *FirstQualifierInScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000970 /// \brief Build a new template name given a nested name specifier and the
971 /// overloaded operator name that is referred to as a template.
972 ///
973 /// By default, performs semantic analysis to determine whether the name can
974 /// be resolved to a specific template, then builds the appropriate kind of
975 /// template name. Subclasses may override this routine to provide different
976 /// behavior.
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000977 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000978 OverloadedOperatorKind Operator,
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000979 SourceLocation NameLoc,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000980 QualType ObjectType);
Douglas Gregor1aee05d2011-01-15 06:45:20 +0000981
982 /// \brief Build a new template name given a template template parameter pack
983 /// and the
984 ///
985 /// By default, performs semantic analysis to determine whether the name can
986 /// be resolved to a specific template, then builds the appropriate kind of
987 /// template name. Subclasses may override this routine to provide different
988 /// behavior.
989 TemplateName RebuildTemplateName(TemplateTemplateParmDecl *Param,
990 const TemplateArgument &ArgPack) {
991 return getSema().Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
992 }
993
Douglas Gregor43959a92009-08-20 07:17:43 +0000994 /// \brief Build a new compound statement.
995 ///
996 /// By default, performs semantic analysis to build the new statement.
997 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +0000998 StmtResult RebuildCompoundStmt(SourceLocation LBraceLoc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000999 MultiStmtArg Statements,
1000 SourceLocation RBraceLoc,
1001 bool IsStmtExpr) {
John McCall9ae2f072010-08-23 23:25:46 +00001002 return getSema().ActOnCompoundStmt(LBraceLoc, RBraceLoc, Statements,
Douglas Gregor43959a92009-08-20 07:17:43 +00001003 IsStmtExpr);
1004 }
1005
1006 /// \brief Build a new case statement.
1007 ///
1008 /// By default, performs semantic analysis to build the new statement.
1009 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001010 StmtResult RebuildCaseStmt(SourceLocation CaseLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001011 Expr *LHS,
Douglas Gregor43959a92009-08-20 07:17:43 +00001012 SourceLocation EllipsisLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001013 Expr *RHS,
Douglas Gregor43959a92009-08-20 07:17:43 +00001014 SourceLocation ColonLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001015 return getSema().ActOnCaseStmt(CaseLoc, LHS, EllipsisLoc, RHS,
Douglas Gregor43959a92009-08-20 07:17:43 +00001016 ColonLoc);
1017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Douglas Gregor43959a92009-08-20 07:17:43 +00001019 /// \brief Attach the body to a new case statement.
1020 ///
1021 /// By default, performs semantic analysis to build the new statement.
1022 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001023 StmtResult RebuildCaseStmtBody(Stmt *S, Stmt *Body) {
John McCall9ae2f072010-08-23 23:25:46 +00001024 getSema().ActOnCaseStmtBody(S, Body);
1025 return S;
Douglas Gregor43959a92009-08-20 07:17:43 +00001026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Douglas Gregor43959a92009-08-20 07:17:43 +00001028 /// \brief Build a new default statement.
1029 ///
1030 /// By default, performs semantic analysis to build the new statement.
1031 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001032 StmtResult RebuildDefaultStmt(SourceLocation DefaultLoc,
Douglas Gregor43959a92009-08-20 07:17:43 +00001033 SourceLocation ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001034 Stmt *SubStmt) {
1035 return getSema().ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt,
Douglas Gregor43959a92009-08-20 07:17:43 +00001036 /*CurScope=*/0);
1037 }
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Douglas Gregor43959a92009-08-20 07:17:43 +00001039 /// \brief Build a new label statement.
1040 ///
1041 /// By default, performs semantic analysis to build the new statement.
1042 /// Subclasses may override this routine to provide different behavior.
Chris Lattner57ad3782011-02-17 20:34:02 +00001043 StmtResult RebuildLabelStmt(SourceLocation IdentLoc, LabelDecl *L,
1044 SourceLocation ColonLoc, Stmt *SubStmt) {
1045 return SemaRef.ActOnLabelStmt(IdentLoc, L, ColonLoc, SubStmt);
Douglas Gregor43959a92009-08-20 07:17:43 +00001046 }
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Douglas Gregor43959a92009-08-20 07:17:43 +00001048 /// \brief Build a new "if" statement.
1049 ///
1050 /// By default, performs semantic analysis to build the new statement.
1051 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001052 StmtResult RebuildIfStmt(SourceLocation IfLoc, Sema::FullExprArg Cond,
Chris Lattner57ad3782011-02-17 20:34:02 +00001053 VarDecl *CondVar, Stmt *Then,
1054 SourceLocation ElseLoc, Stmt *Else) {
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001055 return getSema().ActOnIfStmt(IfLoc, Cond, CondVar, Then, ElseLoc, Else);
Douglas Gregor43959a92009-08-20 07:17:43 +00001056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Douglas Gregor43959a92009-08-20 07:17:43 +00001058 /// \brief Start building a new switch statement.
1059 ///
1060 /// By default, performs semantic analysis to build the new statement.
1061 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001062 StmtResult RebuildSwitchStmtStart(SourceLocation SwitchLoc,
Chris Lattner57ad3782011-02-17 20:34:02 +00001063 Expr *Cond, VarDecl *CondVar) {
John McCall9ae2f072010-08-23 23:25:46 +00001064 return getSema().ActOnStartOfSwitchStmt(SwitchLoc, Cond,
John McCalld226f652010-08-21 09:40:31 +00001065 CondVar);
Douglas Gregor43959a92009-08-20 07:17:43 +00001066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Douglas Gregor43959a92009-08-20 07:17:43 +00001068 /// \brief Attach the body to the switch statement.
1069 ///
1070 /// By default, performs semantic analysis to build the new statement.
1071 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001072 StmtResult RebuildSwitchStmtBody(SourceLocation SwitchLoc,
Chris Lattner57ad3782011-02-17 20:34:02 +00001073 Stmt *Switch, Stmt *Body) {
John McCall9ae2f072010-08-23 23:25:46 +00001074 return getSema().ActOnFinishSwitchStmt(SwitchLoc, Switch, Body);
Douglas Gregor43959a92009-08-20 07:17:43 +00001075 }
1076
1077 /// \brief Build a new while statement.
1078 ///
1079 /// By default, performs semantic analysis to build the new statement.
1080 /// Subclasses may override this routine to provide different behavior.
Chris Lattner57ad3782011-02-17 20:34:02 +00001081 StmtResult RebuildWhileStmt(SourceLocation WhileLoc, Sema::FullExprArg Cond,
1082 VarDecl *CondVar, Stmt *Body) {
John McCall9ae2f072010-08-23 23:25:46 +00001083 return getSema().ActOnWhileStmt(WhileLoc, Cond, CondVar, Body);
Douglas Gregor43959a92009-08-20 07:17:43 +00001084 }
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Douglas Gregor43959a92009-08-20 07:17:43 +00001086 /// \brief Build a new do-while statement.
1087 ///
1088 /// By default, performs semantic analysis to build the new statement.
1089 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001090 StmtResult RebuildDoStmt(SourceLocation DoLoc, Stmt *Body,
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001091 SourceLocation WhileLoc, SourceLocation LParenLoc,
1092 Expr *Cond, SourceLocation RParenLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001093 return getSema().ActOnDoStmt(DoLoc, Body, WhileLoc, LParenLoc,
1094 Cond, RParenLoc);
Douglas Gregor43959a92009-08-20 07:17:43 +00001095 }
1096
1097 /// \brief Build a new for statement.
1098 ///
1099 /// By default, performs semantic analysis to build the new statement.
1100 /// Subclasses may override this routine to provide different behavior.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001101 StmtResult RebuildForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
1102 Stmt *Init, Sema::FullExprArg Cond,
1103 VarDecl *CondVar, Sema::FullExprArg Inc,
1104 SourceLocation RParenLoc, Stmt *Body) {
John McCall9ae2f072010-08-23 23:25:46 +00001105 return getSema().ActOnForStmt(ForLoc, LParenLoc, Init, Cond,
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001106 CondVar, Inc, RParenLoc, Body);
Douglas Gregor43959a92009-08-20 07:17:43 +00001107 }
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Douglas Gregor43959a92009-08-20 07:17:43 +00001109 /// \brief Build a new goto statement.
1110 ///
1111 /// By default, performs semantic analysis to build the new statement.
1112 /// Subclasses may override this routine to provide different behavior.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001113 StmtResult RebuildGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
1114 LabelDecl *Label) {
Chris Lattner57ad3782011-02-17 20:34:02 +00001115 return getSema().ActOnGotoStmt(GotoLoc, LabelLoc, Label);
Douglas Gregor43959a92009-08-20 07:17:43 +00001116 }
1117
1118 /// \brief Build a new indirect goto statement.
1119 ///
1120 /// By default, performs semantic analysis to build the new statement.
1121 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001122 StmtResult RebuildIndirectGotoStmt(SourceLocation GotoLoc,
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001123 SourceLocation StarLoc,
1124 Expr *Target) {
John McCall9ae2f072010-08-23 23:25:46 +00001125 return getSema().ActOnIndirectGotoStmt(GotoLoc, StarLoc, Target);
Douglas Gregor43959a92009-08-20 07:17:43 +00001126 }
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Douglas Gregor43959a92009-08-20 07:17:43 +00001128 /// \brief Build a new return statement.
1129 ///
1130 /// By default, performs semantic analysis to build the new statement.
1131 /// Subclasses may override this routine to provide different behavior.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001132 StmtResult RebuildReturnStmt(SourceLocation ReturnLoc, Expr *Result) {
John McCall9ae2f072010-08-23 23:25:46 +00001133 return getSema().ActOnReturnStmt(ReturnLoc, Result);
Douglas Gregor43959a92009-08-20 07:17:43 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Douglas Gregor43959a92009-08-20 07:17:43 +00001136 /// \brief Build a new declaration statement.
1137 ///
1138 /// By default, performs semantic analysis to build the new statement.
1139 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001140 StmtResult RebuildDeclStmt(Decl **Decls, unsigned NumDecls,
Mike Stump1eb44332009-09-09 15:08:12 +00001141 SourceLocation StartLoc,
Douglas Gregor43959a92009-08-20 07:17:43 +00001142 SourceLocation EndLoc) {
Richard Smith406c38e2011-02-23 00:37:57 +00001143 Sema::DeclGroupPtrTy DG = getSema().BuildDeclaratorGroup(Decls, NumDecls);
1144 return getSema().ActOnDeclStmt(DG, StartLoc, EndLoc);
Douglas Gregor43959a92009-08-20 07:17:43 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Anders Carlsson703e3942010-01-24 05:50:09 +00001147 /// \brief Build a new inline asm statement.
1148 ///
1149 /// By default, performs semantic analysis to build the new statement.
1150 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001151 StmtResult RebuildAsmStmt(SourceLocation AsmLoc,
Anders Carlsson703e3942010-01-24 05:50:09 +00001152 bool IsSimple,
1153 bool IsVolatile,
1154 unsigned NumOutputs,
1155 unsigned NumInputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001156 IdentifierInfo **Names,
Anders Carlsson703e3942010-01-24 05:50:09 +00001157 MultiExprArg Constraints,
1158 MultiExprArg Exprs,
John McCall9ae2f072010-08-23 23:25:46 +00001159 Expr *AsmString,
Anders Carlsson703e3942010-01-24 05:50:09 +00001160 MultiExprArg Clobbers,
1161 SourceLocation RParenLoc,
1162 bool MSAsm) {
Sean Huntc3021132010-05-05 15:23:54 +00001163 return getSema().ActOnAsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs,
Anders Carlsson703e3942010-01-24 05:50:09 +00001164 NumInputs, Names, move(Constraints),
John McCall9ae2f072010-08-23 23:25:46 +00001165 Exprs, AsmString, Clobbers,
Anders Carlsson703e3942010-01-24 05:50:09 +00001166 RParenLoc, MSAsm);
1167 }
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00001168
1169 /// \brief Build a new Objective-C @try statement.
1170 ///
1171 /// By default, performs semantic analysis to build the new statement.
1172 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001173 StmtResult RebuildObjCAtTryStmt(SourceLocation AtLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001174 Stmt *TryBody,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001175 MultiStmtArg CatchStmts,
John McCall9ae2f072010-08-23 23:25:46 +00001176 Stmt *Finally) {
1177 return getSema().ActOnObjCAtTryStmt(AtLoc, TryBody, move(CatchStmts),
1178 Finally);
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00001179 }
1180
Douglas Gregorbe270a02010-04-26 17:57:08 +00001181 /// \brief Rebuild an Objective-C exception declaration.
1182 ///
1183 /// By default, performs semantic analysis to build the new declaration.
1184 /// Subclasses may override this routine to provide different behavior.
1185 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
1186 TypeSourceInfo *TInfo, QualType T) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001187 return getSema().BuildObjCExceptionDecl(TInfo, T,
1188 ExceptionDecl->getInnerLocStart(),
1189 ExceptionDecl->getLocation(),
1190 ExceptionDecl->getIdentifier());
Douglas Gregorbe270a02010-04-26 17:57:08 +00001191 }
Sean Huntc3021132010-05-05 15:23:54 +00001192
Douglas Gregorbe270a02010-04-26 17:57:08 +00001193 /// \brief Build a new Objective-C @catch statement.
1194 ///
1195 /// By default, performs semantic analysis to build the new statement.
1196 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001197 StmtResult RebuildObjCAtCatchStmt(SourceLocation AtLoc,
Douglas Gregorbe270a02010-04-26 17:57:08 +00001198 SourceLocation RParenLoc,
1199 VarDecl *Var,
John McCall9ae2f072010-08-23 23:25:46 +00001200 Stmt *Body) {
Douglas Gregorbe270a02010-04-26 17:57:08 +00001201 return getSema().ActOnObjCAtCatchStmt(AtLoc, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001202 Var, Body);
Douglas Gregorbe270a02010-04-26 17:57:08 +00001203 }
Sean Huntc3021132010-05-05 15:23:54 +00001204
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00001205 /// \brief Build a new Objective-C @finally statement.
1206 ///
1207 /// By default, performs semantic analysis to build the new statement.
1208 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001209 StmtResult RebuildObjCAtFinallyStmt(SourceLocation AtLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001210 Stmt *Body) {
1211 return getSema().ActOnObjCAtFinallyStmt(AtLoc, Body);
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00001212 }
Sean Huntc3021132010-05-05 15:23:54 +00001213
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00001214 /// \brief Build a new Objective-C @throw statement.
Douglas Gregord1377b22010-04-22 21:44:01 +00001215 ///
1216 /// By default, performs semantic analysis to build the new statement.
1217 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001218 StmtResult RebuildObjCAtThrowStmt(SourceLocation AtLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001219 Expr *Operand) {
1220 return getSema().BuildObjCAtThrowStmt(AtLoc, Operand);
Douglas Gregord1377b22010-04-22 21:44:01 +00001221 }
Sean Huntc3021132010-05-05 15:23:54 +00001222
John McCall07524032011-07-27 21:50:02 +00001223 /// \brief Rebuild the operand to an Objective-C @synchronized statement.
1224 ///
1225 /// By default, performs semantic analysis to build the new statement.
1226 /// Subclasses may override this routine to provide different behavior.
1227 ExprResult RebuildObjCAtSynchronizedOperand(SourceLocation atLoc,
1228 Expr *object) {
1229 return getSema().ActOnObjCAtSynchronizedOperand(atLoc, object);
1230 }
1231
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00001232 /// \brief Build a new Objective-C @synchronized statement.
1233 ///
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00001234 /// By default, performs semantic analysis to build the new statement.
1235 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001236 StmtResult RebuildObjCAtSynchronizedStmt(SourceLocation AtLoc,
John McCall07524032011-07-27 21:50:02 +00001237 Expr *Object, Stmt *Body) {
1238 return getSema().ActOnObjCAtSynchronizedStmt(AtLoc, Object, Body);
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00001239 }
Douglas Gregorc3203e72010-04-22 23:10:45 +00001240
John McCallf85e1932011-06-15 23:02:42 +00001241 /// \brief Build a new Objective-C @autoreleasepool statement.
1242 ///
1243 /// By default, performs semantic analysis to build the new statement.
1244 /// Subclasses may override this routine to provide different behavior.
1245 StmtResult RebuildObjCAutoreleasePoolStmt(SourceLocation AtLoc,
1246 Stmt *Body) {
1247 return getSema().ActOnObjCAutoreleasePoolStmt(AtLoc, Body);
1248 }
John McCall990567c2011-07-27 01:07:15 +00001249
1250 /// \brief Build the collection operand to a new Objective-C fast
1251 /// enumeration statement.
1252 ///
1253 /// By default, performs semantic analysis to build the new statement.
1254 /// Subclasses may override this routine to provide different behavior.
1255 ExprResult RebuildObjCForCollectionOperand(SourceLocation forLoc,
1256 Expr *collection) {
1257 return getSema().ActOnObjCForCollectionOperand(forLoc, collection);
1258 }
John McCallf85e1932011-06-15 23:02:42 +00001259
Douglas Gregorc3203e72010-04-22 23:10:45 +00001260 /// \brief Build a new Objective-C fast enumeration statement.
1261 ///
1262 /// By default, performs semantic analysis to build the new statement.
1263 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001264 StmtResult RebuildObjCForCollectionStmt(SourceLocation ForLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001265 SourceLocation LParenLoc,
1266 Stmt *Element,
1267 Expr *Collection,
1268 SourceLocation RParenLoc,
1269 Stmt *Body) {
Douglas Gregorc3203e72010-04-22 23:10:45 +00001270 return getSema().ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001271 Element,
1272 Collection,
Douglas Gregorc3203e72010-04-22 23:10:45 +00001273 RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001274 Body);
Douglas Gregorc3203e72010-04-22 23:10:45 +00001275 }
Sean Huntc3021132010-05-05 15:23:54 +00001276
Douglas Gregor43959a92009-08-20 07:17:43 +00001277 /// \brief Build a new C++ exception declaration.
1278 ///
1279 /// By default, performs semantic analysis to build the new decaration.
1280 /// Subclasses may override this routine to provide different behavior.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001281 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCalla93c9342009-12-07 02:54:59 +00001282 TypeSourceInfo *Declarator,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001283 SourceLocation StartLoc,
1284 SourceLocation IdLoc,
1285 IdentifierInfo *Id) {
Douglas Gregorefdf9882011-04-14 22:32:28 +00001286 VarDecl *Var = getSema().BuildExceptionDeclaration(0, Declarator,
1287 StartLoc, IdLoc, Id);
1288 if (Var)
1289 getSema().CurContext->addDecl(Var);
1290 return Var;
Douglas Gregor43959a92009-08-20 07:17:43 +00001291 }
1292
1293 /// \brief Build a new C++ catch statement.
1294 ///
1295 /// By default, performs semantic analysis to build the new statement.
1296 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001297 StmtResult RebuildCXXCatchStmt(SourceLocation CatchLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001298 VarDecl *ExceptionDecl,
1299 Stmt *Handler) {
John McCall9ae2f072010-08-23 23:25:46 +00001300 return Owned(new (getSema().Context) CXXCatchStmt(CatchLoc, ExceptionDecl,
1301 Handler));
Douglas Gregor43959a92009-08-20 07:17:43 +00001302 }
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Douglas Gregor43959a92009-08-20 07:17:43 +00001304 /// \brief Build a new C++ try statement.
1305 ///
1306 /// By default, performs semantic analysis to build the new statement.
1307 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001308 StmtResult RebuildCXXTryStmt(SourceLocation TryLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001309 Stmt *TryBlock,
1310 MultiStmtArg Handlers) {
John McCall9ae2f072010-08-23 23:25:46 +00001311 return getSema().ActOnCXXTryBlock(TryLoc, TryBlock, move(Handlers));
Douglas Gregor43959a92009-08-20 07:17:43 +00001312 }
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Richard Smithad762fc2011-04-14 22:09:26 +00001314 /// \brief Build a new C++0x range-based for statement.
1315 ///
1316 /// By default, performs semantic analysis to build the new statement.
1317 /// Subclasses may override this routine to provide different behavior.
1318 StmtResult RebuildCXXForRangeStmt(SourceLocation ForLoc,
1319 SourceLocation ColonLoc,
1320 Stmt *Range, Stmt *BeginEnd,
1321 Expr *Cond, Expr *Inc,
1322 Stmt *LoopVar,
1323 SourceLocation RParenLoc) {
1324 return getSema().BuildCXXForRangeStmt(ForLoc, ColonLoc, Range, BeginEnd,
1325 Cond, Inc, LoopVar, RParenLoc);
1326 }
Douglas Gregorba0513d2011-10-25 01:33:02 +00001327
1328 /// \brief Build a new C++0x range-based for statement.
1329 ///
1330 /// By default, performs semantic analysis to build the new statement.
1331 /// Subclasses may override this routine to provide different behavior.
1332 StmtResult RebuildMSDependentExistsStmt(SourceLocation KeywordLoc,
1333 bool IsIfExists,
1334 NestedNameSpecifierLoc QualifierLoc,
1335 DeclarationNameInfo NameInfo,
1336 Stmt *Nested) {
1337 return getSema().BuildMSDependentExistsStmt(KeywordLoc, IsIfExists,
1338 QualifierLoc, NameInfo, Nested);
1339 }
1340
Richard Smithad762fc2011-04-14 22:09:26 +00001341 /// \brief Attach body to a C++0x range-based for statement.
1342 ///
1343 /// By default, performs semantic analysis to finish the new statement.
1344 /// Subclasses may override this routine to provide different behavior.
1345 StmtResult FinishCXXForRangeStmt(Stmt *ForRange, Stmt *Body) {
1346 return getSema().FinishCXXForRangeStmt(ForRange, Body);
1347 }
1348
John Wiegley28bbe4b2011-04-28 01:08:34 +00001349 StmtResult RebuildSEHTryStmt(bool IsCXXTry,
1350 SourceLocation TryLoc,
1351 Stmt *TryBlock,
1352 Stmt *Handler) {
1353 return getSema().ActOnSEHTryBlock(IsCXXTry,TryLoc,TryBlock,Handler);
1354 }
1355
1356 StmtResult RebuildSEHExceptStmt(SourceLocation Loc,
1357 Expr *FilterExpr,
1358 Stmt *Block) {
1359 return getSema().ActOnSEHExceptBlock(Loc,FilterExpr,Block);
1360 }
1361
1362 StmtResult RebuildSEHFinallyStmt(SourceLocation Loc,
1363 Stmt *Block) {
1364 return getSema().ActOnSEHFinallyBlock(Loc,Block);
1365 }
1366
Douglas Gregorb98b1992009-08-11 05:31:07 +00001367 /// \brief Build a new expression that references a declaration.
1368 ///
1369 /// By default, performs semantic analysis to build the new expression.
1370 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001371 ExprResult RebuildDeclarationNameExpr(const CXXScopeSpec &SS,
John McCallf312b1e2010-08-26 23:41:50 +00001372 LookupResult &R,
1373 bool RequiresADL) {
John McCallf7a1a742009-11-24 19:00:30 +00001374 return getSema().BuildDeclarationNameExpr(SS, R, RequiresADL);
1375 }
1376
1377
1378 /// \brief Build a new expression that references a declaration.
1379 ///
1380 /// By default, performs semantic analysis to build the new expression.
1381 /// Subclasses may override this routine to provide different behavior.
Douglas Gregor40d96a62011-02-28 21:54:11 +00001382 ExprResult RebuildDeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001383 ValueDecl *VD,
1384 const DeclarationNameInfo &NameInfo,
1385 TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00001386 CXXScopeSpec SS;
Douglas Gregor40d96a62011-02-28 21:54:11 +00001387 SS.Adopt(QualifierLoc);
John McCalldbd872f2009-12-08 09:08:17 +00001388
1389 // FIXME: loses template args.
Abramo Bagnara25777432010-08-11 22:01:17 +00001390
1391 return getSema().BuildDeclarationNameExpr(SS, NameInfo, VD);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001392 }
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Douglas Gregorb98b1992009-08-11 05:31:07 +00001394 /// \brief Build a new expression in parentheses.
Mike Stump1eb44332009-09-09 15:08:12 +00001395 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001396 /// By default, performs semantic analysis to build the new expression.
1397 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001398 ExprResult RebuildParenExpr(Expr *SubExpr, SourceLocation LParen,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001399 SourceLocation RParen) {
John McCall9ae2f072010-08-23 23:25:46 +00001400 return getSema().ActOnParenExpr(LParen, RParen, SubExpr);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001401 }
1402
Douglas Gregora71d8192009-09-04 17:36:40 +00001403 /// \brief Build a new pseudo-destructor expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001404 ///
Douglas Gregora71d8192009-09-04 17:36:40 +00001405 /// By default, performs semantic analysis to build the new expression.
1406 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001407 ExprResult RebuildCXXPseudoDestructorExpr(Expr *Base,
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001408 SourceLocation OperatorLoc,
1409 bool isArrow,
1410 CXXScopeSpec &SS,
1411 TypeSourceInfo *ScopeType,
1412 SourceLocation CCLoc,
1413 SourceLocation TildeLoc,
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001414 PseudoDestructorTypeStorage Destroyed);
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Douglas Gregorb98b1992009-08-11 05:31:07 +00001416 /// \brief Build a new unary operator expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001417 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001418 /// By default, performs semantic analysis to build the new expression.
1419 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001420 ExprResult RebuildUnaryOperator(SourceLocation OpLoc,
John McCall2de56d12010-08-25 11:45:40 +00001421 UnaryOperatorKind Opc,
John McCall9ae2f072010-08-23 23:25:46 +00001422 Expr *SubExpr) {
1423 return getSema().BuildUnaryOp(/*Scope=*/0, OpLoc, Opc, SubExpr);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001424 }
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001426 /// \brief Build a new builtin offsetof expression.
1427 ///
1428 /// By default, performs semantic analysis to build the new expression.
1429 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001430 ExprResult RebuildOffsetOfExpr(SourceLocation OperatorLoc,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001431 TypeSourceInfo *Type,
John McCallf312b1e2010-08-26 23:41:50 +00001432 Sema::OffsetOfComponent *Components,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001433 unsigned NumComponents,
1434 SourceLocation RParenLoc) {
1435 return getSema().BuildBuiltinOffsetOf(OperatorLoc, Type, Components,
1436 NumComponents, RParenLoc);
1437 }
Sean Huntc3021132010-05-05 15:23:54 +00001438
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001439 /// \brief Build a new sizeof, alignof or vec_step expression with a
1440 /// type argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001441 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001442 /// By default, performs semantic analysis to build the new expression.
1443 /// Subclasses may override this routine to provide different behavior.
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001444 ExprResult RebuildUnaryExprOrTypeTrait(TypeSourceInfo *TInfo,
1445 SourceLocation OpLoc,
1446 UnaryExprOrTypeTrait ExprKind,
1447 SourceRange R) {
1448 return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001449 }
1450
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001451 /// \brief Build a new sizeof, alignof or vec step expression with an
1452 /// expression argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001453 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001454 /// By default, performs semantic analysis to build the new expression.
1455 /// Subclasses may override this routine to provide different behavior.
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001456 ExprResult RebuildUnaryExprOrTypeTrait(Expr *SubExpr, SourceLocation OpLoc,
1457 UnaryExprOrTypeTrait ExprKind,
1458 SourceRange R) {
John McCall60d7b3a2010-08-24 06:29:42 +00001459 ExprResult Result
Chandler Carruthe72c55b2011-05-29 07:32:14 +00001460 = getSema().CreateUnaryExprOrTypeTraitExpr(SubExpr, OpLoc, ExprKind);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001461 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001462 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Douglas Gregorb98b1992009-08-11 05:31:07 +00001464 return move(Result);
1465 }
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Douglas Gregorb98b1992009-08-11 05:31:07 +00001467 /// \brief Build a new array subscript expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001468 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001469 /// By default, performs semantic analysis to build the new expression.
1470 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001471 ExprResult RebuildArraySubscriptExpr(Expr *LHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001472 SourceLocation LBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001473 Expr *RHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001474 SourceLocation RBracketLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001475 return getSema().ActOnArraySubscriptExpr(/*Scope=*/0, LHS,
1476 LBracketLoc, RHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001477 RBracketLoc);
1478 }
1479
1480 /// \brief Build a new call expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001481 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001482 /// By default, performs semantic analysis to build the new expression.
1483 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001484 ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001485 MultiExprArg Args,
Peter Collingbournee08ce652011-02-09 21:07:24 +00001486 SourceLocation RParenLoc,
1487 Expr *ExecConfig = 0) {
John McCall9ae2f072010-08-23 23:25:46 +00001488 return getSema().ActOnCallExpr(/*Scope=*/0, Callee, LParenLoc,
Peter Collingbournee08ce652011-02-09 21:07:24 +00001489 move(Args), RParenLoc, ExecConfig);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001490 }
1491
1492 /// \brief Build a new member access expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001493 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001494 /// By default, performs semantic analysis to build the new expression.
1495 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001496 ExprResult RebuildMemberExpr(Expr *Base, SourceLocation OpLoc,
John McCallf89e55a2010-11-18 06:31:45 +00001497 bool isArrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001498 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001499 SourceLocation TemplateKWLoc,
John McCallf89e55a2010-11-18 06:31:45 +00001500 const DeclarationNameInfo &MemberNameInfo,
1501 ValueDecl *Member,
1502 NamedDecl *FoundDecl,
John McCalld5532b62009-11-23 01:53:49 +00001503 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +00001504 NamedDecl *FirstQualifierInScope) {
Richard Smith9138b4e2011-10-26 19:06:56 +00001505 ExprResult BaseResult = getSema().PerformMemberExprBaseConversion(Base,
1506 isArrow);
Anders Carlssond8b285f2009-09-01 04:26:58 +00001507 if (!Member->getDeclName()) {
John McCallf89e55a2010-11-18 06:31:45 +00001508 // We have a reference to an unnamed field. This is always the
1509 // base of an anonymous struct/union member access, i.e. the
1510 // field is always of record type.
Douglas Gregor40d96a62011-02-28 21:54:11 +00001511 assert(!QualifierLoc && "Can't have an unnamed field with a qualifier!");
John McCallf89e55a2010-11-18 06:31:45 +00001512 assert(Member->getType()->isRecordType() &&
1513 "unnamed member not of record type?");
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Richard Smith9138b4e2011-10-26 19:06:56 +00001515 BaseResult =
1516 getSema().PerformObjectMemberConversion(BaseResult.take(),
John Wiegley429bb272011-04-08 18:41:53 +00001517 QualifierLoc.getNestedNameSpecifier(),
1518 FoundDecl, Member);
1519 if (BaseResult.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001520 return ExprError();
John Wiegley429bb272011-04-08 18:41:53 +00001521 Base = BaseResult.take();
John McCallf89e55a2010-11-18 06:31:45 +00001522 ExprValueKind VK = isArrow ? VK_LValue : Base->getValueKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001523 MemberExpr *ME =
John McCall9ae2f072010-08-23 23:25:46 +00001524 new (getSema().Context) MemberExpr(Base, isArrow,
Abramo Bagnara25777432010-08-11 22:01:17 +00001525 Member, MemberNameInfo,
John McCallf89e55a2010-11-18 06:31:45 +00001526 cast<FieldDecl>(Member)->getType(),
1527 VK, OK_Ordinary);
Anders Carlssond8b285f2009-09-01 04:26:58 +00001528 return getSema().Owned(ME);
1529 }
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001531 CXXScopeSpec SS;
Douglas Gregor40d96a62011-02-28 21:54:11 +00001532 SS.Adopt(QualifierLoc);
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001533
John Wiegley429bb272011-04-08 18:41:53 +00001534 Base = BaseResult.take();
John McCall9ae2f072010-08-23 23:25:46 +00001535 QualType BaseType = Base->getType();
John McCallaa81e162009-12-01 22:10:20 +00001536
John McCall6bb80172010-03-30 21:47:33 +00001537 // FIXME: this involves duplicating earlier analysis in a lot of
1538 // cases; we should avoid this when possible.
Abramo Bagnara25777432010-08-11 22:01:17 +00001539 LookupResult R(getSema(), MemberNameInfo, Sema::LookupMemberName);
John McCall6bb80172010-03-30 21:47:33 +00001540 R.addDecl(FoundDecl);
John McCallc2233c52010-01-15 08:34:02 +00001541 R.resolveKind();
1542
John McCall9ae2f072010-08-23 23:25:46 +00001543 return getSema().BuildMemberReferenceExpr(Base, BaseType, OpLoc, isArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001544 SS, TemplateKWLoc,
1545 FirstQualifierInScope,
John McCallc2233c52010-01-15 08:34:02 +00001546 R, ExplicitTemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Douglas Gregorb98b1992009-08-11 05:31:07 +00001549 /// \brief Build a new binary operator expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001550 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001551 /// By default, performs semantic analysis to build the new expression.
1552 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001553 ExprResult RebuildBinaryOperator(SourceLocation OpLoc,
John McCall2de56d12010-08-25 11:45:40 +00001554 BinaryOperatorKind Opc,
John McCall9ae2f072010-08-23 23:25:46 +00001555 Expr *LHS, Expr *RHS) {
1556 return getSema().BuildBinOp(/*Scope=*/0, OpLoc, Opc, LHS, RHS);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001557 }
1558
1559 /// \brief Build a new conditional operator expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001560 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001561 /// By default, performs semantic analysis to build the new expression.
1562 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001563 ExprResult RebuildConditionalOperator(Expr *Cond,
John McCall56ca35d2011-02-17 10:25:35 +00001564 SourceLocation QuestionLoc,
1565 Expr *LHS,
1566 SourceLocation ColonLoc,
1567 Expr *RHS) {
John McCall9ae2f072010-08-23 23:25:46 +00001568 return getSema().ActOnConditionalOp(QuestionLoc, ColonLoc, Cond,
1569 LHS, RHS);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001570 }
1571
Douglas Gregorb98b1992009-08-11 05:31:07 +00001572 /// \brief Build a new C-style cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001573 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001574 /// By default, performs semantic analysis to build the new expression.
1575 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001576 ExprResult RebuildCStyleCastExpr(SourceLocation LParenLoc,
John McCall9d125032010-01-15 18:39:57 +00001577 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001578 SourceLocation RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001579 Expr *SubExpr) {
John McCallb042fdf2010-01-15 18:56:44 +00001580 return getSema().BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001581 SubExpr);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregorb98b1992009-08-11 05:31:07 +00001584 /// \brief Build a new compound literal expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001585 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001586 /// By default, performs semantic analysis to build the new expression.
1587 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001588 ExprResult RebuildCompoundLiteralExpr(SourceLocation LParenLoc,
John McCall42f56b52010-01-18 19:35:47 +00001589 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001590 SourceLocation RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001591 Expr *Init) {
John McCall42f56b52010-01-18 19:35:47 +00001592 return getSema().BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001593 Init);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001594 }
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregorb98b1992009-08-11 05:31:07 +00001596 /// \brief Build a new extended vector element access expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001597 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001598 /// By default, performs semantic analysis to build the new expression.
1599 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001600 ExprResult RebuildExtVectorElementExpr(Expr *Base,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001601 SourceLocation OpLoc,
1602 SourceLocation AccessorLoc,
1603 IdentifierInfo &Accessor) {
John McCallaa81e162009-12-01 22:10:20 +00001604
John McCall129e2df2009-11-30 22:42:35 +00001605 CXXScopeSpec SS;
Abramo Bagnara25777432010-08-11 22:01:17 +00001606 DeclarationNameInfo NameInfo(&Accessor, AccessorLoc);
John McCall9ae2f072010-08-23 23:25:46 +00001607 return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
John McCall129e2df2009-11-30 22:42:35 +00001608 OpLoc, /*IsArrow*/ false,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001609 SS, SourceLocation(),
1610 /*FirstQualifierInScope*/ 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001611 NameInfo,
John McCall129e2df2009-11-30 22:42:35 +00001612 /* TemplateArgs */ 0);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001613 }
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Douglas Gregorb98b1992009-08-11 05:31:07 +00001615 /// \brief Build a new initializer list expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001616 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001617 /// By default, performs semantic analysis to build the new expression.
1618 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001619 ExprResult RebuildInitList(SourceLocation LBraceLoc,
John McCallc8fc90a2011-07-06 07:30:07 +00001620 MultiExprArg Inits,
1621 SourceLocation RBraceLoc,
1622 QualType ResultTy) {
John McCall60d7b3a2010-08-24 06:29:42 +00001623 ExprResult Result
Douglas Gregore48319a2009-11-09 17:16:50 +00001624 = SemaRef.ActOnInitList(LBraceLoc, move(Inits), RBraceLoc);
1625 if (Result.isInvalid() || ResultTy->isDependentType())
1626 return move(Result);
Sean Huntc3021132010-05-05 15:23:54 +00001627
Douglas Gregore48319a2009-11-09 17:16:50 +00001628 // Patch in the result type we were given, which may have been computed
1629 // when the initial InitListExpr was built.
1630 InitListExpr *ILE = cast<InitListExpr>((Expr *)Result.get());
1631 ILE->setType(ResultTy);
1632 return move(Result);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001633 }
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Douglas Gregorb98b1992009-08-11 05:31:07 +00001635 /// \brief Build a new designated initializer expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001636 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001637 /// By default, performs semantic analysis to build the new expression.
1638 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001639 ExprResult RebuildDesignatedInitExpr(Designation &Desig,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001640 MultiExprArg ArrayExprs,
1641 SourceLocation EqualOrColonLoc,
1642 bool GNUSyntax,
John McCall9ae2f072010-08-23 23:25:46 +00001643 Expr *Init) {
John McCall60d7b3a2010-08-24 06:29:42 +00001644 ExprResult Result
Douglas Gregorb98b1992009-08-11 05:31:07 +00001645 = SemaRef.ActOnDesignatedInitializer(Desig, EqualOrColonLoc, GNUSyntax,
John McCall9ae2f072010-08-23 23:25:46 +00001646 Init);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001647 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001648 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Douglas Gregorb98b1992009-08-11 05:31:07 +00001650 ArrayExprs.release();
1651 return move(Result);
1652 }
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Douglas Gregorb98b1992009-08-11 05:31:07 +00001654 /// \brief Build a new value-initialized expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001655 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001656 /// By default, builds the implicit value initialization without performing
1657 /// any semantic analysis. Subclasses may override this routine to provide
1658 /// different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001659 ExprResult RebuildImplicitValueInitExpr(QualType T) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001660 return SemaRef.Owned(new (SemaRef.Context) ImplicitValueInitExpr(T));
1661 }
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Douglas Gregorb98b1992009-08-11 05:31:07 +00001663 /// \brief Build a new \c va_arg expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001664 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001665 /// By default, performs semantic analysis to build the new expression.
1666 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001667 ExprResult RebuildVAArgExpr(SourceLocation BuiltinLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001668 Expr *SubExpr, TypeSourceInfo *TInfo,
Abramo Bagnara2cad9002010-08-10 10:06:15 +00001669 SourceLocation RParenLoc) {
1670 return getSema().BuildVAArgExpr(BuiltinLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001671 SubExpr, TInfo,
Abramo Bagnara2cad9002010-08-10 10:06:15 +00001672 RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001673 }
1674
1675 /// \brief Build a new expression list in parentheses.
Mike Stump1eb44332009-09-09 15:08:12 +00001676 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001677 /// By default, performs semantic analysis to build the new expression.
1678 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001679 ExprResult RebuildParenListExpr(SourceLocation LParenLoc,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001680 MultiExprArg SubExprs,
1681 SourceLocation RParenLoc) {
1682 return getSema().ActOnParenListExpr(LParenLoc, RParenLoc, move(SubExprs));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001683 }
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Douglas Gregorb98b1992009-08-11 05:31:07 +00001685 /// \brief Build a new address-of-label expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001686 ///
1687 /// By default, performs semantic analysis, using the name of the label
Douglas Gregorb98b1992009-08-11 05:31:07 +00001688 /// rather than attempting to map the label statement itself.
1689 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001690 ExprResult RebuildAddrLabelExpr(SourceLocation AmpAmpLoc,
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001691 SourceLocation LabelLoc, LabelDecl *Label) {
Chris Lattner57ad3782011-02-17 20:34:02 +00001692 return getSema().ActOnAddrLabel(AmpAmpLoc, LabelLoc, Label);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001693 }
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Douglas Gregorb98b1992009-08-11 05:31:07 +00001695 /// \brief Build a new GNU statement expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001696 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001697 /// By default, performs semantic analysis to build the new expression.
1698 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001699 ExprResult RebuildStmtExpr(SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001700 Stmt *SubStmt,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001701 SourceLocation RParenLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001702 return getSema().ActOnStmtExpr(LParenLoc, SubStmt, RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001703 }
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Douglas Gregorb98b1992009-08-11 05:31:07 +00001705 /// \brief Build a new __builtin_choose_expr expression.
1706 ///
1707 /// By default, performs semantic analysis to build the new expression.
1708 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001709 ExprResult RebuildChooseExpr(SourceLocation BuiltinLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001710 Expr *Cond, Expr *LHS, Expr *RHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001711 SourceLocation RParenLoc) {
1712 return SemaRef.ActOnChooseExpr(BuiltinLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001713 Cond, LHS, RHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001714 RParenLoc);
1715 }
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Peter Collingbournef111d932011-04-15 00:35:48 +00001717 /// \brief Build a new generic selection expression.
1718 ///
1719 /// By default, performs semantic analysis to build the new expression.
1720 /// Subclasses may override this routine to provide different behavior.
1721 ExprResult RebuildGenericSelectionExpr(SourceLocation KeyLoc,
1722 SourceLocation DefaultLoc,
1723 SourceLocation RParenLoc,
1724 Expr *ControllingExpr,
1725 TypeSourceInfo **Types,
1726 Expr **Exprs,
1727 unsigned NumAssocs) {
1728 return getSema().CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc,
1729 ControllingExpr, Types, Exprs,
1730 NumAssocs);
1731 }
1732
Douglas Gregorb98b1992009-08-11 05:31:07 +00001733 /// \brief Build a new overloaded operator call expression.
1734 ///
1735 /// By default, performs semantic analysis to build the new expression.
1736 /// The semantic analysis provides the behavior of template instantiation,
1737 /// copying with transformations that turn what looks like an overloaded
Mike Stump1eb44332009-09-09 15:08:12 +00001738 /// operator call into a use of a builtin operator, performing
Douglas Gregorb98b1992009-08-11 05:31:07 +00001739 /// argument-dependent lookup, etc. Subclasses may override this routine to
1740 /// provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001741 ExprResult RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001742 SourceLocation OpLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001743 Expr *Callee,
1744 Expr *First,
1745 Expr *Second);
Mike Stump1eb44332009-09-09 15:08:12 +00001746
1747 /// \brief Build a new C++ "named" cast expression, such as static_cast or
Douglas Gregorb98b1992009-08-11 05:31:07 +00001748 /// reinterpret_cast.
1749 ///
1750 /// By default, this routine dispatches to one of the more-specific routines
Mike Stump1eb44332009-09-09 15:08:12 +00001751 /// for a particular named case, e.g., RebuildCXXStaticCastExpr().
Douglas Gregorb98b1992009-08-11 05:31:07 +00001752 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001753 ExprResult RebuildCXXNamedCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001754 Stmt::StmtClass Class,
1755 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001756 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001757 SourceLocation RAngleLoc,
1758 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001759 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001760 SourceLocation RParenLoc) {
1761 switch (Class) {
1762 case Stmt::CXXStaticCastExprClass:
John McCall9d125032010-01-15 18:39:57 +00001763 return getDerived().RebuildCXXStaticCastExpr(OpLoc, LAngleLoc, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001764 RAngleLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001765 SubExpr, RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001766
1767 case Stmt::CXXDynamicCastExprClass:
John McCall9d125032010-01-15 18:39:57 +00001768 return getDerived().RebuildCXXDynamicCastExpr(OpLoc, LAngleLoc, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001769 RAngleLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001770 SubExpr, RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Douglas Gregorb98b1992009-08-11 05:31:07 +00001772 case Stmt::CXXReinterpretCastExprClass:
John McCall9d125032010-01-15 18:39:57 +00001773 return getDerived().RebuildCXXReinterpretCastExpr(OpLoc, LAngleLoc, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001774 RAngleLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001775 SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001776 RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Douglas Gregorb98b1992009-08-11 05:31:07 +00001778 case Stmt::CXXConstCastExprClass:
John McCall9d125032010-01-15 18:39:57 +00001779 return getDerived().RebuildCXXConstCastExpr(OpLoc, LAngleLoc, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001780 RAngleLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001781 SubExpr, RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Douglas Gregorb98b1992009-08-11 05:31:07 +00001783 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001784 llvm_unreachable("Invalid C++ named cast");
Douglas Gregorb98b1992009-08-11 05:31:07 +00001785 }
Mike Stump1eb44332009-09-09 15:08:12 +00001786
John McCallf312b1e2010-08-26 23:41:50 +00001787 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00001788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Douglas Gregorb98b1992009-08-11 05:31:07 +00001790 /// \brief Build a new C++ static_cast expression.
1791 ///
1792 /// By default, performs semantic analysis to build the new expression.
1793 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001794 ExprResult RebuildCXXStaticCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001795 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001796 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001797 SourceLocation RAngleLoc,
1798 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001799 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001800 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +00001801 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_static_cast,
John McCall9ae2f072010-08-23 23:25:46 +00001802 TInfo, SubExpr,
John McCallc89724c2010-01-15 19:13:16 +00001803 SourceRange(LAngleLoc, RAngleLoc),
1804 SourceRange(LParenLoc, RParenLoc));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001805 }
1806
1807 /// \brief Build a new C++ dynamic_cast expression.
1808 ///
1809 /// By default, performs semantic analysis to build the new expression.
1810 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001811 ExprResult RebuildCXXDynamicCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001812 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001813 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001814 SourceLocation RAngleLoc,
1815 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001816 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001817 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +00001818 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_dynamic_cast,
John McCall9ae2f072010-08-23 23:25:46 +00001819 TInfo, SubExpr,
John McCallc89724c2010-01-15 19:13:16 +00001820 SourceRange(LAngleLoc, RAngleLoc),
1821 SourceRange(LParenLoc, RParenLoc));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001822 }
1823
1824 /// \brief Build a new C++ reinterpret_cast expression.
1825 ///
1826 /// By default, performs semantic analysis to build the new expression.
1827 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001828 ExprResult RebuildCXXReinterpretCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001829 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001830 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001831 SourceLocation RAngleLoc,
1832 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001833 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001834 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +00001835 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_reinterpret_cast,
John McCall9ae2f072010-08-23 23:25:46 +00001836 TInfo, SubExpr,
John McCallc89724c2010-01-15 19:13:16 +00001837 SourceRange(LAngleLoc, RAngleLoc),
1838 SourceRange(LParenLoc, RParenLoc));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001839 }
1840
1841 /// \brief Build a new C++ const_cast expression.
1842 ///
1843 /// By default, performs semantic analysis to build the new expression.
1844 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001845 ExprResult RebuildCXXConstCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001846 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001847 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001848 SourceLocation RAngleLoc,
1849 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001850 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001851 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +00001852 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_const_cast,
John McCall9ae2f072010-08-23 23:25:46 +00001853 TInfo, SubExpr,
John McCallc89724c2010-01-15 19:13:16 +00001854 SourceRange(LAngleLoc, RAngleLoc),
1855 SourceRange(LParenLoc, RParenLoc));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001856 }
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Douglas Gregorb98b1992009-08-11 05:31:07 +00001858 /// \brief Build a new C++ functional-style cast expression.
1859 ///
1860 /// By default, performs semantic analysis to build the new expression.
1861 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorab6677e2010-09-08 00:15:04 +00001862 ExprResult RebuildCXXFunctionalCastExpr(TypeSourceInfo *TInfo,
1863 SourceLocation LParenLoc,
1864 Expr *Sub,
1865 SourceLocation RParenLoc) {
1866 return getSema().BuildCXXTypeConstructExpr(TInfo, LParenLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001867 MultiExprArg(&Sub, 1),
Douglas Gregorb98b1992009-08-11 05:31:07 +00001868 RParenLoc);
1869 }
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Douglas Gregorb98b1992009-08-11 05:31:07 +00001871 /// \brief Build a new C++ typeid(type) expression.
1872 ///
1873 /// By default, performs semantic analysis to build the new expression.
1874 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001875 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00001876 SourceLocation TypeidLoc,
1877 TypeSourceInfo *Operand,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001878 SourceLocation RParenLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00001879 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00001880 RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001881 }
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Francois Pichet01b7c302010-09-08 12:20:18 +00001883
Douglas Gregorb98b1992009-08-11 05:31:07 +00001884 /// \brief Build a new C++ typeid(expr) expression.
1885 ///
1886 /// By default, performs semantic analysis to build the new expression.
1887 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001888 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00001889 SourceLocation TypeidLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001890 Expr *Operand,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001891 SourceLocation RParenLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001892 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00001893 RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001894 }
1895
Francois Pichet01b7c302010-09-08 12:20:18 +00001896 /// \brief Build a new C++ __uuidof(type) expression.
1897 ///
1898 /// By default, performs semantic analysis to build the new expression.
1899 /// Subclasses may override this routine to provide different behavior.
1900 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1901 SourceLocation TypeidLoc,
1902 TypeSourceInfo *Operand,
1903 SourceLocation RParenLoc) {
1904 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1905 RParenLoc);
1906 }
1907
1908 /// \brief Build a new C++ __uuidof(expr) expression.
1909 ///
1910 /// By default, performs semantic analysis to build the new expression.
1911 /// Subclasses may override this routine to provide different behavior.
1912 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1913 SourceLocation TypeidLoc,
1914 Expr *Operand,
1915 SourceLocation RParenLoc) {
1916 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1917 RParenLoc);
1918 }
1919
Douglas Gregorb98b1992009-08-11 05:31:07 +00001920 /// \brief Build a new C++ "this" expression.
1921 ///
1922 /// By default, builds a new "this" expression without performing any
Mike Stump1eb44332009-09-09 15:08:12 +00001923 /// semantic analysis. Subclasses may override this routine to provide
Douglas Gregorb98b1992009-08-11 05:31:07 +00001924 /// different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001925 ExprResult RebuildCXXThisExpr(SourceLocation ThisLoc,
Douglas Gregorba48d6a2010-09-09 16:55:46 +00001926 QualType ThisType,
1927 bool isImplicit) {
Eli Friedmanb69b42c2012-01-11 02:36:31 +00001928 getSema().CheckCXXThisCapture(ThisLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001929 return getSema().Owned(
Douglas Gregor828a1972010-01-07 23:12:05 +00001930 new (getSema().Context) CXXThisExpr(ThisLoc, ThisType,
1931 isImplicit));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001932 }
1933
1934 /// \brief Build a new C++ throw expression.
1935 ///
1936 /// By default, performs semantic analysis to build the new expression.
1937 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorbca01b42011-07-06 22:04:06 +00001938 ExprResult RebuildCXXThrowExpr(SourceLocation ThrowLoc, Expr *Sub,
1939 bool IsThrownVariableInScope) {
1940 return getSema().BuildCXXThrow(ThrowLoc, Sub, IsThrownVariableInScope);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001941 }
1942
1943 /// \brief Build a new C++ default-argument expression.
1944 ///
1945 /// By default, builds a new default-argument expression, which does not
1946 /// require any semantic analysis. Subclasses may override this routine to
1947 /// provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001948 ExprResult RebuildCXXDefaultArgExpr(SourceLocation Loc,
Douglas Gregor036aed12009-12-23 23:03:06 +00001949 ParmVarDecl *Param) {
1950 return getSema().Owned(CXXDefaultArgExpr::Create(getSema().Context, Loc,
1951 Param));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001952 }
1953
1954 /// \brief Build a new C++ zero-initialization expression.
1955 ///
1956 /// By default, performs semantic analysis to build the new expression.
1957 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorab6677e2010-09-08 00:15:04 +00001958 ExprResult RebuildCXXScalarValueInitExpr(TypeSourceInfo *TSInfo,
1959 SourceLocation LParenLoc,
1960 SourceLocation RParenLoc) {
1961 return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001962 MultiExprArg(getSema(), 0, 0),
Douglas Gregorab6677e2010-09-08 00:15:04 +00001963 RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001964 }
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Douglas Gregorb98b1992009-08-11 05:31:07 +00001966 /// \brief Build a new C++ "new" expression.
1967 ///
1968 /// By default, performs semantic analysis to build the new expression.
1969 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001970 ExprResult RebuildCXXNewExpr(SourceLocation StartLoc,
Douglas Gregor1bb2a932010-09-07 21:49:58 +00001971 bool UseGlobal,
1972 SourceLocation PlacementLParen,
1973 MultiExprArg PlacementArgs,
1974 SourceLocation PlacementRParen,
1975 SourceRange TypeIdParens,
1976 QualType AllocatedType,
1977 TypeSourceInfo *AllocatedTypeInfo,
1978 Expr *ArraySize,
Sebastian Redl1548d142012-02-16 11:35:52 +00001979 SourceLocation ConstructorLParen,
1980 MultiExprArg ConstructorArgs,
1981 SourceLocation ConstructorRParen) {
Mike Stump1eb44332009-09-09 15:08:12 +00001982 return getSema().BuildCXXNew(StartLoc, UseGlobal,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001983 PlacementLParen,
1984 move(PlacementArgs),
1985 PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001986 TypeIdParens,
Douglas Gregor1bb2a932010-09-07 21:49:58 +00001987 AllocatedType,
1988 AllocatedTypeInfo,
John McCall9ae2f072010-08-23 23:25:46 +00001989 ArraySize,
Sebastian Redl1548d142012-02-16 11:35:52 +00001990 ConstructorLParen,
1991 move(ConstructorArgs),
1992 ConstructorRParen);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001993 }
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Douglas Gregorb98b1992009-08-11 05:31:07 +00001995 /// \brief Build a new C++ "delete" expression.
1996 ///
1997 /// By default, performs semantic analysis to build the new expression.
1998 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001999 ExprResult RebuildCXXDeleteExpr(SourceLocation StartLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002000 bool IsGlobalDelete,
2001 bool IsArrayForm,
John McCall9ae2f072010-08-23 23:25:46 +00002002 Expr *Operand) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002003 return getSema().ActOnCXXDelete(StartLoc, IsGlobalDelete, IsArrayForm,
John McCall9ae2f072010-08-23 23:25:46 +00002004 Operand);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002005 }
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Douglas Gregorb98b1992009-08-11 05:31:07 +00002007 /// \brief Build a new unary type trait expression.
2008 ///
2009 /// By default, performs semantic analysis to build the new expression.
2010 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002011 ExprResult RebuildUnaryTypeTrait(UnaryTypeTrait Trait,
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002012 SourceLocation StartLoc,
2013 TypeSourceInfo *T,
2014 SourceLocation RParenLoc) {
2015 return getSema().BuildUnaryTypeTrait(Trait, StartLoc, T, RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002016 }
2017
Francois Pichet6ad6f282010-12-07 00:08:36 +00002018 /// \brief Build a new binary type trait expression.
2019 ///
2020 /// By default, performs semantic analysis to build the new expression.
2021 /// Subclasses may override this routine to provide different behavior.
2022 ExprResult RebuildBinaryTypeTrait(BinaryTypeTrait Trait,
2023 SourceLocation StartLoc,
2024 TypeSourceInfo *LhsT,
2025 TypeSourceInfo *RhsT,
2026 SourceLocation RParenLoc) {
2027 return getSema().BuildBinaryTypeTrait(Trait, StartLoc, LhsT, RhsT, RParenLoc);
2028 }
2029
John Wiegley21ff2e52011-04-28 00:16:57 +00002030 /// \brief Build a new array type trait expression.
2031 ///
2032 /// By default, performs semantic analysis to build the new expression.
2033 /// Subclasses may override this routine to provide different behavior.
2034 ExprResult RebuildArrayTypeTrait(ArrayTypeTrait Trait,
2035 SourceLocation StartLoc,
2036 TypeSourceInfo *TSInfo,
2037 Expr *DimExpr,
2038 SourceLocation RParenLoc) {
2039 return getSema().BuildArrayTypeTrait(Trait, StartLoc, TSInfo, DimExpr, RParenLoc);
2040 }
2041
John Wiegley55262202011-04-25 06:54:41 +00002042 /// \brief Build a new expression trait expression.
2043 ///
2044 /// By default, performs semantic analysis to build the new expression.
2045 /// Subclasses may override this routine to provide different behavior.
2046 ExprResult RebuildExpressionTrait(ExpressionTrait Trait,
2047 SourceLocation StartLoc,
2048 Expr *Queried,
2049 SourceLocation RParenLoc) {
2050 return getSema().BuildExpressionTrait(Trait, StartLoc, Queried, RParenLoc);
2051 }
2052
Mike Stump1eb44332009-09-09 15:08:12 +00002053 /// \brief Build a new (previously unresolved) declaration reference
Douglas Gregorb98b1992009-08-11 05:31:07 +00002054 /// expression.
2055 ///
2056 /// By default, performs semantic analysis to build the new expression.
2057 /// Subclasses may override this routine to provide different behavior.
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002058 ExprResult RebuildDependentScopeDeclRefExpr(
2059 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002060 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002061 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002062 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002063 CXXScopeSpec SS;
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002064 SS.Adopt(QualifierLoc);
John McCallf7a1a742009-11-24 19:00:30 +00002065
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002066 if (TemplateArgs || TemplateKWLoc.isValid())
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002067 return getSema().BuildQualifiedTemplateIdExpr(SS, TemplateKWLoc,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002068 NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +00002069
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002070 return getSema().BuildQualifiedDeclarationNameExpr(SS, NameInfo);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002071 }
2072
2073 /// \brief Build a new template-id expression.
2074 ///
2075 /// By default, performs semantic analysis to build the new expression.
2076 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002077 ExprResult RebuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002078 SourceLocation TemplateKWLoc,
2079 LookupResult &R,
2080 bool RequiresADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002081 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002082 return getSema().BuildTemplateIdExpr(SS, TemplateKWLoc, R, RequiresADL,
2083 TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002084 }
2085
2086 /// \brief Build a new object-construction expression.
2087 ///
2088 /// By default, performs semantic analysis to build the new expression.
2089 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002090 ExprResult RebuildCXXConstructExpr(QualType T,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00002091 SourceLocation Loc,
2092 CXXConstructorDecl *Constructor,
2093 bool IsElidable,
2094 MultiExprArg Args,
2095 bool HadMultipleCandidates,
2096 bool RequiresZeroInit,
Chandler Carruth428edaf2010-10-25 08:47:36 +00002097 CXXConstructExpr::ConstructionKind ConstructKind,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00002098 SourceRange ParenRange) {
John McCallca0408f2010-08-23 06:44:23 +00002099 ASTOwningVector<Expr*> ConvertedArgs(SemaRef);
Sean Huntc3021132010-05-05 15:23:54 +00002100 if (getSema().CompleteConstructorCall(Constructor, move(Args), Loc,
Douglas Gregor4411d2e2009-12-14 16:27:04 +00002101 ConvertedArgs))
John McCallf312b1e2010-08-26 23:41:50 +00002102 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00002103
Douglas Gregor4411d2e2009-12-14 16:27:04 +00002104 return getSema().BuildCXXConstructExpr(Loc, T, Constructor, IsElidable,
Douglas Gregor8c3e5542010-08-22 17:20:18 +00002105 move_arg(ConvertedArgs),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00002106 HadMultipleCandidates,
Chandler Carruth428edaf2010-10-25 08:47:36 +00002107 RequiresZeroInit, ConstructKind,
2108 ParenRange);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002109 }
2110
2111 /// \brief Build a new object-construction expression.
2112 ///
2113 /// By default, performs semantic analysis to build the new expression.
2114 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorab6677e2010-09-08 00:15:04 +00002115 ExprResult RebuildCXXTemporaryObjectExpr(TypeSourceInfo *TSInfo,
2116 SourceLocation LParenLoc,
2117 MultiExprArg Args,
2118 SourceLocation RParenLoc) {
2119 return getSema().BuildCXXTypeConstructExpr(TSInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002120 LParenLoc,
2121 move(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00002122 RParenLoc);
2123 }
2124
2125 /// \brief Build a new object-construction expression.
2126 ///
2127 /// By default, performs semantic analysis to build the new expression.
2128 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorab6677e2010-09-08 00:15:04 +00002129 ExprResult RebuildCXXUnresolvedConstructExpr(TypeSourceInfo *TSInfo,
2130 SourceLocation LParenLoc,
2131 MultiExprArg Args,
2132 SourceLocation RParenLoc) {
2133 return getSema().BuildCXXTypeConstructExpr(TSInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002134 LParenLoc,
2135 move(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00002136 RParenLoc);
2137 }
Mike Stump1eb44332009-09-09 15:08:12 +00002138
Douglas Gregorb98b1992009-08-11 05:31:07 +00002139 /// \brief Build a new member reference expression.
2140 ///
2141 /// By default, performs semantic analysis to build the new expression.
2142 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002143 ExprResult RebuildCXXDependentScopeMemberExpr(Expr *BaseE,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002144 QualType BaseType,
2145 bool IsArrow,
2146 SourceLocation OperatorLoc,
2147 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002148 SourceLocation TemplateKWLoc,
John McCall129e2df2009-11-30 22:42:35 +00002149 NamedDecl *FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00002150 const DeclarationNameInfo &MemberNameInfo,
John McCall129e2df2009-11-30 22:42:35 +00002151 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002152 CXXScopeSpec SS;
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002153 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002154
John McCall9ae2f072010-08-23 23:25:46 +00002155 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
John McCallaa81e162009-12-01 22:10:20 +00002156 OperatorLoc, IsArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002157 SS, TemplateKWLoc,
2158 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00002159 MemberNameInfo,
2160 TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002161 }
2162
John McCall129e2df2009-11-30 22:42:35 +00002163 /// \brief Build a new member reference expression.
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00002164 ///
2165 /// By default, performs semantic analysis to build the new expression.
2166 /// Subclasses may override this routine to provide different behavior.
Richard Smith9138b4e2011-10-26 19:06:56 +00002167 ExprResult RebuildUnresolvedMemberExpr(Expr *BaseE, QualType BaseType,
2168 SourceLocation OperatorLoc,
2169 bool IsArrow,
2170 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002171 SourceLocation TemplateKWLoc,
Richard Smith9138b4e2011-10-26 19:06:56 +00002172 NamedDecl *FirstQualifierInScope,
2173 LookupResult &R,
John McCall129e2df2009-11-30 22:42:35 +00002174 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00002175 CXXScopeSpec SS;
Douglas Gregor4c9be892011-02-28 20:01:57 +00002176 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002177
John McCall9ae2f072010-08-23 23:25:46 +00002178 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
John McCallaa81e162009-12-01 22:10:20 +00002179 OperatorLoc, IsArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002180 SS, TemplateKWLoc,
2181 FirstQualifierInScope,
John McCallc2233c52010-01-15 08:34:02 +00002182 R, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00002183 }
Mike Stump1eb44332009-09-09 15:08:12 +00002184
Sebastian Redl2e156222010-09-10 20:55:43 +00002185 /// \brief Build a new noexcept expression.
2186 ///
2187 /// By default, performs semantic analysis to build the new expression.
2188 /// Subclasses may override this routine to provide different behavior.
2189 ExprResult RebuildCXXNoexceptExpr(SourceRange Range, Expr *Arg) {
2190 return SemaRef.BuildCXXNoexceptExpr(Range.getBegin(), Arg, Range.getEnd());
2191 }
2192
Douglas Gregoree8aff02011-01-04 17:33:58 +00002193 /// \brief Build a new expression to compute the length of a parameter pack.
2194 ExprResult RebuildSizeOfPackExpr(SourceLocation OperatorLoc, NamedDecl *Pack,
2195 SourceLocation PackLoc,
2196 SourceLocation RParenLoc,
Douglas Gregor089e8932011-10-10 18:59:29 +00002197 llvm::Optional<unsigned> Length) {
2198 if (Length)
2199 return new (SemaRef.Context) SizeOfPackExpr(SemaRef.Context.getSizeType(),
2200 OperatorLoc, Pack, PackLoc,
2201 RParenLoc, *Length);
2202
Douglas Gregoree8aff02011-01-04 17:33:58 +00002203 return new (SemaRef.Context) SizeOfPackExpr(SemaRef.Context.getSizeType(),
2204 OperatorLoc, Pack, PackLoc,
Douglas Gregor089e8932011-10-10 18:59:29 +00002205 RParenLoc);
Douglas Gregoree8aff02011-01-04 17:33:58 +00002206 }
2207
Douglas Gregorb98b1992009-08-11 05:31:07 +00002208 /// \brief Build a new Objective-C @encode expression.
2209 ///
2210 /// By default, performs semantic analysis to build the new expression.
2211 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002212 ExprResult RebuildObjCEncodeExpr(SourceLocation AtLoc,
Douglas Gregor81d34662010-04-20 15:39:42 +00002213 TypeSourceInfo *EncodeTypeInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002214 SourceLocation RParenLoc) {
Douglas Gregor81d34662010-04-20 15:39:42 +00002215 return SemaRef.Owned(SemaRef.BuildObjCEncodeExpression(AtLoc, EncodeTypeInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002216 RParenLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00002217 }
Douglas Gregorb98b1992009-08-11 05:31:07 +00002218
Douglas Gregor92e986e2010-04-22 16:44:27 +00002219 /// \brief Build a new Objective-C class message.
John McCall60d7b3a2010-08-24 06:29:42 +00002220 ExprResult RebuildObjCMessageExpr(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002221 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002222 ArrayRef<SourceLocation> SelectorLocs,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002223 ObjCMethodDecl *Method,
Sean Huntc3021132010-05-05 15:23:54 +00002224 SourceLocation LBracLoc,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002225 MultiExprArg Args,
2226 SourceLocation RBracLoc) {
Douglas Gregor92e986e2010-04-22 16:44:27 +00002227 return SemaRef.BuildClassMessage(ReceiverTypeInfo,
2228 ReceiverTypeInfo->getType(),
2229 /*SuperLoc=*/SourceLocation(),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002230 Sel, Method, LBracLoc, SelectorLocs,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002231 RBracLoc, move(Args));
Douglas Gregor92e986e2010-04-22 16:44:27 +00002232 }
2233
2234 /// \brief Build a new Objective-C instance message.
John McCall60d7b3a2010-08-24 06:29:42 +00002235 ExprResult RebuildObjCMessageExpr(Expr *Receiver,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002236 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002237 ArrayRef<SourceLocation> SelectorLocs,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002238 ObjCMethodDecl *Method,
Sean Huntc3021132010-05-05 15:23:54 +00002239 SourceLocation LBracLoc,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002240 MultiExprArg Args,
2241 SourceLocation RBracLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00002242 return SemaRef.BuildInstanceMessage(Receiver,
2243 Receiver->getType(),
Douglas Gregor92e986e2010-04-22 16:44:27 +00002244 /*SuperLoc=*/SourceLocation(),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002245 Sel, Method, LBracLoc, SelectorLocs,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002246 RBracLoc, move(Args));
Douglas Gregor92e986e2010-04-22 16:44:27 +00002247 }
2248
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002249 /// \brief Build a new Objective-C ivar reference expression.
2250 ///
2251 /// By default, performs semantic analysis to build the new expression.
2252 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002253 ExprResult RebuildObjCIvarRefExpr(Expr *BaseArg, ObjCIvarDecl *Ivar,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002254 SourceLocation IvarLoc,
2255 bool IsArrow, bool IsFreeIvar) {
2256 // FIXME: We lose track of the IsFreeIvar bit.
2257 CXXScopeSpec SS;
John Wiegley429bb272011-04-08 18:41:53 +00002258 ExprResult Base = getSema().Owned(BaseArg);
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002259 LookupResult R(getSema(), Ivar->getDeclName(), IvarLoc,
2260 Sema::LookupMemberName);
John McCall60d7b3a2010-08-24 06:29:42 +00002261 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002262 /*FIME:*/IvarLoc,
John McCalld226f652010-08-21 09:40:31 +00002263 SS, 0,
John McCallad00b772010-06-16 08:42:20 +00002264 false);
John Wiegley429bb272011-04-08 18:41:53 +00002265 if (Result.isInvalid() || Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00002266 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00002267
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002268 if (Result.get())
2269 return move(Result);
Sean Huntc3021132010-05-05 15:23:54 +00002270
John Wiegley429bb272011-04-08 18:41:53 +00002271 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002272 /*FIXME:*/IvarLoc, IsArrow,
2273 SS, SourceLocation(),
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002274 /*FirstQualifierInScope=*/0,
Sean Huntc3021132010-05-05 15:23:54 +00002275 R,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002276 /*TemplateArgs=*/0);
2277 }
Douglas Gregore3303542010-04-26 20:47:02 +00002278
2279 /// \brief Build a new Objective-C property reference expression.
2280 ///
2281 /// By default, performs semantic analysis to build the new expression.
2282 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002283 ExprResult RebuildObjCPropertyRefExpr(Expr *BaseArg,
John McCall3c3b7f92011-10-25 17:37:35 +00002284 ObjCPropertyDecl *Property,
2285 SourceLocation PropertyLoc) {
Douglas Gregore3303542010-04-26 20:47:02 +00002286 CXXScopeSpec SS;
John Wiegley429bb272011-04-08 18:41:53 +00002287 ExprResult Base = getSema().Owned(BaseArg);
Douglas Gregore3303542010-04-26 20:47:02 +00002288 LookupResult R(getSema(), Property->getDeclName(), PropertyLoc,
2289 Sema::LookupMemberName);
2290 bool IsArrow = false;
John McCall60d7b3a2010-08-24 06:29:42 +00002291 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
Douglas Gregore3303542010-04-26 20:47:02 +00002292 /*FIME:*/PropertyLoc,
John McCalld226f652010-08-21 09:40:31 +00002293 SS, 0, false);
John Wiegley429bb272011-04-08 18:41:53 +00002294 if (Result.isInvalid() || Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00002295 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00002296
Douglas Gregore3303542010-04-26 20:47:02 +00002297 if (Result.get())
2298 return move(Result);
Sean Huntc3021132010-05-05 15:23:54 +00002299
John Wiegley429bb272011-04-08 18:41:53 +00002300 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
Sean Huntc3021132010-05-05 15:23:54 +00002301 /*FIXME:*/PropertyLoc, IsArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002302 SS, SourceLocation(),
Douglas Gregore3303542010-04-26 20:47:02 +00002303 /*FirstQualifierInScope=*/0,
Sean Huntc3021132010-05-05 15:23:54 +00002304 R,
Douglas Gregore3303542010-04-26 20:47:02 +00002305 /*TemplateArgs=*/0);
2306 }
Sean Huntc3021132010-05-05 15:23:54 +00002307
John McCall12f78a62010-12-02 01:19:52 +00002308 /// \brief Build a new Objective-C property reference expression.
Douglas Gregor9cbfdd22010-04-26 21:04:54 +00002309 ///
2310 /// By default, performs semantic analysis to build the new expression.
John McCall12f78a62010-12-02 01:19:52 +00002311 /// Subclasses may override this routine to provide different behavior.
2312 ExprResult RebuildObjCPropertyRefExpr(Expr *Base, QualType T,
2313 ObjCMethodDecl *Getter,
2314 ObjCMethodDecl *Setter,
2315 SourceLocation PropertyLoc) {
2316 // Since these expressions can only be value-dependent, we do not
2317 // need to perform semantic analysis again.
2318 return Owned(
2319 new (getSema().Context) ObjCPropertyRefExpr(Getter, Setter, T,
2320 VK_LValue, OK_ObjCProperty,
2321 PropertyLoc, Base));
Douglas Gregor9cbfdd22010-04-26 21:04:54 +00002322 }
2323
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002324 /// \brief Build a new Objective-C "isa" expression.
2325 ///
2326 /// By default, performs semantic analysis to build the new expression.
2327 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002328 ExprResult RebuildObjCIsaExpr(Expr *BaseArg, SourceLocation IsaLoc,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002329 bool IsArrow) {
2330 CXXScopeSpec SS;
John Wiegley429bb272011-04-08 18:41:53 +00002331 ExprResult Base = getSema().Owned(BaseArg);
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002332 LookupResult R(getSema(), &getSema().Context.Idents.get("isa"), IsaLoc,
2333 Sema::LookupMemberName);
John McCall60d7b3a2010-08-24 06:29:42 +00002334 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002335 /*FIME:*/IsaLoc,
John McCalld226f652010-08-21 09:40:31 +00002336 SS, 0, false);
John Wiegley429bb272011-04-08 18:41:53 +00002337 if (Result.isInvalid() || Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00002338 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00002339
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002340 if (Result.get())
2341 return move(Result);
Sean Huntc3021132010-05-05 15:23:54 +00002342
John Wiegley429bb272011-04-08 18:41:53 +00002343 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002344 /*FIXME:*/IsaLoc, IsArrow,
2345 SS, SourceLocation(),
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002346 /*FirstQualifierInScope=*/0,
Sean Huntc3021132010-05-05 15:23:54 +00002347 R,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002348 /*TemplateArgs=*/0);
2349 }
Sean Huntc3021132010-05-05 15:23:54 +00002350
Douglas Gregorb98b1992009-08-11 05:31:07 +00002351 /// \brief Build a new shuffle vector expression.
2352 ///
2353 /// By default, performs semantic analysis to build the new expression.
2354 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002355 ExprResult RebuildShuffleVectorExpr(SourceLocation BuiltinLoc,
John McCallf89e55a2010-11-18 06:31:45 +00002356 MultiExprArg SubExprs,
2357 SourceLocation RParenLoc) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002358 // Find the declaration for __builtin_shufflevector
Mike Stump1eb44332009-09-09 15:08:12 +00002359 const IdentifierInfo &Name
Douglas Gregorb98b1992009-08-11 05:31:07 +00002360 = SemaRef.Context.Idents.get("__builtin_shufflevector");
2361 TranslationUnitDecl *TUDecl = SemaRef.Context.getTranslationUnitDecl();
2362 DeclContext::lookup_result Lookup = TUDecl->lookup(DeclarationName(&Name));
2363 assert(Lookup.first != Lookup.second && "No __builtin_shufflevector?");
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Douglas Gregorb98b1992009-08-11 05:31:07 +00002365 // Build a reference to the __builtin_shufflevector builtin
2366 FunctionDecl *Builtin = cast<FunctionDecl>(*Lookup.first);
John Wiegley429bb272011-04-08 18:41:53 +00002367 ExprResult Callee
2368 = SemaRef.Owned(new (SemaRef.Context) DeclRefExpr(Builtin, Builtin->getType(),
2369 VK_LValue, BuiltinLoc));
2370 Callee = SemaRef.UsualUnaryConversions(Callee.take());
2371 if (Callee.isInvalid())
2372 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00002373
2374 // Build the CallExpr
Douglas Gregorb98b1992009-08-11 05:31:07 +00002375 unsigned NumSubExprs = SubExprs.size();
2376 Expr **Subs = (Expr **)SubExprs.release();
John Wiegley429bb272011-04-08 18:41:53 +00002377 ExprResult TheCall = SemaRef.Owned(
2378 new (SemaRef.Context) CallExpr(SemaRef.Context, Callee.take(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00002379 Subs, NumSubExprs,
Douglas Gregor5291c3c2010-07-13 08:18:22 +00002380 Builtin->getCallResultType(),
John McCallf89e55a2010-11-18 06:31:45 +00002381 Expr::getValueKindForType(Builtin->getResultType()),
John Wiegley429bb272011-04-08 18:41:53 +00002382 RParenLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Douglas Gregorb98b1992009-08-11 05:31:07 +00002384 // Type-check the __builtin_shufflevector expression.
John Wiegley429bb272011-04-08 18:41:53 +00002385 return SemaRef.SemaBuiltinShuffleVector(cast<CallExpr>(TheCall.take()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00002386 }
John McCall43fed0d2010-11-12 08:19:04 +00002387
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002388 /// \brief Build a new template argument pack expansion.
2389 ///
2390 /// By default, performs semantic analysis to build a new pack expansion
2391 /// for a template argument. Subclasses may override this routine to provide
2392 /// different behavior.
2393 TemplateArgumentLoc RebuildPackExpansion(TemplateArgumentLoc Pattern,
Douglas Gregorcded4f62011-01-14 17:04:44 +00002394 SourceLocation EllipsisLoc,
2395 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002396 switch (Pattern.getArgument().getKind()) {
Douglas Gregor7a21fd42011-01-03 21:37:45 +00002397 case TemplateArgument::Expression: {
2398 ExprResult Result
Douglas Gregor67fd1252011-01-14 21:20:45 +00002399 = getSema().CheckPackExpansion(Pattern.getSourceExpression(),
2400 EllipsisLoc, NumExpansions);
Douglas Gregor7a21fd42011-01-03 21:37:45 +00002401 if (Result.isInvalid())
2402 return TemplateArgumentLoc();
2403
2404 return TemplateArgumentLoc(Result.get(), Result.get());
2405 }
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002406
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002407 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002408 return TemplateArgumentLoc(TemplateArgument(
2409 Pattern.getArgument().getAsTemplate(),
Douglas Gregor2be29f42011-01-14 23:41:42 +00002410 NumExpansions),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002411 Pattern.getTemplateQualifierLoc(),
Douglas Gregora7fc9012011-01-05 18:58:31 +00002412 Pattern.getTemplateNameLoc(),
2413 EllipsisLoc);
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002414
2415 case TemplateArgument::Null:
2416 case TemplateArgument::Integral:
2417 case TemplateArgument::Declaration:
2418 case TemplateArgument::Pack:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002419 case TemplateArgument::TemplateExpansion:
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002420 llvm_unreachable("Pack expansion pattern has no parameter packs");
2421
2422 case TemplateArgument::Type:
2423 if (TypeSourceInfo *Expansion
2424 = getSema().CheckPackExpansion(Pattern.getTypeSourceInfo(),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002425 EllipsisLoc,
2426 NumExpansions))
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002427 return TemplateArgumentLoc(TemplateArgument(Expansion->getType()),
2428 Expansion);
2429 break;
2430 }
2431
2432 return TemplateArgumentLoc();
2433 }
2434
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002435 /// \brief Build a new expression pack expansion.
2436 ///
2437 /// By default, performs semantic analysis to build a new pack expansion
2438 /// for an expression. Subclasses may override this routine to provide
2439 /// different behavior.
Douglas Gregor67fd1252011-01-14 21:20:45 +00002440 ExprResult RebuildPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
2441 llvm::Optional<unsigned> NumExpansions) {
2442 return getSema().CheckPackExpansion(Pattern, EllipsisLoc, NumExpansions);
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002443 }
Eli Friedmandfa64ba2011-10-14 22:48:56 +00002444
2445 /// \brief Build a new atomic operation expression.
2446 ///
2447 /// By default, performs semantic analysis to build the new expression.
2448 /// Subclasses may override this routine to provide different behavior.
2449 ExprResult RebuildAtomicExpr(SourceLocation BuiltinLoc,
2450 MultiExprArg SubExprs,
2451 QualType RetTy,
2452 AtomicExpr::AtomicOp Op,
2453 SourceLocation RParenLoc) {
2454 // Just create the expression; there is not any interesting semantic
2455 // analysis here because we can't actually build an AtomicExpr until
2456 // we are sure it is semantically sound.
2457 unsigned NumSubExprs = SubExprs.size();
2458 Expr **Subs = (Expr **)SubExprs.release();
2459 return new (SemaRef.Context) AtomicExpr(BuiltinLoc, Subs,
2460 NumSubExprs, RetTy, Op,
2461 RParenLoc);
2462 }
2463
John McCall43fed0d2010-11-12 08:19:04 +00002464private:
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002465 TypeLoc TransformTypeInObjectScope(TypeLoc TL,
2466 QualType ObjectType,
2467 NamedDecl *FirstQualifierInScope,
2468 CXXScopeSpec &SS);
Douglas Gregorb71d8212011-03-02 18:32:08 +00002469
2470 TypeSourceInfo *TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
2471 QualType ObjectType,
2472 NamedDecl *FirstQualifierInScope,
2473 CXXScopeSpec &SS);
Douglas Gregor577f75a2009-08-04 16:50:30 +00002474};
Douglas Gregorb98b1992009-08-11 05:31:07 +00002475
Douglas Gregor43959a92009-08-20 07:17:43 +00002476template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00002477StmtResult TreeTransform<Derived>::TransformStmt(Stmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00002478 if (!S)
2479 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00002480
Douglas Gregor43959a92009-08-20 07:17:43 +00002481 switch (S->getStmtClass()) {
2482 case Stmt::NoStmtClass: break;
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Douglas Gregor43959a92009-08-20 07:17:43 +00002484 // Transform individual statement nodes
2485#define STMT(Node, Parent) \
2486 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(S));
John McCall63c00d72011-02-09 08:16:59 +00002487#define ABSTRACT_STMT(Node)
Douglas Gregor43959a92009-08-20 07:17:43 +00002488#define EXPR(Node, Parent)
Sean Hunt4bfe1962010-05-05 15:24:00 +00002489#include "clang/AST/StmtNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Douglas Gregor43959a92009-08-20 07:17:43 +00002491 // Transform expressions by calling TransformExpr.
2492#define STMT(Node, Parent)
Sean Hunt7381d5c2010-05-18 06:22:21 +00002493#define ABSTRACT_STMT(Stmt)
Douglas Gregor43959a92009-08-20 07:17:43 +00002494#define EXPR(Node, Parent) case Stmt::Node##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002495#include "clang/AST/StmtNodes.inc"
Douglas Gregor43959a92009-08-20 07:17:43 +00002496 {
John McCall60d7b3a2010-08-24 06:29:42 +00002497 ExprResult E = getDerived().TransformExpr(cast<Expr>(S));
Douglas Gregor43959a92009-08-20 07:17:43 +00002498 if (E.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00002499 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00002500
John McCall9ae2f072010-08-23 23:25:46 +00002501 return getSema().ActOnExprStmt(getSema().MakeFullExpr(E.take()));
Douglas Gregor43959a92009-08-20 07:17:43 +00002502 }
Mike Stump1eb44332009-09-09 15:08:12 +00002503 }
2504
John McCall3fa5cae2010-10-26 07:05:15 +00002505 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00002506}
Mike Stump1eb44332009-09-09 15:08:12 +00002507
2508
Douglas Gregor670444e2009-08-04 22:27:00 +00002509template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00002510ExprResult TreeTransform<Derived>::TransformExpr(Expr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002511 if (!E)
2512 return SemaRef.Owned(E);
2513
2514 switch (E->getStmtClass()) {
2515 case Stmt::NoStmtClass: break;
2516#define STMT(Node, Parent) case Stmt::Node##Class: break;
Sean Hunt7381d5c2010-05-18 06:22:21 +00002517#define ABSTRACT_STMT(Stmt)
Douglas Gregorb98b1992009-08-11 05:31:07 +00002518#define EXPR(Node, Parent) \
John McCall454feb92009-12-08 09:21:05 +00002519 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(E));
Sean Hunt4bfe1962010-05-05 15:24:00 +00002520#include "clang/AST/StmtNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +00002521 }
2522
John McCall3fa5cae2010-10-26 07:05:15 +00002523 return SemaRef.Owned(E);
Douglas Gregor657c1ac2009-08-06 22:17:10 +00002524}
2525
2526template<typename Derived>
Douglas Gregoraa165f82011-01-03 19:04:46 +00002527bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
2528 unsigned NumInputs,
2529 bool IsCall,
Chris Lattner686775d2011-07-20 06:58:45 +00002530 SmallVectorImpl<Expr *> &Outputs,
Douglas Gregoraa165f82011-01-03 19:04:46 +00002531 bool *ArgChanged) {
2532 for (unsigned I = 0; I != NumInputs; ++I) {
2533 // If requested, drop call arguments that need to be dropped.
2534 if (IsCall && getDerived().DropCallArgument(Inputs[I])) {
2535 if (ArgChanged)
2536 *ArgChanged = true;
2537
2538 break;
2539 }
2540
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002541 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(Inputs[I])) {
2542 Expr *Pattern = Expansion->getPattern();
2543
Chris Lattner686775d2011-07-20 06:58:45 +00002544 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002545 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
2546 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
2547
2548 // Determine whether the set of unexpanded parameter packs can and should
2549 // be expanded.
2550 bool Expand = true;
Douglas Gregord3731192011-01-10 07:32:04 +00002551 bool RetainExpansion = false;
Douglas Gregor67fd1252011-01-14 21:20:45 +00002552 llvm::Optional<unsigned> OrigNumExpansions
2553 = Expansion->getNumExpansions();
2554 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002555 if (getDerived().TryExpandParameterPacks(Expansion->getEllipsisLoc(),
2556 Pattern->getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00002557 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00002558 Expand, RetainExpansion,
2559 NumExpansions))
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002560 return true;
2561
2562 if (!Expand) {
2563 // The transform has determined that we should perform a simple
2564 // transformation on the pack expansion, producing another pack
2565 // expansion.
2566 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
2567 ExprResult OutPattern = getDerived().TransformExpr(Pattern);
2568 if (OutPattern.isInvalid())
2569 return true;
2570
2571 ExprResult Out = getDerived().RebuildPackExpansion(OutPattern.get(),
Douglas Gregor67fd1252011-01-14 21:20:45 +00002572 Expansion->getEllipsisLoc(),
2573 NumExpansions);
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002574 if (Out.isInvalid())
2575 return true;
2576
2577 if (ArgChanged)
2578 *ArgChanged = true;
2579 Outputs.push_back(Out.get());
2580 continue;
2581 }
John McCallc8fc90a2011-07-06 07:30:07 +00002582
2583 // Record right away that the argument was changed. This needs
2584 // to happen even if the array expands to nothing.
2585 if (ArgChanged) *ArgChanged = true;
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002586
2587 // The transform has determined that we should perform an elementwise
2588 // expansion of the pattern. Do so.
Douglas Gregorcded4f62011-01-14 17:04:44 +00002589 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002590 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
2591 ExprResult Out = getDerived().TransformExpr(Pattern);
2592 if (Out.isInvalid())
2593 return true;
2594
Douglas Gregor77d6bb92011-01-11 22:21:24 +00002595 if (Out.get()->containsUnexpandedParameterPack()) {
Douglas Gregor67fd1252011-01-14 21:20:45 +00002596 Out = RebuildPackExpansion(Out.get(), Expansion->getEllipsisLoc(),
2597 OrigNumExpansions);
Douglas Gregor77d6bb92011-01-11 22:21:24 +00002598 if (Out.isInvalid())
2599 return true;
2600 }
2601
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002602 Outputs.push_back(Out.get());
2603 }
2604
2605 continue;
2606 }
2607
Douglas Gregoraa165f82011-01-03 19:04:46 +00002608 ExprResult Result = getDerived().TransformExpr(Inputs[I]);
2609 if (Result.isInvalid())
2610 return true;
2611
2612 if (Result.get() != Inputs[I] && ArgChanged)
2613 *ArgChanged = true;
2614
2615 Outputs.push_back(Result.get());
2616 }
2617
2618 return false;
2619}
2620
2621template<typename Derived>
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002622NestedNameSpecifierLoc
2623TreeTransform<Derived>::TransformNestedNameSpecifierLoc(
2624 NestedNameSpecifierLoc NNS,
2625 QualType ObjectType,
2626 NamedDecl *FirstQualifierInScope) {
Chris Lattner686775d2011-07-20 06:58:45 +00002627 SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002628 for (NestedNameSpecifierLoc Qualifier = NNS; Qualifier;
2629 Qualifier = Qualifier.getPrefix())
2630 Qualifiers.push_back(Qualifier);
2631
2632 CXXScopeSpec SS;
2633 while (!Qualifiers.empty()) {
2634 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
2635 NestedNameSpecifier *QNNS = Q.getNestedNameSpecifier();
2636
2637 switch (QNNS->getKind()) {
2638 case NestedNameSpecifier::Identifier:
2639 if (SemaRef.BuildCXXNestedNameSpecifier(/*Scope=*/0,
2640 *QNNS->getAsIdentifier(),
2641 Q.getLocalBeginLoc(),
2642 Q.getLocalEndLoc(),
2643 ObjectType, false, SS,
2644 FirstQualifierInScope, false))
2645 return NestedNameSpecifierLoc();
2646
2647 break;
2648
2649 case NestedNameSpecifier::Namespace: {
2650 NamespaceDecl *NS
2651 = cast_or_null<NamespaceDecl>(
2652 getDerived().TransformDecl(
2653 Q.getLocalBeginLoc(),
2654 QNNS->getAsNamespace()));
2655 SS.Extend(SemaRef.Context, NS, Q.getLocalBeginLoc(), Q.getLocalEndLoc());
2656 break;
2657 }
2658
2659 case NestedNameSpecifier::NamespaceAlias: {
2660 NamespaceAliasDecl *Alias
2661 = cast_or_null<NamespaceAliasDecl>(
2662 getDerived().TransformDecl(Q.getLocalBeginLoc(),
2663 QNNS->getAsNamespaceAlias()));
2664 SS.Extend(SemaRef.Context, Alias, Q.getLocalBeginLoc(),
2665 Q.getLocalEndLoc());
2666 break;
2667 }
2668
2669 case NestedNameSpecifier::Global:
2670 // There is no meaningful transformation that one could perform on the
2671 // global scope.
2672 SS.MakeGlobal(SemaRef.Context, Q.getBeginLoc());
2673 break;
2674
2675 case NestedNameSpecifier::TypeSpecWithTemplate:
2676 case NestedNameSpecifier::TypeSpec: {
2677 TypeLoc TL = TransformTypeInObjectScope(Q.getTypeLoc(), ObjectType,
2678 FirstQualifierInScope, SS);
2679
2680 if (!TL)
2681 return NestedNameSpecifierLoc();
2682
2683 if (TL.getType()->isDependentType() || TL.getType()->isRecordType() ||
2684 (SemaRef.getLangOptions().CPlusPlus0x &&
2685 TL.getType()->isEnumeralType())) {
2686 assert(!TL.getType().hasLocalQualifiers() &&
2687 "Can't get cv-qualifiers here");
Richard Smith95aafb22011-10-20 03:28:47 +00002688 if (TL.getType()->isEnumeralType())
2689 SemaRef.Diag(TL.getBeginLoc(),
2690 diag::warn_cxx98_compat_enum_nested_name_spec);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002691 SS.Extend(SemaRef.Context, /*FIXME:*/SourceLocation(), TL,
2692 Q.getLocalEndLoc());
2693 break;
2694 }
Richard Trieu00c93a12011-05-07 01:36:37 +00002695 // If the nested-name-specifier is an invalid type def, don't emit an
2696 // error because a previous error should have already been emitted.
2697 TypedefTypeLoc* TTL = dyn_cast<TypedefTypeLoc>(&TL);
2698 if (!TTL || !TTL->getTypedefNameDecl()->isInvalidDecl()) {
2699 SemaRef.Diag(TL.getBeginLoc(), diag::err_nested_name_spec_non_tag)
2700 << TL.getType() << SS.getRange();
2701 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002702 return NestedNameSpecifierLoc();
2703 }
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002704 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002705
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002706 // The qualifier-in-scope and object type only apply to the leftmost entity.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002707 FirstQualifierInScope = 0;
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002708 ObjectType = QualType();
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002709 }
2710
2711 // Don't rebuild the nested-name-specifier if we don't have to.
2712 if (SS.getScopeRep() == NNS.getNestedNameSpecifier() &&
2713 !getDerived().AlwaysRebuild())
2714 return NNS;
2715
2716 // If we can re-use the source-location data from the original
2717 // nested-name-specifier, do so.
2718 if (SS.location_size() == NNS.getDataLength() &&
2719 memcmp(SS.location_data(), NNS.getOpaqueData(), SS.location_size()) == 0)
2720 return NestedNameSpecifierLoc(SS.getScopeRep(), NNS.getOpaqueData());
2721
2722 // Allocate new nested-name-specifier location information.
2723 return SS.getWithLocInContext(SemaRef.Context);
2724}
2725
2726template<typename Derived>
Abramo Bagnara25777432010-08-11 22:01:17 +00002727DeclarationNameInfo
2728TreeTransform<Derived>
John McCall43fed0d2010-11-12 08:19:04 +00002729::TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002730 DeclarationName Name = NameInfo.getName();
Douglas Gregor81499bb2009-09-03 22:13:48 +00002731 if (!Name)
Abramo Bagnara25777432010-08-11 22:01:17 +00002732 return DeclarationNameInfo();
Douglas Gregor81499bb2009-09-03 22:13:48 +00002733
2734 switch (Name.getNameKind()) {
2735 case DeclarationName::Identifier:
2736 case DeclarationName::ObjCZeroArgSelector:
2737 case DeclarationName::ObjCOneArgSelector:
2738 case DeclarationName::ObjCMultiArgSelector:
2739 case DeclarationName::CXXOperatorName:
Sean Hunt3e518bd2009-11-29 07:34:05 +00002740 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor81499bb2009-09-03 22:13:48 +00002741 case DeclarationName::CXXUsingDirective:
Abramo Bagnara25777432010-08-11 22:01:17 +00002742 return NameInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00002743
Douglas Gregor81499bb2009-09-03 22:13:48 +00002744 case DeclarationName::CXXConstructorName:
2745 case DeclarationName::CXXDestructorName:
2746 case DeclarationName::CXXConversionFunctionName: {
Abramo Bagnara25777432010-08-11 22:01:17 +00002747 TypeSourceInfo *NewTInfo;
2748 CanQualType NewCanTy;
2749 if (TypeSourceInfo *OldTInfo = NameInfo.getNamedTypeInfo()) {
John McCall43fed0d2010-11-12 08:19:04 +00002750 NewTInfo = getDerived().TransformType(OldTInfo);
2751 if (!NewTInfo)
2752 return DeclarationNameInfo();
2753 NewCanTy = SemaRef.Context.getCanonicalType(NewTInfo->getType());
Abramo Bagnara25777432010-08-11 22:01:17 +00002754 }
2755 else {
2756 NewTInfo = 0;
2757 TemporaryBase Rebase(*this, NameInfo.getLoc(), Name);
John McCall43fed0d2010-11-12 08:19:04 +00002758 QualType NewT = getDerived().TransformType(Name.getCXXNameType());
Abramo Bagnara25777432010-08-11 22:01:17 +00002759 if (NewT.isNull())
2760 return DeclarationNameInfo();
2761 NewCanTy = SemaRef.Context.getCanonicalType(NewT);
2762 }
Mike Stump1eb44332009-09-09 15:08:12 +00002763
Abramo Bagnara25777432010-08-11 22:01:17 +00002764 DeclarationName NewName
2765 = SemaRef.Context.DeclarationNames.getCXXSpecialName(Name.getNameKind(),
2766 NewCanTy);
2767 DeclarationNameInfo NewNameInfo(NameInfo);
2768 NewNameInfo.setName(NewName);
2769 NewNameInfo.setNamedTypeInfo(NewTInfo);
2770 return NewNameInfo;
Douglas Gregor81499bb2009-09-03 22:13:48 +00002771 }
Mike Stump1eb44332009-09-09 15:08:12 +00002772 }
2773
David Blaikieb219cfc2011-09-23 05:06:16 +00002774 llvm_unreachable("Unknown name kind.");
Douglas Gregor81499bb2009-09-03 22:13:48 +00002775}
2776
2777template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00002778TemplateName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00002779TreeTransform<Derived>::TransformTemplateName(CXXScopeSpec &SS,
2780 TemplateName Name,
2781 SourceLocation NameLoc,
2782 QualType ObjectType,
2783 NamedDecl *FirstQualifierInScope) {
2784 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
2785 TemplateDecl *Template = QTN->getTemplateDecl();
2786 assert(Template && "qualified template name must refer to a template");
2787
2788 TemplateDecl *TransTemplate
2789 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
2790 Template));
2791 if (!TransTemplate)
2792 return TemplateName();
2793
2794 if (!getDerived().AlwaysRebuild() &&
2795 SS.getScopeRep() == QTN->getQualifier() &&
2796 TransTemplate == Template)
2797 return Name;
2798
2799 return getDerived().RebuildTemplateName(SS, QTN->hasTemplateKeyword(),
2800 TransTemplate);
2801 }
2802
2803 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2804 if (SS.getScopeRep()) {
2805 // These apply to the scope specifier, not the template.
2806 ObjectType = QualType();
2807 FirstQualifierInScope = 0;
2808 }
2809
2810 if (!getDerived().AlwaysRebuild() &&
2811 SS.getScopeRep() == DTN->getQualifier() &&
2812 ObjectType.isNull())
2813 return Name;
2814
2815 if (DTN->isIdentifier()) {
2816 return getDerived().RebuildTemplateName(SS,
2817 *DTN->getIdentifier(),
2818 NameLoc,
2819 ObjectType,
2820 FirstQualifierInScope);
2821 }
2822
2823 return getDerived().RebuildTemplateName(SS, DTN->getOperator(), NameLoc,
2824 ObjectType);
2825 }
2826
2827 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2828 TemplateDecl *TransTemplate
2829 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
2830 Template));
2831 if (!TransTemplate)
2832 return TemplateName();
2833
2834 if (!getDerived().AlwaysRebuild() &&
2835 TransTemplate == Template)
2836 return Name;
2837
2838 return TemplateName(TransTemplate);
2839 }
2840
2841 if (SubstTemplateTemplateParmPackStorage *SubstPack
2842 = Name.getAsSubstTemplateTemplateParmPack()) {
2843 TemplateTemplateParmDecl *TransParam
2844 = cast_or_null<TemplateTemplateParmDecl>(
2845 getDerived().TransformDecl(NameLoc, SubstPack->getParameterPack()));
2846 if (!TransParam)
2847 return TemplateName();
2848
2849 if (!getDerived().AlwaysRebuild() &&
2850 TransParam == SubstPack->getParameterPack())
2851 return Name;
2852
2853 return getDerived().RebuildTemplateName(TransParam,
2854 SubstPack->getArgumentPack());
2855 }
2856
2857 // These should be getting filtered out before they reach the AST.
2858 llvm_unreachable("overloaded function decl survived to here");
2859 return TemplateName();
2860}
2861
2862template<typename Derived>
John McCall833ca992009-10-29 08:12:44 +00002863void TreeTransform<Derived>::InventTemplateArgumentLoc(
2864 const TemplateArgument &Arg,
2865 TemplateArgumentLoc &Output) {
2866 SourceLocation Loc = getDerived().getBaseLocation();
2867 switch (Arg.getKind()) {
2868 case TemplateArgument::Null:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002869 llvm_unreachable("null template argument in TreeTransform");
John McCall833ca992009-10-29 08:12:44 +00002870 break;
2871
2872 case TemplateArgument::Type:
2873 Output = TemplateArgumentLoc(Arg,
John McCalla93c9342009-12-07 02:54:59 +00002874 SemaRef.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
Sean Huntc3021132010-05-05 15:23:54 +00002875
John McCall833ca992009-10-29 08:12:44 +00002876 break;
2877
Douglas Gregor788cd062009-11-11 01:00:40 +00002878 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002879 case TemplateArgument::TemplateExpansion: {
2880 NestedNameSpecifierLocBuilder Builder;
2881 TemplateName Template = Arg.getAsTemplate();
2882 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
2883 Builder.MakeTrivial(SemaRef.Context, DTN->getQualifier(), Loc);
2884 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2885 Builder.MakeTrivial(SemaRef.Context, QTN->getQualifier(), Loc);
2886
2887 if (Arg.getKind() == TemplateArgument::Template)
2888 Output = TemplateArgumentLoc(Arg,
2889 Builder.getWithLocInContext(SemaRef.Context),
2890 Loc);
2891 else
2892 Output = TemplateArgumentLoc(Arg,
2893 Builder.getWithLocInContext(SemaRef.Context),
2894 Loc, Loc);
2895
Douglas Gregor788cd062009-11-11 01:00:40 +00002896 break;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002897 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00002898
John McCall833ca992009-10-29 08:12:44 +00002899 case TemplateArgument::Expression:
2900 Output = TemplateArgumentLoc(Arg, Arg.getAsExpr());
2901 break;
2902
2903 case TemplateArgument::Declaration:
2904 case TemplateArgument::Integral:
2905 case TemplateArgument::Pack:
John McCall828bff22009-10-29 18:45:58 +00002906 Output = TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002907 break;
2908 }
2909}
2910
2911template<typename Derived>
2912bool TreeTransform<Derived>::TransformTemplateArgument(
2913 const TemplateArgumentLoc &Input,
2914 TemplateArgumentLoc &Output) {
2915 const TemplateArgument &Arg = Input.getArgument();
Douglas Gregor670444e2009-08-04 22:27:00 +00002916 switch (Arg.getKind()) {
2917 case TemplateArgument::Null:
2918 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002919 Output = Input;
2920 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002921
Douglas Gregor670444e2009-08-04 22:27:00 +00002922 case TemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +00002923 TypeSourceInfo *DI = Input.getTypeSourceInfo();
John McCall833ca992009-10-29 08:12:44 +00002924 if (DI == NULL)
John McCalla93c9342009-12-07 02:54:59 +00002925 DI = InventTypeSourceInfo(Input.getArgument().getAsType());
John McCall833ca992009-10-29 08:12:44 +00002926
2927 DI = getDerived().TransformType(DI);
2928 if (!DI) return true;
2929
2930 Output = TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2931 return false;
Douglas Gregor670444e2009-08-04 22:27:00 +00002932 }
Mike Stump1eb44332009-09-09 15:08:12 +00002933
Douglas Gregor670444e2009-08-04 22:27:00 +00002934 case TemplateArgument::Declaration: {
John McCall833ca992009-10-29 08:12:44 +00002935 // FIXME: we should never have to transform one of these.
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002936 DeclarationName Name;
2937 if (NamedDecl *ND = dyn_cast<NamedDecl>(Arg.getAsDecl()))
2938 Name = ND->getDeclName();
Douglas Gregor788cd062009-11-11 01:00:40 +00002939 TemporaryBase Rebase(*this, Input.getLocation(), Name);
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002940 Decl *D = getDerived().TransformDecl(Input.getLocation(), Arg.getAsDecl());
John McCall833ca992009-10-29 08:12:44 +00002941 if (!D) return true;
2942
John McCall828bff22009-10-29 18:45:58 +00002943 Expr *SourceExpr = Input.getSourceDeclExpression();
2944 if (SourceExpr) {
2945 EnterExpressionEvaluationContext Unevaluated(getSema(),
Richard Smithf6702a32011-12-20 02:08:33 +00002946 Sema::ConstantEvaluated);
John McCall60d7b3a2010-08-24 06:29:42 +00002947 ExprResult E = getDerived().TransformExpr(SourceExpr);
John McCall9ae2f072010-08-23 23:25:46 +00002948 SourceExpr = (E.isInvalid() ? 0 : E.take());
John McCall828bff22009-10-29 18:45:58 +00002949 }
2950
2951 Output = TemplateArgumentLoc(TemplateArgument(D), SourceExpr);
John McCall833ca992009-10-29 08:12:44 +00002952 return false;
Douglas Gregor670444e2009-08-04 22:27:00 +00002953 }
Mike Stump1eb44332009-09-09 15:08:12 +00002954
Douglas Gregor788cd062009-11-11 01:00:40 +00002955 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002956 NestedNameSpecifierLoc QualifierLoc = Input.getTemplateQualifierLoc();
2957 if (QualifierLoc) {
2958 QualifierLoc = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc);
2959 if (!QualifierLoc)
2960 return true;
2961 }
2962
Douglas Gregor1d752d72011-03-02 18:46:51 +00002963 CXXScopeSpec SS;
2964 SS.Adopt(QualifierLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00002965 TemplateName Template
Douglas Gregor1d752d72011-03-02 18:46:51 +00002966 = getDerived().TransformTemplateName(SS, Arg.getAsTemplate(),
2967 Input.getTemplateNameLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +00002968 if (Template.isNull())
2969 return true;
Sean Huntc3021132010-05-05 15:23:54 +00002970
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002971 Output = TemplateArgumentLoc(TemplateArgument(Template), QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002972 Input.getTemplateNameLoc());
2973 return false;
2974 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00002975
2976 case TemplateArgument::TemplateExpansion:
2977 llvm_unreachable("Caller should expand pack expansions");
2978
Douglas Gregor670444e2009-08-04 22:27:00 +00002979 case TemplateArgument::Expression: {
Richard Smithf6702a32011-12-20 02:08:33 +00002980 // Template argument expressions are constant expressions.
Mike Stump1eb44332009-09-09 15:08:12 +00002981 EnterExpressionEvaluationContext Unevaluated(getSema(),
Richard Smithf6702a32011-12-20 02:08:33 +00002982 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00002983
John McCall833ca992009-10-29 08:12:44 +00002984 Expr *InputExpr = Input.getSourceExpression();
2985 if (!InputExpr) InputExpr = Input.getArgument().getAsExpr();
2986
Chris Lattner223de242011-04-25 20:37:58 +00002987 ExprResult E = getDerived().TransformExpr(InputExpr);
John McCall833ca992009-10-29 08:12:44 +00002988 if (E.isInvalid()) return true;
John McCall9ae2f072010-08-23 23:25:46 +00002989 Output = TemplateArgumentLoc(TemplateArgument(E.take()), E.take());
John McCall833ca992009-10-29 08:12:44 +00002990 return false;
Douglas Gregor670444e2009-08-04 22:27:00 +00002991 }
Mike Stump1eb44332009-09-09 15:08:12 +00002992
Douglas Gregor670444e2009-08-04 22:27:00 +00002993 case TemplateArgument::Pack: {
Chris Lattner686775d2011-07-20 06:58:45 +00002994 SmallVector<TemplateArgument, 4> TransformedArgs;
Douglas Gregor670444e2009-08-04 22:27:00 +00002995 TransformedArgs.reserve(Arg.pack_size());
Mike Stump1eb44332009-09-09 15:08:12 +00002996 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor670444e2009-08-04 22:27:00 +00002997 AEnd = Arg.pack_end();
2998 A != AEnd; ++A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002999
John McCall833ca992009-10-29 08:12:44 +00003000 // FIXME: preserve source information here when we start
3001 // caring about parameter packs.
3002
John McCall828bff22009-10-29 18:45:58 +00003003 TemplateArgumentLoc InputArg;
3004 TemplateArgumentLoc OutputArg;
3005 getDerived().InventTemplateArgumentLoc(*A, InputArg);
3006 if (getDerived().TransformTemplateArgument(InputArg, OutputArg))
John McCall833ca992009-10-29 08:12:44 +00003007 return true;
3008
John McCall828bff22009-10-29 18:45:58 +00003009 TransformedArgs.push_back(OutputArg.getArgument());
Douglas Gregor670444e2009-08-04 22:27:00 +00003010 }
Douglas Gregor910f8002010-11-07 23:05:16 +00003011
3012 TemplateArgument *TransformedArgsPtr
3013 = new (getSema().Context) TemplateArgument[TransformedArgs.size()];
3014 std::copy(TransformedArgs.begin(), TransformedArgs.end(),
3015 TransformedArgsPtr);
3016 Output = TemplateArgumentLoc(TemplateArgument(TransformedArgsPtr,
3017 TransformedArgs.size()),
3018 Input.getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00003019 return false;
Douglas Gregor670444e2009-08-04 22:27:00 +00003020 }
3021 }
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Douglas Gregor670444e2009-08-04 22:27:00 +00003023 // Work around bogus GCC warning
John McCall833ca992009-10-29 08:12:44 +00003024 return true;
Douglas Gregor670444e2009-08-04 22:27:00 +00003025}
3026
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003027/// \brief Iterator adaptor that invents template argument location information
3028/// for each of the template arguments in its underlying iterator.
3029template<typename Derived, typename InputIterator>
3030class TemplateArgumentLocInventIterator {
3031 TreeTransform<Derived> &Self;
3032 InputIterator Iter;
3033
3034public:
3035 typedef TemplateArgumentLoc value_type;
3036 typedef TemplateArgumentLoc reference;
3037 typedef typename std::iterator_traits<InputIterator>::difference_type
3038 difference_type;
3039 typedef std::input_iterator_tag iterator_category;
3040
3041 class pointer {
3042 TemplateArgumentLoc Arg;
Douglas Gregorfcc12532010-12-20 17:31:10 +00003043
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003044 public:
3045 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
3046
3047 const TemplateArgumentLoc *operator->() const { return &Arg; }
3048 };
3049
3050 TemplateArgumentLocInventIterator() { }
3051
3052 explicit TemplateArgumentLocInventIterator(TreeTransform<Derived> &Self,
3053 InputIterator Iter)
3054 : Self(Self), Iter(Iter) { }
3055
3056 TemplateArgumentLocInventIterator &operator++() {
3057 ++Iter;
3058 return *this;
Douglas Gregorfcc12532010-12-20 17:31:10 +00003059 }
3060
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003061 TemplateArgumentLocInventIterator operator++(int) {
3062 TemplateArgumentLocInventIterator Old(*this);
3063 ++(*this);
3064 return Old;
3065 }
3066
3067 reference operator*() const {
3068 TemplateArgumentLoc Result;
3069 Self.InventTemplateArgumentLoc(*Iter, Result);
3070 return Result;
3071 }
3072
3073 pointer operator->() const { return pointer(**this); }
3074
3075 friend bool operator==(const TemplateArgumentLocInventIterator &X,
3076 const TemplateArgumentLocInventIterator &Y) {
3077 return X.Iter == Y.Iter;
3078 }
Douglas Gregorfcc12532010-12-20 17:31:10 +00003079
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003080 friend bool operator!=(const TemplateArgumentLocInventIterator &X,
3081 const TemplateArgumentLocInventIterator &Y) {
3082 return X.Iter != Y.Iter;
3083 }
3084};
3085
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00003086template<typename Derived>
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003087template<typename InputIterator>
3088bool TreeTransform<Derived>::TransformTemplateArguments(InputIterator First,
3089 InputIterator Last,
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00003090 TemplateArgumentListInfo &Outputs) {
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003091 for (; First != Last; ++First) {
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00003092 TemplateArgumentLoc Out;
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003093 TemplateArgumentLoc In = *First;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003094
3095 if (In.getArgument().getKind() == TemplateArgument::Pack) {
3096 // Unpack argument packs, which we translate them into separate
3097 // arguments.
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003098 // FIXME: We could do much better if we could guarantee that the
3099 // TemplateArgumentLocInfo for the pack expansion would be usable for
3100 // all of the template arguments in the argument pack.
3101 typedef TemplateArgumentLocInventIterator<Derived,
3102 TemplateArgument::pack_iterator>
3103 PackLocIterator;
3104 if (TransformTemplateArguments(PackLocIterator(*this,
3105 In.getArgument().pack_begin()),
3106 PackLocIterator(*this,
3107 In.getArgument().pack_end()),
3108 Outputs))
3109 return true;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003110
3111 continue;
3112 }
3113
3114 if (In.getArgument().isPackExpansion()) {
3115 // We have a pack expansion, for which we will be substituting into
3116 // the pattern.
3117 SourceLocation Ellipsis;
Douglas Gregorcded4f62011-01-14 17:04:44 +00003118 llvm::Optional<unsigned> OrigNumExpansions;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003119 TemplateArgumentLoc Pattern
Douglas Gregorcded4f62011-01-14 17:04:44 +00003120 = In.getPackExpansionPattern(Ellipsis, OrigNumExpansions,
3121 getSema().Context);
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003122
Chris Lattner686775d2011-07-20 06:58:45 +00003123 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003124 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
3125 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
3126
3127 // Determine whether the set of unexpanded parameter packs can and should
3128 // be expanded.
3129 bool Expand = true;
Douglas Gregord3731192011-01-10 07:32:04 +00003130 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00003131 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003132 if (getDerived().TryExpandParameterPacks(Ellipsis,
3133 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003134 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00003135 Expand,
3136 RetainExpansion,
3137 NumExpansions))
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003138 return true;
3139
3140 if (!Expand) {
3141 // The transform has determined that we should perform a simple
3142 // transformation on the pack expansion, producing another pack
3143 // expansion.
3144 TemplateArgumentLoc OutPattern;
3145 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
3146 if (getDerived().TransformTemplateArgument(Pattern, OutPattern))
3147 return true;
3148
Douglas Gregorcded4f62011-01-14 17:04:44 +00003149 Out = getDerived().RebuildPackExpansion(OutPattern, Ellipsis,
3150 NumExpansions);
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003151 if (Out.getArgument().isNull())
3152 return true;
3153
3154 Outputs.addArgument(Out);
3155 continue;
3156 }
3157
3158 // The transform has determined that we should perform an elementwise
3159 // expansion of the pattern. Do so.
Douglas Gregorcded4f62011-01-14 17:04:44 +00003160 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003161 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
3162
3163 if (getDerived().TransformTemplateArgument(Pattern, Out))
3164 return true;
3165
Douglas Gregor77d6bb92011-01-11 22:21:24 +00003166 if (Out.getArgument().containsUnexpandedParameterPack()) {
Douglas Gregorcded4f62011-01-14 17:04:44 +00003167 Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
3168 OrigNumExpansions);
Douglas Gregor77d6bb92011-01-11 22:21:24 +00003169 if (Out.getArgument().isNull())
3170 return true;
3171 }
3172
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003173 Outputs.addArgument(Out);
3174 }
3175
Douglas Gregor3cae5c92011-01-10 20:53:55 +00003176 // If we're supposed to retain a pack expansion, do so by temporarily
3177 // forgetting the partially-substituted parameter pack.
3178 if (RetainExpansion) {
3179 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
3180
3181 if (getDerived().TransformTemplateArgument(Pattern, Out))
3182 return true;
3183
Douglas Gregorcded4f62011-01-14 17:04:44 +00003184 Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
3185 OrigNumExpansions);
Douglas Gregor3cae5c92011-01-10 20:53:55 +00003186 if (Out.getArgument().isNull())
3187 return true;
3188
3189 Outputs.addArgument(Out);
3190 }
Douglas Gregord3731192011-01-10 07:32:04 +00003191
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003192 continue;
3193 }
3194
3195 // The simple case:
3196 if (getDerived().TransformTemplateArgument(In, Out))
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00003197 return true;
3198
3199 Outputs.addArgument(Out);
3200 }
3201
3202 return false;
3203
3204}
3205
Douglas Gregor577f75a2009-08-04 16:50:30 +00003206//===----------------------------------------------------------------------===//
3207// Type transformation
3208//===----------------------------------------------------------------------===//
3209
3210template<typename Derived>
John McCall43fed0d2010-11-12 08:19:04 +00003211QualType TreeTransform<Derived>::TransformType(QualType T) {
Douglas Gregor577f75a2009-08-04 16:50:30 +00003212 if (getDerived().AlreadyTransformed(T))
3213 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00003214
John McCalla2becad2009-10-21 00:40:46 +00003215 // Temporary workaround. All of these transformations should
3216 // eventually turn into transformations on TypeLocs.
Douglas Gregorc21c7e92011-01-25 19:13:18 +00003217 TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T,
3218 getDerived().getBaseLocation());
Sean Huntc3021132010-05-05 15:23:54 +00003219
John McCall43fed0d2010-11-12 08:19:04 +00003220 TypeSourceInfo *NewDI = getDerived().TransformType(DI);
John McCall0953e762009-09-24 19:53:00 +00003221
John McCalla2becad2009-10-21 00:40:46 +00003222 if (!NewDI)
3223 return QualType();
3224
3225 return NewDI->getType();
3226}
3227
3228template<typename Derived>
John McCall43fed0d2010-11-12 08:19:04 +00003229TypeSourceInfo *TreeTransform<Derived>::TransformType(TypeSourceInfo *DI) {
Richard Smithf6702a32011-12-20 02:08:33 +00003230 // Refine the base location to the type's location.
3231 TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(),
3232 getDerived().getBaseEntity());
John McCalla2becad2009-10-21 00:40:46 +00003233 if (getDerived().AlreadyTransformed(DI->getType()))
3234 return DI;
3235
3236 TypeLocBuilder TLB;
3237
3238 TypeLoc TL = DI->getTypeLoc();
3239 TLB.reserve(TL.getFullDataSize());
3240
John McCall43fed0d2010-11-12 08:19:04 +00003241 QualType Result = getDerived().TransformType(TLB, TL);
John McCalla2becad2009-10-21 00:40:46 +00003242 if (Result.isNull())
3243 return 0;
3244
John McCalla93c9342009-12-07 02:54:59 +00003245 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
John McCalla2becad2009-10-21 00:40:46 +00003246}
3247
3248template<typename Derived>
3249QualType
John McCall43fed0d2010-11-12 08:19:04 +00003250TreeTransform<Derived>::TransformType(TypeLocBuilder &TLB, TypeLoc T) {
John McCalla2becad2009-10-21 00:40:46 +00003251 switch (T.getTypeLocClass()) {
3252#define ABSTRACT_TYPELOC(CLASS, PARENT)
3253#define TYPELOC(CLASS, PARENT) \
3254 case TypeLoc::CLASS: \
John McCall43fed0d2010-11-12 08:19:04 +00003255 return getDerived().Transform##CLASS##Type(TLB, cast<CLASS##TypeLoc>(T));
John McCalla2becad2009-10-21 00:40:46 +00003256#include "clang/AST/TypeLocNodes.def"
Douglas Gregor577f75a2009-08-04 16:50:30 +00003257 }
Mike Stump1eb44332009-09-09 15:08:12 +00003258
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003259 llvm_unreachable("unhandled type loc!");
John McCalla2becad2009-10-21 00:40:46 +00003260 return QualType();
3261}
3262
3263/// FIXME: By default, this routine adds type qualifiers only to types
3264/// that can have qualifiers, and silently suppresses those qualifiers
3265/// that are not permitted (e.g., qualifiers on reference or function
3266/// types). This is the right thing for template instantiation, but
3267/// probably not for other clients.
3268template<typename Derived>
3269QualType
3270TreeTransform<Derived>::TransformQualifiedType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003271 QualifiedTypeLoc T) {
Douglas Gregora4923eb2009-11-16 21:35:15 +00003272 Qualifiers Quals = T.getType().getLocalQualifiers();
John McCalla2becad2009-10-21 00:40:46 +00003273
John McCall43fed0d2010-11-12 08:19:04 +00003274 QualType Result = getDerived().TransformType(TLB, T.getUnqualifiedLoc());
John McCalla2becad2009-10-21 00:40:46 +00003275 if (Result.isNull())
3276 return QualType();
3277
3278 // Silently suppress qualifiers if the result type can't be qualified.
3279 // FIXME: this is the right thing for template instantiation, but
3280 // probably not for other clients.
3281 if (Result->isFunctionType() || Result->isReferenceType())
Douglas Gregor577f75a2009-08-04 16:50:30 +00003282 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00003283
John McCallf85e1932011-06-15 23:02:42 +00003284 // Suppress Objective-C lifetime qualifiers if they don't make sense for the
Douglas Gregore559ca12011-06-17 22:11:49 +00003285 // resulting type.
3286 if (Quals.hasObjCLifetime()) {
3287 if (!Result->isObjCLifetimeType() && !Result->isDependentType())
3288 Quals.removeObjCLifetime();
Douglas Gregor4020cae2011-06-17 23:16:24 +00003289 else if (Result.getObjCLifetime()) {
Douglas Gregore559ca12011-06-17 22:11:49 +00003290 // Objective-C ARC:
3291 // A lifetime qualifier applied to a substituted template parameter
3292 // overrides the lifetime qualifier from the template argument.
3293 if (const SubstTemplateTypeParmType *SubstTypeParam
3294 = dyn_cast<SubstTemplateTypeParmType>(Result)) {
3295 QualType Replacement = SubstTypeParam->getReplacementType();
3296 Qualifiers Qs = Replacement.getQualifiers();
3297 Qs.removeObjCLifetime();
3298 Replacement
3299 = SemaRef.Context.getQualifiedType(Replacement.getUnqualifiedType(),
3300 Qs);
3301 Result = SemaRef.Context.getSubstTemplateTypeParmType(
3302 SubstTypeParam->getReplacedParameter(),
3303 Replacement);
3304 TLB.TypeWasModifiedSafely(Result);
3305 } else {
Douglas Gregor4020cae2011-06-17 23:16:24 +00003306 // Otherwise, complain about the addition of a qualifier to an
3307 // already-qualified type.
3308 SourceRange R = TLB.getTemporaryTypeLoc(Result).getSourceRange();
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003309 SemaRef.Diag(R.getBegin(), diag::err_attr_objc_ownership_redundant)
Douglas Gregor4020cae2011-06-17 23:16:24 +00003310 << Result << R;
3311
Douglas Gregore559ca12011-06-17 22:11:49 +00003312 Quals.removeObjCLifetime();
3313 }
3314 }
3315 }
John McCall28654742010-06-05 06:41:15 +00003316 if (!Quals.empty()) {
3317 Result = SemaRef.BuildQualifiedType(Result, T.getBeginLoc(), Quals);
3318 TLB.push<QualifiedTypeLoc>(Result);
3319 // No location information to preserve.
3320 }
John McCalla2becad2009-10-21 00:40:46 +00003321
3322 return Result;
3323}
3324
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003325template<typename Derived>
3326TypeLoc
3327TreeTransform<Derived>::TransformTypeInObjectScope(TypeLoc TL,
3328 QualType ObjectType,
3329 NamedDecl *UnqualLookup,
3330 CXXScopeSpec &SS) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003331 QualType T = TL.getType();
3332 if (getDerived().AlreadyTransformed(T))
3333 return TL;
3334
3335 TypeLocBuilder TLB;
3336 QualType Result;
3337
3338 if (isa<TemplateSpecializationType>(T)) {
3339 TemplateSpecializationTypeLoc SpecTL
3340 = cast<TemplateSpecializationTypeLoc>(TL);
3341
3342 TemplateName Template =
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00003343 getDerived().TransformTemplateName(SS,
3344 SpecTL.getTypePtr()->getTemplateName(),
3345 SpecTL.getTemplateNameLoc(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003346 ObjectType, UnqualLookup);
3347 if (Template.isNull())
3348 return TypeLoc();
3349
3350 Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
3351 Template);
3352 } else if (isa<DependentTemplateSpecializationType>(T)) {
3353 DependentTemplateSpecializationTypeLoc SpecTL
3354 = cast<DependentTemplateSpecializationTypeLoc>(TL);
3355
Douglas Gregora88f09f2011-02-28 17:23:35 +00003356 TemplateName Template
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00003357 = getDerived().RebuildTemplateName(SS,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00003358 *SpecTL.getTypePtr()->getIdentifier(),
Abramo Bagnara55d23c92012-02-06 14:41:24 +00003359 SpecTL.getTemplateNameLoc(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00003360 ObjectType, UnqualLookup);
Douglas Gregora88f09f2011-02-28 17:23:35 +00003361 if (Template.isNull())
3362 return TypeLoc();
3363
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003364 Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
Douglas Gregora88f09f2011-02-28 17:23:35 +00003365 SpecTL,
Douglas Gregor087eb5a2011-03-04 18:53:13 +00003366 Template,
3367 SS);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003368 } else {
3369 // Nothing special needs to be done for these.
3370 Result = getDerived().TransformType(TLB, TL);
3371 }
3372
3373 if (Result.isNull())
3374 return TypeLoc();
3375
3376 return TLB.getTypeSourceInfo(SemaRef.Context, Result)->getTypeLoc();
3377}
3378
Douglas Gregorb71d8212011-03-02 18:32:08 +00003379template<typename Derived>
3380TypeSourceInfo *
3381TreeTransform<Derived>::TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
3382 QualType ObjectType,
3383 NamedDecl *UnqualLookup,
3384 CXXScopeSpec &SS) {
3385 // FIXME: Painfully copy-paste from the above!
3386
3387 QualType T = TSInfo->getType();
3388 if (getDerived().AlreadyTransformed(T))
3389 return TSInfo;
3390
3391 TypeLocBuilder TLB;
3392 QualType Result;
3393
3394 TypeLoc TL = TSInfo->getTypeLoc();
3395 if (isa<TemplateSpecializationType>(T)) {
3396 TemplateSpecializationTypeLoc SpecTL
3397 = cast<TemplateSpecializationTypeLoc>(TL);
3398
3399 TemplateName Template
3400 = getDerived().TransformTemplateName(SS,
3401 SpecTL.getTypePtr()->getTemplateName(),
3402 SpecTL.getTemplateNameLoc(),
3403 ObjectType, UnqualLookup);
3404 if (Template.isNull())
3405 return 0;
3406
3407 Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
3408 Template);
3409 } else if (isa<DependentTemplateSpecializationType>(T)) {
3410 DependentTemplateSpecializationTypeLoc SpecTL
3411 = cast<DependentTemplateSpecializationTypeLoc>(TL);
3412
3413 TemplateName Template
3414 = getDerived().RebuildTemplateName(SS,
3415 *SpecTL.getTypePtr()->getIdentifier(),
Abramo Bagnara55d23c92012-02-06 14:41:24 +00003416 SpecTL.getTemplateNameLoc(),
Douglas Gregorb71d8212011-03-02 18:32:08 +00003417 ObjectType, UnqualLookup);
3418 if (Template.isNull())
3419 return 0;
3420
3421 Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
3422 SpecTL,
Douglas Gregor087eb5a2011-03-04 18:53:13 +00003423 Template,
3424 SS);
Douglas Gregorb71d8212011-03-02 18:32:08 +00003425 } else {
3426 // Nothing special needs to be done for these.
3427 Result = getDerived().TransformType(TLB, TL);
3428 }
3429
3430 if (Result.isNull())
3431 return 0;
3432
3433 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
3434}
3435
John McCalla2becad2009-10-21 00:40:46 +00003436template <class TyLoc> static inline
3437QualType TransformTypeSpecType(TypeLocBuilder &TLB, TyLoc T) {
3438 TyLoc NewT = TLB.push<TyLoc>(T.getType());
3439 NewT.setNameLoc(T.getNameLoc());
3440 return T.getType();
3441}
3442
John McCalla2becad2009-10-21 00:40:46 +00003443template<typename Derived>
3444QualType TreeTransform<Derived>::TransformBuiltinType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003445 BuiltinTypeLoc T) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00003446 BuiltinTypeLoc NewT = TLB.push<BuiltinTypeLoc>(T.getType());
3447 NewT.setBuiltinLoc(T.getBuiltinLoc());
3448 if (T.needsExtraLocalData())
3449 NewT.getWrittenBuiltinSpecs() = T.getWrittenBuiltinSpecs();
3450 return T.getType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00003451}
Mike Stump1eb44332009-09-09 15:08:12 +00003452
Douglas Gregor577f75a2009-08-04 16:50:30 +00003453template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00003454QualType TreeTransform<Derived>::TransformComplexType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003455 ComplexTypeLoc T) {
John McCalla2becad2009-10-21 00:40:46 +00003456 // FIXME: recurse?
3457 return TransformTypeSpecType(TLB, T);
Douglas Gregor577f75a2009-08-04 16:50:30 +00003458}
Mike Stump1eb44332009-09-09 15:08:12 +00003459
Douglas Gregor577f75a2009-08-04 16:50:30 +00003460template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00003461QualType TreeTransform<Derived>::TransformPointerType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003462 PointerTypeLoc TL) {
Sean Huntc3021132010-05-05 15:23:54 +00003463 QualType PointeeType
3464 = getDerived().TransformType(TLB, TL.getPointeeLoc());
Douglas Gregor92e986e2010-04-22 16:44:27 +00003465 if (PointeeType.isNull())
3466 return QualType();
3467
3468 QualType Result = TL.getType();
John McCallc12c5bb2010-05-15 11:32:37 +00003469 if (PointeeType->getAs<ObjCObjectType>()) {
Douglas Gregor92e986e2010-04-22 16:44:27 +00003470 // A dependent pointer type 'T *' has is being transformed such
3471 // that an Objective-C class type is being replaced for 'T'. The
3472 // resulting pointer type is an ObjCObjectPointerType, not a
3473 // PointerType.
John McCallc12c5bb2010-05-15 11:32:37 +00003474 Result = SemaRef.Context.getObjCObjectPointerType(PointeeType);
Sean Huntc3021132010-05-05 15:23:54 +00003475
John McCallc12c5bb2010-05-15 11:32:37 +00003476 ObjCObjectPointerTypeLoc NewT = TLB.push<ObjCObjectPointerTypeLoc>(Result);
3477 NewT.setStarLoc(TL.getStarLoc());
Douglas Gregor92e986e2010-04-22 16:44:27 +00003478 return Result;
3479 }
John McCall43fed0d2010-11-12 08:19:04 +00003480
Douglas Gregor92e986e2010-04-22 16:44:27 +00003481 if (getDerived().AlwaysRebuild() ||
3482 PointeeType != TL.getPointeeLoc().getType()) {
3483 Result = getDerived().RebuildPointerType(PointeeType, TL.getSigilLoc());
3484 if (Result.isNull())
3485 return QualType();
3486 }
John McCallf85e1932011-06-15 23:02:42 +00003487
3488 // Objective-C ARC can add lifetime qualifiers to the type that we're
3489 // pointing to.
3490 TLB.TypeWasModifiedSafely(Result->getPointeeType());
3491
Douglas Gregor92e986e2010-04-22 16:44:27 +00003492 PointerTypeLoc NewT = TLB.push<PointerTypeLoc>(Result);
3493 NewT.setSigilLoc(TL.getSigilLoc());
Sean Huntc3021132010-05-05 15:23:54 +00003494 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003495}
Mike Stump1eb44332009-09-09 15:08:12 +00003496
3497template<typename Derived>
3498QualType
John McCalla2becad2009-10-21 00:40:46 +00003499TreeTransform<Derived>::TransformBlockPointerType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003500 BlockPointerTypeLoc TL) {
Douglas Gregordb93c4a2010-04-22 16:46:21 +00003501 QualType PointeeType
Sean Huntc3021132010-05-05 15:23:54 +00003502 = getDerived().TransformType(TLB, TL.getPointeeLoc());
3503 if (PointeeType.isNull())
3504 return QualType();
3505
3506 QualType Result = TL.getType();
3507 if (getDerived().AlwaysRebuild() ||
3508 PointeeType != TL.getPointeeLoc().getType()) {
3509 Result = getDerived().RebuildBlockPointerType(PointeeType,
Douglas Gregordb93c4a2010-04-22 16:46:21 +00003510 TL.getSigilLoc());
3511 if (Result.isNull())
3512 return QualType();
3513 }
3514
Douglas Gregor39968ad2010-04-22 16:50:51 +00003515 BlockPointerTypeLoc NewT = TLB.push<BlockPointerTypeLoc>(Result);
Douglas Gregordb93c4a2010-04-22 16:46:21 +00003516 NewT.setSigilLoc(TL.getSigilLoc());
3517 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003518}
3519
John McCall85737a72009-10-30 00:06:24 +00003520/// Transforms a reference type. Note that somewhat paradoxically we
3521/// don't care whether the type itself is an l-value type or an r-value
3522/// type; we only care if the type was *written* as an l-value type
3523/// or an r-value type.
3524template<typename Derived>
3525QualType
3526TreeTransform<Derived>::TransformReferenceType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003527 ReferenceTypeLoc TL) {
John McCall85737a72009-10-30 00:06:24 +00003528 const ReferenceType *T = TL.getTypePtr();
3529
3530 // Note that this works with the pointee-as-written.
3531 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
3532 if (PointeeType.isNull())
3533 return QualType();
3534
3535 QualType Result = TL.getType();
3536 if (getDerived().AlwaysRebuild() ||
3537 PointeeType != T->getPointeeTypeAsWritten()) {
3538 Result = getDerived().RebuildReferenceType(PointeeType,
3539 T->isSpelledAsLValue(),
3540 TL.getSigilLoc());
3541 if (Result.isNull())
3542 return QualType();
3543 }
3544
John McCallf85e1932011-06-15 23:02:42 +00003545 // Objective-C ARC can add lifetime qualifiers to the type that we're
3546 // referring to.
3547 TLB.TypeWasModifiedSafely(
3548 Result->getAs<ReferenceType>()->getPointeeTypeAsWritten());
3549
John McCall85737a72009-10-30 00:06:24 +00003550 // r-value references can be rebuilt as l-value references.
3551 ReferenceTypeLoc NewTL;
3552 if (isa<LValueReferenceType>(Result))
3553 NewTL = TLB.push<LValueReferenceTypeLoc>(Result);
3554 else
3555 NewTL = TLB.push<RValueReferenceTypeLoc>(Result);
3556 NewTL.setSigilLoc(TL.getSigilLoc());
3557
3558 return Result;
3559}
3560
Mike Stump1eb44332009-09-09 15:08:12 +00003561template<typename Derived>
3562QualType
John McCalla2becad2009-10-21 00:40:46 +00003563TreeTransform<Derived>::TransformLValueReferenceType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003564 LValueReferenceTypeLoc TL) {
3565 return TransformReferenceType(TLB, TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +00003566}
3567
Mike Stump1eb44332009-09-09 15:08:12 +00003568template<typename Derived>
3569QualType
John McCalla2becad2009-10-21 00:40:46 +00003570TreeTransform<Derived>::TransformRValueReferenceType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003571 RValueReferenceTypeLoc TL) {
3572 return TransformReferenceType(TLB, TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +00003573}
Mike Stump1eb44332009-09-09 15:08:12 +00003574
Douglas Gregor577f75a2009-08-04 16:50:30 +00003575template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00003576QualType
John McCalla2becad2009-10-21 00:40:46 +00003577TreeTransform<Derived>::TransformMemberPointerType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003578 MemberPointerTypeLoc TL) {
John McCalla2becad2009-10-21 00:40:46 +00003579 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
Douglas Gregor577f75a2009-08-04 16:50:30 +00003580 if (PointeeType.isNull())
3581 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003582
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003583 TypeSourceInfo* OldClsTInfo = TL.getClassTInfo();
3584 TypeSourceInfo* NewClsTInfo = 0;
3585 if (OldClsTInfo) {
3586 NewClsTInfo = getDerived().TransformType(OldClsTInfo);
3587 if (!NewClsTInfo)
3588 return QualType();
3589 }
3590
3591 const MemberPointerType *T = TL.getTypePtr();
3592 QualType OldClsType = QualType(T->getClass(), 0);
3593 QualType NewClsType;
3594 if (NewClsTInfo)
3595 NewClsType = NewClsTInfo->getType();
3596 else {
3597 NewClsType = getDerived().TransformType(OldClsType);
3598 if (NewClsType.isNull())
3599 return QualType();
3600 }
Mike Stump1eb44332009-09-09 15:08:12 +00003601
John McCalla2becad2009-10-21 00:40:46 +00003602 QualType Result = TL.getType();
3603 if (getDerived().AlwaysRebuild() ||
3604 PointeeType != T->getPointeeType() ||
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003605 NewClsType != OldClsType) {
3606 Result = getDerived().RebuildMemberPointerType(PointeeType, NewClsType,
John McCall85737a72009-10-30 00:06:24 +00003607 TL.getStarLoc());
John McCalla2becad2009-10-21 00:40:46 +00003608 if (Result.isNull())
3609 return QualType();
3610 }
Douglas Gregor577f75a2009-08-04 16:50:30 +00003611
John McCalla2becad2009-10-21 00:40:46 +00003612 MemberPointerTypeLoc NewTL = TLB.push<MemberPointerTypeLoc>(Result);
3613 NewTL.setSigilLoc(TL.getSigilLoc());
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003614 NewTL.setClassTInfo(NewClsTInfo);
John McCalla2becad2009-10-21 00:40:46 +00003615
3616 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003617}
3618
Mike Stump1eb44332009-09-09 15:08:12 +00003619template<typename Derived>
3620QualType
John McCalla2becad2009-10-21 00:40:46 +00003621TreeTransform<Derived>::TransformConstantArrayType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003622 ConstantArrayTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003623 const ConstantArrayType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003624 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
Douglas Gregor577f75a2009-08-04 16:50:30 +00003625 if (ElementType.isNull())
3626 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003627
John McCalla2becad2009-10-21 00:40:46 +00003628 QualType Result = TL.getType();
3629 if (getDerived().AlwaysRebuild() ||
3630 ElementType != T->getElementType()) {
3631 Result = getDerived().RebuildConstantArrayType(ElementType,
3632 T->getSizeModifier(),
3633 T->getSize(),
John McCall85737a72009-10-30 00:06:24 +00003634 T->getIndexTypeCVRQualifiers(),
3635 TL.getBracketsRange());
John McCalla2becad2009-10-21 00:40:46 +00003636 if (Result.isNull())
3637 return QualType();
3638 }
Eli Friedman457a3772012-01-25 22:19:07 +00003639
3640 // We might have either a ConstantArrayType or a VariableArrayType now:
3641 // a ConstantArrayType is allowed to have an element type which is a
3642 // VariableArrayType if the type is dependent. Fortunately, all array
3643 // types have the same location layout.
3644 ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
John McCalla2becad2009-10-21 00:40:46 +00003645 NewTL.setLBracketLoc(TL.getLBracketLoc());
3646 NewTL.setRBracketLoc(TL.getRBracketLoc());
Mike Stump1eb44332009-09-09 15:08:12 +00003647
John McCalla2becad2009-10-21 00:40:46 +00003648 Expr *Size = TL.getSizeExpr();
3649 if (Size) {
Richard Smithf6702a32011-12-20 02:08:33 +00003650 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3651 Sema::ConstantEvaluated);
John McCalla2becad2009-10-21 00:40:46 +00003652 Size = getDerived().TransformExpr(Size).template takeAs<Expr>();
3653 }
3654 NewTL.setSizeExpr(Size);
3655
3656 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003657}
Mike Stump1eb44332009-09-09 15:08:12 +00003658
Douglas Gregor577f75a2009-08-04 16:50:30 +00003659template<typename Derived>
Douglas Gregor577f75a2009-08-04 16:50:30 +00003660QualType TreeTransform<Derived>::TransformIncompleteArrayType(
John McCalla2becad2009-10-21 00:40:46 +00003661 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003662 IncompleteArrayTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003663 const IncompleteArrayType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003664 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
Douglas Gregor577f75a2009-08-04 16:50:30 +00003665 if (ElementType.isNull())
3666 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003667
John McCalla2becad2009-10-21 00:40:46 +00003668 QualType Result = TL.getType();
3669 if (getDerived().AlwaysRebuild() ||
3670 ElementType != T->getElementType()) {
3671 Result = getDerived().RebuildIncompleteArrayType(ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00003672 T->getSizeModifier(),
John McCall85737a72009-10-30 00:06:24 +00003673 T->getIndexTypeCVRQualifiers(),
3674 TL.getBracketsRange());
John McCalla2becad2009-10-21 00:40:46 +00003675 if (Result.isNull())
3676 return QualType();
3677 }
Sean Huntc3021132010-05-05 15:23:54 +00003678
John McCalla2becad2009-10-21 00:40:46 +00003679 IncompleteArrayTypeLoc NewTL = TLB.push<IncompleteArrayTypeLoc>(Result);
3680 NewTL.setLBracketLoc(TL.getLBracketLoc());
3681 NewTL.setRBracketLoc(TL.getRBracketLoc());
3682 NewTL.setSizeExpr(0);
3683
3684 return Result;
3685}
3686
3687template<typename Derived>
3688QualType
3689TreeTransform<Derived>::TransformVariableArrayType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003690 VariableArrayTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003691 const VariableArrayType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003692 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3693 if (ElementType.isNull())
3694 return QualType();
3695
John McCall60d7b3a2010-08-24 06:29:42 +00003696 ExprResult SizeResult
John McCalla2becad2009-10-21 00:40:46 +00003697 = getDerived().TransformExpr(T->getSizeExpr());
3698 if (SizeResult.isInvalid())
3699 return QualType();
3700
John McCall9ae2f072010-08-23 23:25:46 +00003701 Expr *Size = SizeResult.take();
John McCalla2becad2009-10-21 00:40:46 +00003702
3703 QualType Result = TL.getType();
3704 if (getDerived().AlwaysRebuild() ||
3705 ElementType != T->getElementType() ||
3706 Size != T->getSizeExpr()) {
3707 Result = getDerived().RebuildVariableArrayType(ElementType,
3708 T->getSizeModifier(),
John McCall9ae2f072010-08-23 23:25:46 +00003709 Size,
John McCalla2becad2009-10-21 00:40:46 +00003710 T->getIndexTypeCVRQualifiers(),
John McCall85737a72009-10-30 00:06:24 +00003711 TL.getBracketsRange());
John McCalla2becad2009-10-21 00:40:46 +00003712 if (Result.isNull())
3713 return QualType();
3714 }
Sean Huntc3021132010-05-05 15:23:54 +00003715
John McCalla2becad2009-10-21 00:40:46 +00003716 VariableArrayTypeLoc NewTL = TLB.push<VariableArrayTypeLoc>(Result);
3717 NewTL.setLBracketLoc(TL.getLBracketLoc());
3718 NewTL.setRBracketLoc(TL.getRBracketLoc());
3719 NewTL.setSizeExpr(Size);
3720
3721 return Result;
3722}
3723
3724template<typename Derived>
3725QualType
3726TreeTransform<Derived>::TransformDependentSizedArrayType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003727 DependentSizedArrayTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003728 const DependentSizedArrayType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003729 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3730 if (ElementType.isNull())
3731 return QualType();
3732
Richard Smithf6702a32011-12-20 02:08:33 +00003733 // Array bounds are constant expressions.
3734 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3735 Sema::ConstantEvaluated);
John McCalla2becad2009-10-21 00:40:46 +00003736
John McCall3b657512011-01-19 10:06:00 +00003737 // Prefer the expression from the TypeLoc; the other may have been uniqued.
3738 Expr *origSize = TL.getSizeExpr();
3739 if (!origSize) origSize = T->getSizeExpr();
3740
3741 ExprResult sizeResult
3742 = getDerived().TransformExpr(origSize);
3743 if (sizeResult.isInvalid())
John McCalla2becad2009-10-21 00:40:46 +00003744 return QualType();
3745
John McCall3b657512011-01-19 10:06:00 +00003746 Expr *size = sizeResult.get();
John McCalla2becad2009-10-21 00:40:46 +00003747
3748 QualType Result = TL.getType();
3749 if (getDerived().AlwaysRebuild() ||
3750 ElementType != T->getElementType() ||
John McCall3b657512011-01-19 10:06:00 +00003751 size != origSize) {
John McCalla2becad2009-10-21 00:40:46 +00003752 Result = getDerived().RebuildDependentSizedArrayType(ElementType,
3753 T->getSizeModifier(),
John McCall3b657512011-01-19 10:06:00 +00003754 size,
John McCalla2becad2009-10-21 00:40:46 +00003755 T->getIndexTypeCVRQualifiers(),
John McCall85737a72009-10-30 00:06:24 +00003756 TL.getBracketsRange());
John McCalla2becad2009-10-21 00:40:46 +00003757 if (Result.isNull())
3758 return QualType();
3759 }
John McCalla2becad2009-10-21 00:40:46 +00003760
3761 // We might have any sort of array type now, but fortunately they
3762 // all have the same location layout.
3763 ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
3764 NewTL.setLBracketLoc(TL.getLBracketLoc());
3765 NewTL.setRBracketLoc(TL.getRBracketLoc());
John McCall3b657512011-01-19 10:06:00 +00003766 NewTL.setSizeExpr(size);
John McCalla2becad2009-10-21 00:40:46 +00003767
3768 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003769}
Mike Stump1eb44332009-09-09 15:08:12 +00003770
3771template<typename Derived>
Douglas Gregor577f75a2009-08-04 16:50:30 +00003772QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType(
John McCalla2becad2009-10-21 00:40:46 +00003773 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003774 DependentSizedExtVectorTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003775 const DependentSizedExtVectorType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003776
3777 // FIXME: ext vector locs should be nested
Douglas Gregor577f75a2009-08-04 16:50:30 +00003778 QualType ElementType = getDerived().TransformType(T->getElementType());
3779 if (ElementType.isNull())
3780 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003781
Richard Smithf6702a32011-12-20 02:08:33 +00003782 // Vector sizes are constant expressions.
3783 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3784 Sema::ConstantEvaluated);
Douglas Gregor670444e2009-08-04 22:27:00 +00003785
John McCall60d7b3a2010-08-24 06:29:42 +00003786 ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
Douglas Gregor577f75a2009-08-04 16:50:30 +00003787 if (Size.isInvalid())
3788 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003789
John McCalla2becad2009-10-21 00:40:46 +00003790 QualType Result = TL.getType();
3791 if (getDerived().AlwaysRebuild() ||
John McCalleee91c32009-10-23 17:55:45 +00003792 ElementType != T->getElementType() ||
3793 Size.get() != T->getSizeExpr()) {
John McCalla2becad2009-10-21 00:40:46 +00003794 Result = getDerived().RebuildDependentSizedExtVectorType(ElementType,
John McCall9ae2f072010-08-23 23:25:46 +00003795 Size.take(),
Douglas Gregor577f75a2009-08-04 16:50:30 +00003796 T->getAttributeLoc());
John McCalla2becad2009-10-21 00:40:46 +00003797 if (Result.isNull())
3798 return QualType();
3799 }
John McCalla2becad2009-10-21 00:40:46 +00003800
3801 // Result might be dependent or not.
3802 if (isa<DependentSizedExtVectorType>(Result)) {
3803 DependentSizedExtVectorTypeLoc NewTL
3804 = TLB.push<DependentSizedExtVectorTypeLoc>(Result);
3805 NewTL.setNameLoc(TL.getNameLoc());
3806 } else {
3807 ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
3808 NewTL.setNameLoc(TL.getNameLoc());
3809 }
3810
3811 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003812}
Mike Stump1eb44332009-09-09 15:08:12 +00003813
3814template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00003815QualType TreeTransform<Derived>::TransformVectorType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003816 VectorTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003817 const VectorType *T = TL.getTypePtr();
Douglas Gregor577f75a2009-08-04 16:50:30 +00003818 QualType ElementType = getDerived().TransformType(T->getElementType());
3819 if (ElementType.isNull())
3820 return QualType();
3821
John McCalla2becad2009-10-21 00:40:46 +00003822 QualType Result = TL.getType();
3823 if (getDerived().AlwaysRebuild() ||
3824 ElementType != T->getElementType()) {
John Thompson82287d12010-02-05 00:12:22 +00003825 Result = getDerived().RebuildVectorType(ElementType, T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00003826 T->getVectorKind());
John McCalla2becad2009-10-21 00:40:46 +00003827 if (Result.isNull())
3828 return QualType();
3829 }
Sean Huntc3021132010-05-05 15:23:54 +00003830
John McCalla2becad2009-10-21 00:40:46 +00003831 VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result);
3832 NewTL.setNameLoc(TL.getNameLoc());
Mike Stump1eb44332009-09-09 15:08:12 +00003833
John McCalla2becad2009-10-21 00:40:46 +00003834 return Result;
3835}
3836
3837template<typename Derived>
3838QualType TreeTransform<Derived>::TransformExtVectorType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003839 ExtVectorTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003840 const VectorType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003841 QualType ElementType = getDerived().TransformType(T->getElementType());
3842 if (ElementType.isNull())
3843 return QualType();
3844
3845 QualType Result = TL.getType();
3846 if (getDerived().AlwaysRebuild() ||
3847 ElementType != T->getElementType()) {
3848 Result = getDerived().RebuildExtVectorType(ElementType,
3849 T->getNumElements(),
3850 /*FIXME*/ SourceLocation());
3851 if (Result.isNull())
3852 return QualType();
3853 }
Sean Huntc3021132010-05-05 15:23:54 +00003854
John McCalla2becad2009-10-21 00:40:46 +00003855 ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
3856 NewTL.setNameLoc(TL.getNameLoc());
3857
3858 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003859}
Mike Stump1eb44332009-09-09 15:08:12 +00003860
3861template<typename Derived>
John McCall21ef0fa2010-03-11 09:03:00 +00003862ParmVarDecl *
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003863TreeTransform<Derived>::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCallfb44de92011-05-01 22:35:37 +00003864 int indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00003865 llvm::Optional<unsigned> NumExpansions,
3866 bool ExpectParameterPack) {
John McCall21ef0fa2010-03-11 09:03:00 +00003867 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003868 TypeSourceInfo *NewDI = 0;
3869
3870 if (NumExpansions && isa<PackExpansionType>(OldDI->getType())) {
3871 // If we're substituting into a pack expansion type and we know the
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00003872 // length we want to expand to, just substitute for the pattern.
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003873 TypeLoc OldTL = OldDI->getTypeLoc();
3874 PackExpansionTypeLoc OldExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
3875
3876 TypeLocBuilder TLB;
3877 TypeLoc NewTL = OldDI->getTypeLoc();
3878 TLB.reserve(NewTL.getFullDataSize());
3879
3880 QualType Result = getDerived().TransformType(TLB,
3881 OldExpansionTL.getPatternLoc());
3882 if (Result.isNull())
3883 return 0;
3884
3885 Result = RebuildPackExpansionType(Result,
3886 OldExpansionTL.getPatternLoc().getSourceRange(),
3887 OldExpansionTL.getEllipsisLoc(),
3888 NumExpansions);
3889 if (Result.isNull())
3890 return 0;
3891
3892 PackExpansionTypeLoc NewExpansionTL
3893 = TLB.push<PackExpansionTypeLoc>(Result);
3894 NewExpansionTL.setEllipsisLoc(OldExpansionTL.getEllipsisLoc());
3895 NewDI = TLB.getTypeSourceInfo(SemaRef.Context, Result);
3896 } else
3897 NewDI = getDerived().TransformType(OldDI);
John McCall21ef0fa2010-03-11 09:03:00 +00003898 if (!NewDI)
3899 return 0;
3900
John McCallfb44de92011-05-01 22:35:37 +00003901 if (NewDI == OldDI && indexAdjustment == 0)
John McCall21ef0fa2010-03-11 09:03:00 +00003902 return OldParm;
John McCallfb44de92011-05-01 22:35:37 +00003903
3904 ParmVarDecl *newParm = ParmVarDecl::Create(SemaRef.Context,
3905 OldParm->getDeclContext(),
3906 OldParm->getInnerLocStart(),
3907 OldParm->getLocation(),
3908 OldParm->getIdentifier(),
3909 NewDI->getType(),
3910 NewDI,
3911 OldParm->getStorageClass(),
3912 OldParm->getStorageClassAsWritten(),
3913 /* DefArg */ NULL);
3914 newParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
3915 OldParm->getFunctionScopeIndex() + indexAdjustment);
3916 return newParm;
John McCall21ef0fa2010-03-11 09:03:00 +00003917}
3918
3919template<typename Derived>
3920bool TreeTransform<Derived>::
Douglas Gregora009b592011-01-07 00:20:55 +00003921 TransformFunctionTypeParams(SourceLocation Loc,
3922 ParmVarDecl **Params, unsigned NumParams,
3923 const QualType *ParamTypes,
Chris Lattner686775d2011-07-20 06:58:45 +00003924 SmallVectorImpl<QualType> &OutParamTypes,
3925 SmallVectorImpl<ParmVarDecl*> *PVars) {
John McCallfb44de92011-05-01 22:35:37 +00003926 int indexAdjustment = 0;
3927
Douglas Gregora009b592011-01-07 00:20:55 +00003928 for (unsigned i = 0; i != NumParams; ++i) {
3929 if (ParmVarDecl *OldParm = Params[i]) {
John McCallfb44de92011-05-01 22:35:37 +00003930 assert(OldParm->getFunctionScopeIndex() == i);
3931
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003932 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor406f98f2011-03-02 02:04:06 +00003933 ParmVarDecl *NewParm = 0;
Douglas Gregor603cfb42011-01-05 23:12:31 +00003934 if (OldParm->isParameterPack()) {
3935 // We have a function parameter pack that may need to be expanded.
Chris Lattner686775d2011-07-20 06:58:45 +00003936 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
John McCall21ef0fa2010-03-11 09:03:00 +00003937
Douglas Gregor603cfb42011-01-05 23:12:31 +00003938 // Find the parameter packs that could be expanded.
Douglas Gregorc8a16fb2011-01-05 23:16:57 +00003939 TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc();
3940 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(TL);
3941 TypeLoc Pattern = ExpansionTL.getPatternLoc();
3942 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
Douglas Gregor406f98f2011-03-02 02:04:06 +00003943 assert(Unexpanded.size() > 0 && "Could not find parameter packs!");
3944
Douglas Gregor603cfb42011-01-05 23:12:31 +00003945 // Determine whether we should expand the parameter packs.
3946 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00003947 bool RetainExpansion = false;
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003948 llvm::Optional<unsigned> OrigNumExpansions
3949 = ExpansionTL.getTypePtr()->getNumExpansions();
3950 NumExpansions = OrigNumExpansions;
Douglas Gregorc8a16fb2011-01-05 23:16:57 +00003951 if (getDerived().TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(),
3952 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003953 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00003954 ShouldExpand,
3955 RetainExpansion,
3956 NumExpansions)) {
Douglas Gregor603cfb42011-01-05 23:12:31 +00003957 return true;
3958 }
3959
3960 if (ShouldExpand) {
3961 // Expand the function parameter pack into multiple, separate
3962 // parameters.
Douglas Gregor12c9c002011-01-07 16:43:16 +00003963 getDerived().ExpandingFunctionParameterPack(OldParm);
Douglas Gregorcded4f62011-01-14 17:04:44 +00003964 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor603cfb42011-01-05 23:12:31 +00003965 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
3966 ParmVarDecl *NewParm
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003967 = getDerived().TransformFunctionTypeParam(OldParm,
John McCallfb44de92011-05-01 22:35:37 +00003968 indexAdjustment++,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00003969 OrigNumExpansions,
3970 /*ExpectParameterPack=*/false);
Douglas Gregor603cfb42011-01-05 23:12:31 +00003971 if (!NewParm)
3972 return true;
3973
Douglas Gregora009b592011-01-07 00:20:55 +00003974 OutParamTypes.push_back(NewParm->getType());
3975 if (PVars)
3976 PVars->push_back(NewParm);
Douglas Gregor603cfb42011-01-05 23:12:31 +00003977 }
Douglas Gregord3731192011-01-10 07:32:04 +00003978
3979 // If we're supposed to retain a pack expansion, do so by temporarily
3980 // forgetting the partially-substituted parameter pack.
3981 if (RetainExpansion) {
3982 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
3983 ParmVarDecl *NewParm
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003984 = getDerived().TransformFunctionTypeParam(OldParm,
John McCallfb44de92011-05-01 22:35:37 +00003985 indexAdjustment++,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00003986 OrigNumExpansions,
3987 /*ExpectParameterPack=*/false);
Douglas Gregord3731192011-01-10 07:32:04 +00003988 if (!NewParm)
3989 return true;
3990
3991 OutParamTypes.push_back(NewParm->getType());
3992 if (PVars)
3993 PVars->push_back(NewParm);
3994 }
3995
John McCallfb44de92011-05-01 22:35:37 +00003996 // The next parameter should have the same adjustment as the
3997 // last thing we pushed, but we post-incremented indexAdjustment
3998 // on every push. Also, if we push nothing, the adjustment should
3999 // go down by one.
4000 indexAdjustment--;
4001
Douglas Gregor603cfb42011-01-05 23:12:31 +00004002 // We're done with the pack expansion.
4003 continue;
4004 }
4005
4006 // We'll substitute the parameter now without expanding the pack
4007 // expansion.
Douglas Gregor406f98f2011-03-02 02:04:06 +00004008 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
4009 NewParm = getDerived().TransformFunctionTypeParam(OldParm,
John McCallfb44de92011-05-01 22:35:37 +00004010 indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00004011 NumExpansions,
4012 /*ExpectParameterPack=*/true);
Douglas Gregor406f98f2011-03-02 02:04:06 +00004013 } else {
4014 NewParm = getDerived().TransformFunctionTypeParam(OldParm,
John McCallfb44de92011-05-01 22:35:37 +00004015 indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00004016 llvm::Optional<unsigned>(),
4017 /*ExpectParameterPack=*/false);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004018 }
Douglas Gregor406f98f2011-03-02 02:04:06 +00004019
John McCall21ef0fa2010-03-11 09:03:00 +00004020 if (!NewParm)
4021 return true;
Douglas Gregor603cfb42011-01-05 23:12:31 +00004022
Douglas Gregora009b592011-01-07 00:20:55 +00004023 OutParamTypes.push_back(NewParm->getType());
4024 if (PVars)
4025 PVars->push_back(NewParm);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004026 continue;
4027 }
John McCall21ef0fa2010-03-11 09:03:00 +00004028
4029 // Deal with the possibility that we don't have a parameter
4030 // declaration for this parameter.
Douglas Gregora009b592011-01-07 00:20:55 +00004031 QualType OldType = ParamTypes[i];
Douglas Gregor603cfb42011-01-05 23:12:31 +00004032 bool IsPackExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00004033 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor406f98f2011-03-02 02:04:06 +00004034 QualType NewType;
Douglas Gregor603cfb42011-01-05 23:12:31 +00004035 if (const PackExpansionType *Expansion
4036 = dyn_cast<PackExpansionType>(OldType)) {
4037 // We have a function parameter pack that may need to be expanded.
4038 QualType Pattern = Expansion->getPattern();
Chris Lattner686775d2011-07-20 06:58:45 +00004039 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor603cfb42011-01-05 23:12:31 +00004040 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
4041
4042 // Determine whether we should expand the parameter packs.
4043 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00004044 bool RetainExpansion = false;
Douglas Gregora009b592011-01-07 00:20:55 +00004045 if (getDerived().TryExpandParameterPacks(Loc, SourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00004046 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00004047 ShouldExpand,
4048 RetainExpansion,
4049 NumExpansions)) {
John McCall21ef0fa2010-03-11 09:03:00 +00004050 return true;
Douglas Gregor603cfb42011-01-05 23:12:31 +00004051 }
4052
4053 if (ShouldExpand) {
4054 // Expand the function parameter pack into multiple, separate
4055 // parameters.
Douglas Gregorcded4f62011-01-14 17:04:44 +00004056 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor603cfb42011-01-05 23:12:31 +00004057 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
4058 QualType NewType = getDerived().TransformType(Pattern);
4059 if (NewType.isNull())
4060 return true;
John McCall21ef0fa2010-03-11 09:03:00 +00004061
Douglas Gregora009b592011-01-07 00:20:55 +00004062 OutParamTypes.push_back(NewType);
4063 if (PVars)
4064 PVars->push_back(0);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004065 }
4066
4067 // We're done with the pack expansion.
4068 continue;
4069 }
4070
Douglas Gregor3cae5c92011-01-10 20:53:55 +00004071 // If we're supposed to retain a pack expansion, do so by temporarily
4072 // forgetting the partially-substituted parameter pack.
4073 if (RetainExpansion) {
4074 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
4075 QualType NewType = getDerived().TransformType(Pattern);
4076 if (NewType.isNull())
4077 return true;
4078
4079 OutParamTypes.push_back(NewType);
4080 if (PVars)
4081 PVars->push_back(0);
4082 }
Douglas Gregord3731192011-01-10 07:32:04 +00004083
Douglas Gregor603cfb42011-01-05 23:12:31 +00004084 // We'll substitute the parameter now without expanding the pack
4085 // expansion.
4086 OldType = Expansion->getPattern();
4087 IsPackExpansion = true;
Douglas Gregor406f98f2011-03-02 02:04:06 +00004088 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
4089 NewType = getDerived().TransformType(OldType);
4090 } else {
4091 NewType = getDerived().TransformType(OldType);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004092 }
4093
Douglas Gregor603cfb42011-01-05 23:12:31 +00004094 if (NewType.isNull())
4095 return true;
4096
4097 if (IsPackExpansion)
Douglas Gregorcded4f62011-01-14 17:04:44 +00004098 NewType = getSema().Context.getPackExpansionType(NewType,
4099 NumExpansions);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004100
Douglas Gregora009b592011-01-07 00:20:55 +00004101 OutParamTypes.push_back(NewType);
4102 if (PVars)
4103 PVars->push_back(0);
John McCall21ef0fa2010-03-11 09:03:00 +00004104 }
4105
John McCallfb44de92011-05-01 22:35:37 +00004106#ifndef NDEBUG
4107 if (PVars) {
4108 for (unsigned i = 0, e = PVars->size(); i != e; ++i)
4109 if (ParmVarDecl *parm = (*PVars)[i])
4110 assert(parm->getFunctionScopeIndex() == i);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004111 }
John McCallfb44de92011-05-01 22:35:37 +00004112#endif
4113
4114 return false;
4115}
John McCall21ef0fa2010-03-11 09:03:00 +00004116
4117template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00004118QualType
John McCalla2becad2009-10-21 00:40:46 +00004119TreeTransform<Derived>::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004120 FunctionProtoTypeLoc TL) {
Douglas Gregor7e010a02010-08-31 00:26:14 +00004121 // Transform the parameters and return type.
4122 //
4123 // We instantiate in source order, with the return type first followed by
4124 // the parameters, because users tend to expect this (even if they shouldn't
4125 // rely on it!).
4126 //
Douglas Gregordab60ad2010-10-01 18:44:50 +00004127 // When the function has a trailing return type, we instantiate the
4128 // parameters before the return type, since the return type can then refer
4129 // to the parameters themselves (via decltype, sizeof, etc.).
4130 //
Chris Lattner686775d2011-07-20 06:58:45 +00004131 SmallVector<QualType, 4> ParamTypes;
4132 SmallVector<ParmVarDecl*, 4> ParamDecls;
John McCallf4c73712011-01-19 06:33:43 +00004133 const FunctionProtoType *T = TL.getTypePtr();
Douglas Gregor7e010a02010-08-31 00:26:14 +00004134
Douglas Gregordab60ad2010-10-01 18:44:50 +00004135 QualType ResultType;
4136
4137 if (TL.getTrailingReturn()) {
Douglas Gregora009b592011-01-07 00:20:55 +00004138 if (getDerived().TransformFunctionTypeParams(TL.getBeginLoc(),
4139 TL.getParmArray(),
4140 TL.getNumArgs(),
4141 TL.getTypePtr()->arg_type_begin(),
4142 ParamTypes, &ParamDecls))
Douglas Gregordab60ad2010-10-01 18:44:50 +00004143 return QualType();
4144
4145 ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4146 if (ResultType.isNull())
4147 return QualType();
4148 }
4149 else {
4150 ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4151 if (ResultType.isNull())
4152 return QualType();
4153
Douglas Gregora009b592011-01-07 00:20:55 +00004154 if (getDerived().TransformFunctionTypeParams(TL.getBeginLoc(),
4155 TL.getParmArray(),
4156 TL.getNumArgs(),
4157 TL.getTypePtr()->arg_type_begin(),
4158 ParamTypes, &ParamDecls))
Douglas Gregordab60ad2010-10-01 18:44:50 +00004159 return QualType();
4160 }
4161
John McCalla2becad2009-10-21 00:40:46 +00004162 QualType Result = TL.getType();
4163 if (getDerived().AlwaysRebuild() ||
4164 ResultType != T->getResultType() ||
Douglas Gregorbd5f9f72011-01-07 19:27:47 +00004165 T->getNumArgs() != ParamTypes.size() ||
John McCalla2becad2009-10-21 00:40:46 +00004166 !std::equal(T->arg_type_begin(), T->arg_type_end(), ParamTypes.begin())) {
4167 Result = getDerived().RebuildFunctionProtoType(ResultType,
4168 ParamTypes.data(),
4169 ParamTypes.size(),
4170 T->isVariadic(),
Richard Smitheefb3d52012-02-10 09:58:53 +00004171 T->hasTrailingReturn(),
Eli Friedmanfa869542010-08-05 02:54:05 +00004172 T->getTypeQuals(),
Douglas Gregorc938c162011-01-26 05:01:58 +00004173 T->getRefQualifier(),
Eli Friedmanfa869542010-08-05 02:54:05 +00004174 T->getExtInfo());
John McCalla2becad2009-10-21 00:40:46 +00004175 if (Result.isNull())
4176 return QualType();
4177 }
Mike Stump1eb44332009-09-09 15:08:12 +00004178
John McCalla2becad2009-10-21 00:40:46 +00004179 FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004180 NewTL.setLocalRangeBegin(TL.getLocalRangeBegin());
4181 NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
Douglas Gregordab60ad2010-10-01 18:44:50 +00004182 NewTL.setTrailingReturn(TL.getTrailingReturn());
John McCalla2becad2009-10-21 00:40:46 +00004183 for (unsigned i = 0, e = NewTL.getNumArgs(); i != e; ++i)
4184 NewTL.setArg(i, ParamDecls[i]);
4185
4186 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004187}
Mike Stump1eb44332009-09-09 15:08:12 +00004188
Douglas Gregor577f75a2009-08-04 16:50:30 +00004189template<typename Derived>
4190QualType TreeTransform<Derived>::TransformFunctionNoProtoType(
John McCalla2becad2009-10-21 00:40:46 +00004191 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004192 FunctionNoProtoTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004193 const FunctionNoProtoType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00004194 QualType ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4195 if (ResultType.isNull())
4196 return QualType();
4197
4198 QualType Result = TL.getType();
4199 if (getDerived().AlwaysRebuild() ||
4200 ResultType != T->getResultType())
4201 Result = getDerived().RebuildFunctionNoProtoType(ResultType);
4202
4203 FunctionNoProtoTypeLoc NewTL = TLB.push<FunctionNoProtoTypeLoc>(Result);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004204 NewTL.setLocalRangeBegin(TL.getLocalRangeBegin());
4205 NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
Douglas Gregordab60ad2010-10-01 18:44:50 +00004206 NewTL.setTrailingReturn(false);
John McCalla2becad2009-10-21 00:40:46 +00004207
4208 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004209}
Mike Stump1eb44332009-09-09 15:08:12 +00004210
John McCalled976492009-12-04 22:46:56 +00004211template<typename Derived> QualType
4212TreeTransform<Derived>::TransformUnresolvedUsingType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004213 UnresolvedUsingTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004214 const UnresolvedUsingType *T = TL.getTypePtr();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004215 Decl *D = getDerived().TransformDecl(TL.getNameLoc(), T->getDecl());
John McCalled976492009-12-04 22:46:56 +00004216 if (!D)
4217 return QualType();
4218
4219 QualType Result = TL.getType();
4220 if (getDerived().AlwaysRebuild() || D != T->getDecl()) {
4221 Result = getDerived().RebuildUnresolvedUsingType(D);
4222 if (Result.isNull())
4223 return QualType();
4224 }
4225
4226 // We might get an arbitrary type spec type back. We should at
4227 // least always get a type spec type, though.
4228 TypeSpecTypeLoc NewTL = TLB.pushTypeSpec(Result);
4229 NewTL.setNameLoc(TL.getNameLoc());
4230
4231 return Result;
4232}
4233
Douglas Gregor577f75a2009-08-04 16:50:30 +00004234template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004235QualType TreeTransform<Derived>::TransformTypedefType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004236 TypedefTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004237 const TypedefType *T = TL.getTypePtr();
Richard Smith162e1c12011-04-15 14:24:37 +00004238 TypedefNameDecl *Typedef
4239 = cast_or_null<TypedefNameDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4240 T->getDecl()));
Douglas Gregor577f75a2009-08-04 16:50:30 +00004241 if (!Typedef)
4242 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004243
John McCalla2becad2009-10-21 00:40:46 +00004244 QualType Result = TL.getType();
4245 if (getDerived().AlwaysRebuild() ||
4246 Typedef != T->getDecl()) {
4247 Result = getDerived().RebuildTypedefType(Typedef);
4248 if (Result.isNull())
4249 return QualType();
4250 }
Mike Stump1eb44332009-09-09 15:08:12 +00004251
John McCalla2becad2009-10-21 00:40:46 +00004252 TypedefTypeLoc NewTL = TLB.push<TypedefTypeLoc>(Result);
4253 NewTL.setNameLoc(TL.getNameLoc());
4254
4255 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004256}
Mike Stump1eb44332009-09-09 15:08:12 +00004257
Douglas Gregor577f75a2009-08-04 16:50:30 +00004258template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004259QualType TreeTransform<Derived>::TransformTypeOfExprType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004260 TypeOfExprTypeLoc TL) {
Douglas Gregor670444e2009-08-04 22:27:00 +00004261 // typeof expressions are not potentially evaluated contexts
John McCallf312b1e2010-08-26 23:41:50 +00004262 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00004263
John McCall60d7b3a2010-08-24 06:29:42 +00004264 ExprResult E = getDerived().TransformExpr(TL.getUnderlyingExpr());
Douglas Gregor577f75a2009-08-04 16:50:30 +00004265 if (E.isInvalid())
4266 return QualType();
4267
John McCalla2becad2009-10-21 00:40:46 +00004268 QualType Result = TL.getType();
4269 if (getDerived().AlwaysRebuild() ||
John McCallcfb708c2010-01-13 20:03:27 +00004270 E.get() != TL.getUnderlyingExpr()) {
John McCall2a984ca2010-10-12 00:20:44 +00004271 Result = getDerived().RebuildTypeOfExprType(E.get(), TL.getTypeofLoc());
John McCalla2becad2009-10-21 00:40:46 +00004272 if (Result.isNull())
4273 return QualType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004274 }
John McCalla2becad2009-10-21 00:40:46 +00004275 else E.take();
Mike Stump1eb44332009-09-09 15:08:12 +00004276
John McCalla2becad2009-10-21 00:40:46 +00004277 TypeOfExprTypeLoc NewTL = TLB.push<TypeOfExprTypeLoc>(Result);
John McCallcfb708c2010-01-13 20:03:27 +00004278 NewTL.setTypeofLoc(TL.getTypeofLoc());
4279 NewTL.setLParenLoc(TL.getLParenLoc());
4280 NewTL.setRParenLoc(TL.getRParenLoc());
John McCalla2becad2009-10-21 00:40:46 +00004281
4282 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004283}
Mike Stump1eb44332009-09-09 15:08:12 +00004284
4285template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004286QualType TreeTransform<Derived>::TransformTypeOfType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004287 TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00004288 TypeSourceInfo* Old_Under_TI = TL.getUnderlyingTInfo();
4289 TypeSourceInfo* New_Under_TI = getDerived().TransformType(Old_Under_TI);
4290 if (!New_Under_TI)
Douglas Gregor577f75a2009-08-04 16:50:30 +00004291 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004292
John McCalla2becad2009-10-21 00:40:46 +00004293 QualType Result = TL.getType();
John McCallcfb708c2010-01-13 20:03:27 +00004294 if (getDerived().AlwaysRebuild() || New_Under_TI != Old_Under_TI) {
4295 Result = getDerived().RebuildTypeOfType(New_Under_TI->getType());
John McCalla2becad2009-10-21 00:40:46 +00004296 if (Result.isNull())
4297 return QualType();
4298 }
Mike Stump1eb44332009-09-09 15:08:12 +00004299
John McCalla2becad2009-10-21 00:40:46 +00004300 TypeOfTypeLoc NewTL = TLB.push<TypeOfTypeLoc>(Result);
John McCallcfb708c2010-01-13 20:03:27 +00004301 NewTL.setTypeofLoc(TL.getTypeofLoc());
4302 NewTL.setLParenLoc(TL.getLParenLoc());
4303 NewTL.setRParenLoc(TL.getRParenLoc());
4304 NewTL.setUnderlyingTInfo(New_Under_TI);
John McCalla2becad2009-10-21 00:40:46 +00004305
4306 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004307}
Mike Stump1eb44332009-09-09 15:08:12 +00004308
4309template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004310QualType TreeTransform<Derived>::TransformDecltypeType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004311 DecltypeTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004312 const DecltypeType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00004313
Douglas Gregor670444e2009-08-04 22:27:00 +00004314 // decltype expressions are not potentially evaluated contexts
John McCallf312b1e2010-08-26 23:41:50 +00004315 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00004316
John McCall60d7b3a2010-08-24 06:29:42 +00004317 ExprResult E = getDerived().TransformExpr(T->getUnderlyingExpr());
Douglas Gregor577f75a2009-08-04 16:50:30 +00004318 if (E.isInvalid())
4319 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004320
John McCalla2becad2009-10-21 00:40:46 +00004321 QualType Result = TL.getType();
4322 if (getDerived().AlwaysRebuild() ||
4323 E.get() != T->getUnderlyingExpr()) {
John McCall2a984ca2010-10-12 00:20:44 +00004324 Result = getDerived().RebuildDecltypeType(E.get(), TL.getNameLoc());
John McCalla2becad2009-10-21 00:40:46 +00004325 if (Result.isNull())
4326 return QualType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004327 }
John McCalla2becad2009-10-21 00:40:46 +00004328 else E.take();
Mike Stump1eb44332009-09-09 15:08:12 +00004329
John McCalla2becad2009-10-21 00:40:46 +00004330 DecltypeTypeLoc NewTL = TLB.push<DecltypeTypeLoc>(Result);
4331 NewTL.setNameLoc(TL.getNameLoc());
4332
4333 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004334}
4335
4336template<typename Derived>
Sean Huntca63c202011-05-24 22:41:36 +00004337QualType TreeTransform<Derived>::TransformUnaryTransformType(
4338 TypeLocBuilder &TLB,
4339 UnaryTransformTypeLoc TL) {
4340 QualType Result = TL.getType();
4341 if (Result->isDependentType()) {
4342 const UnaryTransformType *T = TL.getTypePtr();
4343 QualType NewBase =
4344 getDerived().TransformType(TL.getUnderlyingTInfo())->getType();
4345 Result = getDerived().RebuildUnaryTransformType(NewBase,
4346 T->getUTTKind(),
4347 TL.getKWLoc());
4348 if (Result.isNull())
4349 return QualType();
4350 }
4351
4352 UnaryTransformTypeLoc NewTL = TLB.push<UnaryTransformTypeLoc>(Result);
4353 NewTL.setKWLoc(TL.getKWLoc());
4354 NewTL.setParensRange(TL.getParensRange());
4355 NewTL.setUnderlyingTInfo(TL.getUnderlyingTInfo());
4356 return Result;
4357}
4358
4359template<typename Derived>
Richard Smith34b41d92011-02-20 03:19:35 +00004360QualType TreeTransform<Derived>::TransformAutoType(TypeLocBuilder &TLB,
4361 AutoTypeLoc TL) {
4362 const AutoType *T = TL.getTypePtr();
4363 QualType OldDeduced = T->getDeducedType();
4364 QualType NewDeduced;
4365 if (!OldDeduced.isNull()) {
4366 NewDeduced = getDerived().TransformType(OldDeduced);
4367 if (NewDeduced.isNull())
4368 return QualType();
4369 }
4370
4371 QualType Result = TL.getType();
4372 if (getDerived().AlwaysRebuild() || NewDeduced != OldDeduced) {
4373 Result = getDerived().RebuildAutoType(NewDeduced);
4374 if (Result.isNull())
4375 return QualType();
4376 }
4377
4378 AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
4379 NewTL.setNameLoc(TL.getNameLoc());
4380
4381 return Result;
4382}
4383
4384template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004385QualType TreeTransform<Derived>::TransformRecordType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004386 RecordTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004387 const RecordType *T = TL.getTypePtr();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004388 RecordDecl *Record
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004389 = cast_or_null<RecordDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4390 T->getDecl()));
Douglas Gregor577f75a2009-08-04 16:50:30 +00004391 if (!Record)
4392 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004393
John McCalla2becad2009-10-21 00:40:46 +00004394 QualType Result = TL.getType();
4395 if (getDerived().AlwaysRebuild() ||
4396 Record != T->getDecl()) {
4397 Result = getDerived().RebuildRecordType(Record);
4398 if (Result.isNull())
4399 return QualType();
4400 }
Mike Stump1eb44332009-09-09 15:08:12 +00004401
John McCalla2becad2009-10-21 00:40:46 +00004402 RecordTypeLoc NewTL = TLB.push<RecordTypeLoc>(Result);
4403 NewTL.setNameLoc(TL.getNameLoc());
4404
4405 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004406}
Mike Stump1eb44332009-09-09 15:08:12 +00004407
4408template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004409QualType TreeTransform<Derived>::TransformEnumType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004410 EnumTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004411 const EnumType *T = TL.getTypePtr();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004412 EnumDecl *Enum
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004413 = cast_or_null<EnumDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4414 T->getDecl()));
Douglas Gregor577f75a2009-08-04 16:50:30 +00004415 if (!Enum)
4416 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004417
John McCalla2becad2009-10-21 00:40:46 +00004418 QualType Result = TL.getType();
4419 if (getDerived().AlwaysRebuild() ||
4420 Enum != T->getDecl()) {
4421 Result = getDerived().RebuildEnumType(Enum);
4422 if (Result.isNull())
4423 return QualType();
4424 }
Mike Stump1eb44332009-09-09 15:08:12 +00004425
John McCalla2becad2009-10-21 00:40:46 +00004426 EnumTypeLoc NewTL = TLB.push<EnumTypeLoc>(Result);
4427 NewTL.setNameLoc(TL.getNameLoc());
4428
4429 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004430}
John McCall7da24312009-09-05 00:15:47 +00004431
John McCall3cb0ebd2010-03-10 03:28:59 +00004432template<typename Derived>
4433QualType TreeTransform<Derived>::TransformInjectedClassNameType(
4434 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004435 InjectedClassNameTypeLoc TL) {
John McCall3cb0ebd2010-03-10 03:28:59 +00004436 Decl *D = getDerived().TransformDecl(TL.getNameLoc(),
4437 TL.getTypePtr()->getDecl());
4438 if (!D) return QualType();
4439
4440 QualType T = SemaRef.Context.getTypeDeclType(cast<TypeDecl>(D));
4441 TLB.pushTypeSpec(T).setNameLoc(TL.getNameLoc());
4442 return T;
4443}
4444
Douglas Gregor577f75a2009-08-04 16:50:30 +00004445template<typename Derived>
4446QualType TreeTransform<Derived>::TransformTemplateTypeParmType(
John McCalla2becad2009-10-21 00:40:46 +00004447 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004448 TemplateTypeParmTypeLoc TL) {
John McCalla2becad2009-10-21 00:40:46 +00004449 return TransformTypeSpecType(TLB, TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +00004450}
4451
Mike Stump1eb44332009-09-09 15:08:12 +00004452template<typename Derived>
John McCall49a832b2009-10-18 09:09:24 +00004453QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmType(
John McCalla2becad2009-10-21 00:40:46 +00004454 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004455 SubstTemplateTypeParmTypeLoc TL) {
Douglas Gregor0b4bcb62011-03-05 17:19:27 +00004456 const SubstTemplateTypeParmType *T = TL.getTypePtr();
4457
4458 // Substitute into the replacement type, which itself might involve something
4459 // that needs to be transformed. This only tends to occur with default
4460 // template arguments of template template parameters.
4461 TemporaryBase Rebase(*this, TL.getNameLoc(), DeclarationName());
4462 QualType Replacement = getDerived().TransformType(T->getReplacementType());
4463 if (Replacement.isNull())
4464 return QualType();
4465
4466 // Always canonicalize the replacement type.
4467 Replacement = SemaRef.Context.getCanonicalType(Replacement);
4468 QualType Result
4469 = SemaRef.Context.getSubstTemplateTypeParmType(T->getReplacedParameter(),
4470 Replacement);
4471
4472 // Propagate type-source information.
4473 SubstTemplateTypeParmTypeLoc NewTL
4474 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
4475 NewTL.setNameLoc(TL.getNameLoc());
4476 return Result;
4477
John McCall49a832b2009-10-18 09:09:24 +00004478}
4479
4480template<typename Derived>
Douglas Gregorc3069d62011-01-14 02:55:32 +00004481QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmPackType(
4482 TypeLocBuilder &TLB,
4483 SubstTemplateTypeParmPackTypeLoc TL) {
4484 return TransformTypeSpecType(TLB, TL);
4485}
4486
4487template<typename Derived>
John McCall833ca992009-10-29 08:12:44 +00004488QualType TreeTransform<Derived>::TransformTemplateSpecializationType(
John McCall833ca992009-10-29 08:12:44 +00004489 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004490 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00004491 const TemplateSpecializationType *T = TL.getTypePtr();
4492
Douglas Gregor1d752d72011-03-02 18:46:51 +00004493 // The nested-name-specifier never matters in a TemplateSpecializationType,
4494 // because we can't have a dependent nested-name-specifier anyway.
4495 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +00004496 TemplateName Template
Douglas Gregor1d752d72011-03-02 18:46:51 +00004497 = getDerived().TransformTemplateName(SS, T->getTemplateName(),
4498 TL.getTemplateNameLoc());
Douglas Gregor577f75a2009-08-04 16:50:30 +00004499 if (Template.isNull())
4500 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004501
John McCall43fed0d2010-11-12 08:19:04 +00004502 return getDerived().TransformTemplateSpecializationType(TLB, TL, Template);
4503}
4504
Eli Friedmanb001de72011-10-06 23:00:33 +00004505template<typename Derived>
4506QualType TreeTransform<Derived>::TransformAtomicType(TypeLocBuilder &TLB,
4507 AtomicTypeLoc TL) {
4508 QualType ValueType = getDerived().TransformType(TLB, TL.getValueLoc());
4509 if (ValueType.isNull())
4510 return QualType();
4511
4512 QualType Result = TL.getType();
4513 if (getDerived().AlwaysRebuild() ||
4514 ValueType != TL.getValueLoc().getType()) {
4515 Result = getDerived().RebuildAtomicType(ValueType, TL.getKWLoc());
4516 if (Result.isNull())
4517 return QualType();
4518 }
4519
4520 AtomicTypeLoc NewTL = TLB.push<AtomicTypeLoc>(Result);
4521 NewTL.setKWLoc(TL.getKWLoc());
4522 NewTL.setLParenLoc(TL.getLParenLoc());
4523 NewTL.setRParenLoc(TL.getRParenLoc());
4524
4525 return Result;
4526}
4527
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00004528namespace {
4529 /// \brief Simple iterator that traverses the template arguments in a
4530 /// container that provides a \c getArgLoc() member function.
4531 ///
4532 /// This iterator is intended to be used with the iterator form of
4533 /// \c TreeTransform<Derived>::TransformTemplateArguments().
4534 template<typename ArgLocContainer>
4535 class TemplateArgumentLocContainerIterator {
4536 ArgLocContainer *Container;
4537 unsigned Index;
4538
4539 public:
4540 typedef TemplateArgumentLoc value_type;
4541 typedef TemplateArgumentLoc reference;
4542 typedef int difference_type;
4543 typedef std::input_iterator_tag iterator_category;
4544
4545 class pointer {
4546 TemplateArgumentLoc Arg;
4547
4548 public:
4549 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
4550
4551 const TemplateArgumentLoc *operator->() const {
4552 return &Arg;
4553 }
4554 };
4555
4556
4557 TemplateArgumentLocContainerIterator() {}
4558
4559 TemplateArgumentLocContainerIterator(ArgLocContainer &Container,
4560 unsigned Index)
4561 : Container(&Container), Index(Index) { }
4562
4563 TemplateArgumentLocContainerIterator &operator++() {
4564 ++Index;
4565 return *this;
4566 }
4567
4568 TemplateArgumentLocContainerIterator operator++(int) {
4569 TemplateArgumentLocContainerIterator Old(*this);
4570 ++(*this);
4571 return Old;
4572 }
4573
4574 TemplateArgumentLoc operator*() const {
4575 return Container->getArgLoc(Index);
4576 }
4577
4578 pointer operator->() const {
4579 return pointer(Container->getArgLoc(Index));
4580 }
4581
4582 friend bool operator==(const TemplateArgumentLocContainerIterator &X,
Douglas Gregorf7dd6992010-12-21 21:51:48 +00004583 const TemplateArgumentLocContainerIterator &Y) {
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00004584 return X.Container == Y.Container && X.Index == Y.Index;
4585 }
4586
4587 friend bool operator!=(const TemplateArgumentLocContainerIterator &X,
Douglas Gregorf7dd6992010-12-21 21:51:48 +00004588 const TemplateArgumentLocContainerIterator &Y) {
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00004589 return !(X == Y);
4590 }
4591 };
4592}
4593
4594
John McCall43fed0d2010-11-12 08:19:04 +00004595template <typename Derived>
4596QualType TreeTransform<Derived>::TransformTemplateSpecializationType(
4597 TypeLocBuilder &TLB,
4598 TemplateSpecializationTypeLoc TL,
4599 TemplateName Template) {
John McCalld5532b62009-11-23 01:53:49 +00004600 TemplateArgumentListInfo NewTemplateArgs;
4601 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4602 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00004603 typedef TemplateArgumentLocContainerIterator<TemplateSpecializationTypeLoc>
4604 ArgIterator;
4605 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4606 ArgIterator(TL, TL.getNumArgs()),
4607 NewTemplateArgs))
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00004608 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004609
John McCall833ca992009-10-29 08:12:44 +00004610 // FIXME: maybe don't rebuild if all the template arguments are the same.
4611
4612 QualType Result =
4613 getDerived().RebuildTemplateSpecializationType(Template,
4614 TL.getTemplateNameLoc(),
John McCalld5532b62009-11-23 01:53:49 +00004615 NewTemplateArgs);
John McCall833ca992009-10-29 08:12:44 +00004616
4617 if (!Result.isNull()) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00004618 // Specializations of template template parameters are represented as
4619 // TemplateSpecializationTypes, and substitution of type alias templates
4620 // within a dependent context can transform them into
4621 // DependentTemplateSpecializationTypes.
4622 if (isa<DependentTemplateSpecializationType>(Result)) {
4623 DependentTemplateSpecializationTypeLoc NewTL
4624 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004625 NewTL.setElaboratedKeywordLoc(SourceLocation());
Richard Smith3e4c6c42011-05-05 21:57:07 +00004626 NewTL.setQualifierLoc(NestedNameSpecifierLoc());
Abramo Bagnara66581d42012-02-06 22:45:07 +00004627 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004628 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Richard Smith3e4c6c42011-05-05 21:57:07 +00004629 NewTL.setLAngleLoc(TL.getLAngleLoc());
4630 NewTL.setRAngleLoc(TL.getRAngleLoc());
4631 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4632 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4633 return Result;
4634 }
4635
John McCall833ca992009-10-29 08:12:44 +00004636 TemplateSpecializationTypeLoc NewTL
4637 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004638 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
John McCall833ca992009-10-29 08:12:44 +00004639 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4640 NewTL.setLAngleLoc(TL.getLAngleLoc());
4641 NewTL.setRAngleLoc(TL.getRAngleLoc());
4642 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4643 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
Douglas Gregor577f75a2009-08-04 16:50:30 +00004644 }
Mike Stump1eb44332009-09-09 15:08:12 +00004645
John McCall833ca992009-10-29 08:12:44 +00004646 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004647}
Mike Stump1eb44332009-09-09 15:08:12 +00004648
Douglas Gregora88f09f2011-02-28 17:23:35 +00004649template <typename Derived>
4650QualType TreeTransform<Derived>::TransformDependentTemplateSpecializationType(
4651 TypeLocBuilder &TLB,
4652 DependentTemplateSpecializationTypeLoc TL,
Douglas Gregor087eb5a2011-03-04 18:53:13 +00004653 TemplateName Template,
4654 CXXScopeSpec &SS) {
Douglas Gregora88f09f2011-02-28 17:23:35 +00004655 TemplateArgumentListInfo NewTemplateArgs;
4656 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4657 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
4658 typedef TemplateArgumentLocContainerIterator<
4659 DependentTemplateSpecializationTypeLoc> ArgIterator;
4660 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4661 ArgIterator(TL, TL.getNumArgs()),
4662 NewTemplateArgs))
4663 return QualType();
4664
4665 // FIXME: maybe don't rebuild if all the template arguments are the same.
4666
4667 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
4668 QualType Result
4669 = getSema().Context.getDependentTemplateSpecializationType(
4670 TL.getTypePtr()->getKeyword(),
4671 DTN->getQualifier(),
4672 DTN->getIdentifier(),
4673 NewTemplateArgs);
4674
4675 DependentTemplateSpecializationTypeLoc NewTL
4676 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004677 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004678 NewTL.setQualifierLoc(SS.getWithLocInContext(SemaRef.Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004679 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004680 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregora88f09f2011-02-28 17:23:35 +00004681 NewTL.setLAngleLoc(TL.getLAngleLoc());
4682 NewTL.setRAngleLoc(TL.getRAngleLoc());
4683 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4684 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4685 return Result;
4686 }
4687
4688 QualType Result
4689 = getDerived().RebuildTemplateSpecializationType(Template,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004690 TL.getTemplateNameLoc(),
Douglas Gregora88f09f2011-02-28 17:23:35 +00004691 NewTemplateArgs);
4692
4693 if (!Result.isNull()) {
4694 /// FIXME: Wrap this in an elaborated-type-specifier?
4695 TemplateSpecializationTypeLoc NewTL
4696 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara66581d42012-02-06 22:45:07 +00004697 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004698 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregora88f09f2011-02-28 17:23:35 +00004699 NewTL.setLAngleLoc(TL.getLAngleLoc());
4700 NewTL.setRAngleLoc(TL.getRAngleLoc());
4701 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4702 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4703 }
4704
4705 return Result;
4706}
4707
Mike Stump1eb44332009-09-09 15:08:12 +00004708template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004709QualType
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004710TreeTransform<Derived>::TransformElaboratedType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004711 ElaboratedTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004712 const ElaboratedType *T = TL.getTypePtr();
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004713
Douglas Gregor9e876872011-03-01 18:12:44 +00004714 NestedNameSpecifierLoc QualifierLoc;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004715 // NOTE: the qualifier in an ElaboratedType is optional.
Douglas Gregor9e876872011-03-01 18:12:44 +00004716 if (TL.getQualifierLoc()) {
4717 QualifierLoc
4718 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4719 if (!QualifierLoc)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004720 return QualType();
4721 }
Mike Stump1eb44332009-09-09 15:08:12 +00004722
John McCall43fed0d2010-11-12 08:19:04 +00004723 QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
4724 if (NamedT.isNull())
4725 return QualType();
Daniel Dunbara63db842010-05-14 16:34:09 +00004726
Richard Smith3e4c6c42011-05-05 21:57:07 +00004727 // C++0x [dcl.type.elab]p2:
4728 // If the identifier resolves to a typedef-name or the simple-template-id
4729 // resolves to an alias template specialization, the
4730 // elaborated-type-specifier is ill-formed.
Richard Smith18041742011-05-14 15:04:18 +00004731 if (T->getKeyword() != ETK_None && T->getKeyword() != ETK_Typename) {
4732 if (const TemplateSpecializationType *TST =
4733 NamedT->getAs<TemplateSpecializationType>()) {
4734 TemplateName Template = TST->getTemplateName();
4735 if (TypeAliasTemplateDecl *TAT =
4736 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
4737 SemaRef.Diag(TL.getNamedTypeLoc().getBeginLoc(),
4738 diag::err_tag_reference_non_tag) << 4;
4739 SemaRef.Diag(TAT->getLocation(), diag::note_declared_at);
4740 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00004741 }
4742 }
4743
John McCalla2becad2009-10-21 00:40:46 +00004744 QualType Result = TL.getType();
4745 if (getDerived().AlwaysRebuild() ||
Douglas Gregor9e876872011-03-01 18:12:44 +00004746 QualifierLoc != TL.getQualifierLoc() ||
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004747 NamedT != T->getNamedType()) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004748 Result = getDerived().RebuildElaboratedType(TL.getElaboratedKeywordLoc(),
Douglas Gregor9e876872011-03-01 18:12:44 +00004749 T->getKeyword(),
4750 QualifierLoc, NamedT);
John McCalla2becad2009-10-21 00:40:46 +00004751 if (Result.isNull())
4752 return QualType();
4753 }
Douglas Gregor577f75a2009-08-04 16:50:30 +00004754
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004755 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00004756 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor9e876872011-03-01 18:12:44 +00004757 NewTL.setQualifierLoc(QualifierLoc);
John McCalla2becad2009-10-21 00:40:46 +00004758 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004759}
Mike Stump1eb44332009-09-09 15:08:12 +00004760
4761template<typename Derived>
John McCall9d156a72011-01-06 01:58:22 +00004762QualType TreeTransform<Derived>::TransformAttributedType(
4763 TypeLocBuilder &TLB,
4764 AttributedTypeLoc TL) {
4765 const AttributedType *oldType = TL.getTypePtr();
4766 QualType modifiedType = getDerived().TransformType(TLB, TL.getModifiedLoc());
4767 if (modifiedType.isNull())
4768 return QualType();
4769
4770 QualType result = TL.getType();
4771
4772 // FIXME: dependent operand expressions?
4773 if (getDerived().AlwaysRebuild() ||
4774 modifiedType != oldType->getModifiedType()) {
4775 // TODO: this is really lame; we should really be rebuilding the
4776 // equivalent type from first principles.
4777 QualType equivalentType
4778 = getDerived().TransformType(oldType->getEquivalentType());
4779 if (equivalentType.isNull())
4780 return QualType();
4781 result = SemaRef.Context.getAttributedType(oldType->getAttrKind(),
4782 modifiedType,
4783 equivalentType);
4784 }
4785
4786 AttributedTypeLoc newTL = TLB.push<AttributedTypeLoc>(result);
4787 newTL.setAttrNameLoc(TL.getAttrNameLoc());
4788 if (TL.hasAttrOperand())
4789 newTL.setAttrOperandParensRange(TL.getAttrOperandParensRange());
4790 if (TL.hasAttrExprOperand())
4791 newTL.setAttrExprOperand(TL.getAttrExprOperand());
4792 else if (TL.hasAttrEnumOperand())
4793 newTL.setAttrEnumOperandLoc(TL.getAttrEnumOperandLoc());
4794
4795 return result;
4796}
4797
4798template<typename Derived>
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004799QualType
4800TreeTransform<Derived>::TransformParenType(TypeLocBuilder &TLB,
4801 ParenTypeLoc TL) {
4802 QualType Inner = getDerived().TransformType(TLB, TL.getInnerLoc());
4803 if (Inner.isNull())
4804 return QualType();
4805
4806 QualType Result = TL.getType();
4807 if (getDerived().AlwaysRebuild() ||
4808 Inner != TL.getInnerLoc().getType()) {
4809 Result = getDerived().RebuildParenType(Inner);
4810 if (Result.isNull())
4811 return QualType();
4812 }
4813
4814 ParenTypeLoc NewTL = TLB.push<ParenTypeLoc>(Result);
4815 NewTL.setLParenLoc(TL.getLParenLoc());
4816 NewTL.setRParenLoc(TL.getRParenLoc());
4817 return Result;
4818}
4819
4820template<typename Derived>
Douglas Gregor4714c122010-03-31 17:34:00 +00004821QualType TreeTransform<Derived>::TransformDependentNameType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004822 DependentNameTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004823 const DependentNameType *T = TL.getTypePtr();
John McCall833ca992009-10-29 08:12:44 +00004824
Douglas Gregor2494dd02011-03-01 01:34:45 +00004825 NestedNameSpecifierLoc QualifierLoc
4826 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4827 if (!QualifierLoc)
Douglas Gregor577f75a2009-08-04 16:50:30 +00004828 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004829
John McCall33500952010-06-11 00:33:02 +00004830 QualType Result
Douglas Gregor2494dd02011-03-01 01:34:45 +00004831 = getDerived().RebuildDependentNameType(T->getKeyword(),
Abramo Bagnara38a42912012-02-06 19:09:27 +00004832 TL.getElaboratedKeywordLoc(),
Douglas Gregor2494dd02011-03-01 01:34:45 +00004833 QualifierLoc,
4834 T->getIdentifier(),
John McCall33500952010-06-11 00:33:02 +00004835 TL.getNameLoc());
John McCalla2becad2009-10-21 00:40:46 +00004836 if (Result.isNull())
4837 return QualType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004838
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004839 if (const ElaboratedType* ElabT = Result->getAs<ElaboratedType>()) {
4840 QualType NamedT = ElabT->getNamedType();
John McCall33500952010-06-11 00:33:02 +00004841 TLB.pushTypeSpec(NamedT).setNameLoc(TL.getNameLoc());
4842
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004843 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00004844 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor9e876872011-03-01 18:12:44 +00004845 NewTL.setQualifierLoc(QualifierLoc);
John McCall33500952010-06-11 00:33:02 +00004846 } else {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004847 DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00004848 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor2494dd02011-03-01 01:34:45 +00004849 NewTL.setQualifierLoc(QualifierLoc);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004850 NewTL.setNameLoc(TL.getNameLoc());
4851 }
John McCalla2becad2009-10-21 00:40:46 +00004852 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004853}
Mike Stump1eb44332009-09-09 15:08:12 +00004854
Douglas Gregor577f75a2009-08-04 16:50:30 +00004855template<typename Derived>
John McCall33500952010-06-11 00:33:02 +00004856QualType TreeTransform<Derived>::
4857 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004858 DependentTemplateSpecializationTypeLoc TL) {
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004859 NestedNameSpecifierLoc QualifierLoc;
4860 if (TL.getQualifierLoc()) {
4861 QualifierLoc
4862 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4863 if (!QualifierLoc)
Douglas Gregora88f09f2011-02-28 17:23:35 +00004864 return QualType();
4865 }
4866
John McCall43fed0d2010-11-12 08:19:04 +00004867 return getDerived()
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004868 .TransformDependentTemplateSpecializationType(TLB, TL, QualifierLoc);
John McCall43fed0d2010-11-12 08:19:04 +00004869}
4870
4871template<typename Derived>
4872QualType TreeTransform<Derived>::
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004873TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
4874 DependentTemplateSpecializationTypeLoc TL,
4875 NestedNameSpecifierLoc QualifierLoc) {
4876 const DependentTemplateSpecializationType *T = TL.getTypePtr();
4877
4878 TemplateArgumentListInfo NewTemplateArgs;
4879 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4880 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
4881
4882 typedef TemplateArgumentLocContainerIterator<
4883 DependentTemplateSpecializationTypeLoc> ArgIterator;
4884 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4885 ArgIterator(TL, TL.getNumArgs()),
4886 NewTemplateArgs))
4887 return QualType();
4888
4889 QualType Result
4890 = getDerived().RebuildDependentTemplateSpecializationType(T->getKeyword(),
4891 QualifierLoc,
4892 T->getIdentifier(),
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004893 TL.getTemplateNameLoc(),
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004894 NewTemplateArgs);
4895 if (Result.isNull())
4896 return QualType();
4897
4898 if (const ElaboratedType *ElabT = dyn_cast<ElaboratedType>(Result)) {
4899 QualType NamedT = ElabT->getNamedType();
4900
4901 // Copy information relevant to the template specialization.
4902 TemplateSpecializationTypeLoc NamedTL
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004903 = TLB.push<TemplateSpecializationTypeLoc>(NamedT);
Abramo Bagnara66581d42012-02-06 22:45:07 +00004904 NamedTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004905 NamedTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004906 NamedTL.setLAngleLoc(TL.getLAngleLoc());
4907 NamedTL.setRAngleLoc(TL.getRAngleLoc());
Douglas Gregor944cdae2011-03-07 15:13:34 +00004908 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004909 NamedTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004910
4911 // Copy information relevant to the elaborated type.
4912 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00004913 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004914 NewTL.setQualifierLoc(QualifierLoc);
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004915 } else if (isa<DependentTemplateSpecializationType>(Result)) {
4916 DependentTemplateSpecializationTypeLoc SpecTL
4917 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004918 SpecTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004919 SpecTL.setQualifierLoc(QualifierLoc);
Abramo Bagnara66581d42012-02-06 22:45:07 +00004920 SpecTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004921 SpecTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004922 SpecTL.setLAngleLoc(TL.getLAngleLoc());
4923 SpecTL.setRAngleLoc(TL.getRAngleLoc());
Douglas Gregor944cdae2011-03-07 15:13:34 +00004924 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004925 SpecTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004926 } else {
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004927 TemplateSpecializationTypeLoc SpecTL
4928 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara66581d42012-02-06 22:45:07 +00004929 SpecTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004930 SpecTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004931 SpecTL.setLAngleLoc(TL.getLAngleLoc());
4932 SpecTL.setRAngleLoc(TL.getRAngleLoc());
Douglas Gregor944cdae2011-03-07 15:13:34 +00004933 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004934 SpecTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004935 }
4936 return Result;
4937}
4938
4939template<typename Derived>
Douglas Gregor7536dd52010-12-20 02:24:11 +00004940QualType TreeTransform<Derived>::TransformPackExpansionType(TypeLocBuilder &TLB,
4941 PackExpansionTypeLoc TL) {
Douglas Gregor2fc1bb72011-01-12 17:07:58 +00004942 QualType Pattern
4943 = getDerived().TransformType(TLB, TL.getPatternLoc());
4944 if (Pattern.isNull())
4945 return QualType();
4946
4947 QualType Result = TL.getType();
4948 if (getDerived().AlwaysRebuild() ||
4949 Pattern != TL.getPatternLoc().getType()) {
4950 Result = getDerived().RebuildPackExpansionType(Pattern,
4951 TL.getPatternLoc().getSourceRange(),
Douglas Gregorcded4f62011-01-14 17:04:44 +00004952 TL.getEllipsisLoc(),
4953 TL.getTypePtr()->getNumExpansions());
Douglas Gregor2fc1bb72011-01-12 17:07:58 +00004954 if (Result.isNull())
4955 return QualType();
4956 }
4957
4958 PackExpansionTypeLoc NewT = TLB.push<PackExpansionTypeLoc>(Result);
4959 NewT.setEllipsisLoc(TL.getEllipsisLoc());
4960 return Result;
Douglas Gregor7536dd52010-12-20 02:24:11 +00004961}
4962
4963template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004964QualType
4965TreeTransform<Derived>::TransformObjCInterfaceType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004966 ObjCInterfaceTypeLoc TL) {
Douglas Gregoref57c612010-04-22 17:28:13 +00004967 // ObjCInterfaceType is never dependent.
John McCallc12c5bb2010-05-15 11:32:37 +00004968 TLB.pushFullCopy(TL);
4969 return TL.getType();
4970}
4971
4972template<typename Derived>
4973QualType
4974TreeTransform<Derived>::TransformObjCObjectType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004975 ObjCObjectTypeLoc TL) {
John McCallc12c5bb2010-05-15 11:32:37 +00004976 // ObjCObjectType is never dependent.
4977 TLB.pushFullCopy(TL);
Douglas Gregoref57c612010-04-22 17:28:13 +00004978 return TL.getType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004979}
Mike Stump1eb44332009-09-09 15:08:12 +00004980
4981template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004982QualType
4983TreeTransform<Derived>::TransformObjCObjectPointerType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004984 ObjCObjectPointerTypeLoc TL) {
Douglas Gregoref57c612010-04-22 17:28:13 +00004985 // ObjCObjectPointerType is never dependent.
John McCallc12c5bb2010-05-15 11:32:37 +00004986 TLB.pushFullCopy(TL);
Douglas Gregoref57c612010-04-22 17:28:13 +00004987 return TL.getType();
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004988}
4989
Douglas Gregor577f75a2009-08-04 16:50:30 +00004990//===----------------------------------------------------------------------===//
Douglas Gregor43959a92009-08-20 07:17:43 +00004991// Statement transformation
4992//===----------------------------------------------------------------------===//
4993template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00004994StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00004995TreeTransform<Derived>::TransformNullStmt(NullStmt *S) {
John McCall3fa5cae2010-10-26 07:05:15 +00004996 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00004997}
4998
4999template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005000StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005001TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S) {
5002 return getDerived().TransformCompoundStmt(S, false);
5003}
5004
5005template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005006StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005007TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S,
Douglas Gregor43959a92009-08-20 07:17:43 +00005008 bool IsStmtExpr) {
Dmitri Gribenko625bb562012-02-14 22:14:32 +00005009 Sema::CompoundScopeRAII CompoundScope(getSema());
5010
John McCall7114cba2010-08-27 19:56:05 +00005011 bool SubStmtInvalid = false;
Douglas Gregor43959a92009-08-20 07:17:43 +00005012 bool SubStmtChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00005013 ASTOwningVector<Stmt*> Statements(getSema());
Douglas Gregor43959a92009-08-20 07:17:43 +00005014 for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end();
5015 B != BEnd; ++B) {
John McCall60d7b3a2010-08-24 06:29:42 +00005016 StmtResult Result = getDerived().TransformStmt(*B);
John McCall7114cba2010-08-27 19:56:05 +00005017 if (Result.isInvalid()) {
5018 // Immediately fail if this was a DeclStmt, since it's very
5019 // likely that this will cause problems for future statements.
5020 if (isa<DeclStmt>(*B))
5021 return StmtError();
5022
5023 // Otherwise, just keep processing substatements and fail later.
5024 SubStmtInvalid = true;
5025 continue;
5026 }
Mike Stump1eb44332009-09-09 15:08:12 +00005027
Douglas Gregor43959a92009-08-20 07:17:43 +00005028 SubStmtChanged = SubStmtChanged || Result.get() != *B;
5029 Statements.push_back(Result.takeAs<Stmt>());
5030 }
Mike Stump1eb44332009-09-09 15:08:12 +00005031
John McCall7114cba2010-08-27 19:56:05 +00005032 if (SubStmtInvalid)
5033 return StmtError();
5034
Douglas Gregor43959a92009-08-20 07:17:43 +00005035 if (!getDerived().AlwaysRebuild() &&
5036 !SubStmtChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00005037 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005038
5039 return getDerived().RebuildCompoundStmt(S->getLBracLoc(),
5040 move_arg(Statements),
5041 S->getRBracLoc(),
5042 IsStmtExpr);
5043}
Mike Stump1eb44332009-09-09 15:08:12 +00005044
Douglas Gregor43959a92009-08-20 07:17:43 +00005045template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005046StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005047TreeTransform<Derived>::TransformCaseStmt(CaseStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005048 ExprResult LHS, RHS;
Eli Friedman264c1f82009-11-19 03:14:00 +00005049 {
Eli Friedman6b3014b2012-01-18 02:54:10 +00005050 EnterExpressionEvaluationContext Unevaluated(SemaRef,
5051 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00005052
Eli Friedman264c1f82009-11-19 03:14:00 +00005053 // Transform the left-hand case value.
5054 LHS = getDerived().TransformExpr(S->getLHS());
5055 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005056 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005057
Eli Friedman264c1f82009-11-19 03:14:00 +00005058 // Transform the right-hand case value (for the GNU case-range extension).
5059 RHS = getDerived().TransformExpr(S->getRHS());
5060 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005061 return StmtError();
Eli Friedman264c1f82009-11-19 03:14:00 +00005062 }
Mike Stump1eb44332009-09-09 15:08:12 +00005063
Douglas Gregor43959a92009-08-20 07:17:43 +00005064 // Build the case statement.
5065 // Case statements are always rebuilt so that they will attached to their
5066 // transformed switch statement.
John McCall60d7b3a2010-08-24 06:29:42 +00005067 StmtResult Case = getDerived().RebuildCaseStmt(S->getCaseLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005068 LHS.get(),
Douglas Gregor43959a92009-08-20 07:17:43 +00005069 S->getEllipsisLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005070 RHS.get(),
Douglas Gregor43959a92009-08-20 07:17:43 +00005071 S->getColonLoc());
5072 if (Case.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005073 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005074
Douglas Gregor43959a92009-08-20 07:17:43 +00005075 // Transform the statement following the case
John McCall60d7b3a2010-08-24 06:29:42 +00005076 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
Douglas Gregor43959a92009-08-20 07:17:43 +00005077 if (SubStmt.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005078 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005079
Douglas Gregor43959a92009-08-20 07:17:43 +00005080 // Attach the body to the case statement
John McCall9ae2f072010-08-23 23:25:46 +00005081 return getDerived().RebuildCaseStmtBody(Case.get(), SubStmt.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005082}
5083
5084template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005085StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005086TreeTransform<Derived>::TransformDefaultStmt(DefaultStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005087 // Transform the statement following the default case
John McCall60d7b3a2010-08-24 06:29:42 +00005088 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
Douglas Gregor43959a92009-08-20 07:17:43 +00005089 if (SubStmt.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005090 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005091
Douglas Gregor43959a92009-08-20 07:17:43 +00005092 // Default statements are always rebuilt
5093 return getDerived().RebuildDefaultStmt(S->getDefaultLoc(), S->getColonLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005094 SubStmt.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005095}
Mike Stump1eb44332009-09-09 15:08:12 +00005096
Douglas Gregor43959a92009-08-20 07:17:43 +00005097template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005098StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005099TreeTransform<Derived>::TransformLabelStmt(LabelStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005100 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
Douglas Gregor43959a92009-08-20 07:17:43 +00005101 if (SubStmt.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005102 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005103
Chris Lattner57ad3782011-02-17 20:34:02 +00005104 Decl *LD = getDerived().TransformDecl(S->getDecl()->getLocation(),
5105 S->getDecl());
5106 if (!LD)
5107 return StmtError();
5108
5109
Douglas Gregor43959a92009-08-20 07:17:43 +00005110 // FIXME: Pass the real colon location in.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00005111 return getDerived().RebuildLabelStmt(S->getIdentLoc(),
Chris Lattner57ad3782011-02-17 20:34:02 +00005112 cast<LabelDecl>(LD), SourceLocation(),
5113 SubStmt.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005114}
Mike Stump1eb44332009-09-09 15:08:12 +00005115
Douglas Gregor43959a92009-08-20 07:17:43 +00005116template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005117StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005118TreeTransform<Derived>::TransformIfStmt(IfStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005119 // Transform the condition
John McCall60d7b3a2010-08-24 06:29:42 +00005120 ExprResult Cond;
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00005121 VarDecl *ConditionVar = 0;
5122 if (S->getConditionVariable()) {
Sean Huntc3021132010-05-05 15:23:54 +00005123 ConditionVar
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00005124 = cast_or_null<VarDecl>(
Douglas Gregoraac571c2010-03-01 17:25:41 +00005125 getDerived().TransformDefinition(
5126 S->getConditionVariable()->getLocation(),
5127 S->getConditionVariable()));
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00005128 if (!ConditionVar)
John McCallf312b1e2010-08-26 23:41:50 +00005129 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005130 } else {
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00005131 Cond = getDerived().TransformExpr(S->getCond());
Sean Huntc3021132010-05-05 15:23:54 +00005132
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005133 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005134 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005135
5136 // Convert the condition to a boolean value.
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005137 if (S->getCond()) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +00005138 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getIfLoc(),
5139 Cond.get());
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005140 if (CondE.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005141 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005142
John McCall9ae2f072010-08-23 23:25:46 +00005143 Cond = CondE.get();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005144 }
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005145 }
Sean Huntc3021132010-05-05 15:23:54 +00005146
John McCall9ae2f072010-08-23 23:25:46 +00005147 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5148 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
John McCallf312b1e2010-08-26 23:41:50 +00005149 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005150
Douglas Gregor43959a92009-08-20 07:17:43 +00005151 // Transform the "then" branch.
John McCall60d7b3a2010-08-24 06:29:42 +00005152 StmtResult Then = getDerived().TransformStmt(S->getThen());
Douglas Gregor43959a92009-08-20 07:17:43 +00005153 if (Then.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005154 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005155
Douglas Gregor43959a92009-08-20 07:17:43 +00005156 // Transform the "else" branch.
John McCall60d7b3a2010-08-24 06:29:42 +00005157 StmtResult Else = getDerived().TransformStmt(S->getElse());
Douglas Gregor43959a92009-08-20 07:17:43 +00005158 if (Else.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005159 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005160
Douglas Gregor43959a92009-08-20 07:17:43 +00005161 if (!getDerived().AlwaysRebuild() &&
John McCall9ae2f072010-08-23 23:25:46 +00005162 FullCond.get() == S->getCond() &&
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005163 ConditionVar == S->getConditionVariable() &&
Douglas Gregor43959a92009-08-20 07:17:43 +00005164 Then.get() == S->getThen() &&
5165 Else.get() == S->getElse())
John McCall3fa5cae2010-10-26 07:05:15 +00005166 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005167
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005168 return getDerived().RebuildIfStmt(S->getIfLoc(), FullCond, ConditionVar,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00005169 Then.get(),
John McCall9ae2f072010-08-23 23:25:46 +00005170 S->getElseLoc(), Else.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005171}
5172
5173template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005174StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005175TreeTransform<Derived>::TransformSwitchStmt(SwitchStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005176 // Transform the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00005177 ExprResult Cond;
Douglas Gregord3d53012009-11-24 17:07:59 +00005178 VarDecl *ConditionVar = 0;
5179 if (S->getConditionVariable()) {
Sean Huntc3021132010-05-05 15:23:54 +00005180 ConditionVar
Douglas Gregord3d53012009-11-24 17:07:59 +00005181 = cast_or_null<VarDecl>(
Douglas Gregoraac571c2010-03-01 17:25:41 +00005182 getDerived().TransformDefinition(
5183 S->getConditionVariable()->getLocation(),
5184 S->getConditionVariable()));
Douglas Gregord3d53012009-11-24 17:07:59 +00005185 if (!ConditionVar)
John McCallf312b1e2010-08-26 23:41:50 +00005186 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005187 } else {
Douglas Gregord3d53012009-11-24 17:07:59 +00005188 Cond = getDerived().TransformExpr(S->getCond());
Sean Huntc3021132010-05-05 15:23:54 +00005189
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005190 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005191 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005192 }
Mike Stump1eb44332009-09-09 15:08:12 +00005193
Douglas Gregor43959a92009-08-20 07:17:43 +00005194 // Rebuild the switch statement.
John McCall60d7b3a2010-08-24 06:29:42 +00005195 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00005196 = getDerived().RebuildSwitchStmtStart(S->getSwitchLoc(), Cond.get(),
Douglas Gregor586596f2010-05-06 17:25:47 +00005197 ConditionVar);
Douglas Gregor43959a92009-08-20 07:17:43 +00005198 if (Switch.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005199 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005200
Douglas Gregor43959a92009-08-20 07:17:43 +00005201 // Transform the body of the switch statement.
John McCall60d7b3a2010-08-24 06:29:42 +00005202 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregor43959a92009-08-20 07:17:43 +00005203 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005204 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005205
Douglas Gregor43959a92009-08-20 07:17:43 +00005206 // Complete the switch statement.
John McCall9ae2f072010-08-23 23:25:46 +00005207 return getDerived().RebuildSwitchStmtBody(S->getSwitchLoc(), Switch.get(),
5208 Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005209}
Mike Stump1eb44332009-09-09 15:08:12 +00005210
Douglas Gregor43959a92009-08-20 07:17:43 +00005211template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005212StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005213TreeTransform<Derived>::TransformWhileStmt(WhileStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005214 // Transform the condition
John McCall60d7b3a2010-08-24 06:29:42 +00005215 ExprResult Cond;
Douglas Gregor5656e142009-11-24 21:15:44 +00005216 VarDecl *ConditionVar = 0;
5217 if (S->getConditionVariable()) {
Sean Huntc3021132010-05-05 15:23:54 +00005218 ConditionVar
Douglas Gregor5656e142009-11-24 21:15:44 +00005219 = cast_or_null<VarDecl>(
Douglas Gregoraac571c2010-03-01 17:25:41 +00005220 getDerived().TransformDefinition(
5221 S->getConditionVariable()->getLocation(),
5222 S->getConditionVariable()));
Douglas Gregor5656e142009-11-24 21:15:44 +00005223 if (!ConditionVar)
John McCallf312b1e2010-08-26 23:41:50 +00005224 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005225 } else {
Douglas Gregor5656e142009-11-24 21:15:44 +00005226 Cond = getDerived().TransformExpr(S->getCond());
Sean Huntc3021132010-05-05 15:23:54 +00005227
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005228 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005229 return StmtError();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005230
5231 if (S->getCond()) {
5232 // Convert the condition to a boolean value.
Douglas Gregor8491ffe2010-12-20 22:05:00 +00005233 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getWhileLoc(),
5234 Cond.get());
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005235 if (CondE.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005236 return StmtError();
John McCall9ae2f072010-08-23 23:25:46 +00005237 Cond = CondE;
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005238 }
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005239 }
Mike Stump1eb44332009-09-09 15:08:12 +00005240
John McCall9ae2f072010-08-23 23:25:46 +00005241 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5242 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
John McCallf312b1e2010-08-26 23:41:50 +00005243 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005244
Douglas Gregor43959a92009-08-20 07:17:43 +00005245 // Transform the body
John McCall60d7b3a2010-08-24 06:29:42 +00005246 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregor43959a92009-08-20 07:17:43 +00005247 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005248 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005249
Douglas Gregor43959a92009-08-20 07:17:43 +00005250 if (!getDerived().AlwaysRebuild() &&
John McCall9ae2f072010-08-23 23:25:46 +00005251 FullCond.get() == S->getCond() &&
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005252 ConditionVar == S->getConditionVariable() &&
Douglas Gregor43959a92009-08-20 07:17:43 +00005253 Body.get() == S->getBody())
John McCall9ae2f072010-08-23 23:25:46 +00005254 return Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005255
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005256 return getDerived().RebuildWhileStmt(S->getWhileLoc(), FullCond,
John McCall9ae2f072010-08-23 23:25:46 +00005257 ConditionVar, Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005258}
Mike Stump1eb44332009-09-09 15:08:12 +00005259
Douglas Gregor43959a92009-08-20 07:17:43 +00005260template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005261StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005262TreeTransform<Derived>::TransformDoStmt(DoStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005263 // Transform the body
John McCall60d7b3a2010-08-24 06:29:42 +00005264 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregor43959a92009-08-20 07:17:43 +00005265 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005266 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005267
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005268 // Transform the condition
John McCall60d7b3a2010-08-24 06:29:42 +00005269 ExprResult Cond = getDerived().TransformExpr(S->getCond());
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005270 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005271 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005272
Douglas Gregor43959a92009-08-20 07:17:43 +00005273 if (!getDerived().AlwaysRebuild() &&
5274 Cond.get() == S->getCond() &&
5275 Body.get() == S->getBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005276 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005277
John McCall9ae2f072010-08-23 23:25:46 +00005278 return getDerived().RebuildDoStmt(S->getDoLoc(), Body.get(), S->getWhileLoc(),
5279 /*FIXME:*/S->getWhileLoc(), Cond.get(),
Douglas Gregor43959a92009-08-20 07:17:43 +00005280 S->getRParenLoc());
5281}
Mike Stump1eb44332009-09-09 15:08:12 +00005282
Douglas Gregor43959a92009-08-20 07:17:43 +00005283template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005284StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005285TreeTransform<Derived>::TransformForStmt(ForStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005286 // Transform the initialization statement
John McCall60d7b3a2010-08-24 06:29:42 +00005287 StmtResult Init = getDerived().TransformStmt(S->getInit());
Douglas Gregor43959a92009-08-20 07:17:43 +00005288 if (Init.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005289 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005290
Douglas Gregor43959a92009-08-20 07:17:43 +00005291 // Transform the condition
John McCall60d7b3a2010-08-24 06:29:42 +00005292 ExprResult Cond;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005293 VarDecl *ConditionVar = 0;
5294 if (S->getConditionVariable()) {
Sean Huntc3021132010-05-05 15:23:54 +00005295 ConditionVar
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005296 = cast_or_null<VarDecl>(
Douglas Gregoraac571c2010-03-01 17:25:41 +00005297 getDerived().TransformDefinition(
5298 S->getConditionVariable()->getLocation(),
5299 S->getConditionVariable()));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005300 if (!ConditionVar)
John McCallf312b1e2010-08-26 23:41:50 +00005301 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005302 } else {
5303 Cond = getDerived().TransformExpr(S->getCond());
Sean Huntc3021132010-05-05 15:23:54 +00005304
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005305 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005306 return StmtError();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005307
5308 if (S->getCond()) {
5309 // Convert the condition to a boolean value.
Douglas Gregor8491ffe2010-12-20 22:05:00 +00005310 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getForLoc(),
5311 Cond.get());
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005312 if (CondE.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005313 return StmtError();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005314
John McCall9ae2f072010-08-23 23:25:46 +00005315 Cond = CondE.get();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005316 }
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005317 }
Mike Stump1eb44332009-09-09 15:08:12 +00005318
John McCall9ae2f072010-08-23 23:25:46 +00005319 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5320 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
John McCallf312b1e2010-08-26 23:41:50 +00005321 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005322
Douglas Gregor43959a92009-08-20 07:17:43 +00005323 // Transform the increment
John McCall60d7b3a2010-08-24 06:29:42 +00005324 ExprResult Inc = getDerived().TransformExpr(S->getInc());
Douglas Gregor43959a92009-08-20 07:17:43 +00005325 if (Inc.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005326 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005327
John McCall9ae2f072010-08-23 23:25:46 +00005328 Sema::FullExprArg FullInc(getSema().MakeFullExpr(Inc.get()));
5329 if (S->getInc() && !FullInc.get())
John McCallf312b1e2010-08-26 23:41:50 +00005330 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005331
Douglas Gregor43959a92009-08-20 07:17:43 +00005332 // Transform the body
John McCall60d7b3a2010-08-24 06:29:42 +00005333 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregor43959a92009-08-20 07:17:43 +00005334 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005335 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005336
Douglas Gregor43959a92009-08-20 07:17:43 +00005337 if (!getDerived().AlwaysRebuild() &&
5338 Init.get() == S->getInit() &&
John McCall9ae2f072010-08-23 23:25:46 +00005339 FullCond.get() == S->getCond() &&
Douglas Gregor43959a92009-08-20 07:17:43 +00005340 Inc.get() == S->getInc() &&
5341 Body.get() == S->getBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005342 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005343
Douglas Gregor43959a92009-08-20 07:17:43 +00005344 return getDerived().RebuildForStmt(S->getForLoc(), S->getLParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005345 Init.get(), FullCond, ConditionVar,
5346 FullInc, S->getRParenLoc(), Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005347}
5348
5349template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005350StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005351TreeTransform<Derived>::TransformGotoStmt(GotoStmt *S) {
Chris Lattner57ad3782011-02-17 20:34:02 +00005352 Decl *LD = getDerived().TransformDecl(S->getLabel()->getLocation(),
5353 S->getLabel());
5354 if (!LD)
5355 return StmtError();
5356
Douglas Gregor43959a92009-08-20 07:17:43 +00005357 // Goto statements must always be rebuilt, to resolve the label.
Mike Stump1eb44332009-09-09 15:08:12 +00005358 return getDerived().RebuildGotoStmt(S->getGotoLoc(), S->getLabelLoc(),
Chris Lattner57ad3782011-02-17 20:34:02 +00005359 cast<LabelDecl>(LD));
Douglas Gregor43959a92009-08-20 07:17:43 +00005360}
5361
5362template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005363StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005364TreeTransform<Derived>::TransformIndirectGotoStmt(IndirectGotoStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005365 ExprResult Target = getDerived().TransformExpr(S->getTarget());
Douglas Gregor43959a92009-08-20 07:17:43 +00005366 if (Target.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005367 return StmtError();
Eli Friedmand29975f2012-01-31 22:47:07 +00005368 Target = SemaRef.MaybeCreateExprWithCleanups(Target.take());
Mike Stump1eb44332009-09-09 15:08:12 +00005369
Douglas Gregor43959a92009-08-20 07:17:43 +00005370 if (!getDerived().AlwaysRebuild() &&
5371 Target.get() == S->getTarget())
John McCall3fa5cae2010-10-26 07:05:15 +00005372 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005373
5374 return getDerived().RebuildIndirectGotoStmt(S->getGotoLoc(), S->getStarLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005375 Target.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005376}
5377
5378template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005379StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005380TreeTransform<Derived>::TransformContinueStmt(ContinueStmt *S) {
John McCall3fa5cae2010-10-26 07:05:15 +00005381 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005382}
Mike Stump1eb44332009-09-09 15:08:12 +00005383
Douglas Gregor43959a92009-08-20 07:17:43 +00005384template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005385StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005386TreeTransform<Derived>::TransformBreakStmt(BreakStmt *S) {
John McCall3fa5cae2010-10-26 07:05:15 +00005387 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005388}
Mike Stump1eb44332009-09-09 15:08:12 +00005389
Douglas Gregor43959a92009-08-20 07:17:43 +00005390template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005391StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005392TreeTransform<Derived>::TransformReturnStmt(ReturnStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005393 ExprResult Result = getDerived().TransformExpr(S->getRetValue());
Douglas Gregor43959a92009-08-20 07:17:43 +00005394 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005395 return StmtError();
Douglas Gregor43959a92009-08-20 07:17:43 +00005396
Mike Stump1eb44332009-09-09 15:08:12 +00005397 // FIXME: We always rebuild the return statement because there is no way
Douglas Gregor43959a92009-08-20 07:17:43 +00005398 // to tell whether the return type of the function has changed.
John McCall9ae2f072010-08-23 23:25:46 +00005399 return getDerived().RebuildReturnStmt(S->getReturnLoc(), Result.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005400}
Mike Stump1eb44332009-09-09 15:08:12 +00005401
Douglas Gregor43959a92009-08-20 07:17:43 +00005402template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005403StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005404TreeTransform<Derived>::TransformDeclStmt(DeclStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005405 bool DeclChanged = false;
Chris Lattner686775d2011-07-20 06:58:45 +00005406 SmallVector<Decl *, 4> Decls;
Douglas Gregor43959a92009-08-20 07:17:43 +00005407 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
5408 D != DEnd; ++D) {
Douglas Gregoraac571c2010-03-01 17:25:41 +00005409 Decl *Transformed = getDerived().TransformDefinition((*D)->getLocation(),
5410 *D);
Douglas Gregor43959a92009-08-20 07:17:43 +00005411 if (!Transformed)
John McCallf312b1e2010-08-26 23:41:50 +00005412 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005413
Douglas Gregor43959a92009-08-20 07:17:43 +00005414 if (Transformed != *D)
5415 DeclChanged = true;
Mike Stump1eb44332009-09-09 15:08:12 +00005416
Douglas Gregor43959a92009-08-20 07:17:43 +00005417 Decls.push_back(Transformed);
5418 }
Mike Stump1eb44332009-09-09 15:08:12 +00005419
Douglas Gregor43959a92009-08-20 07:17:43 +00005420 if (!getDerived().AlwaysRebuild() && !DeclChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00005421 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005422
5423 return getDerived().RebuildDeclStmt(Decls.data(), Decls.size(),
Douglas Gregor43959a92009-08-20 07:17:43 +00005424 S->getStartLoc(), S->getEndLoc());
5425}
Mike Stump1eb44332009-09-09 15:08:12 +00005426
Douglas Gregor43959a92009-08-20 07:17:43 +00005427template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005428StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005429TreeTransform<Derived>::TransformAsmStmt(AsmStmt *S) {
Sean Huntc3021132010-05-05 15:23:54 +00005430
John McCallca0408f2010-08-23 06:44:23 +00005431 ASTOwningVector<Expr*> Constraints(getSema());
5432 ASTOwningVector<Expr*> Exprs(getSema());
Chris Lattner686775d2011-07-20 06:58:45 +00005433 SmallVector<IdentifierInfo *, 4> Names;
Anders Carlssona5a79f72010-01-30 20:05:21 +00005434
John McCall60d7b3a2010-08-24 06:29:42 +00005435 ExprResult AsmString;
John McCallca0408f2010-08-23 06:44:23 +00005436 ASTOwningVector<Expr*> Clobbers(getSema());
Anders Carlsson703e3942010-01-24 05:50:09 +00005437
5438 bool ExprsChanged = false;
Sean Huntc3021132010-05-05 15:23:54 +00005439
Anders Carlsson703e3942010-01-24 05:50:09 +00005440 // Go through the outputs.
5441 for (unsigned I = 0, E = S->getNumOutputs(); I != E; ++I) {
Anders Carlssonff93dbd2010-01-30 22:25:16 +00005442 Names.push_back(S->getOutputIdentifier(I));
Sean Huntc3021132010-05-05 15:23:54 +00005443
Anders Carlsson703e3942010-01-24 05:50:09 +00005444 // No need to transform the constraint literal.
John McCall3fa5cae2010-10-26 07:05:15 +00005445 Constraints.push_back(S->getOutputConstraintLiteral(I));
Sean Huntc3021132010-05-05 15:23:54 +00005446
Anders Carlsson703e3942010-01-24 05:50:09 +00005447 // Transform the output expr.
5448 Expr *OutputExpr = S->getOutputExpr(I);
John McCall60d7b3a2010-08-24 06:29:42 +00005449 ExprResult Result = getDerived().TransformExpr(OutputExpr);
Anders Carlsson703e3942010-01-24 05:50:09 +00005450 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005451 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005452
Anders Carlsson703e3942010-01-24 05:50:09 +00005453 ExprsChanged |= Result.get() != OutputExpr;
Sean Huntc3021132010-05-05 15:23:54 +00005454
John McCall9ae2f072010-08-23 23:25:46 +00005455 Exprs.push_back(Result.get());
Anders Carlsson703e3942010-01-24 05:50:09 +00005456 }
Sean Huntc3021132010-05-05 15:23:54 +00005457
Anders Carlsson703e3942010-01-24 05:50:09 +00005458 // Go through the inputs.
5459 for (unsigned I = 0, E = S->getNumInputs(); I != E; ++I) {
Anders Carlssonff93dbd2010-01-30 22:25:16 +00005460 Names.push_back(S->getInputIdentifier(I));
Sean Huntc3021132010-05-05 15:23:54 +00005461
Anders Carlsson703e3942010-01-24 05:50:09 +00005462 // No need to transform the constraint literal.
John McCall3fa5cae2010-10-26 07:05:15 +00005463 Constraints.push_back(S->getInputConstraintLiteral(I));
Sean Huntc3021132010-05-05 15:23:54 +00005464
Anders Carlsson703e3942010-01-24 05:50:09 +00005465 // Transform the input expr.
5466 Expr *InputExpr = S->getInputExpr(I);
John McCall60d7b3a2010-08-24 06:29:42 +00005467 ExprResult Result = getDerived().TransformExpr(InputExpr);
Anders Carlsson703e3942010-01-24 05:50:09 +00005468 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005469 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005470
Anders Carlsson703e3942010-01-24 05:50:09 +00005471 ExprsChanged |= Result.get() != InputExpr;
Sean Huntc3021132010-05-05 15:23:54 +00005472
John McCall9ae2f072010-08-23 23:25:46 +00005473 Exprs.push_back(Result.get());
Anders Carlsson703e3942010-01-24 05:50:09 +00005474 }
Sean Huntc3021132010-05-05 15:23:54 +00005475
Anders Carlsson703e3942010-01-24 05:50:09 +00005476 if (!getDerived().AlwaysRebuild() && !ExprsChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00005477 return SemaRef.Owned(S);
Anders Carlsson703e3942010-01-24 05:50:09 +00005478
5479 // Go through the clobbers.
5480 for (unsigned I = 0, E = S->getNumClobbers(); I != E; ++I)
John McCall3fa5cae2010-10-26 07:05:15 +00005481 Clobbers.push_back(S->getClobber(I));
Anders Carlsson703e3942010-01-24 05:50:09 +00005482
5483 // No need to transform the asm string literal.
5484 AsmString = SemaRef.Owned(S->getAsmString());
5485
5486 return getDerived().RebuildAsmStmt(S->getAsmLoc(),
5487 S->isSimple(),
5488 S->isVolatile(),
5489 S->getNumOutputs(),
5490 S->getNumInputs(),
Anders Carlssona5a79f72010-01-30 20:05:21 +00005491 Names.data(),
Anders Carlsson703e3942010-01-24 05:50:09 +00005492 move_arg(Constraints),
5493 move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00005494 AsmString.get(),
Anders Carlsson703e3942010-01-24 05:50:09 +00005495 move_arg(Clobbers),
5496 S->getRParenLoc(),
5497 S->isMSAsm());
Douglas Gregor43959a92009-08-20 07:17:43 +00005498}
5499
5500
5501template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005502StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005503TreeTransform<Derived>::TransformObjCAtTryStmt(ObjCAtTryStmt *S) {
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005504 // Transform the body of the @try.
John McCall60d7b3a2010-08-24 06:29:42 +00005505 StmtResult TryBody = getDerived().TransformStmt(S->getTryBody());
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005506 if (TryBody.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005507 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005508
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005509 // Transform the @catch statements (if present).
5510 bool AnyCatchChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00005511 ASTOwningVector<Stmt*> CatchStmts(SemaRef);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005512 for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
John McCall60d7b3a2010-08-24 06:29:42 +00005513 StmtResult Catch = getDerived().TransformStmt(S->getCatchStmt(I));
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005514 if (Catch.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005515 return StmtError();
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005516 if (Catch.get() != S->getCatchStmt(I))
5517 AnyCatchChanged = true;
5518 CatchStmts.push_back(Catch.release());
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005519 }
Sean Huntc3021132010-05-05 15:23:54 +00005520
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005521 // Transform the @finally statement (if present).
John McCall60d7b3a2010-08-24 06:29:42 +00005522 StmtResult Finally;
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005523 if (S->getFinallyStmt()) {
5524 Finally = getDerived().TransformStmt(S->getFinallyStmt());
5525 if (Finally.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005526 return StmtError();
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005527 }
5528
5529 // If nothing changed, just retain this statement.
5530 if (!getDerived().AlwaysRebuild() &&
5531 TryBody.get() == S->getTryBody() &&
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005532 !AnyCatchChanged &&
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005533 Finally.get() == S->getFinallyStmt())
John McCall3fa5cae2010-10-26 07:05:15 +00005534 return SemaRef.Owned(S);
Sean Huntc3021132010-05-05 15:23:54 +00005535
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005536 // Build a new statement.
John McCall9ae2f072010-08-23 23:25:46 +00005537 return getDerived().RebuildObjCAtTryStmt(S->getAtTryLoc(), TryBody.get(),
5538 move_arg(CatchStmts), Finally.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005539}
Mike Stump1eb44332009-09-09 15:08:12 +00005540
Douglas Gregor43959a92009-08-20 07:17:43 +00005541template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005542StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005543TreeTransform<Derived>::TransformObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Douglas Gregorbe270a02010-04-26 17:57:08 +00005544 // Transform the @catch parameter, if there is one.
5545 VarDecl *Var = 0;
5546 if (VarDecl *FromVar = S->getCatchParamDecl()) {
5547 TypeSourceInfo *TSInfo = 0;
5548 if (FromVar->getTypeSourceInfo()) {
5549 TSInfo = getDerived().TransformType(FromVar->getTypeSourceInfo());
5550 if (!TSInfo)
John McCallf312b1e2010-08-26 23:41:50 +00005551 return StmtError();
Douglas Gregorbe270a02010-04-26 17:57:08 +00005552 }
Sean Huntc3021132010-05-05 15:23:54 +00005553
Douglas Gregorbe270a02010-04-26 17:57:08 +00005554 QualType T;
5555 if (TSInfo)
5556 T = TSInfo->getType();
5557 else {
5558 T = getDerived().TransformType(FromVar->getType());
5559 if (T.isNull())
John McCallf312b1e2010-08-26 23:41:50 +00005560 return StmtError();
Douglas Gregorbe270a02010-04-26 17:57:08 +00005561 }
Sean Huntc3021132010-05-05 15:23:54 +00005562
Douglas Gregorbe270a02010-04-26 17:57:08 +00005563 Var = getDerived().RebuildObjCExceptionDecl(FromVar, TSInfo, T);
5564 if (!Var)
John McCallf312b1e2010-08-26 23:41:50 +00005565 return StmtError();
Douglas Gregorbe270a02010-04-26 17:57:08 +00005566 }
Sean Huntc3021132010-05-05 15:23:54 +00005567
John McCall60d7b3a2010-08-24 06:29:42 +00005568 StmtResult Body = getDerived().TransformStmt(S->getCatchBody());
Douglas Gregorbe270a02010-04-26 17:57:08 +00005569 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005570 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005571
5572 return getDerived().RebuildObjCAtCatchStmt(S->getAtCatchLoc(),
Douglas Gregorbe270a02010-04-26 17:57:08 +00005573 S->getRParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005574 Var, Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005575}
Mike Stump1eb44332009-09-09 15:08:12 +00005576
Douglas Gregor43959a92009-08-20 07:17:43 +00005577template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005578StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005579TreeTransform<Derived>::TransformObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005580 // Transform the body.
John McCall60d7b3a2010-08-24 06:29:42 +00005581 StmtResult Body = getDerived().TransformStmt(S->getFinallyBody());
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005582 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005583 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005584
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005585 // If nothing changed, just retain this statement.
5586 if (!getDerived().AlwaysRebuild() &&
5587 Body.get() == S->getFinallyBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005588 return SemaRef.Owned(S);
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005589
5590 // Build a new statement.
5591 return getDerived().RebuildObjCAtFinallyStmt(S->getAtFinallyLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005592 Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005593}
Mike Stump1eb44332009-09-09 15:08:12 +00005594
Douglas Gregor43959a92009-08-20 07:17:43 +00005595template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005596StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005597TreeTransform<Derived>::TransformObjCAtThrowStmt(ObjCAtThrowStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005598 ExprResult Operand;
Douglas Gregord1377b22010-04-22 21:44:01 +00005599 if (S->getThrowExpr()) {
5600 Operand = getDerived().TransformExpr(S->getThrowExpr());
5601 if (Operand.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005602 return StmtError();
Douglas Gregord1377b22010-04-22 21:44:01 +00005603 }
Sean Huntc3021132010-05-05 15:23:54 +00005604
Douglas Gregord1377b22010-04-22 21:44:01 +00005605 if (!getDerived().AlwaysRebuild() &&
5606 Operand.get() == S->getThrowExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00005607 return getSema().Owned(S);
Sean Huntc3021132010-05-05 15:23:54 +00005608
John McCall9ae2f072010-08-23 23:25:46 +00005609 return getDerived().RebuildObjCAtThrowStmt(S->getThrowLoc(), Operand.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005610}
Mike Stump1eb44332009-09-09 15:08:12 +00005611
Douglas Gregor43959a92009-08-20 07:17:43 +00005612template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005613StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005614TreeTransform<Derived>::TransformObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +00005615 ObjCAtSynchronizedStmt *S) {
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005616 // Transform the object we are locking.
John McCall60d7b3a2010-08-24 06:29:42 +00005617 ExprResult Object = getDerived().TransformExpr(S->getSynchExpr());
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005618 if (Object.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005619 return StmtError();
John McCall07524032011-07-27 21:50:02 +00005620 Object =
5621 getDerived().RebuildObjCAtSynchronizedOperand(S->getAtSynchronizedLoc(),
5622 Object.get());
5623 if (Object.isInvalid())
5624 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005625
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005626 // Transform the body.
John McCall60d7b3a2010-08-24 06:29:42 +00005627 StmtResult Body = getDerived().TransformStmt(S->getSynchBody());
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005628 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005629 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005630
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005631 // If nothing change, just retain the current statement.
5632 if (!getDerived().AlwaysRebuild() &&
5633 Object.get() == S->getSynchExpr() &&
5634 Body.get() == S->getSynchBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005635 return SemaRef.Owned(S);
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005636
5637 // Build a new statement.
5638 return getDerived().RebuildObjCAtSynchronizedStmt(S->getAtSynchronizedLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005639 Object.get(), Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005640}
5641
5642template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005643StmtResult
John McCallf85e1932011-06-15 23:02:42 +00005644TreeTransform<Derived>::TransformObjCAutoreleasePoolStmt(
5645 ObjCAutoreleasePoolStmt *S) {
5646 // Transform the body.
5647 StmtResult Body = getDerived().TransformStmt(S->getSubStmt());
5648 if (Body.isInvalid())
5649 return StmtError();
5650
5651 // If nothing changed, just retain this statement.
5652 if (!getDerived().AlwaysRebuild() &&
5653 Body.get() == S->getSubStmt())
5654 return SemaRef.Owned(S);
5655
5656 // Build a new statement.
5657 return getDerived().RebuildObjCAutoreleasePoolStmt(
5658 S->getAtLoc(), Body.get());
5659}
5660
5661template<typename Derived>
5662StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005663TreeTransform<Derived>::TransformObjCForCollectionStmt(
Mike Stump1eb44332009-09-09 15:08:12 +00005664 ObjCForCollectionStmt *S) {
Douglas Gregorc3203e72010-04-22 23:10:45 +00005665 // Transform the element statement.
John McCall60d7b3a2010-08-24 06:29:42 +00005666 StmtResult Element = getDerived().TransformStmt(S->getElement());
Douglas Gregorc3203e72010-04-22 23:10:45 +00005667 if (Element.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005668 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005669
Douglas Gregorc3203e72010-04-22 23:10:45 +00005670 // Transform the collection expression.
John McCall60d7b3a2010-08-24 06:29:42 +00005671 ExprResult Collection = getDerived().TransformExpr(S->getCollection());
Douglas Gregorc3203e72010-04-22 23:10:45 +00005672 if (Collection.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005673 return StmtError();
John McCall990567c2011-07-27 01:07:15 +00005674 Collection = getDerived().RebuildObjCForCollectionOperand(S->getForLoc(),
5675 Collection.take());
5676 if (Collection.isInvalid())
5677 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005678
Douglas Gregorc3203e72010-04-22 23:10:45 +00005679 // Transform the body.
John McCall60d7b3a2010-08-24 06:29:42 +00005680 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregorc3203e72010-04-22 23:10:45 +00005681 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005682 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005683
Douglas Gregorc3203e72010-04-22 23:10:45 +00005684 // If nothing changed, just retain this statement.
5685 if (!getDerived().AlwaysRebuild() &&
5686 Element.get() == S->getElement() &&
5687 Collection.get() == S->getCollection() &&
5688 Body.get() == S->getBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005689 return SemaRef.Owned(S);
Sean Huntc3021132010-05-05 15:23:54 +00005690
Douglas Gregorc3203e72010-04-22 23:10:45 +00005691 // Build a new statement.
5692 return getDerived().RebuildObjCForCollectionStmt(S->getForLoc(),
5693 /*FIXME:*/S->getForLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005694 Element.get(),
5695 Collection.get(),
Douglas Gregorc3203e72010-04-22 23:10:45 +00005696 S->getRParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005697 Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005698}
5699
5700
5701template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005702StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005703TreeTransform<Derived>::TransformCXXCatchStmt(CXXCatchStmt *S) {
5704 // Transform the exception declaration, if any.
5705 VarDecl *Var = 0;
5706 if (S->getExceptionDecl()) {
5707 VarDecl *ExceptionDecl = S->getExceptionDecl();
Douglas Gregor83cb9422010-09-09 17:09:21 +00005708 TypeSourceInfo *T = getDerived().TransformType(
5709 ExceptionDecl->getTypeSourceInfo());
5710 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00005711 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005712
Douglas Gregor83cb9422010-09-09 17:09:21 +00005713 Var = getDerived().RebuildExceptionDecl(ExceptionDecl, T,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005714 ExceptionDecl->getInnerLocStart(),
5715 ExceptionDecl->getLocation(),
5716 ExceptionDecl->getIdentifier());
Douglas Gregorff331c12010-07-25 18:17:45 +00005717 if (!Var || Var->isInvalidDecl())
John McCallf312b1e2010-08-26 23:41:50 +00005718 return StmtError();
Douglas Gregor43959a92009-08-20 07:17:43 +00005719 }
Mike Stump1eb44332009-09-09 15:08:12 +00005720
Douglas Gregor43959a92009-08-20 07:17:43 +00005721 // Transform the actual exception handler.
John McCall60d7b3a2010-08-24 06:29:42 +00005722 StmtResult Handler = getDerived().TransformStmt(S->getHandlerBlock());
Douglas Gregorff331c12010-07-25 18:17:45 +00005723 if (Handler.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005724 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005725
Douglas Gregor43959a92009-08-20 07:17:43 +00005726 if (!getDerived().AlwaysRebuild() &&
5727 !Var &&
5728 Handler.get() == S->getHandlerBlock())
John McCall3fa5cae2010-10-26 07:05:15 +00005729 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005730
5731 return getDerived().RebuildCXXCatchStmt(S->getCatchLoc(),
5732 Var,
John McCall9ae2f072010-08-23 23:25:46 +00005733 Handler.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005734}
Mike Stump1eb44332009-09-09 15:08:12 +00005735
Douglas Gregor43959a92009-08-20 07:17:43 +00005736template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005737StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005738TreeTransform<Derived>::TransformCXXTryStmt(CXXTryStmt *S) {
5739 // Transform the try block itself.
John McCall60d7b3a2010-08-24 06:29:42 +00005740 StmtResult TryBlock
Douglas Gregor43959a92009-08-20 07:17:43 +00005741 = getDerived().TransformCompoundStmt(S->getTryBlock());
5742 if (TryBlock.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005743 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005744
Douglas Gregor43959a92009-08-20 07:17:43 +00005745 // Transform the handlers.
5746 bool HandlerChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00005747 ASTOwningVector<Stmt*> Handlers(SemaRef);
Douglas Gregor43959a92009-08-20 07:17:43 +00005748 for (unsigned I = 0, N = S->getNumHandlers(); I != N; ++I) {
John McCall60d7b3a2010-08-24 06:29:42 +00005749 StmtResult Handler
Douglas Gregor43959a92009-08-20 07:17:43 +00005750 = getDerived().TransformCXXCatchStmt(S->getHandler(I));
5751 if (Handler.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005752 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005753
Douglas Gregor43959a92009-08-20 07:17:43 +00005754 HandlerChanged = HandlerChanged || Handler.get() != S->getHandler(I);
5755 Handlers.push_back(Handler.takeAs<Stmt>());
5756 }
Mike Stump1eb44332009-09-09 15:08:12 +00005757
Douglas Gregor43959a92009-08-20 07:17:43 +00005758 if (!getDerived().AlwaysRebuild() &&
5759 TryBlock.get() == S->getTryBlock() &&
5760 !HandlerChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00005761 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005762
John McCall9ae2f072010-08-23 23:25:46 +00005763 return getDerived().RebuildCXXTryStmt(S->getTryLoc(), TryBlock.get(),
Mike Stump1eb44332009-09-09 15:08:12 +00005764 move_arg(Handlers));
Douglas Gregor43959a92009-08-20 07:17:43 +00005765}
Mike Stump1eb44332009-09-09 15:08:12 +00005766
Richard Smithad762fc2011-04-14 22:09:26 +00005767template<typename Derived>
5768StmtResult
5769TreeTransform<Derived>::TransformCXXForRangeStmt(CXXForRangeStmt *S) {
5770 StmtResult Range = getDerived().TransformStmt(S->getRangeStmt());
5771 if (Range.isInvalid())
5772 return StmtError();
5773
5774 StmtResult BeginEnd = getDerived().TransformStmt(S->getBeginEndStmt());
5775 if (BeginEnd.isInvalid())
5776 return StmtError();
5777
5778 ExprResult Cond = getDerived().TransformExpr(S->getCond());
5779 if (Cond.isInvalid())
5780 return StmtError();
Eli Friedmanc6c14e52012-01-31 22:45:40 +00005781 if (Cond.get())
5782 Cond = SemaRef.CheckBooleanCondition(Cond.take(), S->getColonLoc());
5783 if (Cond.isInvalid())
5784 return StmtError();
5785 if (Cond.get())
5786 Cond = SemaRef.MaybeCreateExprWithCleanups(Cond.take());
Richard Smithad762fc2011-04-14 22:09:26 +00005787
5788 ExprResult Inc = getDerived().TransformExpr(S->getInc());
5789 if (Inc.isInvalid())
5790 return StmtError();
Eli Friedmanc6c14e52012-01-31 22:45:40 +00005791 if (Inc.get())
5792 Inc = SemaRef.MaybeCreateExprWithCleanups(Inc.take());
Richard Smithad762fc2011-04-14 22:09:26 +00005793
5794 StmtResult LoopVar = getDerived().TransformStmt(S->getLoopVarStmt());
5795 if (LoopVar.isInvalid())
5796 return StmtError();
5797
5798 StmtResult NewStmt = S;
5799 if (getDerived().AlwaysRebuild() ||
5800 Range.get() != S->getRangeStmt() ||
5801 BeginEnd.get() != S->getBeginEndStmt() ||
5802 Cond.get() != S->getCond() ||
5803 Inc.get() != S->getInc() ||
5804 LoopVar.get() != S->getLoopVarStmt())
5805 NewStmt = getDerived().RebuildCXXForRangeStmt(S->getForLoc(),
5806 S->getColonLoc(), Range.get(),
5807 BeginEnd.get(), Cond.get(),
5808 Inc.get(), LoopVar.get(),
5809 S->getRParenLoc());
5810
5811 StmtResult Body = getDerived().TransformStmt(S->getBody());
5812 if (Body.isInvalid())
5813 return StmtError();
5814
5815 // Body has changed but we didn't rebuild the for-range statement. Rebuild
5816 // it now so we have a new statement to attach the body to.
5817 if (Body.get() != S->getBody() && NewStmt.get() == S)
5818 NewStmt = getDerived().RebuildCXXForRangeStmt(S->getForLoc(),
5819 S->getColonLoc(), Range.get(),
5820 BeginEnd.get(), Cond.get(),
5821 Inc.get(), LoopVar.get(),
5822 S->getRParenLoc());
5823
5824 if (NewStmt.get() == S)
5825 return SemaRef.Owned(S);
5826
5827 return FinishCXXForRangeStmt(NewStmt.get(), Body.get());
5828}
5829
John Wiegley28bbe4b2011-04-28 01:08:34 +00005830template<typename Derived>
5831StmtResult
Douglas Gregorba0513d2011-10-25 01:33:02 +00005832TreeTransform<Derived>::TransformMSDependentExistsStmt(
5833 MSDependentExistsStmt *S) {
5834 // Transform the nested-name-specifier, if any.
5835 NestedNameSpecifierLoc QualifierLoc;
5836 if (S->getQualifierLoc()) {
5837 QualifierLoc
5838 = getDerived().TransformNestedNameSpecifierLoc(S->getQualifierLoc());
5839 if (!QualifierLoc)
5840 return StmtError();
5841 }
5842
5843 // Transform the declaration name.
5844 DeclarationNameInfo NameInfo = S->getNameInfo();
5845 if (NameInfo.getName()) {
5846 NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo);
5847 if (!NameInfo.getName())
5848 return StmtError();
5849 }
5850
5851 // Check whether anything changed.
5852 if (!getDerived().AlwaysRebuild() &&
5853 QualifierLoc == S->getQualifierLoc() &&
5854 NameInfo.getName() == S->getNameInfo().getName())
5855 return S;
5856
5857 // Determine whether this name exists, if we can.
5858 CXXScopeSpec SS;
5859 SS.Adopt(QualifierLoc);
5860 bool Dependent = false;
5861 switch (getSema().CheckMicrosoftIfExistsSymbol(/*S=*/0, SS, NameInfo)) {
5862 case Sema::IER_Exists:
5863 if (S->isIfExists())
5864 break;
5865
5866 return new (getSema().Context) NullStmt(S->getKeywordLoc());
5867
5868 case Sema::IER_DoesNotExist:
5869 if (S->isIfNotExists())
5870 break;
5871
5872 return new (getSema().Context) NullStmt(S->getKeywordLoc());
5873
5874 case Sema::IER_Dependent:
5875 Dependent = true;
5876 break;
Douglas Gregor65019ac2011-10-25 03:44:56 +00005877
5878 case Sema::IER_Error:
5879 return StmtError();
Douglas Gregorba0513d2011-10-25 01:33:02 +00005880 }
5881
5882 // We need to continue with the instantiation, so do so now.
5883 StmtResult SubStmt = getDerived().TransformCompoundStmt(S->getSubStmt());
5884 if (SubStmt.isInvalid())
5885 return StmtError();
5886
5887 // If we have resolved the name, just transform to the substatement.
5888 if (!Dependent)
5889 return SubStmt;
5890
5891 // The name is still dependent, so build a dependent expression again.
5892 return getDerived().RebuildMSDependentExistsStmt(S->getKeywordLoc(),
5893 S->isIfExists(),
5894 QualifierLoc,
5895 NameInfo,
5896 SubStmt.get());
5897}
5898
5899template<typename Derived>
5900StmtResult
John Wiegley28bbe4b2011-04-28 01:08:34 +00005901TreeTransform<Derived>::TransformSEHTryStmt(SEHTryStmt *S) {
5902 StmtResult TryBlock; // = getDerived().TransformCompoundStmt(S->getTryBlock());
5903 if(TryBlock.isInvalid()) return StmtError();
5904
5905 StmtResult Handler = getDerived().TransformSEHHandler(S->getHandler());
5906 if(!getDerived().AlwaysRebuild() &&
5907 TryBlock.get() == S->getTryBlock() &&
5908 Handler.get() == S->getHandler())
5909 return SemaRef.Owned(S);
5910
5911 return getDerived().RebuildSEHTryStmt(S->getIsCXXTry(),
5912 S->getTryLoc(),
5913 TryBlock.take(),
5914 Handler.take());
5915}
5916
5917template<typename Derived>
5918StmtResult
5919TreeTransform<Derived>::TransformSEHFinallyStmt(SEHFinallyStmt *S) {
5920 StmtResult Block; // = getDerived().TransformCompoundStatement(S->getBlock());
5921 if(Block.isInvalid()) return StmtError();
5922
5923 return getDerived().RebuildSEHFinallyStmt(S->getFinallyLoc(),
5924 Block.take());
5925}
5926
5927template<typename Derived>
5928StmtResult
5929TreeTransform<Derived>::TransformSEHExceptStmt(SEHExceptStmt *S) {
5930 ExprResult FilterExpr = getDerived().TransformExpr(S->getFilterExpr());
5931 if(FilterExpr.isInvalid()) return StmtError();
5932
5933 StmtResult Block; // = getDerived().TransformCompoundStatement(S->getBlock());
5934 if(Block.isInvalid()) return StmtError();
5935
5936 return getDerived().RebuildSEHExceptStmt(S->getExceptLoc(),
5937 FilterExpr.take(),
5938 Block.take());
5939}
5940
5941template<typename Derived>
5942StmtResult
5943TreeTransform<Derived>::TransformSEHHandler(Stmt *Handler) {
5944 if(isa<SEHFinallyStmt>(Handler))
5945 return getDerived().TransformSEHFinallyStmt(cast<SEHFinallyStmt>(Handler));
5946 else
5947 return getDerived().TransformSEHExceptStmt(cast<SEHExceptStmt>(Handler));
5948}
5949
Douglas Gregor43959a92009-08-20 07:17:43 +00005950//===----------------------------------------------------------------------===//
Douglas Gregorb98b1992009-08-11 05:31:07 +00005951// Expression transformation
5952//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00005953template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005954ExprResult
John McCall454feb92009-12-08 09:21:05 +00005955TreeTransform<Derived>::TransformPredefinedExpr(PredefinedExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00005956 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00005957}
Mike Stump1eb44332009-09-09 15:08:12 +00005958
5959template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005960ExprResult
John McCall454feb92009-12-08 09:21:05 +00005961TreeTransform<Derived>::TransformDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00005962 NestedNameSpecifierLoc QualifierLoc;
5963 if (E->getQualifierLoc()) {
5964 QualifierLoc
5965 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
5966 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00005967 return ExprError();
Douglas Gregora2813ce2009-10-23 18:54:35 +00005968 }
John McCalldbd872f2009-12-08 09:08:17 +00005969
5970 ValueDecl *ND
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00005971 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getLocation(),
5972 E->getDecl()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00005973 if (!ND)
John McCallf312b1e2010-08-26 23:41:50 +00005974 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00005975
John McCallec8045d2010-08-17 21:27:17 +00005976 DeclarationNameInfo NameInfo = E->getNameInfo();
5977 if (NameInfo.getName()) {
5978 NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo);
5979 if (!NameInfo.getName())
John McCallf312b1e2010-08-26 23:41:50 +00005980 return ExprError();
John McCallec8045d2010-08-17 21:27:17 +00005981 }
Abramo Bagnara25777432010-08-11 22:01:17 +00005982
5983 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor40d96a62011-02-28 21:54:11 +00005984 QualifierLoc == E->getQualifierLoc() &&
Douglas Gregora2813ce2009-10-23 18:54:35 +00005985 ND == E->getDecl() &&
Abramo Bagnara25777432010-08-11 22:01:17 +00005986 NameInfo.getName() == E->getDecl()->getDeclName() &&
John McCall096832c2010-08-19 23:49:38 +00005987 !E->hasExplicitTemplateArgs()) {
John McCalldbd872f2009-12-08 09:08:17 +00005988
5989 // Mark it referenced in the new context regardless.
5990 // FIXME: this is a bit instantiation-specific.
Eli Friedman5f2987c2012-02-02 03:46:19 +00005991 SemaRef.MarkDeclRefReferenced(E);
John McCalldbd872f2009-12-08 09:08:17 +00005992
John McCall3fa5cae2010-10-26 07:05:15 +00005993 return SemaRef.Owned(E);
Douglas Gregora2813ce2009-10-23 18:54:35 +00005994 }
John McCalldbd872f2009-12-08 09:08:17 +00005995
5996 TemplateArgumentListInfo TransArgs, *TemplateArgs = 0;
John McCall096832c2010-08-19 23:49:38 +00005997 if (E->hasExplicitTemplateArgs()) {
John McCalldbd872f2009-12-08 09:08:17 +00005998 TemplateArgs = &TransArgs;
5999 TransArgs.setLAngleLoc(E->getLAngleLoc());
6000 TransArgs.setRAngleLoc(E->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00006001 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
6002 E->getNumTemplateArgs(),
6003 TransArgs))
6004 return ExprError();
John McCalldbd872f2009-12-08 09:08:17 +00006005 }
6006
Douglas Gregor40d96a62011-02-28 21:54:11 +00006007 return getDerived().RebuildDeclRefExpr(QualifierLoc, ND, NameInfo,
6008 TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006009}
Mike Stump1eb44332009-09-09 15:08:12 +00006010
Douglas Gregorb98b1992009-08-11 05:31:07 +00006011template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006012ExprResult
John McCall454feb92009-12-08 09:21:05 +00006013TreeTransform<Derived>::TransformIntegerLiteral(IntegerLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006014 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006015}
Mike Stump1eb44332009-09-09 15:08:12 +00006016
Douglas Gregorb98b1992009-08-11 05:31:07 +00006017template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006018ExprResult
John McCall454feb92009-12-08 09:21:05 +00006019TreeTransform<Derived>::TransformFloatingLiteral(FloatingLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006020 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006021}
Mike Stump1eb44332009-09-09 15:08:12 +00006022
Douglas Gregorb98b1992009-08-11 05:31:07 +00006023template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006024ExprResult
John McCall454feb92009-12-08 09:21:05 +00006025TreeTransform<Derived>::TransformImaginaryLiteral(ImaginaryLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006026 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006027}
Mike Stump1eb44332009-09-09 15:08:12 +00006028
Douglas Gregorb98b1992009-08-11 05:31:07 +00006029template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006030ExprResult
John McCall454feb92009-12-08 09:21:05 +00006031TreeTransform<Derived>::TransformStringLiteral(StringLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006032 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006033}
Mike Stump1eb44332009-09-09 15:08:12 +00006034
Douglas Gregorb98b1992009-08-11 05:31:07 +00006035template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006036ExprResult
John McCall454feb92009-12-08 09:21:05 +00006037TreeTransform<Derived>::TransformCharacterLiteral(CharacterLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006038 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006039}
6040
6041template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006042ExprResult
Peter Collingbournef111d932011-04-15 00:35:48 +00006043TreeTransform<Derived>::TransformGenericSelectionExpr(GenericSelectionExpr *E) {
6044 ExprResult ControllingExpr =
6045 getDerived().TransformExpr(E->getControllingExpr());
6046 if (ControllingExpr.isInvalid())
6047 return ExprError();
6048
Chris Lattner686775d2011-07-20 06:58:45 +00006049 SmallVector<Expr *, 4> AssocExprs;
6050 SmallVector<TypeSourceInfo *, 4> AssocTypes;
Peter Collingbournef111d932011-04-15 00:35:48 +00006051 for (unsigned i = 0; i != E->getNumAssocs(); ++i) {
6052 TypeSourceInfo *TS = E->getAssocTypeSourceInfo(i);
6053 if (TS) {
6054 TypeSourceInfo *AssocType = getDerived().TransformType(TS);
6055 if (!AssocType)
6056 return ExprError();
6057 AssocTypes.push_back(AssocType);
6058 } else {
6059 AssocTypes.push_back(0);
6060 }
6061
6062 ExprResult AssocExpr = getDerived().TransformExpr(E->getAssocExpr(i));
6063 if (AssocExpr.isInvalid())
6064 return ExprError();
6065 AssocExprs.push_back(AssocExpr.release());
6066 }
6067
6068 return getDerived().RebuildGenericSelectionExpr(E->getGenericLoc(),
6069 E->getDefaultLoc(),
6070 E->getRParenLoc(),
6071 ControllingExpr.release(),
6072 AssocTypes.data(),
6073 AssocExprs.data(),
6074 E->getNumAssocs());
6075}
6076
6077template<typename Derived>
6078ExprResult
John McCall454feb92009-12-08 09:21:05 +00006079TreeTransform<Derived>::TransformParenExpr(ParenExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006080 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006081 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006082 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006083
Douglas Gregorb98b1992009-08-11 05:31:07 +00006084 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006085 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006086
John McCall9ae2f072010-08-23 23:25:46 +00006087 return getDerived().RebuildParenExpr(SubExpr.get(), E->getLParen(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006088 E->getRParen());
6089}
6090
Mike Stump1eb44332009-09-09 15:08:12 +00006091template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006092ExprResult
John McCall454feb92009-12-08 09:21:05 +00006093TreeTransform<Derived>::TransformUnaryOperator(UnaryOperator *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006094 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006095 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006096 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006097
Douglas Gregorb98b1992009-08-11 05:31:07 +00006098 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006099 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006100
Douglas Gregorb98b1992009-08-11 05:31:07 +00006101 return getDerived().RebuildUnaryOperator(E->getOperatorLoc(),
6102 E->getOpcode(),
John McCall9ae2f072010-08-23 23:25:46 +00006103 SubExpr.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006104}
Mike Stump1eb44332009-09-09 15:08:12 +00006105
Douglas Gregorb98b1992009-08-11 05:31:07 +00006106template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006107ExprResult
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006108TreeTransform<Derived>::TransformOffsetOfExpr(OffsetOfExpr *E) {
6109 // Transform the type.
6110 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeSourceInfo());
6111 if (!Type)
John McCallf312b1e2010-08-26 23:41:50 +00006112 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00006113
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006114 // Transform all of the components into components similar to what the
6115 // parser uses.
Sean Huntc3021132010-05-05 15:23:54 +00006116 // FIXME: It would be slightly more efficient in the non-dependent case to
6117 // just map FieldDecls, rather than requiring the rebuilder to look for
6118 // the fields again. However, __builtin_offsetof is rare enough in
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006119 // template code that we don't care.
6120 bool ExprChanged = false;
John McCallf312b1e2010-08-26 23:41:50 +00006121 typedef Sema::OffsetOfComponent Component;
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006122 typedef OffsetOfExpr::OffsetOfNode Node;
Chris Lattner686775d2011-07-20 06:58:45 +00006123 SmallVector<Component, 4> Components;
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006124 for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
6125 const Node &ON = E->getComponent(I);
6126 Component Comp;
Douglas Gregor72be24f2010-04-30 20:35:01 +00006127 Comp.isBrackets = true;
Abramo Bagnara06dec892011-03-12 09:45:03 +00006128 Comp.LocStart = ON.getSourceRange().getBegin();
6129 Comp.LocEnd = ON.getSourceRange().getEnd();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006130 switch (ON.getKind()) {
6131 case Node::Array: {
6132 Expr *FromIndex = E->getIndexExpr(ON.getArrayExprIndex());
John McCall60d7b3a2010-08-24 06:29:42 +00006133 ExprResult Index = getDerived().TransformExpr(FromIndex);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006134 if (Index.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006135 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00006136
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006137 ExprChanged = ExprChanged || Index.get() != FromIndex;
6138 Comp.isBrackets = true;
John McCall9ae2f072010-08-23 23:25:46 +00006139 Comp.U.E = Index.get();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006140 break;
6141 }
Sean Huntc3021132010-05-05 15:23:54 +00006142
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006143 case Node::Field:
6144 case Node::Identifier:
6145 Comp.isBrackets = false;
6146 Comp.U.IdentInfo = ON.getFieldName();
Douglas Gregor29d2fd52010-04-28 22:43:14 +00006147 if (!Comp.U.IdentInfo)
6148 continue;
Sean Huntc3021132010-05-05 15:23:54 +00006149
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006150 break;
Sean Huntc3021132010-05-05 15:23:54 +00006151
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00006152 case Node::Base:
6153 // Will be recomputed during the rebuild.
6154 continue;
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006155 }
Sean Huntc3021132010-05-05 15:23:54 +00006156
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006157 Components.push_back(Comp);
6158 }
Sean Huntc3021132010-05-05 15:23:54 +00006159
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006160 // If nothing changed, retain the existing expression.
6161 if (!getDerived().AlwaysRebuild() &&
6162 Type == E->getTypeSourceInfo() &&
6163 !ExprChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00006164 return SemaRef.Owned(E);
Sean Huntc3021132010-05-05 15:23:54 +00006165
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006166 // Build a new offsetof expression.
6167 return getDerived().RebuildOffsetOfExpr(E->getOperatorLoc(), Type,
6168 Components.data(), Components.size(),
6169 E->getRParenLoc());
6170}
6171
6172template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006173ExprResult
John McCall7cd7d1a2010-11-15 23:31:06 +00006174TreeTransform<Derived>::TransformOpaqueValueExpr(OpaqueValueExpr *E) {
6175 assert(getDerived().AlreadyTransformed(E->getType()) &&
6176 "opaque value expression requires transformation");
6177 return SemaRef.Owned(E);
6178}
6179
6180template<typename Derived>
6181ExprResult
John McCall4b9c2d22011-11-06 09:01:30 +00006182TreeTransform<Derived>::TransformPseudoObjectExpr(PseudoObjectExpr *E) {
John McCall01e19be2011-11-30 04:42:31 +00006183 // Rebuild the syntactic form. The original syntactic form has
6184 // opaque-value expressions in it, so strip those away and rebuild
6185 // the result. This is a really awful way of doing this, but the
6186 // better solution (rebuilding the semantic expressions and
6187 // rebinding OVEs as necessary) doesn't work; we'd need
6188 // TreeTransform to not strip away implicit conversions.
6189 Expr *newSyntacticForm = SemaRef.recreateSyntacticForm(E);
6190 ExprResult result = getDerived().TransformExpr(newSyntacticForm);
John McCall4b9c2d22011-11-06 09:01:30 +00006191 if (result.isInvalid()) return ExprError();
6192
6193 // If that gives us a pseudo-object result back, the pseudo-object
6194 // expression must have been an lvalue-to-rvalue conversion which we
6195 // should reapply.
6196 if (result.get()->hasPlaceholderType(BuiltinType::PseudoObject))
6197 result = SemaRef.checkPseudoObjectRValue(result.take());
6198
6199 return result;
6200}
6201
6202template<typename Derived>
6203ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00006204TreeTransform<Derived>::TransformUnaryExprOrTypeTraitExpr(
6205 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006206 if (E->isArgumentType()) {
John McCalla93c9342009-12-07 02:54:59 +00006207 TypeSourceInfo *OldT = E->getArgumentTypeInfo();
Douglas Gregor5557b252009-10-28 00:29:27 +00006208
John McCalla93c9342009-12-07 02:54:59 +00006209 TypeSourceInfo *NewT = getDerived().TransformType(OldT);
John McCall5ab75172009-11-04 07:28:41 +00006210 if (!NewT)
John McCallf312b1e2010-08-26 23:41:50 +00006211 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006212
John McCall5ab75172009-11-04 07:28:41 +00006213 if (!getDerived().AlwaysRebuild() && OldT == NewT)
John McCall3fa5cae2010-10-26 07:05:15 +00006214 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006215
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00006216 return getDerived().RebuildUnaryExprOrTypeTrait(NewT, E->getOperatorLoc(),
6217 E->getKind(),
6218 E->getSourceRange());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006219 }
Mike Stump1eb44332009-09-09 15:08:12 +00006220
John McCall60d7b3a2010-08-24 06:29:42 +00006221 ExprResult SubExpr;
Mike Stump1eb44332009-09-09 15:08:12 +00006222 {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006223 // C++0x [expr.sizeof]p1:
6224 // The operand is either an expression, which is an unevaluated operand
6225 // [...]
John McCallf312b1e2010-08-26 23:41:50 +00006226 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00006227
Douglas Gregorb98b1992009-08-11 05:31:07 +00006228 SubExpr = getDerived().TransformExpr(E->getArgumentExpr());
6229 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006230 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006231
Douglas Gregorb98b1992009-08-11 05:31:07 +00006232 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getArgumentExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006233 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006234 }
Mike Stump1eb44332009-09-09 15:08:12 +00006235
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00006236 return getDerived().RebuildUnaryExprOrTypeTrait(SubExpr.get(),
6237 E->getOperatorLoc(),
6238 E->getKind(),
6239 E->getSourceRange());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006240}
Mike Stump1eb44332009-09-09 15:08:12 +00006241
Douglas Gregorb98b1992009-08-11 05:31:07 +00006242template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006243ExprResult
John McCall454feb92009-12-08 09:21:05 +00006244TreeTransform<Derived>::TransformArraySubscriptExpr(ArraySubscriptExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006245 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006246 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006247 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006248
John McCall60d7b3a2010-08-24 06:29:42 +00006249 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006250 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006251 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006252
6253
Douglas Gregorb98b1992009-08-11 05:31:07 +00006254 if (!getDerived().AlwaysRebuild() &&
6255 LHS.get() == E->getLHS() &&
6256 RHS.get() == E->getRHS())
John McCall3fa5cae2010-10-26 07:05:15 +00006257 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006258
John McCall9ae2f072010-08-23 23:25:46 +00006259 return getDerived().RebuildArraySubscriptExpr(LHS.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006260 /*FIXME:*/E->getLHS()->getLocStart(),
John McCall9ae2f072010-08-23 23:25:46 +00006261 RHS.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006262 E->getRBracketLoc());
6263}
Mike Stump1eb44332009-09-09 15:08:12 +00006264
6265template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006266ExprResult
John McCall454feb92009-12-08 09:21:05 +00006267TreeTransform<Derived>::TransformCallExpr(CallExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006268 // Transform the callee.
John McCall60d7b3a2010-08-24 06:29:42 +00006269 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006270 if (Callee.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006271 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00006272
6273 // Transform arguments.
6274 bool ArgChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00006275 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00006276 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
6277 &ArgChanged))
6278 return ExprError();
6279
Douglas Gregorb98b1992009-08-11 05:31:07 +00006280 if (!getDerived().AlwaysRebuild() &&
6281 Callee.get() == E->getCallee() &&
6282 !ArgChanged)
Douglas Gregor92be2a52011-12-10 00:23:21 +00006283 return SemaRef.MaybeBindToTemporary(E);;
Mike Stump1eb44332009-09-09 15:08:12 +00006284
Douglas Gregorb98b1992009-08-11 05:31:07 +00006285 // FIXME: Wrong source location information for the '('.
Mike Stump1eb44332009-09-09 15:08:12 +00006286 SourceLocation FakeLParenLoc
Douglas Gregorb98b1992009-08-11 05:31:07 +00006287 = ((Expr *)Callee.get())->getSourceRange().getBegin();
John McCall9ae2f072010-08-23 23:25:46 +00006288 return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006289 move_arg(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006290 E->getRParenLoc());
6291}
Mike Stump1eb44332009-09-09 15:08:12 +00006292
6293template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006294ExprResult
John McCall454feb92009-12-08 09:21:05 +00006295TreeTransform<Derived>::TransformMemberExpr(MemberExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006296 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006297 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006298 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006299
Douglas Gregor40d96a62011-02-28 21:54:11 +00006300 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00006301 if (E->hasQualifier()) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00006302 QualifierLoc
6303 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
6304
6305 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00006306 return ExprError();
Douglas Gregor83f6faf2009-08-31 23:41:50 +00006307 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00006308 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00006309
Eli Friedmanf595cc42009-12-04 06:40:45 +00006310 ValueDecl *Member
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00006311 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getMemberLoc(),
6312 E->getMemberDecl()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006313 if (!Member)
John McCallf312b1e2010-08-26 23:41:50 +00006314 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006315
John McCall6bb80172010-03-30 21:47:33 +00006316 NamedDecl *FoundDecl = E->getFoundDecl();
6317 if (FoundDecl == E->getMemberDecl()) {
6318 FoundDecl = Member;
6319 } else {
6320 FoundDecl = cast_or_null<NamedDecl>(
6321 getDerived().TransformDecl(E->getMemberLoc(), FoundDecl));
6322 if (!FoundDecl)
John McCallf312b1e2010-08-26 23:41:50 +00006323 return ExprError();
John McCall6bb80172010-03-30 21:47:33 +00006324 }
6325
Douglas Gregorb98b1992009-08-11 05:31:07 +00006326 if (!getDerived().AlwaysRebuild() &&
6327 Base.get() == E->getBase() &&
Douglas Gregor40d96a62011-02-28 21:54:11 +00006328 QualifierLoc == E->getQualifierLoc() &&
Douglas Gregor8a4386b2009-11-04 23:20:05 +00006329 Member == E->getMemberDecl() &&
John McCall6bb80172010-03-30 21:47:33 +00006330 FoundDecl == E->getFoundDecl() &&
John McCall096832c2010-08-19 23:49:38 +00006331 !E->hasExplicitTemplateArgs()) {
Sean Huntc3021132010-05-05 15:23:54 +00006332
Anders Carlsson1f240322009-12-22 05:24:09 +00006333 // Mark it referenced in the new context regardless.
6334 // FIXME: this is a bit instantiation-specific.
Eli Friedman5f2987c2012-02-02 03:46:19 +00006335 SemaRef.MarkMemberReferenced(E);
6336
John McCall3fa5cae2010-10-26 07:05:15 +00006337 return SemaRef.Owned(E);
Anders Carlsson1f240322009-12-22 05:24:09 +00006338 }
Douglas Gregorb98b1992009-08-11 05:31:07 +00006339
John McCalld5532b62009-11-23 01:53:49 +00006340 TemplateArgumentListInfo TransArgs;
John McCall096832c2010-08-19 23:49:38 +00006341 if (E->hasExplicitTemplateArgs()) {
John McCalld5532b62009-11-23 01:53:49 +00006342 TransArgs.setLAngleLoc(E->getLAngleLoc());
6343 TransArgs.setRAngleLoc(E->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00006344 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
6345 E->getNumTemplateArgs(),
6346 TransArgs))
6347 return ExprError();
Douglas Gregor8a4386b2009-11-04 23:20:05 +00006348 }
Sean Huntc3021132010-05-05 15:23:54 +00006349
Douglas Gregorb98b1992009-08-11 05:31:07 +00006350 // FIXME: Bogus source location for the operator
6351 SourceLocation FakeOperatorLoc
6352 = SemaRef.PP.getLocForEndOfToken(E->getBase()->getSourceRange().getEnd());
6353
John McCallc2233c52010-01-15 08:34:02 +00006354 // FIXME: to do this check properly, we will need to preserve the
6355 // first-qualifier-in-scope here, just in case we had a dependent
6356 // base (and therefore couldn't do the check) and a
6357 // nested-name-qualifier (and therefore could do the lookup).
6358 NamedDecl *FirstQualifierInScope = 0;
6359
John McCall9ae2f072010-08-23 23:25:46 +00006360 return getDerived().RebuildMemberExpr(Base.get(), FakeOperatorLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006361 E->isArrow(),
Douglas Gregor40d96a62011-02-28 21:54:11 +00006362 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00006363 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00006364 E->getMemberNameInfo(),
Douglas Gregor8a4386b2009-11-04 23:20:05 +00006365 Member,
John McCall6bb80172010-03-30 21:47:33 +00006366 FoundDecl,
John McCall096832c2010-08-19 23:49:38 +00006367 (E->hasExplicitTemplateArgs()
John McCalld5532b62009-11-23 01:53:49 +00006368 ? &TransArgs : 0),
John McCallc2233c52010-01-15 08:34:02 +00006369 FirstQualifierInScope);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006370}
Mike Stump1eb44332009-09-09 15:08:12 +00006371
Douglas Gregorb98b1992009-08-11 05:31:07 +00006372template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006373ExprResult
John McCall454feb92009-12-08 09:21:05 +00006374TreeTransform<Derived>::TransformBinaryOperator(BinaryOperator *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006375 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006376 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006377 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006378
John McCall60d7b3a2010-08-24 06:29:42 +00006379 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006380 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006381 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006382
Douglas Gregorb98b1992009-08-11 05:31:07 +00006383 if (!getDerived().AlwaysRebuild() &&
6384 LHS.get() == E->getLHS() &&
6385 RHS.get() == E->getRHS())
John McCall3fa5cae2010-10-26 07:05:15 +00006386 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006387
Douglas Gregorb98b1992009-08-11 05:31:07 +00006388 return getDerived().RebuildBinaryOperator(E->getOperatorLoc(), E->getOpcode(),
John McCall9ae2f072010-08-23 23:25:46 +00006389 LHS.get(), RHS.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006390}
6391
Mike Stump1eb44332009-09-09 15:08:12 +00006392template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006393ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00006394TreeTransform<Derived>::TransformCompoundAssignOperator(
John McCall454feb92009-12-08 09:21:05 +00006395 CompoundAssignOperator *E) {
6396 return getDerived().TransformBinaryOperator(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006397}
Mike Stump1eb44332009-09-09 15:08:12 +00006398
Douglas Gregorb98b1992009-08-11 05:31:07 +00006399template<typename Derived>
John McCall56ca35d2011-02-17 10:25:35 +00006400ExprResult TreeTransform<Derived>::
6401TransformBinaryConditionalOperator(BinaryConditionalOperator *e) {
6402 // Just rebuild the common and RHS expressions and see whether we
6403 // get any changes.
6404
6405 ExprResult commonExpr = getDerived().TransformExpr(e->getCommon());
6406 if (commonExpr.isInvalid())
6407 return ExprError();
6408
6409 ExprResult rhs = getDerived().TransformExpr(e->getFalseExpr());
6410 if (rhs.isInvalid())
6411 return ExprError();
6412
6413 if (!getDerived().AlwaysRebuild() &&
6414 commonExpr.get() == e->getCommon() &&
6415 rhs.get() == e->getFalseExpr())
6416 return SemaRef.Owned(e);
6417
6418 return getDerived().RebuildConditionalOperator(commonExpr.take(),
6419 e->getQuestionLoc(),
6420 0,
6421 e->getColonLoc(),
6422 rhs.get());
6423}
6424
6425template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006426ExprResult
John McCall454feb92009-12-08 09:21:05 +00006427TreeTransform<Derived>::TransformConditionalOperator(ConditionalOperator *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006428 ExprResult Cond = getDerived().TransformExpr(E->getCond());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006429 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006430 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006431
John McCall60d7b3a2010-08-24 06:29:42 +00006432 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006433 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006434 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006435
John McCall60d7b3a2010-08-24 06:29:42 +00006436 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006437 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006438 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006439
Douglas Gregorb98b1992009-08-11 05:31:07 +00006440 if (!getDerived().AlwaysRebuild() &&
6441 Cond.get() == E->getCond() &&
6442 LHS.get() == E->getLHS() &&
6443 RHS.get() == E->getRHS())
John McCall3fa5cae2010-10-26 07:05:15 +00006444 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006445
John McCall9ae2f072010-08-23 23:25:46 +00006446 return getDerived().RebuildConditionalOperator(Cond.get(),
Douglas Gregor47e1f7c2009-08-26 14:37:04 +00006447 E->getQuestionLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006448 LHS.get(),
Douglas Gregor47e1f7c2009-08-26 14:37:04 +00006449 E->getColonLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006450 RHS.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006451}
Mike Stump1eb44332009-09-09 15:08:12 +00006452
6453template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006454ExprResult
John McCall454feb92009-12-08 09:21:05 +00006455TreeTransform<Derived>::TransformImplicitCastExpr(ImplicitCastExpr *E) {
Douglas Gregora88cfbf2009-12-12 18:16:41 +00006456 // Implicit casts are eliminated during transformation, since they
6457 // will be recomputed by semantic analysis after transformation.
Douglas Gregor6eef5192009-12-14 19:27:10 +00006458 return getDerived().TransformExpr(E->getSubExprAsWritten());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006459}
Mike Stump1eb44332009-09-09 15:08:12 +00006460
Douglas Gregorb98b1992009-08-11 05:31:07 +00006461template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006462ExprResult
John McCall454feb92009-12-08 09:21:05 +00006463TreeTransform<Derived>::TransformCStyleCastExpr(CStyleCastExpr *E) {
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006464 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6465 if (!Type)
6466 return ExprError();
6467
John McCall60d7b3a2010-08-24 06:29:42 +00006468 ExprResult SubExpr
Douglas Gregor6eef5192009-12-14 19:27:10 +00006469 = getDerived().TransformExpr(E->getSubExprAsWritten());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006470 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006471 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006472
Douglas Gregorb98b1992009-08-11 05:31:07 +00006473 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006474 Type == E->getTypeInfoAsWritten() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006475 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006476 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006477
John McCall9d125032010-01-15 18:39:57 +00006478 return getDerived().RebuildCStyleCastExpr(E->getLParenLoc(),
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006479 Type,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006480 E->getRParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006481 SubExpr.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006482}
Mike Stump1eb44332009-09-09 15:08:12 +00006483
Douglas Gregorb98b1992009-08-11 05:31:07 +00006484template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006485ExprResult
John McCall454feb92009-12-08 09:21:05 +00006486TreeTransform<Derived>::TransformCompoundLiteralExpr(CompoundLiteralExpr *E) {
John McCall42f56b52010-01-18 19:35:47 +00006487 TypeSourceInfo *OldT = E->getTypeSourceInfo();
6488 TypeSourceInfo *NewT = getDerived().TransformType(OldT);
6489 if (!NewT)
John McCallf312b1e2010-08-26 23:41:50 +00006490 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006491
John McCall60d7b3a2010-08-24 06:29:42 +00006492 ExprResult Init = getDerived().TransformExpr(E->getInitializer());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006493 if (Init.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006494 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006495
Douglas Gregorb98b1992009-08-11 05:31:07 +00006496 if (!getDerived().AlwaysRebuild() &&
John McCall42f56b52010-01-18 19:35:47 +00006497 OldT == NewT &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006498 Init.get() == E->getInitializer())
Douglas Gregor92be2a52011-12-10 00:23:21 +00006499 return SemaRef.MaybeBindToTemporary(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006500
John McCall1d7d8d62010-01-19 22:33:45 +00006501 // Note: the expression type doesn't necessarily match the
6502 // type-as-written, but that's okay, because it should always be
6503 // derivable from the initializer.
6504
John McCall42f56b52010-01-18 19:35:47 +00006505 return getDerived().RebuildCompoundLiteralExpr(E->getLParenLoc(), NewT,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006506 /*FIXME:*/E->getInitializer()->getLocEnd(),
John McCall9ae2f072010-08-23 23:25:46 +00006507 Init.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006508}
Mike Stump1eb44332009-09-09 15:08:12 +00006509
Douglas Gregorb98b1992009-08-11 05:31:07 +00006510template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006511ExprResult
John McCall454feb92009-12-08 09:21:05 +00006512TreeTransform<Derived>::TransformExtVectorElementExpr(ExtVectorElementExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006513 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006514 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006515 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006516
Douglas Gregorb98b1992009-08-11 05:31:07 +00006517 if (!getDerived().AlwaysRebuild() &&
6518 Base.get() == E->getBase())
John McCall3fa5cae2010-10-26 07:05:15 +00006519 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006520
Douglas Gregorb98b1992009-08-11 05:31:07 +00006521 // FIXME: Bad source location
Mike Stump1eb44332009-09-09 15:08:12 +00006522 SourceLocation FakeOperatorLoc
Douglas Gregorb98b1992009-08-11 05:31:07 +00006523 = SemaRef.PP.getLocForEndOfToken(E->getBase()->getLocEnd());
John McCall9ae2f072010-08-23 23:25:46 +00006524 return getDerived().RebuildExtVectorElementExpr(Base.get(), FakeOperatorLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006525 E->getAccessorLoc(),
6526 E->getAccessor());
6527}
Mike Stump1eb44332009-09-09 15:08:12 +00006528
Douglas Gregorb98b1992009-08-11 05:31:07 +00006529template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006530ExprResult
John McCall454feb92009-12-08 09:21:05 +00006531TreeTransform<Derived>::TransformInitListExpr(InitListExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006532 bool InitChanged = false;
Mike Stump1eb44332009-09-09 15:08:12 +00006533
John McCallca0408f2010-08-23 06:44:23 +00006534 ASTOwningVector<Expr*, 4> Inits(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00006535 if (getDerived().TransformExprs(E->getInits(), E->getNumInits(), false,
6536 Inits, &InitChanged))
6537 return ExprError();
6538
Douglas Gregorb98b1992009-08-11 05:31:07 +00006539 if (!getDerived().AlwaysRebuild() && !InitChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00006540 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006541
Douglas Gregorb98b1992009-08-11 05:31:07 +00006542 return getDerived().RebuildInitList(E->getLBraceLoc(), move_arg(Inits),
Douglas Gregore48319a2009-11-09 17:16:50 +00006543 E->getRBraceLoc(), E->getType());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006544}
Mike Stump1eb44332009-09-09 15:08:12 +00006545
Douglas Gregorb98b1992009-08-11 05:31:07 +00006546template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006547ExprResult
John McCall454feb92009-12-08 09:21:05 +00006548TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006549 Designation Desig;
Mike Stump1eb44332009-09-09 15:08:12 +00006550
Douglas Gregor43959a92009-08-20 07:17:43 +00006551 // transform the initializer value
John McCall60d7b3a2010-08-24 06:29:42 +00006552 ExprResult Init = getDerived().TransformExpr(E->getInit());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006553 if (Init.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006554 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006555
Douglas Gregor43959a92009-08-20 07:17:43 +00006556 // transform the designators.
John McCallca0408f2010-08-23 06:44:23 +00006557 ASTOwningVector<Expr*, 4> ArrayExprs(SemaRef);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006558 bool ExprChanged = false;
6559 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
6560 DEnd = E->designators_end();
6561 D != DEnd; ++D) {
6562 if (D->isFieldDesignator()) {
6563 Desig.AddDesignator(Designator::getField(D->getFieldName(),
6564 D->getDotLoc(),
6565 D->getFieldLoc()));
6566 continue;
6567 }
Mike Stump1eb44332009-09-09 15:08:12 +00006568
Douglas Gregorb98b1992009-08-11 05:31:07 +00006569 if (D->isArrayDesignator()) {
John McCall60d7b3a2010-08-24 06:29:42 +00006570 ExprResult Index = getDerived().TransformExpr(E->getArrayIndex(*D));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006571 if (Index.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006572 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006573
6574 Desig.AddDesignator(Designator::getArray(Index.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006575 D->getLBracketLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +00006576
Douglas Gregorb98b1992009-08-11 05:31:07 +00006577 ExprChanged = ExprChanged || Init.get() != E->getArrayIndex(*D);
6578 ArrayExprs.push_back(Index.release());
6579 continue;
6580 }
Mike Stump1eb44332009-09-09 15:08:12 +00006581
Douglas Gregorb98b1992009-08-11 05:31:07 +00006582 assert(D->isArrayRangeDesignator() && "New kind of designator?");
John McCall60d7b3a2010-08-24 06:29:42 +00006583 ExprResult Start
Douglas Gregorb98b1992009-08-11 05:31:07 +00006584 = getDerived().TransformExpr(E->getArrayRangeStart(*D));
6585 if (Start.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006586 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006587
John McCall60d7b3a2010-08-24 06:29:42 +00006588 ExprResult End = getDerived().TransformExpr(E->getArrayRangeEnd(*D));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006589 if (End.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006590 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006591
6592 Desig.AddDesignator(Designator::getArrayRange(Start.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006593 End.get(),
6594 D->getLBracketLoc(),
6595 D->getEllipsisLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +00006596
Douglas Gregorb98b1992009-08-11 05:31:07 +00006597 ExprChanged = ExprChanged || Start.get() != E->getArrayRangeStart(*D) ||
6598 End.get() != E->getArrayRangeEnd(*D);
Mike Stump1eb44332009-09-09 15:08:12 +00006599
Douglas Gregorb98b1992009-08-11 05:31:07 +00006600 ArrayExprs.push_back(Start.release());
6601 ArrayExprs.push_back(End.release());
6602 }
Mike Stump1eb44332009-09-09 15:08:12 +00006603
Douglas Gregorb98b1992009-08-11 05:31:07 +00006604 if (!getDerived().AlwaysRebuild() &&
6605 Init.get() == E->getInit() &&
6606 !ExprChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00006607 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006608
Douglas Gregorb98b1992009-08-11 05:31:07 +00006609 return getDerived().RebuildDesignatedInitExpr(Desig, move_arg(ArrayExprs),
6610 E->getEqualOrColonLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006611 E->usesGNUSyntax(), Init.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006612}
Mike Stump1eb44332009-09-09 15:08:12 +00006613
Douglas Gregorb98b1992009-08-11 05:31:07 +00006614template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006615ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00006616TreeTransform<Derived>::TransformImplicitValueInitExpr(
John McCall454feb92009-12-08 09:21:05 +00006617 ImplicitValueInitExpr *E) {
Douglas Gregor5557b252009-10-28 00:29:27 +00006618 TemporaryBase Rebase(*this, E->getLocStart(), DeclarationName());
Sean Huntc3021132010-05-05 15:23:54 +00006619
Douglas Gregor5557b252009-10-28 00:29:27 +00006620 // FIXME: Will we ever have proper type location here? Will we actually
6621 // need to transform the type?
Douglas Gregorb98b1992009-08-11 05:31:07 +00006622 QualType T = getDerived().TransformType(E->getType());
6623 if (T.isNull())
John McCallf312b1e2010-08-26 23:41:50 +00006624 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006625
Douglas Gregorb98b1992009-08-11 05:31:07 +00006626 if (!getDerived().AlwaysRebuild() &&
6627 T == E->getType())
John McCall3fa5cae2010-10-26 07:05:15 +00006628 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006629
Douglas Gregorb98b1992009-08-11 05:31:07 +00006630 return getDerived().RebuildImplicitValueInitExpr(T);
6631}
Mike Stump1eb44332009-09-09 15:08:12 +00006632
Douglas Gregorb98b1992009-08-11 05:31:07 +00006633template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006634ExprResult
John McCall454feb92009-12-08 09:21:05 +00006635TreeTransform<Derived>::TransformVAArgExpr(VAArgExpr *E) {
Douglas Gregor9bcd4d42010-08-10 14:27:00 +00006636 TypeSourceInfo *TInfo = getDerived().TransformType(E->getWrittenTypeInfo());
6637 if (!TInfo)
John McCallf312b1e2010-08-26 23:41:50 +00006638 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006639
John McCall60d7b3a2010-08-24 06:29:42 +00006640 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006641 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006642 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006643
Douglas Gregorb98b1992009-08-11 05:31:07 +00006644 if (!getDerived().AlwaysRebuild() &&
Abramo Bagnara2cad9002010-08-10 10:06:15 +00006645 TInfo == E->getWrittenTypeInfo() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006646 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006647 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006648
John McCall9ae2f072010-08-23 23:25:46 +00006649 return getDerived().RebuildVAArgExpr(E->getBuiltinLoc(), SubExpr.get(),
Abramo Bagnara2cad9002010-08-10 10:06:15 +00006650 TInfo, E->getRParenLoc());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006651}
6652
6653template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006654ExprResult
John McCall454feb92009-12-08 09:21:05 +00006655TreeTransform<Derived>::TransformParenListExpr(ParenListExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006656 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00006657 ASTOwningVector<Expr*, 4> Inits(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00006658 if (TransformExprs(E->getExprs(), E->getNumExprs(), true, Inits,
6659 &ArgumentChanged))
6660 return ExprError();
6661
Douglas Gregorb98b1992009-08-11 05:31:07 +00006662 return getDerived().RebuildParenListExpr(E->getLParenLoc(),
6663 move_arg(Inits),
6664 E->getRParenLoc());
6665}
Mike Stump1eb44332009-09-09 15:08:12 +00006666
Douglas Gregorb98b1992009-08-11 05:31:07 +00006667/// \brief Transform an address-of-label expression.
6668///
6669/// By default, the transformation of an address-of-label expression always
6670/// rebuilds the expression, so that the label identifier can be resolved to
6671/// the corresponding label statement by semantic analysis.
6672template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006673ExprResult
John McCall454feb92009-12-08 09:21:05 +00006674TreeTransform<Derived>::TransformAddrLabelExpr(AddrLabelExpr *E) {
Chris Lattner57ad3782011-02-17 20:34:02 +00006675 Decl *LD = getDerived().TransformDecl(E->getLabel()->getLocation(),
6676 E->getLabel());
6677 if (!LD)
6678 return ExprError();
6679
Douglas Gregorb98b1992009-08-11 05:31:07 +00006680 return getDerived().RebuildAddrLabelExpr(E->getAmpAmpLoc(), E->getLabelLoc(),
Chris Lattner57ad3782011-02-17 20:34:02 +00006681 cast<LabelDecl>(LD));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006682}
Mike Stump1eb44332009-09-09 15:08:12 +00006683
6684template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006685ExprResult
John McCall454feb92009-12-08 09:21:05 +00006686TreeTransform<Derived>::TransformStmtExpr(StmtExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006687 StmtResult SubStmt
Douglas Gregorb98b1992009-08-11 05:31:07 +00006688 = getDerived().TransformCompoundStmt(E->getSubStmt(), true);
6689 if (SubStmt.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006690 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006691
Douglas Gregorb98b1992009-08-11 05:31:07 +00006692 if (!getDerived().AlwaysRebuild() &&
6693 SubStmt.get() == E->getSubStmt())
Douglas Gregor92be2a52011-12-10 00:23:21 +00006694 return SemaRef.MaybeBindToTemporary(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006695
6696 return getDerived().RebuildStmtExpr(E->getLParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006697 SubStmt.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006698 E->getRParenLoc());
6699}
Mike Stump1eb44332009-09-09 15:08:12 +00006700
Douglas Gregorb98b1992009-08-11 05:31:07 +00006701template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006702ExprResult
John McCall454feb92009-12-08 09:21:05 +00006703TreeTransform<Derived>::TransformChooseExpr(ChooseExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006704 ExprResult Cond = getDerived().TransformExpr(E->getCond());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006705 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006706 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006707
John McCall60d7b3a2010-08-24 06:29:42 +00006708 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006709 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006710 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006711
John McCall60d7b3a2010-08-24 06:29:42 +00006712 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006713 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006714 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006715
Douglas Gregorb98b1992009-08-11 05:31:07 +00006716 if (!getDerived().AlwaysRebuild() &&
6717 Cond.get() == E->getCond() &&
6718 LHS.get() == E->getLHS() &&
6719 RHS.get() == E->getRHS())
John McCall3fa5cae2010-10-26 07:05:15 +00006720 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006721
Douglas Gregorb98b1992009-08-11 05:31:07 +00006722 return getDerived().RebuildChooseExpr(E->getBuiltinLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006723 Cond.get(), LHS.get(), RHS.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006724 E->getRParenLoc());
6725}
Mike Stump1eb44332009-09-09 15:08:12 +00006726
Douglas Gregorb98b1992009-08-11 05:31:07 +00006727template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006728ExprResult
John McCall454feb92009-12-08 09:21:05 +00006729TreeTransform<Derived>::TransformGNUNullExpr(GNUNullExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006730 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006731}
6732
6733template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006734ExprResult
John McCall454feb92009-12-08 09:21:05 +00006735TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
Douglas Gregor668d6d92009-12-13 20:44:55 +00006736 switch (E->getOperator()) {
6737 case OO_New:
6738 case OO_Delete:
6739 case OO_Array_New:
6740 case OO_Array_Delete:
6741 llvm_unreachable("new and delete operators cannot use CXXOperatorCallExpr");
John McCallf312b1e2010-08-26 23:41:50 +00006742 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00006743
Douglas Gregor668d6d92009-12-13 20:44:55 +00006744 case OO_Call: {
6745 // This is a call to an object's operator().
6746 assert(E->getNumArgs() >= 1 && "Object call is missing arguments");
6747
6748 // Transform the object itself.
John McCall60d7b3a2010-08-24 06:29:42 +00006749 ExprResult Object = getDerived().TransformExpr(E->getArg(0));
Douglas Gregor668d6d92009-12-13 20:44:55 +00006750 if (Object.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006751 return ExprError();
Douglas Gregor668d6d92009-12-13 20:44:55 +00006752
6753 // FIXME: Poor location information
6754 SourceLocation FakeLParenLoc
6755 = SemaRef.PP.getLocForEndOfToken(
6756 static_cast<Expr *>(Object.get())->getLocEnd());
6757
6758 // Transform the call arguments.
John McCallca0408f2010-08-23 06:44:23 +00006759 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00006760 if (getDerived().TransformExprs(E->getArgs() + 1, E->getNumArgs() - 1, true,
6761 Args))
6762 return ExprError();
Douglas Gregor668d6d92009-12-13 20:44:55 +00006763
John McCall9ae2f072010-08-23 23:25:46 +00006764 return getDerived().RebuildCallExpr(Object.get(), FakeLParenLoc,
Douglas Gregor668d6d92009-12-13 20:44:55 +00006765 move_arg(Args),
Douglas Gregor668d6d92009-12-13 20:44:55 +00006766 E->getLocEnd());
6767 }
6768
6769#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
6770 case OO_##Name:
6771#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
6772#include "clang/Basic/OperatorKinds.def"
6773 case OO_Subscript:
6774 // Handled below.
6775 break;
6776
6777 case OO_Conditional:
6778 llvm_unreachable("conditional operator is not actually overloadable");
John McCallf312b1e2010-08-26 23:41:50 +00006779 return ExprError();
Douglas Gregor668d6d92009-12-13 20:44:55 +00006780
6781 case OO_None:
6782 case NUM_OVERLOADED_OPERATORS:
6783 llvm_unreachable("not an overloaded operator?");
John McCallf312b1e2010-08-26 23:41:50 +00006784 return ExprError();
Douglas Gregor668d6d92009-12-13 20:44:55 +00006785 }
6786
John McCall60d7b3a2010-08-24 06:29:42 +00006787 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006788 if (Callee.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006789 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006790
John McCall60d7b3a2010-08-24 06:29:42 +00006791 ExprResult First = getDerived().TransformExpr(E->getArg(0));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006792 if (First.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006793 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00006794
John McCall60d7b3a2010-08-24 06:29:42 +00006795 ExprResult Second;
Douglas Gregorb98b1992009-08-11 05:31:07 +00006796 if (E->getNumArgs() == 2) {
6797 Second = getDerived().TransformExpr(E->getArg(1));
6798 if (Second.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006799 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00006800 }
Mike Stump1eb44332009-09-09 15:08:12 +00006801
Douglas Gregorb98b1992009-08-11 05:31:07 +00006802 if (!getDerived().AlwaysRebuild() &&
6803 Callee.get() == E->getCallee() &&
6804 First.get() == E->getArg(0) &&
Mike Stump1eb44332009-09-09 15:08:12 +00006805 (E->getNumArgs() != 2 || Second.get() == E->getArg(1)))
Douglas Gregor92be2a52011-12-10 00:23:21 +00006806 return SemaRef.MaybeBindToTemporary(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006807
Douglas Gregorb98b1992009-08-11 05:31:07 +00006808 return getDerived().RebuildCXXOperatorCallExpr(E->getOperator(),
6809 E->getOperatorLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006810 Callee.get(),
6811 First.get(),
6812 Second.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006813}
Mike Stump1eb44332009-09-09 15:08:12 +00006814
Douglas Gregorb98b1992009-08-11 05:31:07 +00006815template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006816ExprResult
John McCall454feb92009-12-08 09:21:05 +00006817TreeTransform<Derived>::TransformCXXMemberCallExpr(CXXMemberCallExpr *E) {
6818 return getDerived().TransformCallExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006819}
Mike Stump1eb44332009-09-09 15:08:12 +00006820
Douglas Gregorb98b1992009-08-11 05:31:07 +00006821template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006822ExprResult
Peter Collingbournee08ce652011-02-09 21:07:24 +00006823TreeTransform<Derived>::TransformCUDAKernelCallExpr(CUDAKernelCallExpr *E) {
6824 // Transform the callee.
6825 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
6826 if (Callee.isInvalid())
6827 return ExprError();
6828
6829 // Transform exec config.
6830 ExprResult EC = getDerived().TransformCallExpr(E->getConfig());
6831 if (EC.isInvalid())
6832 return ExprError();
6833
6834 // Transform arguments.
6835 bool ArgChanged = false;
6836 ASTOwningVector<Expr*> Args(SemaRef);
6837 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
6838 &ArgChanged))
6839 return ExprError();
6840
6841 if (!getDerived().AlwaysRebuild() &&
6842 Callee.get() == E->getCallee() &&
6843 !ArgChanged)
Douglas Gregor92be2a52011-12-10 00:23:21 +00006844 return SemaRef.MaybeBindToTemporary(E);
Peter Collingbournee08ce652011-02-09 21:07:24 +00006845
6846 // FIXME: Wrong source location information for the '('.
6847 SourceLocation FakeLParenLoc
6848 = ((Expr *)Callee.get())->getSourceRange().getBegin();
6849 return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc,
6850 move_arg(Args),
6851 E->getRParenLoc(), EC.get());
6852}
6853
6854template<typename Derived>
6855ExprResult
John McCall454feb92009-12-08 09:21:05 +00006856TreeTransform<Derived>::TransformCXXNamedCastExpr(CXXNamedCastExpr *E) {
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006857 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6858 if (!Type)
6859 return ExprError();
6860
John McCall60d7b3a2010-08-24 06:29:42 +00006861 ExprResult SubExpr
Douglas Gregor6eef5192009-12-14 19:27:10 +00006862 = getDerived().TransformExpr(E->getSubExprAsWritten());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006863 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006864 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006865
Douglas Gregorb98b1992009-08-11 05:31:07 +00006866 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006867 Type == E->getTypeInfoAsWritten() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006868 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006869 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006870
Douglas Gregorb98b1992009-08-11 05:31:07 +00006871 // FIXME: Poor source location information here.
Mike Stump1eb44332009-09-09 15:08:12 +00006872 SourceLocation FakeLAngleLoc
Douglas Gregorb98b1992009-08-11 05:31:07 +00006873 = SemaRef.PP.getLocForEndOfToken(E->getOperatorLoc());
6874 SourceLocation FakeRAngleLoc = E->getSubExpr()->getSourceRange().getBegin();
6875 SourceLocation FakeRParenLoc
6876 = SemaRef.PP.getLocForEndOfToken(
6877 E->getSubExpr()->getSourceRange().getEnd());
6878 return getDerived().RebuildCXXNamedCastExpr(E->getOperatorLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +00006879 E->getStmtClass(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006880 FakeLAngleLoc,
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006881 Type,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006882 FakeRAngleLoc,
6883 FakeRAngleLoc,
John McCall9ae2f072010-08-23 23:25:46 +00006884 SubExpr.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006885 FakeRParenLoc);
6886}
Mike Stump1eb44332009-09-09 15:08:12 +00006887
Douglas Gregorb98b1992009-08-11 05:31:07 +00006888template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006889ExprResult
John McCall454feb92009-12-08 09:21:05 +00006890TreeTransform<Derived>::TransformCXXStaticCastExpr(CXXStaticCastExpr *E) {
6891 return getDerived().TransformCXXNamedCastExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006892}
Mike Stump1eb44332009-09-09 15:08:12 +00006893
6894template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006895ExprResult
John McCall454feb92009-12-08 09:21:05 +00006896TreeTransform<Derived>::TransformCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
6897 return getDerived().TransformCXXNamedCastExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006898}
6899
Douglas Gregorb98b1992009-08-11 05:31:07 +00006900template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006901ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00006902TreeTransform<Derived>::TransformCXXReinterpretCastExpr(
John McCall454feb92009-12-08 09:21:05 +00006903 CXXReinterpretCastExpr *E) {
6904 return getDerived().TransformCXXNamedCastExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006905}
Mike Stump1eb44332009-09-09 15:08:12 +00006906
Douglas Gregorb98b1992009-08-11 05:31:07 +00006907template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006908ExprResult
John McCall454feb92009-12-08 09:21:05 +00006909TreeTransform<Derived>::TransformCXXConstCastExpr(CXXConstCastExpr *E) {
6910 return getDerived().TransformCXXNamedCastExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006911}
Mike Stump1eb44332009-09-09 15:08:12 +00006912
Douglas Gregorb98b1992009-08-11 05:31:07 +00006913template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006914ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00006915TreeTransform<Derived>::TransformCXXFunctionalCastExpr(
John McCall454feb92009-12-08 09:21:05 +00006916 CXXFunctionalCastExpr *E) {
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006917 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6918 if (!Type)
6919 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006920
John McCall60d7b3a2010-08-24 06:29:42 +00006921 ExprResult SubExpr
Douglas Gregor6eef5192009-12-14 19:27:10 +00006922 = getDerived().TransformExpr(E->getSubExprAsWritten());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006923 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006924 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006925
Douglas Gregorb98b1992009-08-11 05:31:07 +00006926 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006927 Type == E->getTypeInfoAsWritten() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006928 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006929 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006930
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006931 return getDerived().RebuildCXXFunctionalCastExpr(Type,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006932 /*FIXME:*/E->getSubExpr()->getLocStart(),
John McCall9ae2f072010-08-23 23:25:46 +00006933 SubExpr.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006934 E->getRParenLoc());
6935}
Mike Stump1eb44332009-09-09 15:08:12 +00006936
Douglas Gregorb98b1992009-08-11 05:31:07 +00006937template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006938ExprResult
John McCall454feb92009-12-08 09:21:05 +00006939TreeTransform<Derived>::TransformCXXTypeidExpr(CXXTypeidExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006940 if (E->isTypeOperand()) {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00006941 TypeSourceInfo *TInfo
6942 = getDerived().TransformType(E->getTypeOperandSourceInfo());
6943 if (!TInfo)
John McCallf312b1e2010-08-26 23:41:50 +00006944 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006945
Douglas Gregorb98b1992009-08-11 05:31:07 +00006946 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00006947 TInfo == E->getTypeOperandSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00006948 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006949
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00006950 return getDerived().RebuildCXXTypeidExpr(E->getType(),
6951 E->getLocStart(),
6952 TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006953 E->getLocEnd());
6954 }
Mike Stump1eb44332009-09-09 15:08:12 +00006955
Eli Friedmanef331b72012-01-20 01:26:23 +00006956 // We don't know whether the subexpression is potentially evaluated until
6957 // after we perform semantic analysis. We speculatively assume it is
6958 // unevaluated; it will get fixed later if the subexpression is in fact
Douglas Gregorb98b1992009-08-11 05:31:07 +00006959 // potentially evaluated.
Eli Friedmanef331b72012-01-20 01:26:23 +00006960 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00006961
John McCall60d7b3a2010-08-24 06:29:42 +00006962 ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006963 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006964 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006965
Douglas Gregorb98b1992009-08-11 05:31:07 +00006966 if (!getDerived().AlwaysRebuild() &&
6967 SubExpr.get() == E->getExprOperand())
John McCall3fa5cae2010-10-26 07:05:15 +00006968 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006969
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00006970 return getDerived().RebuildCXXTypeidExpr(E->getType(),
6971 E->getLocStart(),
John McCall9ae2f072010-08-23 23:25:46 +00006972 SubExpr.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006973 E->getLocEnd());
6974}
6975
6976template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006977ExprResult
Francois Pichet01b7c302010-09-08 12:20:18 +00006978TreeTransform<Derived>::TransformCXXUuidofExpr(CXXUuidofExpr *E) {
6979 if (E->isTypeOperand()) {
6980 TypeSourceInfo *TInfo
6981 = getDerived().TransformType(E->getTypeOperandSourceInfo());
6982 if (!TInfo)
6983 return ExprError();
6984
6985 if (!getDerived().AlwaysRebuild() &&
6986 TInfo == E->getTypeOperandSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00006987 return SemaRef.Owned(E);
Francois Pichet01b7c302010-09-08 12:20:18 +00006988
Douglas Gregor3c52a212011-03-06 17:40:41 +00006989 return getDerived().RebuildCXXUuidofExpr(E->getType(),
Francois Pichet01b7c302010-09-08 12:20:18 +00006990 E->getLocStart(),
6991 TInfo,
6992 E->getLocEnd());
6993 }
6994
Francois Pichet01b7c302010-09-08 12:20:18 +00006995 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
6996
6997 ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
6998 if (SubExpr.isInvalid())
6999 return ExprError();
7000
7001 if (!getDerived().AlwaysRebuild() &&
7002 SubExpr.get() == E->getExprOperand())
John McCall3fa5cae2010-10-26 07:05:15 +00007003 return SemaRef.Owned(E);
Francois Pichet01b7c302010-09-08 12:20:18 +00007004
7005 return getDerived().RebuildCXXUuidofExpr(E->getType(),
7006 E->getLocStart(),
7007 SubExpr.get(),
7008 E->getLocEnd());
7009}
7010
7011template<typename Derived>
7012ExprResult
John McCall454feb92009-12-08 09:21:05 +00007013TreeTransform<Derived>::TransformCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00007014 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007015}
Mike Stump1eb44332009-09-09 15:08:12 +00007016
Douglas Gregorb98b1992009-08-11 05:31:07 +00007017template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007018ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00007019TreeTransform<Derived>::TransformCXXNullPtrLiteralExpr(
John McCall454feb92009-12-08 09:21:05 +00007020 CXXNullPtrLiteralExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00007021 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007022}
Mike Stump1eb44332009-09-09 15:08:12 +00007023
Douglas Gregorb98b1992009-08-11 05:31:07 +00007024template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007025ExprResult
John McCall454feb92009-12-08 09:21:05 +00007026TreeTransform<Derived>::TransformCXXThisExpr(CXXThisExpr *E) {
Douglas Gregorba48d6a2010-09-09 16:55:46 +00007027 DeclContext *DC = getSema().getFunctionLevelDeclContext();
Richard Smith7a614d82011-06-11 17:19:42 +00007028 QualType T;
7029 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
7030 T = MD->getThisType(getSema().Context);
7031 else
7032 T = getSema().Context.getPointerType(
7033 getSema().Context.getRecordType(cast<CXXRecordDecl>(DC)));
Mike Stump1eb44332009-09-09 15:08:12 +00007034
Douglas Gregorba48d6a2010-09-09 16:55:46 +00007035 if (!getDerived().AlwaysRebuild() && T == E->getType())
John McCall3fa5cae2010-10-26 07:05:15 +00007036 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007037
Douglas Gregor828a1972010-01-07 23:12:05 +00007038 return getDerived().RebuildCXXThisExpr(E->getLocStart(), T, E->isImplicit());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007039}
Mike Stump1eb44332009-09-09 15:08:12 +00007040
Douglas Gregorb98b1992009-08-11 05:31:07 +00007041template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007042ExprResult
John McCall454feb92009-12-08 09:21:05 +00007043TreeTransform<Derived>::TransformCXXThrowExpr(CXXThrowExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00007044 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007045 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007046 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007047
Douglas Gregorb98b1992009-08-11 05:31:07 +00007048 if (!getDerived().AlwaysRebuild() &&
7049 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00007050 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007051
Douglas Gregorbca01b42011-07-06 22:04:06 +00007052 return getDerived().RebuildCXXThrowExpr(E->getThrowLoc(), SubExpr.get(),
7053 E->isThrownVariableInScope());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007054}
Mike Stump1eb44332009-09-09 15:08:12 +00007055
Douglas Gregorb98b1992009-08-11 05:31:07 +00007056template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007057ExprResult
John McCall454feb92009-12-08 09:21:05 +00007058TreeTransform<Derived>::TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +00007059 ParmVarDecl *Param
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007060 = cast_or_null<ParmVarDecl>(getDerived().TransformDecl(E->getLocStart(),
7061 E->getParam()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00007062 if (!Param)
John McCallf312b1e2010-08-26 23:41:50 +00007063 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007064
Chandler Carruth53cb6f82010-02-08 06:42:49 +00007065 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00007066 Param == E->getParam())
John McCall3fa5cae2010-10-26 07:05:15 +00007067 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007068
Douglas Gregor036aed12009-12-23 23:03:06 +00007069 return getDerived().RebuildCXXDefaultArgExpr(E->getUsedLocation(), Param);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007070}
Mike Stump1eb44332009-09-09 15:08:12 +00007071
Douglas Gregorb98b1992009-08-11 05:31:07 +00007072template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007073ExprResult
Douglas Gregorab6677e2010-09-08 00:15:04 +00007074TreeTransform<Derived>::TransformCXXScalarValueInitExpr(
7075 CXXScalarValueInitExpr *E) {
7076 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
7077 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007078 return ExprError();
Douglas Gregorab6677e2010-09-08 00:15:04 +00007079
Douglas Gregorb98b1992009-08-11 05:31:07 +00007080 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorab6677e2010-09-08 00:15:04 +00007081 T == E->getTypeSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00007082 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007083
Douglas Gregorab6677e2010-09-08 00:15:04 +00007084 return getDerived().RebuildCXXScalarValueInitExpr(T,
7085 /*FIXME:*/T->getTypeLoc().getEndLoc(),
Douglas Gregored8abf12010-07-08 06:14:04 +00007086 E->getRParenLoc());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007087}
Mike Stump1eb44332009-09-09 15:08:12 +00007088
Douglas Gregorb98b1992009-08-11 05:31:07 +00007089template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007090ExprResult
John McCall454feb92009-12-08 09:21:05 +00007091TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00007092 // Transform the type that we're allocating
Douglas Gregor1bb2a932010-09-07 21:49:58 +00007093 TypeSourceInfo *AllocTypeInfo
7094 = getDerived().TransformType(E->getAllocatedTypeSourceInfo());
7095 if (!AllocTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00007096 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007097
Douglas Gregorb98b1992009-08-11 05:31:07 +00007098 // Transform the size of the array we're allocating (if any).
John McCall60d7b3a2010-08-24 06:29:42 +00007099 ExprResult ArraySize = getDerived().TransformExpr(E->getArraySize());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007100 if (ArraySize.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007101 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007102
Douglas Gregorb98b1992009-08-11 05:31:07 +00007103 // Transform the placement arguments (if any).
7104 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00007105 ASTOwningVector<Expr*> PlacementArgs(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00007106 if (getDerived().TransformExprs(E->getPlacementArgs(),
7107 E->getNumPlacementArgs(), true,
7108 PlacementArgs, &ArgumentChanged))
Sebastian Redl1548d142012-02-16 11:35:52 +00007109 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007110
Sebastian Redl1548d142012-02-16 11:35:52 +00007111 // Transform the constructor arguments (if any).
7112 // As an annoying corner case, we may have introduced an implicit value-
7113 // initialization expression when allocating a new array, which we implicitly
7114 // drop. It will be re-created during type checking.
7115 ASTOwningVector<Expr*> ConstructorArgs(SemaRef);
7116 if (!(E->isArray() && E->getNumConstructorArgs() == 1 &&
7117 isa<ImplicitValueInitExpr>(E->getConstructorArgs()[0])) &&
7118 TransformExprs(E->getConstructorArgs(), E->getNumConstructorArgs(), true,
7119 ConstructorArgs, &ArgumentChanged))
7120 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007121
Sebastian Redl1548d142012-02-16 11:35:52 +00007122 // Transform constructor, new operator, and delete operator.
7123 CXXConstructorDecl *Constructor = 0;
7124 if (E->getConstructor()) {
7125 Constructor = cast_or_null<CXXConstructorDecl>(
7126 getDerived().TransformDecl(E->getLocStart(),
7127 E->getConstructor()));
7128 if (!Constructor)
7129 return ExprError();
7130 }
7131
Douglas Gregor1af74512010-02-26 00:38:10 +00007132 FunctionDecl *OperatorNew = 0;
7133 if (E->getOperatorNew()) {
7134 OperatorNew = cast_or_null<FunctionDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007135 getDerived().TransformDecl(E->getLocStart(),
7136 E->getOperatorNew()));
Douglas Gregor1af74512010-02-26 00:38:10 +00007137 if (!OperatorNew)
John McCallf312b1e2010-08-26 23:41:50 +00007138 return ExprError();
Douglas Gregor1af74512010-02-26 00:38:10 +00007139 }
7140
7141 FunctionDecl *OperatorDelete = 0;
7142 if (E->getOperatorDelete()) {
7143 OperatorDelete = cast_or_null<FunctionDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007144 getDerived().TransformDecl(E->getLocStart(),
7145 E->getOperatorDelete()));
Douglas Gregor1af74512010-02-26 00:38:10 +00007146 if (!OperatorDelete)
John McCallf312b1e2010-08-26 23:41:50 +00007147 return ExprError();
Douglas Gregor1af74512010-02-26 00:38:10 +00007148 }
Sean Huntc3021132010-05-05 15:23:54 +00007149
Douglas Gregorb98b1992009-08-11 05:31:07 +00007150 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor1bb2a932010-09-07 21:49:58 +00007151 AllocTypeInfo == E->getAllocatedTypeSourceInfo() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00007152 ArraySize.get() == E->getArraySize() &&
Sebastian Redl1548d142012-02-16 11:35:52 +00007153 Constructor == E->getConstructor() &&
Douglas Gregor1af74512010-02-26 00:38:10 +00007154 OperatorNew == E->getOperatorNew() &&
7155 OperatorDelete == E->getOperatorDelete() &&
7156 !ArgumentChanged) {
7157 // Mark any declarations we need as referenced.
7158 // FIXME: instantiation-specific.
Sebastian Redl1548d142012-02-16 11:35:52 +00007159 if (Constructor)
7160 SemaRef.MarkFunctionReferenced(E->getLocStart(), Constructor);
Douglas Gregor1af74512010-02-26 00:38:10 +00007161 if (OperatorNew)
Eli Friedman5f2987c2012-02-02 03:46:19 +00007162 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorNew);
Douglas Gregor1af74512010-02-26 00:38:10 +00007163 if (OperatorDelete)
Eli Friedman5f2987c2012-02-02 03:46:19 +00007164 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorDelete);
Douglas Gregor2ad63cf2011-07-26 15:11:03 +00007165
Sebastian Redl1548d142012-02-16 11:35:52 +00007166 if (E->isArray() && Constructor &&
7167 !E->getAllocatedType()->isDependentType()) {
Douglas Gregor2ad63cf2011-07-26 15:11:03 +00007168 QualType ElementType
7169 = SemaRef.Context.getBaseElementType(E->getAllocatedType());
7170 if (const RecordType *RecordT = ElementType->getAs<RecordType>()) {
7171 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordT->getDecl());
7172 if (CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(Record)) {
Eli Friedman5f2987c2012-02-02 03:46:19 +00007173 SemaRef.MarkFunctionReferenced(E->getLocStart(), Destructor);
Douglas Gregor2ad63cf2011-07-26 15:11:03 +00007174 }
7175 }
7176 }
Sebastian Redl1548d142012-02-16 11:35:52 +00007177
John McCall3fa5cae2010-10-26 07:05:15 +00007178 return SemaRef.Owned(E);
Douglas Gregor1af74512010-02-26 00:38:10 +00007179 }
Mike Stump1eb44332009-09-09 15:08:12 +00007180
Douglas Gregor1bb2a932010-09-07 21:49:58 +00007181 QualType AllocType = AllocTypeInfo->getType();
Douglas Gregor5b5ad842009-12-22 17:13:37 +00007182 if (!ArraySize.get()) {
7183 // If no array size was specified, but the new expression was
7184 // instantiated with an array type (e.g., "new T" where T is
7185 // instantiated with "int[4]"), extract the outer bound from the
7186 // array type as our array size. We do this with constant and
7187 // dependently-sized array types.
7188 const ArrayType *ArrayT = SemaRef.Context.getAsArrayType(AllocType);
7189 if (!ArrayT) {
7190 // Do nothing
7191 } else if (const ConstantArrayType *ConsArrayT
7192 = dyn_cast<ConstantArrayType>(ArrayT)) {
Sean Huntc3021132010-05-05 15:23:54 +00007193 ArraySize
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00007194 = SemaRef.Owned(IntegerLiteral::Create(SemaRef.Context,
7195 ConsArrayT->getSize(),
7196 SemaRef.Context.getSizeType(),
7197 /*FIXME:*/E->getLocStart()));
Douglas Gregor5b5ad842009-12-22 17:13:37 +00007198 AllocType = ConsArrayT->getElementType();
7199 } else if (const DependentSizedArrayType *DepArrayT
7200 = dyn_cast<DependentSizedArrayType>(ArrayT)) {
7201 if (DepArrayT->getSizeExpr()) {
John McCall3fa5cae2010-10-26 07:05:15 +00007202 ArraySize = SemaRef.Owned(DepArrayT->getSizeExpr());
Douglas Gregor5b5ad842009-12-22 17:13:37 +00007203 AllocType = DepArrayT->getElementType();
7204 }
7205 }
7206 }
Sebastian Redl1548d142012-02-16 11:35:52 +00007207
Douglas Gregorb98b1992009-08-11 05:31:07 +00007208 return getDerived().RebuildCXXNewExpr(E->getLocStart(),
7209 E->isGlobalNew(),
7210 /*FIXME:*/E->getLocStart(),
7211 move_arg(PlacementArgs),
7212 /*FIXME:*/E->getLocStart(),
Douglas Gregor4bd40312010-07-13 15:54:32 +00007213 E->getTypeIdParens(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007214 AllocType,
Douglas Gregor1bb2a932010-09-07 21:49:58 +00007215 AllocTypeInfo,
John McCall9ae2f072010-08-23 23:25:46 +00007216 ArraySize.get(),
Sebastian Redl1548d142012-02-16 11:35:52 +00007217 E->getConstructorLParen(),
7218 move_arg(ConstructorArgs),
7219 E->getConstructorRParen());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007220}
Mike Stump1eb44332009-09-09 15:08:12 +00007221
Douglas Gregorb98b1992009-08-11 05:31:07 +00007222template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007223ExprResult
John McCall454feb92009-12-08 09:21:05 +00007224TreeTransform<Derived>::TransformCXXDeleteExpr(CXXDeleteExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00007225 ExprResult Operand = getDerived().TransformExpr(E->getArgument());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007226 if (Operand.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007227 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007228
Douglas Gregor1af74512010-02-26 00:38:10 +00007229 // Transform the delete operator, if known.
7230 FunctionDecl *OperatorDelete = 0;
7231 if (E->getOperatorDelete()) {
7232 OperatorDelete = cast_or_null<FunctionDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007233 getDerived().TransformDecl(E->getLocStart(),
7234 E->getOperatorDelete()));
Douglas Gregor1af74512010-02-26 00:38:10 +00007235 if (!OperatorDelete)
John McCallf312b1e2010-08-26 23:41:50 +00007236 return ExprError();
Douglas Gregor1af74512010-02-26 00:38:10 +00007237 }
Sean Huntc3021132010-05-05 15:23:54 +00007238
Douglas Gregorb98b1992009-08-11 05:31:07 +00007239 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor1af74512010-02-26 00:38:10 +00007240 Operand.get() == E->getArgument() &&
7241 OperatorDelete == E->getOperatorDelete()) {
7242 // Mark any declarations we need as referenced.
7243 // FIXME: instantiation-specific.
7244 if (OperatorDelete)
Eli Friedman5f2987c2012-02-02 03:46:19 +00007245 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorDelete);
Douglas Gregor5833b0b2010-09-14 22:55:20 +00007246
7247 if (!E->getArgument()->isTypeDependent()) {
7248 QualType Destroyed = SemaRef.Context.getBaseElementType(
7249 E->getDestroyedType());
7250 if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) {
7251 CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl());
Eli Friedman5f2987c2012-02-02 03:46:19 +00007252 SemaRef.MarkFunctionReferenced(E->getLocStart(),
7253 SemaRef.LookupDestructor(Record));
Douglas Gregor5833b0b2010-09-14 22:55:20 +00007254 }
7255 }
7256
John McCall3fa5cae2010-10-26 07:05:15 +00007257 return SemaRef.Owned(E);
Douglas Gregor1af74512010-02-26 00:38:10 +00007258 }
Mike Stump1eb44332009-09-09 15:08:12 +00007259
Douglas Gregorb98b1992009-08-11 05:31:07 +00007260 return getDerived().RebuildCXXDeleteExpr(E->getLocStart(),
7261 E->isGlobalDelete(),
7262 E->isArrayForm(),
John McCall9ae2f072010-08-23 23:25:46 +00007263 Operand.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007264}
Mike Stump1eb44332009-09-09 15:08:12 +00007265
Douglas Gregorb98b1992009-08-11 05:31:07 +00007266template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007267ExprResult
Douglas Gregora71d8192009-09-04 17:36:40 +00007268TreeTransform<Derived>::TransformCXXPseudoDestructorExpr(
John McCall454feb92009-12-08 09:21:05 +00007269 CXXPseudoDestructorExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00007270 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregora71d8192009-09-04 17:36:40 +00007271 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007272 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007273
John McCallb3d87482010-08-24 05:47:05 +00007274 ParsedType ObjectTypePtr;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007275 bool MayBePseudoDestructor = false;
John McCall9ae2f072010-08-23 23:25:46 +00007276 Base = SemaRef.ActOnStartCXXMemberReference(0, Base.get(),
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007277 E->getOperatorLoc(),
7278 E->isArrow()? tok::arrow : tok::period,
7279 ObjectTypePtr,
7280 MayBePseudoDestructor);
7281 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007282 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007283
John McCallb3d87482010-08-24 05:47:05 +00007284 QualType ObjectType = ObjectTypePtr.get();
Douglas Gregorf3db29f2011-02-25 18:19:59 +00007285 NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc();
7286 if (QualifierLoc) {
7287 QualifierLoc
7288 = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc, ObjectType);
7289 if (!QualifierLoc)
John McCall43fed0d2010-11-12 08:19:04 +00007290 return ExprError();
7291 }
Douglas Gregorf3db29f2011-02-25 18:19:59 +00007292 CXXScopeSpec SS;
7293 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00007294
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007295 PseudoDestructorTypeStorage Destroyed;
7296 if (E->getDestroyedTypeInfo()) {
7297 TypeSourceInfo *DestroyedTypeInfo
John McCall43fed0d2010-11-12 08:19:04 +00007298 = getDerived().TransformTypeInObjectScope(E->getDestroyedTypeInfo(),
Douglas Gregorb71d8212011-03-02 18:32:08 +00007299 ObjectType, 0, SS);
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007300 if (!DestroyedTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00007301 return ExprError();
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007302 Destroyed = DestroyedTypeInfo;
Douglas Gregor6b18e742011-11-09 02:19:47 +00007303 } else if (!ObjectType.isNull() && ObjectType->isDependentType()) {
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007304 // We aren't likely to be able to resolve the identifier down to a type
7305 // now anyway, so just retain the identifier.
7306 Destroyed = PseudoDestructorTypeStorage(E->getDestroyedTypeIdentifier(),
7307 E->getDestroyedTypeLoc());
7308 } else {
7309 // Look for a destructor known with the given name.
John McCallb3d87482010-08-24 05:47:05 +00007310 ParsedType T = SemaRef.getDestructorName(E->getTildeLoc(),
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007311 *E->getDestroyedTypeIdentifier(),
7312 E->getDestroyedTypeLoc(),
7313 /*Scope=*/0,
7314 SS, ObjectTypePtr,
7315 false);
7316 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007317 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007318
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007319 Destroyed
7320 = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.GetTypeFromParser(T),
7321 E->getDestroyedTypeLoc());
7322 }
Douglas Gregor26d4ac92010-02-24 23:40:28 +00007323
Douglas Gregor26d4ac92010-02-24 23:40:28 +00007324 TypeSourceInfo *ScopeTypeInfo = 0;
7325 if (E->getScopeTypeInfo()) {
John McCall43fed0d2010-11-12 08:19:04 +00007326 ScopeTypeInfo = getDerived().TransformType(E->getScopeTypeInfo());
Douglas Gregor26d4ac92010-02-24 23:40:28 +00007327 if (!ScopeTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00007328 return ExprError();
Douglas Gregora71d8192009-09-04 17:36:40 +00007329 }
Sean Huntc3021132010-05-05 15:23:54 +00007330
John McCall9ae2f072010-08-23 23:25:46 +00007331 return getDerived().RebuildCXXPseudoDestructorExpr(Base.get(),
Douglas Gregora71d8192009-09-04 17:36:40 +00007332 E->getOperatorLoc(),
7333 E->isArrow(),
Douglas Gregorf3db29f2011-02-25 18:19:59 +00007334 SS,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00007335 ScopeTypeInfo,
7336 E->getColonColonLoc(),
Douglas Gregorfce46ee2010-02-24 23:50:37 +00007337 E->getTildeLoc(),
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007338 Destroyed);
Douglas Gregora71d8192009-09-04 17:36:40 +00007339}
Mike Stump1eb44332009-09-09 15:08:12 +00007340
Douglas Gregora71d8192009-09-04 17:36:40 +00007341template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007342ExprResult
John McCallba135432009-11-21 08:51:07 +00007343TreeTransform<Derived>::TransformUnresolvedLookupExpr(
John McCall454feb92009-12-08 09:21:05 +00007344 UnresolvedLookupExpr *Old) {
John McCallf7a1a742009-11-24 19:00:30 +00007345 LookupResult R(SemaRef, Old->getName(), Old->getNameLoc(),
7346 Sema::LookupOrdinaryName);
7347
7348 // Transform all the decls.
7349 for (UnresolvedLookupExpr::decls_iterator I = Old->decls_begin(),
7350 E = Old->decls_end(); I != E; ++I) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007351 NamedDecl *InstD = static_cast<NamedDecl*>(
7352 getDerived().TransformDecl(Old->getNameLoc(),
7353 *I));
John McCall9f54ad42009-12-10 09:41:52 +00007354 if (!InstD) {
7355 // Silently ignore these if a UsingShadowDecl instantiated to nothing.
7356 // This can happen because of dependent hiding.
7357 if (isa<UsingShadowDecl>(*I))
7358 continue;
7359 else
John McCallf312b1e2010-08-26 23:41:50 +00007360 return ExprError();
John McCall9f54ad42009-12-10 09:41:52 +00007361 }
John McCallf7a1a742009-11-24 19:00:30 +00007362
7363 // Expand using declarations.
7364 if (isa<UsingDecl>(InstD)) {
7365 UsingDecl *UD = cast<UsingDecl>(InstD);
7366 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
7367 E = UD->shadow_end(); I != E; ++I)
7368 R.addDecl(*I);
7369 continue;
7370 }
7371
7372 R.addDecl(InstD);
7373 }
7374
7375 // Resolve a kind, but don't do any further analysis. If it's
7376 // ambiguous, the callee needs to deal with it.
7377 R.resolveKind();
7378
7379 // Rebuild the nested-name qualifier, if present.
7380 CXXScopeSpec SS;
Douglas Gregor4c9be892011-02-28 20:01:57 +00007381 if (Old->getQualifierLoc()) {
7382 NestedNameSpecifierLoc QualifierLoc
7383 = getDerived().TransformNestedNameSpecifierLoc(Old->getQualifierLoc());
7384 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00007385 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007386
Douglas Gregor4c9be892011-02-28 20:01:57 +00007387 SS.Adopt(QualifierLoc);
Sean Huntc3021132010-05-05 15:23:54 +00007388 }
7389
Douglas Gregorc96be1e2010-04-27 18:19:34 +00007390 if (Old->getNamingClass()) {
Douglas Gregor66c45152010-04-27 16:10:10 +00007391 CXXRecordDecl *NamingClass
7392 = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
7393 Old->getNameLoc(),
7394 Old->getNamingClass()));
7395 if (!NamingClass)
John McCallf312b1e2010-08-26 23:41:50 +00007396 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007397
Douglas Gregor66c45152010-04-27 16:10:10 +00007398 R.setNamingClass(NamingClass);
John McCallf7a1a742009-11-24 19:00:30 +00007399 }
7400
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007401 SourceLocation TemplateKWLoc = Old->getTemplateKeywordLoc();
7402
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00007403 // If we have neither explicit template arguments, nor the template keyword,
7404 // it's a normal declaration name.
7405 if (!Old->hasExplicitTemplateArgs() && !TemplateKWLoc.isValid())
John McCallf7a1a742009-11-24 19:00:30 +00007406 return getDerived().RebuildDeclarationNameExpr(SS, R, Old->requiresADL());
7407
7408 // If we have template arguments, rebuild them, then rebuild the
7409 // templateid expression.
7410 TemplateArgumentListInfo TransArgs(Old->getLAngleLoc(), Old->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00007411 if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(),
7412 Old->getNumTemplateArgs(),
7413 TransArgs))
7414 return ExprError();
John McCallf7a1a742009-11-24 19:00:30 +00007415
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007416 return getDerived().RebuildTemplateIdExpr(SS, TemplateKWLoc, R,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00007417 Old->requiresADL(), &TransArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007418}
Mike Stump1eb44332009-09-09 15:08:12 +00007419
Douglas Gregorb98b1992009-08-11 05:31:07 +00007420template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007421ExprResult
John McCall454feb92009-12-08 09:21:05 +00007422TreeTransform<Derived>::TransformUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00007423 TypeSourceInfo *T = getDerived().TransformType(E->getQueriedTypeSourceInfo());
7424 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007425 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007426
Douglas Gregorb98b1992009-08-11 05:31:07 +00007427 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00007428 T == E->getQueriedTypeSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00007429 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007430
Mike Stump1eb44332009-09-09 15:08:12 +00007431 return getDerived().RebuildUnaryTypeTrait(E->getTrait(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007432 E->getLocStart(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007433 T,
7434 E->getLocEnd());
7435}
Mike Stump1eb44332009-09-09 15:08:12 +00007436
Douglas Gregorb98b1992009-08-11 05:31:07 +00007437template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007438ExprResult
Francois Pichet6ad6f282010-12-07 00:08:36 +00007439TreeTransform<Derived>::TransformBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
7440 TypeSourceInfo *LhsT = getDerived().TransformType(E->getLhsTypeSourceInfo());
7441 if (!LhsT)
7442 return ExprError();
7443
7444 TypeSourceInfo *RhsT = getDerived().TransformType(E->getRhsTypeSourceInfo());
7445 if (!RhsT)
7446 return ExprError();
7447
7448 if (!getDerived().AlwaysRebuild() &&
7449 LhsT == E->getLhsTypeSourceInfo() && RhsT == E->getRhsTypeSourceInfo())
7450 return SemaRef.Owned(E);
7451
7452 return getDerived().RebuildBinaryTypeTrait(E->getTrait(),
7453 E->getLocStart(),
7454 LhsT, RhsT,
7455 E->getLocEnd());
7456}
7457
7458template<typename Derived>
7459ExprResult
John Wiegley21ff2e52011-04-28 00:16:57 +00007460TreeTransform<Derived>::TransformArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
7461 TypeSourceInfo *T = getDerived().TransformType(E->getQueriedTypeSourceInfo());
7462 if (!T)
7463 return ExprError();
7464
7465 if (!getDerived().AlwaysRebuild() &&
7466 T == E->getQueriedTypeSourceInfo())
7467 return SemaRef.Owned(E);
7468
7469 ExprResult SubExpr;
7470 {
7471 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
7472 SubExpr = getDerived().TransformExpr(E->getDimensionExpression());
7473 if (SubExpr.isInvalid())
7474 return ExprError();
7475
7476 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getDimensionExpression())
7477 return SemaRef.Owned(E);
7478 }
7479
7480 return getDerived().RebuildArrayTypeTrait(E->getTrait(),
7481 E->getLocStart(),
7482 T,
7483 SubExpr.get(),
7484 E->getLocEnd());
7485}
7486
7487template<typename Derived>
7488ExprResult
John Wiegley55262202011-04-25 06:54:41 +00007489TreeTransform<Derived>::TransformExpressionTraitExpr(ExpressionTraitExpr *E) {
7490 ExprResult SubExpr;
7491 {
7492 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
7493 SubExpr = getDerived().TransformExpr(E->getQueriedExpression());
7494 if (SubExpr.isInvalid())
7495 return ExprError();
7496
7497 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getQueriedExpression())
7498 return SemaRef.Owned(E);
7499 }
7500
7501 return getDerived().RebuildExpressionTrait(
7502 E->getTrait(), E->getLocStart(), SubExpr.get(), E->getLocEnd());
7503}
7504
7505template<typename Derived>
7506ExprResult
John McCall865d4472009-11-19 22:55:06 +00007507TreeTransform<Derived>::TransformDependentScopeDeclRefExpr(
Abramo Bagnara25777432010-08-11 22:01:17 +00007508 DependentScopeDeclRefExpr *E) {
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00007509 NestedNameSpecifierLoc QualifierLoc
7510 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
7511 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00007512 return ExprError();
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007513 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00007514
John McCall43fed0d2010-11-12 08:19:04 +00007515 // TODO: If this is a conversion-function-id, verify that the
7516 // destination type name (if present) resolves the same way after
7517 // instantiation as it did in the local scope.
7518
Abramo Bagnara25777432010-08-11 22:01:17 +00007519 DeclarationNameInfo NameInfo
7520 = getDerived().TransformDeclarationNameInfo(E->getNameInfo());
7521 if (!NameInfo.getName())
John McCallf312b1e2010-08-26 23:41:50 +00007522 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007523
John McCallf7a1a742009-11-24 19:00:30 +00007524 if (!E->hasExplicitTemplateArgs()) {
7525 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00007526 QualifierLoc == E->getQualifierLoc() &&
Abramo Bagnara25777432010-08-11 22:01:17 +00007527 // Note: it is sufficient to compare the Name component of NameInfo:
7528 // if name has not changed, DNLoc has not changed either.
7529 NameInfo.getName() == E->getDeclName())
John McCall3fa5cae2010-10-26 07:05:15 +00007530 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007531
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00007532 return getDerived().RebuildDependentScopeDeclRefExpr(QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007533 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00007534 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00007535 /*TemplateArgs*/ 0);
Douglas Gregorf17bb742009-10-22 17:20:55 +00007536 }
John McCalld5532b62009-11-23 01:53:49 +00007537
7538 TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00007539 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
7540 E->getNumTemplateArgs(),
7541 TransArgs))
7542 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00007543
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00007544 return getDerived().RebuildDependentScopeDeclRefExpr(QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007545 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00007546 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00007547 &TransArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007548}
7549
7550template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007551ExprResult
John McCall454feb92009-12-08 09:21:05 +00007552TreeTransform<Derived>::TransformCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregor321725d2010-02-03 03:01:57 +00007553 // CXXConstructExprs are always implicit, so when we have a
7554 // 1-argument construction we just transform that argument.
7555 if (E->getNumArgs() == 1 ||
7556 (E->getNumArgs() > 1 && getDerived().DropCallArgument(E->getArg(1))))
7557 return getDerived().TransformExpr(E->getArg(0));
7558
Douglas Gregorb98b1992009-08-11 05:31:07 +00007559 TemporaryBase Rebase(*this, /*FIXME*/E->getLocStart(), DeclarationName());
7560
7561 QualType T = getDerived().TransformType(E->getType());
7562 if (T.isNull())
John McCallf312b1e2010-08-26 23:41:50 +00007563 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00007564
7565 CXXConstructorDecl *Constructor
7566 = cast_or_null<CXXConstructorDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007567 getDerived().TransformDecl(E->getLocStart(),
7568 E->getConstructor()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00007569 if (!Constructor)
John McCallf312b1e2010-08-26 23:41:50 +00007570 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007571
Douglas Gregorb98b1992009-08-11 05:31:07 +00007572 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00007573 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00007574 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
7575 &ArgumentChanged))
7576 return ExprError();
7577
Douglas Gregorb98b1992009-08-11 05:31:07 +00007578 if (!getDerived().AlwaysRebuild() &&
7579 T == E->getType() &&
7580 Constructor == E->getConstructor() &&
Douglas Gregorc845aad2010-02-26 00:01:57 +00007581 !ArgumentChanged) {
Douglas Gregor1af74512010-02-26 00:38:10 +00007582 // Mark the constructor as referenced.
7583 // FIXME: Instantiation-specific
Eli Friedman5f2987c2012-02-02 03:46:19 +00007584 SemaRef.MarkFunctionReferenced(E->getLocStart(), Constructor);
John McCall3fa5cae2010-10-26 07:05:15 +00007585 return SemaRef.Owned(E);
Douglas Gregorc845aad2010-02-26 00:01:57 +00007586 }
Mike Stump1eb44332009-09-09 15:08:12 +00007587
Douglas Gregor4411d2e2009-12-14 16:27:04 +00007588 return getDerived().RebuildCXXConstructExpr(T, /*FIXME:*/E->getLocStart(),
7589 Constructor, E->isElidable(),
Douglas Gregor8c3e5542010-08-22 17:20:18 +00007590 move_arg(Args),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00007591 E->hadMultipleCandidates(),
Douglas Gregor8c3e5542010-08-22 17:20:18 +00007592 E->requiresZeroInitialization(),
Chandler Carruth428edaf2010-10-25 08:47:36 +00007593 E->getConstructionKind(),
7594 E->getParenRange());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007595}
Mike Stump1eb44332009-09-09 15:08:12 +00007596
Douglas Gregorb98b1992009-08-11 05:31:07 +00007597/// \brief Transform a C++ temporary-binding expression.
7598///
Douglas Gregor51326552009-12-24 18:51:59 +00007599/// Since CXXBindTemporaryExpr nodes are implicitly generated, we just
7600/// transform the subexpression and return that.
Douglas Gregorb98b1992009-08-11 05:31:07 +00007601template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007602ExprResult
John McCall454feb92009-12-08 09:21:05 +00007603TreeTransform<Derived>::TransformCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
Douglas Gregor51326552009-12-24 18:51:59 +00007604 return getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007605}
Mike Stump1eb44332009-09-09 15:08:12 +00007606
John McCall4765fa02010-12-06 08:20:24 +00007607/// \brief Transform a C++ expression that contains cleanups that should
7608/// be run after the expression is evaluated.
Douglas Gregorb98b1992009-08-11 05:31:07 +00007609///
John McCall4765fa02010-12-06 08:20:24 +00007610/// Since ExprWithCleanups nodes are implicitly generated, we
Douglas Gregor51326552009-12-24 18:51:59 +00007611/// just transform the subexpression and return that.
Douglas Gregorb98b1992009-08-11 05:31:07 +00007612template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007613ExprResult
John McCall4765fa02010-12-06 08:20:24 +00007614TreeTransform<Derived>::TransformExprWithCleanups(ExprWithCleanups *E) {
Douglas Gregor51326552009-12-24 18:51:59 +00007615 return getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007616}
Mike Stump1eb44332009-09-09 15:08:12 +00007617
Douglas Gregorb98b1992009-08-11 05:31:07 +00007618template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007619ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00007620TreeTransform<Derived>::TransformCXXTemporaryObjectExpr(
Douglas Gregorab6677e2010-09-08 00:15:04 +00007621 CXXTemporaryObjectExpr *E) {
7622 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
7623 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007624 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007625
Douglas Gregorb98b1992009-08-11 05:31:07 +00007626 CXXConstructorDecl *Constructor
7627 = cast_or_null<CXXConstructorDecl>(
Sean Huntc3021132010-05-05 15:23:54 +00007628 getDerived().TransformDecl(E->getLocStart(),
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007629 E->getConstructor()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00007630 if (!Constructor)
John McCallf312b1e2010-08-26 23:41:50 +00007631 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007632
Douglas Gregorb98b1992009-08-11 05:31:07 +00007633 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00007634 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007635 Args.reserve(E->getNumArgs());
Douglas Gregoraa165f82011-01-03 19:04:46 +00007636 if (TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
7637 &ArgumentChanged))
7638 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007639
Douglas Gregorb98b1992009-08-11 05:31:07 +00007640 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorab6677e2010-09-08 00:15:04 +00007641 T == E->getTypeSourceInfo() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00007642 Constructor == E->getConstructor() &&
Douglas Gregor91be6f52010-03-02 17:18:33 +00007643 !ArgumentChanged) {
7644 // FIXME: Instantiation-specific
Eli Friedman5f2987c2012-02-02 03:46:19 +00007645 SemaRef.MarkFunctionReferenced(E->getLocStart(), Constructor);
John McCall3fa5cae2010-10-26 07:05:15 +00007646 return SemaRef.MaybeBindToTemporary(E);
Douglas Gregor91be6f52010-03-02 17:18:33 +00007647 }
Douglas Gregorab6677e2010-09-08 00:15:04 +00007648
7649 return getDerived().RebuildCXXTemporaryObjectExpr(T,
7650 /*FIXME:*/T->getTypeLoc().getEndLoc(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007651 move_arg(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007652 E->getLocEnd());
7653}
Mike Stump1eb44332009-09-09 15:08:12 +00007654
Douglas Gregorb98b1992009-08-11 05:31:07 +00007655template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007656ExprResult
Douglas Gregor01d08012012-02-07 10:09:13 +00007657TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
Douglas Gregordfca6f52012-02-13 22:00:16 +00007658 // Create the local class that will describe the lambda.
7659 CXXRecordDecl *Class
7660 = getSema().createLambdaClosureType(E->getIntroducerRange());
7661 getDerived().transformedLocalDecl(E->getLambdaClass(), Class);
7662
7663 // Transform the type of the lambda parameters and start the definition of
7664 // the lambda itself.
7665 TypeSourceInfo *MethodTy
7666 = TransformType(E->getCallOperator()->getTypeSourceInfo());
7667 if (!MethodTy)
7668 return ExprError();
7669
Douglas Gregorc6889e72012-02-14 22:28:59 +00007670 // Transform lambda parameters.
7671 bool Invalid = false;
7672 llvm::SmallVector<QualType, 4> ParamTypes;
7673 llvm::SmallVector<ParmVarDecl *, 4> Params;
7674 if (getDerived().TransformFunctionTypeParams(E->getLocStart(),
7675 E->getCallOperator()->param_begin(),
7676 E->getCallOperator()->param_size(),
7677 0, ParamTypes, &Params))
7678 Invalid = true;
7679
Douglas Gregordfca6f52012-02-13 22:00:16 +00007680 // Build the call operator.
7681 CXXMethodDecl *CallOperator
7682 = getSema().startLambdaDefinition(Class, E->getIntroducerRange(),
7683 MethodTy,
Douglas Gregorc6889e72012-02-14 22:28:59 +00007684 E->getCallOperator()->getLocEnd(),
7685 Params);
Douglas Gregordfca6f52012-02-13 22:00:16 +00007686 getDerived().transformAttrs(E->getCallOperator(), CallOperator);
7687
Douglas Gregord5387e82012-02-14 00:00:48 +00007688 // FIXME: Instantiation-specific.
7689 CallOperator->setInstantiationOfMemberFunction(E->getCallOperator(),
7690 TSK_ImplicitInstantiation);
7691
7692 // Introduce the context of the call operator.
7693 Sema::ContextRAII SavedContext(getSema(), CallOperator);
7694
Douglas Gregordfca6f52012-02-13 22:00:16 +00007695 // Enter the scope of the lambda.
7696 sema::LambdaScopeInfo *LSI
7697 = getSema().enterLambdaScope(CallOperator, E->getIntroducerRange(),
7698 E->getCaptureDefault(),
7699 E->hasExplicitParameters(),
7700 E->hasExplicitResultType(),
7701 E->isMutable());
7702
7703 // Transform captures.
Douglas Gregordfca6f52012-02-13 22:00:16 +00007704 bool FinishedExplicitCaptures = false;
7705 for (LambdaExpr::capture_iterator C = E->capture_begin(),
7706 CEnd = E->capture_end();
7707 C != CEnd; ++C) {
7708 // When we hit the first implicit capture, tell Sema that we've finished
7709 // the list of explicit captures.
7710 if (!FinishedExplicitCaptures && C->isImplicit()) {
7711 getSema().finishLambdaExplicitCaptures(LSI);
7712 FinishedExplicitCaptures = true;
7713 }
7714
7715 // Capturing 'this' is trivial.
7716 if (C->capturesThis()) {
7717 getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit());
7718 continue;
7719 }
7720
Douglas Gregora7365242012-02-14 19:27:52 +00007721 // Determine the capture kind for Sema.
7722 Sema::TryCaptureKind Kind
7723 = C->isImplicit()? Sema::TryCapture_Implicit
7724 : C->getCaptureKind() == LCK_ByCopy
7725 ? Sema::TryCapture_ExplicitByVal
7726 : Sema::TryCapture_ExplicitByRef;
7727 SourceLocation EllipsisLoc;
7728 if (C->isPackExpansion()) {
7729 UnexpandedParameterPack Unexpanded(C->getCapturedVar(), C->getLocation());
7730 bool ShouldExpand = false;
7731 bool RetainExpansion = false;
7732 llvm::Optional<unsigned> NumExpansions;
7733 if (getDerived().TryExpandParameterPacks(C->getEllipsisLoc(),
7734 C->getLocation(),
7735 Unexpanded,
7736 ShouldExpand, RetainExpansion,
7737 NumExpansions))
7738 return ExprError();
7739
7740 if (ShouldExpand) {
7741 // The transform has determined that we should perform an expansion;
7742 // transform and capture each of the arguments.
7743 // expansion of the pattern. Do so.
7744 VarDecl *Pack = C->getCapturedVar();
7745 for (unsigned I = 0; I != *NumExpansions; ++I) {
7746 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
7747 VarDecl *CapturedVar
7748 = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(),
7749 Pack));
7750 if (!CapturedVar) {
7751 Invalid = true;
7752 continue;
7753 }
7754
7755 // Capture the transformed variable.
7756 getSema().TryCaptureVar(CapturedVar, C->getLocation(), Kind);
7757 }
7758 continue;
7759 }
7760
7761 EllipsisLoc = C->getEllipsisLoc();
7762 }
7763
Douglas Gregordfca6f52012-02-13 22:00:16 +00007764 // Transform the captured variable.
7765 VarDecl *CapturedVar
7766 = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(),
7767 C->getCapturedVar()));
7768 if (!CapturedVar) {
7769 Invalid = true;
7770 continue;
7771 }
Douglas Gregora7365242012-02-14 19:27:52 +00007772
Douglas Gregordfca6f52012-02-13 22:00:16 +00007773 // Capture the transformed variable.
Douglas Gregora7365242012-02-14 19:27:52 +00007774 getSema().TryCaptureVar(CapturedVar, C->getLocation(), Kind);
Douglas Gregordfca6f52012-02-13 22:00:16 +00007775 }
7776 if (!FinishedExplicitCaptures)
7777 getSema().finishLambdaExplicitCaptures(LSI);
7778
Douglas Gregordfca6f52012-02-13 22:00:16 +00007779
7780 // Enter a new evaluation context to insulate the lambda from any
7781 // cleanups from the enclosing full-expression.
7782 getSema().PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
7783
7784 if (Invalid) {
7785 getSema().ActOnLambdaError(E->getLocStart(), /*CurScope=*/0,
7786 /*IsInstantiation=*/true);
7787 return ExprError();
7788 }
7789
7790 // Instantiate the body of the lambda expression.
Douglas Gregord5387e82012-02-14 00:00:48 +00007791 StmtResult Body = getDerived().TransformStmt(E->getBody());
7792 if (Body.isInvalid()) {
7793 getSema().ActOnLambdaError(E->getLocStart(), /*CurScope=*/0,
7794 /*IsInstantiation=*/true);
7795 return ExprError();
7796 }
Douglas Gregordfca6f52012-02-13 22:00:16 +00007797
7798 return getSema().ActOnLambdaExpr(E->getLocStart(), Body.take(),
7799 /*CurScope=*/0,
7800 /*IsInstantiation=*/true);
Douglas Gregor01d08012012-02-07 10:09:13 +00007801}
7802
7803template<typename Derived>
7804ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00007805TreeTransform<Derived>::TransformCXXUnresolvedConstructExpr(
John McCall454feb92009-12-08 09:21:05 +00007806 CXXUnresolvedConstructExpr *E) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00007807 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
7808 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007809 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007810
Douglas Gregorb98b1992009-08-11 05:31:07 +00007811 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00007812 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00007813 Args.reserve(E->arg_size());
7814 if (getDerived().TransformExprs(E->arg_begin(), E->arg_size(), true, Args,
7815 &ArgumentChanged))
7816 return ExprError();
7817
Douglas Gregorb98b1992009-08-11 05:31:07 +00007818 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorab6677e2010-09-08 00:15:04 +00007819 T == E->getTypeSourceInfo() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00007820 !ArgumentChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00007821 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007822
Douglas Gregorb98b1992009-08-11 05:31:07 +00007823 // FIXME: we're faking the locations of the commas
Douglas Gregorab6677e2010-09-08 00:15:04 +00007824 return getDerived().RebuildCXXUnresolvedConstructExpr(T,
Douglas Gregorb98b1992009-08-11 05:31:07 +00007825 E->getLParenLoc(),
7826 move_arg(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007827 E->getRParenLoc());
7828}
Mike Stump1eb44332009-09-09 15:08:12 +00007829
Douglas Gregorb98b1992009-08-11 05:31:07 +00007830template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007831ExprResult
John McCall865d4472009-11-19 22:55:06 +00007832TreeTransform<Derived>::TransformCXXDependentScopeMemberExpr(
Abramo Bagnara25777432010-08-11 22:01:17 +00007833 CXXDependentScopeMemberExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00007834 // Transform the base of the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00007835 ExprResult Base((Expr*) 0);
John McCallaa81e162009-12-01 22:10:20 +00007836 Expr *OldBase;
7837 QualType BaseType;
7838 QualType ObjectType;
7839 if (!E->isImplicitAccess()) {
7840 OldBase = E->getBase();
7841 Base = getDerived().TransformExpr(OldBase);
7842 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007843 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007844
John McCallaa81e162009-12-01 22:10:20 +00007845 // Start the member reference and compute the object's type.
John McCallb3d87482010-08-24 05:47:05 +00007846 ParsedType ObjectTy;
Douglas Gregord4dca082010-02-24 18:44:31 +00007847 bool MayBePseudoDestructor = false;
John McCall9ae2f072010-08-23 23:25:46 +00007848 Base = SemaRef.ActOnStartCXXMemberReference(0, Base.get(),
John McCallaa81e162009-12-01 22:10:20 +00007849 E->getOperatorLoc(),
Douglas Gregora38c6872009-09-03 16:14:30 +00007850 E->isArrow()? tok::arrow : tok::period,
Douglas Gregord4dca082010-02-24 18:44:31 +00007851 ObjectTy,
7852 MayBePseudoDestructor);
John McCallaa81e162009-12-01 22:10:20 +00007853 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007854 return ExprError();
John McCallaa81e162009-12-01 22:10:20 +00007855
John McCallb3d87482010-08-24 05:47:05 +00007856 ObjectType = ObjectTy.get();
John McCallaa81e162009-12-01 22:10:20 +00007857 BaseType = ((Expr*) Base.get())->getType();
7858 } else {
7859 OldBase = 0;
7860 BaseType = getDerived().TransformType(E->getBaseType());
7861 ObjectType = BaseType->getAs<PointerType>()->getPointeeType();
7862 }
Mike Stump1eb44332009-09-09 15:08:12 +00007863
Douglas Gregor6cd21982009-10-20 05:58:46 +00007864 // Transform the first part of the nested-name-specifier that qualifies
7865 // the member name.
Douglas Gregorc68afe22009-09-03 21:38:09 +00007866 NamedDecl *FirstQualifierInScope
Douglas Gregor6cd21982009-10-20 05:58:46 +00007867 = getDerived().TransformFirstQualifierInScope(
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007868 E->getFirstQualifierFoundInScope(),
7869 E->getQualifierLoc().getBeginLoc());
Mike Stump1eb44332009-09-09 15:08:12 +00007870
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007871 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregora38c6872009-09-03 16:14:30 +00007872 if (E->getQualifier()) {
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007873 QualifierLoc
7874 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc(),
7875 ObjectType,
7876 FirstQualifierInScope);
7877 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00007878 return ExprError();
Douglas Gregora38c6872009-09-03 16:14:30 +00007879 }
Mike Stump1eb44332009-09-09 15:08:12 +00007880
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007881 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
7882
John McCall43fed0d2010-11-12 08:19:04 +00007883 // TODO: If this is a conversion-function-id, verify that the
7884 // destination type name (if present) resolves the same way after
7885 // instantiation as it did in the local scope.
7886
Abramo Bagnara25777432010-08-11 22:01:17 +00007887 DeclarationNameInfo NameInfo
John McCall43fed0d2010-11-12 08:19:04 +00007888 = getDerived().TransformDeclarationNameInfo(E->getMemberNameInfo());
Abramo Bagnara25777432010-08-11 22:01:17 +00007889 if (!NameInfo.getName())
John McCallf312b1e2010-08-26 23:41:50 +00007890 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007891
John McCallaa81e162009-12-01 22:10:20 +00007892 if (!E->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007893 // This is a reference to a member without an explicitly-specified
7894 // template argument list. Optimize for this common case.
7895 if (!getDerived().AlwaysRebuild() &&
John McCallaa81e162009-12-01 22:10:20 +00007896 Base.get() == OldBase &&
7897 BaseType == E->getBaseType() &&
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007898 QualifierLoc == E->getQualifierLoc() &&
Abramo Bagnara25777432010-08-11 22:01:17 +00007899 NameInfo.getName() == E->getMember() &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007900 FirstQualifierInScope == E->getFirstQualifierFoundInScope())
John McCall3fa5cae2010-10-26 07:05:15 +00007901 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007902
John McCall9ae2f072010-08-23 23:25:46 +00007903 return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(),
John McCallaa81e162009-12-01 22:10:20 +00007904 BaseType,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007905 E->isArrow(),
7906 E->getOperatorLoc(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007907 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007908 TemplateKWLoc,
John McCall129e2df2009-11-30 22:42:35 +00007909 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00007910 NameInfo,
John McCall129e2df2009-11-30 22:42:35 +00007911 /*TemplateArgs*/ 0);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007912 }
7913
John McCalld5532b62009-11-23 01:53:49 +00007914 TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00007915 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
7916 E->getNumTemplateArgs(),
7917 TransArgs))
7918 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007919
John McCall9ae2f072010-08-23 23:25:46 +00007920 return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(),
John McCallaa81e162009-12-01 22:10:20 +00007921 BaseType,
Douglas Gregorb98b1992009-08-11 05:31:07 +00007922 E->isArrow(),
7923 E->getOperatorLoc(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007924 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007925 TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007926 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00007927 NameInfo,
John McCall129e2df2009-11-30 22:42:35 +00007928 &TransArgs);
7929}
7930
7931template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007932ExprResult
John McCall454feb92009-12-08 09:21:05 +00007933TreeTransform<Derived>::TransformUnresolvedMemberExpr(UnresolvedMemberExpr *Old) {
John McCall129e2df2009-11-30 22:42:35 +00007934 // Transform the base of the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00007935 ExprResult Base((Expr*) 0);
John McCallaa81e162009-12-01 22:10:20 +00007936 QualType BaseType;
7937 if (!Old->isImplicitAccess()) {
7938 Base = getDerived().TransformExpr(Old->getBase());
7939 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007940 return ExprError();
Richard Smith9138b4e2011-10-26 19:06:56 +00007941 Base = getSema().PerformMemberExprBaseConversion(Base.take(),
7942 Old->isArrow());
7943 if (Base.isInvalid())
7944 return ExprError();
7945 BaseType = Base.get()->getType();
John McCallaa81e162009-12-01 22:10:20 +00007946 } else {
7947 BaseType = getDerived().TransformType(Old->getBaseType());
7948 }
John McCall129e2df2009-11-30 22:42:35 +00007949
Douglas Gregor4c9be892011-02-28 20:01:57 +00007950 NestedNameSpecifierLoc QualifierLoc;
7951 if (Old->getQualifierLoc()) {
7952 QualifierLoc
7953 = getDerived().TransformNestedNameSpecifierLoc(Old->getQualifierLoc());
7954 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00007955 return ExprError();
John McCall129e2df2009-11-30 22:42:35 +00007956 }
7957
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007958 SourceLocation TemplateKWLoc = Old->getTemplateKeywordLoc();
7959
Abramo Bagnara25777432010-08-11 22:01:17 +00007960 LookupResult R(SemaRef, Old->getMemberNameInfo(),
John McCall129e2df2009-11-30 22:42:35 +00007961 Sema::LookupOrdinaryName);
7962
7963 // Transform all the decls.
7964 for (UnresolvedMemberExpr::decls_iterator I = Old->decls_begin(),
7965 E = Old->decls_end(); I != E; ++I) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007966 NamedDecl *InstD = static_cast<NamedDecl*>(
7967 getDerived().TransformDecl(Old->getMemberLoc(),
7968 *I));
John McCall9f54ad42009-12-10 09:41:52 +00007969 if (!InstD) {
7970 // Silently ignore these if a UsingShadowDecl instantiated to nothing.
7971 // This can happen because of dependent hiding.
7972 if (isa<UsingShadowDecl>(*I))
7973 continue;
Argyrios Kyrtzidis34f52d12011-04-22 01:18:40 +00007974 else {
7975 R.clear();
John McCallf312b1e2010-08-26 23:41:50 +00007976 return ExprError();
Argyrios Kyrtzidis34f52d12011-04-22 01:18:40 +00007977 }
John McCall9f54ad42009-12-10 09:41:52 +00007978 }
John McCall129e2df2009-11-30 22:42:35 +00007979
7980 // Expand using declarations.
7981 if (isa<UsingDecl>(InstD)) {
7982 UsingDecl *UD = cast<UsingDecl>(InstD);
7983 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
7984 E = UD->shadow_end(); I != E; ++I)
7985 R.addDecl(*I);
7986 continue;
7987 }
7988
7989 R.addDecl(InstD);
7990 }
7991
7992 R.resolveKind();
7993
Douglas Gregorc96be1e2010-04-27 18:19:34 +00007994 // Determine the naming class.
Chandler Carruth042d6f92010-05-19 01:37:01 +00007995 if (Old->getNamingClass()) {
Sean Huntc3021132010-05-05 15:23:54 +00007996 CXXRecordDecl *NamingClass
Douglas Gregorc96be1e2010-04-27 18:19:34 +00007997 = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
Douglas Gregor66c45152010-04-27 16:10:10 +00007998 Old->getMemberLoc(),
7999 Old->getNamingClass()));
8000 if (!NamingClass)
John McCallf312b1e2010-08-26 23:41:50 +00008001 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00008002
Douglas Gregor66c45152010-04-27 16:10:10 +00008003 R.setNamingClass(NamingClass);
Douglas Gregorc96be1e2010-04-27 18:19:34 +00008004 }
Sean Huntc3021132010-05-05 15:23:54 +00008005
John McCall129e2df2009-11-30 22:42:35 +00008006 TemplateArgumentListInfo TransArgs;
8007 if (Old->hasExplicitTemplateArgs()) {
8008 TransArgs.setLAngleLoc(Old->getLAngleLoc());
8009 TransArgs.setRAngleLoc(Old->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00008010 if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(),
8011 Old->getNumTemplateArgs(),
8012 TransArgs))
8013 return ExprError();
John McCall129e2df2009-11-30 22:42:35 +00008014 }
John McCallc2233c52010-01-15 08:34:02 +00008015
8016 // FIXME: to do this check properly, we will need to preserve the
8017 // first-qualifier-in-scope here, just in case we had a dependent
8018 // base (and therefore couldn't do the check) and a
8019 // nested-name-qualifier (and therefore could do the lookup).
8020 NamedDecl *FirstQualifierInScope = 0;
Sean Huntc3021132010-05-05 15:23:54 +00008021
John McCall9ae2f072010-08-23 23:25:46 +00008022 return getDerived().RebuildUnresolvedMemberExpr(Base.get(),
John McCallaa81e162009-12-01 22:10:20 +00008023 BaseType,
John McCall129e2df2009-11-30 22:42:35 +00008024 Old->getOperatorLoc(),
8025 Old->isArrow(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00008026 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008027 TemplateKWLoc,
John McCallc2233c52010-01-15 08:34:02 +00008028 FirstQualifierInScope,
John McCall129e2df2009-11-30 22:42:35 +00008029 R,
8030 (Old->hasExplicitTemplateArgs()
8031 ? &TransArgs : 0));
Douglas Gregorb98b1992009-08-11 05:31:07 +00008032}
8033
8034template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008035ExprResult
Sebastian Redl2e156222010-09-10 20:55:43 +00008036TreeTransform<Derived>::TransformCXXNoexceptExpr(CXXNoexceptExpr *E) {
Sean Hunteea06c62011-05-31 19:54:49 +00008037 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Sebastian Redl2e156222010-09-10 20:55:43 +00008038 ExprResult SubExpr = getDerived().TransformExpr(E->getOperand());
8039 if (SubExpr.isInvalid())
8040 return ExprError();
8041
8042 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getOperand())
John McCall3fa5cae2010-10-26 07:05:15 +00008043 return SemaRef.Owned(E);
Sebastian Redl2e156222010-09-10 20:55:43 +00008044
8045 return getDerived().RebuildCXXNoexceptExpr(E->getSourceRange(),SubExpr.get());
8046}
8047
8048template<typename Derived>
8049ExprResult
Douglas Gregorbe230c32011-01-03 17:17:50 +00008050TreeTransform<Derived>::TransformPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregor4f1d2822011-01-13 00:19:55 +00008051 ExprResult Pattern = getDerived().TransformExpr(E->getPattern());
8052 if (Pattern.isInvalid())
8053 return ExprError();
8054
8055 if (!getDerived().AlwaysRebuild() && Pattern.get() == E->getPattern())
8056 return SemaRef.Owned(E);
8057
Douglas Gregor67fd1252011-01-14 21:20:45 +00008058 return getDerived().RebuildPackExpansion(Pattern.get(), E->getEllipsisLoc(),
8059 E->getNumExpansions());
Douglas Gregorbe230c32011-01-03 17:17:50 +00008060}
Douglas Gregoree8aff02011-01-04 17:33:58 +00008061
8062template<typename Derived>
8063ExprResult
8064TreeTransform<Derived>::TransformSizeOfPackExpr(SizeOfPackExpr *E) {
8065 // If E is not value-dependent, then nothing will change when we transform it.
8066 // Note: This is an instantiation-centric view.
8067 if (!E->isValueDependent())
8068 return SemaRef.Owned(E);
8069
8070 // Note: None of the implementations of TryExpandParameterPacks can ever
8071 // produce a diagnostic when given only a single unexpanded parameter pack,
8072 // so
8073 UnexpandedParameterPack Unexpanded(E->getPack(), E->getPackLoc());
8074 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00008075 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00008076 llvm::Optional<unsigned> NumExpansions;
Douglas Gregoree8aff02011-01-04 17:33:58 +00008077 if (getDerived().TryExpandParameterPacks(E->getOperatorLoc(), E->getPackLoc(),
David Blaikiea71f9d02011-09-22 02:34:54 +00008078 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00008079 ShouldExpand, RetainExpansion,
8080 NumExpansions))
Douglas Gregoree8aff02011-01-04 17:33:58 +00008081 return ExprError();
Douglas Gregorbe230c32011-01-03 17:17:50 +00008082
Douglas Gregor089e8932011-10-10 18:59:29 +00008083 if (RetainExpansion)
Douglas Gregoree8aff02011-01-04 17:33:58 +00008084 return SemaRef.Owned(E);
Douglas Gregor089e8932011-10-10 18:59:29 +00008085
8086 NamedDecl *Pack = E->getPack();
8087 if (!ShouldExpand) {
8088 Pack = cast_or_null<NamedDecl>(getDerived().TransformDecl(E->getPackLoc(),
8089 Pack));
8090 if (!Pack)
8091 return ExprError();
8092 }
8093
Douglas Gregoree8aff02011-01-04 17:33:58 +00008094
8095 // We now know the length of the parameter pack, so build a new expression
8096 // that stores that length.
Douglas Gregor089e8932011-10-10 18:59:29 +00008097 return getDerived().RebuildSizeOfPackExpr(E->getOperatorLoc(), Pack,
Douglas Gregoree8aff02011-01-04 17:33:58 +00008098 E->getPackLoc(), E->getRParenLoc(),
Douglas Gregor089e8932011-10-10 18:59:29 +00008099 NumExpansions);
Douglas Gregoree8aff02011-01-04 17:33:58 +00008100}
8101
Douglas Gregorbe230c32011-01-03 17:17:50 +00008102template<typename Derived>
8103ExprResult
Douglas Gregorc7793c72011-01-15 01:15:58 +00008104TreeTransform<Derived>::TransformSubstNonTypeTemplateParmPackExpr(
8105 SubstNonTypeTemplateParmPackExpr *E) {
8106 // Default behavior is to do nothing with this transformation.
8107 return SemaRef.Owned(E);
8108}
8109
8110template<typename Derived>
8111ExprResult
John McCall91a57552011-07-15 05:09:51 +00008112TreeTransform<Derived>::TransformSubstNonTypeTemplateParmExpr(
8113 SubstNonTypeTemplateParmExpr *E) {
8114 // Default behavior is to do nothing with this transformation.
8115 return SemaRef.Owned(E);
8116}
8117
8118template<typename Derived>
8119ExprResult
Douglas Gregor03e80032011-06-21 17:03:29 +00008120TreeTransform<Derived>::TransformMaterializeTemporaryExpr(
8121 MaterializeTemporaryExpr *E) {
8122 return getDerived().TransformExpr(E->GetTemporaryExpr());
8123}
8124
8125template<typename Derived>
8126ExprResult
John McCall454feb92009-12-08 09:21:05 +00008127TreeTransform<Derived>::TransformObjCStringLiteral(ObjCStringLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00008128 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008129}
8130
Mike Stump1eb44332009-09-09 15:08:12 +00008131template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008132ExprResult
John McCall454feb92009-12-08 09:21:05 +00008133TreeTransform<Derived>::TransformObjCEncodeExpr(ObjCEncodeExpr *E) {
Douglas Gregor81d34662010-04-20 15:39:42 +00008134 TypeSourceInfo *EncodedTypeInfo
8135 = getDerived().TransformType(E->getEncodedTypeSourceInfo());
8136 if (!EncodedTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00008137 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00008138
Douglas Gregorb98b1992009-08-11 05:31:07 +00008139 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor81d34662010-04-20 15:39:42 +00008140 EncodedTypeInfo == E->getEncodedTypeSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00008141 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008142
8143 return getDerived().RebuildObjCEncodeExpr(E->getAtLoc(),
Douglas Gregor81d34662010-04-20 15:39:42 +00008144 EncodedTypeInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00008145 E->getRParenLoc());
8146}
Mike Stump1eb44332009-09-09 15:08:12 +00008147
Douglas Gregorb98b1992009-08-11 05:31:07 +00008148template<typename Derived>
John McCallf85e1932011-06-15 23:02:42 +00008149ExprResult TreeTransform<Derived>::
8150TransformObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
8151 ExprResult result = getDerived().TransformExpr(E->getSubExpr());
8152 if (result.isInvalid()) return ExprError();
8153 Expr *subExpr = result.take();
8154
8155 if (!getDerived().AlwaysRebuild() &&
8156 subExpr == E->getSubExpr())
8157 return SemaRef.Owned(E);
8158
8159 return SemaRef.Owned(new(SemaRef.Context)
8160 ObjCIndirectCopyRestoreExpr(subExpr, E->getType(), E->shouldCopy()));
8161}
8162
8163template<typename Derived>
8164ExprResult TreeTransform<Derived>::
8165TransformObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
8166 TypeSourceInfo *TSInfo
8167 = getDerived().TransformType(E->getTypeInfoAsWritten());
8168 if (!TSInfo)
8169 return ExprError();
8170
8171 ExprResult Result = getDerived().TransformExpr(E->getSubExpr());
8172 if (Result.isInvalid())
8173 return ExprError();
8174
8175 if (!getDerived().AlwaysRebuild() &&
8176 TSInfo == E->getTypeInfoAsWritten() &&
8177 Result.get() == E->getSubExpr())
8178 return SemaRef.Owned(E);
8179
8180 return SemaRef.BuildObjCBridgedCast(E->getLParenLoc(), E->getBridgeKind(),
8181 E->getBridgeKeywordLoc(), TSInfo,
8182 Result.get());
8183}
8184
8185template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008186ExprResult
John McCall454feb92009-12-08 09:21:05 +00008187TreeTransform<Derived>::TransformObjCMessageExpr(ObjCMessageExpr *E) {
Douglas Gregor92e986e2010-04-22 16:44:27 +00008188 // Transform arguments.
8189 bool ArgChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00008190 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00008191 Args.reserve(E->getNumArgs());
8192 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), false, Args,
8193 &ArgChanged))
8194 return ExprError();
8195
Douglas Gregor92e986e2010-04-22 16:44:27 +00008196 if (E->getReceiverKind() == ObjCMessageExpr::Class) {
8197 // Class message: transform the receiver type.
8198 TypeSourceInfo *ReceiverTypeInfo
8199 = getDerived().TransformType(E->getClassReceiverTypeInfo());
8200 if (!ReceiverTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00008201 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00008202
Douglas Gregor92e986e2010-04-22 16:44:27 +00008203 // If nothing changed, just retain the existing message send.
8204 if (!getDerived().AlwaysRebuild() &&
8205 ReceiverTypeInfo == E->getClassReceiverTypeInfo() && !ArgChanged)
Douglas Gregor92be2a52011-12-10 00:23:21 +00008206 return SemaRef.MaybeBindToTemporary(E);
Douglas Gregor92e986e2010-04-22 16:44:27 +00008207
8208 // Build a new class message send.
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00008209 SmallVector<SourceLocation, 16> SelLocs;
8210 E->getSelectorLocs(SelLocs);
Douglas Gregor92e986e2010-04-22 16:44:27 +00008211 return getDerived().RebuildObjCMessageExpr(ReceiverTypeInfo,
8212 E->getSelector(),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00008213 SelLocs,
Douglas Gregor92e986e2010-04-22 16:44:27 +00008214 E->getMethodDecl(),
8215 E->getLeftLoc(),
8216 move_arg(Args),
8217 E->getRightLoc());
8218 }
8219
8220 // Instance message: transform the receiver
8221 assert(E->getReceiverKind() == ObjCMessageExpr::Instance &&
8222 "Only class and instance messages may be instantiated");
John McCall60d7b3a2010-08-24 06:29:42 +00008223 ExprResult Receiver
Douglas Gregor92e986e2010-04-22 16:44:27 +00008224 = getDerived().TransformExpr(E->getInstanceReceiver());
8225 if (Receiver.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008226 return ExprError();
Douglas Gregor92e986e2010-04-22 16:44:27 +00008227
8228 // If nothing changed, just retain the existing message send.
8229 if (!getDerived().AlwaysRebuild() &&
8230 Receiver.get() == E->getInstanceReceiver() && !ArgChanged)
Douglas Gregor92be2a52011-12-10 00:23:21 +00008231 return SemaRef.MaybeBindToTemporary(E);
Sean Huntc3021132010-05-05 15:23:54 +00008232
Douglas Gregor92e986e2010-04-22 16:44:27 +00008233 // Build a new instance message send.
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00008234 SmallVector<SourceLocation, 16> SelLocs;
8235 E->getSelectorLocs(SelLocs);
John McCall9ae2f072010-08-23 23:25:46 +00008236 return getDerived().RebuildObjCMessageExpr(Receiver.get(),
Douglas Gregor92e986e2010-04-22 16:44:27 +00008237 E->getSelector(),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00008238 SelLocs,
Douglas Gregor92e986e2010-04-22 16:44:27 +00008239 E->getMethodDecl(),
8240 E->getLeftLoc(),
8241 move_arg(Args),
8242 E->getRightLoc());
Douglas Gregorb98b1992009-08-11 05:31:07 +00008243}
8244
Mike Stump1eb44332009-09-09 15:08:12 +00008245template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008246ExprResult
John McCall454feb92009-12-08 09:21:05 +00008247TreeTransform<Derived>::TransformObjCSelectorExpr(ObjCSelectorExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00008248 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008249}
8250
Mike Stump1eb44332009-09-09 15:08:12 +00008251template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008252ExprResult
John McCall454feb92009-12-08 09:21:05 +00008253TreeTransform<Derived>::TransformObjCProtocolExpr(ObjCProtocolExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00008254 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008255}
8256
Mike Stump1eb44332009-09-09 15:08:12 +00008257template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008258ExprResult
John McCall454feb92009-12-08 09:21:05 +00008259TreeTransform<Derived>::TransformObjCIvarRefExpr(ObjCIvarRefExpr *E) {
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008260 // Transform the base expression.
John McCall60d7b3a2010-08-24 06:29:42 +00008261 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008262 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008263 return ExprError();
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008264
8265 // We don't need to transform the ivar; it will never change.
Sean Huntc3021132010-05-05 15:23:54 +00008266
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008267 // If nothing changed, just retain the existing expression.
8268 if (!getDerived().AlwaysRebuild() &&
8269 Base.get() == E->getBase())
John McCall3fa5cae2010-10-26 07:05:15 +00008270 return SemaRef.Owned(E);
Sean Huntc3021132010-05-05 15:23:54 +00008271
John McCall9ae2f072010-08-23 23:25:46 +00008272 return getDerived().RebuildObjCIvarRefExpr(Base.get(), E->getDecl(),
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008273 E->getLocation(),
8274 E->isArrow(), E->isFreeIvar());
Douglas Gregorb98b1992009-08-11 05:31:07 +00008275}
8276
Mike Stump1eb44332009-09-09 15:08:12 +00008277template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008278ExprResult
John McCall454feb92009-12-08 09:21:05 +00008279TreeTransform<Derived>::TransformObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
John McCall12f78a62010-12-02 01:19:52 +00008280 // 'super' and types never change. Property never changes. Just
8281 // retain the existing expression.
8282 if (!E->isObjectReceiver())
John McCall3fa5cae2010-10-26 07:05:15 +00008283 return SemaRef.Owned(E);
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00008284
Douglas Gregore3303542010-04-26 20:47:02 +00008285 // Transform the base expression.
John McCall60d7b3a2010-08-24 06:29:42 +00008286 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregore3303542010-04-26 20:47:02 +00008287 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008288 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00008289
Douglas Gregore3303542010-04-26 20:47:02 +00008290 // We don't need to transform the property; it will never change.
Sean Huntc3021132010-05-05 15:23:54 +00008291
Douglas Gregore3303542010-04-26 20:47:02 +00008292 // If nothing changed, just retain the existing expression.
8293 if (!getDerived().AlwaysRebuild() &&
8294 Base.get() == E->getBase())
John McCall3fa5cae2010-10-26 07:05:15 +00008295 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008296
John McCall12f78a62010-12-02 01:19:52 +00008297 if (E->isExplicitProperty())
8298 return getDerived().RebuildObjCPropertyRefExpr(Base.get(),
8299 E->getExplicitProperty(),
8300 E->getLocation());
8301
8302 return getDerived().RebuildObjCPropertyRefExpr(Base.get(),
John McCall3c3b7f92011-10-25 17:37:35 +00008303 SemaRef.Context.PseudoObjectTy,
John McCall12f78a62010-12-02 01:19:52 +00008304 E->getImplicitPropertyGetter(),
8305 E->getImplicitPropertySetter(),
8306 E->getLocation());
Douglas Gregorb98b1992009-08-11 05:31:07 +00008307}
8308
Mike Stump1eb44332009-09-09 15:08:12 +00008309template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008310ExprResult
John McCall454feb92009-12-08 09:21:05 +00008311TreeTransform<Derived>::TransformObjCIsaExpr(ObjCIsaExpr *E) {
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008312 // Transform the base expression.
John McCall60d7b3a2010-08-24 06:29:42 +00008313 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008314 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008315 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00008316
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008317 // If nothing changed, just retain the existing expression.
8318 if (!getDerived().AlwaysRebuild() &&
8319 Base.get() == E->getBase())
John McCall3fa5cae2010-10-26 07:05:15 +00008320 return SemaRef.Owned(E);
Sean Huntc3021132010-05-05 15:23:54 +00008321
John McCall9ae2f072010-08-23 23:25:46 +00008322 return getDerived().RebuildObjCIsaExpr(Base.get(), E->getIsaMemberLoc(),
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008323 E->isArrow());
Douglas Gregorb98b1992009-08-11 05:31:07 +00008324}
8325
Mike Stump1eb44332009-09-09 15:08:12 +00008326template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008327ExprResult
John McCall454feb92009-12-08 09:21:05 +00008328TreeTransform<Derived>::TransformShuffleVectorExpr(ShuffleVectorExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00008329 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00008330 ASTOwningVector<Expr*> SubExprs(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00008331 SubExprs.reserve(E->getNumSubExprs());
8332 if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false,
8333 SubExprs, &ArgumentChanged))
8334 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00008335
Douglas Gregorb98b1992009-08-11 05:31:07 +00008336 if (!getDerived().AlwaysRebuild() &&
8337 !ArgumentChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00008338 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00008339
Douglas Gregorb98b1992009-08-11 05:31:07 +00008340 return getDerived().RebuildShuffleVectorExpr(E->getBuiltinLoc(),
8341 move_arg(SubExprs),
8342 E->getRParenLoc());
8343}
8344
Mike Stump1eb44332009-09-09 15:08:12 +00008345template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008346ExprResult
John McCall454feb92009-12-08 09:21:05 +00008347TreeTransform<Derived>::TransformBlockExpr(BlockExpr *E) {
John McCallc6ac9c32011-02-04 18:33:18 +00008348 BlockDecl *oldBlock = E->getBlockDecl();
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008349
John McCallc6ac9c32011-02-04 18:33:18 +00008350 SemaRef.ActOnBlockStart(E->getCaretLocation(), /*Scope=*/0);
8351 BlockScopeInfo *blockScope = SemaRef.getCurBlock();
8352
8353 blockScope->TheDecl->setIsVariadic(oldBlock->isVariadic());
Fariborz Jahanian05865202011-12-03 17:47:53 +00008354 blockScope->TheDecl->setBlockMissingReturnType(
8355 oldBlock->blockMissingReturnType());
Fariborz Jahanianff365592011-05-05 17:18:12 +00008356
Chris Lattner686775d2011-07-20 06:58:45 +00008357 SmallVector<ParmVarDecl*, 4> params;
8358 SmallVector<QualType, 4> paramTypes;
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008359
8360 // Parameter substitution.
John McCallc6ac9c32011-02-04 18:33:18 +00008361 if (getDerived().TransformFunctionTypeParams(E->getCaretLocation(),
8362 oldBlock->param_begin(),
8363 oldBlock->param_size(),
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008364 0, paramTypes, &params)) {
8365 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
Douglas Gregor92be2a52011-12-10 00:23:21 +00008366 return ExprError();
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008367 }
John McCallc6ac9c32011-02-04 18:33:18 +00008368
8369 const FunctionType *exprFunctionType = E->getFunctionType();
Eli Friedman84b007f2012-01-26 03:00:14 +00008370 QualType exprResultType =
8371 getDerived().TransformType(exprFunctionType->getResultType());
Douglas Gregora779d9c2011-01-19 21:32:01 +00008372
8373 // Don't allow returning a objc interface by value.
Eli Friedman84b007f2012-01-26 03:00:14 +00008374 if (exprResultType->isObjCObjectType()) {
John McCallc6ac9c32011-02-04 18:33:18 +00008375 getSema().Diag(E->getCaretLocation(),
Douglas Gregora779d9c2011-01-19 21:32:01 +00008376 diag::err_object_cannot_be_passed_returned_by_value)
Eli Friedman84b007f2012-01-26 03:00:14 +00008377 << 0 << exprResultType;
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008378 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
Douglas Gregora779d9c2011-01-19 21:32:01 +00008379 return ExprError();
8380 }
John McCall711c52b2011-01-05 12:14:39 +00008381
John McCallc6ac9c32011-02-04 18:33:18 +00008382 QualType functionType = getDerived().RebuildFunctionProtoType(
Eli Friedman84b007f2012-01-26 03:00:14 +00008383 exprResultType,
John McCallc6ac9c32011-02-04 18:33:18 +00008384 paramTypes.data(),
8385 paramTypes.size(),
8386 oldBlock->isVariadic(),
Richard Smitheefb3d52012-02-10 09:58:53 +00008387 false, 0, RQ_None,
John McCallc6ac9c32011-02-04 18:33:18 +00008388 exprFunctionType->getExtInfo());
8389 blockScope->FunctionType = functionType;
John McCall711c52b2011-01-05 12:14:39 +00008390
8391 // Set the parameters on the block decl.
John McCallc6ac9c32011-02-04 18:33:18 +00008392 if (!params.empty())
David Blaikie4278c652011-09-21 18:16:56 +00008393 blockScope->TheDecl->setParams(params);
Eli Friedman84b007f2012-01-26 03:00:14 +00008394
8395 if (!oldBlock->blockMissingReturnType()) {
8396 blockScope->HasImplicitReturnType = false;
8397 blockScope->ReturnType = exprResultType;
8398 }
Douglas Gregora779d9c2011-01-19 21:32:01 +00008399
John McCall711c52b2011-01-05 12:14:39 +00008400 // Transform the body
John McCallc6ac9c32011-02-04 18:33:18 +00008401 StmtResult body = getDerived().TransformStmt(E->getBody());
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008402 if (body.isInvalid()) {
8403 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
John McCall711c52b2011-01-05 12:14:39 +00008404 return ExprError();
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008405 }
John McCall711c52b2011-01-05 12:14:39 +00008406
John McCallc6ac9c32011-02-04 18:33:18 +00008407#ifndef NDEBUG
8408 // In builds with assertions, make sure that we captured everything we
8409 // captured before.
Douglas Gregorfc921372011-05-20 15:32:55 +00008410 if (!SemaRef.getDiagnostics().hasErrorOccurred()) {
8411 for (BlockDecl::capture_iterator i = oldBlock->capture_begin(),
8412 e = oldBlock->capture_end(); i != e; ++i) {
8413 VarDecl *oldCapture = i->getVariable();
John McCallc6ac9c32011-02-04 18:33:18 +00008414
Douglas Gregorfc921372011-05-20 15:32:55 +00008415 // Ignore parameter packs.
8416 if (isa<ParmVarDecl>(oldCapture) &&
8417 cast<ParmVarDecl>(oldCapture)->isParameterPack())
8418 continue;
John McCallc6ac9c32011-02-04 18:33:18 +00008419
Douglas Gregorfc921372011-05-20 15:32:55 +00008420 VarDecl *newCapture =
8421 cast<VarDecl>(getDerived().TransformDecl(E->getCaretLocation(),
8422 oldCapture));
8423 assert(blockScope->CaptureMap.count(newCapture));
8424 }
John McCallc6ac9c32011-02-04 18:33:18 +00008425 }
8426#endif
8427
8428 return SemaRef.ActOnBlockStmtExpr(E->getCaretLocation(), body.get(),
8429 /*Scope=*/0);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008430}
8431
Mike Stump1eb44332009-09-09 15:08:12 +00008432template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008433ExprResult
John McCall454feb92009-12-08 09:21:05 +00008434TreeTransform<Derived>::TransformBlockDeclRefExpr(BlockDeclRefExpr *E) {
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008435 ValueDecl *ND
8436 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getLocation(),
8437 E->getDecl()));
8438 if (!ND)
John McCallf312b1e2010-08-26 23:41:50 +00008439 return ExprError();
Abramo Bagnara25777432010-08-11 22:01:17 +00008440
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008441 if (!getDerived().AlwaysRebuild() &&
8442 ND == E->getDecl()) {
8443 // Mark it referenced in the new context regardless.
8444 // FIXME: this is a bit instantiation-specific.
Eli Friedman5f2987c2012-02-02 03:46:19 +00008445 SemaRef.MarkBlockDeclRefReferenced(E);
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008446
John McCall3fa5cae2010-10-26 07:05:15 +00008447 return SemaRef.Owned(E);
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008448 }
8449
Abramo Bagnara25777432010-08-11 22:01:17 +00008450 DeclarationNameInfo NameInfo(E->getDecl()->getDeclName(), E->getLocation());
Douglas Gregor40d96a62011-02-28 21:54:11 +00008451 return getDerived().RebuildDeclRefExpr(NestedNameSpecifierLoc(),
Abramo Bagnara25777432010-08-11 22:01:17 +00008452 ND, NameInfo, 0);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008453}
Mike Stump1eb44332009-09-09 15:08:12 +00008454
Tanya Lattner61eee0c2011-06-04 00:47:47 +00008455template<typename Derived>
8456ExprResult
8457TreeTransform<Derived>::TransformAsTypeExpr(AsTypeExpr *E) {
David Blaikieb219cfc2011-09-23 05:06:16 +00008458 llvm_unreachable("Cannot transform asType expressions yet");
Tanya Lattner61eee0c2011-06-04 00:47:47 +00008459}
Eli Friedman276b0612011-10-11 02:20:01 +00008460
8461template<typename Derived>
8462ExprResult
8463TreeTransform<Derived>::TransformAtomicExpr(AtomicExpr *E) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00008464 QualType RetTy = getDerived().TransformType(E->getType());
8465 bool ArgumentChanged = false;
8466 ASTOwningVector<Expr*> SubExprs(SemaRef);
8467 SubExprs.reserve(E->getNumSubExprs());
8468 if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false,
8469 SubExprs, &ArgumentChanged))
8470 return ExprError();
8471
8472 if (!getDerived().AlwaysRebuild() &&
8473 !ArgumentChanged)
8474 return SemaRef.Owned(E);
8475
8476 return getDerived().RebuildAtomicExpr(E->getBuiltinLoc(), move_arg(SubExprs),
8477 RetTy, E->getOp(), E->getRParenLoc());
Eli Friedman276b0612011-10-11 02:20:01 +00008478}
Tanya Lattner61eee0c2011-06-04 00:47:47 +00008479
Douglas Gregorb98b1992009-08-11 05:31:07 +00008480//===----------------------------------------------------------------------===//
Douglas Gregor577f75a2009-08-04 16:50:30 +00008481// Type reconstruction
8482//===----------------------------------------------------------------------===//
8483
Mike Stump1eb44332009-09-09 15:08:12 +00008484template<typename Derived>
John McCall85737a72009-10-30 00:06:24 +00008485QualType TreeTransform<Derived>::RebuildPointerType(QualType PointeeType,
8486 SourceLocation Star) {
John McCall28654742010-06-05 06:41:15 +00008487 return SemaRef.BuildPointerType(PointeeType, Star,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008488 getDerived().getBaseEntity());
8489}
8490
Mike Stump1eb44332009-09-09 15:08:12 +00008491template<typename Derived>
John McCall85737a72009-10-30 00:06:24 +00008492QualType TreeTransform<Derived>::RebuildBlockPointerType(QualType PointeeType,
8493 SourceLocation Star) {
John McCall28654742010-06-05 06:41:15 +00008494 return SemaRef.BuildBlockPointerType(PointeeType, Star,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008495 getDerived().getBaseEntity());
8496}
8497
Mike Stump1eb44332009-09-09 15:08:12 +00008498template<typename Derived>
8499QualType
John McCall85737a72009-10-30 00:06:24 +00008500TreeTransform<Derived>::RebuildReferenceType(QualType ReferentType,
8501 bool WrittenAsLValue,
8502 SourceLocation Sigil) {
John McCall28654742010-06-05 06:41:15 +00008503 return SemaRef.BuildReferenceType(ReferentType, WrittenAsLValue,
John McCall85737a72009-10-30 00:06:24 +00008504 Sigil, getDerived().getBaseEntity());
Douglas Gregor577f75a2009-08-04 16:50:30 +00008505}
8506
8507template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008508QualType
John McCall85737a72009-10-30 00:06:24 +00008509TreeTransform<Derived>::RebuildMemberPointerType(QualType PointeeType,
8510 QualType ClassType,
8511 SourceLocation Sigil) {
John McCall28654742010-06-05 06:41:15 +00008512 return SemaRef.BuildMemberPointerType(PointeeType, ClassType,
John McCall85737a72009-10-30 00:06:24 +00008513 Sigil, getDerived().getBaseEntity());
Douglas Gregor577f75a2009-08-04 16:50:30 +00008514}
8515
8516template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008517QualType
Douglas Gregor577f75a2009-08-04 16:50:30 +00008518TreeTransform<Derived>::RebuildArrayType(QualType ElementType,
8519 ArrayType::ArraySizeModifier SizeMod,
8520 const llvm::APInt *Size,
8521 Expr *SizeExpr,
8522 unsigned IndexTypeQuals,
8523 SourceRange BracketsRange) {
8524 if (SizeExpr || !Size)
8525 return SemaRef.BuildArrayType(ElementType, SizeMod, SizeExpr,
8526 IndexTypeQuals, BracketsRange,
8527 getDerived().getBaseEntity());
Mike Stump1eb44332009-09-09 15:08:12 +00008528
8529 QualType Types[] = {
8530 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
8531 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
8532 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
Douglas Gregor577f75a2009-08-04 16:50:30 +00008533 };
8534 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
8535 QualType SizeType;
8536 for (unsigned I = 0; I != NumTypes; ++I)
8537 if (Size->getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
8538 SizeType = Types[I];
8539 break;
8540 }
Mike Stump1eb44332009-09-09 15:08:12 +00008541
Eli Friedman01f276d2012-01-25 23:20:27 +00008542 // Note that we can return a VariableArrayType here in the case where
8543 // the element type was a dependent VariableArrayType.
8544 IntegerLiteral *ArraySize
8545 = IntegerLiteral::Create(SemaRef.Context, *Size, SizeType,
8546 /*FIXME*/BracketsRange.getBegin());
8547 return SemaRef.BuildArrayType(ElementType, SizeMod, ArraySize,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008548 IndexTypeQuals, BracketsRange,
Mike Stump1eb44332009-09-09 15:08:12 +00008549 getDerived().getBaseEntity());
Douglas Gregor577f75a2009-08-04 16:50:30 +00008550}
Mike Stump1eb44332009-09-09 15:08:12 +00008551
Douglas Gregor577f75a2009-08-04 16:50:30 +00008552template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008553QualType
8554TreeTransform<Derived>::RebuildConstantArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008555 ArrayType::ArraySizeModifier SizeMod,
8556 const llvm::APInt &Size,
John McCall85737a72009-10-30 00:06:24 +00008557 unsigned IndexTypeQuals,
8558 SourceRange BracketsRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00008559 return getDerived().RebuildArrayType(ElementType, SizeMod, &Size, 0,
John McCall85737a72009-10-30 00:06:24 +00008560 IndexTypeQuals, BracketsRange);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008561}
8562
8563template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008564QualType
Mike Stump1eb44332009-09-09 15:08:12 +00008565TreeTransform<Derived>::RebuildIncompleteArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008566 ArrayType::ArraySizeModifier SizeMod,
John McCall85737a72009-10-30 00:06:24 +00008567 unsigned IndexTypeQuals,
8568 SourceRange BracketsRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00008569 return getDerived().RebuildArrayType(ElementType, SizeMod, 0, 0,
John McCall85737a72009-10-30 00:06:24 +00008570 IndexTypeQuals, BracketsRange);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008571}
Mike Stump1eb44332009-09-09 15:08:12 +00008572
Douglas Gregor577f75a2009-08-04 16:50:30 +00008573template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008574QualType
8575TreeTransform<Derived>::RebuildVariableArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008576 ArrayType::ArraySizeModifier SizeMod,
John McCall9ae2f072010-08-23 23:25:46 +00008577 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008578 unsigned IndexTypeQuals,
8579 SourceRange BracketsRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00008580 return getDerived().RebuildArrayType(ElementType, SizeMod, 0,
John McCall9ae2f072010-08-23 23:25:46 +00008581 SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008582 IndexTypeQuals, BracketsRange);
8583}
8584
8585template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008586QualType
8587TreeTransform<Derived>::RebuildDependentSizedArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008588 ArrayType::ArraySizeModifier SizeMod,
John McCall9ae2f072010-08-23 23:25:46 +00008589 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008590 unsigned IndexTypeQuals,
8591 SourceRange BracketsRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00008592 return getDerived().RebuildArrayType(ElementType, SizeMod, 0,
John McCall9ae2f072010-08-23 23:25:46 +00008593 SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008594 IndexTypeQuals, BracketsRange);
8595}
8596
8597template<typename Derived>
8598QualType TreeTransform<Derived>::RebuildVectorType(QualType ElementType,
Bob Wilsone86d78c2010-11-10 21:56:12 +00008599 unsigned NumElements,
8600 VectorType::VectorKind VecKind) {
Douglas Gregor577f75a2009-08-04 16:50:30 +00008601 // FIXME: semantic checking!
Bob Wilsone86d78c2010-11-10 21:56:12 +00008602 return SemaRef.Context.getVectorType(ElementType, NumElements, VecKind);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008603}
Mike Stump1eb44332009-09-09 15:08:12 +00008604
Douglas Gregor577f75a2009-08-04 16:50:30 +00008605template<typename Derived>
8606QualType TreeTransform<Derived>::RebuildExtVectorType(QualType ElementType,
8607 unsigned NumElements,
8608 SourceLocation AttributeLoc) {
8609 llvm::APInt numElements(SemaRef.Context.getIntWidth(SemaRef.Context.IntTy),
8610 NumElements, true);
8611 IntegerLiteral *VectorSize
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00008612 = IntegerLiteral::Create(SemaRef.Context, numElements, SemaRef.Context.IntTy,
8613 AttributeLoc);
John McCall9ae2f072010-08-23 23:25:46 +00008614 return SemaRef.BuildExtVectorType(ElementType, VectorSize, AttributeLoc);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008615}
Mike Stump1eb44332009-09-09 15:08:12 +00008616
Douglas Gregor577f75a2009-08-04 16:50:30 +00008617template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008618QualType
8619TreeTransform<Derived>::RebuildDependentSizedExtVectorType(QualType ElementType,
John McCall9ae2f072010-08-23 23:25:46 +00008620 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008621 SourceLocation AttributeLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00008622 return SemaRef.BuildExtVectorType(ElementType, SizeExpr, AttributeLoc);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008623}
Mike Stump1eb44332009-09-09 15:08:12 +00008624
Douglas Gregor577f75a2009-08-04 16:50:30 +00008625template<typename Derived>
8626QualType TreeTransform<Derived>::RebuildFunctionProtoType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00008627 QualType *ParamTypes,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008628 unsigned NumParamTypes,
Mike Stump1eb44332009-09-09 15:08:12 +00008629 bool Variadic,
Richard Smitheefb3d52012-02-10 09:58:53 +00008630 bool HasTrailingReturn,
Eli Friedmanfa869542010-08-05 02:54:05 +00008631 unsigned Quals,
Douglas Gregorc938c162011-01-26 05:01:58 +00008632 RefQualifierKind RefQualifier,
Eli Friedmanfa869542010-08-05 02:54:05 +00008633 const FunctionType::ExtInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00008634 return SemaRef.BuildFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Richard Smitheefb3d52012-02-10 09:58:53 +00008635 HasTrailingReturn, Quals, RefQualifier,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008636 getDerived().getBaseLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00008637 getDerived().getBaseEntity(),
8638 Info);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008639}
Mike Stump1eb44332009-09-09 15:08:12 +00008640
Douglas Gregor577f75a2009-08-04 16:50:30 +00008641template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00008642QualType TreeTransform<Derived>::RebuildFunctionNoProtoType(QualType T) {
8643 return SemaRef.Context.getFunctionNoProtoType(T);
8644}
8645
8646template<typename Derived>
John McCalled976492009-12-04 22:46:56 +00008647QualType TreeTransform<Derived>::RebuildUnresolvedUsingType(Decl *D) {
8648 assert(D && "no decl found");
8649 if (D->isInvalidDecl()) return QualType();
8650
Douglas Gregor92e986e2010-04-22 16:44:27 +00008651 // FIXME: Doesn't account for ObjCInterfaceDecl!
John McCalled976492009-12-04 22:46:56 +00008652 TypeDecl *Ty;
8653 if (isa<UsingDecl>(D)) {
8654 UsingDecl *Using = cast<UsingDecl>(D);
8655 assert(Using->isTypeName() &&
8656 "UnresolvedUsingTypenameDecl transformed to non-typename using");
8657
8658 // A valid resolved using typename decl points to exactly one type decl.
8659 assert(++Using->shadow_begin() == Using->shadow_end());
8660 Ty = cast<TypeDecl>((*Using->shadow_begin())->getTargetDecl());
Sean Huntc3021132010-05-05 15:23:54 +00008661
John McCalled976492009-12-04 22:46:56 +00008662 } else {
8663 assert(isa<UnresolvedUsingTypenameDecl>(D) &&
8664 "UnresolvedUsingTypenameDecl transformed to non-using decl");
8665 Ty = cast<UnresolvedUsingTypenameDecl>(D);
8666 }
8667
8668 return SemaRef.Context.getTypeDeclType(Ty);
8669}
8670
8671template<typename Derived>
John McCall2a984ca2010-10-12 00:20:44 +00008672QualType TreeTransform<Derived>::RebuildTypeOfExprType(Expr *E,
8673 SourceLocation Loc) {
8674 return SemaRef.BuildTypeofExprType(E, Loc);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008675}
8676
8677template<typename Derived>
8678QualType TreeTransform<Derived>::RebuildTypeOfType(QualType Underlying) {
8679 return SemaRef.Context.getTypeOfType(Underlying);
8680}
8681
8682template<typename Derived>
John McCall2a984ca2010-10-12 00:20:44 +00008683QualType TreeTransform<Derived>::RebuildDecltypeType(Expr *E,
8684 SourceLocation Loc) {
8685 return SemaRef.BuildDecltypeType(E, Loc);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008686}
8687
8688template<typename Derived>
Sean Huntca63c202011-05-24 22:41:36 +00008689QualType TreeTransform<Derived>::RebuildUnaryTransformType(QualType BaseType,
8690 UnaryTransformType::UTTKind UKind,
8691 SourceLocation Loc) {
8692 return SemaRef.BuildUnaryTransformType(BaseType, UKind, Loc);
8693}
8694
8695template<typename Derived>
Douglas Gregor577f75a2009-08-04 16:50:30 +00008696QualType TreeTransform<Derived>::RebuildTemplateSpecializationType(
John McCall833ca992009-10-29 08:12:44 +00008697 TemplateName Template,
8698 SourceLocation TemplateNameLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00008699 TemplateArgumentListInfo &TemplateArgs) {
John McCalld5532b62009-11-23 01:53:49 +00008700 return SemaRef.CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008701}
Mike Stump1eb44332009-09-09 15:08:12 +00008702
Douglas Gregordcee1a12009-08-06 05:28:30 +00008703template<typename Derived>
Eli Friedmanb001de72011-10-06 23:00:33 +00008704QualType TreeTransform<Derived>::RebuildAtomicType(QualType ValueType,
8705 SourceLocation KWLoc) {
8706 return SemaRef.BuildAtomicType(ValueType, KWLoc);
8707}
8708
8709template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008710TemplateName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008711TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
Douglas Gregord1067e52009-08-06 06:41:21 +00008712 bool TemplateKW,
8713 TemplateDecl *Template) {
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008714 return SemaRef.Context.getQualifiedTemplateName(SS.getScopeRep(), TemplateKW,
Douglas Gregord1067e52009-08-06 06:41:21 +00008715 Template);
8716}
8717
8718template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008719TemplateName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008720TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
8721 const IdentifierInfo &Name,
8722 SourceLocation NameLoc,
John McCall43fed0d2010-11-12 08:19:04 +00008723 QualType ObjectType,
8724 NamedDecl *FirstQualifierInScope) {
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008725 UnqualifiedId TemplateName;
8726 TemplateName.setIdentifier(&Name, NameLoc);
Douglas Gregord6ab2322010-06-16 23:00:59 +00008727 Sema::TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008728 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
Douglas Gregord6ab2322010-06-16 23:00:59 +00008729 getSema().ActOnDependentTemplateName(/*Scope=*/0,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008730 SS, TemplateKWLoc, TemplateName,
John McCallb3d87482010-08-24 05:47:05 +00008731 ParsedType::make(ObjectType),
Douglas Gregord6ab2322010-06-16 23:00:59 +00008732 /*EnteringContext=*/false,
8733 Template);
John McCall43fed0d2010-11-12 08:19:04 +00008734 return Template.get();
Douglas Gregord1067e52009-08-06 06:41:21 +00008735}
Mike Stump1eb44332009-09-09 15:08:12 +00008736
Douglas Gregorb98b1992009-08-11 05:31:07 +00008737template<typename Derived>
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008738TemplateName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008739TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008740 OverloadedOperatorKind Operator,
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008741 SourceLocation NameLoc,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008742 QualType ObjectType) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008743 UnqualifiedId Name;
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008744 // FIXME: Bogus location information.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008745 SourceLocation SymbolLocations[3] = { NameLoc, NameLoc, NameLoc };
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008746 Name.setOperatorFunctionId(NameLoc, Operator, SymbolLocations);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008747 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
Douglas Gregord6ab2322010-06-16 23:00:59 +00008748 Sema::TemplateTy Template;
8749 getSema().ActOnDependentTemplateName(/*Scope=*/0,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008750 SS, TemplateKWLoc, Name,
John McCallb3d87482010-08-24 05:47:05 +00008751 ParsedType::make(ObjectType),
Douglas Gregord6ab2322010-06-16 23:00:59 +00008752 /*EnteringContext=*/false,
8753 Template);
8754 return Template.template getAsVal<TemplateName>();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008755}
Sean Huntc3021132010-05-05 15:23:54 +00008756
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008757template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008758ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00008759TreeTransform<Derived>::RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
8760 SourceLocation OpLoc,
John McCall9ae2f072010-08-23 23:25:46 +00008761 Expr *OrigCallee,
8762 Expr *First,
8763 Expr *Second) {
8764 Expr *Callee = OrigCallee->IgnoreParenCasts();
8765 bool isPostIncDec = Second && (Op == OO_PlusPlus || Op == OO_MinusMinus);
Mike Stump1eb44332009-09-09 15:08:12 +00008766
Douglas Gregorb98b1992009-08-11 05:31:07 +00008767 // Determine whether this should be a builtin operation.
Sebastian Redlf322ed62009-10-29 20:17:01 +00008768 if (Op == OO_Subscript) {
John McCall9ae2f072010-08-23 23:25:46 +00008769 if (!First->getType()->isOverloadableType() &&
8770 !Second->getType()->isOverloadableType())
8771 return getSema().CreateBuiltinArraySubscriptExpr(First,
8772 Callee->getLocStart(),
8773 Second, OpLoc);
Eli Friedman1a3c75f2009-11-16 19:13:03 +00008774 } else if (Op == OO_Arrow) {
8775 // -> is never a builtin operation.
John McCall9ae2f072010-08-23 23:25:46 +00008776 return SemaRef.BuildOverloadedArrowExpr(0, First, OpLoc);
8777 } else if (Second == 0 || isPostIncDec) {
8778 if (!First->getType()->isOverloadableType()) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00008779 // The argument is not of overloadable type, so try to create a
8780 // built-in unary operation.
John McCall2de56d12010-08-25 11:45:40 +00008781 UnaryOperatorKind Opc
Douglas Gregorb98b1992009-08-11 05:31:07 +00008782 = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec);
Mike Stump1eb44332009-09-09 15:08:12 +00008783
John McCall9ae2f072010-08-23 23:25:46 +00008784 return getSema().CreateBuiltinUnaryOp(OpLoc, Opc, First);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008785 }
8786 } else {
John McCall9ae2f072010-08-23 23:25:46 +00008787 if (!First->getType()->isOverloadableType() &&
8788 !Second->getType()->isOverloadableType()) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00008789 // Neither of the arguments is an overloadable type, so try to
8790 // create a built-in binary operation.
John McCall2de56d12010-08-25 11:45:40 +00008791 BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op);
John McCall60d7b3a2010-08-24 06:29:42 +00008792 ExprResult Result
John McCall9ae2f072010-08-23 23:25:46 +00008793 = SemaRef.CreateBuiltinBinOp(OpLoc, Opc, First, Second);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008794 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008795 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00008796
Douglas Gregorb98b1992009-08-11 05:31:07 +00008797 return move(Result);
8798 }
8799 }
Mike Stump1eb44332009-09-09 15:08:12 +00008800
8801 // Compute the transformed set of functions (and function templates) to be
Douglas Gregorb98b1992009-08-11 05:31:07 +00008802 // used during overload resolution.
John McCall6e266892010-01-26 03:27:55 +00008803 UnresolvedSet<16> Functions;
Mike Stump1eb44332009-09-09 15:08:12 +00008804
John McCall9ae2f072010-08-23 23:25:46 +00008805 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Callee)) {
John McCallba135432009-11-21 08:51:07 +00008806 assert(ULE->requiresADL());
8807
8808 // FIXME: Do we have to check
8809 // IsAcceptableNonMemberOperatorCandidate for each of these?
John McCall6e266892010-01-26 03:27:55 +00008810 Functions.append(ULE->decls_begin(), ULE->decls_end());
John McCallba135432009-11-21 08:51:07 +00008811 } else {
John McCall9ae2f072010-08-23 23:25:46 +00008812 Functions.addDecl(cast<DeclRefExpr>(Callee)->getDecl());
John McCallba135432009-11-21 08:51:07 +00008813 }
Mike Stump1eb44332009-09-09 15:08:12 +00008814
Douglas Gregorb98b1992009-08-11 05:31:07 +00008815 // Add any functions found via argument-dependent lookup.
John McCall9ae2f072010-08-23 23:25:46 +00008816 Expr *Args[2] = { First, Second };
8817 unsigned NumArgs = 1 + (Second != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00008818
Douglas Gregorb98b1992009-08-11 05:31:07 +00008819 // Create the overloaded operator invocation for unary operators.
8820 if (NumArgs == 1 || isPostIncDec) {
John McCall2de56d12010-08-25 11:45:40 +00008821 UnaryOperatorKind Opc
Douglas Gregorb98b1992009-08-11 05:31:07 +00008822 = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec);
John McCall9ae2f072010-08-23 23:25:46 +00008823 return SemaRef.CreateOverloadedUnaryOp(OpLoc, Opc, Functions, First);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008824 }
Mike Stump1eb44332009-09-09 15:08:12 +00008825
Douglas Gregor5b8968c2011-07-15 16:25:15 +00008826 if (Op == OO_Subscript) {
8827 SourceLocation LBrace;
8828 SourceLocation RBrace;
8829
8830 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) {
8831 DeclarationNameLoc &NameLoc = DRE->getNameInfo().getInfo();
8832 LBrace = SourceLocation::getFromRawEncoding(
8833 NameLoc.CXXOperatorName.BeginOpNameLoc);
8834 RBrace = SourceLocation::getFromRawEncoding(
8835 NameLoc.CXXOperatorName.EndOpNameLoc);
8836 } else {
8837 LBrace = Callee->getLocStart();
8838 RBrace = OpLoc;
8839 }
8840
8841 return SemaRef.CreateOverloadedArraySubscriptExpr(LBrace, RBrace,
8842 First, Second);
8843 }
Sebastian Redlf322ed62009-10-29 20:17:01 +00008844
Douglas Gregorb98b1992009-08-11 05:31:07 +00008845 // Create the overloaded operator invocation for binary operators.
John McCall2de56d12010-08-25 11:45:40 +00008846 BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op);
John McCall60d7b3a2010-08-24 06:29:42 +00008847 ExprResult Result
Douglas Gregorb98b1992009-08-11 05:31:07 +00008848 = SemaRef.CreateOverloadedBinOp(OpLoc, Opc, Functions, Args[0], Args[1]);
8849 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008850 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00008851
Mike Stump1eb44332009-09-09 15:08:12 +00008852 return move(Result);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008853}
Mike Stump1eb44332009-09-09 15:08:12 +00008854
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008855template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008856ExprResult
John McCall9ae2f072010-08-23 23:25:46 +00008857TreeTransform<Derived>::RebuildCXXPseudoDestructorExpr(Expr *Base,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008858 SourceLocation OperatorLoc,
8859 bool isArrow,
Douglas Gregorf3db29f2011-02-25 18:19:59 +00008860 CXXScopeSpec &SS,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008861 TypeSourceInfo *ScopeType,
8862 SourceLocation CCLoc,
Douglas Gregorfce46ee2010-02-24 23:50:37 +00008863 SourceLocation TildeLoc,
Douglas Gregora2e7dd22010-02-25 01:56:36 +00008864 PseudoDestructorTypeStorage Destroyed) {
John McCall9ae2f072010-08-23 23:25:46 +00008865 QualType BaseType = Base->getType();
8866 if (Base->isTypeDependent() || Destroyed.getIdentifier() ||
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008867 (!isArrow && !BaseType->getAs<RecordType>()) ||
Sean Huntc3021132010-05-05 15:23:54 +00008868 (isArrow && BaseType->getAs<PointerType>() &&
Gabor Greifbf2ca2f2010-02-25 13:04:33 +00008869 !BaseType->getAs<PointerType>()->getPointeeType()
8870 ->template getAs<RecordType>())){
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008871 // This pseudo-destructor expression is still a pseudo-destructor.
John McCall9ae2f072010-08-23 23:25:46 +00008872 return SemaRef.BuildPseudoDestructorExpr(Base, OperatorLoc,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008873 isArrow? tok::arrow : tok::period,
Douglas Gregorfce46ee2010-02-24 23:50:37 +00008874 SS, ScopeType, CCLoc, TildeLoc,
Douglas Gregora2e7dd22010-02-25 01:56:36 +00008875 Destroyed,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008876 /*FIXME?*/true);
8877 }
Abramo Bagnara25777432010-08-11 22:01:17 +00008878
Douglas Gregora2e7dd22010-02-25 01:56:36 +00008879 TypeSourceInfo *DestroyedType = Destroyed.getTypeSourceInfo();
Abramo Bagnara25777432010-08-11 22:01:17 +00008880 DeclarationName Name(SemaRef.Context.DeclarationNames.getCXXDestructorName(
8881 SemaRef.Context.getCanonicalType(DestroyedType->getType())));
8882 DeclarationNameInfo NameInfo(Name, Destroyed.getLocation());
8883 NameInfo.setNamedTypeInfo(DestroyedType);
8884
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008885 // FIXME: the ScopeType should be tacked onto SS.
Abramo Bagnara25777432010-08-11 22:01:17 +00008886
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008887 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
John McCall9ae2f072010-08-23 23:25:46 +00008888 return getSema().BuildMemberReferenceExpr(Base, BaseType,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008889 OperatorLoc, isArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008890 SS, TemplateKWLoc,
8891 /*FIXME: FirstQualifier*/ 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00008892 NameInfo,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008893 /*TemplateArgs*/ 0);
8894}
8895
Douglas Gregor577f75a2009-08-04 16:50:30 +00008896} // end namespace clang
8897
8898#endif // LLVM_CLANG_SEMA_TREETRANSFORM_H