blob: 7e095f7336bce476dd73c406e600d647387528ff [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");
Sean Huntc3021132010-05-05 15:23:54 +0000881
Douglas Gregor40336422010-03-31 22:19:08 +0000882 case LookupResult::Ambiguous:
883 // Let the LookupResult structure handle ambiguities.
884 return QualType();
885 }
886
887 if (!Tag) {
Nick Lewycky446e4022011-01-24 19:01:04 +0000888 // Check where the name exists but isn't a tag type and use that to emit
889 // better diagnostics.
890 LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
891 SemaRef.LookupQualifiedName(Result, DC);
892 switch (Result.getResultKind()) {
893 case LookupResult::Found:
894 case LookupResult::FoundOverloaded:
895 case LookupResult::FoundUnresolvedValue: {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000896 NamedDecl *SomeDecl = Result.getRepresentativeDecl();
Nick Lewycky446e4022011-01-24 19:01:04 +0000897 unsigned Kind = 0;
898 if (isa<TypedefDecl>(SomeDecl)) Kind = 1;
Richard Smith162e1c12011-04-15 14:24:37 +0000899 else if (isa<TypeAliasDecl>(SomeDecl)) Kind = 2;
900 else if (isa<ClassTemplateDecl>(SomeDecl)) Kind = 3;
Nick Lewycky446e4022011-01-24 19:01:04 +0000901 SemaRef.Diag(IdLoc, diag::err_tag_reference_non_tag) << Kind;
902 SemaRef.Diag(SomeDecl->getLocation(), diag::note_declared_at);
903 break;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000904 }
Nick Lewycky446e4022011-01-24 19:01:04 +0000905 default:
906 // FIXME: Would be nice to highlight just the source range.
907 SemaRef.Diag(IdLoc, diag::err_not_tag_in_scope)
908 << Kind << Id << DC;
909 break;
910 }
Douglas Gregor40336422010-03-31 22:19:08 +0000911 return QualType();
912 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000913
Richard Trieubbf34c02011-06-10 03:11:26 +0000914 if (!SemaRef.isAcceptableTagRedeclaration(Tag, Kind, /*isDefinition*/false,
915 IdLoc, *Id)) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000916 SemaRef.Diag(KeywordLoc, diag::err_use_with_wrong_tag) << Id;
Douglas Gregor40336422010-03-31 22:19:08 +0000917 SemaRef.Diag(Tag->getLocation(), diag::note_previous_use);
918 return QualType();
919 }
920
921 // Build the elaborated-type-specifier type.
922 QualType T = SemaRef.Context.getTypeDeclType(Tag);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000923 return SemaRef.Context.getElaboratedType(Keyword,
924 QualifierLoc.getNestedNameSpecifier(),
925 T);
Douglas Gregordcee1a12009-08-06 05:28:30 +0000926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000928 /// \brief Build a new pack expansion type.
929 ///
930 /// By default, builds a new PackExpansionType type from the given pattern.
931 /// Subclasses may override this routine to provide different behavior.
932 QualType RebuildPackExpansionType(QualType Pattern,
933 SourceRange PatternRange,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000934 SourceLocation EllipsisLoc,
935 llvm::Optional<unsigned> NumExpansions) {
936 return getSema().CheckPackExpansion(Pattern, PatternRange, EllipsisLoc,
937 NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000938 }
939
Eli Friedmanb001de72011-10-06 23:00:33 +0000940 /// \brief Build a new atomic type given its value type.
941 ///
942 /// By default, performs semantic analysis when building the atomic type.
943 /// Subclasses may override this routine to provide different behavior.
944 QualType RebuildAtomicType(QualType ValueType, SourceLocation KWLoc);
945
Douglas Gregord1067e52009-08-06 06:41:21 +0000946 /// \brief Build a new template name given a nested name specifier, a flag
947 /// indicating whether the "template" keyword was provided, and the template
948 /// that the template name refers to.
949 ///
950 /// By default, builds the new template name directly. Subclasses may override
951 /// this routine to provide different behavior.
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000952 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
Douglas Gregord1067e52009-08-06 06:41:21 +0000953 bool TemplateKW,
954 TemplateDecl *Template);
955
Douglas Gregord1067e52009-08-06 06:41:21 +0000956 /// \brief Build a new template name given a nested name specifier and the
957 /// name that is referred to as a template.
958 ///
959 /// By default, performs semantic analysis to determine whether the name can
960 /// be resolved to a specific template, then builds the appropriate kind of
961 /// template name. Subclasses may override this routine to provide different
962 /// behavior.
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000963 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
964 const IdentifierInfo &Name,
965 SourceLocation NameLoc,
John McCall43fed0d2010-11-12 08:19:04 +0000966 QualType ObjectType,
967 NamedDecl *FirstQualifierInScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000969 /// \brief Build a new template name given a nested name specifier and the
970 /// overloaded operator name that is referred to as a template.
971 ///
972 /// By default, performs semantic analysis to determine whether the name can
973 /// be resolved to a specific template, then builds the appropriate kind of
974 /// template name. Subclasses may override this routine to provide different
975 /// behavior.
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000976 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000977 OverloadedOperatorKind Operator,
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000978 SourceLocation NameLoc,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000979 QualType ObjectType);
Douglas Gregor1aee05d2011-01-15 06:45:20 +0000980
981 /// \brief Build a new template name given a template template parameter pack
982 /// and the
983 ///
984 /// By default, performs semantic analysis to determine whether the name can
985 /// be resolved to a specific template, then builds the appropriate kind of
986 /// template name. Subclasses may override this routine to provide different
987 /// behavior.
988 TemplateName RebuildTemplateName(TemplateTemplateParmDecl *Param,
989 const TemplateArgument &ArgPack) {
990 return getSema().Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
991 }
992
Douglas Gregor43959a92009-08-20 07:17:43 +0000993 /// \brief Build a new compound statement.
994 ///
995 /// By default, performs semantic analysis to build the new statement.
996 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +0000997 StmtResult RebuildCompoundStmt(SourceLocation LBraceLoc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000998 MultiStmtArg Statements,
999 SourceLocation RBraceLoc,
1000 bool IsStmtExpr) {
John McCall9ae2f072010-08-23 23:25:46 +00001001 return getSema().ActOnCompoundStmt(LBraceLoc, RBraceLoc, Statements,
Douglas Gregor43959a92009-08-20 07:17:43 +00001002 IsStmtExpr);
1003 }
1004
1005 /// \brief Build a new case statement.
1006 ///
1007 /// By default, performs semantic analysis to build the new statement.
1008 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001009 StmtResult RebuildCaseStmt(SourceLocation CaseLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001010 Expr *LHS,
Douglas Gregor43959a92009-08-20 07:17:43 +00001011 SourceLocation EllipsisLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001012 Expr *RHS,
Douglas Gregor43959a92009-08-20 07:17:43 +00001013 SourceLocation ColonLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001014 return getSema().ActOnCaseStmt(CaseLoc, LHS, EllipsisLoc, RHS,
Douglas Gregor43959a92009-08-20 07:17:43 +00001015 ColonLoc);
1016 }
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregor43959a92009-08-20 07:17:43 +00001018 /// \brief Attach the body to a new case statement.
1019 ///
1020 /// By default, performs semantic analysis to build the new statement.
1021 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001022 StmtResult RebuildCaseStmtBody(Stmt *S, Stmt *Body) {
John McCall9ae2f072010-08-23 23:25:46 +00001023 getSema().ActOnCaseStmtBody(S, Body);
1024 return S;
Douglas Gregor43959a92009-08-20 07:17:43 +00001025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Douglas Gregor43959a92009-08-20 07:17:43 +00001027 /// \brief Build a new default statement.
1028 ///
1029 /// By default, performs semantic analysis to build the new statement.
1030 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001031 StmtResult RebuildDefaultStmt(SourceLocation DefaultLoc,
Douglas Gregor43959a92009-08-20 07:17:43 +00001032 SourceLocation ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001033 Stmt *SubStmt) {
1034 return getSema().ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt,
Douglas Gregor43959a92009-08-20 07:17:43 +00001035 /*CurScope=*/0);
1036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Douglas Gregor43959a92009-08-20 07:17:43 +00001038 /// \brief Build a new label statement.
1039 ///
1040 /// By default, performs semantic analysis to build the new statement.
1041 /// Subclasses may override this routine to provide different behavior.
Chris Lattner57ad3782011-02-17 20:34:02 +00001042 StmtResult RebuildLabelStmt(SourceLocation IdentLoc, LabelDecl *L,
1043 SourceLocation ColonLoc, Stmt *SubStmt) {
1044 return SemaRef.ActOnLabelStmt(IdentLoc, L, ColonLoc, SubStmt);
Douglas Gregor43959a92009-08-20 07:17:43 +00001045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Douglas Gregor43959a92009-08-20 07:17:43 +00001047 /// \brief Build a new "if" statement.
1048 ///
1049 /// By default, performs semantic analysis to build the new statement.
1050 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001051 StmtResult RebuildIfStmt(SourceLocation IfLoc, Sema::FullExprArg Cond,
Chris Lattner57ad3782011-02-17 20:34:02 +00001052 VarDecl *CondVar, Stmt *Then,
1053 SourceLocation ElseLoc, Stmt *Else) {
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001054 return getSema().ActOnIfStmt(IfLoc, Cond, CondVar, Then, ElseLoc, Else);
Douglas Gregor43959a92009-08-20 07:17:43 +00001055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Douglas Gregor43959a92009-08-20 07:17:43 +00001057 /// \brief Start building a new switch statement.
1058 ///
1059 /// By default, performs semantic analysis to build the new statement.
1060 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001061 StmtResult RebuildSwitchStmtStart(SourceLocation SwitchLoc,
Chris Lattner57ad3782011-02-17 20:34:02 +00001062 Expr *Cond, VarDecl *CondVar) {
John McCall9ae2f072010-08-23 23:25:46 +00001063 return getSema().ActOnStartOfSwitchStmt(SwitchLoc, Cond,
John McCalld226f652010-08-21 09:40:31 +00001064 CondVar);
Douglas Gregor43959a92009-08-20 07:17:43 +00001065 }
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Douglas Gregor43959a92009-08-20 07:17:43 +00001067 /// \brief Attach the body to the switch statement.
1068 ///
1069 /// By default, performs semantic analysis to build the new statement.
1070 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001071 StmtResult RebuildSwitchStmtBody(SourceLocation SwitchLoc,
Chris Lattner57ad3782011-02-17 20:34:02 +00001072 Stmt *Switch, Stmt *Body) {
John McCall9ae2f072010-08-23 23:25:46 +00001073 return getSema().ActOnFinishSwitchStmt(SwitchLoc, Switch, Body);
Douglas Gregor43959a92009-08-20 07:17:43 +00001074 }
1075
1076 /// \brief Build a new while statement.
1077 ///
1078 /// By default, performs semantic analysis to build the new statement.
1079 /// Subclasses may override this routine to provide different behavior.
Chris Lattner57ad3782011-02-17 20:34:02 +00001080 StmtResult RebuildWhileStmt(SourceLocation WhileLoc, Sema::FullExprArg Cond,
1081 VarDecl *CondVar, Stmt *Body) {
John McCall9ae2f072010-08-23 23:25:46 +00001082 return getSema().ActOnWhileStmt(WhileLoc, Cond, CondVar, Body);
Douglas Gregor43959a92009-08-20 07:17:43 +00001083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Douglas Gregor43959a92009-08-20 07:17:43 +00001085 /// \brief Build a new do-while statement.
1086 ///
1087 /// By default, performs semantic analysis to build the new statement.
1088 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001089 StmtResult RebuildDoStmt(SourceLocation DoLoc, Stmt *Body,
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001090 SourceLocation WhileLoc, SourceLocation LParenLoc,
1091 Expr *Cond, SourceLocation RParenLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001092 return getSema().ActOnDoStmt(DoLoc, Body, WhileLoc, LParenLoc,
1093 Cond, RParenLoc);
Douglas Gregor43959a92009-08-20 07:17:43 +00001094 }
1095
1096 /// \brief Build a new for statement.
1097 ///
1098 /// By default, performs semantic analysis to build the new statement.
1099 /// Subclasses may override this routine to provide different behavior.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001100 StmtResult RebuildForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
1101 Stmt *Init, Sema::FullExprArg Cond,
1102 VarDecl *CondVar, Sema::FullExprArg Inc,
1103 SourceLocation RParenLoc, Stmt *Body) {
John McCall9ae2f072010-08-23 23:25:46 +00001104 return getSema().ActOnForStmt(ForLoc, LParenLoc, Init, Cond,
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001105 CondVar, Inc, RParenLoc, Body);
Douglas Gregor43959a92009-08-20 07:17:43 +00001106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Douglas Gregor43959a92009-08-20 07:17:43 +00001108 /// \brief Build a new goto statement.
1109 ///
1110 /// By default, performs semantic analysis to build the new statement.
1111 /// Subclasses may override this routine to provide different behavior.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001112 StmtResult RebuildGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
1113 LabelDecl *Label) {
Chris Lattner57ad3782011-02-17 20:34:02 +00001114 return getSema().ActOnGotoStmt(GotoLoc, LabelLoc, Label);
Douglas Gregor43959a92009-08-20 07:17:43 +00001115 }
1116
1117 /// \brief Build a new indirect goto statement.
1118 ///
1119 /// By default, performs semantic analysis to build the new statement.
1120 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001121 StmtResult RebuildIndirectGotoStmt(SourceLocation GotoLoc,
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001122 SourceLocation StarLoc,
1123 Expr *Target) {
John McCall9ae2f072010-08-23 23:25:46 +00001124 return getSema().ActOnIndirectGotoStmt(GotoLoc, StarLoc, Target);
Douglas Gregor43959a92009-08-20 07:17:43 +00001125 }
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Douglas Gregor43959a92009-08-20 07:17:43 +00001127 /// \brief Build a new return statement.
1128 ///
1129 /// By default, performs semantic analysis to build the new statement.
1130 /// Subclasses may override this routine to provide different behavior.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001131 StmtResult RebuildReturnStmt(SourceLocation ReturnLoc, Expr *Result) {
John McCall9ae2f072010-08-23 23:25:46 +00001132 return getSema().ActOnReturnStmt(ReturnLoc, Result);
Douglas Gregor43959a92009-08-20 07:17:43 +00001133 }
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Douglas Gregor43959a92009-08-20 07:17:43 +00001135 /// \brief Build a new declaration statement.
1136 ///
1137 /// By default, performs semantic analysis to build the new statement.
1138 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001139 StmtResult RebuildDeclStmt(Decl **Decls, unsigned NumDecls,
Mike Stump1eb44332009-09-09 15:08:12 +00001140 SourceLocation StartLoc,
Douglas Gregor43959a92009-08-20 07:17:43 +00001141 SourceLocation EndLoc) {
Richard Smith406c38e2011-02-23 00:37:57 +00001142 Sema::DeclGroupPtrTy DG = getSema().BuildDeclaratorGroup(Decls, NumDecls);
1143 return getSema().ActOnDeclStmt(DG, StartLoc, EndLoc);
Douglas Gregor43959a92009-08-20 07:17:43 +00001144 }
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Anders Carlsson703e3942010-01-24 05:50:09 +00001146 /// \brief Build a new inline asm statement.
1147 ///
1148 /// By default, performs semantic analysis to build the new statement.
1149 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001150 StmtResult RebuildAsmStmt(SourceLocation AsmLoc,
Anders Carlsson703e3942010-01-24 05:50:09 +00001151 bool IsSimple,
1152 bool IsVolatile,
1153 unsigned NumOutputs,
1154 unsigned NumInputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001155 IdentifierInfo **Names,
Anders Carlsson703e3942010-01-24 05:50:09 +00001156 MultiExprArg Constraints,
1157 MultiExprArg Exprs,
John McCall9ae2f072010-08-23 23:25:46 +00001158 Expr *AsmString,
Anders Carlsson703e3942010-01-24 05:50:09 +00001159 MultiExprArg Clobbers,
1160 SourceLocation RParenLoc,
1161 bool MSAsm) {
Sean Huntc3021132010-05-05 15:23:54 +00001162 return getSema().ActOnAsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs,
Anders Carlsson703e3942010-01-24 05:50:09 +00001163 NumInputs, Names, move(Constraints),
John McCall9ae2f072010-08-23 23:25:46 +00001164 Exprs, AsmString, Clobbers,
Anders Carlsson703e3942010-01-24 05:50:09 +00001165 RParenLoc, MSAsm);
1166 }
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00001167
1168 /// \brief Build a new Objective-C @try statement.
1169 ///
1170 /// By default, performs semantic analysis to build the new statement.
1171 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001172 StmtResult RebuildObjCAtTryStmt(SourceLocation AtLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001173 Stmt *TryBody,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001174 MultiStmtArg CatchStmts,
John McCall9ae2f072010-08-23 23:25:46 +00001175 Stmt *Finally) {
1176 return getSema().ActOnObjCAtTryStmt(AtLoc, TryBody, move(CatchStmts),
1177 Finally);
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00001178 }
1179
Douglas Gregorbe270a02010-04-26 17:57:08 +00001180 /// \brief Rebuild an Objective-C exception declaration.
1181 ///
1182 /// By default, performs semantic analysis to build the new declaration.
1183 /// Subclasses may override this routine to provide different behavior.
1184 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
1185 TypeSourceInfo *TInfo, QualType T) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001186 return getSema().BuildObjCExceptionDecl(TInfo, T,
1187 ExceptionDecl->getInnerLocStart(),
1188 ExceptionDecl->getLocation(),
1189 ExceptionDecl->getIdentifier());
Douglas Gregorbe270a02010-04-26 17:57:08 +00001190 }
Sean Huntc3021132010-05-05 15:23:54 +00001191
Douglas Gregorbe270a02010-04-26 17:57:08 +00001192 /// \brief Build a new Objective-C @catch statement.
1193 ///
1194 /// By default, performs semantic analysis to build the new statement.
1195 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001196 StmtResult RebuildObjCAtCatchStmt(SourceLocation AtLoc,
Douglas Gregorbe270a02010-04-26 17:57:08 +00001197 SourceLocation RParenLoc,
1198 VarDecl *Var,
John McCall9ae2f072010-08-23 23:25:46 +00001199 Stmt *Body) {
Douglas Gregorbe270a02010-04-26 17:57:08 +00001200 return getSema().ActOnObjCAtCatchStmt(AtLoc, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001201 Var, Body);
Douglas Gregorbe270a02010-04-26 17:57:08 +00001202 }
Sean Huntc3021132010-05-05 15:23:54 +00001203
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00001204 /// \brief Build a new Objective-C @finally statement.
1205 ///
1206 /// By default, performs semantic analysis to build the new statement.
1207 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001208 StmtResult RebuildObjCAtFinallyStmt(SourceLocation AtLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001209 Stmt *Body) {
1210 return getSema().ActOnObjCAtFinallyStmt(AtLoc, Body);
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00001211 }
Sean Huntc3021132010-05-05 15:23:54 +00001212
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00001213 /// \brief Build a new Objective-C @throw statement.
Douglas Gregord1377b22010-04-22 21:44:01 +00001214 ///
1215 /// By default, performs semantic analysis to build the new statement.
1216 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001217 StmtResult RebuildObjCAtThrowStmt(SourceLocation AtLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001218 Expr *Operand) {
1219 return getSema().BuildObjCAtThrowStmt(AtLoc, Operand);
Douglas Gregord1377b22010-04-22 21:44:01 +00001220 }
Sean Huntc3021132010-05-05 15:23:54 +00001221
John McCall07524032011-07-27 21:50:02 +00001222 /// \brief Rebuild the operand to an Objective-C @synchronized statement.
1223 ///
1224 /// By default, performs semantic analysis to build the new statement.
1225 /// Subclasses may override this routine to provide different behavior.
1226 ExprResult RebuildObjCAtSynchronizedOperand(SourceLocation atLoc,
1227 Expr *object) {
1228 return getSema().ActOnObjCAtSynchronizedOperand(atLoc, object);
1229 }
1230
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00001231 /// \brief Build a new Objective-C @synchronized statement.
1232 ///
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00001233 /// By default, performs semantic analysis to build the new statement.
1234 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001235 StmtResult RebuildObjCAtSynchronizedStmt(SourceLocation AtLoc,
John McCall07524032011-07-27 21:50:02 +00001236 Expr *Object, Stmt *Body) {
1237 return getSema().ActOnObjCAtSynchronizedStmt(AtLoc, Object, Body);
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00001238 }
Douglas Gregorc3203e72010-04-22 23:10:45 +00001239
John McCallf85e1932011-06-15 23:02:42 +00001240 /// \brief Build a new Objective-C @autoreleasepool statement.
1241 ///
1242 /// By default, performs semantic analysis to build the new statement.
1243 /// Subclasses may override this routine to provide different behavior.
1244 StmtResult RebuildObjCAutoreleasePoolStmt(SourceLocation AtLoc,
1245 Stmt *Body) {
1246 return getSema().ActOnObjCAutoreleasePoolStmt(AtLoc, Body);
1247 }
John McCall990567c2011-07-27 01:07:15 +00001248
1249 /// \brief Build the collection operand to a new Objective-C fast
1250 /// enumeration statement.
1251 ///
1252 /// By default, performs semantic analysis to build the new statement.
1253 /// Subclasses may override this routine to provide different behavior.
1254 ExprResult RebuildObjCForCollectionOperand(SourceLocation forLoc,
1255 Expr *collection) {
1256 return getSema().ActOnObjCForCollectionOperand(forLoc, collection);
1257 }
John McCallf85e1932011-06-15 23:02:42 +00001258
Douglas Gregorc3203e72010-04-22 23:10:45 +00001259 /// \brief Build a new Objective-C fast enumeration statement.
1260 ///
1261 /// By default, performs semantic analysis to build the new statement.
1262 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001263 StmtResult RebuildObjCForCollectionStmt(SourceLocation ForLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001264 SourceLocation LParenLoc,
1265 Stmt *Element,
1266 Expr *Collection,
1267 SourceLocation RParenLoc,
1268 Stmt *Body) {
Douglas Gregorc3203e72010-04-22 23:10:45 +00001269 return getSema().ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001270 Element,
1271 Collection,
Douglas Gregorc3203e72010-04-22 23:10:45 +00001272 RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001273 Body);
Douglas Gregorc3203e72010-04-22 23:10:45 +00001274 }
Sean Huntc3021132010-05-05 15:23:54 +00001275
Douglas Gregor43959a92009-08-20 07:17:43 +00001276 /// \brief Build a new C++ exception declaration.
1277 ///
1278 /// By default, performs semantic analysis to build the new decaration.
1279 /// Subclasses may override this routine to provide different behavior.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001280 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCalla93c9342009-12-07 02:54:59 +00001281 TypeSourceInfo *Declarator,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001282 SourceLocation StartLoc,
1283 SourceLocation IdLoc,
1284 IdentifierInfo *Id) {
Douglas Gregorefdf9882011-04-14 22:32:28 +00001285 VarDecl *Var = getSema().BuildExceptionDeclaration(0, Declarator,
1286 StartLoc, IdLoc, Id);
1287 if (Var)
1288 getSema().CurContext->addDecl(Var);
1289 return Var;
Douglas Gregor43959a92009-08-20 07:17:43 +00001290 }
1291
1292 /// \brief Build a new C++ catch statement.
1293 ///
1294 /// By default, performs semantic analysis to build the new statement.
1295 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001296 StmtResult RebuildCXXCatchStmt(SourceLocation CatchLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001297 VarDecl *ExceptionDecl,
1298 Stmt *Handler) {
John McCall9ae2f072010-08-23 23:25:46 +00001299 return Owned(new (getSema().Context) CXXCatchStmt(CatchLoc, ExceptionDecl,
1300 Handler));
Douglas Gregor43959a92009-08-20 07:17:43 +00001301 }
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Douglas Gregor43959a92009-08-20 07:17:43 +00001303 /// \brief Build a new C++ try statement.
1304 ///
1305 /// By default, performs semantic analysis to build the new statement.
1306 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001307 StmtResult RebuildCXXTryStmt(SourceLocation TryLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001308 Stmt *TryBlock,
1309 MultiStmtArg Handlers) {
John McCall9ae2f072010-08-23 23:25:46 +00001310 return getSema().ActOnCXXTryBlock(TryLoc, TryBlock, move(Handlers));
Douglas Gregor43959a92009-08-20 07:17:43 +00001311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Richard Smithad762fc2011-04-14 22:09:26 +00001313 /// \brief Build a new C++0x range-based for statement.
1314 ///
1315 /// By default, performs semantic analysis to build the new statement.
1316 /// Subclasses may override this routine to provide different behavior.
1317 StmtResult RebuildCXXForRangeStmt(SourceLocation ForLoc,
1318 SourceLocation ColonLoc,
1319 Stmt *Range, Stmt *BeginEnd,
1320 Expr *Cond, Expr *Inc,
1321 Stmt *LoopVar,
1322 SourceLocation RParenLoc) {
1323 return getSema().BuildCXXForRangeStmt(ForLoc, ColonLoc, Range, BeginEnd,
1324 Cond, Inc, LoopVar, RParenLoc);
1325 }
Douglas Gregorba0513d2011-10-25 01:33:02 +00001326
1327 /// \brief Build a new C++0x range-based for statement.
1328 ///
1329 /// By default, performs semantic analysis to build the new statement.
1330 /// Subclasses may override this routine to provide different behavior.
1331 StmtResult RebuildMSDependentExistsStmt(SourceLocation KeywordLoc,
1332 bool IsIfExists,
1333 NestedNameSpecifierLoc QualifierLoc,
1334 DeclarationNameInfo NameInfo,
1335 Stmt *Nested) {
1336 return getSema().BuildMSDependentExistsStmt(KeywordLoc, IsIfExists,
1337 QualifierLoc, NameInfo, Nested);
1338 }
1339
Richard Smithad762fc2011-04-14 22:09:26 +00001340 /// \brief Attach body to a C++0x range-based for statement.
1341 ///
1342 /// By default, performs semantic analysis to finish the new statement.
1343 /// Subclasses may override this routine to provide different behavior.
1344 StmtResult FinishCXXForRangeStmt(Stmt *ForRange, Stmt *Body) {
1345 return getSema().FinishCXXForRangeStmt(ForRange, Body);
1346 }
1347
John Wiegley28bbe4b2011-04-28 01:08:34 +00001348 StmtResult RebuildSEHTryStmt(bool IsCXXTry,
1349 SourceLocation TryLoc,
1350 Stmt *TryBlock,
1351 Stmt *Handler) {
1352 return getSema().ActOnSEHTryBlock(IsCXXTry,TryLoc,TryBlock,Handler);
1353 }
1354
1355 StmtResult RebuildSEHExceptStmt(SourceLocation Loc,
1356 Expr *FilterExpr,
1357 Stmt *Block) {
1358 return getSema().ActOnSEHExceptBlock(Loc,FilterExpr,Block);
1359 }
1360
1361 StmtResult RebuildSEHFinallyStmt(SourceLocation Loc,
1362 Stmt *Block) {
1363 return getSema().ActOnSEHFinallyBlock(Loc,Block);
1364 }
1365
Douglas Gregorb98b1992009-08-11 05:31:07 +00001366 /// \brief Build a new expression that references a declaration.
1367 ///
1368 /// By default, performs semantic analysis to build the new expression.
1369 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001370 ExprResult RebuildDeclarationNameExpr(const CXXScopeSpec &SS,
John McCallf312b1e2010-08-26 23:41:50 +00001371 LookupResult &R,
1372 bool RequiresADL) {
John McCallf7a1a742009-11-24 19:00:30 +00001373 return getSema().BuildDeclarationNameExpr(SS, R, RequiresADL);
1374 }
1375
1376
1377 /// \brief Build a new expression that references a declaration.
1378 ///
1379 /// By default, performs semantic analysis to build the new expression.
1380 /// Subclasses may override this routine to provide different behavior.
Douglas Gregor40d96a62011-02-28 21:54:11 +00001381 ExprResult RebuildDeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001382 ValueDecl *VD,
1383 const DeclarationNameInfo &NameInfo,
1384 TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00001385 CXXScopeSpec SS;
Douglas Gregor40d96a62011-02-28 21:54:11 +00001386 SS.Adopt(QualifierLoc);
John McCalldbd872f2009-12-08 09:08:17 +00001387
1388 // FIXME: loses template args.
Abramo Bagnara25777432010-08-11 22:01:17 +00001389
1390 return getSema().BuildDeclarationNameExpr(SS, NameInfo, VD);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregorb98b1992009-08-11 05:31:07 +00001393 /// \brief Build a new expression in parentheses.
Mike Stump1eb44332009-09-09 15:08:12 +00001394 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001395 /// By default, performs semantic analysis to build the new expression.
1396 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001397 ExprResult RebuildParenExpr(Expr *SubExpr, SourceLocation LParen,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001398 SourceLocation RParen) {
John McCall9ae2f072010-08-23 23:25:46 +00001399 return getSema().ActOnParenExpr(LParen, RParen, SubExpr);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001400 }
1401
Douglas Gregora71d8192009-09-04 17:36:40 +00001402 /// \brief Build a new pseudo-destructor expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001403 ///
Douglas Gregora71d8192009-09-04 17:36:40 +00001404 /// By default, performs semantic analysis to build the new expression.
1405 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001406 ExprResult RebuildCXXPseudoDestructorExpr(Expr *Base,
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001407 SourceLocation OperatorLoc,
1408 bool isArrow,
1409 CXXScopeSpec &SS,
1410 TypeSourceInfo *ScopeType,
1411 SourceLocation CCLoc,
1412 SourceLocation TildeLoc,
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001413 PseudoDestructorTypeStorage Destroyed);
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Douglas Gregorb98b1992009-08-11 05:31:07 +00001415 /// \brief Build a new unary operator expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001416 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001417 /// By default, performs semantic analysis to build the new expression.
1418 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001419 ExprResult RebuildUnaryOperator(SourceLocation OpLoc,
John McCall2de56d12010-08-25 11:45:40 +00001420 UnaryOperatorKind Opc,
John McCall9ae2f072010-08-23 23:25:46 +00001421 Expr *SubExpr) {
1422 return getSema().BuildUnaryOp(/*Scope=*/0, OpLoc, Opc, SubExpr);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001423 }
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001425 /// \brief Build a new builtin offsetof expression.
1426 ///
1427 /// By default, performs semantic analysis to build the new expression.
1428 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001429 ExprResult RebuildOffsetOfExpr(SourceLocation OperatorLoc,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001430 TypeSourceInfo *Type,
John McCallf312b1e2010-08-26 23:41:50 +00001431 Sema::OffsetOfComponent *Components,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001432 unsigned NumComponents,
1433 SourceLocation RParenLoc) {
1434 return getSema().BuildBuiltinOffsetOf(OperatorLoc, Type, Components,
1435 NumComponents, RParenLoc);
1436 }
Sean Huntc3021132010-05-05 15:23:54 +00001437
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001438 /// \brief Build a new sizeof, alignof or vec_step expression with a
1439 /// type argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001440 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001441 /// By default, performs semantic analysis to build the new expression.
1442 /// Subclasses may override this routine to provide different behavior.
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001443 ExprResult RebuildUnaryExprOrTypeTrait(TypeSourceInfo *TInfo,
1444 SourceLocation OpLoc,
1445 UnaryExprOrTypeTrait ExprKind,
1446 SourceRange R) {
1447 return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001448 }
1449
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001450 /// \brief Build a new sizeof, alignof or vec step expression with an
1451 /// expression argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001452 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001453 /// By default, performs semantic analysis to build the new expression.
1454 /// Subclasses may override this routine to provide different behavior.
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001455 ExprResult RebuildUnaryExprOrTypeTrait(Expr *SubExpr, SourceLocation OpLoc,
1456 UnaryExprOrTypeTrait ExprKind,
1457 SourceRange R) {
John McCall60d7b3a2010-08-24 06:29:42 +00001458 ExprResult Result
Chandler Carruthe72c55b2011-05-29 07:32:14 +00001459 = getSema().CreateUnaryExprOrTypeTraitExpr(SubExpr, OpLoc, ExprKind);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001460 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001461 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Douglas Gregorb98b1992009-08-11 05:31:07 +00001463 return move(Result);
1464 }
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Douglas Gregorb98b1992009-08-11 05:31:07 +00001466 /// \brief Build a new array subscript expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001467 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001468 /// By default, performs semantic analysis to build the new expression.
1469 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001470 ExprResult RebuildArraySubscriptExpr(Expr *LHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001471 SourceLocation LBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001472 Expr *RHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001473 SourceLocation RBracketLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001474 return getSema().ActOnArraySubscriptExpr(/*Scope=*/0, LHS,
1475 LBracketLoc, RHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001476 RBracketLoc);
1477 }
1478
1479 /// \brief Build a new call expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001480 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001481 /// By default, performs semantic analysis to build the new expression.
1482 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001483 ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001484 MultiExprArg Args,
Peter Collingbournee08ce652011-02-09 21:07:24 +00001485 SourceLocation RParenLoc,
1486 Expr *ExecConfig = 0) {
John McCall9ae2f072010-08-23 23:25:46 +00001487 return getSema().ActOnCallExpr(/*Scope=*/0, Callee, LParenLoc,
Peter Collingbournee08ce652011-02-09 21:07:24 +00001488 move(Args), RParenLoc, ExecConfig);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001489 }
1490
1491 /// \brief Build a new member access expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001492 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001493 /// By default, performs semantic analysis to build the new expression.
1494 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001495 ExprResult RebuildMemberExpr(Expr *Base, SourceLocation OpLoc,
John McCallf89e55a2010-11-18 06:31:45 +00001496 bool isArrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001497 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001498 SourceLocation TemplateKWLoc,
John McCallf89e55a2010-11-18 06:31:45 +00001499 const DeclarationNameInfo &MemberNameInfo,
1500 ValueDecl *Member,
1501 NamedDecl *FoundDecl,
John McCalld5532b62009-11-23 01:53:49 +00001502 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +00001503 NamedDecl *FirstQualifierInScope) {
Richard Smith9138b4e2011-10-26 19:06:56 +00001504 ExprResult BaseResult = getSema().PerformMemberExprBaseConversion(Base,
1505 isArrow);
Anders Carlssond8b285f2009-09-01 04:26:58 +00001506 if (!Member->getDeclName()) {
John McCallf89e55a2010-11-18 06:31:45 +00001507 // We have a reference to an unnamed field. This is always the
1508 // base of an anonymous struct/union member access, i.e. the
1509 // field is always of record type.
Douglas Gregor40d96a62011-02-28 21:54:11 +00001510 assert(!QualifierLoc && "Can't have an unnamed field with a qualifier!");
John McCallf89e55a2010-11-18 06:31:45 +00001511 assert(Member->getType()->isRecordType() &&
1512 "unnamed member not of record type?");
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Richard Smith9138b4e2011-10-26 19:06:56 +00001514 BaseResult =
1515 getSema().PerformObjectMemberConversion(BaseResult.take(),
John Wiegley429bb272011-04-08 18:41:53 +00001516 QualifierLoc.getNestedNameSpecifier(),
1517 FoundDecl, Member);
1518 if (BaseResult.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001519 return ExprError();
John Wiegley429bb272011-04-08 18:41:53 +00001520 Base = BaseResult.take();
John McCallf89e55a2010-11-18 06:31:45 +00001521 ExprValueKind VK = isArrow ? VK_LValue : Base->getValueKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001522 MemberExpr *ME =
John McCall9ae2f072010-08-23 23:25:46 +00001523 new (getSema().Context) MemberExpr(Base, isArrow,
Abramo Bagnara25777432010-08-11 22:01:17 +00001524 Member, MemberNameInfo,
John McCallf89e55a2010-11-18 06:31:45 +00001525 cast<FieldDecl>(Member)->getType(),
1526 VK, OK_Ordinary);
Anders Carlssond8b285f2009-09-01 04:26:58 +00001527 return getSema().Owned(ME);
1528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001530 CXXScopeSpec SS;
Douglas Gregor40d96a62011-02-28 21:54:11 +00001531 SS.Adopt(QualifierLoc);
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001532
John Wiegley429bb272011-04-08 18:41:53 +00001533 Base = BaseResult.take();
John McCall9ae2f072010-08-23 23:25:46 +00001534 QualType BaseType = Base->getType();
John McCallaa81e162009-12-01 22:10:20 +00001535
John McCall6bb80172010-03-30 21:47:33 +00001536 // FIXME: this involves duplicating earlier analysis in a lot of
1537 // cases; we should avoid this when possible.
Abramo Bagnara25777432010-08-11 22:01:17 +00001538 LookupResult R(getSema(), MemberNameInfo, Sema::LookupMemberName);
John McCall6bb80172010-03-30 21:47:33 +00001539 R.addDecl(FoundDecl);
John McCallc2233c52010-01-15 08:34:02 +00001540 R.resolveKind();
1541
John McCall9ae2f072010-08-23 23:25:46 +00001542 return getSema().BuildMemberReferenceExpr(Base, BaseType, OpLoc, isArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001543 SS, TemplateKWLoc,
1544 FirstQualifierInScope,
John McCallc2233c52010-01-15 08:34:02 +00001545 R, ExplicitTemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Douglas Gregorb98b1992009-08-11 05:31:07 +00001548 /// \brief Build a new binary operator expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001549 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001550 /// By default, performs semantic analysis to build the new expression.
1551 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001552 ExprResult RebuildBinaryOperator(SourceLocation OpLoc,
John McCall2de56d12010-08-25 11:45:40 +00001553 BinaryOperatorKind Opc,
John McCall9ae2f072010-08-23 23:25:46 +00001554 Expr *LHS, Expr *RHS) {
1555 return getSema().BuildBinOp(/*Scope=*/0, OpLoc, Opc, LHS, RHS);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001556 }
1557
1558 /// \brief Build a new conditional operator expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001559 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001560 /// By default, performs semantic analysis to build the new expression.
1561 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001562 ExprResult RebuildConditionalOperator(Expr *Cond,
John McCall56ca35d2011-02-17 10:25:35 +00001563 SourceLocation QuestionLoc,
1564 Expr *LHS,
1565 SourceLocation ColonLoc,
1566 Expr *RHS) {
John McCall9ae2f072010-08-23 23:25:46 +00001567 return getSema().ActOnConditionalOp(QuestionLoc, ColonLoc, Cond,
1568 LHS, RHS);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001569 }
1570
Douglas Gregorb98b1992009-08-11 05:31:07 +00001571 /// \brief Build a new C-style cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001572 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001573 /// By default, performs semantic analysis to build the new expression.
1574 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001575 ExprResult RebuildCStyleCastExpr(SourceLocation LParenLoc,
John McCall9d125032010-01-15 18:39:57 +00001576 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001577 SourceLocation RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001578 Expr *SubExpr) {
John McCallb042fdf2010-01-15 18:56:44 +00001579 return getSema().BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001580 SubExpr);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001581 }
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Douglas Gregorb98b1992009-08-11 05:31:07 +00001583 /// \brief Build a new compound literal expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001584 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001585 /// By default, performs semantic analysis to build the new expression.
1586 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001587 ExprResult RebuildCompoundLiteralExpr(SourceLocation LParenLoc,
John McCall42f56b52010-01-18 19:35:47 +00001588 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001589 SourceLocation RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001590 Expr *Init) {
John McCall42f56b52010-01-18 19:35:47 +00001591 return getSema().BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001592 Init);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001593 }
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Douglas Gregorb98b1992009-08-11 05:31:07 +00001595 /// \brief Build a new extended vector element access expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001596 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001597 /// By default, performs semantic analysis to build the new expression.
1598 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001599 ExprResult RebuildExtVectorElementExpr(Expr *Base,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001600 SourceLocation OpLoc,
1601 SourceLocation AccessorLoc,
1602 IdentifierInfo &Accessor) {
John McCallaa81e162009-12-01 22:10:20 +00001603
John McCall129e2df2009-11-30 22:42:35 +00001604 CXXScopeSpec SS;
Abramo Bagnara25777432010-08-11 22:01:17 +00001605 DeclarationNameInfo NameInfo(&Accessor, AccessorLoc);
John McCall9ae2f072010-08-23 23:25:46 +00001606 return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
John McCall129e2df2009-11-30 22:42:35 +00001607 OpLoc, /*IsArrow*/ false,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001608 SS, SourceLocation(),
1609 /*FirstQualifierInScope*/ 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001610 NameInfo,
John McCall129e2df2009-11-30 22:42:35 +00001611 /* TemplateArgs */ 0);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001612 }
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Douglas Gregorb98b1992009-08-11 05:31:07 +00001614 /// \brief Build a new initializer list expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001615 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001616 /// By default, performs semantic analysis to build the new expression.
1617 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001618 ExprResult RebuildInitList(SourceLocation LBraceLoc,
John McCallc8fc90a2011-07-06 07:30:07 +00001619 MultiExprArg Inits,
1620 SourceLocation RBraceLoc,
1621 QualType ResultTy) {
John McCall60d7b3a2010-08-24 06:29:42 +00001622 ExprResult Result
Douglas Gregore48319a2009-11-09 17:16:50 +00001623 = SemaRef.ActOnInitList(LBraceLoc, move(Inits), RBraceLoc);
1624 if (Result.isInvalid() || ResultTy->isDependentType())
1625 return move(Result);
Sean Huntc3021132010-05-05 15:23:54 +00001626
Douglas Gregore48319a2009-11-09 17:16:50 +00001627 // Patch in the result type we were given, which may have been computed
1628 // when the initial InitListExpr was built.
1629 InitListExpr *ILE = cast<InitListExpr>((Expr *)Result.get());
1630 ILE->setType(ResultTy);
1631 return move(Result);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001632 }
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Douglas Gregorb98b1992009-08-11 05:31:07 +00001634 /// \brief Build a new designated initializer expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001635 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001636 /// By default, performs semantic analysis to build the new expression.
1637 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001638 ExprResult RebuildDesignatedInitExpr(Designation &Desig,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001639 MultiExprArg ArrayExprs,
1640 SourceLocation EqualOrColonLoc,
1641 bool GNUSyntax,
John McCall9ae2f072010-08-23 23:25:46 +00001642 Expr *Init) {
John McCall60d7b3a2010-08-24 06:29:42 +00001643 ExprResult Result
Douglas Gregorb98b1992009-08-11 05:31:07 +00001644 = SemaRef.ActOnDesignatedInitializer(Desig, EqualOrColonLoc, GNUSyntax,
John McCall9ae2f072010-08-23 23:25:46 +00001645 Init);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001646 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001647 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Douglas Gregorb98b1992009-08-11 05:31:07 +00001649 ArrayExprs.release();
1650 return move(Result);
1651 }
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Douglas Gregorb98b1992009-08-11 05:31:07 +00001653 /// \brief Build a new value-initialized expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001654 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001655 /// By default, builds the implicit value initialization without performing
1656 /// any semantic analysis. Subclasses may override this routine to provide
1657 /// different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001658 ExprResult RebuildImplicitValueInitExpr(QualType T) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001659 return SemaRef.Owned(new (SemaRef.Context) ImplicitValueInitExpr(T));
1660 }
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Douglas Gregorb98b1992009-08-11 05:31:07 +00001662 /// \brief Build a new \c va_arg expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001663 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001664 /// By default, performs semantic analysis to build the new expression.
1665 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001666 ExprResult RebuildVAArgExpr(SourceLocation BuiltinLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001667 Expr *SubExpr, TypeSourceInfo *TInfo,
Abramo Bagnara2cad9002010-08-10 10:06:15 +00001668 SourceLocation RParenLoc) {
1669 return getSema().BuildVAArgExpr(BuiltinLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001670 SubExpr, TInfo,
Abramo Bagnara2cad9002010-08-10 10:06:15 +00001671 RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001672 }
1673
1674 /// \brief Build a new expression list in parentheses.
Mike Stump1eb44332009-09-09 15:08:12 +00001675 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001676 /// By default, performs semantic analysis to build the new expression.
1677 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001678 ExprResult RebuildParenListExpr(SourceLocation LParenLoc,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001679 MultiExprArg SubExprs,
1680 SourceLocation RParenLoc) {
1681 return getSema().ActOnParenListExpr(LParenLoc, RParenLoc, move(SubExprs));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Douglas Gregorb98b1992009-08-11 05:31:07 +00001684 /// \brief Build a new address-of-label expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001685 ///
1686 /// By default, performs semantic analysis, using the name of the label
Douglas Gregorb98b1992009-08-11 05:31:07 +00001687 /// rather than attempting to map the label statement itself.
1688 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001689 ExprResult RebuildAddrLabelExpr(SourceLocation AmpAmpLoc,
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001690 SourceLocation LabelLoc, LabelDecl *Label) {
Chris Lattner57ad3782011-02-17 20:34:02 +00001691 return getSema().ActOnAddrLabel(AmpAmpLoc, LabelLoc, Label);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001692 }
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Douglas Gregorb98b1992009-08-11 05:31:07 +00001694 /// \brief Build a new GNU statement expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001695 ///
Douglas Gregorb98b1992009-08-11 05:31:07 +00001696 /// By default, performs semantic analysis to build the new expression.
1697 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001698 ExprResult RebuildStmtExpr(SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001699 Stmt *SubStmt,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001700 SourceLocation RParenLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001701 return getSema().ActOnStmtExpr(LParenLoc, SubStmt, RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001702 }
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Douglas Gregorb98b1992009-08-11 05:31:07 +00001704 /// \brief Build a new __builtin_choose_expr expression.
1705 ///
1706 /// By default, performs semantic analysis to build the new expression.
1707 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001708 ExprResult RebuildChooseExpr(SourceLocation BuiltinLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001709 Expr *Cond, Expr *LHS, Expr *RHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001710 SourceLocation RParenLoc) {
1711 return SemaRef.ActOnChooseExpr(BuiltinLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001712 Cond, LHS, RHS,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001713 RParenLoc);
1714 }
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Peter Collingbournef111d932011-04-15 00:35:48 +00001716 /// \brief Build a new generic selection expression.
1717 ///
1718 /// By default, performs semantic analysis to build the new expression.
1719 /// Subclasses may override this routine to provide different behavior.
1720 ExprResult RebuildGenericSelectionExpr(SourceLocation KeyLoc,
1721 SourceLocation DefaultLoc,
1722 SourceLocation RParenLoc,
1723 Expr *ControllingExpr,
1724 TypeSourceInfo **Types,
1725 Expr **Exprs,
1726 unsigned NumAssocs) {
1727 return getSema().CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc,
1728 ControllingExpr, Types, Exprs,
1729 NumAssocs);
1730 }
1731
Douglas Gregorb98b1992009-08-11 05:31:07 +00001732 /// \brief Build a new overloaded operator call expression.
1733 ///
1734 /// By default, performs semantic analysis to build the new expression.
1735 /// The semantic analysis provides the behavior of template instantiation,
1736 /// copying with transformations that turn what looks like an overloaded
Mike Stump1eb44332009-09-09 15:08:12 +00001737 /// operator call into a use of a builtin operator, performing
Douglas Gregorb98b1992009-08-11 05:31:07 +00001738 /// argument-dependent lookup, etc. Subclasses may override this routine to
1739 /// provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001740 ExprResult RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001741 SourceLocation OpLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001742 Expr *Callee,
1743 Expr *First,
1744 Expr *Second);
Mike Stump1eb44332009-09-09 15:08:12 +00001745
1746 /// \brief Build a new C++ "named" cast expression, such as static_cast or
Douglas Gregorb98b1992009-08-11 05:31:07 +00001747 /// reinterpret_cast.
1748 ///
1749 /// By default, this routine dispatches to one of the more-specific routines
Mike Stump1eb44332009-09-09 15:08:12 +00001750 /// for a particular named case, e.g., RebuildCXXStaticCastExpr().
Douglas Gregorb98b1992009-08-11 05:31:07 +00001751 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001752 ExprResult RebuildCXXNamedCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001753 Stmt::StmtClass Class,
1754 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001755 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001756 SourceLocation RAngleLoc,
1757 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001758 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001759 SourceLocation RParenLoc) {
1760 switch (Class) {
1761 case Stmt::CXXStaticCastExprClass:
John McCall9d125032010-01-15 18:39:57 +00001762 return getDerived().RebuildCXXStaticCastExpr(OpLoc, LAngleLoc, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001763 RAngleLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001764 SubExpr, RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001765
1766 case Stmt::CXXDynamicCastExprClass:
John McCall9d125032010-01-15 18:39:57 +00001767 return getDerived().RebuildCXXDynamicCastExpr(OpLoc, LAngleLoc, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001768 RAngleLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001769 SubExpr, RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Douglas Gregorb98b1992009-08-11 05:31:07 +00001771 case Stmt::CXXReinterpretCastExprClass:
John McCall9d125032010-01-15 18:39:57 +00001772 return getDerived().RebuildCXXReinterpretCastExpr(OpLoc, LAngleLoc, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001773 RAngleLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001774 SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001775 RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Douglas Gregorb98b1992009-08-11 05:31:07 +00001777 case Stmt::CXXConstCastExprClass:
John McCall9d125032010-01-15 18:39:57 +00001778 return getDerived().RebuildCXXConstCastExpr(OpLoc, LAngleLoc, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001779 RAngleLoc, LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001780 SubExpr, RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Douglas Gregorb98b1992009-08-11 05:31:07 +00001782 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001783 llvm_unreachable("Invalid C++ named cast");
Douglas Gregorb98b1992009-08-11 05:31:07 +00001784 }
Douglas Gregorb98b1992009-08-11 05:31:07 +00001785 }
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Douglas Gregorb98b1992009-08-11 05:31:07 +00001787 /// \brief Build a new C++ static_cast expression.
1788 ///
1789 /// By default, performs semantic analysis to build the new expression.
1790 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001791 ExprResult RebuildCXXStaticCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001792 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001793 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001794 SourceLocation RAngleLoc,
1795 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001796 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001797 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +00001798 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_static_cast,
John McCall9ae2f072010-08-23 23:25:46 +00001799 TInfo, SubExpr,
John McCallc89724c2010-01-15 19:13:16 +00001800 SourceRange(LAngleLoc, RAngleLoc),
1801 SourceRange(LParenLoc, RParenLoc));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001802 }
1803
1804 /// \brief Build a new C++ dynamic_cast expression.
1805 ///
1806 /// By default, performs semantic analysis to build the new expression.
1807 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001808 ExprResult RebuildCXXDynamicCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001809 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001810 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001811 SourceLocation RAngleLoc,
1812 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001813 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001814 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +00001815 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_dynamic_cast,
John McCall9ae2f072010-08-23 23:25:46 +00001816 TInfo, SubExpr,
John McCallc89724c2010-01-15 19:13:16 +00001817 SourceRange(LAngleLoc, RAngleLoc),
1818 SourceRange(LParenLoc, RParenLoc));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001819 }
1820
1821 /// \brief Build a new C++ reinterpret_cast expression.
1822 ///
1823 /// By default, performs semantic analysis to build the new expression.
1824 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001825 ExprResult RebuildCXXReinterpretCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001826 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001827 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001828 SourceLocation RAngleLoc,
1829 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001830 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001831 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +00001832 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_reinterpret_cast,
John McCall9ae2f072010-08-23 23:25:46 +00001833 TInfo, SubExpr,
John McCallc89724c2010-01-15 19:13:16 +00001834 SourceRange(LAngleLoc, RAngleLoc),
1835 SourceRange(LParenLoc, RParenLoc));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001836 }
1837
1838 /// \brief Build a new C++ const_cast expression.
1839 ///
1840 /// By default, performs semantic analysis to build the new expression.
1841 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001842 ExprResult RebuildCXXConstCastExpr(SourceLocation OpLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001843 SourceLocation LAngleLoc,
John McCall9d125032010-01-15 18:39:57 +00001844 TypeSourceInfo *TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001845 SourceLocation RAngleLoc,
1846 SourceLocation LParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001847 Expr *SubExpr,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001848 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +00001849 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_const_cast,
John McCall9ae2f072010-08-23 23:25:46 +00001850 TInfo, SubExpr,
John McCallc89724c2010-01-15 19:13:16 +00001851 SourceRange(LAngleLoc, RAngleLoc),
1852 SourceRange(LParenLoc, RParenLoc));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001853 }
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Douglas Gregorb98b1992009-08-11 05:31:07 +00001855 /// \brief Build a new C++ functional-style cast expression.
1856 ///
1857 /// By default, performs semantic analysis to build the new expression.
1858 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorab6677e2010-09-08 00:15:04 +00001859 ExprResult RebuildCXXFunctionalCastExpr(TypeSourceInfo *TInfo,
1860 SourceLocation LParenLoc,
1861 Expr *Sub,
1862 SourceLocation RParenLoc) {
1863 return getSema().BuildCXXTypeConstructExpr(TInfo, LParenLoc,
John McCallf312b1e2010-08-26 23:41:50 +00001864 MultiExprArg(&Sub, 1),
Douglas Gregorb98b1992009-08-11 05:31:07 +00001865 RParenLoc);
1866 }
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Douglas Gregorb98b1992009-08-11 05:31:07 +00001868 /// \brief Build a new C++ typeid(type) expression.
1869 ///
1870 /// By default, performs semantic analysis to build the new expression.
1871 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001872 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00001873 SourceLocation TypeidLoc,
1874 TypeSourceInfo *Operand,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001875 SourceLocation RParenLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00001876 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00001877 RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001878 }
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Francois Pichet01b7c302010-09-08 12:20:18 +00001880
Douglas Gregorb98b1992009-08-11 05:31:07 +00001881 /// \brief Build a new C++ typeid(expr) expression.
1882 ///
1883 /// By default, performs semantic analysis to build the new expression.
1884 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001885 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00001886 SourceLocation TypeidLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001887 Expr *Operand,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001888 SourceLocation RParenLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00001889 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00001890 RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001891 }
1892
Francois Pichet01b7c302010-09-08 12:20:18 +00001893 /// \brief Build a new C++ __uuidof(type) expression.
1894 ///
1895 /// By default, performs semantic analysis to build the new expression.
1896 /// Subclasses may override this routine to provide different behavior.
1897 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1898 SourceLocation TypeidLoc,
1899 TypeSourceInfo *Operand,
1900 SourceLocation RParenLoc) {
1901 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1902 RParenLoc);
1903 }
1904
1905 /// \brief Build a new C++ __uuidof(expr) expression.
1906 ///
1907 /// By default, performs semantic analysis to build the new expression.
1908 /// Subclasses may override this routine to provide different behavior.
1909 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1910 SourceLocation TypeidLoc,
1911 Expr *Operand,
1912 SourceLocation RParenLoc) {
1913 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1914 RParenLoc);
1915 }
1916
Douglas Gregorb98b1992009-08-11 05:31:07 +00001917 /// \brief Build a new C++ "this" expression.
1918 ///
1919 /// By default, builds a new "this" expression without performing any
Mike Stump1eb44332009-09-09 15:08:12 +00001920 /// semantic analysis. Subclasses may override this routine to provide
Douglas Gregorb98b1992009-08-11 05:31:07 +00001921 /// different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001922 ExprResult RebuildCXXThisExpr(SourceLocation ThisLoc,
Douglas Gregorba48d6a2010-09-09 16:55:46 +00001923 QualType ThisType,
1924 bool isImplicit) {
Eli Friedmanb69b42c2012-01-11 02:36:31 +00001925 getSema().CheckCXXThisCapture(ThisLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001926 return getSema().Owned(
Douglas Gregor828a1972010-01-07 23:12:05 +00001927 new (getSema().Context) CXXThisExpr(ThisLoc, ThisType,
1928 isImplicit));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001929 }
1930
1931 /// \brief Build a new C++ throw expression.
1932 ///
1933 /// By default, performs semantic analysis to build the new expression.
1934 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorbca01b42011-07-06 22:04:06 +00001935 ExprResult RebuildCXXThrowExpr(SourceLocation ThrowLoc, Expr *Sub,
1936 bool IsThrownVariableInScope) {
1937 return getSema().BuildCXXThrow(ThrowLoc, Sub, IsThrownVariableInScope);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001938 }
1939
1940 /// \brief Build a new C++ default-argument expression.
1941 ///
1942 /// By default, builds a new default-argument expression, which does not
1943 /// require any semantic analysis. Subclasses may override this routine to
1944 /// provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001945 ExprResult RebuildCXXDefaultArgExpr(SourceLocation Loc,
Douglas Gregor036aed12009-12-23 23:03:06 +00001946 ParmVarDecl *Param) {
1947 return getSema().Owned(CXXDefaultArgExpr::Create(getSema().Context, Loc,
1948 Param));
Douglas Gregorb98b1992009-08-11 05:31:07 +00001949 }
1950
1951 /// \brief Build a new C++ zero-initialization expression.
1952 ///
1953 /// By default, performs semantic analysis to build the new expression.
1954 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorab6677e2010-09-08 00:15:04 +00001955 ExprResult RebuildCXXScalarValueInitExpr(TypeSourceInfo *TSInfo,
1956 SourceLocation LParenLoc,
1957 SourceLocation RParenLoc) {
1958 return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001959 MultiExprArg(getSema(), 0, 0),
Douglas Gregorab6677e2010-09-08 00:15:04 +00001960 RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001961 }
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Douglas Gregorb98b1992009-08-11 05:31:07 +00001963 /// \brief Build a new C++ "new" expression.
1964 ///
1965 /// By default, performs semantic analysis to build the new expression.
1966 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001967 ExprResult RebuildCXXNewExpr(SourceLocation StartLoc,
Douglas Gregor1bb2a932010-09-07 21:49:58 +00001968 bool UseGlobal,
1969 SourceLocation PlacementLParen,
1970 MultiExprArg PlacementArgs,
1971 SourceLocation PlacementRParen,
1972 SourceRange TypeIdParens,
1973 QualType AllocatedType,
1974 TypeSourceInfo *AllocatedTypeInfo,
1975 Expr *ArraySize,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00001976 SourceRange DirectInitRange,
1977 Expr *Initializer) {
Mike Stump1eb44332009-09-09 15:08:12 +00001978 return getSema().BuildCXXNew(StartLoc, UseGlobal,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001979 PlacementLParen,
1980 move(PlacementArgs),
1981 PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001982 TypeIdParens,
Douglas Gregor1bb2a932010-09-07 21:49:58 +00001983 AllocatedType,
1984 AllocatedTypeInfo,
John McCall9ae2f072010-08-23 23:25:46 +00001985 ArraySize,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00001986 DirectInitRange,
1987 Initializer);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001988 }
Mike Stump1eb44332009-09-09 15:08:12 +00001989
Douglas Gregorb98b1992009-08-11 05:31:07 +00001990 /// \brief Build a new C++ "delete" expression.
1991 ///
1992 /// By default, performs semantic analysis to build the new expression.
1993 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00001994 ExprResult RebuildCXXDeleteExpr(SourceLocation StartLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00001995 bool IsGlobalDelete,
1996 bool IsArrayForm,
John McCall9ae2f072010-08-23 23:25:46 +00001997 Expr *Operand) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001998 return getSema().ActOnCXXDelete(StartLoc, IsGlobalDelete, IsArrayForm,
John McCall9ae2f072010-08-23 23:25:46 +00001999 Operand);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002000 }
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Douglas Gregorb98b1992009-08-11 05:31:07 +00002002 /// \brief Build a new unary type trait expression.
2003 ///
2004 /// By default, performs semantic analysis to build the new expression.
2005 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002006 ExprResult RebuildUnaryTypeTrait(UnaryTypeTrait Trait,
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002007 SourceLocation StartLoc,
2008 TypeSourceInfo *T,
2009 SourceLocation RParenLoc) {
2010 return getSema().BuildUnaryTypeTrait(Trait, StartLoc, T, RParenLoc);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002011 }
2012
Francois Pichet6ad6f282010-12-07 00:08:36 +00002013 /// \brief Build a new binary type trait expression.
2014 ///
2015 /// By default, performs semantic analysis to build the new expression.
2016 /// Subclasses may override this routine to provide different behavior.
2017 ExprResult RebuildBinaryTypeTrait(BinaryTypeTrait Trait,
2018 SourceLocation StartLoc,
2019 TypeSourceInfo *LhsT,
2020 TypeSourceInfo *RhsT,
2021 SourceLocation RParenLoc) {
2022 return getSema().BuildBinaryTypeTrait(Trait, StartLoc, LhsT, RhsT, RParenLoc);
2023 }
2024
John Wiegley21ff2e52011-04-28 00:16:57 +00002025 /// \brief Build a new array type trait expression.
2026 ///
2027 /// By default, performs semantic analysis to build the new expression.
2028 /// Subclasses may override this routine to provide different behavior.
2029 ExprResult RebuildArrayTypeTrait(ArrayTypeTrait Trait,
2030 SourceLocation StartLoc,
2031 TypeSourceInfo *TSInfo,
2032 Expr *DimExpr,
2033 SourceLocation RParenLoc) {
2034 return getSema().BuildArrayTypeTrait(Trait, StartLoc, TSInfo, DimExpr, RParenLoc);
2035 }
2036
John Wiegley55262202011-04-25 06:54:41 +00002037 /// \brief Build a new expression trait expression.
2038 ///
2039 /// By default, performs semantic analysis to build the new expression.
2040 /// Subclasses may override this routine to provide different behavior.
2041 ExprResult RebuildExpressionTrait(ExpressionTrait Trait,
2042 SourceLocation StartLoc,
2043 Expr *Queried,
2044 SourceLocation RParenLoc) {
2045 return getSema().BuildExpressionTrait(Trait, StartLoc, Queried, RParenLoc);
2046 }
2047
Mike Stump1eb44332009-09-09 15:08:12 +00002048 /// \brief Build a new (previously unresolved) declaration reference
Douglas Gregorb98b1992009-08-11 05:31:07 +00002049 /// expression.
2050 ///
2051 /// By default, performs semantic analysis to build the new expression.
2052 /// Subclasses may override this routine to provide different behavior.
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002053 ExprResult RebuildDependentScopeDeclRefExpr(
2054 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002055 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002056 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002057 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002058 CXXScopeSpec SS;
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002059 SS.Adopt(QualifierLoc);
John McCallf7a1a742009-11-24 19:00:30 +00002060
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002061 if (TemplateArgs || TemplateKWLoc.isValid())
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002062 return getSema().BuildQualifiedTemplateIdExpr(SS, TemplateKWLoc,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002063 NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +00002064
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002065 return getSema().BuildQualifiedDeclarationNameExpr(SS, NameInfo);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002066 }
2067
2068 /// \brief Build a new template-id expression.
2069 ///
2070 /// By default, performs semantic analysis to build the new expression.
2071 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002072 ExprResult RebuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002073 SourceLocation TemplateKWLoc,
2074 LookupResult &R,
2075 bool RequiresADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002076 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002077 return getSema().BuildTemplateIdExpr(SS, TemplateKWLoc, R, RequiresADL,
2078 TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002079 }
2080
2081 /// \brief Build a new object-construction expression.
2082 ///
2083 /// By default, performs semantic analysis to build the new expression.
2084 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002085 ExprResult RebuildCXXConstructExpr(QualType T,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00002086 SourceLocation Loc,
2087 CXXConstructorDecl *Constructor,
2088 bool IsElidable,
2089 MultiExprArg Args,
2090 bool HadMultipleCandidates,
2091 bool RequiresZeroInit,
Chandler Carruth428edaf2010-10-25 08:47:36 +00002092 CXXConstructExpr::ConstructionKind ConstructKind,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00002093 SourceRange ParenRange) {
John McCallca0408f2010-08-23 06:44:23 +00002094 ASTOwningVector<Expr*> ConvertedArgs(SemaRef);
Sean Huntc3021132010-05-05 15:23:54 +00002095 if (getSema().CompleteConstructorCall(Constructor, move(Args), Loc,
Douglas Gregor4411d2e2009-12-14 16:27:04 +00002096 ConvertedArgs))
John McCallf312b1e2010-08-26 23:41:50 +00002097 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00002098
Douglas Gregor4411d2e2009-12-14 16:27:04 +00002099 return getSema().BuildCXXConstructExpr(Loc, T, Constructor, IsElidable,
Douglas Gregor8c3e5542010-08-22 17:20:18 +00002100 move_arg(ConvertedArgs),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00002101 HadMultipleCandidates,
Chandler Carruth428edaf2010-10-25 08:47:36 +00002102 RequiresZeroInit, ConstructKind,
2103 ParenRange);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002104 }
2105
2106 /// \brief Build a new object-construction expression.
2107 ///
2108 /// By default, performs semantic analysis to build the new expression.
2109 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorab6677e2010-09-08 00:15:04 +00002110 ExprResult RebuildCXXTemporaryObjectExpr(TypeSourceInfo *TSInfo,
2111 SourceLocation LParenLoc,
2112 MultiExprArg Args,
2113 SourceLocation RParenLoc) {
2114 return getSema().BuildCXXTypeConstructExpr(TSInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002115 LParenLoc,
2116 move(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00002117 RParenLoc);
2118 }
2119
2120 /// \brief Build a new object-construction expression.
2121 ///
2122 /// By default, performs semantic analysis to build the new expression.
2123 /// Subclasses may override this routine to provide different behavior.
Douglas Gregorab6677e2010-09-08 00:15:04 +00002124 ExprResult RebuildCXXUnresolvedConstructExpr(TypeSourceInfo *TSInfo,
2125 SourceLocation LParenLoc,
2126 MultiExprArg Args,
2127 SourceLocation RParenLoc) {
2128 return getSema().BuildCXXTypeConstructExpr(TSInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002129 LParenLoc,
2130 move(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00002131 RParenLoc);
2132 }
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Douglas Gregorb98b1992009-08-11 05:31:07 +00002134 /// \brief Build a new member reference expression.
2135 ///
2136 /// By default, performs semantic analysis to build the new expression.
2137 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002138 ExprResult RebuildCXXDependentScopeMemberExpr(Expr *BaseE,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002139 QualType BaseType,
2140 bool IsArrow,
2141 SourceLocation OperatorLoc,
2142 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002143 SourceLocation TemplateKWLoc,
John McCall129e2df2009-11-30 22:42:35 +00002144 NamedDecl *FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00002145 const DeclarationNameInfo &MemberNameInfo,
John McCall129e2df2009-11-30 22:42:35 +00002146 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002147 CXXScopeSpec SS;
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002148 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002149
John McCall9ae2f072010-08-23 23:25:46 +00002150 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
John McCallaa81e162009-12-01 22:10:20 +00002151 OperatorLoc, IsArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002152 SS, TemplateKWLoc,
2153 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00002154 MemberNameInfo,
2155 TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00002156 }
2157
John McCall129e2df2009-11-30 22:42:35 +00002158 /// \brief Build a new member reference expression.
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00002159 ///
2160 /// By default, performs semantic analysis to build the new expression.
2161 /// Subclasses may override this routine to provide different behavior.
Richard Smith9138b4e2011-10-26 19:06:56 +00002162 ExprResult RebuildUnresolvedMemberExpr(Expr *BaseE, QualType BaseType,
2163 SourceLocation OperatorLoc,
2164 bool IsArrow,
2165 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002166 SourceLocation TemplateKWLoc,
Richard Smith9138b4e2011-10-26 19:06:56 +00002167 NamedDecl *FirstQualifierInScope,
2168 LookupResult &R,
John McCall129e2df2009-11-30 22:42:35 +00002169 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00002170 CXXScopeSpec SS;
Douglas Gregor4c9be892011-02-28 20:01:57 +00002171 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002172
John McCall9ae2f072010-08-23 23:25:46 +00002173 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
John McCallaa81e162009-12-01 22:10:20 +00002174 OperatorLoc, IsArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002175 SS, TemplateKWLoc,
2176 FirstQualifierInScope,
John McCallc2233c52010-01-15 08:34:02 +00002177 R, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00002178 }
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Sebastian Redl2e156222010-09-10 20:55:43 +00002180 /// \brief Build a new noexcept expression.
2181 ///
2182 /// By default, performs semantic analysis to build the new expression.
2183 /// Subclasses may override this routine to provide different behavior.
2184 ExprResult RebuildCXXNoexceptExpr(SourceRange Range, Expr *Arg) {
2185 return SemaRef.BuildCXXNoexceptExpr(Range.getBegin(), Arg, Range.getEnd());
2186 }
2187
Douglas Gregoree8aff02011-01-04 17:33:58 +00002188 /// \brief Build a new expression to compute the length of a parameter pack.
2189 ExprResult RebuildSizeOfPackExpr(SourceLocation OperatorLoc, NamedDecl *Pack,
2190 SourceLocation PackLoc,
2191 SourceLocation RParenLoc,
Douglas Gregor089e8932011-10-10 18:59:29 +00002192 llvm::Optional<unsigned> Length) {
2193 if (Length)
2194 return new (SemaRef.Context) SizeOfPackExpr(SemaRef.Context.getSizeType(),
2195 OperatorLoc, Pack, PackLoc,
2196 RParenLoc, *Length);
2197
Douglas Gregoree8aff02011-01-04 17:33:58 +00002198 return new (SemaRef.Context) SizeOfPackExpr(SemaRef.Context.getSizeType(),
2199 OperatorLoc, Pack, PackLoc,
Douglas Gregor089e8932011-10-10 18:59:29 +00002200 RParenLoc);
Douglas Gregoree8aff02011-01-04 17:33:58 +00002201 }
2202
Douglas Gregorb98b1992009-08-11 05:31:07 +00002203 /// \brief Build a new Objective-C @encode expression.
2204 ///
2205 /// By default, performs semantic analysis to build the new expression.
2206 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002207 ExprResult RebuildObjCEncodeExpr(SourceLocation AtLoc,
Douglas Gregor81d34662010-04-20 15:39:42 +00002208 TypeSourceInfo *EncodeTypeInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002209 SourceLocation RParenLoc) {
Douglas Gregor81d34662010-04-20 15:39:42 +00002210 return SemaRef.Owned(SemaRef.BuildObjCEncodeExpression(AtLoc, EncodeTypeInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00002211 RParenLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00002212 }
Douglas Gregorb98b1992009-08-11 05:31:07 +00002213
Douglas Gregor92e986e2010-04-22 16:44:27 +00002214 /// \brief Build a new Objective-C class message.
John McCall60d7b3a2010-08-24 06:29:42 +00002215 ExprResult RebuildObjCMessageExpr(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002216 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002217 ArrayRef<SourceLocation> SelectorLocs,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002218 ObjCMethodDecl *Method,
Sean Huntc3021132010-05-05 15:23:54 +00002219 SourceLocation LBracLoc,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002220 MultiExprArg Args,
2221 SourceLocation RBracLoc) {
Douglas Gregor92e986e2010-04-22 16:44:27 +00002222 return SemaRef.BuildClassMessage(ReceiverTypeInfo,
2223 ReceiverTypeInfo->getType(),
2224 /*SuperLoc=*/SourceLocation(),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002225 Sel, Method, LBracLoc, SelectorLocs,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002226 RBracLoc, move(Args));
Douglas Gregor92e986e2010-04-22 16:44:27 +00002227 }
2228
2229 /// \brief Build a new Objective-C instance message.
John McCall60d7b3a2010-08-24 06:29:42 +00002230 ExprResult RebuildObjCMessageExpr(Expr *Receiver,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002231 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002232 ArrayRef<SourceLocation> SelectorLocs,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002233 ObjCMethodDecl *Method,
Sean Huntc3021132010-05-05 15:23:54 +00002234 SourceLocation LBracLoc,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002235 MultiExprArg Args,
2236 SourceLocation RBracLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00002237 return SemaRef.BuildInstanceMessage(Receiver,
2238 Receiver->getType(),
Douglas Gregor92e986e2010-04-22 16:44:27 +00002239 /*SuperLoc=*/SourceLocation(),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002240 Sel, Method, LBracLoc, SelectorLocs,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002241 RBracLoc, move(Args));
Douglas Gregor92e986e2010-04-22 16:44:27 +00002242 }
2243
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002244 /// \brief Build a new Objective-C ivar reference expression.
2245 ///
2246 /// By default, performs semantic analysis to build the new expression.
2247 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002248 ExprResult RebuildObjCIvarRefExpr(Expr *BaseArg, ObjCIvarDecl *Ivar,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002249 SourceLocation IvarLoc,
2250 bool IsArrow, bool IsFreeIvar) {
2251 // FIXME: We lose track of the IsFreeIvar bit.
2252 CXXScopeSpec SS;
John Wiegley429bb272011-04-08 18:41:53 +00002253 ExprResult Base = getSema().Owned(BaseArg);
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002254 LookupResult R(getSema(), Ivar->getDeclName(), IvarLoc,
2255 Sema::LookupMemberName);
John McCall60d7b3a2010-08-24 06:29:42 +00002256 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002257 /*FIME:*/IvarLoc,
John McCalld226f652010-08-21 09:40:31 +00002258 SS, 0,
John McCallad00b772010-06-16 08:42:20 +00002259 false);
John Wiegley429bb272011-04-08 18:41:53 +00002260 if (Result.isInvalid() || Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00002261 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00002262
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002263 if (Result.get())
2264 return move(Result);
Sean Huntc3021132010-05-05 15:23:54 +00002265
John Wiegley429bb272011-04-08 18:41:53 +00002266 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002267 /*FIXME:*/IvarLoc, IsArrow,
2268 SS, SourceLocation(),
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002269 /*FirstQualifierInScope=*/0,
Sean Huntc3021132010-05-05 15:23:54 +00002270 R,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002271 /*TemplateArgs=*/0);
2272 }
Douglas Gregore3303542010-04-26 20:47:02 +00002273
2274 /// \brief Build a new Objective-C property reference expression.
2275 ///
2276 /// By default, performs semantic analysis to build the new expression.
2277 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002278 ExprResult RebuildObjCPropertyRefExpr(Expr *BaseArg,
John McCall3c3b7f92011-10-25 17:37:35 +00002279 ObjCPropertyDecl *Property,
2280 SourceLocation PropertyLoc) {
Douglas Gregore3303542010-04-26 20:47:02 +00002281 CXXScopeSpec SS;
John Wiegley429bb272011-04-08 18:41:53 +00002282 ExprResult Base = getSema().Owned(BaseArg);
Douglas Gregore3303542010-04-26 20:47:02 +00002283 LookupResult R(getSema(), Property->getDeclName(), PropertyLoc,
2284 Sema::LookupMemberName);
2285 bool IsArrow = false;
John McCall60d7b3a2010-08-24 06:29:42 +00002286 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
Douglas Gregore3303542010-04-26 20:47:02 +00002287 /*FIME:*/PropertyLoc,
John McCalld226f652010-08-21 09:40:31 +00002288 SS, 0, false);
John Wiegley429bb272011-04-08 18:41:53 +00002289 if (Result.isInvalid() || Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00002290 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00002291
Douglas Gregore3303542010-04-26 20:47:02 +00002292 if (Result.get())
2293 return move(Result);
Sean Huntc3021132010-05-05 15:23:54 +00002294
John Wiegley429bb272011-04-08 18:41:53 +00002295 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
Sean Huntc3021132010-05-05 15:23:54 +00002296 /*FIXME:*/PropertyLoc, IsArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002297 SS, SourceLocation(),
Douglas Gregore3303542010-04-26 20:47:02 +00002298 /*FirstQualifierInScope=*/0,
Sean Huntc3021132010-05-05 15:23:54 +00002299 R,
Douglas Gregore3303542010-04-26 20:47:02 +00002300 /*TemplateArgs=*/0);
2301 }
Sean Huntc3021132010-05-05 15:23:54 +00002302
John McCall12f78a62010-12-02 01:19:52 +00002303 /// \brief Build a new Objective-C property reference expression.
Douglas Gregor9cbfdd22010-04-26 21:04:54 +00002304 ///
2305 /// By default, performs semantic analysis to build the new expression.
John McCall12f78a62010-12-02 01:19:52 +00002306 /// Subclasses may override this routine to provide different behavior.
2307 ExprResult RebuildObjCPropertyRefExpr(Expr *Base, QualType T,
2308 ObjCMethodDecl *Getter,
2309 ObjCMethodDecl *Setter,
2310 SourceLocation PropertyLoc) {
2311 // Since these expressions can only be value-dependent, we do not
2312 // need to perform semantic analysis again.
2313 return Owned(
2314 new (getSema().Context) ObjCPropertyRefExpr(Getter, Setter, T,
2315 VK_LValue, OK_ObjCProperty,
2316 PropertyLoc, Base));
Douglas Gregor9cbfdd22010-04-26 21:04:54 +00002317 }
2318
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002319 /// \brief Build a new Objective-C "isa" expression.
2320 ///
2321 /// By default, performs semantic analysis to build the new expression.
2322 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002323 ExprResult RebuildObjCIsaExpr(Expr *BaseArg, SourceLocation IsaLoc,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002324 bool IsArrow) {
2325 CXXScopeSpec SS;
John Wiegley429bb272011-04-08 18:41:53 +00002326 ExprResult Base = getSema().Owned(BaseArg);
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002327 LookupResult R(getSema(), &getSema().Context.Idents.get("isa"), IsaLoc,
2328 Sema::LookupMemberName);
John McCall60d7b3a2010-08-24 06:29:42 +00002329 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002330 /*FIME:*/IsaLoc,
John McCalld226f652010-08-21 09:40:31 +00002331 SS, 0, false);
John Wiegley429bb272011-04-08 18:41:53 +00002332 if (Result.isInvalid() || Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00002333 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00002334
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002335 if (Result.get())
2336 return move(Result);
Sean Huntc3021132010-05-05 15:23:54 +00002337
John Wiegley429bb272011-04-08 18:41:53 +00002338 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002339 /*FIXME:*/IsaLoc, IsArrow,
2340 SS, SourceLocation(),
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002341 /*FirstQualifierInScope=*/0,
Sean Huntc3021132010-05-05 15:23:54 +00002342 R,
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00002343 /*TemplateArgs=*/0);
2344 }
Sean Huntc3021132010-05-05 15:23:54 +00002345
Douglas Gregorb98b1992009-08-11 05:31:07 +00002346 /// \brief Build a new shuffle vector expression.
2347 ///
2348 /// By default, performs semantic analysis to build the new expression.
2349 /// Subclasses may override this routine to provide different behavior.
John McCall60d7b3a2010-08-24 06:29:42 +00002350 ExprResult RebuildShuffleVectorExpr(SourceLocation BuiltinLoc,
John McCallf89e55a2010-11-18 06:31:45 +00002351 MultiExprArg SubExprs,
2352 SourceLocation RParenLoc) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002353 // Find the declaration for __builtin_shufflevector
Mike Stump1eb44332009-09-09 15:08:12 +00002354 const IdentifierInfo &Name
Douglas Gregorb98b1992009-08-11 05:31:07 +00002355 = SemaRef.Context.Idents.get("__builtin_shufflevector");
2356 TranslationUnitDecl *TUDecl = SemaRef.Context.getTranslationUnitDecl();
2357 DeclContext::lookup_result Lookup = TUDecl->lookup(DeclarationName(&Name));
2358 assert(Lookup.first != Lookup.second && "No __builtin_shufflevector?");
Mike Stump1eb44332009-09-09 15:08:12 +00002359
Douglas Gregorb98b1992009-08-11 05:31:07 +00002360 // Build a reference to the __builtin_shufflevector builtin
2361 FunctionDecl *Builtin = cast<FunctionDecl>(*Lookup.first);
John Wiegley429bb272011-04-08 18:41:53 +00002362 ExprResult Callee
2363 = SemaRef.Owned(new (SemaRef.Context) DeclRefExpr(Builtin, Builtin->getType(),
2364 VK_LValue, BuiltinLoc));
2365 Callee = SemaRef.UsualUnaryConversions(Callee.take());
2366 if (Callee.isInvalid())
2367 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00002368
2369 // Build the CallExpr
Douglas Gregorb98b1992009-08-11 05:31:07 +00002370 unsigned NumSubExprs = SubExprs.size();
2371 Expr **Subs = (Expr **)SubExprs.release();
John Wiegley429bb272011-04-08 18:41:53 +00002372 ExprResult TheCall = SemaRef.Owned(
2373 new (SemaRef.Context) CallExpr(SemaRef.Context, Callee.take(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00002374 Subs, NumSubExprs,
Douglas Gregor5291c3c2010-07-13 08:18:22 +00002375 Builtin->getCallResultType(),
John McCallf89e55a2010-11-18 06:31:45 +00002376 Expr::getValueKindForType(Builtin->getResultType()),
John Wiegley429bb272011-04-08 18:41:53 +00002377 RParenLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Douglas Gregorb98b1992009-08-11 05:31:07 +00002379 // Type-check the __builtin_shufflevector expression.
John Wiegley429bb272011-04-08 18:41:53 +00002380 return SemaRef.SemaBuiltinShuffleVector(cast<CallExpr>(TheCall.take()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00002381 }
John McCall43fed0d2010-11-12 08:19:04 +00002382
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002383 /// \brief Build a new template argument pack expansion.
2384 ///
2385 /// By default, performs semantic analysis to build a new pack expansion
2386 /// for a template argument. Subclasses may override this routine to provide
2387 /// different behavior.
2388 TemplateArgumentLoc RebuildPackExpansion(TemplateArgumentLoc Pattern,
Douglas Gregorcded4f62011-01-14 17:04:44 +00002389 SourceLocation EllipsisLoc,
2390 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002391 switch (Pattern.getArgument().getKind()) {
Douglas Gregor7a21fd42011-01-03 21:37:45 +00002392 case TemplateArgument::Expression: {
2393 ExprResult Result
Douglas Gregor67fd1252011-01-14 21:20:45 +00002394 = getSema().CheckPackExpansion(Pattern.getSourceExpression(),
2395 EllipsisLoc, NumExpansions);
Douglas Gregor7a21fd42011-01-03 21:37:45 +00002396 if (Result.isInvalid())
2397 return TemplateArgumentLoc();
2398
2399 return TemplateArgumentLoc(Result.get(), Result.get());
2400 }
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002401
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002402 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002403 return TemplateArgumentLoc(TemplateArgument(
2404 Pattern.getArgument().getAsTemplate(),
Douglas Gregor2be29f42011-01-14 23:41:42 +00002405 NumExpansions),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002406 Pattern.getTemplateQualifierLoc(),
Douglas Gregora7fc9012011-01-05 18:58:31 +00002407 Pattern.getTemplateNameLoc(),
2408 EllipsisLoc);
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002409
2410 case TemplateArgument::Null:
2411 case TemplateArgument::Integral:
2412 case TemplateArgument::Declaration:
2413 case TemplateArgument::Pack:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002414 case TemplateArgument::TemplateExpansion:
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002415 llvm_unreachable("Pack expansion pattern has no parameter packs");
2416
2417 case TemplateArgument::Type:
2418 if (TypeSourceInfo *Expansion
2419 = getSema().CheckPackExpansion(Pattern.getTypeSourceInfo(),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002420 EllipsisLoc,
2421 NumExpansions))
Douglas Gregor8491ffe2010-12-20 22:05:00 +00002422 return TemplateArgumentLoc(TemplateArgument(Expansion->getType()),
2423 Expansion);
2424 break;
2425 }
2426
2427 return TemplateArgumentLoc();
2428 }
2429
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002430 /// \brief Build a new expression pack expansion.
2431 ///
2432 /// By default, performs semantic analysis to build a new pack expansion
2433 /// for an expression. Subclasses may override this routine to provide
2434 /// different behavior.
Douglas Gregor67fd1252011-01-14 21:20:45 +00002435 ExprResult RebuildPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
2436 llvm::Optional<unsigned> NumExpansions) {
2437 return getSema().CheckPackExpansion(Pattern, EllipsisLoc, NumExpansions);
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002438 }
Eli Friedmandfa64ba2011-10-14 22:48:56 +00002439
2440 /// \brief Build a new atomic operation expression.
2441 ///
2442 /// By default, performs semantic analysis to build the new expression.
2443 /// Subclasses may override this routine to provide different behavior.
2444 ExprResult RebuildAtomicExpr(SourceLocation BuiltinLoc,
2445 MultiExprArg SubExprs,
2446 QualType RetTy,
2447 AtomicExpr::AtomicOp Op,
2448 SourceLocation RParenLoc) {
2449 // Just create the expression; there is not any interesting semantic
2450 // analysis here because we can't actually build an AtomicExpr until
2451 // we are sure it is semantically sound.
2452 unsigned NumSubExprs = SubExprs.size();
2453 Expr **Subs = (Expr **)SubExprs.release();
2454 return new (SemaRef.Context) AtomicExpr(BuiltinLoc, Subs,
2455 NumSubExprs, RetTy, Op,
2456 RParenLoc);
2457 }
2458
John McCall43fed0d2010-11-12 08:19:04 +00002459private:
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002460 TypeLoc TransformTypeInObjectScope(TypeLoc TL,
2461 QualType ObjectType,
2462 NamedDecl *FirstQualifierInScope,
2463 CXXScopeSpec &SS);
Douglas Gregorb71d8212011-03-02 18:32:08 +00002464
2465 TypeSourceInfo *TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
2466 QualType ObjectType,
2467 NamedDecl *FirstQualifierInScope,
2468 CXXScopeSpec &SS);
Douglas Gregor577f75a2009-08-04 16:50:30 +00002469};
Douglas Gregorb98b1992009-08-11 05:31:07 +00002470
Douglas Gregor43959a92009-08-20 07:17:43 +00002471template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00002472StmtResult TreeTransform<Derived>::TransformStmt(Stmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00002473 if (!S)
2474 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00002475
Douglas Gregor43959a92009-08-20 07:17:43 +00002476 switch (S->getStmtClass()) {
2477 case Stmt::NoStmtClass: break;
Mike Stump1eb44332009-09-09 15:08:12 +00002478
Douglas Gregor43959a92009-08-20 07:17:43 +00002479 // Transform individual statement nodes
2480#define STMT(Node, Parent) \
2481 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(S));
John McCall63c00d72011-02-09 08:16:59 +00002482#define ABSTRACT_STMT(Node)
Douglas Gregor43959a92009-08-20 07:17:43 +00002483#define EXPR(Node, Parent)
Sean Hunt4bfe1962010-05-05 15:24:00 +00002484#include "clang/AST/StmtNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +00002485
Douglas Gregor43959a92009-08-20 07:17:43 +00002486 // Transform expressions by calling TransformExpr.
2487#define STMT(Node, Parent)
Sean Hunt7381d5c2010-05-18 06:22:21 +00002488#define ABSTRACT_STMT(Stmt)
Douglas Gregor43959a92009-08-20 07:17:43 +00002489#define EXPR(Node, Parent) case Stmt::Node##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002490#include "clang/AST/StmtNodes.inc"
Douglas Gregor43959a92009-08-20 07:17:43 +00002491 {
John McCall60d7b3a2010-08-24 06:29:42 +00002492 ExprResult E = getDerived().TransformExpr(cast<Expr>(S));
Douglas Gregor43959a92009-08-20 07:17:43 +00002493 if (E.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00002494 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00002495
John McCall9ae2f072010-08-23 23:25:46 +00002496 return getSema().ActOnExprStmt(getSema().MakeFullExpr(E.take()));
Douglas Gregor43959a92009-08-20 07:17:43 +00002497 }
Mike Stump1eb44332009-09-09 15:08:12 +00002498 }
2499
John McCall3fa5cae2010-10-26 07:05:15 +00002500 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00002501}
Mike Stump1eb44332009-09-09 15:08:12 +00002502
2503
Douglas Gregor670444e2009-08-04 22:27:00 +00002504template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00002505ExprResult TreeTransform<Derived>::TransformExpr(Expr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002506 if (!E)
2507 return SemaRef.Owned(E);
2508
2509 switch (E->getStmtClass()) {
2510 case Stmt::NoStmtClass: break;
2511#define STMT(Node, Parent) case Stmt::Node##Class: break;
Sean Hunt7381d5c2010-05-18 06:22:21 +00002512#define ABSTRACT_STMT(Stmt)
Douglas Gregorb98b1992009-08-11 05:31:07 +00002513#define EXPR(Node, Parent) \
John McCall454feb92009-12-08 09:21:05 +00002514 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(E));
Sean Hunt4bfe1962010-05-05 15:24:00 +00002515#include "clang/AST/StmtNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +00002516 }
2517
John McCall3fa5cae2010-10-26 07:05:15 +00002518 return SemaRef.Owned(E);
Douglas Gregor657c1ac2009-08-06 22:17:10 +00002519}
2520
2521template<typename Derived>
Douglas Gregoraa165f82011-01-03 19:04:46 +00002522bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
2523 unsigned NumInputs,
2524 bool IsCall,
Chris Lattner686775d2011-07-20 06:58:45 +00002525 SmallVectorImpl<Expr *> &Outputs,
Douglas Gregoraa165f82011-01-03 19:04:46 +00002526 bool *ArgChanged) {
2527 for (unsigned I = 0; I != NumInputs; ++I) {
2528 // If requested, drop call arguments that need to be dropped.
2529 if (IsCall && getDerived().DropCallArgument(Inputs[I])) {
2530 if (ArgChanged)
2531 *ArgChanged = true;
2532
2533 break;
2534 }
2535
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002536 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(Inputs[I])) {
2537 Expr *Pattern = Expansion->getPattern();
2538
Chris Lattner686775d2011-07-20 06:58:45 +00002539 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002540 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
2541 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
2542
2543 // Determine whether the set of unexpanded parameter packs can and should
2544 // be expanded.
2545 bool Expand = true;
Douglas Gregord3731192011-01-10 07:32:04 +00002546 bool RetainExpansion = false;
Douglas Gregor67fd1252011-01-14 21:20:45 +00002547 llvm::Optional<unsigned> OrigNumExpansions
2548 = Expansion->getNumExpansions();
2549 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002550 if (getDerived().TryExpandParameterPacks(Expansion->getEllipsisLoc(),
2551 Pattern->getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00002552 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00002553 Expand, RetainExpansion,
2554 NumExpansions))
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002555 return true;
2556
2557 if (!Expand) {
2558 // The transform has determined that we should perform a simple
2559 // transformation on the pack expansion, producing another pack
2560 // expansion.
2561 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
2562 ExprResult OutPattern = getDerived().TransformExpr(Pattern);
2563 if (OutPattern.isInvalid())
2564 return true;
2565
2566 ExprResult Out = getDerived().RebuildPackExpansion(OutPattern.get(),
Douglas Gregor67fd1252011-01-14 21:20:45 +00002567 Expansion->getEllipsisLoc(),
2568 NumExpansions);
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002569 if (Out.isInvalid())
2570 return true;
2571
2572 if (ArgChanged)
2573 *ArgChanged = true;
2574 Outputs.push_back(Out.get());
2575 continue;
2576 }
John McCallc8fc90a2011-07-06 07:30:07 +00002577
2578 // Record right away that the argument was changed. This needs
2579 // to happen even if the array expands to nothing.
2580 if (ArgChanged) *ArgChanged = true;
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002581
2582 // The transform has determined that we should perform an elementwise
2583 // expansion of the pattern. Do so.
Douglas Gregorcded4f62011-01-14 17:04:44 +00002584 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002585 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
2586 ExprResult Out = getDerived().TransformExpr(Pattern);
2587 if (Out.isInvalid())
2588 return true;
2589
Douglas Gregor77d6bb92011-01-11 22:21:24 +00002590 if (Out.get()->containsUnexpandedParameterPack()) {
Douglas Gregor67fd1252011-01-14 21:20:45 +00002591 Out = RebuildPackExpansion(Out.get(), Expansion->getEllipsisLoc(),
2592 OrigNumExpansions);
Douglas Gregor77d6bb92011-01-11 22:21:24 +00002593 if (Out.isInvalid())
2594 return true;
2595 }
2596
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002597 Outputs.push_back(Out.get());
2598 }
2599
2600 continue;
2601 }
2602
Douglas Gregoraa165f82011-01-03 19:04:46 +00002603 ExprResult Result = getDerived().TransformExpr(Inputs[I]);
2604 if (Result.isInvalid())
2605 return true;
2606
2607 if (Result.get() != Inputs[I] && ArgChanged)
2608 *ArgChanged = true;
2609
2610 Outputs.push_back(Result.get());
2611 }
2612
2613 return false;
2614}
2615
2616template<typename Derived>
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002617NestedNameSpecifierLoc
2618TreeTransform<Derived>::TransformNestedNameSpecifierLoc(
2619 NestedNameSpecifierLoc NNS,
2620 QualType ObjectType,
2621 NamedDecl *FirstQualifierInScope) {
Chris Lattner686775d2011-07-20 06:58:45 +00002622 SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002623 for (NestedNameSpecifierLoc Qualifier = NNS; Qualifier;
2624 Qualifier = Qualifier.getPrefix())
2625 Qualifiers.push_back(Qualifier);
2626
2627 CXXScopeSpec SS;
2628 while (!Qualifiers.empty()) {
2629 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
2630 NestedNameSpecifier *QNNS = Q.getNestedNameSpecifier();
2631
2632 switch (QNNS->getKind()) {
2633 case NestedNameSpecifier::Identifier:
2634 if (SemaRef.BuildCXXNestedNameSpecifier(/*Scope=*/0,
2635 *QNNS->getAsIdentifier(),
2636 Q.getLocalBeginLoc(),
2637 Q.getLocalEndLoc(),
2638 ObjectType, false, SS,
2639 FirstQualifierInScope, false))
2640 return NestedNameSpecifierLoc();
2641
2642 break;
2643
2644 case NestedNameSpecifier::Namespace: {
2645 NamespaceDecl *NS
2646 = cast_or_null<NamespaceDecl>(
2647 getDerived().TransformDecl(
2648 Q.getLocalBeginLoc(),
2649 QNNS->getAsNamespace()));
2650 SS.Extend(SemaRef.Context, NS, Q.getLocalBeginLoc(), Q.getLocalEndLoc());
2651 break;
2652 }
2653
2654 case NestedNameSpecifier::NamespaceAlias: {
2655 NamespaceAliasDecl *Alias
2656 = cast_or_null<NamespaceAliasDecl>(
2657 getDerived().TransformDecl(Q.getLocalBeginLoc(),
2658 QNNS->getAsNamespaceAlias()));
2659 SS.Extend(SemaRef.Context, Alias, Q.getLocalBeginLoc(),
2660 Q.getLocalEndLoc());
2661 break;
2662 }
2663
2664 case NestedNameSpecifier::Global:
2665 // There is no meaningful transformation that one could perform on the
2666 // global scope.
2667 SS.MakeGlobal(SemaRef.Context, Q.getBeginLoc());
2668 break;
2669
2670 case NestedNameSpecifier::TypeSpecWithTemplate:
2671 case NestedNameSpecifier::TypeSpec: {
2672 TypeLoc TL = TransformTypeInObjectScope(Q.getTypeLoc(), ObjectType,
2673 FirstQualifierInScope, SS);
2674
2675 if (!TL)
2676 return NestedNameSpecifierLoc();
2677
2678 if (TL.getType()->isDependentType() || TL.getType()->isRecordType() ||
2679 (SemaRef.getLangOptions().CPlusPlus0x &&
2680 TL.getType()->isEnumeralType())) {
2681 assert(!TL.getType().hasLocalQualifiers() &&
2682 "Can't get cv-qualifiers here");
Richard Smith95aafb22011-10-20 03:28:47 +00002683 if (TL.getType()->isEnumeralType())
2684 SemaRef.Diag(TL.getBeginLoc(),
2685 diag::warn_cxx98_compat_enum_nested_name_spec);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002686 SS.Extend(SemaRef.Context, /*FIXME:*/SourceLocation(), TL,
2687 Q.getLocalEndLoc());
2688 break;
2689 }
Richard Trieu00c93a12011-05-07 01:36:37 +00002690 // If the nested-name-specifier is an invalid type def, don't emit an
2691 // error because a previous error should have already been emitted.
2692 TypedefTypeLoc* TTL = dyn_cast<TypedefTypeLoc>(&TL);
2693 if (!TTL || !TTL->getTypedefNameDecl()->isInvalidDecl()) {
2694 SemaRef.Diag(TL.getBeginLoc(), diag::err_nested_name_spec_non_tag)
2695 << TL.getType() << SS.getRange();
2696 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002697 return NestedNameSpecifierLoc();
2698 }
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002699 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002700
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002701 // The qualifier-in-scope and object type only apply to the leftmost entity.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002702 FirstQualifierInScope = 0;
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002703 ObjectType = QualType();
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002704 }
2705
2706 // Don't rebuild the nested-name-specifier if we don't have to.
2707 if (SS.getScopeRep() == NNS.getNestedNameSpecifier() &&
2708 !getDerived().AlwaysRebuild())
2709 return NNS;
2710
2711 // If we can re-use the source-location data from the original
2712 // nested-name-specifier, do so.
2713 if (SS.location_size() == NNS.getDataLength() &&
2714 memcmp(SS.location_data(), NNS.getOpaqueData(), SS.location_size()) == 0)
2715 return NestedNameSpecifierLoc(SS.getScopeRep(), NNS.getOpaqueData());
2716
2717 // Allocate new nested-name-specifier location information.
2718 return SS.getWithLocInContext(SemaRef.Context);
2719}
2720
2721template<typename Derived>
Abramo Bagnara25777432010-08-11 22:01:17 +00002722DeclarationNameInfo
2723TreeTransform<Derived>
John McCall43fed0d2010-11-12 08:19:04 +00002724::TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002725 DeclarationName Name = NameInfo.getName();
Douglas Gregor81499bb2009-09-03 22:13:48 +00002726 if (!Name)
Abramo Bagnara25777432010-08-11 22:01:17 +00002727 return DeclarationNameInfo();
Douglas Gregor81499bb2009-09-03 22:13:48 +00002728
2729 switch (Name.getNameKind()) {
2730 case DeclarationName::Identifier:
2731 case DeclarationName::ObjCZeroArgSelector:
2732 case DeclarationName::ObjCOneArgSelector:
2733 case DeclarationName::ObjCMultiArgSelector:
2734 case DeclarationName::CXXOperatorName:
Sean Hunt3e518bd2009-11-29 07:34:05 +00002735 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor81499bb2009-09-03 22:13:48 +00002736 case DeclarationName::CXXUsingDirective:
Abramo Bagnara25777432010-08-11 22:01:17 +00002737 return NameInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00002738
Douglas Gregor81499bb2009-09-03 22:13:48 +00002739 case DeclarationName::CXXConstructorName:
2740 case DeclarationName::CXXDestructorName:
2741 case DeclarationName::CXXConversionFunctionName: {
Abramo Bagnara25777432010-08-11 22:01:17 +00002742 TypeSourceInfo *NewTInfo;
2743 CanQualType NewCanTy;
2744 if (TypeSourceInfo *OldTInfo = NameInfo.getNamedTypeInfo()) {
John McCall43fed0d2010-11-12 08:19:04 +00002745 NewTInfo = getDerived().TransformType(OldTInfo);
2746 if (!NewTInfo)
2747 return DeclarationNameInfo();
2748 NewCanTy = SemaRef.Context.getCanonicalType(NewTInfo->getType());
Abramo Bagnara25777432010-08-11 22:01:17 +00002749 }
2750 else {
2751 NewTInfo = 0;
2752 TemporaryBase Rebase(*this, NameInfo.getLoc(), Name);
John McCall43fed0d2010-11-12 08:19:04 +00002753 QualType NewT = getDerived().TransformType(Name.getCXXNameType());
Abramo Bagnara25777432010-08-11 22:01:17 +00002754 if (NewT.isNull())
2755 return DeclarationNameInfo();
2756 NewCanTy = SemaRef.Context.getCanonicalType(NewT);
2757 }
Mike Stump1eb44332009-09-09 15:08:12 +00002758
Abramo Bagnara25777432010-08-11 22:01:17 +00002759 DeclarationName NewName
2760 = SemaRef.Context.DeclarationNames.getCXXSpecialName(Name.getNameKind(),
2761 NewCanTy);
2762 DeclarationNameInfo NewNameInfo(NameInfo);
2763 NewNameInfo.setName(NewName);
2764 NewNameInfo.setNamedTypeInfo(NewTInfo);
2765 return NewNameInfo;
Douglas Gregor81499bb2009-09-03 22:13:48 +00002766 }
Mike Stump1eb44332009-09-09 15:08:12 +00002767 }
2768
David Blaikieb219cfc2011-09-23 05:06:16 +00002769 llvm_unreachable("Unknown name kind.");
Douglas Gregor81499bb2009-09-03 22:13:48 +00002770}
2771
2772template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00002773TemplateName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00002774TreeTransform<Derived>::TransformTemplateName(CXXScopeSpec &SS,
2775 TemplateName Name,
2776 SourceLocation NameLoc,
2777 QualType ObjectType,
2778 NamedDecl *FirstQualifierInScope) {
2779 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
2780 TemplateDecl *Template = QTN->getTemplateDecl();
2781 assert(Template && "qualified template name must refer to a template");
2782
2783 TemplateDecl *TransTemplate
2784 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
2785 Template));
2786 if (!TransTemplate)
2787 return TemplateName();
2788
2789 if (!getDerived().AlwaysRebuild() &&
2790 SS.getScopeRep() == QTN->getQualifier() &&
2791 TransTemplate == Template)
2792 return Name;
2793
2794 return getDerived().RebuildTemplateName(SS, QTN->hasTemplateKeyword(),
2795 TransTemplate);
2796 }
2797
2798 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2799 if (SS.getScopeRep()) {
2800 // These apply to the scope specifier, not the template.
2801 ObjectType = QualType();
2802 FirstQualifierInScope = 0;
2803 }
2804
2805 if (!getDerived().AlwaysRebuild() &&
2806 SS.getScopeRep() == DTN->getQualifier() &&
2807 ObjectType.isNull())
2808 return Name;
2809
2810 if (DTN->isIdentifier()) {
2811 return getDerived().RebuildTemplateName(SS,
2812 *DTN->getIdentifier(),
2813 NameLoc,
2814 ObjectType,
2815 FirstQualifierInScope);
2816 }
2817
2818 return getDerived().RebuildTemplateName(SS, DTN->getOperator(), NameLoc,
2819 ObjectType);
2820 }
2821
2822 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2823 TemplateDecl *TransTemplate
2824 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
2825 Template));
2826 if (!TransTemplate)
2827 return TemplateName();
2828
2829 if (!getDerived().AlwaysRebuild() &&
2830 TransTemplate == Template)
2831 return Name;
2832
2833 return TemplateName(TransTemplate);
2834 }
2835
2836 if (SubstTemplateTemplateParmPackStorage *SubstPack
2837 = Name.getAsSubstTemplateTemplateParmPack()) {
2838 TemplateTemplateParmDecl *TransParam
2839 = cast_or_null<TemplateTemplateParmDecl>(
2840 getDerived().TransformDecl(NameLoc, SubstPack->getParameterPack()));
2841 if (!TransParam)
2842 return TemplateName();
2843
2844 if (!getDerived().AlwaysRebuild() &&
2845 TransParam == SubstPack->getParameterPack())
2846 return Name;
2847
2848 return getDerived().RebuildTemplateName(TransParam,
2849 SubstPack->getArgumentPack());
2850 }
2851
2852 // These should be getting filtered out before they reach the AST.
2853 llvm_unreachable("overloaded function decl survived to here");
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00002854}
2855
2856template<typename Derived>
John McCall833ca992009-10-29 08:12:44 +00002857void TreeTransform<Derived>::InventTemplateArgumentLoc(
2858 const TemplateArgument &Arg,
2859 TemplateArgumentLoc &Output) {
2860 SourceLocation Loc = getDerived().getBaseLocation();
2861 switch (Arg.getKind()) {
2862 case TemplateArgument::Null:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002863 llvm_unreachable("null template argument in TreeTransform");
John McCall833ca992009-10-29 08:12:44 +00002864 break;
2865
2866 case TemplateArgument::Type:
2867 Output = TemplateArgumentLoc(Arg,
John McCalla93c9342009-12-07 02:54:59 +00002868 SemaRef.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
Sean Huntc3021132010-05-05 15:23:54 +00002869
John McCall833ca992009-10-29 08:12:44 +00002870 break;
2871
Douglas Gregor788cd062009-11-11 01:00:40 +00002872 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002873 case TemplateArgument::TemplateExpansion: {
2874 NestedNameSpecifierLocBuilder Builder;
2875 TemplateName Template = Arg.getAsTemplate();
2876 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
2877 Builder.MakeTrivial(SemaRef.Context, DTN->getQualifier(), Loc);
2878 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2879 Builder.MakeTrivial(SemaRef.Context, QTN->getQualifier(), Loc);
2880
2881 if (Arg.getKind() == TemplateArgument::Template)
2882 Output = TemplateArgumentLoc(Arg,
2883 Builder.getWithLocInContext(SemaRef.Context),
2884 Loc);
2885 else
2886 Output = TemplateArgumentLoc(Arg,
2887 Builder.getWithLocInContext(SemaRef.Context),
2888 Loc, Loc);
2889
Douglas Gregor788cd062009-11-11 01:00:40 +00002890 break;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002891 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00002892
John McCall833ca992009-10-29 08:12:44 +00002893 case TemplateArgument::Expression:
2894 Output = TemplateArgumentLoc(Arg, Arg.getAsExpr());
2895 break;
2896
2897 case TemplateArgument::Declaration:
2898 case TemplateArgument::Integral:
2899 case TemplateArgument::Pack:
John McCall828bff22009-10-29 18:45:58 +00002900 Output = TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002901 break;
2902 }
2903}
2904
2905template<typename Derived>
2906bool TreeTransform<Derived>::TransformTemplateArgument(
2907 const TemplateArgumentLoc &Input,
2908 TemplateArgumentLoc &Output) {
2909 const TemplateArgument &Arg = Input.getArgument();
Douglas Gregor670444e2009-08-04 22:27:00 +00002910 switch (Arg.getKind()) {
2911 case TemplateArgument::Null:
2912 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002913 Output = Input;
2914 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002915
Douglas Gregor670444e2009-08-04 22:27:00 +00002916 case TemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +00002917 TypeSourceInfo *DI = Input.getTypeSourceInfo();
John McCall833ca992009-10-29 08:12:44 +00002918 if (DI == NULL)
John McCalla93c9342009-12-07 02:54:59 +00002919 DI = InventTypeSourceInfo(Input.getArgument().getAsType());
John McCall833ca992009-10-29 08:12:44 +00002920
2921 DI = getDerived().TransformType(DI);
2922 if (!DI) return true;
2923
2924 Output = TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2925 return false;
Douglas Gregor670444e2009-08-04 22:27:00 +00002926 }
Mike Stump1eb44332009-09-09 15:08:12 +00002927
Douglas Gregor670444e2009-08-04 22:27:00 +00002928 case TemplateArgument::Declaration: {
John McCall833ca992009-10-29 08:12:44 +00002929 // FIXME: we should never have to transform one of these.
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002930 DeclarationName Name;
2931 if (NamedDecl *ND = dyn_cast<NamedDecl>(Arg.getAsDecl()))
2932 Name = ND->getDeclName();
Douglas Gregor788cd062009-11-11 01:00:40 +00002933 TemporaryBase Rebase(*this, Input.getLocation(), Name);
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002934 Decl *D = getDerived().TransformDecl(Input.getLocation(), Arg.getAsDecl());
John McCall833ca992009-10-29 08:12:44 +00002935 if (!D) return true;
2936
John McCall828bff22009-10-29 18:45:58 +00002937 Expr *SourceExpr = Input.getSourceDeclExpression();
2938 if (SourceExpr) {
2939 EnterExpressionEvaluationContext Unevaluated(getSema(),
Richard Smithf6702a32011-12-20 02:08:33 +00002940 Sema::ConstantEvaluated);
John McCall60d7b3a2010-08-24 06:29:42 +00002941 ExprResult E = getDerived().TransformExpr(SourceExpr);
John McCall9ae2f072010-08-23 23:25:46 +00002942 SourceExpr = (E.isInvalid() ? 0 : E.take());
John McCall828bff22009-10-29 18:45:58 +00002943 }
2944
2945 Output = TemplateArgumentLoc(TemplateArgument(D), SourceExpr);
John McCall833ca992009-10-29 08:12:44 +00002946 return false;
Douglas Gregor670444e2009-08-04 22:27:00 +00002947 }
Mike Stump1eb44332009-09-09 15:08:12 +00002948
Douglas Gregor788cd062009-11-11 01:00:40 +00002949 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002950 NestedNameSpecifierLoc QualifierLoc = Input.getTemplateQualifierLoc();
2951 if (QualifierLoc) {
2952 QualifierLoc = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc);
2953 if (!QualifierLoc)
2954 return true;
2955 }
2956
Douglas Gregor1d752d72011-03-02 18:46:51 +00002957 CXXScopeSpec SS;
2958 SS.Adopt(QualifierLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00002959 TemplateName Template
Douglas Gregor1d752d72011-03-02 18:46:51 +00002960 = getDerived().TransformTemplateName(SS, Arg.getAsTemplate(),
2961 Input.getTemplateNameLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +00002962 if (Template.isNull())
2963 return true;
Sean Huntc3021132010-05-05 15:23:54 +00002964
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002965 Output = TemplateArgumentLoc(TemplateArgument(Template), QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002966 Input.getTemplateNameLoc());
2967 return false;
2968 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00002969
2970 case TemplateArgument::TemplateExpansion:
2971 llvm_unreachable("Caller should expand pack expansions");
2972
Douglas Gregor670444e2009-08-04 22:27:00 +00002973 case TemplateArgument::Expression: {
Richard Smithf6702a32011-12-20 02:08:33 +00002974 // Template argument expressions are constant expressions.
Mike Stump1eb44332009-09-09 15:08:12 +00002975 EnterExpressionEvaluationContext Unevaluated(getSema(),
Richard Smithf6702a32011-12-20 02:08:33 +00002976 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00002977
John McCall833ca992009-10-29 08:12:44 +00002978 Expr *InputExpr = Input.getSourceExpression();
2979 if (!InputExpr) InputExpr = Input.getArgument().getAsExpr();
2980
Chris Lattner223de242011-04-25 20:37:58 +00002981 ExprResult E = getDerived().TransformExpr(InputExpr);
John McCall833ca992009-10-29 08:12:44 +00002982 if (E.isInvalid()) return true;
John McCall9ae2f072010-08-23 23:25:46 +00002983 Output = TemplateArgumentLoc(TemplateArgument(E.take()), E.take());
John McCall833ca992009-10-29 08:12:44 +00002984 return false;
Douglas Gregor670444e2009-08-04 22:27:00 +00002985 }
Mike Stump1eb44332009-09-09 15:08:12 +00002986
Douglas Gregor670444e2009-08-04 22:27:00 +00002987 case TemplateArgument::Pack: {
Chris Lattner686775d2011-07-20 06:58:45 +00002988 SmallVector<TemplateArgument, 4> TransformedArgs;
Douglas Gregor670444e2009-08-04 22:27:00 +00002989 TransformedArgs.reserve(Arg.pack_size());
Mike Stump1eb44332009-09-09 15:08:12 +00002990 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor670444e2009-08-04 22:27:00 +00002991 AEnd = Arg.pack_end();
2992 A != AEnd; ++A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002993
John McCall833ca992009-10-29 08:12:44 +00002994 // FIXME: preserve source information here when we start
2995 // caring about parameter packs.
2996
John McCall828bff22009-10-29 18:45:58 +00002997 TemplateArgumentLoc InputArg;
2998 TemplateArgumentLoc OutputArg;
2999 getDerived().InventTemplateArgumentLoc(*A, InputArg);
3000 if (getDerived().TransformTemplateArgument(InputArg, OutputArg))
John McCall833ca992009-10-29 08:12:44 +00003001 return true;
3002
John McCall828bff22009-10-29 18:45:58 +00003003 TransformedArgs.push_back(OutputArg.getArgument());
Douglas Gregor670444e2009-08-04 22:27:00 +00003004 }
Douglas Gregor910f8002010-11-07 23:05:16 +00003005
3006 TemplateArgument *TransformedArgsPtr
3007 = new (getSema().Context) TemplateArgument[TransformedArgs.size()];
3008 std::copy(TransformedArgs.begin(), TransformedArgs.end(),
3009 TransformedArgsPtr);
3010 Output = TemplateArgumentLoc(TemplateArgument(TransformedArgsPtr,
3011 TransformedArgs.size()),
3012 Input.getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00003013 return false;
Douglas Gregor670444e2009-08-04 22:27:00 +00003014 }
3015 }
Mike Stump1eb44332009-09-09 15:08:12 +00003016
Douglas Gregor670444e2009-08-04 22:27:00 +00003017 // Work around bogus GCC warning
John McCall833ca992009-10-29 08:12:44 +00003018 return true;
Douglas Gregor670444e2009-08-04 22:27:00 +00003019}
3020
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003021/// \brief Iterator adaptor that invents template argument location information
3022/// for each of the template arguments in its underlying iterator.
3023template<typename Derived, typename InputIterator>
3024class TemplateArgumentLocInventIterator {
3025 TreeTransform<Derived> &Self;
3026 InputIterator Iter;
3027
3028public:
3029 typedef TemplateArgumentLoc value_type;
3030 typedef TemplateArgumentLoc reference;
3031 typedef typename std::iterator_traits<InputIterator>::difference_type
3032 difference_type;
3033 typedef std::input_iterator_tag iterator_category;
3034
3035 class pointer {
3036 TemplateArgumentLoc Arg;
Douglas Gregorfcc12532010-12-20 17:31:10 +00003037
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003038 public:
3039 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
3040
3041 const TemplateArgumentLoc *operator->() const { return &Arg; }
3042 };
3043
3044 TemplateArgumentLocInventIterator() { }
3045
3046 explicit TemplateArgumentLocInventIterator(TreeTransform<Derived> &Self,
3047 InputIterator Iter)
3048 : Self(Self), Iter(Iter) { }
3049
3050 TemplateArgumentLocInventIterator &operator++() {
3051 ++Iter;
3052 return *this;
Douglas Gregorfcc12532010-12-20 17:31:10 +00003053 }
3054
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003055 TemplateArgumentLocInventIterator operator++(int) {
3056 TemplateArgumentLocInventIterator Old(*this);
3057 ++(*this);
3058 return Old;
3059 }
3060
3061 reference operator*() const {
3062 TemplateArgumentLoc Result;
3063 Self.InventTemplateArgumentLoc(*Iter, Result);
3064 return Result;
3065 }
3066
3067 pointer operator->() const { return pointer(**this); }
3068
3069 friend bool operator==(const TemplateArgumentLocInventIterator &X,
3070 const TemplateArgumentLocInventIterator &Y) {
3071 return X.Iter == Y.Iter;
3072 }
Douglas Gregorfcc12532010-12-20 17:31:10 +00003073
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003074 friend bool operator!=(const TemplateArgumentLocInventIterator &X,
3075 const TemplateArgumentLocInventIterator &Y) {
3076 return X.Iter != Y.Iter;
3077 }
3078};
3079
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00003080template<typename Derived>
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003081template<typename InputIterator>
3082bool TreeTransform<Derived>::TransformTemplateArguments(InputIterator First,
3083 InputIterator Last,
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00003084 TemplateArgumentListInfo &Outputs) {
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003085 for (; First != Last; ++First) {
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00003086 TemplateArgumentLoc Out;
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003087 TemplateArgumentLoc In = *First;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003088
3089 if (In.getArgument().getKind() == TemplateArgument::Pack) {
3090 // Unpack argument packs, which we translate them into separate
3091 // arguments.
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00003092 // FIXME: We could do much better if we could guarantee that the
3093 // TemplateArgumentLocInfo for the pack expansion would be usable for
3094 // all of the template arguments in the argument pack.
3095 typedef TemplateArgumentLocInventIterator<Derived,
3096 TemplateArgument::pack_iterator>
3097 PackLocIterator;
3098 if (TransformTemplateArguments(PackLocIterator(*this,
3099 In.getArgument().pack_begin()),
3100 PackLocIterator(*this,
3101 In.getArgument().pack_end()),
3102 Outputs))
3103 return true;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003104
3105 continue;
3106 }
3107
3108 if (In.getArgument().isPackExpansion()) {
3109 // We have a pack expansion, for which we will be substituting into
3110 // the pattern.
3111 SourceLocation Ellipsis;
Douglas Gregorcded4f62011-01-14 17:04:44 +00003112 llvm::Optional<unsigned> OrigNumExpansions;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003113 TemplateArgumentLoc Pattern
Douglas Gregorcded4f62011-01-14 17:04:44 +00003114 = In.getPackExpansionPattern(Ellipsis, OrigNumExpansions,
3115 getSema().Context);
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003116
Chris Lattner686775d2011-07-20 06:58:45 +00003117 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003118 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
3119 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
3120
3121 // Determine whether the set of unexpanded parameter packs can and should
3122 // be expanded.
3123 bool Expand = true;
Douglas Gregord3731192011-01-10 07:32:04 +00003124 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00003125 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003126 if (getDerived().TryExpandParameterPacks(Ellipsis,
3127 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003128 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00003129 Expand,
3130 RetainExpansion,
3131 NumExpansions))
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003132 return true;
3133
3134 if (!Expand) {
3135 // The transform has determined that we should perform a simple
3136 // transformation on the pack expansion, producing another pack
3137 // expansion.
3138 TemplateArgumentLoc OutPattern;
3139 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
3140 if (getDerived().TransformTemplateArgument(Pattern, OutPattern))
3141 return true;
3142
Douglas Gregorcded4f62011-01-14 17:04:44 +00003143 Out = getDerived().RebuildPackExpansion(OutPattern, Ellipsis,
3144 NumExpansions);
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003145 if (Out.getArgument().isNull())
3146 return true;
3147
3148 Outputs.addArgument(Out);
3149 continue;
3150 }
3151
3152 // The transform has determined that we should perform an elementwise
3153 // expansion of the pattern. Do so.
Douglas Gregorcded4f62011-01-14 17:04:44 +00003154 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003155 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
3156
3157 if (getDerived().TransformTemplateArgument(Pattern, Out))
3158 return true;
3159
Douglas Gregor77d6bb92011-01-11 22:21:24 +00003160 if (Out.getArgument().containsUnexpandedParameterPack()) {
Douglas Gregorcded4f62011-01-14 17:04:44 +00003161 Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
3162 OrigNumExpansions);
Douglas Gregor77d6bb92011-01-11 22:21:24 +00003163 if (Out.getArgument().isNull())
3164 return true;
3165 }
3166
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003167 Outputs.addArgument(Out);
3168 }
3169
Douglas Gregor3cae5c92011-01-10 20:53:55 +00003170 // If we're supposed to retain a pack expansion, do so by temporarily
3171 // forgetting the partially-substituted parameter pack.
3172 if (RetainExpansion) {
3173 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
3174
3175 if (getDerived().TransformTemplateArgument(Pattern, Out))
3176 return true;
3177
Douglas Gregorcded4f62011-01-14 17:04:44 +00003178 Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
3179 OrigNumExpansions);
Douglas Gregor3cae5c92011-01-10 20:53:55 +00003180 if (Out.getArgument().isNull())
3181 return true;
3182
3183 Outputs.addArgument(Out);
3184 }
Douglas Gregord3731192011-01-10 07:32:04 +00003185
Douglas Gregor8491ffe2010-12-20 22:05:00 +00003186 continue;
3187 }
3188
3189 // The simple case:
3190 if (getDerived().TransformTemplateArgument(In, Out))
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00003191 return true;
3192
3193 Outputs.addArgument(Out);
3194 }
3195
3196 return false;
3197
3198}
3199
Douglas Gregor577f75a2009-08-04 16:50:30 +00003200//===----------------------------------------------------------------------===//
3201// Type transformation
3202//===----------------------------------------------------------------------===//
3203
3204template<typename Derived>
John McCall43fed0d2010-11-12 08:19:04 +00003205QualType TreeTransform<Derived>::TransformType(QualType T) {
Douglas Gregor577f75a2009-08-04 16:50:30 +00003206 if (getDerived().AlreadyTransformed(T))
3207 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00003208
John McCalla2becad2009-10-21 00:40:46 +00003209 // Temporary workaround. All of these transformations should
3210 // eventually turn into transformations on TypeLocs.
Douglas Gregorc21c7e92011-01-25 19:13:18 +00003211 TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T,
3212 getDerived().getBaseLocation());
Sean Huntc3021132010-05-05 15:23:54 +00003213
John McCall43fed0d2010-11-12 08:19:04 +00003214 TypeSourceInfo *NewDI = getDerived().TransformType(DI);
John McCall0953e762009-09-24 19:53:00 +00003215
John McCalla2becad2009-10-21 00:40:46 +00003216 if (!NewDI)
3217 return QualType();
3218
3219 return NewDI->getType();
3220}
3221
3222template<typename Derived>
John McCall43fed0d2010-11-12 08:19:04 +00003223TypeSourceInfo *TreeTransform<Derived>::TransformType(TypeSourceInfo *DI) {
Richard Smithf6702a32011-12-20 02:08:33 +00003224 // Refine the base location to the type's location.
3225 TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(),
3226 getDerived().getBaseEntity());
John McCalla2becad2009-10-21 00:40:46 +00003227 if (getDerived().AlreadyTransformed(DI->getType()))
3228 return DI;
3229
3230 TypeLocBuilder TLB;
3231
3232 TypeLoc TL = DI->getTypeLoc();
3233 TLB.reserve(TL.getFullDataSize());
3234
John McCall43fed0d2010-11-12 08:19:04 +00003235 QualType Result = getDerived().TransformType(TLB, TL);
John McCalla2becad2009-10-21 00:40:46 +00003236 if (Result.isNull())
3237 return 0;
3238
John McCalla93c9342009-12-07 02:54:59 +00003239 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
John McCalla2becad2009-10-21 00:40:46 +00003240}
3241
3242template<typename Derived>
3243QualType
John McCall43fed0d2010-11-12 08:19:04 +00003244TreeTransform<Derived>::TransformType(TypeLocBuilder &TLB, TypeLoc T) {
John McCalla2becad2009-10-21 00:40:46 +00003245 switch (T.getTypeLocClass()) {
3246#define ABSTRACT_TYPELOC(CLASS, PARENT)
3247#define TYPELOC(CLASS, PARENT) \
3248 case TypeLoc::CLASS: \
John McCall43fed0d2010-11-12 08:19:04 +00003249 return getDerived().Transform##CLASS##Type(TLB, cast<CLASS##TypeLoc>(T));
John McCalla2becad2009-10-21 00:40:46 +00003250#include "clang/AST/TypeLocNodes.def"
Douglas Gregor577f75a2009-08-04 16:50:30 +00003251 }
Mike Stump1eb44332009-09-09 15:08:12 +00003252
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003253 llvm_unreachable("unhandled type loc!");
John McCalla2becad2009-10-21 00:40:46 +00003254}
3255
3256/// FIXME: By default, this routine adds type qualifiers only to types
3257/// that can have qualifiers, and silently suppresses those qualifiers
3258/// that are not permitted (e.g., qualifiers on reference or function
3259/// types). This is the right thing for template instantiation, but
3260/// probably not for other clients.
3261template<typename Derived>
3262QualType
3263TreeTransform<Derived>::TransformQualifiedType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003264 QualifiedTypeLoc T) {
Douglas Gregora4923eb2009-11-16 21:35:15 +00003265 Qualifiers Quals = T.getType().getLocalQualifiers();
John McCalla2becad2009-10-21 00:40:46 +00003266
John McCall43fed0d2010-11-12 08:19:04 +00003267 QualType Result = getDerived().TransformType(TLB, T.getUnqualifiedLoc());
John McCalla2becad2009-10-21 00:40:46 +00003268 if (Result.isNull())
3269 return QualType();
3270
3271 // Silently suppress qualifiers if the result type can't be qualified.
3272 // FIXME: this is the right thing for template instantiation, but
3273 // probably not for other clients.
3274 if (Result->isFunctionType() || Result->isReferenceType())
Douglas Gregor577f75a2009-08-04 16:50:30 +00003275 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00003276
John McCallf85e1932011-06-15 23:02:42 +00003277 // Suppress Objective-C lifetime qualifiers if they don't make sense for the
Douglas Gregore559ca12011-06-17 22:11:49 +00003278 // resulting type.
3279 if (Quals.hasObjCLifetime()) {
3280 if (!Result->isObjCLifetimeType() && !Result->isDependentType())
3281 Quals.removeObjCLifetime();
Douglas Gregor4020cae2011-06-17 23:16:24 +00003282 else if (Result.getObjCLifetime()) {
Douglas Gregore559ca12011-06-17 22:11:49 +00003283 // Objective-C ARC:
3284 // A lifetime qualifier applied to a substituted template parameter
3285 // overrides the lifetime qualifier from the template argument.
3286 if (const SubstTemplateTypeParmType *SubstTypeParam
3287 = dyn_cast<SubstTemplateTypeParmType>(Result)) {
3288 QualType Replacement = SubstTypeParam->getReplacementType();
3289 Qualifiers Qs = Replacement.getQualifiers();
3290 Qs.removeObjCLifetime();
3291 Replacement
3292 = SemaRef.Context.getQualifiedType(Replacement.getUnqualifiedType(),
3293 Qs);
3294 Result = SemaRef.Context.getSubstTemplateTypeParmType(
3295 SubstTypeParam->getReplacedParameter(),
3296 Replacement);
3297 TLB.TypeWasModifiedSafely(Result);
3298 } else {
Douglas Gregor4020cae2011-06-17 23:16:24 +00003299 // Otherwise, complain about the addition of a qualifier to an
3300 // already-qualified type.
3301 SourceRange R = TLB.getTemporaryTypeLoc(Result).getSourceRange();
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003302 SemaRef.Diag(R.getBegin(), diag::err_attr_objc_ownership_redundant)
Douglas Gregor4020cae2011-06-17 23:16:24 +00003303 << Result << R;
3304
Douglas Gregore559ca12011-06-17 22:11:49 +00003305 Quals.removeObjCLifetime();
3306 }
3307 }
3308 }
John McCall28654742010-06-05 06:41:15 +00003309 if (!Quals.empty()) {
3310 Result = SemaRef.BuildQualifiedType(Result, T.getBeginLoc(), Quals);
3311 TLB.push<QualifiedTypeLoc>(Result);
3312 // No location information to preserve.
3313 }
John McCalla2becad2009-10-21 00:40:46 +00003314
3315 return Result;
3316}
3317
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003318template<typename Derived>
3319TypeLoc
3320TreeTransform<Derived>::TransformTypeInObjectScope(TypeLoc TL,
3321 QualType ObjectType,
3322 NamedDecl *UnqualLookup,
3323 CXXScopeSpec &SS) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003324 QualType T = TL.getType();
3325 if (getDerived().AlreadyTransformed(T))
3326 return TL;
3327
3328 TypeLocBuilder TLB;
3329 QualType Result;
3330
3331 if (isa<TemplateSpecializationType>(T)) {
3332 TemplateSpecializationTypeLoc SpecTL
3333 = cast<TemplateSpecializationTypeLoc>(TL);
3334
3335 TemplateName Template =
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00003336 getDerived().TransformTemplateName(SS,
3337 SpecTL.getTypePtr()->getTemplateName(),
3338 SpecTL.getTemplateNameLoc(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003339 ObjectType, UnqualLookup);
3340 if (Template.isNull())
3341 return TypeLoc();
3342
3343 Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
3344 Template);
3345 } else if (isa<DependentTemplateSpecializationType>(T)) {
3346 DependentTemplateSpecializationTypeLoc SpecTL
3347 = cast<DependentTemplateSpecializationTypeLoc>(TL);
3348
Douglas Gregora88f09f2011-02-28 17:23:35 +00003349 TemplateName Template
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00003350 = getDerived().RebuildTemplateName(SS,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00003351 *SpecTL.getTypePtr()->getIdentifier(),
Abramo Bagnara55d23c92012-02-06 14:41:24 +00003352 SpecTL.getTemplateNameLoc(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00003353 ObjectType, UnqualLookup);
Douglas Gregora88f09f2011-02-28 17:23:35 +00003354 if (Template.isNull())
3355 return TypeLoc();
3356
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003357 Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
Douglas Gregora88f09f2011-02-28 17:23:35 +00003358 SpecTL,
Douglas Gregor087eb5a2011-03-04 18:53:13 +00003359 Template,
3360 SS);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003361 } else {
3362 // Nothing special needs to be done for these.
3363 Result = getDerived().TransformType(TLB, TL);
3364 }
3365
3366 if (Result.isNull())
3367 return TypeLoc();
3368
3369 return TLB.getTypeSourceInfo(SemaRef.Context, Result)->getTypeLoc();
3370}
3371
Douglas Gregorb71d8212011-03-02 18:32:08 +00003372template<typename Derived>
3373TypeSourceInfo *
3374TreeTransform<Derived>::TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
3375 QualType ObjectType,
3376 NamedDecl *UnqualLookup,
3377 CXXScopeSpec &SS) {
3378 // FIXME: Painfully copy-paste from the above!
3379
3380 QualType T = TSInfo->getType();
3381 if (getDerived().AlreadyTransformed(T))
3382 return TSInfo;
3383
3384 TypeLocBuilder TLB;
3385 QualType Result;
3386
3387 TypeLoc TL = TSInfo->getTypeLoc();
3388 if (isa<TemplateSpecializationType>(T)) {
3389 TemplateSpecializationTypeLoc SpecTL
3390 = cast<TemplateSpecializationTypeLoc>(TL);
3391
3392 TemplateName Template
3393 = getDerived().TransformTemplateName(SS,
3394 SpecTL.getTypePtr()->getTemplateName(),
3395 SpecTL.getTemplateNameLoc(),
3396 ObjectType, UnqualLookup);
3397 if (Template.isNull())
3398 return 0;
3399
3400 Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
3401 Template);
3402 } else if (isa<DependentTemplateSpecializationType>(T)) {
3403 DependentTemplateSpecializationTypeLoc SpecTL
3404 = cast<DependentTemplateSpecializationTypeLoc>(TL);
3405
3406 TemplateName Template
3407 = getDerived().RebuildTemplateName(SS,
3408 *SpecTL.getTypePtr()->getIdentifier(),
Abramo Bagnara55d23c92012-02-06 14:41:24 +00003409 SpecTL.getTemplateNameLoc(),
Douglas Gregorb71d8212011-03-02 18:32:08 +00003410 ObjectType, UnqualLookup);
3411 if (Template.isNull())
3412 return 0;
3413
3414 Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
3415 SpecTL,
Douglas Gregor087eb5a2011-03-04 18:53:13 +00003416 Template,
3417 SS);
Douglas Gregorb71d8212011-03-02 18:32:08 +00003418 } else {
3419 // Nothing special needs to be done for these.
3420 Result = getDerived().TransformType(TLB, TL);
3421 }
3422
3423 if (Result.isNull())
3424 return 0;
3425
3426 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
3427}
3428
John McCalla2becad2009-10-21 00:40:46 +00003429template <class TyLoc> static inline
3430QualType TransformTypeSpecType(TypeLocBuilder &TLB, TyLoc T) {
3431 TyLoc NewT = TLB.push<TyLoc>(T.getType());
3432 NewT.setNameLoc(T.getNameLoc());
3433 return T.getType();
3434}
3435
John McCalla2becad2009-10-21 00:40:46 +00003436template<typename Derived>
3437QualType TreeTransform<Derived>::TransformBuiltinType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003438 BuiltinTypeLoc T) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00003439 BuiltinTypeLoc NewT = TLB.push<BuiltinTypeLoc>(T.getType());
3440 NewT.setBuiltinLoc(T.getBuiltinLoc());
3441 if (T.needsExtraLocalData())
3442 NewT.getWrittenBuiltinSpecs() = T.getWrittenBuiltinSpecs();
3443 return T.getType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00003444}
Mike Stump1eb44332009-09-09 15:08:12 +00003445
Douglas Gregor577f75a2009-08-04 16:50:30 +00003446template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00003447QualType TreeTransform<Derived>::TransformComplexType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003448 ComplexTypeLoc T) {
John McCalla2becad2009-10-21 00:40:46 +00003449 // FIXME: recurse?
3450 return TransformTypeSpecType(TLB, T);
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>::TransformPointerType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003455 PointerTypeLoc TL) {
Sean Huntc3021132010-05-05 15:23:54 +00003456 QualType PointeeType
3457 = getDerived().TransformType(TLB, TL.getPointeeLoc());
Douglas Gregor92e986e2010-04-22 16:44:27 +00003458 if (PointeeType.isNull())
3459 return QualType();
3460
3461 QualType Result = TL.getType();
John McCallc12c5bb2010-05-15 11:32:37 +00003462 if (PointeeType->getAs<ObjCObjectType>()) {
Douglas Gregor92e986e2010-04-22 16:44:27 +00003463 // A dependent pointer type 'T *' has is being transformed such
3464 // that an Objective-C class type is being replaced for 'T'. The
3465 // resulting pointer type is an ObjCObjectPointerType, not a
3466 // PointerType.
John McCallc12c5bb2010-05-15 11:32:37 +00003467 Result = SemaRef.Context.getObjCObjectPointerType(PointeeType);
Sean Huntc3021132010-05-05 15:23:54 +00003468
John McCallc12c5bb2010-05-15 11:32:37 +00003469 ObjCObjectPointerTypeLoc NewT = TLB.push<ObjCObjectPointerTypeLoc>(Result);
3470 NewT.setStarLoc(TL.getStarLoc());
Douglas Gregor92e986e2010-04-22 16:44:27 +00003471 return Result;
3472 }
John McCall43fed0d2010-11-12 08:19:04 +00003473
Douglas Gregor92e986e2010-04-22 16:44:27 +00003474 if (getDerived().AlwaysRebuild() ||
3475 PointeeType != TL.getPointeeLoc().getType()) {
3476 Result = getDerived().RebuildPointerType(PointeeType, TL.getSigilLoc());
3477 if (Result.isNull())
3478 return QualType();
3479 }
John McCallf85e1932011-06-15 23:02:42 +00003480
3481 // Objective-C ARC can add lifetime qualifiers to the type that we're
3482 // pointing to.
3483 TLB.TypeWasModifiedSafely(Result->getPointeeType());
3484
Douglas Gregor92e986e2010-04-22 16:44:27 +00003485 PointerTypeLoc NewT = TLB.push<PointerTypeLoc>(Result);
3486 NewT.setSigilLoc(TL.getSigilLoc());
Sean Huntc3021132010-05-05 15:23:54 +00003487 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003488}
Mike Stump1eb44332009-09-09 15:08:12 +00003489
3490template<typename Derived>
3491QualType
John McCalla2becad2009-10-21 00:40:46 +00003492TreeTransform<Derived>::TransformBlockPointerType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003493 BlockPointerTypeLoc TL) {
Douglas Gregordb93c4a2010-04-22 16:46:21 +00003494 QualType PointeeType
Sean Huntc3021132010-05-05 15:23:54 +00003495 = getDerived().TransformType(TLB, TL.getPointeeLoc());
3496 if (PointeeType.isNull())
3497 return QualType();
3498
3499 QualType Result = TL.getType();
3500 if (getDerived().AlwaysRebuild() ||
3501 PointeeType != TL.getPointeeLoc().getType()) {
3502 Result = getDerived().RebuildBlockPointerType(PointeeType,
Douglas Gregordb93c4a2010-04-22 16:46:21 +00003503 TL.getSigilLoc());
3504 if (Result.isNull())
3505 return QualType();
3506 }
3507
Douglas Gregor39968ad2010-04-22 16:50:51 +00003508 BlockPointerTypeLoc NewT = TLB.push<BlockPointerTypeLoc>(Result);
Douglas Gregordb93c4a2010-04-22 16:46:21 +00003509 NewT.setSigilLoc(TL.getSigilLoc());
3510 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003511}
3512
John McCall85737a72009-10-30 00:06:24 +00003513/// Transforms a reference type. Note that somewhat paradoxically we
3514/// don't care whether the type itself is an l-value type or an r-value
3515/// type; we only care if the type was *written* as an l-value type
3516/// or an r-value type.
3517template<typename Derived>
3518QualType
3519TreeTransform<Derived>::TransformReferenceType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003520 ReferenceTypeLoc TL) {
John McCall85737a72009-10-30 00:06:24 +00003521 const ReferenceType *T = TL.getTypePtr();
3522
3523 // Note that this works with the pointee-as-written.
3524 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
3525 if (PointeeType.isNull())
3526 return QualType();
3527
3528 QualType Result = TL.getType();
3529 if (getDerived().AlwaysRebuild() ||
3530 PointeeType != T->getPointeeTypeAsWritten()) {
3531 Result = getDerived().RebuildReferenceType(PointeeType,
3532 T->isSpelledAsLValue(),
3533 TL.getSigilLoc());
3534 if (Result.isNull())
3535 return QualType();
3536 }
3537
John McCallf85e1932011-06-15 23:02:42 +00003538 // Objective-C ARC can add lifetime qualifiers to the type that we're
3539 // referring to.
3540 TLB.TypeWasModifiedSafely(
3541 Result->getAs<ReferenceType>()->getPointeeTypeAsWritten());
3542
John McCall85737a72009-10-30 00:06:24 +00003543 // r-value references can be rebuilt as l-value references.
3544 ReferenceTypeLoc NewTL;
3545 if (isa<LValueReferenceType>(Result))
3546 NewTL = TLB.push<LValueReferenceTypeLoc>(Result);
3547 else
3548 NewTL = TLB.push<RValueReferenceTypeLoc>(Result);
3549 NewTL.setSigilLoc(TL.getSigilLoc());
3550
3551 return Result;
3552}
3553
Mike Stump1eb44332009-09-09 15:08:12 +00003554template<typename Derived>
3555QualType
John McCalla2becad2009-10-21 00:40:46 +00003556TreeTransform<Derived>::TransformLValueReferenceType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003557 LValueReferenceTypeLoc TL) {
3558 return TransformReferenceType(TLB, TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +00003559}
3560
Mike Stump1eb44332009-09-09 15:08:12 +00003561template<typename Derived>
3562QualType
John McCalla2becad2009-10-21 00:40:46 +00003563TreeTransform<Derived>::TransformRValueReferenceType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003564 RValueReferenceTypeLoc TL) {
3565 return TransformReferenceType(TLB, TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +00003566}
Mike Stump1eb44332009-09-09 15:08:12 +00003567
Douglas Gregor577f75a2009-08-04 16:50:30 +00003568template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00003569QualType
John McCalla2becad2009-10-21 00:40:46 +00003570TreeTransform<Derived>::TransformMemberPointerType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003571 MemberPointerTypeLoc TL) {
John McCalla2becad2009-10-21 00:40:46 +00003572 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
Douglas Gregor577f75a2009-08-04 16:50:30 +00003573 if (PointeeType.isNull())
3574 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003575
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003576 TypeSourceInfo* OldClsTInfo = TL.getClassTInfo();
3577 TypeSourceInfo* NewClsTInfo = 0;
3578 if (OldClsTInfo) {
3579 NewClsTInfo = getDerived().TransformType(OldClsTInfo);
3580 if (!NewClsTInfo)
3581 return QualType();
3582 }
3583
3584 const MemberPointerType *T = TL.getTypePtr();
3585 QualType OldClsType = QualType(T->getClass(), 0);
3586 QualType NewClsType;
3587 if (NewClsTInfo)
3588 NewClsType = NewClsTInfo->getType();
3589 else {
3590 NewClsType = getDerived().TransformType(OldClsType);
3591 if (NewClsType.isNull())
3592 return QualType();
3593 }
Mike Stump1eb44332009-09-09 15:08:12 +00003594
John McCalla2becad2009-10-21 00:40:46 +00003595 QualType Result = TL.getType();
3596 if (getDerived().AlwaysRebuild() ||
3597 PointeeType != T->getPointeeType() ||
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003598 NewClsType != OldClsType) {
3599 Result = getDerived().RebuildMemberPointerType(PointeeType, NewClsType,
John McCall85737a72009-10-30 00:06:24 +00003600 TL.getStarLoc());
John McCalla2becad2009-10-21 00:40:46 +00003601 if (Result.isNull())
3602 return QualType();
3603 }
Douglas Gregor577f75a2009-08-04 16:50:30 +00003604
John McCalla2becad2009-10-21 00:40:46 +00003605 MemberPointerTypeLoc NewTL = TLB.push<MemberPointerTypeLoc>(Result);
3606 NewTL.setSigilLoc(TL.getSigilLoc());
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003607 NewTL.setClassTInfo(NewClsTInfo);
John McCalla2becad2009-10-21 00:40:46 +00003608
3609 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003610}
3611
Mike Stump1eb44332009-09-09 15:08:12 +00003612template<typename Derived>
3613QualType
John McCalla2becad2009-10-21 00:40:46 +00003614TreeTransform<Derived>::TransformConstantArrayType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003615 ConstantArrayTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003616 const ConstantArrayType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003617 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
Douglas Gregor577f75a2009-08-04 16:50:30 +00003618 if (ElementType.isNull())
3619 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003620
John McCalla2becad2009-10-21 00:40:46 +00003621 QualType Result = TL.getType();
3622 if (getDerived().AlwaysRebuild() ||
3623 ElementType != T->getElementType()) {
3624 Result = getDerived().RebuildConstantArrayType(ElementType,
3625 T->getSizeModifier(),
3626 T->getSize(),
John McCall85737a72009-10-30 00:06:24 +00003627 T->getIndexTypeCVRQualifiers(),
3628 TL.getBracketsRange());
John McCalla2becad2009-10-21 00:40:46 +00003629 if (Result.isNull())
3630 return QualType();
3631 }
Eli Friedman457a3772012-01-25 22:19:07 +00003632
3633 // We might have either a ConstantArrayType or a VariableArrayType now:
3634 // a ConstantArrayType is allowed to have an element type which is a
3635 // VariableArrayType if the type is dependent. Fortunately, all array
3636 // types have the same location layout.
3637 ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
John McCalla2becad2009-10-21 00:40:46 +00003638 NewTL.setLBracketLoc(TL.getLBracketLoc());
3639 NewTL.setRBracketLoc(TL.getRBracketLoc());
Mike Stump1eb44332009-09-09 15:08:12 +00003640
John McCalla2becad2009-10-21 00:40:46 +00003641 Expr *Size = TL.getSizeExpr();
3642 if (Size) {
Richard Smithf6702a32011-12-20 02:08:33 +00003643 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3644 Sema::ConstantEvaluated);
John McCalla2becad2009-10-21 00:40:46 +00003645 Size = getDerived().TransformExpr(Size).template takeAs<Expr>();
3646 }
3647 NewTL.setSizeExpr(Size);
3648
3649 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003650}
Mike Stump1eb44332009-09-09 15:08:12 +00003651
Douglas Gregor577f75a2009-08-04 16:50:30 +00003652template<typename Derived>
Douglas Gregor577f75a2009-08-04 16:50:30 +00003653QualType TreeTransform<Derived>::TransformIncompleteArrayType(
John McCalla2becad2009-10-21 00:40:46 +00003654 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003655 IncompleteArrayTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003656 const IncompleteArrayType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003657 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
Douglas Gregor577f75a2009-08-04 16:50:30 +00003658 if (ElementType.isNull())
3659 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003660
John McCalla2becad2009-10-21 00:40:46 +00003661 QualType Result = TL.getType();
3662 if (getDerived().AlwaysRebuild() ||
3663 ElementType != T->getElementType()) {
3664 Result = getDerived().RebuildIncompleteArrayType(ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00003665 T->getSizeModifier(),
John McCall85737a72009-10-30 00:06:24 +00003666 T->getIndexTypeCVRQualifiers(),
3667 TL.getBracketsRange());
John McCalla2becad2009-10-21 00:40:46 +00003668 if (Result.isNull())
3669 return QualType();
3670 }
Sean Huntc3021132010-05-05 15:23:54 +00003671
John McCalla2becad2009-10-21 00:40:46 +00003672 IncompleteArrayTypeLoc NewTL = TLB.push<IncompleteArrayTypeLoc>(Result);
3673 NewTL.setLBracketLoc(TL.getLBracketLoc());
3674 NewTL.setRBracketLoc(TL.getRBracketLoc());
3675 NewTL.setSizeExpr(0);
3676
3677 return Result;
3678}
3679
3680template<typename Derived>
3681QualType
3682TreeTransform<Derived>::TransformVariableArrayType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003683 VariableArrayTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003684 const VariableArrayType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003685 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3686 if (ElementType.isNull())
3687 return QualType();
3688
John McCall60d7b3a2010-08-24 06:29:42 +00003689 ExprResult SizeResult
John McCalla2becad2009-10-21 00:40:46 +00003690 = getDerived().TransformExpr(T->getSizeExpr());
3691 if (SizeResult.isInvalid())
3692 return QualType();
3693
John McCall9ae2f072010-08-23 23:25:46 +00003694 Expr *Size = SizeResult.take();
John McCalla2becad2009-10-21 00:40:46 +00003695
3696 QualType Result = TL.getType();
3697 if (getDerived().AlwaysRebuild() ||
3698 ElementType != T->getElementType() ||
3699 Size != T->getSizeExpr()) {
3700 Result = getDerived().RebuildVariableArrayType(ElementType,
3701 T->getSizeModifier(),
John McCall9ae2f072010-08-23 23:25:46 +00003702 Size,
John McCalla2becad2009-10-21 00:40:46 +00003703 T->getIndexTypeCVRQualifiers(),
John McCall85737a72009-10-30 00:06:24 +00003704 TL.getBracketsRange());
John McCalla2becad2009-10-21 00:40:46 +00003705 if (Result.isNull())
3706 return QualType();
3707 }
Sean Huntc3021132010-05-05 15:23:54 +00003708
John McCalla2becad2009-10-21 00:40:46 +00003709 VariableArrayTypeLoc NewTL = TLB.push<VariableArrayTypeLoc>(Result);
3710 NewTL.setLBracketLoc(TL.getLBracketLoc());
3711 NewTL.setRBracketLoc(TL.getRBracketLoc());
3712 NewTL.setSizeExpr(Size);
3713
3714 return Result;
3715}
3716
3717template<typename Derived>
3718QualType
3719TreeTransform<Derived>::TransformDependentSizedArrayType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003720 DependentSizedArrayTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003721 const DependentSizedArrayType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003722 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3723 if (ElementType.isNull())
3724 return QualType();
3725
Richard Smithf6702a32011-12-20 02:08:33 +00003726 // Array bounds are constant expressions.
3727 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3728 Sema::ConstantEvaluated);
John McCalla2becad2009-10-21 00:40:46 +00003729
John McCall3b657512011-01-19 10:06:00 +00003730 // Prefer the expression from the TypeLoc; the other may have been uniqued.
3731 Expr *origSize = TL.getSizeExpr();
3732 if (!origSize) origSize = T->getSizeExpr();
3733
3734 ExprResult sizeResult
3735 = getDerived().TransformExpr(origSize);
3736 if (sizeResult.isInvalid())
John McCalla2becad2009-10-21 00:40:46 +00003737 return QualType();
3738
John McCall3b657512011-01-19 10:06:00 +00003739 Expr *size = sizeResult.get();
John McCalla2becad2009-10-21 00:40:46 +00003740
3741 QualType Result = TL.getType();
3742 if (getDerived().AlwaysRebuild() ||
3743 ElementType != T->getElementType() ||
John McCall3b657512011-01-19 10:06:00 +00003744 size != origSize) {
John McCalla2becad2009-10-21 00:40:46 +00003745 Result = getDerived().RebuildDependentSizedArrayType(ElementType,
3746 T->getSizeModifier(),
John McCall3b657512011-01-19 10:06:00 +00003747 size,
John McCalla2becad2009-10-21 00:40:46 +00003748 T->getIndexTypeCVRQualifiers(),
John McCall85737a72009-10-30 00:06:24 +00003749 TL.getBracketsRange());
John McCalla2becad2009-10-21 00:40:46 +00003750 if (Result.isNull())
3751 return QualType();
3752 }
John McCalla2becad2009-10-21 00:40:46 +00003753
3754 // We might have any sort of array type now, but fortunately they
3755 // all have the same location layout.
3756 ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
3757 NewTL.setLBracketLoc(TL.getLBracketLoc());
3758 NewTL.setRBracketLoc(TL.getRBracketLoc());
John McCall3b657512011-01-19 10:06:00 +00003759 NewTL.setSizeExpr(size);
John McCalla2becad2009-10-21 00:40:46 +00003760
3761 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003762}
Mike Stump1eb44332009-09-09 15:08:12 +00003763
3764template<typename Derived>
Douglas Gregor577f75a2009-08-04 16:50:30 +00003765QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType(
John McCalla2becad2009-10-21 00:40:46 +00003766 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003767 DependentSizedExtVectorTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003768 const DependentSizedExtVectorType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003769
3770 // FIXME: ext vector locs should be nested
Douglas Gregor577f75a2009-08-04 16:50:30 +00003771 QualType ElementType = getDerived().TransformType(T->getElementType());
3772 if (ElementType.isNull())
3773 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003774
Richard Smithf6702a32011-12-20 02:08:33 +00003775 // Vector sizes are constant expressions.
3776 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3777 Sema::ConstantEvaluated);
Douglas Gregor670444e2009-08-04 22:27:00 +00003778
John McCall60d7b3a2010-08-24 06:29:42 +00003779 ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
Douglas Gregor577f75a2009-08-04 16:50:30 +00003780 if (Size.isInvalid())
3781 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003782
John McCalla2becad2009-10-21 00:40:46 +00003783 QualType Result = TL.getType();
3784 if (getDerived().AlwaysRebuild() ||
John McCalleee91c32009-10-23 17:55:45 +00003785 ElementType != T->getElementType() ||
3786 Size.get() != T->getSizeExpr()) {
John McCalla2becad2009-10-21 00:40:46 +00003787 Result = getDerived().RebuildDependentSizedExtVectorType(ElementType,
John McCall9ae2f072010-08-23 23:25:46 +00003788 Size.take(),
Douglas Gregor577f75a2009-08-04 16:50:30 +00003789 T->getAttributeLoc());
John McCalla2becad2009-10-21 00:40:46 +00003790 if (Result.isNull())
3791 return QualType();
3792 }
John McCalla2becad2009-10-21 00:40:46 +00003793
3794 // Result might be dependent or not.
3795 if (isa<DependentSizedExtVectorType>(Result)) {
3796 DependentSizedExtVectorTypeLoc NewTL
3797 = TLB.push<DependentSizedExtVectorTypeLoc>(Result);
3798 NewTL.setNameLoc(TL.getNameLoc());
3799 } else {
3800 ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
3801 NewTL.setNameLoc(TL.getNameLoc());
3802 }
3803
3804 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003805}
Mike Stump1eb44332009-09-09 15:08:12 +00003806
3807template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00003808QualType TreeTransform<Derived>::TransformVectorType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003809 VectorTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003810 const VectorType *T = TL.getTypePtr();
Douglas Gregor577f75a2009-08-04 16:50:30 +00003811 QualType ElementType = getDerived().TransformType(T->getElementType());
3812 if (ElementType.isNull())
3813 return QualType();
3814
John McCalla2becad2009-10-21 00:40:46 +00003815 QualType Result = TL.getType();
3816 if (getDerived().AlwaysRebuild() ||
3817 ElementType != T->getElementType()) {
John Thompson82287d12010-02-05 00:12:22 +00003818 Result = getDerived().RebuildVectorType(ElementType, T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00003819 T->getVectorKind());
John McCalla2becad2009-10-21 00:40:46 +00003820 if (Result.isNull())
3821 return QualType();
3822 }
Sean Huntc3021132010-05-05 15:23:54 +00003823
John McCalla2becad2009-10-21 00:40:46 +00003824 VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result);
3825 NewTL.setNameLoc(TL.getNameLoc());
Mike Stump1eb44332009-09-09 15:08:12 +00003826
John McCalla2becad2009-10-21 00:40:46 +00003827 return Result;
3828}
3829
3830template<typename Derived>
3831QualType TreeTransform<Derived>::TransformExtVectorType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00003832 ExtVectorTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00003833 const VectorType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00003834 QualType ElementType = getDerived().TransformType(T->getElementType());
3835 if (ElementType.isNull())
3836 return QualType();
3837
3838 QualType Result = TL.getType();
3839 if (getDerived().AlwaysRebuild() ||
3840 ElementType != T->getElementType()) {
3841 Result = getDerived().RebuildExtVectorType(ElementType,
3842 T->getNumElements(),
3843 /*FIXME*/ SourceLocation());
3844 if (Result.isNull())
3845 return QualType();
3846 }
Sean Huntc3021132010-05-05 15:23:54 +00003847
John McCalla2becad2009-10-21 00:40:46 +00003848 ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
3849 NewTL.setNameLoc(TL.getNameLoc());
3850
3851 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00003852}
Mike Stump1eb44332009-09-09 15:08:12 +00003853
3854template<typename Derived>
John McCall21ef0fa2010-03-11 09:03:00 +00003855ParmVarDecl *
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003856TreeTransform<Derived>::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCallfb44de92011-05-01 22:35:37 +00003857 int indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00003858 llvm::Optional<unsigned> NumExpansions,
3859 bool ExpectParameterPack) {
John McCall21ef0fa2010-03-11 09:03:00 +00003860 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003861 TypeSourceInfo *NewDI = 0;
3862
3863 if (NumExpansions && isa<PackExpansionType>(OldDI->getType())) {
3864 // If we're substituting into a pack expansion type and we know the
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00003865 // length we want to expand to, just substitute for the pattern.
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003866 TypeLoc OldTL = OldDI->getTypeLoc();
3867 PackExpansionTypeLoc OldExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
3868
3869 TypeLocBuilder TLB;
3870 TypeLoc NewTL = OldDI->getTypeLoc();
3871 TLB.reserve(NewTL.getFullDataSize());
3872
3873 QualType Result = getDerived().TransformType(TLB,
3874 OldExpansionTL.getPatternLoc());
3875 if (Result.isNull())
3876 return 0;
3877
3878 Result = RebuildPackExpansionType(Result,
3879 OldExpansionTL.getPatternLoc().getSourceRange(),
3880 OldExpansionTL.getEllipsisLoc(),
3881 NumExpansions);
3882 if (Result.isNull())
3883 return 0;
3884
3885 PackExpansionTypeLoc NewExpansionTL
3886 = TLB.push<PackExpansionTypeLoc>(Result);
3887 NewExpansionTL.setEllipsisLoc(OldExpansionTL.getEllipsisLoc());
3888 NewDI = TLB.getTypeSourceInfo(SemaRef.Context, Result);
3889 } else
3890 NewDI = getDerived().TransformType(OldDI);
John McCall21ef0fa2010-03-11 09:03:00 +00003891 if (!NewDI)
3892 return 0;
3893
John McCallfb44de92011-05-01 22:35:37 +00003894 if (NewDI == OldDI && indexAdjustment == 0)
John McCall21ef0fa2010-03-11 09:03:00 +00003895 return OldParm;
John McCallfb44de92011-05-01 22:35:37 +00003896
3897 ParmVarDecl *newParm = ParmVarDecl::Create(SemaRef.Context,
3898 OldParm->getDeclContext(),
3899 OldParm->getInnerLocStart(),
3900 OldParm->getLocation(),
3901 OldParm->getIdentifier(),
3902 NewDI->getType(),
3903 NewDI,
3904 OldParm->getStorageClass(),
3905 OldParm->getStorageClassAsWritten(),
3906 /* DefArg */ NULL);
3907 newParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
3908 OldParm->getFunctionScopeIndex() + indexAdjustment);
3909 return newParm;
John McCall21ef0fa2010-03-11 09:03:00 +00003910}
3911
3912template<typename Derived>
3913bool TreeTransform<Derived>::
Douglas Gregora009b592011-01-07 00:20:55 +00003914 TransformFunctionTypeParams(SourceLocation Loc,
3915 ParmVarDecl **Params, unsigned NumParams,
3916 const QualType *ParamTypes,
Chris Lattner686775d2011-07-20 06:58:45 +00003917 SmallVectorImpl<QualType> &OutParamTypes,
3918 SmallVectorImpl<ParmVarDecl*> *PVars) {
John McCallfb44de92011-05-01 22:35:37 +00003919 int indexAdjustment = 0;
3920
Douglas Gregora009b592011-01-07 00:20:55 +00003921 for (unsigned i = 0; i != NumParams; ++i) {
3922 if (ParmVarDecl *OldParm = Params[i]) {
John McCallfb44de92011-05-01 22:35:37 +00003923 assert(OldParm->getFunctionScopeIndex() == i);
3924
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003925 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor406f98f2011-03-02 02:04:06 +00003926 ParmVarDecl *NewParm = 0;
Douglas Gregor603cfb42011-01-05 23:12:31 +00003927 if (OldParm->isParameterPack()) {
3928 // We have a function parameter pack that may need to be expanded.
Chris Lattner686775d2011-07-20 06:58:45 +00003929 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
John McCall21ef0fa2010-03-11 09:03:00 +00003930
Douglas Gregor603cfb42011-01-05 23:12:31 +00003931 // Find the parameter packs that could be expanded.
Douglas Gregorc8a16fb2011-01-05 23:16:57 +00003932 TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc();
3933 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(TL);
3934 TypeLoc Pattern = ExpansionTL.getPatternLoc();
3935 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
Douglas Gregor406f98f2011-03-02 02:04:06 +00003936 assert(Unexpanded.size() > 0 && "Could not find parameter packs!");
3937
Douglas Gregor603cfb42011-01-05 23:12:31 +00003938 // Determine whether we should expand the parameter packs.
3939 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00003940 bool RetainExpansion = false;
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003941 llvm::Optional<unsigned> OrigNumExpansions
3942 = ExpansionTL.getTypePtr()->getNumExpansions();
3943 NumExpansions = OrigNumExpansions;
Douglas Gregorc8a16fb2011-01-05 23:16:57 +00003944 if (getDerived().TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(),
3945 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003946 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00003947 ShouldExpand,
3948 RetainExpansion,
3949 NumExpansions)) {
Douglas Gregor603cfb42011-01-05 23:12:31 +00003950 return true;
3951 }
3952
3953 if (ShouldExpand) {
3954 // Expand the function parameter pack into multiple, separate
3955 // parameters.
Douglas Gregor12c9c002011-01-07 16:43:16 +00003956 getDerived().ExpandingFunctionParameterPack(OldParm);
Douglas Gregorcded4f62011-01-14 17:04:44 +00003957 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor603cfb42011-01-05 23:12:31 +00003958 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
3959 ParmVarDecl *NewParm
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003960 = getDerived().TransformFunctionTypeParam(OldParm,
John McCallfb44de92011-05-01 22:35:37 +00003961 indexAdjustment++,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00003962 OrigNumExpansions,
3963 /*ExpectParameterPack=*/false);
Douglas Gregor603cfb42011-01-05 23:12:31 +00003964 if (!NewParm)
3965 return true;
3966
Douglas Gregora009b592011-01-07 00:20:55 +00003967 OutParamTypes.push_back(NewParm->getType());
3968 if (PVars)
3969 PVars->push_back(NewParm);
Douglas Gregor603cfb42011-01-05 23:12:31 +00003970 }
Douglas Gregord3731192011-01-10 07:32:04 +00003971
3972 // If we're supposed to retain a pack expansion, do so by temporarily
3973 // forgetting the partially-substituted parameter pack.
3974 if (RetainExpansion) {
3975 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
3976 ParmVarDecl *NewParm
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00003977 = getDerived().TransformFunctionTypeParam(OldParm,
John McCallfb44de92011-05-01 22:35:37 +00003978 indexAdjustment++,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00003979 OrigNumExpansions,
3980 /*ExpectParameterPack=*/false);
Douglas Gregord3731192011-01-10 07:32:04 +00003981 if (!NewParm)
3982 return true;
3983
3984 OutParamTypes.push_back(NewParm->getType());
3985 if (PVars)
3986 PVars->push_back(NewParm);
3987 }
3988
John McCallfb44de92011-05-01 22:35:37 +00003989 // The next parameter should have the same adjustment as the
3990 // last thing we pushed, but we post-incremented indexAdjustment
3991 // on every push. Also, if we push nothing, the adjustment should
3992 // go down by one.
3993 indexAdjustment--;
3994
Douglas Gregor603cfb42011-01-05 23:12:31 +00003995 // We're done with the pack expansion.
3996 continue;
3997 }
3998
3999 // We'll substitute the parameter now without expanding the pack
4000 // expansion.
Douglas Gregor406f98f2011-03-02 02:04:06 +00004001 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
4002 NewParm = getDerived().TransformFunctionTypeParam(OldParm,
John McCallfb44de92011-05-01 22:35:37 +00004003 indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00004004 NumExpansions,
4005 /*ExpectParameterPack=*/true);
Douglas Gregor406f98f2011-03-02 02:04:06 +00004006 } else {
4007 NewParm = getDerived().TransformFunctionTypeParam(OldParm,
John McCallfb44de92011-05-01 22:35:37 +00004008 indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00004009 llvm::Optional<unsigned>(),
4010 /*ExpectParameterPack=*/false);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004011 }
Douglas Gregor406f98f2011-03-02 02:04:06 +00004012
John McCall21ef0fa2010-03-11 09:03:00 +00004013 if (!NewParm)
4014 return true;
Douglas Gregor603cfb42011-01-05 23:12:31 +00004015
Douglas Gregora009b592011-01-07 00:20:55 +00004016 OutParamTypes.push_back(NewParm->getType());
4017 if (PVars)
4018 PVars->push_back(NewParm);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004019 continue;
4020 }
John McCall21ef0fa2010-03-11 09:03:00 +00004021
4022 // Deal with the possibility that we don't have a parameter
4023 // declaration for this parameter.
Douglas Gregora009b592011-01-07 00:20:55 +00004024 QualType OldType = ParamTypes[i];
Douglas Gregor603cfb42011-01-05 23:12:31 +00004025 bool IsPackExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00004026 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor406f98f2011-03-02 02:04:06 +00004027 QualType NewType;
Douglas Gregor603cfb42011-01-05 23:12:31 +00004028 if (const PackExpansionType *Expansion
4029 = dyn_cast<PackExpansionType>(OldType)) {
4030 // We have a function parameter pack that may need to be expanded.
4031 QualType Pattern = Expansion->getPattern();
Chris Lattner686775d2011-07-20 06:58:45 +00004032 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor603cfb42011-01-05 23:12:31 +00004033 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
4034
4035 // Determine whether we should expand the parameter packs.
4036 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00004037 bool RetainExpansion = false;
Douglas Gregora009b592011-01-07 00:20:55 +00004038 if (getDerived().TryExpandParameterPacks(Loc, SourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00004039 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00004040 ShouldExpand,
4041 RetainExpansion,
4042 NumExpansions)) {
John McCall21ef0fa2010-03-11 09:03:00 +00004043 return true;
Douglas Gregor603cfb42011-01-05 23:12:31 +00004044 }
4045
4046 if (ShouldExpand) {
4047 // Expand the function parameter pack into multiple, separate
4048 // parameters.
Douglas Gregorcded4f62011-01-14 17:04:44 +00004049 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor603cfb42011-01-05 23:12:31 +00004050 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
4051 QualType NewType = getDerived().TransformType(Pattern);
4052 if (NewType.isNull())
4053 return true;
John McCall21ef0fa2010-03-11 09:03:00 +00004054
Douglas Gregora009b592011-01-07 00:20:55 +00004055 OutParamTypes.push_back(NewType);
4056 if (PVars)
4057 PVars->push_back(0);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004058 }
4059
4060 // We're done with the pack expansion.
4061 continue;
4062 }
4063
Douglas Gregor3cae5c92011-01-10 20:53:55 +00004064 // If we're supposed to retain a pack expansion, do so by temporarily
4065 // forgetting the partially-substituted parameter pack.
4066 if (RetainExpansion) {
4067 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
4068 QualType NewType = getDerived().TransformType(Pattern);
4069 if (NewType.isNull())
4070 return true;
4071
4072 OutParamTypes.push_back(NewType);
4073 if (PVars)
4074 PVars->push_back(0);
4075 }
Douglas Gregord3731192011-01-10 07:32:04 +00004076
Douglas Gregor603cfb42011-01-05 23:12:31 +00004077 // We'll substitute the parameter now without expanding the pack
4078 // expansion.
4079 OldType = Expansion->getPattern();
4080 IsPackExpansion = true;
Douglas Gregor406f98f2011-03-02 02:04:06 +00004081 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
4082 NewType = getDerived().TransformType(OldType);
4083 } else {
4084 NewType = getDerived().TransformType(OldType);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004085 }
4086
Douglas Gregor603cfb42011-01-05 23:12:31 +00004087 if (NewType.isNull())
4088 return true;
4089
4090 if (IsPackExpansion)
Douglas Gregorcded4f62011-01-14 17:04:44 +00004091 NewType = getSema().Context.getPackExpansionType(NewType,
4092 NumExpansions);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004093
Douglas Gregora009b592011-01-07 00:20:55 +00004094 OutParamTypes.push_back(NewType);
4095 if (PVars)
4096 PVars->push_back(0);
John McCall21ef0fa2010-03-11 09:03:00 +00004097 }
4098
John McCallfb44de92011-05-01 22:35:37 +00004099#ifndef NDEBUG
4100 if (PVars) {
4101 for (unsigned i = 0, e = PVars->size(); i != e; ++i)
4102 if (ParmVarDecl *parm = (*PVars)[i])
4103 assert(parm->getFunctionScopeIndex() == i);
Douglas Gregor603cfb42011-01-05 23:12:31 +00004104 }
John McCallfb44de92011-05-01 22:35:37 +00004105#endif
4106
4107 return false;
4108}
John McCall21ef0fa2010-03-11 09:03:00 +00004109
4110template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00004111QualType
John McCalla2becad2009-10-21 00:40:46 +00004112TreeTransform<Derived>::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004113 FunctionProtoTypeLoc TL) {
Douglas Gregor7e010a02010-08-31 00:26:14 +00004114 // Transform the parameters and return type.
4115 //
4116 // We instantiate in source order, with the return type first followed by
4117 // the parameters, because users tend to expect this (even if they shouldn't
4118 // rely on it!).
4119 //
Douglas Gregordab60ad2010-10-01 18:44:50 +00004120 // When the function has a trailing return type, we instantiate the
4121 // parameters before the return type, since the return type can then refer
4122 // to the parameters themselves (via decltype, sizeof, etc.).
4123 //
Chris Lattner686775d2011-07-20 06:58:45 +00004124 SmallVector<QualType, 4> ParamTypes;
4125 SmallVector<ParmVarDecl*, 4> ParamDecls;
John McCallf4c73712011-01-19 06:33:43 +00004126 const FunctionProtoType *T = TL.getTypePtr();
Douglas Gregor7e010a02010-08-31 00:26:14 +00004127
Douglas Gregordab60ad2010-10-01 18:44:50 +00004128 QualType ResultType;
4129
4130 if (TL.getTrailingReturn()) {
Douglas Gregora009b592011-01-07 00:20:55 +00004131 if (getDerived().TransformFunctionTypeParams(TL.getBeginLoc(),
4132 TL.getParmArray(),
4133 TL.getNumArgs(),
4134 TL.getTypePtr()->arg_type_begin(),
4135 ParamTypes, &ParamDecls))
Douglas Gregordab60ad2010-10-01 18:44:50 +00004136 return QualType();
4137
4138 ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4139 if (ResultType.isNull())
4140 return QualType();
4141 }
4142 else {
4143 ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4144 if (ResultType.isNull())
4145 return QualType();
4146
Douglas Gregora009b592011-01-07 00:20:55 +00004147 if (getDerived().TransformFunctionTypeParams(TL.getBeginLoc(),
4148 TL.getParmArray(),
4149 TL.getNumArgs(),
4150 TL.getTypePtr()->arg_type_begin(),
4151 ParamTypes, &ParamDecls))
Douglas Gregordab60ad2010-10-01 18:44:50 +00004152 return QualType();
4153 }
4154
John McCalla2becad2009-10-21 00:40:46 +00004155 QualType Result = TL.getType();
4156 if (getDerived().AlwaysRebuild() ||
4157 ResultType != T->getResultType() ||
Douglas Gregorbd5f9f72011-01-07 19:27:47 +00004158 T->getNumArgs() != ParamTypes.size() ||
John McCalla2becad2009-10-21 00:40:46 +00004159 !std::equal(T->arg_type_begin(), T->arg_type_end(), ParamTypes.begin())) {
4160 Result = getDerived().RebuildFunctionProtoType(ResultType,
4161 ParamTypes.data(),
4162 ParamTypes.size(),
4163 T->isVariadic(),
Richard Smitheefb3d52012-02-10 09:58:53 +00004164 T->hasTrailingReturn(),
Eli Friedmanfa869542010-08-05 02:54:05 +00004165 T->getTypeQuals(),
Douglas Gregorc938c162011-01-26 05:01:58 +00004166 T->getRefQualifier(),
Eli Friedmanfa869542010-08-05 02:54:05 +00004167 T->getExtInfo());
John McCalla2becad2009-10-21 00:40:46 +00004168 if (Result.isNull())
4169 return QualType();
4170 }
Mike Stump1eb44332009-09-09 15:08:12 +00004171
John McCalla2becad2009-10-21 00:40:46 +00004172 FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004173 NewTL.setLocalRangeBegin(TL.getLocalRangeBegin());
4174 NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
Douglas Gregordab60ad2010-10-01 18:44:50 +00004175 NewTL.setTrailingReturn(TL.getTrailingReturn());
John McCalla2becad2009-10-21 00:40:46 +00004176 for (unsigned i = 0, e = NewTL.getNumArgs(); i != e; ++i)
4177 NewTL.setArg(i, ParamDecls[i]);
4178
4179 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004180}
Mike Stump1eb44332009-09-09 15:08:12 +00004181
Douglas Gregor577f75a2009-08-04 16:50:30 +00004182template<typename Derived>
4183QualType TreeTransform<Derived>::TransformFunctionNoProtoType(
John McCalla2becad2009-10-21 00:40:46 +00004184 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004185 FunctionNoProtoTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004186 const FunctionNoProtoType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00004187 QualType ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4188 if (ResultType.isNull())
4189 return QualType();
4190
4191 QualType Result = TL.getType();
4192 if (getDerived().AlwaysRebuild() ||
4193 ResultType != T->getResultType())
4194 Result = getDerived().RebuildFunctionNoProtoType(ResultType);
4195
4196 FunctionNoProtoTypeLoc NewTL = TLB.push<FunctionNoProtoTypeLoc>(Result);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004197 NewTL.setLocalRangeBegin(TL.getLocalRangeBegin());
4198 NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
Douglas Gregordab60ad2010-10-01 18:44:50 +00004199 NewTL.setTrailingReturn(false);
John McCalla2becad2009-10-21 00:40:46 +00004200
4201 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004202}
Mike Stump1eb44332009-09-09 15:08:12 +00004203
John McCalled976492009-12-04 22:46:56 +00004204template<typename Derived> QualType
4205TreeTransform<Derived>::TransformUnresolvedUsingType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004206 UnresolvedUsingTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004207 const UnresolvedUsingType *T = TL.getTypePtr();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004208 Decl *D = getDerived().TransformDecl(TL.getNameLoc(), T->getDecl());
John McCalled976492009-12-04 22:46:56 +00004209 if (!D)
4210 return QualType();
4211
4212 QualType Result = TL.getType();
4213 if (getDerived().AlwaysRebuild() || D != T->getDecl()) {
4214 Result = getDerived().RebuildUnresolvedUsingType(D);
4215 if (Result.isNull())
4216 return QualType();
4217 }
4218
4219 // We might get an arbitrary type spec type back. We should at
4220 // least always get a type spec type, though.
4221 TypeSpecTypeLoc NewTL = TLB.pushTypeSpec(Result);
4222 NewTL.setNameLoc(TL.getNameLoc());
4223
4224 return Result;
4225}
4226
Douglas Gregor577f75a2009-08-04 16:50:30 +00004227template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004228QualType TreeTransform<Derived>::TransformTypedefType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004229 TypedefTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004230 const TypedefType *T = TL.getTypePtr();
Richard Smith162e1c12011-04-15 14:24:37 +00004231 TypedefNameDecl *Typedef
4232 = cast_or_null<TypedefNameDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4233 T->getDecl()));
Douglas Gregor577f75a2009-08-04 16:50:30 +00004234 if (!Typedef)
4235 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004236
John McCalla2becad2009-10-21 00:40:46 +00004237 QualType Result = TL.getType();
4238 if (getDerived().AlwaysRebuild() ||
4239 Typedef != T->getDecl()) {
4240 Result = getDerived().RebuildTypedefType(Typedef);
4241 if (Result.isNull())
4242 return QualType();
4243 }
Mike Stump1eb44332009-09-09 15:08:12 +00004244
John McCalla2becad2009-10-21 00:40:46 +00004245 TypedefTypeLoc NewTL = TLB.push<TypedefTypeLoc>(Result);
4246 NewTL.setNameLoc(TL.getNameLoc());
4247
4248 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004249}
Mike Stump1eb44332009-09-09 15:08:12 +00004250
Douglas Gregor577f75a2009-08-04 16:50:30 +00004251template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004252QualType TreeTransform<Derived>::TransformTypeOfExprType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004253 TypeOfExprTypeLoc TL) {
Douglas Gregor670444e2009-08-04 22:27:00 +00004254 // typeof expressions are not potentially evaluated contexts
John McCallf312b1e2010-08-26 23:41:50 +00004255 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00004256
John McCall60d7b3a2010-08-24 06:29:42 +00004257 ExprResult E = getDerived().TransformExpr(TL.getUnderlyingExpr());
Douglas Gregor577f75a2009-08-04 16:50:30 +00004258 if (E.isInvalid())
4259 return QualType();
4260
John McCalla2becad2009-10-21 00:40:46 +00004261 QualType Result = TL.getType();
4262 if (getDerived().AlwaysRebuild() ||
John McCallcfb708c2010-01-13 20:03:27 +00004263 E.get() != TL.getUnderlyingExpr()) {
John McCall2a984ca2010-10-12 00:20:44 +00004264 Result = getDerived().RebuildTypeOfExprType(E.get(), TL.getTypeofLoc());
John McCalla2becad2009-10-21 00:40:46 +00004265 if (Result.isNull())
4266 return QualType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004267 }
John McCalla2becad2009-10-21 00:40:46 +00004268 else E.take();
Mike Stump1eb44332009-09-09 15:08:12 +00004269
John McCalla2becad2009-10-21 00:40:46 +00004270 TypeOfExprTypeLoc NewTL = TLB.push<TypeOfExprTypeLoc>(Result);
John McCallcfb708c2010-01-13 20:03:27 +00004271 NewTL.setTypeofLoc(TL.getTypeofLoc());
4272 NewTL.setLParenLoc(TL.getLParenLoc());
4273 NewTL.setRParenLoc(TL.getRParenLoc());
John McCalla2becad2009-10-21 00:40:46 +00004274
4275 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004276}
Mike Stump1eb44332009-09-09 15:08:12 +00004277
4278template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004279QualType TreeTransform<Derived>::TransformTypeOfType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004280 TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00004281 TypeSourceInfo* Old_Under_TI = TL.getUnderlyingTInfo();
4282 TypeSourceInfo* New_Under_TI = getDerived().TransformType(Old_Under_TI);
4283 if (!New_Under_TI)
Douglas Gregor577f75a2009-08-04 16:50:30 +00004284 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004285
John McCalla2becad2009-10-21 00:40:46 +00004286 QualType Result = TL.getType();
John McCallcfb708c2010-01-13 20:03:27 +00004287 if (getDerived().AlwaysRebuild() || New_Under_TI != Old_Under_TI) {
4288 Result = getDerived().RebuildTypeOfType(New_Under_TI->getType());
John McCalla2becad2009-10-21 00:40:46 +00004289 if (Result.isNull())
4290 return QualType();
4291 }
Mike Stump1eb44332009-09-09 15:08:12 +00004292
John McCalla2becad2009-10-21 00:40:46 +00004293 TypeOfTypeLoc NewTL = TLB.push<TypeOfTypeLoc>(Result);
John McCallcfb708c2010-01-13 20:03:27 +00004294 NewTL.setTypeofLoc(TL.getTypeofLoc());
4295 NewTL.setLParenLoc(TL.getLParenLoc());
4296 NewTL.setRParenLoc(TL.getRParenLoc());
4297 NewTL.setUnderlyingTInfo(New_Under_TI);
John McCalla2becad2009-10-21 00:40:46 +00004298
4299 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004300}
Mike Stump1eb44332009-09-09 15:08:12 +00004301
4302template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004303QualType TreeTransform<Derived>::TransformDecltypeType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004304 DecltypeTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004305 const DecltypeType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00004306
Douglas Gregor670444e2009-08-04 22:27:00 +00004307 // decltype expressions are not potentially evaluated contexts
John McCallf312b1e2010-08-26 23:41:50 +00004308 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00004309
John McCall60d7b3a2010-08-24 06:29:42 +00004310 ExprResult E = getDerived().TransformExpr(T->getUnderlyingExpr());
Douglas Gregor577f75a2009-08-04 16:50:30 +00004311 if (E.isInvalid())
4312 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004313
John McCalla2becad2009-10-21 00:40:46 +00004314 QualType Result = TL.getType();
4315 if (getDerived().AlwaysRebuild() ||
4316 E.get() != T->getUnderlyingExpr()) {
John McCall2a984ca2010-10-12 00:20:44 +00004317 Result = getDerived().RebuildDecltypeType(E.get(), TL.getNameLoc());
John McCalla2becad2009-10-21 00:40:46 +00004318 if (Result.isNull())
4319 return QualType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004320 }
John McCalla2becad2009-10-21 00:40:46 +00004321 else E.take();
Mike Stump1eb44332009-09-09 15:08:12 +00004322
John McCalla2becad2009-10-21 00:40:46 +00004323 DecltypeTypeLoc NewTL = TLB.push<DecltypeTypeLoc>(Result);
4324 NewTL.setNameLoc(TL.getNameLoc());
4325
4326 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004327}
4328
4329template<typename Derived>
Sean Huntca63c202011-05-24 22:41:36 +00004330QualType TreeTransform<Derived>::TransformUnaryTransformType(
4331 TypeLocBuilder &TLB,
4332 UnaryTransformTypeLoc TL) {
4333 QualType Result = TL.getType();
4334 if (Result->isDependentType()) {
4335 const UnaryTransformType *T = TL.getTypePtr();
4336 QualType NewBase =
4337 getDerived().TransformType(TL.getUnderlyingTInfo())->getType();
4338 Result = getDerived().RebuildUnaryTransformType(NewBase,
4339 T->getUTTKind(),
4340 TL.getKWLoc());
4341 if (Result.isNull())
4342 return QualType();
4343 }
4344
4345 UnaryTransformTypeLoc NewTL = TLB.push<UnaryTransformTypeLoc>(Result);
4346 NewTL.setKWLoc(TL.getKWLoc());
4347 NewTL.setParensRange(TL.getParensRange());
4348 NewTL.setUnderlyingTInfo(TL.getUnderlyingTInfo());
4349 return Result;
4350}
4351
4352template<typename Derived>
Richard Smith34b41d92011-02-20 03:19:35 +00004353QualType TreeTransform<Derived>::TransformAutoType(TypeLocBuilder &TLB,
4354 AutoTypeLoc TL) {
4355 const AutoType *T = TL.getTypePtr();
4356 QualType OldDeduced = T->getDeducedType();
4357 QualType NewDeduced;
4358 if (!OldDeduced.isNull()) {
4359 NewDeduced = getDerived().TransformType(OldDeduced);
4360 if (NewDeduced.isNull())
4361 return QualType();
4362 }
4363
4364 QualType Result = TL.getType();
4365 if (getDerived().AlwaysRebuild() || NewDeduced != OldDeduced) {
4366 Result = getDerived().RebuildAutoType(NewDeduced);
4367 if (Result.isNull())
4368 return QualType();
4369 }
4370
4371 AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
4372 NewTL.setNameLoc(TL.getNameLoc());
4373
4374 return Result;
4375}
4376
4377template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004378QualType TreeTransform<Derived>::TransformRecordType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004379 RecordTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004380 const RecordType *T = TL.getTypePtr();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004381 RecordDecl *Record
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004382 = cast_or_null<RecordDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4383 T->getDecl()));
Douglas Gregor577f75a2009-08-04 16:50:30 +00004384 if (!Record)
4385 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004386
John McCalla2becad2009-10-21 00:40:46 +00004387 QualType Result = TL.getType();
4388 if (getDerived().AlwaysRebuild() ||
4389 Record != T->getDecl()) {
4390 Result = getDerived().RebuildRecordType(Record);
4391 if (Result.isNull())
4392 return QualType();
4393 }
Mike Stump1eb44332009-09-09 15:08:12 +00004394
John McCalla2becad2009-10-21 00:40:46 +00004395 RecordTypeLoc NewTL = TLB.push<RecordTypeLoc>(Result);
4396 NewTL.setNameLoc(TL.getNameLoc());
4397
4398 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004399}
Mike Stump1eb44332009-09-09 15:08:12 +00004400
4401template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004402QualType TreeTransform<Derived>::TransformEnumType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004403 EnumTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004404 const EnumType *T = TL.getTypePtr();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004405 EnumDecl *Enum
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004406 = cast_or_null<EnumDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4407 T->getDecl()));
Douglas Gregor577f75a2009-08-04 16:50:30 +00004408 if (!Enum)
4409 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004410
John McCalla2becad2009-10-21 00:40:46 +00004411 QualType Result = TL.getType();
4412 if (getDerived().AlwaysRebuild() ||
4413 Enum != T->getDecl()) {
4414 Result = getDerived().RebuildEnumType(Enum);
4415 if (Result.isNull())
4416 return QualType();
4417 }
Mike Stump1eb44332009-09-09 15:08:12 +00004418
John McCalla2becad2009-10-21 00:40:46 +00004419 EnumTypeLoc NewTL = TLB.push<EnumTypeLoc>(Result);
4420 NewTL.setNameLoc(TL.getNameLoc());
4421
4422 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004423}
John McCall7da24312009-09-05 00:15:47 +00004424
John McCall3cb0ebd2010-03-10 03:28:59 +00004425template<typename Derived>
4426QualType TreeTransform<Derived>::TransformInjectedClassNameType(
4427 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004428 InjectedClassNameTypeLoc TL) {
John McCall3cb0ebd2010-03-10 03:28:59 +00004429 Decl *D = getDerived().TransformDecl(TL.getNameLoc(),
4430 TL.getTypePtr()->getDecl());
4431 if (!D) return QualType();
4432
4433 QualType T = SemaRef.Context.getTypeDeclType(cast<TypeDecl>(D));
4434 TLB.pushTypeSpec(T).setNameLoc(TL.getNameLoc());
4435 return T;
4436}
4437
Douglas Gregor577f75a2009-08-04 16:50:30 +00004438template<typename Derived>
4439QualType TreeTransform<Derived>::TransformTemplateTypeParmType(
John McCalla2becad2009-10-21 00:40:46 +00004440 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004441 TemplateTypeParmTypeLoc TL) {
John McCalla2becad2009-10-21 00:40:46 +00004442 return TransformTypeSpecType(TLB, TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +00004443}
4444
Mike Stump1eb44332009-09-09 15:08:12 +00004445template<typename Derived>
John McCall49a832b2009-10-18 09:09:24 +00004446QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmType(
John McCalla2becad2009-10-21 00:40:46 +00004447 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004448 SubstTemplateTypeParmTypeLoc TL) {
Douglas Gregor0b4bcb62011-03-05 17:19:27 +00004449 const SubstTemplateTypeParmType *T = TL.getTypePtr();
4450
4451 // Substitute into the replacement type, which itself might involve something
4452 // that needs to be transformed. This only tends to occur with default
4453 // template arguments of template template parameters.
4454 TemporaryBase Rebase(*this, TL.getNameLoc(), DeclarationName());
4455 QualType Replacement = getDerived().TransformType(T->getReplacementType());
4456 if (Replacement.isNull())
4457 return QualType();
4458
4459 // Always canonicalize the replacement type.
4460 Replacement = SemaRef.Context.getCanonicalType(Replacement);
4461 QualType Result
4462 = SemaRef.Context.getSubstTemplateTypeParmType(T->getReplacedParameter(),
4463 Replacement);
4464
4465 // Propagate type-source information.
4466 SubstTemplateTypeParmTypeLoc NewTL
4467 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
4468 NewTL.setNameLoc(TL.getNameLoc());
4469 return Result;
4470
John McCall49a832b2009-10-18 09:09:24 +00004471}
4472
4473template<typename Derived>
Douglas Gregorc3069d62011-01-14 02:55:32 +00004474QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmPackType(
4475 TypeLocBuilder &TLB,
4476 SubstTemplateTypeParmPackTypeLoc TL) {
4477 return TransformTypeSpecType(TLB, TL);
4478}
4479
4480template<typename Derived>
John McCall833ca992009-10-29 08:12:44 +00004481QualType TreeTransform<Derived>::TransformTemplateSpecializationType(
John McCall833ca992009-10-29 08:12:44 +00004482 TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004483 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00004484 const TemplateSpecializationType *T = TL.getTypePtr();
4485
Douglas Gregor1d752d72011-03-02 18:46:51 +00004486 // The nested-name-specifier never matters in a TemplateSpecializationType,
4487 // because we can't have a dependent nested-name-specifier anyway.
4488 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +00004489 TemplateName Template
Douglas Gregor1d752d72011-03-02 18:46:51 +00004490 = getDerived().TransformTemplateName(SS, T->getTemplateName(),
4491 TL.getTemplateNameLoc());
Douglas Gregor577f75a2009-08-04 16:50:30 +00004492 if (Template.isNull())
4493 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004494
John McCall43fed0d2010-11-12 08:19:04 +00004495 return getDerived().TransformTemplateSpecializationType(TLB, TL, Template);
4496}
4497
Eli Friedmanb001de72011-10-06 23:00:33 +00004498template<typename Derived>
4499QualType TreeTransform<Derived>::TransformAtomicType(TypeLocBuilder &TLB,
4500 AtomicTypeLoc TL) {
4501 QualType ValueType = getDerived().TransformType(TLB, TL.getValueLoc());
4502 if (ValueType.isNull())
4503 return QualType();
4504
4505 QualType Result = TL.getType();
4506 if (getDerived().AlwaysRebuild() ||
4507 ValueType != TL.getValueLoc().getType()) {
4508 Result = getDerived().RebuildAtomicType(ValueType, TL.getKWLoc());
4509 if (Result.isNull())
4510 return QualType();
4511 }
4512
4513 AtomicTypeLoc NewTL = TLB.push<AtomicTypeLoc>(Result);
4514 NewTL.setKWLoc(TL.getKWLoc());
4515 NewTL.setLParenLoc(TL.getLParenLoc());
4516 NewTL.setRParenLoc(TL.getRParenLoc());
4517
4518 return Result;
4519}
4520
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00004521namespace {
4522 /// \brief Simple iterator that traverses the template arguments in a
4523 /// container that provides a \c getArgLoc() member function.
4524 ///
4525 /// This iterator is intended to be used with the iterator form of
4526 /// \c TreeTransform<Derived>::TransformTemplateArguments().
4527 template<typename ArgLocContainer>
4528 class TemplateArgumentLocContainerIterator {
4529 ArgLocContainer *Container;
4530 unsigned Index;
4531
4532 public:
4533 typedef TemplateArgumentLoc value_type;
4534 typedef TemplateArgumentLoc reference;
4535 typedef int difference_type;
4536 typedef std::input_iterator_tag iterator_category;
4537
4538 class pointer {
4539 TemplateArgumentLoc Arg;
4540
4541 public:
4542 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
4543
4544 const TemplateArgumentLoc *operator->() const {
4545 return &Arg;
4546 }
4547 };
4548
4549
4550 TemplateArgumentLocContainerIterator() {}
4551
4552 TemplateArgumentLocContainerIterator(ArgLocContainer &Container,
4553 unsigned Index)
4554 : Container(&Container), Index(Index) { }
4555
4556 TemplateArgumentLocContainerIterator &operator++() {
4557 ++Index;
4558 return *this;
4559 }
4560
4561 TemplateArgumentLocContainerIterator operator++(int) {
4562 TemplateArgumentLocContainerIterator Old(*this);
4563 ++(*this);
4564 return Old;
4565 }
4566
4567 TemplateArgumentLoc operator*() const {
4568 return Container->getArgLoc(Index);
4569 }
4570
4571 pointer operator->() const {
4572 return pointer(Container->getArgLoc(Index));
4573 }
4574
4575 friend bool operator==(const TemplateArgumentLocContainerIterator &X,
Douglas Gregorf7dd6992010-12-21 21:51:48 +00004576 const TemplateArgumentLocContainerIterator &Y) {
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00004577 return X.Container == Y.Container && X.Index == Y.Index;
4578 }
4579
4580 friend bool operator!=(const TemplateArgumentLocContainerIterator &X,
Douglas Gregorf7dd6992010-12-21 21:51:48 +00004581 const TemplateArgumentLocContainerIterator &Y) {
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00004582 return !(X == Y);
4583 }
4584 };
4585}
4586
4587
John McCall43fed0d2010-11-12 08:19:04 +00004588template <typename Derived>
4589QualType TreeTransform<Derived>::TransformTemplateSpecializationType(
4590 TypeLocBuilder &TLB,
4591 TemplateSpecializationTypeLoc TL,
4592 TemplateName Template) {
John McCalld5532b62009-11-23 01:53:49 +00004593 TemplateArgumentListInfo NewTemplateArgs;
4594 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4595 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
Douglas Gregor7ca7ac42010-12-20 23:36:19 +00004596 typedef TemplateArgumentLocContainerIterator<TemplateSpecializationTypeLoc>
4597 ArgIterator;
4598 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4599 ArgIterator(TL, TL.getNumArgs()),
4600 NewTemplateArgs))
Douglas Gregor7f61f2f2010-12-20 17:42:22 +00004601 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004602
John McCall833ca992009-10-29 08:12:44 +00004603 // FIXME: maybe don't rebuild if all the template arguments are the same.
4604
4605 QualType Result =
4606 getDerived().RebuildTemplateSpecializationType(Template,
4607 TL.getTemplateNameLoc(),
John McCalld5532b62009-11-23 01:53:49 +00004608 NewTemplateArgs);
John McCall833ca992009-10-29 08:12:44 +00004609
4610 if (!Result.isNull()) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00004611 // Specializations of template template parameters are represented as
4612 // TemplateSpecializationTypes, and substitution of type alias templates
4613 // within a dependent context can transform them into
4614 // DependentTemplateSpecializationTypes.
4615 if (isa<DependentTemplateSpecializationType>(Result)) {
4616 DependentTemplateSpecializationTypeLoc NewTL
4617 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004618 NewTL.setElaboratedKeywordLoc(SourceLocation());
Richard Smith3e4c6c42011-05-05 21:57:07 +00004619 NewTL.setQualifierLoc(NestedNameSpecifierLoc());
Abramo Bagnara66581d42012-02-06 22:45:07 +00004620 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004621 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Richard Smith3e4c6c42011-05-05 21:57:07 +00004622 NewTL.setLAngleLoc(TL.getLAngleLoc());
4623 NewTL.setRAngleLoc(TL.getRAngleLoc());
4624 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4625 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4626 return Result;
4627 }
4628
John McCall833ca992009-10-29 08:12:44 +00004629 TemplateSpecializationTypeLoc NewTL
4630 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004631 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
John McCall833ca992009-10-29 08:12:44 +00004632 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4633 NewTL.setLAngleLoc(TL.getLAngleLoc());
4634 NewTL.setRAngleLoc(TL.getRAngleLoc());
4635 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4636 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
Douglas Gregor577f75a2009-08-04 16:50:30 +00004637 }
Mike Stump1eb44332009-09-09 15:08:12 +00004638
John McCall833ca992009-10-29 08:12:44 +00004639 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004640}
Mike Stump1eb44332009-09-09 15:08:12 +00004641
Douglas Gregora88f09f2011-02-28 17:23:35 +00004642template <typename Derived>
4643QualType TreeTransform<Derived>::TransformDependentTemplateSpecializationType(
4644 TypeLocBuilder &TLB,
4645 DependentTemplateSpecializationTypeLoc TL,
Douglas Gregor087eb5a2011-03-04 18:53:13 +00004646 TemplateName Template,
4647 CXXScopeSpec &SS) {
Douglas Gregora88f09f2011-02-28 17:23:35 +00004648 TemplateArgumentListInfo NewTemplateArgs;
4649 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4650 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
4651 typedef TemplateArgumentLocContainerIterator<
4652 DependentTemplateSpecializationTypeLoc> ArgIterator;
4653 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4654 ArgIterator(TL, TL.getNumArgs()),
4655 NewTemplateArgs))
4656 return QualType();
4657
4658 // FIXME: maybe don't rebuild if all the template arguments are the same.
4659
4660 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
4661 QualType Result
4662 = getSema().Context.getDependentTemplateSpecializationType(
4663 TL.getTypePtr()->getKeyword(),
4664 DTN->getQualifier(),
4665 DTN->getIdentifier(),
4666 NewTemplateArgs);
4667
4668 DependentTemplateSpecializationTypeLoc NewTL
4669 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004670 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004671 NewTL.setQualifierLoc(SS.getWithLocInContext(SemaRef.Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004672 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004673 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregora88f09f2011-02-28 17:23:35 +00004674 NewTL.setLAngleLoc(TL.getLAngleLoc());
4675 NewTL.setRAngleLoc(TL.getRAngleLoc());
4676 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4677 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4678 return Result;
4679 }
4680
4681 QualType Result
4682 = getDerived().RebuildTemplateSpecializationType(Template,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004683 TL.getTemplateNameLoc(),
Douglas Gregora88f09f2011-02-28 17:23:35 +00004684 NewTemplateArgs);
4685
4686 if (!Result.isNull()) {
4687 /// FIXME: Wrap this in an elaborated-type-specifier?
4688 TemplateSpecializationTypeLoc NewTL
4689 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara66581d42012-02-06 22:45:07 +00004690 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004691 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregora88f09f2011-02-28 17:23:35 +00004692 NewTL.setLAngleLoc(TL.getLAngleLoc());
4693 NewTL.setRAngleLoc(TL.getRAngleLoc());
4694 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4695 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4696 }
4697
4698 return Result;
4699}
4700
Mike Stump1eb44332009-09-09 15:08:12 +00004701template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004702QualType
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004703TreeTransform<Derived>::TransformElaboratedType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004704 ElaboratedTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004705 const ElaboratedType *T = TL.getTypePtr();
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004706
Douglas Gregor9e876872011-03-01 18:12:44 +00004707 NestedNameSpecifierLoc QualifierLoc;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004708 // NOTE: the qualifier in an ElaboratedType is optional.
Douglas Gregor9e876872011-03-01 18:12:44 +00004709 if (TL.getQualifierLoc()) {
4710 QualifierLoc
4711 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4712 if (!QualifierLoc)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004713 return QualType();
4714 }
Mike Stump1eb44332009-09-09 15:08:12 +00004715
John McCall43fed0d2010-11-12 08:19:04 +00004716 QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
4717 if (NamedT.isNull())
4718 return QualType();
Daniel Dunbara63db842010-05-14 16:34:09 +00004719
Richard Smith3e4c6c42011-05-05 21:57:07 +00004720 // C++0x [dcl.type.elab]p2:
4721 // If the identifier resolves to a typedef-name or the simple-template-id
4722 // resolves to an alias template specialization, the
4723 // elaborated-type-specifier is ill-formed.
Richard Smith18041742011-05-14 15:04:18 +00004724 if (T->getKeyword() != ETK_None && T->getKeyword() != ETK_Typename) {
4725 if (const TemplateSpecializationType *TST =
4726 NamedT->getAs<TemplateSpecializationType>()) {
4727 TemplateName Template = TST->getTemplateName();
4728 if (TypeAliasTemplateDecl *TAT =
4729 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
4730 SemaRef.Diag(TL.getNamedTypeLoc().getBeginLoc(),
4731 diag::err_tag_reference_non_tag) << 4;
4732 SemaRef.Diag(TAT->getLocation(), diag::note_declared_at);
4733 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00004734 }
4735 }
4736
John McCalla2becad2009-10-21 00:40:46 +00004737 QualType Result = TL.getType();
4738 if (getDerived().AlwaysRebuild() ||
Douglas Gregor9e876872011-03-01 18:12:44 +00004739 QualifierLoc != TL.getQualifierLoc() ||
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004740 NamedT != T->getNamedType()) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004741 Result = getDerived().RebuildElaboratedType(TL.getElaboratedKeywordLoc(),
Douglas Gregor9e876872011-03-01 18:12:44 +00004742 T->getKeyword(),
4743 QualifierLoc, NamedT);
John McCalla2becad2009-10-21 00:40:46 +00004744 if (Result.isNull())
4745 return QualType();
4746 }
Douglas Gregor577f75a2009-08-04 16:50:30 +00004747
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004748 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00004749 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor9e876872011-03-01 18:12:44 +00004750 NewTL.setQualifierLoc(QualifierLoc);
John McCalla2becad2009-10-21 00:40:46 +00004751 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004752}
Mike Stump1eb44332009-09-09 15:08:12 +00004753
4754template<typename Derived>
John McCall9d156a72011-01-06 01:58:22 +00004755QualType TreeTransform<Derived>::TransformAttributedType(
4756 TypeLocBuilder &TLB,
4757 AttributedTypeLoc TL) {
4758 const AttributedType *oldType = TL.getTypePtr();
4759 QualType modifiedType = getDerived().TransformType(TLB, TL.getModifiedLoc());
4760 if (modifiedType.isNull())
4761 return QualType();
4762
4763 QualType result = TL.getType();
4764
4765 // FIXME: dependent operand expressions?
4766 if (getDerived().AlwaysRebuild() ||
4767 modifiedType != oldType->getModifiedType()) {
4768 // TODO: this is really lame; we should really be rebuilding the
4769 // equivalent type from first principles.
4770 QualType equivalentType
4771 = getDerived().TransformType(oldType->getEquivalentType());
4772 if (equivalentType.isNull())
4773 return QualType();
4774 result = SemaRef.Context.getAttributedType(oldType->getAttrKind(),
4775 modifiedType,
4776 equivalentType);
4777 }
4778
4779 AttributedTypeLoc newTL = TLB.push<AttributedTypeLoc>(result);
4780 newTL.setAttrNameLoc(TL.getAttrNameLoc());
4781 if (TL.hasAttrOperand())
4782 newTL.setAttrOperandParensRange(TL.getAttrOperandParensRange());
4783 if (TL.hasAttrExprOperand())
4784 newTL.setAttrExprOperand(TL.getAttrExprOperand());
4785 else if (TL.hasAttrEnumOperand())
4786 newTL.setAttrEnumOperandLoc(TL.getAttrEnumOperandLoc());
4787
4788 return result;
4789}
4790
4791template<typename Derived>
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004792QualType
4793TreeTransform<Derived>::TransformParenType(TypeLocBuilder &TLB,
4794 ParenTypeLoc TL) {
4795 QualType Inner = getDerived().TransformType(TLB, TL.getInnerLoc());
4796 if (Inner.isNull())
4797 return QualType();
4798
4799 QualType Result = TL.getType();
4800 if (getDerived().AlwaysRebuild() ||
4801 Inner != TL.getInnerLoc().getType()) {
4802 Result = getDerived().RebuildParenType(Inner);
4803 if (Result.isNull())
4804 return QualType();
4805 }
4806
4807 ParenTypeLoc NewTL = TLB.push<ParenTypeLoc>(Result);
4808 NewTL.setLParenLoc(TL.getLParenLoc());
4809 NewTL.setRParenLoc(TL.getRParenLoc());
4810 return Result;
4811}
4812
4813template<typename Derived>
Douglas Gregor4714c122010-03-31 17:34:00 +00004814QualType TreeTransform<Derived>::TransformDependentNameType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004815 DependentNameTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00004816 const DependentNameType *T = TL.getTypePtr();
John McCall833ca992009-10-29 08:12:44 +00004817
Douglas Gregor2494dd02011-03-01 01:34:45 +00004818 NestedNameSpecifierLoc QualifierLoc
4819 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4820 if (!QualifierLoc)
Douglas Gregor577f75a2009-08-04 16:50:30 +00004821 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004822
John McCall33500952010-06-11 00:33:02 +00004823 QualType Result
Douglas Gregor2494dd02011-03-01 01:34:45 +00004824 = getDerived().RebuildDependentNameType(T->getKeyword(),
Abramo Bagnara38a42912012-02-06 19:09:27 +00004825 TL.getElaboratedKeywordLoc(),
Douglas Gregor2494dd02011-03-01 01:34:45 +00004826 QualifierLoc,
4827 T->getIdentifier(),
John McCall33500952010-06-11 00:33:02 +00004828 TL.getNameLoc());
John McCalla2becad2009-10-21 00:40:46 +00004829 if (Result.isNull())
4830 return QualType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004831
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004832 if (const ElaboratedType* ElabT = Result->getAs<ElaboratedType>()) {
4833 QualType NamedT = ElabT->getNamedType();
John McCall33500952010-06-11 00:33:02 +00004834 TLB.pushTypeSpec(NamedT).setNameLoc(TL.getNameLoc());
4835
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004836 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00004837 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor9e876872011-03-01 18:12:44 +00004838 NewTL.setQualifierLoc(QualifierLoc);
John McCall33500952010-06-11 00:33:02 +00004839 } else {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004840 DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00004841 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor2494dd02011-03-01 01:34:45 +00004842 NewTL.setQualifierLoc(QualifierLoc);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004843 NewTL.setNameLoc(TL.getNameLoc());
4844 }
John McCalla2becad2009-10-21 00:40:46 +00004845 return Result;
Douglas Gregor577f75a2009-08-04 16:50:30 +00004846}
Mike Stump1eb44332009-09-09 15:08:12 +00004847
Douglas Gregor577f75a2009-08-04 16:50:30 +00004848template<typename Derived>
John McCall33500952010-06-11 00:33:02 +00004849QualType TreeTransform<Derived>::
4850 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004851 DependentTemplateSpecializationTypeLoc TL) {
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004852 NestedNameSpecifierLoc QualifierLoc;
4853 if (TL.getQualifierLoc()) {
4854 QualifierLoc
4855 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4856 if (!QualifierLoc)
Douglas Gregora88f09f2011-02-28 17:23:35 +00004857 return QualType();
4858 }
4859
John McCall43fed0d2010-11-12 08:19:04 +00004860 return getDerived()
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004861 .TransformDependentTemplateSpecializationType(TLB, TL, QualifierLoc);
John McCall43fed0d2010-11-12 08:19:04 +00004862}
4863
4864template<typename Derived>
4865QualType TreeTransform<Derived>::
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004866TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
4867 DependentTemplateSpecializationTypeLoc TL,
4868 NestedNameSpecifierLoc QualifierLoc) {
4869 const DependentTemplateSpecializationType *T = TL.getTypePtr();
4870
4871 TemplateArgumentListInfo NewTemplateArgs;
4872 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4873 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
4874
4875 typedef TemplateArgumentLocContainerIterator<
4876 DependentTemplateSpecializationTypeLoc> ArgIterator;
4877 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4878 ArgIterator(TL, TL.getNumArgs()),
4879 NewTemplateArgs))
4880 return QualType();
4881
4882 QualType Result
4883 = getDerived().RebuildDependentTemplateSpecializationType(T->getKeyword(),
4884 QualifierLoc,
4885 T->getIdentifier(),
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004886 TL.getTemplateNameLoc(),
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004887 NewTemplateArgs);
4888 if (Result.isNull())
4889 return QualType();
4890
4891 if (const ElaboratedType *ElabT = dyn_cast<ElaboratedType>(Result)) {
4892 QualType NamedT = ElabT->getNamedType();
4893
4894 // Copy information relevant to the template specialization.
4895 TemplateSpecializationTypeLoc NamedTL
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004896 = TLB.push<TemplateSpecializationTypeLoc>(NamedT);
Abramo Bagnara66581d42012-02-06 22:45:07 +00004897 NamedTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004898 NamedTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004899 NamedTL.setLAngleLoc(TL.getLAngleLoc());
4900 NamedTL.setRAngleLoc(TL.getRAngleLoc());
Douglas Gregor944cdae2011-03-07 15:13:34 +00004901 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004902 NamedTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004903
4904 // Copy information relevant to the elaborated type.
4905 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00004906 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004907 NewTL.setQualifierLoc(QualifierLoc);
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004908 } else if (isa<DependentTemplateSpecializationType>(Result)) {
4909 DependentTemplateSpecializationTypeLoc SpecTL
4910 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004911 SpecTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004912 SpecTL.setQualifierLoc(QualifierLoc);
Abramo Bagnara66581d42012-02-06 22:45:07 +00004913 SpecTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004914 SpecTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004915 SpecTL.setLAngleLoc(TL.getLAngleLoc());
4916 SpecTL.setRAngleLoc(TL.getRAngleLoc());
Douglas Gregor944cdae2011-03-07 15:13:34 +00004917 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004918 SpecTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004919 } else {
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004920 TemplateSpecializationTypeLoc SpecTL
4921 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara66581d42012-02-06 22:45:07 +00004922 SpecTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004923 SpecTL.setTemplateNameLoc(TL.getTemplateNameLoc());
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004924 SpecTL.setLAngleLoc(TL.getLAngleLoc());
4925 SpecTL.setRAngleLoc(TL.getRAngleLoc());
Douglas Gregor944cdae2011-03-07 15:13:34 +00004926 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
Douglas Gregor0a0367a2011-03-07 02:33:33 +00004927 SpecTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004928 }
4929 return Result;
4930}
4931
4932template<typename Derived>
Douglas Gregor7536dd52010-12-20 02:24:11 +00004933QualType TreeTransform<Derived>::TransformPackExpansionType(TypeLocBuilder &TLB,
4934 PackExpansionTypeLoc TL) {
Douglas Gregor2fc1bb72011-01-12 17:07:58 +00004935 QualType Pattern
4936 = getDerived().TransformType(TLB, TL.getPatternLoc());
4937 if (Pattern.isNull())
4938 return QualType();
4939
4940 QualType Result = TL.getType();
4941 if (getDerived().AlwaysRebuild() ||
4942 Pattern != TL.getPatternLoc().getType()) {
4943 Result = getDerived().RebuildPackExpansionType(Pattern,
4944 TL.getPatternLoc().getSourceRange(),
Douglas Gregorcded4f62011-01-14 17:04:44 +00004945 TL.getEllipsisLoc(),
4946 TL.getTypePtr()->getNumExpansions());
Douglas Gregor2fc1bb72011-01-12 17:07:58 +00004947 if (Result.isNull())
4948 return QualType();
4949 }
4950
4951 PackExpansionTypeLoc NewT = TLB.push<PackExpansionTypeLoc>(Result);
4952 NewT.setEllipsisLoc(TL.getEllipsisLoc());
4953 return Result;
Douglas Gregor7536dd52010-12-20 02:24:11 +00004954}
4955
4956template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004957QualType
4958TreeTransform<Derived>::TransformObjCInterfaceType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004959 ObjCInterfaceTypeLoc TL) {
Douglas Gregoref57c612010-04-22 17:28:13 +00004960 // ObjCInterfaceType is never dependent.
John McCallc12c5bb2010-05-15 11:32:37 +00004961 TLB.pushFullCopy(TL);
4962 return TL.getType();
4963}
4964
4965template<typename Derived>
4966QualType
4967TreeTransform<Derived>::TransformObjCObjectType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004968 ObjCObjectTypeLoc TL) {
John McCallc12c5bb2010-05-15 11:32:37 +00004969 // ObjCObjectType is never dependent.
4970 TLB.pushFullCopy(TL);
Douglas Gregoref57c612010-04-22 17:28:13 +00004971 return TL.getType();
Douglas Gregor577f75a2009-08-04 16:50:30 +00004972}
Mike Stump1eb44332009-09-09 15:08:12 +00004973
4974template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00004975QualType
4976TreeTransform<Derived>::TransformObjCObjectPointerType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00004977 ObjCObjectPointerTypeLoc TL) {
Douglas Gregoref57c612010-04-22 17:28:13 +00004978 // ObjCObjectPointerType is never dependent.
John McCallc12c5bb2010-05-15 11:32:37 +00004979 TLB.pushFullCopy(TL);
Douglas Gregoref57c612010-04-22 17:28:13 +00004980 return TL.getType();
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004981}
4982
Douglas Gregor577f75a2009-08-04 16:50:30 +00004983//===----------------------------------------------------------------------===//
Douglas Gregor43959a92009-08-20 07:17:43 +00004984// Statement transformation
4985//===----------------------------------------------------------------------===//
4986template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00004987StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00004988TreeTransform<Derived>::TransformNullStmt(NullStmt *S) {
John McCall3fa5cae2010-10-26 07:05:15 +00004989 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00004990}
4991
4992template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00004993StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00004994TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S) {
4995 return getDerived().TransformCompoundStmt(S, false);
4996}
4997
4998template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00004999StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005000TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S,
Douglas Gregor43959a92009-08-20 07:17:43 +00005001 bool IsStmtExpr) {
Dmitri Gribenko625bb562012-02-14 22:14:32 +00005002 Sema::CompoundScopeRAII CompoundScope(getSema());
5003
John McCall7114cba2010-08-27 19:56:05 +00005004 bool SubStmtInvalid = false;
Douglas Gregor43959a92009-08-20 07:17:43 +00005005 bool SubStmtChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00005006 ASTOwningVector<Stmt*> Statements(getSema());
Douglas Gregor43959a92009-08-20 07:17:43 +00005007 for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end();
5008 B != BEnd; ++B) {
John McCall60d7b3a2010-08-24 06:29:42 +00005009 StmtResult Result = getDerived().TransformStmt(*B);
John McCall7114cba2010-08-27 19:56:05 +00005010 if (Result.isInvalid()) {
5011 // Immediately fail if this was a DeclStmt, since it's very
5012 // likely that this will cause problems for future statements.
5013 if (isa<DeclStmt>(*B))
5014 return StmtError();
5015
5016 // Otherwise, just keep processing substatements and fail later.
5017 SubStmtInvalid = true;
5018 continue;
5019 }
Mike Stump1eb44332009-09-09 15:08:12 +00005020
Douglas Gregor43959a92009-08-20 07:17:43 +00005021 SubStmtChanged = SubStmtChanged || Result.get() != *B;
5022 Statements.push_back(Result.takeAs<Stmt>());
5023 }
Mike Stump1eb44332009-09-09 15:08:12 +00005024
John McCall7114cba2010-08-27 19:56:05 +00005025 if (SubStmtInvalid)
5026 return StmtError();
5027
Douglas Gregor43959a92009-08-20 07:17:43 +00005028 if (!getDerived().AlwaysRebuild() &&
5029 !SubStmtChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00005030 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005031
5032 return getDerived().RebuildCompoundStmt(S->getLBracLoc(),
5033 move_arg(Statements),
5034 S->getRBracLoc(),
5035 IsStmtExpr);
5036}
Mike Stump1eb44332009-09-09 15:08:12 +00005037
Douglas Gregor43959a92009-08-20 07:17:43 +00005038template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005039StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005040TreeTransform<Derived>::TransformCaseStmt(CaseStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005041 ExprResult LHS, RHS;
Eli Friedman264c1f82009-11-19 03:14:00 +00005042 {
Eli Friedman6b3014b2012-01-18 02:54:10 +00005043 EnterExpressionEvaluationContext Unevaluated(SemaRef,
5044 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00005045
Eli Friedman264c1f82009-11-19 03:14:00 +00005046 // Transform the left-hand case value.
5047 LHS = getDerived().TransformExpr(S->getLHS());
5048 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005049 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005050
Eli Friedman264c1f82009-11-19 03:14:00 +00005051 // Transform the right-hand case value (for the GNU case-range extension).
5052 RHS = getDerived().TransformExpr(S->getRHS());
5053 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005054 return StmtError();
Eli Friedman264c1f82009-11-19 03:14:00 +00005055 }
Mike Stump1eb44332009-09-09 15:08:12 +00005056
Douglas Gregor43959a92009-08-20 07:17:43 +00005057 // Build the case statement.
5058 // Case statements are always rebuilt so that they will attached to their
5059 // transformed switch statement.
John McCall60d7b3a2010-08-24 06:29:42 +00005060 StmtResult Case = getDerived().RebuildCaseStmt(S->getCaseLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005061 LHS.get(),
Douglas Gregor43959a92009-08-20 07:17:43 +00005062 S->getEllipsisLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005063 RHS.get(),
Douglas Gregor43959a92009-08-20 07:17:43 +00005064 S->getColonLoc());
5065 if (Case.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005066 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005067
Douglas Gregor43959a92009-08-20 07:17:43 +00005068 // Transform the statement following the case
John McCall60d7b3a2010-08-24 06:29:42 +00005069 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
Douglas Gregor43959a92009-08-20 07:17:43 +00005070 if (SubStmt.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005071 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005072
Douglas Gregor43959a92009-08-20 07:17:43 +00005073 // Attach the body to the case statement
John McCall9ae2f072010-08-23 23:25:46 +00005074 return getDerived().RebuildCaseStmtBody(Case.get(), SubStmt.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005075}
5076
5077template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005078StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005079TreeTransform<Derived>::TransformDefaultStmt(DefaultStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005080 // Transform the statement following the default case
John McCall60d7b3a2010-08-24 06:29:42 +00005081 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
Douglas Gregor43959a92009-08-20 07:17:43 +00005082 if (SubStmt.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005083 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005084
Douglas Gregor43959a92009-08-20 07:17:43 +00005085 // Default statements are always rebuilt
5086 return getDerived().RebuildDefaultStmt(S->getDefaultLoc(), S->getColonLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005087 SubStmt.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005088}
Mike Stump1eb44332009-09-09 15:08:12 +00005089
Douglas Gregor43959a92009-08-20 07:17:43 +00005090template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005091StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005092TreeTransform<Derived>::TransformLabelStmt(LabelStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005093 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
Douglas Gregor43959a92009-08-20 07:17:43 +00005094 if (SubStmt.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005095 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005096
Chris Lattner57ad3782011-02-17 20:34:02 +00005097 Decl *LD = getDerived().TransformDecl(S->getDecl()->getLocation(),
5098 S->getDecl());
5099 if (!LD)
5100 return StmtError();
5101
5102
Douglas Gregor43959a92009-08-20 07:17:43 +00005103 // FIXME: Pass the real colon location in.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00005104 return getDerived().RebuildLabelStmt(S->getIdentLoc(),
Chris Lattner57ad3782011-02-17 20:34:02 +00005105 cast<LabelDecl>(LD), SourceLocation(),
5106 SubStmt.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005107}
Mike Stump1eb44332009-09-09 15:08:12 +00005108
Douglas Gregor43959a92009-08-20 07:17:43 +00005109template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005110StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005111TreeTransform<Derived>::TransformIfStmt(IfStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005112 // Transform the condition
John McCall60d7b3a2010-08-24 06:29:42 +00005113 ExprResult Cond;
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00005114 VarDecl *ConditionVar = 0;
5115 if (S->getConditionVariable()) {
Sean Huntc3021132010-05-05 15:23:54 +00005116 ConditionVar
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00005117 = cast_or_null<VarDecl>(
Douglas Gregoraac571c2010-03-01 17:25:41 +00005118 getDerived().TransformDefinition(
5119 S->getConditionVariable()->getLocation(),
5120 S->getConditionVariable()));
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00005121 if (!ConditionVar)
John McCallf312b1e2010-08-26 23:41:50 +00005122 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005123 } else {
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00005124 Cond = getDerived().TransformExpr(S->getCond());
Sean Huntc3021132010-05-05 15:23:54 +00005125
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005126 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005127 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005128
5129 // Convert the condition to a boolean value.
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005130 if (S->getCond()) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +00005131 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getIfLoc(),
5132 Cond.get());
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005133 if (CondE.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005134 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005135
John McCall9ae2f072010-08-23 23:25:46 +00005136 Cond = CondE.get();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005137 }
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005138 }
Sean Huntc3021132010-05-05 15:23:54 +00005139
John McCall9ae2f072010-08-23 23:25:46 +00005140 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5141 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
John McCallf312b1e2010-08-26 23:41:50 +00005142 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005143
Douglas Gregor43959a92009-08-20 07:17:43 +00005144 // Transform the "then" branch.
John McCall60d7b3a2010-08-24 06:29:42 +00005145 StmtResult Then = getDerived().TransformStmt(S->getThen());
Douglas Gregor43959a92009-08-20 07:17:43 +00005146 if (Then.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005147 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005148
Douglas Gregor43959a92009-08-20 07:17:43 +00005149 // Transform the "else" branch.
John McCall60d7b3a2010-08-24 06:29:42 +00005150 StmtResult Else = getDerived().TransformStmt(S->getElse());
Douglas Gregor43959a92009-08-20 07:17:43 +00005151 if (Else.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005152 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005153
Douglas Gregor43959a92009-08-20 07:17:43 +00005154 if (!getDerived().AlwaysRebuild() &&
John McCall9ae2f072010-08-23 23:25:46 +00005155 FullCond.get() == S->getCond() &&
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005156 ConditionVar == S->getConditionVariable() &&
Douglas Gregor43959a92009-08-20 07:17:43 +00005157 Then.get() == S->getThen() &&
5158 Else.get() == S->getElse())
John McCall3fa5cae2010-10-26 07:05:15 +00005159 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005160
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005161 return getDerived().RebuildIfStmt(S->getIfLoc(), FullCond, ConditionVar,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00005162 Then.get(),
John McCall9ae2f072010-08-23 23:25:46 +00005163 S->getElseLoc(), Else.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005164}
5165
5166template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005167StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005168TreeTransform<Derived>::TransformSwitchStmt(SwitchStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005169 // Transform the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00005170 ExprResult Cond;
Douglas Gregord3d53012009-11-24 17:07:59 +00005171 VarDecl *ConditionVar = 0;
5172 if (S->getConditionVariable()) {
Sean Huntc3021132010-05-05 15:23:54 +00005173 ConditionVar
Douglas Gregord3d53012009-11-24 17:07:59 +00005174 = cast_or_null<VarDecl>(
Douglas Gregoraac571c2010-03-01 17:25:41 +00005175 getDerived().TransformDefinition(
5176 S->getConditionVariable()->getLocation(),
5177 S->getConditionVariable()));
Douglas Gregord3d53012009-11-24 17:07:59 +00005178 if (!ConditionVar)
John McCallf312b1e2010-08-26 23:41:50 +00005179 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005180 } else {
Douglas Gregord3d53012009-11-24 17:07:59 +00005181 Cond = getDerived().TransformExpr(S->getCond());
Sean Huntc3021132010-05-05 15:23:54 +00005182
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005183 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005184 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005185 }
Mike Stump1eb44332009-09-09 15:08:12 +00005186
Douglas Gregor43959a92009-08-20 07:17:43 +00005187 // Rebuild the switch statement.
John McCall60d7b3a2010-08-24 06:29:42 +00005188 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00005189 = getDerived().RebuildSwitchStmtStart(S->getSwitchLoc(), Cond.get(),
Douglas Gregor586596f2010-05-06 17:25:47 +00005190 ConditionVar);
Douglas Gregor43959a92009-08-20 07:17:43 +00005191 if (Switch.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005192 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005193
Douglas Gregor43959a92009-08-20 07:17:43 +00005194 // Transform the body of the switch statement.
John McCall60d7b3a2010-08-24 06:29:42 +00005195 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregor43959a92009-08-20 07:17:43 +00005196 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005197 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005198
Douglas Gregor43959a92009-08-20 07:17:43 +00005199 // Complete the switch statement.
John McCall9ae2f072010-08-23 23:25:46 +00005200 return getDerived().RebuildSwitchStmtBody(S->getSwitchLoc(), Switch.get(),
5201 Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005202}
Mike Stump1eb44332009-09-09 15:08:12 +00005203
Douglas Gregor43959a92009-08-20 07:17:43 +00005204template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005205StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005206TreeTransform<Derived>::TransformWhileStmt(WhileStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005207 // Transform the condition
John McCall60d7b3a2010-08-24 06:29:42 +00005208 ExprResult Cond;
Douglas Gregor5656e142009-11-24 21:15:44 +00005209 VarDecl *ConditionVar = 0;
5210 if (S->getConditionVariable()) {
Sean Huntc3021132010-05-05 15:23:54 +00005211 ConditionVar
Douglas Gregor5656e142009-11-24 21:15:44 +00005212 = cast_or_null<VarDecl>(
Douglas Gregoraac571c2010-03-01 17:25:41 +00005213 getDerived().TransformDefinition(
5214 S->getConditionVariable()->getLocation(),
5215 S->getConditionVariable()));
Douglas Gregor5656e142009-11-24 21:15:44 +00005216 if (!ConditionVar)
John McCallf312b1e2010-08-26 23:41:50 +00005217 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005218 } else {
Douglas Gregor5656e142009-11-24 21:15:44 +00005219 Cond = getDerived().TransformExpr(S->getCond());
Sean Huntc3021132010-05-05 15:23:54 +00005220
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005221 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005222 return StmtError();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005223
5224 if (S->getCond()) {
5225 // Convert the condition to a boolean value.
Douglas Gregor8491ffe2010-12-20 22:05:00 +00005226 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getWhileLoc(),
5227 Cond.get());
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005228 if (CondE.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005229 return StmtError();
John McCall9ae2f072010-08-23 23:25:46 +00005230 Cond = CondE;
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005231 }
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005232 }
Mike Stump1eb44332009-09-09 15:08:12 +00005233
John McCall9ae2f072010-08-23 23:25:46 +00005234 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5235 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
John McCallf312b1e2010-08-26 23:41:50 +00005236 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005237
Douglas Gregor43959a92009-08-20 07:17:43 +00005238 // Transform the body
John McCall60d7b3a2010-08-24 06:29:42 +00005239 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregor43959a92009-08-20 07:17:43 +00005240 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005241 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005242
Douglas Gregor43959a92009-08-20 07:17:43 +00005243 if (!getDerived().AlwaysRebuild() &&
John McCall9ae2f072010-08-23 23:25:46 +00005244 FullCond.get() == S->getCond() &&
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005245 ConditionVar == S->getConditionVariable() &&
Douglas Gregor43959a92009-08-20 07:17:43 +00005246 Body.get() == S->getBody())
John McCall9ae2f072010-08-23 23:25:46 +00005247 return Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005248
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005249 return getDerived().RebuildWhileStmt(S->getWhileLoc(), FullCond,
John McCall9ae2f072010-08-23 23:25:46 +00005250 ConditionVar, Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005251}
Mike Stump1eb44332009-09-09 15:08:12 +00005252
Douglas Gregor43959a92009-08-20 07:17:43 +00005253template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005254StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005255TreeTransform<Derived>::TransformDoStmt(DoStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005256 // Transform the body
John McCall60d7b3a2010-08-24 06:29:42 +00005257 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregor43959a92009-08-20 07:17:43 +00005258 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005259 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005260
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005261 // Transform the condition
John McCall60d7b3a2010-08-24 06:29:42 +00005262 ExprResult Cond = getDerived().TransformExpr(S->getCond());
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005263 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005264 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005265
Douglas Gregor43959a92009-08-20 07:17:43 +00005266 if (!getDerived().AlwaysRebuild() &&
5267 Cond.get() == S->getCond() &&
5268 Body.get() == S->getBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005269 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005270
John McCall9ae2f072010-08-23 23:25:46 +00005271 return getDerived().RebuildDoStmt(S->getDoLoc(), Body.get(), S->getWhileLoc(),
5272 /*FIXME:*/S->getWhileLoc(), Cond.get(),
Douglas Gregor43959a92009-08-20 07:17:43 +00005273 S->getRParenLoc());
5274}
Mike Stump1eb44332009-09-09 15:08:12 +00005275
Douglas Gregor43959a92009-08-20 07:17:43 +00005276template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005277StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005278TreeTransform<Derived>::TransformForStmt(ForStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005279 // Transform the initialization statement
John McCall60d7b3a2010-08-24 06:29:42 +00005280 StmtResult Init = getDerived().TransformStmt(S->getInit());
Douglas Gregor43959a92009-08-20 07:17:43 +00005281 if (Init.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005282 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005283
Douglas Gregor43959a92009-08-20 07:17:43 +00005284 // Transform the condition
John McCall60d7b3a2010-08-24 06:29:42 +00005285 ExprResult Cond;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005286 VarDecl *ConditionVar = 0;
5287 if (S->getConditionVariable()) {
Sean Huntc3021132010-05-05 15:23:54 +00005288 ConditionVar
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005289 = cast_or_null<VarDecl>(
Douglas Gregoraac571c2010-03-01 17:25:41 +00005290 getDerived().TransformDefinition(
5291 S->getConditionVariable()->getLocation(),
5292 S->getConditionVariable()));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005293 if (!ConditionVar)
John McCallf312b1e2010-08-26 23:41:50 +00005294 return StmtError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005295 } else {
5296 Cond = getDerived().TransformExpr(S->getCond());
Sean Huntc3021132010-05-05 15:23:54 +00005297
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005298 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005299 return StmtError();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005300
5301 if (S->getCond()) {
5302 // Convert the condition to a boolean value.
Douglas Gregor8491ffe2010-12-20 22:05:00 +00005303 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getForLoc(),
5304 Cond.get());
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005305 if (CondE.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005306 return StmtError();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005307
John McCall9ae2f072010-08-23 23:25:46 +00005308 Cond = CondE.get();
Douglas Gregorafa0fef2010-05-08 23:34:38 +00005309 }
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00005310 }
Mike Stump1eb44332009-09-09 15:08:12 +00005311
John McCall9ae2f072010-08-23 23:25:46 +00005312 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5313 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
John McCallf312b1e2010-08-26 23:41:50 +00005314 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005315
Douglas Gregor43959a92009-08-20 07:17:43 +00005316 // Transform the increment
John McCall60d7b3a2010-08-24 06:29:42 +00005317 ExprResult Inc = getDerived().TransformExpr(S->getInc());
Douglas Gregor43959a92009-08-20 07:17:43 +00005318 if (Inc.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005319 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005320
John McCall9ae2f072010-08-23 23:25:46 +00005321 Sema::FullExprArg FullInc(getSema().MakeFullExpr(Inc.get()));
5322 if (S->getInc() && !FullInc.get())
John McCallf312b1e2010-08-26 23:41:50 +00005323 return StmtError();
Douglas Gregoreaa18e42010-05-08 22:20:28 +00005324
Douglas Gregor43959a92009-08-20 07:17:43 +00005325 // Transform the body
John McCall60d7b3a2010-08-24 06:29:42 +00005326 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregor43959a92009-08-20 07:17:43 +00005327 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005328 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005329
Douglas Gregor43959a92009-08-20 07:17:43 +00005330 if (!getDerived().AlwaysRebuild() &&
5331 Init.get() == S->getInit() &&
John McCall9ae2f072010-08-23 23:25:46 +00005332 FullCond.get() == S->getCond() &&
Douglas Gregor43959a92009-08-20 07:17:43 +00005333 Inc.get() == S->getInc() &&
5334 Body.get() == S->getBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005335 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005336
Douglas Gregor43959a92009-08-20 07:17:43 +00005337 return getDerived().RebuildForStmt(S->getForLoc(), S->getLParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005338 Init.get(), FullCond, ConditionVar,
5339 FullInc, S->getRParenLoc(), Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005340}
5341
5342template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005343StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005344TreeTransform<Derived>::TransformGotoStmt(GotoStmt *S) {
Chris Lattner57ad3782011-02-17 20:34:02 +00005345 Decl *LD = getDerived().TransformDecl(S->getLabel()->getLocation(),
5346 S->getLabel());
5347 if (!LD)
5348 return StmtError();
5349
Douglas Gregor43959a92009-08-20 07:17:43 +00005350 // Goto statements must always be rebuilt, to resolve the label.
Mike Stump1eb44332009-09-09 15:08:12 +00005351 return getDerived().RebuildGotoStmt(S->getGotoLoc(), S->getLabelLoc(),
Chris Lattner57ad3782011-02-17 20:34:02 +00005352 cast<LabelDecl>(LD));
Douglas Gregor43959a92009-08-20 07:17:43 +00005353}
5354
5355template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005356StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005357TreeTransform<Derived>::TransformIndirectGotoStmt(IndirectGotoStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005358 ExprResult Target = getDerived().TransformExpr(S->getTarget());
Douglas Gregor43959a92009-08-20 07:17:43 +00005359 if (Target.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005360 return StmtError();
Eli Friedmand29975f2012-01-31 22:47:07 +00005361 Target = SemaRef.MaybeCreateExprWithCleanups(Target.take());
Mike Stump1eb44332009-09-09 15:08:12 +00005362
Douglas Gregor43959a92009-08-20 07:17:43 +00005363 if (!getDerived().AlwaysRebuild() &&
5364 Target.get() == S->getTarget())
John McCall3fa5cae2010-10-26 07:05:15 +00005365 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005366
5367 return getDerived().RebuildIndirectGotoStmt(S->getGotoLoc(), S->getStarLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005368 Target.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005369}
5370
5371template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005372StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005373TreeTransform<Derived>::TransformContinueStmt(ContinueStmt *S) {
John McCall3fa5cae2010-10-26 07:05:15 +00005374 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005375}
Mike Stump1eb44332009-09-09 15:08:12 +00005376
Douglas Gregor43959a92009-08-20 07:17:43 +00005377template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005378StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005379TreeTransform<Derived>::TransformBreakStmt(BreakStmt *S) {
John McCall3fa5cae2010-10-26 07:05:15 +00005380 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005381}
Mike Stump1eb44332009-09-09 15:08:12 +00005382
Douglas Gregor43959a92009-08-20 07:17:43 +00005383template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005384StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005385TreeTransform<Derived>::TransformReturnStmt(ReturnStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005386 ExprResult Result = getDerived().TransformExpr(S->getRetValue());
Douglas Gregor43959a92009-08-20 07:17:43 +00005387 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005388 return StmtError();
Douglas Gregor43959a92009-08-20 07:17:43 +00005389
Mike Stump1eb44332009-09-09 15:08:12 +00005390 // FIXME: We always rebuild the return statement because there is no way
Douglas Gregor43959a92009-08-20 07:17:43 +00005391 // to tell whether the return type of the function has changed.
John McCall9ae2f072010-08-23 23:25:46 +00005392 return getDerived().RebuildReturnStmt(S->getReturnLoc(), Result.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005393}
Mike Stump1eb44332009-09-09 15:08:12 +00005394
Douglas Gregor43959a92009-08-20 07:17:43 +00005395template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005396StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005397TreeTransform<Derived>::TransformDeclStmt(DeclStmt *S) {
Douglas Gregor43959a92009-08-20 07:17:43 +00005398 bool DeclChanged = false;
Chris Lattner686775d2011-07-20 06:58:45 +00005399 SmallVector<Decl *, 4> Decls;
Douglas Gregor43959a92009-08-20 07:17:43 +00005400 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
5401 D != DEnd; ++D) {
Douglas Gregoraac571c2010-03-01 17:25:41 +00005402 Decl *Transformed = getDerived().TransformDefinition((*D)->getLocation(),
5403 *D);
Douglas Gregor43959a92009-08-20 07:17:43 +00005404 if (!Transformed)
John McCallf312b1e2010-08-26 23:41:50 +00005405 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005406
Douglas Gregor43959a92009-08-20 07:17:43 +00005407 if (Transformed != *D)
5408 DeclChanged = true;
Mike Stump1eb44332009-09-09 15:08:12 +00005409
Douglas Gregor43959a92009-08-20 07:17:43 +00005410 Decls.push_back(Transformed);
5411 }
Mike Stump1eb44332009-09-09 15:08:12 +00005412
Douglas Gregor43959a92009-08-20 07:17:43 +00005413 if (!getDerived().AlwaysRebuild() && !DeclChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00005414 return SemaRef.Owned(S);
Mike Stump1eb44332009-09-09 15:08:12 +00005415
5416 return getDerived().RebuildDeclStmt(Decls.data(), Decls.size(),
Douglas Gregor43959a92009-08-20 07:17:43 +00005417 S->getStartLoc(), S->getEndLoc());
5418}
Mike Stump1eb44332009-09-09 15:08:12 +00005419
Douglas Gregor43959a92009-08-20 07:17:43 +00005420template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005421StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005422TreeTransform<Derived>::TransformAsmStmt(AsmStmt *S) {
Sean Huntc3021132010-05-05 15:23:54 +00005423
John McCallca0408f2010-08-23 06:44:23 +00005424 ASTOwningVector<Expr*> Constraints(getSema());
5425 ASTOwningVector<Expr*> Exprs(getSema());
Chris Lattner686775d2011-07-20 06:58:45 +00005426 SmallVector<IdentifierInfo *, 4> Names;
Anders Carlssona5a79f72010-01-30 20:05:21 +00005427
John McCall60d7b3a2010-08-24 06:29:42 +00005428 ExprResult AsmString;
John McCallca0408f2010-08-23 06:44:23 +00005429 ASTOwningVector<Expr*> Clobbers(getSema());
Anders Carlsson703e3942010-01-24 05:50:09 +00005430
5431 bool ExprsChanged = false;
Sean Huntc3021132010-05-05 15:23:54 +00005432
Anders Carlsson703e3942010-01-24 05:50:09 +00005433 // Go through the outputs.
5434 for (unsigned I = 0, E = S->getNumOutputs(); I != E; ++I) {
Anders Carlssonff93dbd2010-01-30 22:25:16 +00005435 Names.push_back(S->getOutputIdentifier(I));
Sean Huntc3021132010-05-05 15:23:54 +00005436
Anders Carlsson703e3942010-01-24 05:50:09 +00005437 // No need to transform the constraint literal.
John McCall3fa5cae2010-10-26 07:05:15 +00005438 Constraints.push_back(S->getOutputConstraintLiteral(I));
Sean Huntc3021132010-05-05 15:23:54 +00005439
Anders Carlsson703e3942010-01-24 05:50:09 +00005440 // Transform the output expr.
5441 Expr *OutputExpr = S->getOutputExpr(I);
John McCall60d7b3a2010-08-24 06:29:42 +00005442 ExprResult Result = getDerived().TransformExpr(OutputExpr);
Anders Carlsson703e3942010-01-24 05:50:09 +00005443 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005444 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005445
Anders Carlsson703e3942010-01-24 05:50:09 +00005446 ExprsChanged |= Result.get() != OutputExpr;
Sean Huntc3021132010-05-05 15:23:54 +00005447
John McCall9ae2f072010-08-23 23:25:46 +00005448 Exprs.push_back(Result.get());
Anders Carlsson703e3942010-01-24 05:50:09 +00005449 }
Sean Huntc3021132010-05-05 15:23:54 +00005450
Anders Carlsson703e3942010-01-24 05:50:09 +00005451 // Go through the inputs.
5452 for (unsigned I = 0, E = S->getNumInputs(); I != E; ++I) {
Anders Carlssonff93dbd2010-01-30 22:25:16 +00005453 Names.push_back(S->getInputIdentifier(I));
Sean Huntc3021132010-05-05 15:23:54 +00005454
Anders Carlsson703e3942010-01-24 05:50:09 +00005455 // No need to transform the constraint literal.
John McCall3fa5cae2010-10-26 07:05:15 +00005456 Constraints.push_back(S->getInputConstraintLiteral(I));
Sean Huntc3021132010-05-05 15:23:54 +00005457
Anders Carlsson703e3942010-01-24 05:50:09 +00005458 // Transform the input expr.
5459 Expr *InputExpr = S->getInputExpr(I);
John McCall60d7b3a2010-08-24 06:29:42 +00005460 ExprResult Result = getDerived().TransformExpr(InputExpr);
Anders Carlsson703e3942010-01-24 05:50:09 +00005461 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005462 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005463
Anders Carlsson703e3942010-01-24 05:50:09 +00005464 ExprsChanged |= Result.get() != InputExpr;
Sean Huntc3021132010-05-05 15:23:54 +00005465
John McCall9ae2f072010-08-23 23:25:46 +00005466 Exprs.push_back(Result.get());
Anders Carlsson703e3942010-01-24 05:50:09 +00005467 }
Sean Huntc3021132010-05-05 15:23:54 +00005468
Anders Carlsson703e3942010-01-24 05:50:09 +00005469 if (!getDerived().AlwaysRebuild() && !ExprsChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00005470 return SemaRef.Owned(S);
Anders Carlsson703e3942010-01-24 05:50:09 +00005471
5472 // Go through the clobbers.
5473 for (unsigned I = 0, E = S->getNumClobbers(); I != E; ++I)
John McCall3fa5cae2010-10-26 07:05:15 +00005474 Clobbers.push_back(S->getClobber(I));
Anders Carlsson703e3942010-01-24 05:50:09 +00005475
5476 // No need to transform the asm string literal.
5477 AsmString = SemaRef.Owned(S->getAsmString());
5478
5479 return getDerived().RebuildAsmStmt(S->getAsmLoc(),
5480 S->isSimple(),
5481 S->isVolatile(),
5482 S->getNumOutputs(),
5483 S->getNumInputs(),
Anders Carlssona5a79f72010-01-30 20:05:21 +00005484 Names.data(),
Anders Carlsson703e3942010-01-24 05:50:09 +00005485 move_arg(Constraints),
5486 move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00005487 AsmString.get(),
Anders Carlsson703e3942010-01-24 05:50:09 +00005488 move_arg(Clobbers),
5489 S->getRParenLoc(),
5490 S->isMSAsm());
Douglas Gregor43959a92009-08-20 07:17:43 +00005491}
5492
5493
5494template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005495StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005496TreeTransform<Derived>::TransformObjCAtTryStmt(ObjCAtTryStmt *S) {
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005497 // Transform the body of the @try.
John McCall60d7b3a2010-08-24 06:29:42 +00005498 StmtResult TryBody = getDerived().TransformStmt(S->getTryBody());
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005499 if (TryBody.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005500 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005501
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005502 // Transform the @catch statements (if present).
5503 bool AnyCatchChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00005504 ASTOwningVector<Stmt*> CatchStmts(SemaRef);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005505 for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
John McCall60d7b3a2010-08-24 06:29:42 +00005506 StmtResult Catch = getDerived().TransformStmt(S->getCatchStmt(I));
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005507 if (Catch.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005508 return StmtError();
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005509 if (Catch.get() != S->getCatchStmt(I))
5510 AnyCatchChanged = true;
5511 CatchStmts.push_back(Catch.release());
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005512 }
Sean Huntc3021132010-05-05 15:23:54 +00005513
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005514 // Transform the @finally statement (if present).
John McCall60d7b3a2010-08-24 06:29:42 +00005515 StmtResult Finally;
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005516 if (S->getFinallyStmt()) {
5517 Finally = getDerived().TransformStmt(S->getFinallyStmt());
5518 if (Finally.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005519 return StmtError();
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005520 }
5521
5522 // If nothing changed, just retain this statement.
5523 if (!getDerived().AlwaysRebuild() &&
5524 TryBody.get() == S->getTryBody() &&
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005525 !AnyCatchChanged &&
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005526 Finally.get() == S->getFinallyStmt())
John McCall3fa5cae2010-10-26 07:05:15 +00005527 return SemaRef.Owned(S);
Sean Huntc3021132010-05-05 15:23:54 +00005528
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005529 // Build a new statement.
John McCall9ae2f072010-08-23 23:25:46 +00005530 return getDerived().RebuildObjCAtTryStmt(S->getAtTryLoc(), TryBody.get(),
5531 move_arg(CatchStmts), Finally.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005532}
Mike Stump1eb44332009-09-09 15:08:12 +00005533
Douglas Gregor43959a92009-08-20 07:17:43 +00005534template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005535StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005536TreeTransform<Derived>::TransformObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Douglas Gregorbe270a02010-04-26 17:57:08 +00005537 // Transform the @catch parameter, if there is one.
5538 VarDecl *Var = 0;
5539 if (VarDecl *FromVar = S->getCatchParamDecl()) {
5540 TypeSourceInfo *TSInfo = 0;
5541 if (FromVar->getTypeSourceInfo()) {
5542 TSInfo = getDerived().TransformType(FromVar->getTypeSourceInfo());
5543 if (!TSInfo)
John McCallf312b1e2010-08-26 23:41:50 +00005544 return StmtError();
Douglas Gregorbe270a02010-04-26 17:57:08 +00005545 }
Sean Huntc3021132010-05-05 15:23:54 +00005546
Douglas Gregorbe270a02010-04-26 17:57:08 +00005547 QualType T;
5548 if (TSInfo)
5549 T = TSInfo->getType();
5550 else {
5551 T = getDerived().TransformType(FromVar->getType());
5552 if (T.isNull())
John McCallf312b1e2010-08-26 23:41:50 +00005553 return StmtError();
Douglas Gregorbe270a02010-04-26 17:57:08 +00005554 }
Sean Huntc3021132010-05-05 15:23:54 +00005555
Douglas Gregorbe270a02010-04-26 17:57:08 +00005556 Var = getDerived().RebuildObjCExceptionDecl(FromVar, TSInfo, T);
5557 if (!Var)
John McCallf312b1e2010-08-26 23:41:50 +00005558 return StmtError();
Douglas Gregorbe270a02010-04-26 17:57:08 +00005559 }
Sean Huntc3021132010-05-05 15:23:54 +00005560
John McCall60d7b3a2010-08-24 06:29:42 +00005561 StmtResult Body = getDerived().TransformStmt(S->getCatchBody());
Douglas Gregorbe270a02010-04-26 17:57:08 +00005562 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005563 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005564
5565 return getDerived().RebuildObjCAtCatchStmt(S->getAtCatchLoc(),
Douglas Gregorbe270a02010-04-26 17:57:08 +00005566 S->getRParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005567 Var, Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005568}
Mike Stump1eb44332009-09-09 15:08:12 +00005569
Douglas Gregor43959a92009-08-20 07:17:43 +00005570template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005571StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005572TreeTransform<Derived>::TransformObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005573 // Transform the body.
John McCall60d7b3a2010-08-24 06:29:42 +00005574 StmtResult Body = getDerived().TransformStmt(S->getFinallyBody());
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005575 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005576 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005577
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005578 // If nothing changed, just retain this statement.
5579 if (!getDerived().AlwaysRebuild() &&
5580 Body.get() == S->getFinallyBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005581 return SemaRef.Owned(S);
Douglas Gregor4dfdd1b2010-04-22 23:59:56 +00005582
5583 // Build a new statement.
5584 return getDerived().RebuildObjCAtFinallyStmt(S->getAtFinallyLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005585 Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005586}
Mike Stump1eb44332009-09-09 15:08:12 +00005587
Douglas Gregor43959a92009-08-20 07:17:43 +00005588template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005589StmtResult
Mike Stump1eb44332009-09-09 15:08:12 +00005590TreeTransform<Derived>::TransformObjCAtThrowStmt(ObjCAtThrowStmt *S) {
John McCall60d7b3a2010-08-24 06:29:42 +00005591 ExprResult Operand;
Douglas Gregord1377b22010-04-22 21:44:01 +00005592 if (S->getThrowExpr()) {
5593 Operand = getDerived().TransformExpr(S->getThrowExpr());
5594 if (Operand.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005595 return StmtError();
Douglas Gregord1377b22010-04-22 21:44:01 +00005596 }
Sean Huntc3021132010-05-05 15:23:54 +00005597
Douglas Gregord1377b22010-04-22 21:44:01 +00005598 if (!getDerived().AlwaysRebuild() &&
5599 Operand.get() == S->getThrowExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00005600 return getSema().Owned(S);
Sean Huntc3021132010-05-05 15:23:54 +00005601
John McCall9ae2f072010-08-23 23:25:46 +00005602 return getDerived().RebuildObjCAtThrowStmt(S->getThrowLoc(), Operand.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005603}
Mike Stump1eb44332009-09-09 15:08:12 +00005604
Douglas Gregor43959a92009-08-20 07:17:43 +00005605template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005606StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005607TreeTransform<Derived>::TransformObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +00005608 ObjCAtSynchronizedStmt *S) {
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005609 // Transform the object we are locking.
John McCall60d7b3a2010-08-24 06:29:42 +00005610 ExprResult Object = getDerived().TransformExpr(S->getSynchExpr());
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005611 if (Object.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005612 return StmtError();
John McCall07524032011-07-27 21:50:02 +00005613 Object =
5614 getDerived().RebuildObjCAtSynchronizedOperand(S->getAtSynchronizedLoc(),
5615 Object.get());
5616 if (Object.isInvalid())
5617 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005618
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005619 // Transform the body.
John McCall60d7b3a2010-08-24 06:29:42 +00005620 StmtResult Body = getDerived().TransformStmt(S->getSynchBody());
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005621 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005622 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005623
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005624 // If nothing change, just retain the current statement.
5625 if (!getDerived().AlwaysRebuild() &&
5626 Object.get() == S->getSynchExpr() &&
5627 Body.get() == S->getSynchBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005628 return SemaRef.Owned(S);
Douglas Gregor8fdc13a2010-04-22 22:01:21 +00005629
5630 // Build a new statement.
5631 return getDerived().RebuildObjCAtSynchronizedStmt(S->getAtSynchronizedLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005632 Object.get(), Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005633}
5634
5635template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005636StmtResult
John McCallf85e1932011-06-15 23:02:42 +00005637TreeTransform<Derived>::TransformObjCAutoreleasePoolStmt(
5638 ObjCAutoreleasePoolStmt *S) {
5639 // Transform the body.
5640 StmtResult Body = getDerived().TransformStmt(S->getSubStmt());
5641 if (Body.isInvalid())
5642 return StmtError();
5643
5644 // If nothing changed, just retain this statement.
5645 if (!getDerived().AlwaysRebuild() &&
5646 Body.get() == S->getSubStmt())
5647 return SemaRef.Owned(S);
5648
5649 // Build a new statement.
5650 return getDerived().RebuildObjCAutoreleasePoolStmt(
5651 S->getAtLoc(), Body.get());
5652}
5653
5654template<typename Derived>
5655StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005656TreeTransform<Derived>::TransformObjCForCollectionStmt(
Mike Stump1eb44332009-09-09 15:08:12 +00005657 ObjCForCollectionStmt *S) {
Douglas Gregorc3203e72010-04-22 23:10:45 +00005658 // Transform the element statement.
John McCall60d7b3a2010-08-24 06:29:42 +00005659 StmtResult Element = getDerived().TransformStmt(S->getElement());
Douglas Gregorc3203e72010-04-22 23:10:45 +00005660 if (Element.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005661 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005662
Douglas Gregorc3203e72010-04-22 23:10:45 +00005663 // Transform the collection expression.
John McCall60d7b3a2010-08-24 06:29:42 +00005664 ExprResult Collection = getDerived().TransformExpr(S->getCollection());
Douglas Gregorc3203e72010-04-22 23:10:45 +00005665 if (Collection.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005666 return StmtError();
John McCall990567c2011-07-27 01:07:15 +00005667 Collection = getDerived().RebuildObjCForCollectionOperand(S->getForLoc(),
5668 Collection.take());
5669 if (Collection.isInvalid())
5670 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005671
Douglas Gregorc3203e72010-04-22 23:10:45 +00005672 // Transform the body.
John McCall60d7b3a2010-08-24 06:29:42 +00005673 StmtResult Body = getDerived().TransformStmt(S->getBody());
Douglas Gregorc3203e72010-04-22 23:10:45 +00005674 if (Body.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005675 return StmtError();
Sean Huntc3021132010-05-05 15:23:54 +00005676
Douglas Gregorc3203e72010-04-22 23:10:45 +00005677 // If nothing changed, just retain this statement.
5678 if (!getDerived().AlwaysRebuild() &&
5679 Element.get() == S->getElement() &&
5680 Collection.get() == S->getCollection() &&
5681 Body.get() == S->getBody())
John McCall3fa5cae2010-10-26 07:05:15 +00005682 return SemaRef.Owned(S);
Sean Huntc3021132010-05-05 15:23:54 +00005683
Douglas Gregorc3203e72010-04-22 23:10:45 +00005684 // Build a new statement.
5685 return getDerived().RebuildObjCForCollectionStmt(S->getForLoc(),
5686 /*FIXME:*/S->getForLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005687 Element.get(),
5688 Collection.get(),
Douglas Gregorc3203e72010-04-22 23:10:45 +00005689 S->getRParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00005690 Body.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005691}
5692
5693
5694template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005695StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005696TreeTransform<Derived>::TransformCXXCatchStmt(CXXCatchStmt *S) {
5697 // Transform the exception declaration, if any.
5698 VarDecl *Var = 0;
5699 if (S->getExceptionDecl()) {
5700 VarDecl *ExceptionDecl = S->getExceptionDecl();
Douglas Gregor83cb9422010-09-09 17:09:21 +00005701 TypeSourceInfo *T = getDerived().TransformType(
5702 ExceptionDecl->getTypeSourceInfo());
5703 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00005704 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005705
Douglas Gregor83cb9422010-09-09 17:09:21 +00005706 Var = getDerived().RebuildExceptionDecl(ExceptionDecl, T,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005707 ExceptionDecl->getInnerLocStart(),
5708 ExceptionDecl->getLocation(),
5709 ExceptionDecl->getIdentifier());
Douglas Gregorff331c12010-07-25 18:17:45 +00005710 if (!Var || Var->isInvalidDecl())
John McCallf312b1e2010-08-26 23:41:50 +00005711 return StmtError();
Douglas Gregor43959a92009-08-20 07:17:43 +00005712 }
Mike Stump1eb44332009-09-09 15:08:12 +00005713
Douglas Gregor43959a92009-08-20 07:17:43 +00005714 // Transform the actual exception handler.
John McCall60d7b3a2010-08-24 06:29:42 +00005715 StmtResult Handler = getDerived().TransformStmt(S->getHandlerBlock());
Douglas Gregorff331c12010-07-25 18:17:45 +00005716 if (Handler.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005717 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005718
Douglas Gregor43959a92009-08-20 07:17:43 +00005719 if (!getDerived().AlwaysRebuild() &&
5720 !Var &&
5721 Handler.get() == S->getHandlerBlock())
John McCall3fa5cae2010-10-26 07:05:15 +00005722 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005723
5724 return getDerived().RebuildCXXCatchStmt(S->getCatchLoc(),
5725 Var,
John McCall9ae2f072010-08-23 23:25:46 +00005726 Handler.get());
Douglas Gregor43959a92009-08-20 07:17:43 +00005727}
Mike Stump1eb44332009-09-09 15:08:12 +00005728
Douglas Gregor43959a92009-08-20 07:17:43 +00005729template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005730StmtResult
Douglas Gregor43959a92009-08-20 07:17:43 +00005731TreeTransform<Derived>::TransformCXXTryStmt(CXXTryStmt *S) {
5732 // Transform the try block itself.
John McCall60d7b3a2010-08-24 06:29:42 +00005733 StmtResult TryBlock
Douglas Gregor43959a92009-08-20 07:17:43 +00005734 = getDerived().TransformCompoundStmt(S->getTryBlock());
5735 if (TryBlock.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005736 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005737
Douglas Gregor43959a92009-08-20 07:17:43 +00005738 // Transform the handlers.
5739 bool HandlerChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00005740 ASTOwningVector<Stmt*> Handlers(SemaRef);
Douglas Gregor43959a92009-08-20 07:17:43 +00005741 for (unsigned I = 0, N = S->getNumHandlers(); I != N; ++I) {
John McCall60d7b3a2010-08-24 06:29:42 +00005742 StmtResult Handler
Douglas Gregor43959a92009-08-20 07:17:43 +00005743 = getDerived().TransformCXXCatchStmt(S->getHandler(I));
5744 if (Handler.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00005745 return StmtError();
Mike Stump1eb44332009-09-09 15:08:12 +00005746
Douglas Gregor43959a92009-08-20 07:17:43 +00005747 HandlerChanged = HandlerChanged || Handler.get() != S->getHandler(I);
5748 Handlers.push_back(Handler.takeAs<Stmt>());
5749 }
Mike Stump1eb44332009-09-09 15:08:12 +00005750
Douglas Gregor43959a92009-08-20 07:17:43 +00005751 if (!getDerived().AlwaysRebuild() &&
5752 TryBlock.get() == S->getTryBlock() &&
5753 !HandlerChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00005754 return SemaRef.Owned(S);
Douglas Gregor43959a92009-08-20 07:17:43 +00005755
John McCall9ae2f072010-08-23 23:25:46 +00005756 return getDerived().RebuildCXXTryStmt(S->getTryLoc(), TryBlock.get(),
Mike Stump1eb44332009-09-09 15:08:12 +00005757 move_arg(Handlers));
Douglas Gregor43959a92009-08-20 07:17:43 +00005758}
Mike Stump1eb44332009-09-09 15:08:12 +00005759
Richard Smithad762fc2011-04-14 22:09:26 +00005760template<typename Derived>
5761StmtResult
5762TreeTransform<Derived>::TransformCXXForRangeStmt(CXXForRangeStmt *S) {
5763 StmtResult Range = getDerived().TransformStmt(S->getRangeStmt());
5764 if (Range.isInvalid())
5765 return StmtError();
5766
5767 StmtResult BeginEnd = getDerived().TransformStmt(S->getBeginEndStmt());
5768 if (BeginEnd.isInvalid())
5769 return StmtError();
5770
5771 ExprResult Cond = getDerived().TransformExpr(S->getCond());
5772 if (Cond.isInvalid())
5773 return StmtError();
Eli Friedmanc6c14e52012-01-31 22:45:40 +00005774 if (Cond.get())
5775 Cond = SemaRef.CheckBooleanCondition(Cond.take(), S->getColonLoc());
5776 if (Cond.isInvalid())
5777 return StmtError();
5778 if (Cond.get())
5779 Cond = SemaRef.MaybeCreateExprWithCleanups(Cond.take());
Richard Smithad762fc2011-04-14 22:09:26 +00005780
5781 ExprResult Inc = getDerived().TransformExpr(S->getInc());
5782 if (Inc.isInvalid())
5783 return StmtError();
Eli Friedmanc6c14e52012-01-31 22:45:40 +00005784 if (Inc.get())
5785 Inc = SemaRef.MaybeCreateExprWithCleanups(Inc.take());
Richard Smithad762fc2011-04-14 22:09:26 +00005786
5787 StmtResult LoopVar = getDerived().TransformStmt(S->getLoopVarStmt());
5788 if (LoopVar.isInvalid())
5789 return StmtError();
5790
5791 StmtResult NewStmt = S;
5792 if (getDerived().AlwaysRebuild() ||
5793 Range.get() != S->getRangeStmt() ||
5794 BeginEnd.get() != S->getBeginEndStmt() ||
5795 Cond.get() != S->getCond() ||
5796 Inc.get() != S->getInc() ||
5797 LoopVar.get() != S->getLoopVarStmt())
5798 NewStmt = getDerived().RebuildCXXForRangeStmt(S->getForLoc(),
5799 S->getColonLoc(), Range.get(),
5800 BeginEnd.get(), Cond.get(),
5801 Inc.get(), LoopVar.get(),
5802 S->getRParenLoc());
5803
5804 StmtResult Body = getDerived().TransformStmt(S->getBody());
5805 if (Body.isInvalid())
5806 return StmtError();
5807
5808 // Body has changed but we didn't rebuild the for-range statement. Rebuild
5809 // it now so we have a new statement to attach the body to.
5810 if (Body.get() != S->getBody() && NewStmt.get() == S)
5811 NewStmt = getDerived().RebuildCXXForRangeStmt(S->getForLoc(),
5812 S->getColonLoc(), Range.get(),
5813 BeginEnd.get(), Cond.get(),
5814 Inc.get(), LoopVar.get(),
5815 S->getRParenLoc());
5816
5817 if (NewStmt.get() == S)
5818 return SemaRef.Owned(S);
5819
5820 return FinishCXXForRangeStmt(NewStmt.get(), Body.get());
5821}
5822
John Wiegley28bbe4b2011-04-28 01:08:34 +00005823template<typename Derived>
5824StmtResult
Douglas Gregorba0513d2011-10-25 01:33:02 +00005825TreeTransform<Derived>::TransformMSDependentExistsStmt(
5826 MSDependentExistsStmt *S) {
5827 // Transform the nested-name-specifier, if any.
5828 NestedNameSpecifierLoc QualifierLoc;
5829 if (S->getQualifierLoc()) {
5830 QualifierLoc
5831 = getDerived().TransformNestedNameSpecifierLoc(S->getQualifierLoc());
5832 if (!QualifierLoc)
5833 return StmtError();
5834 }
5835
5836 // Transform the declaration name.
5837 DeclarationNameInfo NameInfo = S->getNameInfo();
5838 if (NameInfo.getName()) {
5839 NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo);
5840 if (!NameInfo.getName())
5841 return StmtError();
5842 }
5843
5844 // Check whether anything changed.
5845 if (!getDerived().AlwaysRebuild() &&
5846 QualifierLoc == S->getQualifierLoc() &&
5847 NameInfo.getName() == S->getNameInfo().getName())
5848 return S;
5849
5850 // Determine whether this name exists, if we can.
5851 CXXScopeSpec SS;
5852 SS.Adopt(QualifierLoc);
5853 bool Dependent = false;
5854 switch (getSema().CheckMicrosoftIfExistsSymbol(/*S=*/0, SS, NameInfo)) {
5855 case Sema::IER_Exists:
5856 if (S->isIfExists())
5857 break;
5858
5859 return new (getSema().Context) NullStmt(S->getKeywordLoc());
5860
5861 case Sema::IER_DoesNotExist:
5862 if (S->isIfNotExists())
5863 break;
5864
5865 return new (getSema().Context) NullStmt(S->getKeywordLoc());
5866
5867 case Sema::IER_Dependent:
5868 Dependent = true;
5869 break;
Douglas Gregor65019ac2011-10-25 03:44:56 +00005870
5871 case Sema::IER_Error:
5872 return StmtError();
Douglas Gregorba0513d2011-10-25 01:33:02 +00005873 }
5874
5875 // We need to continue with the instantiation, so do so now.
5876 StmtResult SubStmt = getDerived().TransformCompoundStmt(S->getSubStmt());
5877 if (SubStmt.isInvalid())
5878 return StmtError();
5879
5880 // If we have resolved the name, just transform to the substatement.
5881 if (!Dependent)
5882 return SubStmt;
5883
5884 // The name is still dependent, so build a dependent expression again.
5885 return getDerived().RebuildMSDependentExistsStmt(S->getKeywordLoc(),
5886 S->isIfExists(),
5887 QualifierLoc,
5888 NameInfo,
5889 SubStmt.get());
5890}
5891
5892template<typename Derived>
5893StmtResult
John Wiegley28bbe4b2011-04-28 01:08:34 +00005894TreeTransform<Derived>::TransformSEHTryStmt(SEHTryStmt *S) {
5895 StmtResult TryBlock; // = getDerived().TransformCompoundStmt(S->getTryBlock());
5896 if(TryBlock.isInvalid()) return StmtError();
5897
5898 StmtResult Handler = getDerived().TransformSEHHandler(S->getHandler());
5899 if(!getDerived().AlwaysRebuild() &&
5900 TryBlock.get() == S->getTryBlock() &&
5901 Handler.get() == S->getHandler())
5902 return SemaRef.Owned(S);
5903
5904 return getDerived().RebuildSEHTryStmt(S->getIsCXXTry(),
5905 S->getTryLoc(),
5906 TryBlock.take(),
5907 Handler.take());
5908}
5909
5910template<typename Derived>
5911StmtResult
5912TreeTransform<Derived>::TransformSEHFinallyStmt(SEHFinallyStmt *S) {
5913 StmtResult Block; // = getDerived().TransformCompoundStatement(S->getBlock());
5914 if(Block.isInvalid()) return StmtError();
5915
5916 return getDerived().RebuildSEHFinallyStmt(S->getFinallyLoc(),
5917 Block.take());
5918}
5919
5920template<typename Derived>
5921StmtResult
5922TreeTransform<Derived>::TransformSEHExceptStmt(SEHExceptStmt *S) {
5923 ExprResult FilterExpr = getDerived().TransformExpr(S->getFilterExpr());
5924 if(FilterExpr.isInvalid()) return StmtError();
5925
5926 StmtResult Block; // = getDerived().TransformCompoundStatement(S->getBlock());
5927 if(Block.isInvalid()) return StmtError();
5928
5929 return getDerived().RebuildSEHExceptStmt(S->getExceptLoc(),
5930 FilterExpr.take(),
5931 Block.take());
5932}
5933
5934template<typename Derived>
5935StmtResult
5936TreeTransform<Derived>::TransformSEHHandler(Stmt *Handler) {
5937 if(isa<SEHFinallyStmt>(Handler))
5938 return getDerived().TransformSEHFinallyStmt(cast<SEHFinallyStmt>(Handler));
5939 else
5940 return getDerived().TransformSEHExceptStmt(cast<SEHExceptStmt>(Handler));
5941}
5942
Douglas Gregor43959a92009-08-20 07:17:43 +00005943//===----------------------------------------------------------------------===//
Douglas Gregorb98b1992009-08-11 05:31:07 +00005944// Expression transformation
5945//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00005946template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005947ExprResult
John McCall454feb92009-12-08 09:21:05 +00005948TreeTransform<Derived>::TransformPredefinedExpr(PredefinedExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00005949 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00005950}
Mike Stump1eb44332009-09-09 15:08:12 +00005951
5952template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00005953ExprResult
John McCall454feb92009-12-08 09:21:05 +00005954TreeTransform<Derived>::TransformDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00005955 NestedNameSpecifierLoc QualifierLoc;
5956 if (E->getQualifierLoc()) {
5957 QualifierLoc
5958 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
5959 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00005960 return ExprError();
Douglas Gregora2813ce2009-10-23 18:54:35 +00005961 }
John McCalldbd872f2009-12-08 09:08:17 +00005962
5963 ValueDecl *ND
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00005964 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getLocation(),
5965 E->getDecl()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00005966 if (!ND)
John McCallf312b1e2010-08-26 23:41:50 +00005967 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00005968
John McCallec8045d2010-08-17 21:27:17 +00005969 DeclarationNameInfo NameInfo = E->getNameInfo();
5970 if (NameInfo.getName()) {
5971 NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo);
5972 if (!NameInfo.getName())
John McCallf312b1e2010-08-26 23:41:50 +00005973 return ExprError();
John McCallec8045d2010-08-17 21:27:17 +00005974 }
Abramo Bagnara25777432010-08-11 22:01:17 +00005975
5976 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor40d96a62011-02-28 21:54:11 +00005977 QualifierLoc == E->getQualifierLoc() &&
Douglas Gregora2813ce2009-10-23 18:54:35 +00005978 ND == E->getDecl() &&
Abramo Bagnara25777432010-08-11 22:01:17 +00005979 NameInfo.getName() == E->getDecl()->getDeclName() &&
John McCall096832c2010-08-19 23:49:38 +00005980 !E->hasExplicitTemplateArgs()) {
John McCalldbd872f2009-12-08 09:08:17 +00005981
5982 // Mark it referenced in the new context regardless.
5983 // FIXME: this is a bit instantiation-specific.
Eli Friedman5f2987c2012-02-02 03:46:19 +00005984 SemaRef.MarkDeclRefReferenced(E);
John McCalldbd872f2009-12-08 09:08:17 +00005985
John McCall3fa5cae2010-10-26 07:05:15 +00005986 return SemaRef.Owned(E);
Douglas Gregora2813ce2009-10-23 18:54:35 +00005987 }
John McCalldbd872f2009-12-08 09:08:17 +00005988
5989 TemplateArgumentListInfo TransArgs, *TemplateArgs = 0;
John McCall096832c2010-08-19 23:49:38 +00005990 if (E->hasExplicitTemplateArgs()) {
John McCalldbd872f2009-12-08 09:08:17 +00005991 TemplateArgs = &TransArgs;
5992 TransArgs.setLAngleLoc(E->getLAngleLoc());
5993 TransArgs.setRAngleLoc(E->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00005994 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
5995 E->getNumTemplateArgs(),
5996 TransArgs))
5997 return ExprError();
John McCalldbd872f2009-12-08 09:08:17 +00005998 }
5999
Douglas Gregor40d96a62011-02-28 21:54:11 +00006000 return getDerived().RebuildDeclRefExpr(QualifierLoc, ND, NameInfo,
6001 TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006002}
Mike Stump1eb44332009-09-09 15:08:12 +00006003
Douglas Gregorb98b1992009-08-11 05:31:07 +00006004template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006005ExprResult
John McCall454feb92009-12-08 09:21:05 +00006006TreeTransform<Derived>::TransformIntegerLiteral(IntegerLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006007 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006008}
Mike Stump1eb44332009-09-09 15:08:12 +00006009
Douglas Gregorb98b1992009-08-11 05:31:07 +00006010template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006011ExprResult
John McCall454feb92009-12-08 09:21:05 +00006012TreeTransform<Derived>::TransformFloatingLiteral(FloatingLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006013 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006014}
Mike Stump1eb44332009-09-09 15:08:12 +00006015
Douglas Gregorb98b1992009-08-11 05:31:07 +00006016template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006017ExprResult
John McCall454feb92009-12-08 09:21:05 +00006018TreeTransform<Derived>::TransformImaginaryLiteral(ImaginaryLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006019 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006020}
Mike Stump1eb44332009-09-09 15:08:12 +00006021
Douglas Gregorb98b1992009-08-11 05:31:07 +00006022template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006023ExprResult
John McCall454feb92009-12-08 09:21:05 +00006024TreeTransform<Derived>::TransformStringLiteral(StringLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006025 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006026}
Mike Stump1eb44332009-09-09 15:08:12 +00006027
Douglas Gregorb98b1992009-08-11 05:31:07 +00006028template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006029ExprResult
John McCall454feb92009-12-08 09:21:05 +00006030TreeTransform<Derived>::TransformCharacterLiteral(CharacterLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006031 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006032}
6033
6034template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006035ExprResult
Peter Collingbournef111d932011-04-15 00:35:48 +00006036TreeTransform<Derived>::TransformGenericSelectionExpr(GenericSelectionExpr *E) {
6037 ExprResult ControllingExpr =
6038 getDerived().TransformExpr(E->getControllingExpr());
6039 if (ControllingExpr.isInvalid())
6040 return ExprError();
6041
Chris Lattner686775d2011-07-20 06:58:45 +00006042 SmallVector<Expr *, 4> AssocExprs;
6043 SmallVector<TypeSourceInfo *, 4> AssocTypes;
Peter Collingbournef111d932011-04-15 00:35:48 +00006044 for (unsigned i = 0; i != E->getNumAssocs(); ++i) {
6045 TypeSourceInfo *TS = E->getAssocTypeSourceInfo(i);
6046 if (TS) {
6047 TypeSourceInfo *AssocType = getDerived().TransformType(TS);
6048 if (!AssocType)
6049 return ExprError();
6050 AssocTypes.push_back(AssocType);
6051 } else {
6052 AssocTypes.push_back(0);
6053 }
6054
6055 ExprResult AssocExpr = getDerived().TransformExpr(E->getAssocExpr(i));
6056 if (AssocExpr.isInvalid())
6057 return ExprError();
6058 AssocExprs.push_back(AssocExpr.release());
6059 }
6060
6061 return getDerived().RebuildGenericSelectionExpr(E->getGenericLoc(),
6062 E->getDefaultLoc(),
6063 E->getRParenLoc(),
6064 ControllingExpr.release(),
6065 AssocTypes.data(),
6066 AssocExprs.data(),
6067 E->getNumAssocs());
6068}
6069
6070template<typename Derived>
6071ExprResult
John McCall454feb92009-12-08 09:21:05 +00006072TreeTransform<Derived>::TransformParenExpr(ParenExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006073 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006074 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006075 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006076
Douglas Gregorb98b1992009-08-11 05:31:07 +00006077 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006078 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006079
John McCall9ae2f072010-08-23 23:25:46 +00006080 return getDerived().RebuildParenExpr(SubExpr.get(), E->getLParen(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006081 E->getRParen());
6082}
6083
Mike Stump1eb44332009-09-09 15:08:12 +00006084template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006085ExprResult
John McCall454feb92009-12-08 09:21:05 +00006086TreeTransform<Derived>::TransformUnaryOperator(UnaryOperator *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006087 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006088 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006089 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006090
Douglas Gregorb98b1992009-08-11 05:31:07 +00006091 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006092 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006093
Douglas Gregorb98b1992009-08-11 05:31:07 +00006094 return getDerived().RebuildUnaryOperator(E->getOperatorLoc(),
6095 E->getOpcode(),
John McCall9ae2f072010-08-23 23:25:46 +00006096 SubExpr.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006097}
Mike Stump1eb44332009-09-09 15:08:12 +00006098
Douglas Gregorb98b1992009-08-11 05:31:07 +00006099template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006100ExprResult
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006101TreeTransform<Derived>::TransformOffsetOfExpr(OffsetOfExpr *E) {
6102 // Transform the type.
6103 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeSourceInfo());
6104 if (!Type)
John McCallf312b1e2010-08-26 23:41:50 +00006105 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00006106
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006107 // Transform all of the components into components similar to what the
6108 // parser uses.
Sean Huntc3021132010-05-05 15:23:54 +00006109 // FIXME: It would be slightly more efficient in the non-dependent case to
6110 // just map FieldDecls, rather than requiring the rebuilder to look for
6111 // the fields again. However, __builtin_offsetof is rare enough in
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006112 // template code that we don't care.
6113 bool ExprChanged = false;
John McCallf312b1e2010-08-26 23:41:50 +00006114 typedef Sema::OffsetOfComponent Component;
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006115 typedef OffsetOfExpr::OffsetOfNode Node;
Chris Lattner686775d2011-07-20 06:58:45 +00006116 SmallVector<Component, 4> Components;
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006117 for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
6118 const Node &ON = E->getComponent(I);
6119 Component Comp;
Douglas Gregor72be24f2010-04-30 20:35:01 +00006120 Comp.isBrackets = true;
Abramo Bagnara06dec892011-03-12 09:45:03 +00006121 Comp.LocStart = ON.getSourceRange().getBegin();
6122 Comp.LocEnd = ON.getSourceRange().getEnd();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006123 switch (ON.getKind()) {
6124 case Node::Array: {
6125 Expr *FromIndex = E->getIndexExpr(ON.getArrayExprIndex());
John McCall60d7b3a2010-08-24 06:29:42 +00006126 ExprResult Index = getDerived().TransformExpr(FromIndex);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006127 if (Index.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006128 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00006129
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006130 ExprChanged = ExprChanged || Index.get() != FromIndex;
6131 Comp.isBrackets = true;
John McCall9ae2f072010-08-23 23:25:46 +00006132 Comp.U.E = Index.get();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006133 break;
6134 }
Sean Huntc3021132010-05-05 15:23:54 +00006135
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006136 case Node::Field:
6137 case Node::Identifier:
6138 Comp.isBrackets = false;
6139 Comp.U.IdentInfo = ON.getFieldName();
Douglas Gregor29d2fd52010-04-28 22:43:14 +00006140 if (!Comp.U.IdentInfo)
6141 continue;
Sean Huntc3021132010-05-05 15:23:54 +00006142
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006143 break;
Sean Huntc3021132010-05-05 15:23:54 +00006144
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00006145 case Node::Base:
6146 // Will be recomputed during the rebuild.
6147 continue;
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006148 }
Sean Huntc3021132010-05-05 15:23:54 +00006149
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006150 Components.push_back(Comp);
6151 }
Sean Huntc3021132010-05-05 15:23:54 +00006152
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006153 // If nothing changed, retain the existing expression.
6154 if (!getDerived().AlwaysRebuild() &&
6155 Type == E->getTypeSourceInfo() &&
6156 !ExprChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00006157 return SemaRef.Owned(E);
Sean Huntc3021132010-05-05 15:23:54 +00006158
Douglas Gregor8ecdb652010-04-28 22:16:22 +00006159 // Build a new offsetof expression.
6160 return getDerived().RebuildOffsetOfExpr(E->getOperatorLoc(), Type,
6161 Components.data(), Components.size(),
6162 E->getRParenLoc());
6163}
6164
6165template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006166ExprResult
John McCall7cd7d1a2010-11-15 23:31:06 +00006167TreeTransform<Derived>::TransformOpaqueValueExpr(OpaqueValueExpr *E) {
6168 assert(getDerived().AlreadyTransformed(E->getType()) &&
6169 "opaque value expression requires transformation");
6170 return SemaRef.Owned(E);
6171}
6172
6173template<typename Derived>
6174ExprResult
John McCall4b9c2d22011-11-06 09:01:30 +00006175TreeTransform<Derived>::TransformPseudoObjectExpr(PseudoObjectExpr *E) {
John McCall01e19be2011-11-30 04:42:31 +00006176 // Rebuild the syntactic form. The original syntactic form has
6177 // opaque-value expressions in it, so strip those away and rebuild
6178 // the result. This is a really awful way of doing this, but the
6179 // better solution (rebuilding the semantic expressions and
6180 // rebinding OVEs as necessary) doesn't work; we'd need
6181 // TreeTransform to not strip away implicit conversions.
6182 Expr *newSyntacticForm = SemaRef.recreateSyntacticForm(E);
6183 ExprResult result = getDerived().TransformExpr(newSyntacticForm);
John McCall4b9c2d22011-11-06 09:01:30 +00006184 if (result.isInvalid()) return ExprError();
6185
6186 // If that gives us a pseudo-object result back, the pseudo-object
6187 // expression must have been an lvalue-to-rvalue conversion which we
6188 // should reapply.
6189 if (result.get()->hasPlaceholderType(BuiltinType::PseudoObject))
6190 result = SemaRef.checkPseudoObjectRValue(result.take());
6191
6192 return result;
6193}
6194
6195template<typename Derived>
6196ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00006197TreeTransform<Derived>::TransformUnaryExprOrTypeTraitExpr(
6198 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006199 if (E->isArgumentType()) {
John McCalla93c9342009-12-07 02:54:59 +00006200 TypeSourceInfo *OldT = E->getArgumentTypeInfo();
Douglas Gregor5557b252009-10-28 00:29:27 +00006201
John McCalla93c9342009-12-07 02:54:59 +00006202 TypeSourceInfo *NewT = getDerived().TransformType(OldT);
John McCall5ab75172009-11-04 07:28:41 +00006203 if (!NewT)
John McCallf312b1e2010-08-26 23:41:50 +00006204 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006205
John McCall5ab75172009-11-04 07:28:41 +00006206 if (!getDerived().AlwaysRebuild() && OldT == NewT)
John McCall3fa5cae2010-10-26 07:05:15 +00006207 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006208
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00006209 return getDerived().RebuildUnaryExprOrTypeTrait(NewT, E->getOperatorLoc(),
6210 E->getKind(),
6211 E->getSourceRange());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006212 }
Mike Stump1eb44332009-09-09 15:08:12 +00006213
John McCall60d7b3a2010-08-24 06:29:42 +00006214 ExprResult SubExpr;
Mike Stump1eb44332009-09-09 15:08:12 +00006215 {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006216 // C++0x [expr.sizeof]p1:
6217 // The operand is either an expression, which is an unevaluated operand
6218 // [...]
John McCallf312b1e2010-08-26 23:41:50 +00006219 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00006220
Douglas Gregorb98b1992009-08-11 05:31:07 +00006221 SubExpr = getDerived().TransformExpr(E->getArgumentExpr());
6222 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006223 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006224
Douglas Gregorb98b1992009-08-11 05:31:07 +00006225 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getArgumentExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006226 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006227 }
Mike Stump1eb44332009-09-09 15:08:12 +00006228
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00006229 return getDerived().RebuildUnaryExprOrTypeTrait(SubExpr.get(),
6230 E->getOperatorLoc(),
6231 E->getKind(),
6232 E->getSourceRange());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006233}
Mike Stump1eb44332009-09-09 15:08:12 +00006234
Douglas Gregorb98b1992009-08-11 05:31:07 +00006235template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006236ExprResult
John McCall454feb92009-12-08 09:21:05 +00006237TreeTransform<Derived>::TransformArraySubscriptExpr(ArraySubscriptExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006238 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006239 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006240 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006241
John McCall60d7b3a2010-08-24 06:29:42 +00006242 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006243 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006244 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006245
6246
Douglas Gregorb98b1992009-08-11 05:31:07 +00006247 if (!getDerived().AlwaysRebuild() &&
6248 LHS.get() == E->getLHS() &&
6249 RHS.get() == E->getRHS())
John McCall3fa5cae2010-10-26 07:05:15 +00006250 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006251
John McCall9ae2f072010-08-23 23:25:46 +00006252 return getDerived().RebuildArraySubscriptExpr(LHS.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006253 /*FIXME:*/E->getLHS()->getLocStart(),
John McCall9ae2f072010-08-23 23:25:46 +00006254 RHS.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006255 E->getRBracketLoc());
6256}
Mike Stump1eb44332009-09-09 15:08:12 +00006257
6258template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006259ExprResult
John McCall454feb92009-12-08 09:21:05 +00006260TreeTransform<Derived>::TransformCallExpr(CallExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006261 // Transform the callee.
John McCall60d7b3a2010-08-24 06:29:42 +00006262 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006263 if (Callee.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006264 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00006265
6266 // Transform arguments.
6267 bool ArgChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00006268 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00006269 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
6270 &ArgChanged))
6271 return ExprError();
6272
Douglas Gregorb98b1992009-08-11 05:31:07 +00006273 if (!getDerived().AlwaysRebuild() &&
6274 Callee.get() == E->getCallee() &&
6275 !ArgChanged)
Douglas Gregor92be2a52011-12-10 00:23:21 +00006276 return SemaRef.MaybeBindToTemporary(E);;
Mike Stump1eb44332009-09-09 15:08:12 +00006277
Douglas Gregorb98b1992009-08-11 05:31:07 +00006278 // FIXME: Wrong source location information for the '('.
Mike Stump1eb44332009-09-09 15:08:12 +00006279 SourceLocation FakeLParenLoc
Douglas Gregorb98b1992009-08-11 05:31:07 +00006280 = ((Expr *)Callee.get())->getSourceRange().getBegin();
John McCall9ae2f072010-08-23 23:25:46 +00006281 return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006282 move_arg(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006283 E->getRParenLoc());
6284}
Mike Stump1eb44332009-09-09 15:08:12 +00006285
6286template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006287ExprResult
John McCall454feb92009-12-08 09:21:05 +00006288TreeTransform<Derived>::TransformMemberExpr(MemberExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006289 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006290 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006291 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006292
Douglas Gregor40d96a62011-02-28 21:54:11 +00006293 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00006294 if (E->hasQualifier()) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00006295 QualifierLoc
6296 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
6297
6298 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00006299 return ExprError();
Douglas Gregor83f6faf2009-08-31 23:41:50 +00006300 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00006301 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00006302
Eli Friedmanf595cc42009-12-04 06:40:45 +00006303 ValueDecl *Member
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00006304 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getMemberLoc(),
6305 E->getMemberDecl()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006306 if (!Member)
John McCallf312b1e2010-08-26 23:41:50 +00006307 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006308
John McCall6bb80172010-03-30 21:47:33 +00006309 NamedDecl *FoundDecl = E->getFoundDecl();
6310 if (FoundDecl == E->getMemberDecl()) {
6311 FoundDecl = Member;
6312 } else {
6313 FoundDecl = cast_or_null<NamedDecl>(
6314 getDerived().TransformDecl(E->getMemberLoc(), FoundDecl));
6315 if (!FoundDecl)
John McCallf312b1e2010-08-26 23:41:50 +00006316 return ExprError();
John McCall6bb80172010-03-30 21:47:33 +00006317 }
6318
Douglas Gregorb98b1992009-08-11 05:31:07 +00006319 if (!getDerived().AlwaysRebuild() &&
6320 Base.get() == E->getBase() &&
Douglas Gregor40d96a62011-02-28 21:54:11 +00006321 QualifierLoc == E->getQualifierLoc() &&
Douglas Gregor8a4386b2009-11-04 23:20:05 +00006322 Member == E->getMemberDecl() &&
John McCall6bb80172010-03-30 21:47:33 +00006323 FoundDecl == E->getFoundDecl() &&
John McCall096832c2010-08-19 23:49:38 +00006324 !E->hasExplicitTemplateArgs()) {
Sean Huntc3021132010-05-05 15:23:54 +00006325
Anders Carlsson1f240322009-12-22 05:24:09 +00006326 // Mark it referenced in the new context regardless.
6327 // FIXME: this is a bit instantiation-specific.
Eli Friedman5f2987c2012-02-02 03:46:19 +00006328 SemaRef.MarkMemberReferenced(E);
6329
John McCall3fa5cae2010-10-26 07:05:15 +00006330 return SemaRef.Owned(E);
Anders Carlsson1f240322009-12-22 05:24:09 +00006331 }
Douglas Gregorb98b1992009-08-11 05:31:07 +00006332
John McCalld5532b62009-11-23 01:53:49 +00006333 TemplateArgumentListInfo TransArgs;
John McCall096832c2010-08-19 23:49:38 +00006334 if (E->hasExplicitTemplateArgs()) {
John McCalld5532b62009-11-23 01:53:49 +00006335 TransArgs.setLAngleLoc(E->getLAngleLoc());
6336 TransArgs.setRAngleLoc(E->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00006337 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
6338 E->getNumTemplateArgs(),
6339 TransArgs))
6340 return ExprError();
Douglas Gregor8a4386b2009-11-04 23:20:05 +00006341 }
Sean Huntc3021132010-05-05 15:23:54 +00006342
Douglas Gregorb98b1992009-08-11 05:31:07 +00006343 // FIXME: Bogus source location for the operator
6344 SourceLocation FakeOperatorLoc
6345 = SemaRef.PP.getLocForEndOfToken(E->getBase()->getSourceRange().getEnd());
6346
John McCallc2233c52010-01-15 08:34:02 +00006347 // FIXME: to do this check properly, we will need to preserve the
6348 // first-qualifier-in-scope here, just in case we had a dependent
6349 // base (and therefore couldn't do the check) and a
6350 // nested-name-qualifier (and therefore could do the lookup).
6351 NamedDecl *FirstQualifierInScope = 0;
6352
John McCall9ae2f072010-08-23 23:25:46 +00006353 return getDerived().RebuildMemberExpr(Base.get(), FakeOperatorLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006354 E->isArrow(),
Douglas Gregor40d96a62011-02-28 21:54:11 +00006355 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00006356 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00006357 E->getMemberNameInfo(),
Douglas Gregor8a4386b2009-11-04 23:20:05 +00006358 Member,
John McCall6bb80172010-03-30 21:47:33 +00006359 FoundDecl,
John McCall096832c2010-08-19 23:49:38 +00006360 (E->hasExplicitTemplateArgs()
John McCalld5532b62009-11-23 01:53:49 +00006361 ? &TransArgs : 0),
John McCallc2233c52010-01-15 08:34:02 +00006362 FirstQualifierInScope);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006363}
Mike Stump1eb44332009-09-09 15:08:12 +00006364
Douglas Gregorb98b1992009-08-11 05:31:07 +00006365template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006366ExprResult
John McCall454feb92009-12-08 09:21:05 +00006367TreeTransform<Derived>::TransformBinaryOperator(BinaryOperator *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006368 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006369 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006370 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006371
John McCall60d7b3a2010-08-24 06:29:42 +00006372 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006373 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006374 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006375
Douglas Gregorb98b1992009-08-11 05:31:07 +00006376 if (!getDerived().AlwaysRebuild() &&
6377 LHS.get() == E->getLHS() &&
6378 RHS.get() == E->getRHS())
John McCall3fa5cae2010-10-26 07:05:15 +00006379 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006380
Douglas Gregorb98b1992009-08-11 05:31:07 +00006381 return getDerived().RebuildBinaryOperator(E->getOperatorLoc(), E->getOpcode(),
John McCall9ae2f072010-08-23 23:25:46 +00006382 LHS.get(), RHS.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006383}
6384
Mike Stump1eb44332009-09-09 15:08:12 +00006385template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006386ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00006387TreeTransform<Derived>::TransformCompoundAssignOperator(
John McCall454feb92009-12-08 09:21:05 +00006388 CompoundAssignOperator *E) {
6389 return getDerived().TransformBinaryOperator(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006390}
Mike Stump1eb44332009-09-09 15:08:12 +00006391
Douglas Gregorb98b1992009-08-11 05:31:07 +00006392template<typename Derived>
John McCall56ca35d2011-02-17 10:25:35 +00006393ExprResult TreeTransform<Derived>::
6394TransformBinaryConditionalOperator(BinaryConditionalOperator *e) {
6395 // Just rebuild the common and RHS expressions and see whether we
6396 // get any changes.
6397
6398 ExprResult commonExpr = getDerived().TransformExpr(e->getCommon());
6399 if (commonExpr.isInvalid())
6400 return ExprError();
6401
6402 ExprResult rhs = getDerived().TransformExpr(e->getFalseExpr());
6403 if (rhs.isInvalid())
6404 return ExprError();
6405
6406 if (!getDerived().AlwaysRebuild() &&
6407 commonExpr.get() == e->getCommon() &&
6408 rhs.get() == e->getFalseExpr())
6409 return SemaRef.Owned(e);
6410
6411 return getDerived().RebuildConditionalOperator(commonExpr.take(),
6412 e->getQuestionLoc(),
6413 0,
6414 e->getColonLoc(),
6415 rhs.get());
6416}
6417
6418template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006419ExprResult
John McCall454feb92009-12-08 09:21:05 +00006420TreeTransform<Derived>::TransformConditionalOperator(ConditionalOperator *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006421 ExprResult Cond = getDerived().TransformExpr(E->getCond());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006422 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006423 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006424
John McCall60d7b3a2010-08-24 06:29:42 +00006425 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006426 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006427 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006428
John McCall60d7b3a2010-08-24 06:29:42 +00006429 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006430 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006431 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006432
Douglas Gregorb98b1992009-08-11 05:31:07 +00006433 if (!getDerived().AlwaysRebuild() &&
6434 Cond.get() == E->getCond() &&
6435 LHS.get() == E->getLHS() &&
6436 RHS.get() == E->getRHS())
John McCall3fa5cae2010-10-26 07:05:15 +00006437 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006438
John McCall9ae2f072010-08-23 23:25:46 +00006439 return getDerived().RebuildConditionalOperator(Cond.get(),
Douglas Gregor47e1f7c2009-08-26 14:37:04 +00006440 E->getQuestionLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006441 LHS.get(),
Douglas Gregor47e1f7c2009-08-26 14:37:04 +00006442 E->getColonLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006443 RHS.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006444}
Mike Stump1eb44332009-09-09 15:08:12 +00006445
6446template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006447ExprResult
John McCall454feb92009-12-08 09:21:05 +00006448TreeTransform<Derived>::TransformImplicitCastExpr(ImplicitCastExpr *E) {
Douglas Gregora88cfbf2009-12-12 18:16:41 +00006449 // Implicit casts are eliminated during transformation, since they
6450 // will be recomputed by semantic analysis after transformation.
Douglas Gregor6eef5192009-12-14 19:27:10 +00006451 return getDerived().TransformExpr(E->getSubExprAsWritten());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006452}
Mike Stump1eb44332009-09-09 15:08:12 +00006453
Douglas Gregorb98b1992009-08-11 05:31:07 +00006454template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006455ExprResult
John McCall454feb92009-12-08 09:21:05 +00006456TreeTransform<Derived>::TransformCStyleCastExpr(CStyleCastExpr *E) {
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006457 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6458 if (!Type)
6459 return ExprError();
6460
John McCall60d7b3a2010-08-24 06:29:42 +00006461 ExprResult SubExpr
Douglas Gregor6eef5192009-12-14 19:27:10 +00006462 = getDerived().TransformExpr(E->getSubExprAsWritten());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006463 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006464 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006465
Douglas Gregorb98b1992009-08-11 05:31:07 +00006466 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006467 Type == E->getTypeInfoAsWritten() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006468 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006469 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006470
John McCall9d125032010-01-15 18:39:57 +00006471 return getDerived().RebuildCStyleCastExpr(E->getLParenLoc(),
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006472 Type,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006473 E->getRParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006474 SubExpr.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006475}
Mike Stump1eb44332009-09-09 15:08:12 +00006476
Douglas Gregorb98b1992009-08-11 05:31:07 +00006477template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006478ExprResult
John McCall454feb92009-12-08 09:21:05 +00006479TreeTransform<Derived>::TransformCompoundLiteralExpr(CompoundLiteralExpr *E) {
John McCall42f56b52010-01-18 19:35:47 +00006480 TypeSourceInfo *OldT = E->getTypeSourceInfo();
6481 TypeSourceInfo *NewT = getDerived().TransformType(OldT);
6482 if (!NewT)
John McCallf312b1e2010-08-26 23:41:50 +00006483 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006484
John McCall60d7b3a2010-08-24 06:29:42 +00006485 ExprResult Init = getDerived().TransformExpr(E->getInitializer());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006486 if (Init.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006487 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006488
Douglas Gregorb98b1992009-08-11 05:31:07 +00006489 if (!getDerived().AlwaysRebuild() &&
John McCall42f56b52010-01-18 19:35:47 +00006490 OldT == NewT &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006491 Init.get() == E->getInitializer())
Douglas Gregor92be2a52011-12-10 00:23:21 +00006492 return SemaRef.MaybeBindToTemporary(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006493
John McCall1d7d8d62010-01-19 22:33:45 +00006494 // Note: the expression type doesn't necessarily match the
6495 // type-as-written, but that's okay, because it should always be
6496 // derivable from the initializer.
6497
John McCall42f56b52010-01-18 19:35:47 +00006498 return getDerived().RebuildCompoundLiteralExpr(E->getLParenLoc(), NewT,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006499 /*FIXME:*/E->getInitializer()->getLocEnd(),
John McCall9ae2f072010-08-23 23:25:46 +00006500 Init.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006501}
Mike Stump1eb44332009-09-09 15:08:12 +00006502
Douglas Gregorb98b1992009-08-11 05:31:07 +00006503template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006504ExprResult
John McCall454feb92009-12-08 09:21:05 +00006505TreeTransform<Derived>::TransformExtVectorElementExpr(ExtVectorElementExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006506 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006507 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006508 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006509
Douglas Gregorb98b1992009-08-11 05:31:07 +00006510 if (!getDerived().AlwaysRebuild() &&
6511 Base.get() == E->getBase())
John McCall3fa5cae2010-10-26 07:05:15 +00006512 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006513
Douglas Gregorb98b1992009-08-11 05:31:07 +00006514 // FIXME: Bad source location
Mike Stump1eb44332009-09-09 15:08:12 +00006515 SourceLocation FakeOperatorLoc
Douglas Gregorb98b1992009-08-11 05:31:07 +00006516 = SemaRef.PP.getLocForEndOfToken(E->getBase()->getLocEnd());
John McCall9ae2f072010-08-23 23:25:46 +00006517 return getDerived().RebuildExtVectorElementExpr(Base.get(), FakeOperatorLoc,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006518 E->getAccessorLoc(),
6519 E->getAccessor());
6520}
Mike Stump1eb44332009-09-09 15:08:12 +00006521
Douglas Gregorb98b1992009-08-11 05:31:07 +00006522template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006523ExprResult
John McCall454feb92009-12-08 09:21:05 +00006524TreeTransform<Derived>::TransformInitListExpr(InitListExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006525 bool InitChanged = false;
Mike Stump1eb44332009-09-09 15:08:12 +00006526
John McCallca0408f2010-08-23 06:44:23 +00006527 ASTOwningVector<Expr*, 4> Inits(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00006528 if (getDerived().TransformExprs(E->getInits(), E->getNumInits(), false,
6529 Inits, &InitChanged))
6530 return ExprError();
6531
Douglas Gregorb98b1992009-08-11 05:31:07 +00006532 if (!getDerived().AlwaysRebuild() && !InitChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00006533 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006534
Douglas Gregorb98b1992009-08-11 05:31:07 +00006535 return getDerived().RebuildInitList(E->getLBraceLoc(), move_arg(Inits),
Douglas Gregore48319a2009-11-09 17:16:50 +00006536 E->getRBraceLoc(), E->getType());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006537}
Mike Stump1eb44332009-09-09 15:08:12 +00006538
Douglas Gregorb98b1992009-08-11 05:31:07 +00006539template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006540ExprResult
John McCall454feb92009-12-08 09:21:05 +00006541TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006542 Designation Desig;
Mike Stump1eb44332009-09-09 15:08:12 +00006543
Douglas Gregor43959a92009-08-20 07:17:43 +00006544 // transform the initializer value
John McCall60d7b3a2010-08-24 06:29:42 +00006545 ExprResult Init = getDerived().TransformExpr(E->getInit());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006546 if (Init.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006547 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006548
Douglas Gregor43959a92009-08-20 07:17:43 +00006549 // transform the designators.
John McCallca0408f2010-08-23 06:44:23 +00006550 ASTOwningVector<Expr*, 4> ArrayExprs(SemaRef);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006551 bool ExprChanged = false;
6552 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
6553 DEnd = E->designators_end();
6554 D != DEnd; ++D) {
6555 if (D->isFieldDesignator()) {
6556 Desig.AddDesignator(Designator::getField(D->getFieldName(),
6557 D->getDotLoc(),
6558 D->getFieldLoc()));
6559 continue;
6560 }
Mike Stump1eb44332009-09-09 15:08:12 +00006561
Douglas Gregorb98b1992009-08-11 05:31:07 +00006562 if (D->isArrayDesignator()) {
John McCall60d7b3a2010-08-24 06:29:42 +00006563 ExprResult Index = getDerived().TransformExpr(E->getArrayIndex(*D));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006564 if (Index.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006565 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006566
6567 Desig.AddDesignator(Designator::getArray(Index.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006568 D->getLBracketLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +00006569
Douglas Gregorb98b1992009-08-11 05:31:07 +00006570 ExprChanged = ExprChanged || Init.get() != E->getArrayIndex(*D);
6571 ArrayExprs.push_back(Index.release());
6572 continue;
6573 }
Mike Stump1eb44332009-09-09 15:08:12 +00006574
Douglas Gregorb98b1992009-08-11 05:31:07 +00006575 assert(D->isArrayRangeDesignator() && "New kind of designator?");
John McCall60d7b3a2010-08-24 06:29:42 +00006576 ExprResult Start
Douglas Gregorb98b1992009-08-11 05:31:07 +00006577 = getDerived().TransformExpr(E->getArrayRangeStart(*D));
6578 if (Start.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006579 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006580
John McCall60d7b3a2010-08-24 06:29:42 +00006581 ExprResult End = getDerived().TransformExpr(E->getArrayRangeEnd(*D));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006582 if (End.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006583 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006584
6585 Desig.AddDesignator(Designator::getArrayRange(Start.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006586 End.get(),
6587 D->getLBracketLoc(),
6588 D->getEllipsisLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +00006589
Douglas Gregorb98b1992009-08-11 05:31:07 +00006590 ExprChanged = ExprChanged || Start.get() != E->getArrayRangeStart(*D) ||
6591 End.get() != E->getArrayRangeEnd(*D);
Mike Stump1eb44332009-09-09 15:08:12 +00006592
Douglas Gregorb98b1992009-08-11 05:31:07 +00006593 ArrayExprs.push_back(Start.release());
6594 ArrayExprs.push_back(End.release());
6595 }
Mike Stump1eb44332009-09-09 15:08:12 +00006596
Douglas Gregorb98b1992009-08-11 05:31:07 +00006597 if (!getDerived().AlwaysRebuild() &&
6598 Init.get() == E->getInit() &&
6599 !ExprChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00006600 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006601
Douglas Gregorb98b1992009-08-11 05:31:07 +00006602 return getDerived().RebuildDesignatedInitExpr(Desig, move_arg(ArrayExprs),
6603 E->getEqualOrColonLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006604 E->usesGNUSyntax(), Init.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006605}
Mike Stump1eb44332009-09-09 15:08:12 +00006606
Douglas Gregorb98b1992009-08-11 05:31:07 +00006607template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006608ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00006609TreeTransform<Derived>::TransformImplicitValueInitExpr(
John McCall454feb92009-12-08 09:21:05 +00006610 ImplicitValueInitExpr *E) {
Douglas Gregor5557b252009-10-28 00:29:27 +00006611 TemporaryBase Rebase(*this, E->getLocStart(), DeclarationName());
Sean Huntc3021132010-05-05 15:23:54 +00006612
Douglas Gregor5557b252009-10-28 00:29:27 +00006613 // FIXME: Will we ever have proper type location here? Will we actually
6614 // need to transform the type?
Douglas Gregorb98b1992009-08-11 05:31:07 +00006615 QualType T = getDerived().TransformType(E->getType());
6616 if (T.isNull())
John McCallf312b1e2010-08-26 23:41:50 +00006617 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006618
Douglas Gregorb98b1992009-08-11 05:31:07 +00006619 if (!getDerived().AlwaysRebuild() &&
6620 T == E->getType())
John McCall3fa5cae2010-10-26 07:05:15 +00006621 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006622
Douglas Gregorb98b1992009-08-11 05:31:07 +00006623 return getDerived().RebuildImplicitValueInitExpr(T);
6624}
Mike Stump1eb44332009-09-09 15:08:12 +00006625
Douglas Gregorb98b1992009-08-11 05:31:07 +00006626template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006627ExprResult
John McCall454feb92009-12-08 09:21:05 +00006628TreeTransform<Derived>::TransformVAArgExpr(VAArgExpr *E) {
Douglas Gregor9bcd4d42010-08-10 14:27:00 +00006629 TypeSourceInfo *TInfo = getDerived().TransformType(E->getWrittenTypeInfo());
6630 if (!TInfo)
John McCallf312b1e2010-08-26 23:41:50 +00006631 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006632
John McCall60d7b3a2010-08-24 06:29:42 +00006633 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006634 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006635 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006636
Douglas Gregorb98b1992009-08-11 05:31:07 +00006637 if (!getDerived().AlwaysRebuild() &&
Abramo Bagnara2cad9002010-08-10 10:06:15 +00006638 TInfo == E->getWrittenTypeInfo() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006639 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006640 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006641
John McCall9ae2f072010-08-23 23:25:46 +00006642 return getDerived().RebuildVAArgExpr(E->getBuiltinLoc(), SubExpr.get(),
Abramo Bagnara2cad9002010-08-10 10:06:15 +00006643 TInfo, E->getRParenLoc());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006644}
6645
6646template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006647ExprResult
John McCall454feb92009-12-08 09:21:05 +00006648TreeTransform<Derived>::TransformParenListExpr(ParenListExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006649 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00006650 ASTOwningVector<Expr*, 4> Inits(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00006651 if (TransformExprs(E->getExprs(), E->getNumExprs(), true, Inits,
6652 &ArgumentChanged))
6653 return ExprError();
6654
Douglas Gregorb98b1992009-08-11 05:31:07 +00006655 return getDerived().RebuildParenListExpr(E->getLParenLoc(),
6656 move_arg(Inits),
6657 E->getRParenLoc());
6658}
Mike Stump1eb44332009-09-09 15:08:12 +00006659
Douglas Gregorb98b1992009-08-11 05:31:07 +00006660/// \brief Transform an address-of-label expression.
6661///
6662/// By default, the transformation of an address-of-label expression always
6663/// rebuilds the expression, so that the label identifier can be resolved to
6664/// the corresponding label statement by semantic analysis.
6665template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006666ExprResult
John McCall454feb92009-12-08 09:21:05 +00006667TreeTransform<Derived>::TransformAddrLabelExpr(AddrLabelExpr *E) {
Chris Lattner57ad3782011-02-17 20:34:02 +00006668 Decl *LD = getDerived().TransformDecl(E->getLabel()->getLocation(),
6669 E->getLabel());
6670 if (!LD)
6671 return ExprError();
6672
Douglas Gregorb98b1992009-08-11 05:31:07 +00006673 return getDerived().RebuildAddrLabelExpr(E->getAmpAmpLoc(), E->getLabelLoc(),
Chris Lattner57ad3782011-02-17 20:34:02 +00006674 cast<LabelDecl>(LD));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006675}
Mike Stump1eb44332009-09-09 15:08:12 +00006676
6677template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006678ExprResult
John McCall454feb92009-12-08 09:21:05 +00006679TreeTransform<Derived>::TransformStmtExpr(StmtExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006680 StmtResult SubStmt
Douglas Gregorb98b1992009-08-11 05:31:07 +00006681 = getDerived().TransformCompoundStmt(E->getSubStmt(), true);
6682 if (SubStmt.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006683 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006684
Douglas Gregorb98b1992009-08-11 05:31:07 +00006685 if (!getDerived().AlwaysRebuild() &&
6686 SubStmt.get() == E->getSubStmt())
Douglas Gregor92be2a52011-12-10 00:23:21 +00006687 return SemaRef.MaybeBindToTemporary(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006688
6689 return getDerived().RebuildStmtExpr(E->getLParenLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006690 SubStmt.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006691 E->getRParenLoc());
6692}
Mike Stump1eb44332009-09-09 15:08:12 +00006693
Douglas Gregorb98b1992009-08-11 05:31:07 +00006694template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006695ExprResult
John McCall454feb92009-12-08 09:21:05 +00006696TreeTransform<Derived>::TransformChooseExpr(ChooseExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00006697 ExprResult Cond = getDerived().TransformExpr(E->getCond());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006698 if (Cond.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006699 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006700
John McCall60d7b3a2010-08-24 06:29:42 +00006701 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006702 if (LHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006703 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006704
John McCall60d7b3a2010-08-24 06:29:42 +00006705 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006706 if (RHS.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006707 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006708
Douglas Gregorb98b1992009-08-11 05:31:07 +00006709 if (!getDerived().AlwaysRebuild() &&
6710 Cond.get() == E->getCond() &&
6711 LHS.get() == E->getLHS() &&
6712 RHS.get() == E->getRHS())
John McCall3fa5cae2010-10-26 07:05:15 +00006713 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006714
Douglas Gregorb98b1992009-08-11 05:31:07 +00006715 return getDerived().RebuildChooseExpr(E->getBuiltinLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006716 Cond.get(), LHS.get(), RHS.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006717 E->getRParenLoc());
6718}
Mike Stump1eb44332009-09-09 15:08:12 +00006719
Douglas Gregorb98b1992009-08-11 05:31:07 +00006720template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006721ExprResult
John McCall454feb92009-12-08 09:21:05 +00006722TreeTransform<Derived>::TransformGNUNullExpr(GNUNullExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00006723 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006724}
6725
6726template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006727ExprResult
John McCall454feb92009-12-08 09:21:05 +00006728TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
Douglas Gregor668d6d92009-12-13 20:44:55 +00006729 switch (E->getOperator()) {
6730 case OO_New:
6731 case OO_Delete:
6732 case OO_Array_New:
6733 case OO_Array_Delete:
6734 llvm_unreachable("new and delete operators cannot use CXXOperatorCallExpr");
Sean Huntc3021132010-05-05 15:23:54 +00006735
Douglas Gregor668d6d92009-12-13 20:44:55 +00006736 case OO_Call: {
6737 // This is a call to an object's operator().
6738 assert(E->getNumArgs() >= 1 && "Object call is missing arguments");
6739
6740 // Transform the object itself.
John McCall60d7b3a2010-08-24 06:29:42 +00006741 ExprResult Object = getDerived().TransformExpr(E->getArg(0));
Douglas Gregor668d6d92009-12-13 20:44:55 +00006742 if (Object.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006743 return ExprError();
Douglas Gregor668d6d92009-12-13 20:44:55 +00006744
6745 // FIXME: Poor location information
6746 SourceLocation FakeLParenLoc
6747 = SemaRef.PP.getLocForEndOfToken(
6748 static_cast<Expr *>(Object.get())->getLocEnd());
6749
6750 // Transform the call arguments.
John McCallca0408f2010-08-23 06:44:23 +00006751 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00006752 if (getDerived().TransformExprs(E->getArgs() + 1, E->getNumArgs() - 1, true,
6753 Args))
6754 return ExprError();
Douglas Gregor668d6d92009-12-13 20:44:55 +00006755
John McCall9ae2f072010-08-23 23:25:46 +00006756 return getDerived().RebuildCallExpr(Object.get(), FakeLParenLoc,
Douglas Gregor668d6d92009-12-13 20:44:55 +00006757 move_arg(Args),
Douglas Gregor668d6d92009-12-13 20:44:55 +00006758 E->getLocEnd());
6759 }
6760
6761#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
6762 case OO_##Name:
6763#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
6764#include "clang/Basic/OperatorKinds.def"
6765 case OO_Subscript:
6766 // Handled below.
6767 break;
6768
6769 case OO_Conditional:
6770 llvm_unreachable("conditional operator is not actually overloadable");
Douglas Gregor668d6d92009-12-13 20:44:55 +00006771
6772 case OO_None:
6773 case NUM_OVERLOADED_OPERATORS:
6774 llvm_unreachable("not an overloaded operator?");
Douglas Gregor668d6d92009-12-13 20:44:55 +00006775 }
6776
John McCall60d7b3a2010-08-24 06:29:42 +00006777 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006778 if (Callee.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006779 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006780
John McCall60d7b3a2010-08-24 06:29:42 +00006781 ExprResult First = getDerived().TransformExpr(E->getArg(0));
Douglas Gregorb98b1992009-08-11 05:31:07 +00006782 if (First.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006783 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00006784
John McCall60d7b3a2010-08-24 06:29:42 +00006785 ExprResult Second;
Douglas Gregorb98b1992009-08-11 05:31:07 +00006786 if (E->getNumArgs() == 2) {
6787 Second = getDerived().TransformExpr(E->getArg(1));
6788 if (Second.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006789 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00006790 }
Mike Stump1eb44332009-09-09 15:08:12 +00006791
Douglas Gregorb98b1992009-08-11 05:31:07 +00006792 if (!getDerived().AlwaysRebuild() &&
6793 Callee.get() == E->getCallee() &&
6794 First.get() == E->getArg(0) &&
Mike Stump1eb44332009-09-09 15:08:12 +00006795 (E->getNumArgs() != 2 || Second.get() == E->getArg(1)))
Douglas Gregor92be2a52011-12-10 00:23:21 +00006796 return SemaRef.MaybeBindToTemporary(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006797
Douglas Gregorb98b1992009-08-11 05:31:07 +00006798 return getDerived().RebuildCXXOperatorCallExpr(E->getOperator(),
6799 E->getOperatorLoc(),
John McCall9ae2f072010-08-23 23:25:46 +00006800 Callee.get(),
6801 First.get(),
6802 Second.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006803}
Mike Stump1eb44332009-09-09 15:08:12 +00006804
Douglas Gregorb98b1992009-08-11 05:31:07 +00006805template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006806ExprResult
John McCall454feb92009-12-08 09:21:05 +00006807TreeTransform<Derived>::TransformCXXMemberCallExpr(CXXMemberCallExpr *E) {
6808 return getDerived().TransformCallExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006809}
Mike Stump1eb44332009-09-09 15:08:12 +00006810
Douglas Gregorb98b1992009-08-11 05:31:07 +00006811template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006812ExprResult
Peter Collingbournee08ce652011-02-09 21:07:24 +00006813TreeTransform<Derived>::TransformCUDAKernelCallExpr(CUDAKernelCallExpr *E) {
6814 // Transform the callee.
6815 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
6816 if (Callee.isInvalid())
6817 return ExprError();
6818
6819 // Transform exec config.
6820 ExprResult EC = getDerived().TransformCallExpr(E->getConfig());
6821 if (EC.isInvalid())
6822 return ExprError();
6823
6824 // Transform arguments.
6825 bool ArgChanged = false;
6826 ASTOwningVector<Expr*> Args(SemaRef);
6827 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
6828 &ArgChanged))
6829 return ExprError();
6830
6831 if (!getDerived().AlwaysRebuild() &&
6832 Callee.get() == E->getCallee() &&
6833 !ArgChanged)
Douglas Gregor92be2a52011-12-10 00:23:21 +00006834 return SemaRef.MaybeBindToTemporary(E);
Peter Collingbournee08ce652011-02-09 21:07:24 +00006835
6836 // FIXME: Wrong source location information for the '('.
6837 SourceLocation FakeLParenLoc
6838 = ((Expr *)Callee.get())->getSourceRange().getBegin();
6839 return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc,
6840 move_arg(Args),
6841 E->getRParenLoc(), EC.get());
6842}
6843
6844template<typename Derived>
6845ExprResult
John McCall454feb92009-12-08 09:21:05 +00006846TreeTransform<Derived>::TransformCXXNamedCastExpr(CXXNamedCastExpr *E) {
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006847 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6848 if (!Type)
6849 return ExprError();
6850
John McCall60d7b3a2010-08-24 06:29:42 +00006851 ExprResult SubExpr
Douglas Gregor6eef5192009-12-14 19:27:10 +00006852 = getDerived().TransformExpr(E->getSubExprAsWritten());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006853 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006854 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006855
Douglas Gregorb98b1992009-08-11 05:31:07 +00006856 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006857 Type == E->getTypeInfoAsWritten() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006858 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006859 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006860
Douglas Gregorb98b1992009-08-11 05:31:07 +00006861 // FIXME: Poor source location information here.
Mike Stump1eb44332009-09-09 15:08:12 +00006862 SourceLocation FakeLAngleLoc
Douglas Gregorb98b1992009-08-11 05:31:07 +00006863 = SemaRef.PP.getLocForEndOfToken(E->getOperatorLoc());
6864 SourceLocation FakeRAngleLoc = E->getSubExpr()->getSourceRange().getBegin();
6865 SourceLocation FakeRParenLoc
6866 = SemaRef.PP.getLocForEndOfToken(
6867 E->getSubExpr()->getSourceRange().getEnd());
6868 return getDerived().RebuildCXXNamedCastExpr(E->getOperatorLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +00006869 E->getStmtClass(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006870 FakeLAngleLoc,
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006871 Type,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006872 FakeRAngleLoc,
6873 FakeRAngleLoc,
John McCall9ae2f072010-08-23 23:25:46 +00006874 SubExpr.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006875 FakeRParenLoc);
6876}
Mike Stump1eb44332009-09-09 15:08:12 +00006877
Douglas Gregorb98b1992009-08-11 05:31:07 +00006878template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006879ExprResult
John McCall454feb92009-12-08 09:21:05 +00006880TreeTransform<Derived>::TransformCXXStaticCastExpr(CXXStaticCastExpr *E) {
6881 return getDerived().TransformCXXNamedCastExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006882}
Mike Stump1eb44332009-09-09 15:08:12 +00006883
6884template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006885ExprResult
John McCall454feb92009-12-08 09:21:05 +00006886TreeTransform<Derived>::TransformCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
6887 return getDerived().TransformCXXNamedCastExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006888}
6889
Douglas Gregorb98b1992009-08-11 05:31:07 +00006890template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006891ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00006892TreeTransform<Derived>::TransformCXXReinterpretCastExpr(
John McCall454feb92009-12-08 09:21:05 +00006893 CXXReinterpretCastExpr *E) {
6894 return getDerived().TransformCXXNamedCastExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006895}
Mike Stump1eb44332009-09-09 15:08:12 +00006896
Douglas Gregorb98b1992009-08-11 05:31:07 +00006897template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006898ExprResult
John McCall454feb92009-12-08 09:21:05 +00006899TreeTransform<Derived>::TransformCXXConstCastExpr(CXXConstCastExpr *E) {
6900 return getDerived().TransformCXXNamedCastExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00006901}
Mike Stump1eb44332009-09-09 15:08:12 +00006902
Douglas Gregorb98b1992009-08-11 05:31:07 +00006903template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006904ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00006905TreeTransform<Derived>::TransformCXXFunctionalCastExpr(
John McCall454feb92009-12-08 09:21:05 +00006906 CXXFunctionalCastExpr *E) {
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006907 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6908 if (!Type)
6909 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006910
John McCall60d7b3a2010-08-24 06:29:42 +00006911 ExprResult SubExpr
Douglas Gregor6eef5192009-12-14 19:27:10 +00006912 = getDerived().TransformExpr(E->getSubExprAsWritten());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006913 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006914 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006915
Douglas Gregorb98b1992009-08-11 05:31:07 +00006916 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006917 Type == E->getTypeInfoAsWritten() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00006918 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00006919 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006920
Douglas Gregorba48d6a2010-09-09 16:55:46 +00006921 return getDerived().RebuildCXXFunctionalCastExpr(Type,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006922 /*FIXME:*/E->getSubExpr()->getLocStart(),
John McCall9ae2f072010-08-23 23:25:46 +00006923 SubExpr.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006924 E->getRParenLoc());
6925}
Mike Stump1eb44332009-09-09 15:08:12 +00006926
Douglas Gregorb98b1992009-08-11 05:31:07 +00006927template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006928ExprResult
John McCall454feb92009-12-08 09:21:05 +00006929TreeTransform<Derived>::TransformCXXTypeidExpr(CXXTypeidExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00006930 if (E->isTypeOperand()) {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00006931 TypeSourceInfo *TInfo
6932 = getDerived().TransformType(E->getTypeOperandSourceInfo());
6933 if (!TInfo)
John McCallf312b1e2010-08-26 23:41:50 +00006934 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006935
Douglas Gregorb98b1992009-08-11 05:31:07 +00006936 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00006937 TInfo == E->getTypeOperandSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00006938 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006939
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00006940 return getDerived().RebuildCXXTypeidExpr(E->getType(),
6941 E->getLocStart(),
6942 TInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00006943 E->getLocEnd());
6944 }
Mike Stump1eb44332009-09-09 15:08:12 +00006945
Eli Friedmanef331b72012-01-20 01:26:23 +00006946 // We don't know whether the subexpression is potentially evaluated until
6947 // after we perform semantic analysis. We speculatively assume it is
6948 // unevaluated; it will get fixed later if the subexpression is in fact
Douglas Gregorb98b1992009-08-11 05:31:07 +00006949 // potentially evaluated.
Eli Friedmanef331b72012-01-20 01:26:23 +00006950 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +00006951
John McCall60d7b3a2010-08-24 06:29:42 +00006952 ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
Douglas Gregorb98b1992009-08-11 05:31:07 +00006953 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00006954 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00006955
Douglas Gregorb98b1992009-08-11 05:31:07 +00006956 if (!getDerived().AlwaysRebuild() &&
6957 SubExpr.get() == E->getExprOperand())
John McCall3fa5cae2010-10-26 07:05:15 +00006958 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00006959
Douglas Gregor57fdc8a2010-04-26 22:37:10 +00006960 return getDerived().RebuildCXXTypeidExpr(E->getType(),
6961 E->getLocStart(),
John McCall9ae2f072010-08-23 23:25:46 +00006962 SubExpr.get(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00006963 E->getLocEnd());
6964}
6965
6966template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00006967ExprResult
Francois Pichet01b7c302010-09-08 12:20:18 +00006968TreeTransform<Derived>::TransformCXXUuidofExpr(CXXUuidofExpr *E) {
6969 if (E->isTypeOperand()) {
6970 TypeSourceInfo *TInfo
6971 = getDerived().TransformType(E->getTypeOperandSourceInfo());
6972 if (!TInfo)
6973 return ExprError();
6974
6975 if (!getDerived().AlwaysRebuild() &&
6976 TInfo == E->getTypeOperandSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00006977 return SemaRef.Owned(E);
Francois Pichet01b7c302010-09-08 12:20:18 +00006978
Douglas Gregor3c52a212011-03-06 17:40:41 +00006979 return getDerived().RebuildCXXUuidofExpr(E->getType(),
Francois Pichet01b7c302010-09-08 12:20:18 +00006980 E->getLocStart(),
6981 TInfo,
6982 E->getLocEnd());
6983 }
6984
Francois Pichet01b7c302010-09-08 12:20:18 +00006985 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
6986
6987 ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
6988 if (SubExpr.isInvalid())
6989 return ExprError();
6990
6991 if (!getDerived().AlwaysRebuild() &&
6992 SubExpr.get() == E->getExprOperand())
John McCall3fa5cae2010-10-26 07:05:15 +00006993 return SemaRef.Owned(E);
Francois Pichet01b7c302010-09-08 12:20:18 +00006994
6995 return getDerived().RebuildCXXUuidofExpr(E->getType(),
6996 E->getLocStart(),
6997 SubExpr.get(),
6998 E->getLocEnd());
6999}
7000
7001template<typename Derived>
7002ExprResult
John McCall454feb92009-12-08 09:21:05 +00007003TreeTransform<Derived>::TransformCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00007004 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007005}
Mike Stump1eb44332009-09-09 15:08:12 +00007006
Douglas Gregorb98b1992009-08-11 05:31:07 +00007007template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007008ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00007009TreeTransform<Derived>::TransformCXXNullPtrLiteralExpr(
John McCall454feb92009-12-08 09:21:05 +00007010 CXXNullPtrLiteralExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00007011 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007012}
Mike Stump1eb44332009-09-09 15:08:12 +00007013
Douglas Gregorb98b1992009-08-11 05:31:07 +00007014template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007015ExprResult
John McCall454feb92009-12-08 09:21:05 +00007016TreeTransform<Derived>::TransformCXXThisExpr(CXXThisExpr *E) {
Douglas Gregorba48d6a2010-09-09 16:55:46 +00007017 DeclContext *DC = getSema().getFunctionLevelDeclContext();
Richard Smith7a614d82011-06-11 17:19:42 +00007018 QualType T;
7019 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
7020 T = MD->getThisType(getSema().Context);
7021 else
7022 T = getSema().Context.getPointerType(
7023 getSema().Context.getRecordType(cast<CXXRecordDecl>(DC)));
Mike Stump1eb44332009-09-09 15:08:12 +00007024
Douglas Gregorba48d6a2010-09-09 16:55:46 +00007025 if (!getDerived().AlwaysRebuild() && T == E->getType())
John McCall3fa5cae2010-10-26 07:05:15 +00007026 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007027
Douglas Gregor828a1972010-01-07 23:12:05 +00007028 return getDerived().RebuildCXXThisExpr(E->getLocStart(), T, E->isImplicit());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007029}
Mike Stump1eb44332009-09-09 15:08:12 +00007030
Douglas Gregorb98b1992009-08-11 05:31:07 +00007031template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007032ExprResult
John McCall454feb92009-12-08 09:21:05 +00007033TreeTransform<Derived>::TransformCXXThrowExpr(CXXThrowExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00007034 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007035 if (SubExpr.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007036 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007037
Douglas Gregorb98b1992009-08-11 05:31:07 +00007038 if (!getDerived().AlwaysRebuild() &&
7039 SubExpr.get() == E->getSubExpr())
John McCall3fa5cae2010-10-26 07:05:15 +00007040 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007041
Douglas Gregorbca01b42011-07-06 22:04:06 +00007042 return getDerived().RebuildCXXThrowExpr(E->getThrowLoc(), SubExpr.get(),
7043 E->isThrownVariableInScope());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007044}
Mike Stump1eb44332009-09-09 15:08:12 +00007045
Douglas Gregorb98b1992009-08-11 05:31:07 +00007046template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007047ExprResult
John McCall454feb92009-12-08 09:21:05 +00007048TreeTransform<Derived>::TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +00007049 ParmVarDecl *Param
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007050 = cast_or_null<ParmVarDecl>(getDerived().TransformDecl(E->getLocStart(),
7051 E->getParam()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00007052 if (!Param)
John McCallf312b1e2010-08-26 23:41:50 +00007053 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007054
Chandler Carruth53cb6f82010-02-08 06:42:49 +00007055 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00007056 Param == E->getParam())
John McCall3fa5cae2010-10-26 07:05:15 +00007057 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007058
Douglas Gregor036aed12009-12-23 23:03:06 +00007059 return getDerived().RebuildCXXDefaultArgExpr(E->getUsedLocation(), Param);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007060}
Mike Stump1eb44332009-09-09 15:08:12 +00007061
Douglas Gregorb98b1992009-08-11 05:31:07 +00007062template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007063ExprResult
Douglas Gregorab6677e2010-09-08 00:15:04 +00007064TreeTransform<Derived>::TransformCXXScalarValueInitExpr(
7065 CXXScalarValueInitExpr *E) {
7066 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
7067 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007068 return ExprError();
Douglas Gregorab6677e2010-09-08 00:15:04 +00007069
Douglas Gregorb98b1992009-08-11 05:31:07 +00007070 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorab6677e2010-09-08 00:15:04 +00007071 T == E->getTypeSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00007072 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007073
Douglas Gregorab6677e2010-09-08 00:15:04 +00007074 return getDerived().RebuildCXXScalarValueInitExpr(T,
7075 /*FIXME:*/T->getTypeLoc().getEndLoc(),
Douglas Gregored8abf12010-07-08 06:14:04 +00007076 E->getRParenLoc());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007077}
Mike Stump1eb44332009-09-09 15:08:12 +00007078
Douglas Gregorb98b1992009-08-11 05:31:07 +00007079template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007080ExprResult
John McCall454feb92009-12-08 09:21:05 +00007081TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00007082 // Transform the type that we're allocating
Douglas Gregor1bb2a932010-09-07 21:49:58 +00007083 TypeSourceInfo *AllocTypeInfo
7084 = getDerived().TransformType(E->getAllocatedTypeSourceInfo());
7085 if (!AllocTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00007086 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007087
Douglas Gregorb98b1992009-08-11 05:31:07 +00007088 // Transform the size of the array we're allocating (if any).
John McCall60d7b3a2010-08-24 06:29:42 +00007089 ExprResult ArraySize = getDerived().TransformExpr(E->getArraySize());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007090 if (ArraySize.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007091 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007092
Douglas Gregorb98b1992009-08-11 05:31:07 +00007093 // Transform the placement arguments (if any).
7094 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00007095 ASTOwningVector<Expr*> PlacementArgs(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00007096 if (getDerived().TransformExprs(E->getPlacementArgs(),
7097 E->getNumPlacementArgs(), true,
7098 PlacementArgs, &ArgumentChanged))
Sebastian Redl2aed8b82012-02-16 12:22:20 +00007099 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007100
Sebastian Redl2aed8b82012-02-16 12:22:20 +00007101 // Transform the initializer (if any).
7102 Expr *OldInit = E->getInitializer();
7103 ExprResult NewInit;
7104 if (OldInit)
7105 NewInit = getDerived().TransformExpr(OldInit);
7106 if (NewInit.isInvalid())
7107 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007108
Sebastian Redl2aed8b82012-02-16 12:22:20 +00007109 // Transform new operator and delete operator.
Douglas Gregor1af74512010-02-26 00:38:10 +00007110 FunctionDecl *OperatorNew = 0;
7111 if (E->getOperatorNew()) {
7112 OperatorNew = cast_or_null<FunctionDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007113 getDerived().TransformDecl(E->getLocStart(),
7114 E->getOperatorNew()));
Douglas Gregor1af74512010-02-26 00:38:10 +00007115 if (!OperatorNew)
John McCallf312b1e2010-08-26 23:41:50 +00007116 return ExprError();
Douglas Gregor1af74512010-02-26 00:38:10 +00007117 }
7118
7119 FunctionDecl *OperatorDelete = 0;
7120 if (E->getOperatorDelete()) {
7121 OperatorDelete = cast_or_null<FunctionDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007122 getDerived().TransformDecl(E->getLocStart(),
7123 E->getOperatorDelete()));
Douglas Gregor1af74512010-02-26 00:38:10 +00007124 if (!OperatorDelete)
John McCallf312b1e2010-08-26 23:41:50 +00007125 return ExprError();
Douglas Gregor1af74512010-02-26 00:38:10 +00007126 }
Sean Huntc3021132010-05-05 15:23:54 +00007127
Douglas Gregorb98b1992009-08-11 05:31:07 +00007128 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor1bb2a932010-09-07 21:49:58 +00007129 AllocTypeInfo == E->getAllocatedTypeSourceInfo() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00007130 ArraySize.get() == E->getArraySize() &&
Sebastian Redl2aed8b82012-02-16 12:22:20 +00007131 NewInit.get() == OldInit &&
Douglas Gregor1af74512010-02-26 00:38:10 +00007132 OperatorNew == E->getOperatorNew() &&
7133 OperatorDelete == E->getOperatorDelete() &&
7134 !ArgumentChanged) {
7135 // Mark any declarations we need as referenced.
7136 // FIXME: instantiation-specific.
Douglas Gregor1af74512010-02-26 00:38:10 +00007137 if (OperatorNew)
Eli Friedman5f2987c2012-02-02 03:46:19 +00007138 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorNew);
Douglas Gregor1af74512010-02-26 00:38:10 +00007139 if (OperatorDelete)
Eli Friedman5f2987c2012-02-02 03:46:19 +00007140 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorDelete);
Douglas Gregor2ad63cf2011-07-26 15:11:03 +00007141
Sebastian Redl2aed8b82012-02-16 12:22:20 +00007142 if (E->isArray() && !E->getAllocatedType()->isDependentType()) {
Douglas Gregor2ad63cf2011-07-26 15:11:03 +00007143 QualType ElementType
7144 = SemaRef.Context.getBaseElementType(E->getAllocatedType());
7145 if (const RecordType *RecordT = ElementType->getAs<RecordType>()) {
7146 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordT->getDecl());
7147 if (CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(Record)) {
Eli Friedman5f2987c2012-02-02 03:46:19 +00007148 SemaRef.MarkFunctionReferenced(E->getLocStart(), Destructor);
Douglas Gregor2ad63cf2011-07-26 15:11:03 +00007149 }
7150 }
7151 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00007152
John McCall3fa5cae2010-10-26 07:05:15 +00007153 return SemaRef.Owned(E);
Douglas Gregor1af74512010-02-26 00:38:10 +00007154 }
Mike Stump1eb44332009-09-09 15:08:12 +00007155
Douglas Gregor1bb2a932010-09-07 21:49:58 +00007156 QualType AllocType = AllocTypeInfo->getType();
Douglas Gregor5b5ad842009-12-22 17:13:37 +00007157 if (!ArraySize.get()) {
7158 // If no array size was specified, but the new expression was
7159 // instantiated with an array type (e.g., "new T" where T is
7160 // instantiated with "int[4]"), extract the outer bound from the
7161 // array type as our array size. We do this with constant and
7162 // dependently-sized array types.
7163 const ArrayType *ArrayT = SemaRef.Context.getAsArrayType(AllocType);
7164 if (!ArrayT) {
7165 // Do nothing
7166 } else if (const ConstantArrayType *ConsArrayT
7167 = dyn_cast<ConstantArrayType>(ArrayT)) {
Sean Huntc3021132010-05-05 15:23:54 +00007168 ArraySize
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00007169 = SemaRef.Owned(IntegerLiteral::Create(SemaRef.Context,
7170 ConsArrayT->getSize(),
7171 SemaRef.Context.getSizeType(),
7172 /*FIXME:*/E->getLocStart()));
Douglas Gregor5b5ad842009-12-22 17:13:37 +00007173 AllocType = ConsArrayT->getElementType();
7174 } else if (const DependentSizedArrayType *DepArrayT
7175 = dyn_cast<DependentSizedArrayType>(ArrayT)) {
7176 if (DepArrayT->getSizeExpr()) {
John McCall3fa5cae2010-10-26 07:05:15 +00007177 ArraySize = SemaRef.Owned(DepArrayT->getSizeExpr());
Douglas Gregor5b5ad842009-12-22 17:13:37 +00007178 AllocType = DepArrayT->getElementType();
7179 }
7180 }
7181 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00007182
Douglas Gregorb98b1992009-08-11 05:31:07 +00007183 return getDerived().RebuildCXXNewExpr(E->getLocStart(),
7184 E->isGlobalNew(),
7185 /*FIXME:*/E->getLocStart(),
7186 move_arg(PlacementArgs),
7187 /*FIXME:*/E->getLocStart(),
Douglas Gregor4bd40312010-07-13 15:54:32 +00007188 E->getTypeIdParens(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007189 AllocType,
Douglas Gregor1bb2a932010-09-07 21:49:58 +00007190 AllocTypeInfo,
John McCall9ae2f072010-08-23 23:25:46 +00007191 ArraySize.get(),
Sebastian Redl2aed8b82012-02-16 12:22:20 +00007192 E->getDirectInitRange(),
7193 NewInit.take());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007194}
Mike Stump1eb44332009-09-09 15:08:12 +00007195
Douglas Gregorb98b1992009-08-11 05:31:07 +00007196template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007197ExprResult
John McCall454feb92009-12-08 09:21:05 +00007198TreeTransform<Derived>::TransformCXXDeleteExpr(CXXDeleteExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00007199 ExprResult Operand = getDerived().TransformExpr(E->getArgument());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007200 if (Operand.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007201 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007202
Douglas Gregor1af74512010-02-26 00:38:10 +00007203 // Transform the delete operator, if known.
7204 FunctionDecl *OperatorDelete = 0;
7205 if (E->getOperatorDelete()) {
7206 OperatorDelete = cast_or_null<FunctionDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007207 getDerived().TransformDecl(E->getLocStart(),
7208 E->getOperatorDelete()));
Douglas Gregor1af74512010-02-26 00:38:10 +00007209 if (!OperatorDelete)
John McCallf312b1e2010-08-26 23:41:50 +00007210 return ExprError();
Douglas Gregor1af74512010-02-26 00:38:10 +00007211 }
Sean Huntc3021132010-05-05 15:23:54 +00007212
Douglas Gregorb98b1992009-08-11 05:31:07 +00007213 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor1af74512010-02-26 00:38:10 +00007214 Operand.get() == E->getArgument() &&
7215 OperatorDelete == E->getOperatorDelete()) {
7216 // Mark any declarations we need as referenced.
7217 // FIXME: instantiation-specific.
7218 if (OperatorDelete)
Eli Friedman5f2987c2012-02-02 03:46:19 +00007219 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorDelete);
Douglas Gregor5833b0b2010-09-14 22:55:20 +00007220
7221 if (!E->getArgument()->isTypeDependent()) {
7222 QualType Destroyed = SemaRef.Context.getBaseElementType(
7223 E->getDestroyedType());
7224 if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) {
7225 CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl());
Eli Friedman5f2987c2012-02-02 03:46:19 +00007226 SemaRef.MarkFunctionReferenced(E->getLocStart(),
7227 SemaRef.LookupDestructor(Record));
Douglas Gregor5833b0b2010-09-14 22:55:20 +00007228 }
7229 }
7230
John McCall3fa5cae2010-10-26 07:05:15 +00007231 return SemaRef.Owned(E);
Douglas Gregor1af74512010-02-26 00:38:10 +00007232 }
Mike Stump1eb44332009-09-09 15:08:12 +00007233
Douglas Gregorb98b1992009-08-11 05:31:07 +00007234 return getDerived().RebuildCXXDeleteExpr(E->getLocStart(),
7235 E->isGlobalDelete(),
7236 E->isArrayForm(),
John McCall9ae2f072010-08-23 23:25:46 +00007237 Operand.get());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007238}
Mike Stump1eb44332009-09-09 15:08:12 +00007239
Douglas Gregorb98b1992009-08-11 05:31:07 +00007240template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007241ExprResult
Douglas Gregora71d8192009-09-04 17:36:40 +00007242TreeTransform<Derived>::TransformCXXPseudoDestructorExpr(
John McCall454feb92009-12-08 09:21:05 +00007243 CXXPseudoDestructorExpr *E) {
John McCall60d7b3a2010-08-24 06:29:42 +00007244 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregora71d8192009-09-04 17:36:40 +00007245 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007246 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007247
John McCallb3d87482010-08-24 05:47:05 +00007248 ParsedType ObjectTypePtr;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007249 bool MayBePseudoDestructor = false;
John McCall9ae2f072010-08-23 23:25:46 +00007250 Base = SemaRef.ActOnStartCXXMemberReference(0, Base.get(),
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007251 E->getOperatorLoc(),
7252 E->isArrow()? tok::arrow : tok::period,
7253 ObjectTypePtr,
7254 MayBePseudoDestructor);
7255 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007256 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007257
John McCallb3d87482010-08-24 05:47:05 +00007258 QualType ObjectType = ObjectTypePtr.get();
Douglas Gregorf3db29f2011-02-25 18:19:59 +00007259 NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc();
7260 if (QualifierLoc) {
7261 QualifierLoc
7262 = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc, ObjectType);
7263 if (!QualifierLoc)
John McCall43fed0d2010-11-12 08:19:04 +00007264 return ExprError();
7265 }
Douglas Gregorf3db29f2011-02-25 18:19:59 +00007266 CXXScopeSpec SS;
7267 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00007268
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007269 PseudoDestructorTypeStorage Destroyed;
7270 if (E->getDestroyedTypeInfo()) {
7271 TypeSourceInfo *DestroyedTypeInfo
John McCall43fed0d2010-11-12 08:19:04 +00007272 = getDerived().TransformTypeInObjectScope(E->getDestroyedTypeInfo(),
Douglas Gregorb71d8212011-03-02 18:32:08 +00007273 ObjectType, 0, SS);
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007274 if (!DestroyedTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00007275 return ExprError();
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007276 Destroyed = DestroyedTypeInfo;
Douglas Gregor6b18e742011-11-09 02:19:47 +00007277 } else if (!ObjectType.isNull() && ObjectType->isDependentType()) {
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007278 // We aren't likely to be able to resolve the identifier down to a type
7279 // now anyway, so just retain the identifier.
7280 Destroyed = PseudoDestructorTypeStorage(E->getDestroyedTypeIdentifier(),
7281 E->getDestroyedTypeLoc());
7282 } else {
7283 // Look for a destructor known with the given name.
John McCallb3d87482010-08-24 05:47:05 +00007284 ParsedType T = SemaRef.getDestructorName(E->getTildeLoc(),
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007285 *E->getDestroyedTypeIdentifier(),
7286 E->getDestroyedTypeLoc(),
7287 /*Scope=*/0,
7288 SS, ObjectTypePtr,
7289 false);
7290 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007291 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007292
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007293 Destroyed
7294 = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.GetTypeFromParser(T),
7295 E->getDestroyedTypeLoc());
7296 }
Douglas Gregor26d4ac92010-02-24 23:40:28 +00007297
Douglas Gregor26d4ac92010-02-24 23:40:28 +00007298 TypeSourceInfo *ScopeTypeInfo = 0;
7299 if (E->getScopeTypeInfo()) {
John McCall43fed0d2010-11-12 08:19:04 +00007300 ScopeTypeInfo = getDerived().TransformType(E->getScopeTypeInfo());
Douglas Gregor26d4ac92010-02-24 23:40:28 +00007301 if (!ScopeTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00007302 return ExprError();
Douglas Gregora71d8192009-09-04 17:36:40 +00007303 }
Sean Huntc3021132010-05-05 15:23:54 +00007304
John McCall9ae2f072010-08-23 23:25:46 +00007305 return getDerived().RebuildCXXPseudoDestructorExpr(Base.get(),
Douglas Gregora71d8192009-09-04 17:36:40 +00007306 E->getOperatorLoc(),
7307 E->isArrow(),
Douglas Gregorf3db29f2011-02-25 18:19:59 +00007308 SS,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00007309 ScopeTypeInfo,
7310 E->getColonColonLoc(),
Douglas Gregorfce46ee2010-02-24 23:50:37 +00007311 E->getTildeLoc(),
Douglas Gregora2e7dd22010-02-25 01:56:36 +00007312 Destroyed);
Douglas Gregora71d8192009-09-04 17:36:40 +00007313}
Mike Stump1eb44332009-09-09 15:08:12 +00007314
Douglas Gregora71d8192009-09-04 17:36:40 +00007315template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007316ExprResult
John McCallba135432009-11-21 08:51:07 +00007317TreeTransform<Derived>::TransformUnresolvedLookupExpr(
John McCall454feb92009-12-08 09:21:05 +00007318 UnresolvedLookupExpr *Old) {
John McCallf7a1a742009-11-24 19:00:30 +00007319 LookupResult R(SemaRef, Old->getName(), Old->getNameLoc(),
7320 Sema::LookupOrdinaryName);
7321
7322 // Transform all the decls.
7323 for (UnresolvedLookupExpr::decls_iterator I = Old->decls_begin(),
7324 E = Old->decls_end(); I != E; ++I) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007325 NamedDecl *InstD = static_cast<NamedDecl*>(
7326 getDerived().TransformDecl(Old->getNameLoc(),
7327 *I));
John McCall9f54ad42009-12-10 09:41:52 +00007328 if (!InstD) {
7329 // Silently ignore these if a UsingShadowDecl instantiated to nothing.
7330 // This can happen because of dependent hiding.
7331 if (isa<UsingShadowDecl>(*I))
7332 continue;
7333 else
John McCallf312b1e2010-08-26 23:41:50 +00007334 return ExprError();
John McCall9f54ad42009-12-10 09:41:52 +00007335 }
John McCallf7a1a742009-11-24 19:00:30 +00007336
7337 // Expand using declarations.
7338 if (isa<UsingDecl>(InstD)) {
7339 UsingDecl *UD = cast<UsingDecl>(InstD);
7340 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
7341 E = UD->shadow_end(); I != E; ++I)
7342 R.addDecl(*I);
7343 continue;
7344 }
7345
7346 R.addDecl(InstD);
7347 }
7348
7349 // Resolve a kind, but don't do any further analysis. If it's
7350 // ambiguous, the callee needs to deal with it.
7351 R.resolveKind();
7352
7353 // Rebuild the nested-name qualifier, if present.
7354 CXXScopeSpec SS;
Douglas Gregor4c9be892011-02-28 20:01:57 +00007355 if (Old->getQualifierLoc()) {
7356 NestedNameSpecifierLoc QualifierLoc
7357 = getDerived().TransformNestedNameSpecifierLoc(Old->getQualifierLoc());
7358 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00007359 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007360
Douglas Gregor4c9be892011-02-28 20:01:57 +00007361 SS.Adopt(QualifierLoc);
Sean Huntc3021132010-05-05 15:23:54 +00007362 }
7363
Douglas Gregorc96be1e2010-04-27 18:19:34 +00007364 if (Old->getNamingClass()) {
Douglas Gregor66c45152010-04-27 16:10:10 +00007365 CXXRecordDecl *NamingClass
7366 = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
7367 Old->getNameLoc(),
7368 Old->getNamingClass()));
7369 if (!NamingClass)
John McCallf312b1e2010-08-26 23:41:50 +00007370 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007371
Douglas Gregor66c45152010-04-27 16:10:10 +00007372 R.setNamingClass(NamingClass);
John McCallf7a1a742009-11-24 19:00:30 +00007373 }
7374
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007375 SourceLocation TemplateKWLoc = Old->getTemplateKeywordLoc();
7376
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00007377 // If we have neither explicit template arguments, nor the template keyword,
7378 // it's a normal declaration name.
7379 if (!Old->hasExplicitTemplateArgs() && !TemplateKWLoc.isValid())
John McCallf7a1a742009-11-24 19:00:30 +00007380 return getDerived().RebuildDeclarationNameExpr(SS, R, Old->requiresADL());
7381
7382 // If we have template arguments, rebuild them, then rebuild the
7383 // templateid expression.
7384 TemplateArgumentListInfo TransArgs(Old->getLAngleLoc(), Old->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00007385 if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(),
7386 Old->getNumTemplateArgs(),
7387 TransArgs))
7388 return ExprError();
John McCallf7a1a742009-11-24 19:00:30 +00007389
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007390 return getDerived().RebuildTemplateIdExpr(SS, TemplateKWLoc, R,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00007391 Old->requiresADL(), &TransArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007392}
Mike Stump1eb44332009-09-09 15:08:12 +00007393
Douglas Gregorb98b1992009-08-11 05:31:07 +00007394template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007395ExprResult
John McCall454feb92009-12-08 09:21:05 +00007396TreeTransform<Derived>::TransformUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00007397 TypeSourceInfo *T = getDerived().TransformType(E->getQueriedTypeSourceInfo());
7398 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007399 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007400
Douglas Gregorb98b1992009-08-11 05:31:07 +00007401 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00007402 T == E->getQueriedTypeSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00007403 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007404
Mike Stump1eb44332009-09-09 15:08:12 +00007405 return getDerived().RebuildUnaryTypeTrait(E->getTrait(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007406 E->getLocStart(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007407 T,
7408 E->getLocEnd());
7409}
Mike Stump1eb44332009-09-09 15:08:12 +00007410
Douglas Gregorb98b1992009-08-11 05:31:07 +00007411template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007412ExprResult
Francois Pichet6ad6f282010-12-07 00:08:36 +00007413TreeTransform<Derived>::TransformBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
7414 TypeSourceInfo *LhsT = getDerived().TransformType(E->getLhsTypeSourceInfo());
7415 if (!LhsT)
7416 return ExprError();
7417
7418 TypeSourceInfo *RhsT = getDerived().TransformType(E->getRhsTypeSourceInfo());
7419 if (!RhsT)
7420 return ExprError();
7421
7422 if (!getDerived().AlwaysRebuild() &&
7423 LhsT == E->getLhsTypeSourceInfo() && RhsT == E->getRhsTypeSourceInfo())
7424 return SemaRef.Owned(E);
7425
7426 return getDerived().RebuildBinaryTypeTrait(E->getTrait(),
7427 E->getLocStart(),
7428 LhsT, RhsT,
7429 E->getLocEnd());
7430}
7431
7432template<typename Derived>
7433ExprResult
John Wiegley21ff2e52011-04-28 00:16:57 +00007434TreeTransform<Derived>::TransformArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
7435 TypeSourceInfo *T = getDerived().TransformType(E->getQueriedTypeSourceInfo());
7436 if (!T)
7437 return ExprError();
7438
7439 if (!getDerived().AlwaysRebuild() &&
7440 T == E->getQueriedTypeSourceInfo())
7441 return SemaRef.Owned(E);
7442
7443 ExprResult SubExpr;
7444 {
7445 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
7446 SubExpr = getDerived().TransformExpr(E->getDimensionExpression());
7447 if (SubExpr.isInvalid())
7448 return ExprError();
7449
7450 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getDimensionExpression())
7451 return SemaRef.Owned(E);
7452 }
7453
7454 return getDerived().RebuildArrayTypeTrait(E->getTrait(),
7455 E->getLocStart(),
7456 T,
7457 SubExpr.get(),
7458 E->getLocEnd());
7459}
7460
7461template<typename Derived>
7462ExprResult
John Wiegley55262202011-04-25 06:54:41 +00007463TreeTransform<Derived>::TransformExpressionTraitExpr(ExpressionTraitExpr *E) {
7464 ExprResult SubExpr;
7465 {
7466 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
7467 SubExpr = getDerived().TransformExpr(E->getQueriedExpression());
7468 if (SubExpr.isInvalid())
7469 return ExprError();
7470
7471 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getQueriedExpression())
7472 return SemaRef.Owned(E);
7473 }
7474
7475 return getDerived().RebuildExpressionTrait(
7476 E->getTrait(), E->getLocStart(), SubExpr.get(), E->getLocEnd());
7477}
7478
7479template<typename Derived>
7480ExprResult
John McCall865d4472009-11-19 22:55:06 +00007481TreeTransform<Derived>::TransformDependentScopeDeclRefExpr(
Abramo Bagnara25777432010-08-11 22:01:17 +00007482 DependentScopeDeclRefExpr *E) {
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00007483 NestedNameSpecifierLoc QualifierLoc
7484 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
7485 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00007486 return ExprError();
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007487 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00007488
John McCall43fed0d2010-11-12 08:19:04 +00007489 // TODO: If this is a conversion-function-id, verify that the
7490 // destination type name (if present) resolves the same way after
7491 // instantiation as it did in the local scope.
7492
Abramo Bagnara25777432010-08-11 22:01:17 +00007493 DeclarationNameInfo NameInfo
7494 = getDerived().TransformDeclarationNameInfo(E->getNameInfo());
7495 if (!NameInfo.getName())
John McCallf312b1e2010-08-26 23:41:50 +00007496 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007497
John McCallf7a1a742009-11-24 19:00:30 +00007498 if (!E->hasExplicitTemplateArgs()) {
7499 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00007500 QualifierLoc == E->getQualifierLoc() &&
Abramo Bagnara25777432010-08-11 22:01:17 +00007501 // Note: it is sufficient to compare the Name component of NameInfo:
7502 // if name has not changed, DNLoc has not changed either.
7503 NameInfo.getName() == E->getDeclName())
John McCall3fa5cae2010-10-26 07:05:15 +00007504 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007505
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00007506 return getDerived().RebuildDependentScopeDeclRefExpr(QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007507 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00007508 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00007509 /*TemplateArgs*/ 0);
Douglas Gregorf17bb742009-10-22 17:20:55 +00007510 }
John McCalld5532b62009-11-23 01:53:49 +00007511
7512 TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00007513 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
7514 E->getNumTemplateArgs(),
7515 TransArgs))
7516 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00007517
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00007518 return getDerived().RebuildDependentScopeDeclRefExpr(QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007519 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00007520 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00007521 &TransArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007522}
7523
7524template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007525ExprResult
John McCall454feb92009-12-08 09:21:05 +00007526TreeTransform<Derived>::TransformCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregor321725d2010-02-03 03:01:57 +00007527 // CXXConstructExprs are always implicit, so when we have a
7528 // 1-argument construction we just transform that argument.
7529 if (E->getNumArgs() == 1 ||
7530 (E->getNumArgs() > 1 && getDerived().DropCallArgument(E->getArg(1))))
7531 return getDerived().TransformExpr(E->getArg(0));
7532
Douglas Gregorb98b1992009-08-11 05:31:07 +00007533 TemporaryBase Rebase(*this, /*FIXME*/E->getLocStart(), DeclarationName());
7534
7535 QualType T = getDerived().TransformType(E->getType());
7536 if (T.isNull())
John McCallf312b1e2010-08-26 23:41:50 +00007537 return ExprError();
Douglas Gregorb98b1992009-08-11 05:31:07 +00007538
7539 CXXConstructorDecl *Constructor
7540 = cast_or_null<CXXConstructorDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007541 getDerived().TransformDecl(E->getLocStart(),
7542 E->getConstructor()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00007543 if (!Constructor)
John McCallf312b1e2010-08-26 23:41:50 +00007544 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007545
Douglas Gregorb98b1992009-08-11 05:31:07 +00007546 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00007547 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00007548 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
7549 &ArgumentChanged))
7550 return ExprError();
7551
Douglas Gregorb98b1992009-08-11 05:31:07 +00007552 if (!getDerived().AlwaysRebuild() &&
7553 T == E->getType() &&
7554 Constructor == E->getConstructor() &&
Douglas Gregorc845aad2010-02-26 00:01:57 +00007555 !ArgumentChanged) {
Douglas Gregor1af74512010-02-26 00:38:10 +00007556 // Mark the constructor as referenced.
7557 // FIXME: Instantiation-specific
Eli Friedman5f2987c2012-02-02 03:46:19 +00007558 SemaRef.MarkFunctionReferenced(E->getLocStart(), Constructor);
John McCall3fa5cae2010-10-26 07:05:15 +00007559 return SemaRef.Owned(E);
Douglas Gregorc845aad2010-02-26 00:01:57 +00007560 }
Mike Stump1eb44332009-09-09 15:08:12 +00007561
Douglas Gregor4411d2e2009-12-14 16:27:04 +00007562 return getDerived().RebuildCXXConstructExpr(T, /*FIXME:*/E->getLocStart(),
7563 Constructor, E->isElidable(),
Douglas Gregor8c3e5542010-08-22 17:20:18 +00007564 move_arg(Args),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00007565 E->hadMultipleCandidates(),
Douglas Gregor8c3e5542010-08-22 17:20:18 +00007566 E->requiresZeroInitialization(),
Chandler Carruth428edaf2010-10-25 08:47:36 +00007567 E->getConstructionKind(),
7568 E->getParenRange());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007569}
Mike Stump1eb44332009-09-09 15:08:12 +00007570
Douglas Gregorb98b1992009-08-11 05:31:07 +00007571/// \brief Transform a C++ temporary-binding expression.
7572///
Douglas Gregor51326552009-12-24 18:51:59 +00007573/// Since CXXBindTemporaryExpr nodes are implicitly generated, we just
7574/// transform the subexpression and return that.
Douglas Gregorb98b1992009-08-11 05:31:07 +00007575template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007576ExprResult
John McCall454feb92009-12-08 09:21:05 +00007577TreeTransform<Derived>::TransformCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
Douglas Gregor51326552009-12-24 18:51:59 +00007578 return getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007579}
Mike Stump1eb44332009-09-09 15:08:12 +00007580
John McCall4765fa02010-12-06 08:20:24 +00007581/// \brief Transform a C++ expression that contains cleanups that should
7582/// be run after the expression is evaluated.
Douglas Gregorb98b1992009-08-11 05:31:07 +00007583///
John McCall4765fa02010-12-06 08:20:24 +00007584/// Since ExprWithCleanups nodes are implicitly generated, we
Douglas Gregor51326552009-12-24 18:51:59 +00007585/// just transform the subexpression and return that.
Douglas Gregorb98b1992009-08-11 05:31:07 +00007586template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007587ExprResult
John McCall4765fa02010-12-06 08:20:24 +00007588TreeTransform<Derived>::TransformExprWithCleanups(ExprWithCleanups *E) {
Douglas Gregor51326552009-12-24 18:51:59 +00007589 return getDerived().TransformExpr(E->getSubExpr());
Douglas Gregorb98b1992009-08-11 05:31:07 +00007590}
Mike Stump1eb44332009-09-09 15:08:12 +00007591
Douglas Gregorb98b1992009-08-11 05:31:07 +00007592template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007593ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00007594TreeTransform<Derived>::TransformCXXTemporaryObjectExpr(
Douglas Gregorab6677e2010-09-08 00:15:04 +00007595 CXXTemporaryObjectExpr *E) {
7596 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
7597 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007598 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007599
Douglas Gregorb98b1992009-08-11 05:31:07 +00007600 CXXConstructorDecl *Constructor
7601 = cast_or_null<CXXConstructorDecl>(
Sean Huntc3021132010-05-05 15:23:54 +00007602 getDerived().TransformDecl(E->getLocStart(),
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007603 E->getConstructor()));
Douglas Gregorb98b1992009-08-11 05:31:07 +00007604 if (!Constructor)
John McCallf312b1e2010-08-26 23:41:50 +00007605 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007606
Douglas Gregorb98b1992009-08-11 05:31:07 +00007607 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00007608 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregorb98b1992009-08-11 05:31:07 +00007609 Args.reserve(E->getNumArgs());
Douglas Gregoraa165f82011-01-03 19:04:46 +00007610 if (TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
7611 &ArgumentChanged))
7612 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007613
Douglas Gregorb98b1992009-08-11 05:31:07 +00007614 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorab6677e2010-09-08 00:15:04 +00007615 T == E->getTypeSourceInfo() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00007616 Constructor == E->getConstructor() &&
Douglas Gregor91be6f52010-03-02 17:18:33 +00007617 !ArgumentChanged) {
7618 // FIXME: Instantiation-specific
Eli Friedman5f2987c2012-02-02 03:46:19 +00007619 SemaRef.MarkFunctionReferenced(E->getLocStart(), Constructor);
John McCall3fa5cae2010-10-26 07:05:15 +00007620 return SemaRef.MaybeBindToTemporary(E);
Douglas Gregor91be6f52010-03-02 17:18:33 +00007621 }
Douglas Gregorab6677e2010-09-08 00:15:04 +00007622
7623 return getDerived().RebuildCXXTemporaryObjectExpr(T,
7624 /*FIXME:*/T->getTypeLoc().getEndLoc(),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007625 move_arg(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007626 E->getLocEnd());
7627}
Mike Stump1eb44332009-09-09 15:08:12 +00007628
Douglas Gregorb98b1992009-08-11 05:31:07 +00007629template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007630ExprResult
Douglas Gregor01d08012012-02-07 10:09:13 +00007631TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
Douglas Gregordfca6f52012-02-13 22:00:16 +00007632 // Create the local class that will describe the lambda.
7633 CXXRecordDecl *Class
7634 = getSema().createLambdaClosureType(E->getIntroducerRange());
7635 getDerived().transformedLocalDecl(E->getLambdaClass(), Class);
7636
7637 // Transform the type of the lambda parameters and start the definition of
7638 // the lambda itself.
7639 TypeSourceInfo *MethodTy
7640 = TransformType(E->getCallOperator()->getTypeSourceInfo());
7641 if (!MethodTy)
7642 return ExprError();
7643
Douglas Gregorc6889e72012-02-14 22:28:59 +00007644 // Transform lambda parameters.
7645 bool Invalid = false;
7646 llvm::SmallVector<QualType, 4> ParamTypes;
7647 llvm::SmallVector<ParmVarDecl *, 4> Params;
7648 if (getDerived().TransformFunctionTypeParams(E->getLocStart(),
7649 E->getCallOperator()->param_begin(),
7650 E->getCallOperator()->param_size(),
7651 0, ParamTypes, &Params))
7652 Invalid = true;
7653
Douglas Gregordfca6f52012-02-13 22:00:16 +00007654 // Build the call operator.
7655 CXXMethodDecl *CallOperator
7656 = getSema().startLambdaDefinition(Class, E->getIntroducerRange(),
7657 MethodTy,
Douglas Gregorc6889e72012-02-14 22:28:59 +00007658 E->getCallOperator()->getLocEnd(),
7659 Params);
Douglas Gregordfca6f52012-02-13 22:00:16 +00007660 getDerived().transformAttrs(E->getCallOperator(), CallOperator);
7661
Douglas Gregord5387e82012-02-14 00:00:48 +00007662 // FIXME: Instantiation-specific.
7663 CallOperator->setInstantiationOfMemberFunction(E->getCallOperator(),
7664 TSK_ImplicitInstantiation);
7665
7666 // Introduce the context of the call operator.
7667 Sema::ContextRAII SavedContext(getSema(), CallOperator);
7668
Douglas Gregordfca6f52012-02-13 22:00:16 +00007669 // Enter the scope of the lambda.
7670 sema::LambdaScopeInfo *LSI
7671 = getSema().enterLambdaScope(CallOperator, E->getIntroducerRange(),
7672 E->getCaptureDefault(),
7673 E->hasExplicitParameters(),
7674 E->hasExplicitResultType(),
7675 E->isMutable());
7676
7677 // Transform captures.
Douglas Gregordfca6f52012-02-13 22:00:16 +00007678 bool FinishedExplicitCaptures = false;
7679 for (LambdaExpr::capture_iterator C = E->capture_begin(),
7680 CEnd = E->capture_end();
7681 C != CEnd; ++C) {
7682 // When we hit the first implicit capture, tell Sema that we've finished
7683 // the list of explicit captures.
7684 if (!FinishedExplicitCaptures && C->isImplicit()) {
7685 getSema().finishLambdaExplicitCaptures(LSI);
7686 FinishedExplicitCaptures = true;
7687 }
7688
7689 // Capturing 'this' is trivial.
7690 if (C->capturesThis()) {
7691 getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit());
7692 continue;
7693 }
7694
Douglas Gregora7365242012-02-14 19:27:52 +00007695 // Determine the capture kind for Sema.
7696 Sema::TryCaptureKind Kind
7697 = C->isImplicit()? Sema::TryCapture_Implicit
7698 : C->getCaptureKind() == LCK_ByCopy
7699 ? Sema::TryCapture_ExplicitByVal
7700 : Sema::TryCapture_ExplicitByRef;
7701 SourceLocation EllipsisLoc;
7702 if (C->isPackExpansion()) {
7703 UnexpandedParameterPack Unexpanded(C->getCapturedVar(), C->getLocation());
7704 bool ShouldExpand = false;
7705 bool RetainExpansion = false;
7706 llvm::Optional<unsigned> NumExpansions;
7707 if (getDerived().TryExpandParameterPacks(C->getEllipsisLoc(),
7708 C->getLocation(),
7709 Unexpanded,
7710 ShouldExpand, RetainExpansion,
7711 NumExpansions))
7712 return ExprError();
7713
7714 if (ShouldExpand) {
7715 // The transform has determined that we should perform an expansion;
7716 // transform and capture each of the arguments.
7717 // expansion of the pattern. Do so.
7718 VarDecl *Pack = C->getCapturedVar();
7719 for (unsigned I = 0; I != *NumExpansions; ++I) {
7720 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
7721 VarDecl *CapturedVar
7722 = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(),
7723 Pack));
7724 if (!CapturedVar) {
7725 Invalid = true;
7726 continue;
7727 }
7728
7729 // Capture the transformed variable.
Douglas Gregor999713e2012-02-18 09:37:24 +00007730 getSema().tryCaptureVariable(CapturedVar, C->getLocation(), Kind);
Douglas Gregora7365242012-02-14 19:27:52 +00007731 }
7732 continue;
7733 }
7734
7735 EllipsisLoc = C->getEllipsisLoc();
7736 }
7737
Douglas Gregordfca6f52012-02-13 22:00:16 +00007738 // Transform the captured variable.
7739 VarDecl *CapturedVar
7740 = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(),
7741 C->getCapturedVar()));
7742 if (!CapturedVar) {
7743 Invalid = true;
7744 continue;
7745 }
Douglas Gregora7365242012-02-14 19:27:52 +00007746
Douglas Gregordfca6f52012-02-13 22:00:16 +00007747 // Capture the transformed variable.
Douglas Gregor999713e2012-02-18 09:37:24 +00007748 getSema().tryCaptureVariable(CapturedVar, C->getLocation(), Kind);
Douglas Gregordfca6f52012-02-13 22:00:16 +00007749 }
7750 if (!FinishedExplicitCaptures)
7751 getSema().finishLambdaExplicitCaptures(LSI);
7752
Douglas Gregordfca6f52012-02-13 22:00:16 +00007753
7754 // Enter a new evaluation context to insulate the lambda from any
7755 // cleanups from the enclosing full-expression.
7756 getSema().PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
7757
7758 if (Invalid) {
7759 getSema().ActOnLambdaError(E->getLocStart(), /*CurScope=*/0,
7760 /*IsInstantiation=*/true);
7761 return ExprError();
7762 }
7763
7764 // Instantiate the body of the lambda expression.
Douglas Gregord5387e82012-02-14 00:00:48 +00007765 StmtResult Body = getDerived().TransformStmt(E->getBody());
7766 if (Body.isInvalid()) {
7767 getSema().ActOnLambdaError(E->getLocStart(), /*CurScope=*/0,
7768 /*IsInstantiation=*/true);
7769 return ExprError();
7770 }
Douglas Gregordfca6f52012-02-13 22:00:16 +00007771
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00007772 unsigned ManglingNumber = E->getLambdaClass()->getLambdaManglingNumber();
Douglas Gregordfca6f52012-02-13 22:00:16 +00007773 return getSema().ActOnLambdaExpr(E->getLocStart(), Body.take(),
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00007774 /*CurScope=*/0, ManglingNumber,
Douglas Gregordfca6f52012-02-13 22:00:16 +00007775 /*IsInstantiation=*/true);
Douglas Gregor01d08012012-02-07 10:09:13 +00007776}
7777
7778template<typename Derived>
7779ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00007780TreeTransform<Derived>::TransformCXXUnresolvedConstructExpr(
John McCall454feb92009-12-08 09:21:05 +00007781 CXXUnresolvedConstructExpr *E) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00007782 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
7783 if (!T)
John McCallf312b1e2010-08-26 23:41:50 +00007784 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007785
Douglas Gregorb98b1992009-08-11 05:31:07 +00007786 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00007787 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00007788 Args.reserve(E->arg_size());
7789 if (getDerived().TransformExprs(E->arg_begin(), E->arg_size(), true, Args,
7790 &ArgumentChanged))
7791 return ExprError();
7792
Douglas Gregorb98b1992009-08-11 05:31:07 +00007793 if (!getDerived().AlwaysRebuild() &&
Douglas Gregorab6677e2010-09-08 00:15:04 +00007794 T == E->getTypeSourceInfo() &&
Douglas Gregorb98b1992009-08-11 05:31:07 +00007795 !ArgumentChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00007796 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007797
Douglas Gregorb98b1992009-08-11 05:31:07 +00007798 // FIXME: we're faking the locations of the commas
Douglas Gregorab6677e2010-09-08 00:15:04 +00007799 return getDerived().RebuildCXXUnresolvedConstructExpr(T,
Douglas Gregorb98b1992009-08-11 05:31:07 +00007800 E->getLParenLoc(),
7801 move_arg(Args),
Douglas Gregorb98b1992009-08-11 05:31:07 +00007802 E->getRParenLoc());
7803}
Mike Stump1eb44332009-09-09 15:08:12 +00007804
Douglas Gregorb98b1992009-08-11 05:31:07 +00007805template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007806ExprResult
John McCall865d4472009-11-19 22:55:06 +00007807TreeTransform<Derived>::TransformCXXDependentScopeMemberExpr(
Abramo Bagnara25777432010-08-11 22:01:17 +00007808 CXXDependentScopeMemberExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00007809 // Transform the base of the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00007810 ExprResult Base((Expr*) 0);
John McCallaa81e162009-12-01 22:10:20 +00007811 Expr *OldBase;
7812 QualType BaseType;
7813 QualType ObjectType;
7814 if (!E->isImplicitAccess()) {
7815 OldBase = E->getBase();
7816 Base = getDerived().TransformExpr(OldBase);
7817 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007818 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007819
John McCallaa81e162009-12-01 22:10:20 +00007820 // Start the member reference and compute the object's type.
John McCallb3d87482010-08-24 05:47:05 +00007821 ParsedType ObjectTy;
Douglas Gregord4dca082010-02-24 18:44:31 +00007822 bool MayBePseudoDestructor = false;
John McCall9ae2f072010-08-23 23:25:46 +00007823 Base = SemaRef.ActOnStartCXXMemberReference(0, Base.get(),
John McCallaa81e162009-12-01 22:10:20 +00007824 E->getOperatorLoc(),
Douglas Gregora38c6872009-09-03 16:14:30 +00007825 E->isArrow()? tok::arrow : tok::period,
Douglas Gregord4dca082010-02-24 18:44:31 +00007826 ObjectTy,
7827 MayBePseudoDestructor);
John McCallaa81e162009-12-01 22:10:20 +00007828 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007829 return ExprError();
John McCallaa81e162009-12-01 22:10:20 +00007830
John McCallb3d87482010-08-24 05:47:05 +00007831 ObjectType = ObjectTy.get();
John McCallaa81e162009-12-01 22:10:20 +00007832 BaseType = ((Expr*) Base.get())->getType();
7833 } else {
7834 OldBase = 0;
7835 BaseType = getDerived().TransformType(E->getBaseType());
7836 ObjectType = BaseType->getAs<PointerType>()->getPointeeType();
7837 }
Mike Stump1eb44332009-09-09 15:08:12 +00007838
Douglas Gregor6cd21982009-10-20 05:58:46 +00007839 // Transform the first part of the nested-name-specifier that qualifies
7840 // the member name.
Douglas Gregorc68afe22009-09-03 21:38:09 +00007841 NamedDecl *FirstQualifierInScope
Douglas Gregor6cd21982009-10-20 05:58:46 +00007842 = getDerived().TransformFirstQualifierInScope(
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007843 E->getFirstQualifierFoundInScope(),
7844 E->getQualifierLoc().getBeginLoc());
Mike Stump1eb44332009-09-09 15:08:12 +00007845
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007846 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregora38c6872009-09-03 16:14:30 +00007847 if (E->getQualifier()) {
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007848 QualifierLoc
7849 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc(),
7850 ObjectType,
7851 FirstQualifierInScope);
7852 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00007853 return ExprError();
Douglas Gregora38c6872009-09-03 16:14:30 +00007854 }
Mike Stump1eb44332009-09-09 15:08:12 +00007855
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007856 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
7857
John McCall43fed0d2010-11-12 08:19:04 +00007858 // TODO: If this is a conversion-function-id, verify that the
7859 // destination type name (if present) resolves the same way after
7860 // instantiation as it did in the local scope.
7861
Abramo Bagnara25777432010-08-11 22:01:17 +00007862 DeclarationNameInfo NameInfo
John McCall43fed0d2010-11-12 08:19:04 +00007863 = getDerived().TransformDeclarationNameInfo(E->getMemberNameInfo());
Abramo Bagnara25777432010-08-11 22:01:17 +00007864 if (!NameInfo.getName())
John McCallf312b1e2010-08-26 23:41:50 +00007865 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007866
John McCallaa81e162009-12-01 22:10:20 +00007867 if (!E->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007868 // This is a reference to a member without an explicitly-specified
7869 // template argument list. Optimize for this common case.
7870 if (!getDerived().AlwaysRebuild() &&
John McCallaa81e162009-12-01 22:10:20 +00007871 Base.get() == OldBase &&
7872 BaseType == E->getBaseType() &&
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007873 QualifierLoc == E->getQualifierLoc() &&
Abramo Bagnara25777432010-08-11 22:01:17 +00007874 NameInfo.getName() == E->getMember() &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007875 FirstQualifierInScope == E->getFirstQualifierFoundInScope())
John McCall3fa5cae2010-10-26 07:05:15 +00007876 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00007877
John McCall9ae2f072010-08-23 23:25:46 +00007878 return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(),
John McCallaa81e162009-12-01 22:10:20 +00007879 BaseType,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007880 E->isArrow(),
7881 E->getOperatorLoc(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007882 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007883 TemplateKWLoc,
John McCall129e2df2009-11-30 22:42:35 +00007884 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00007885 NameInfo,
John McCall129e2df2009-11-30 22:42:35 +00007886 /*TemplateArgs*/ 0);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007887 }
7888
John McCalld5532b62009-11-23 01:53:49 +00007889 TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00007890 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
7891 E->getNumTemplateArgs(),
7892 TransArgs))
7893 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00007894
John McCall9ae2f072010-08-23 23:25:46 +00007895 return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(),
John McCallaa81e162009-12-01 22:10:20 +00007896 BaseType,
Douglas Gregorb98b1992009-08-11 05:31:07 +00007897 E->isArrow(),
7898 E->getOperatorLoc(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00007899 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007900 TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00007901 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00007902 NameInfo,
John McCall129e2df2009-11-30 22:42:35 +00007903 &TransArgs);
7904}
7905
7906template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00007907ExprResult
John McCall454feb92009-12-08 09:21:05 +00007908TreeTransform<Derived>::TransformUnresolvedMemberExpr(UnresolvedMemberExpr *Old) {
John McCall129e2df2009-11-30 22:42:35 +00007909 // Transform the base of the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00007910 ExprResult Base((Expr*) 0);
John McCallaa81e162009-12-01 22:10:20 +00007911 QualType BaseType;
7912 if (!Old->isImplicitAccess()) {
7913 Base = getDerived().TransformExpr(Old->getBase());
7914 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00007915 return ExprError();
Richard Smith9138b4e2011-10-26 19:06:56 +00007916 Base = getSema().PerformMemberExprBaseConversion(Base.take(),
7917 Old->isArrow());
7918 if (Base.isInvalid())
7919 return ExprError();
7920 BaseType = Base.get()->getType();
John McCallaa81e162009-12-01 22:10:20 +00007921 } else {
7922 BaseType = getDerived().TransformType(Old->getBaseType());
7923 }
John McCall129e2df2009-11-30 22:42:35 +00007924
Douglas Gregor4c9be892011-02-28 20:01:57 +00007925 NestedNameSpecifierLoc QualifierLoc;
7926 if (Old->getQualifierLoc()) {
7927 QualifierLoc
7928 = getDerived().TransformNestedNameSpecifierLoc(Old->getQualifierLoc());
7929 if (!QualifierLoc)
John McCallf312b1e2010-08-26 23:41:50 +00007930 return ExprError();
John McCall129e2df2009-11-30 22:42:35 +00007931 }
7932
Abramo Bagnarae4b92762012-01-27 09:46:47 +00007933 SourceLocation TemplateKWLoc = Old->getTemplateKeywordLoc();
7934
Abramo Bagnara25777432010-08-11 22:01:17 +00007935 LookupResult R(SemaRef, Old->getMemberNameInfo(),
John McCall129e2df2009-11-30 22:42:35 +00007936 Sema::LookupOrdinaryName);
7937
7938 // Transform all the decls.
7939 for (UnresolvedMemberExpr::decls_iterator I = Old->decls_begin(),
7940 E = Old->decls_end(); I != E; ++I) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00007941 NamedDecl *InstD = static_cast<NamedDecl*>(
7942 getDerived().TransformDecl(Old->getMemberLoc(),
7943 *I));
John McCall9f54ad42009-12-10 09:41:52 +00007944 if (!InstD) {
7945 // Silently ignore these if a UsingShadowDecl instantiated to nothing.
7946 // This can happen because of dependent hiding.
7947 if (isa<UsingShadowDecl>(*I))
7948 continue;
Argyrios Kyrtzidis34f52d12011-04-22 01:18:40 +00007949 else {
7950 R.clear();
John McCallf312b1e2010-08-26 23:41:50 +00007951 return ExprError();
Argyrios Kyrtzidis34f52d12011-04-22 01:18:40 +00007952 }
John McCall9f54ad42009-12-10 09:41:52 +00007953 }
John McCall129e2df2009-11-30 22:42:35 +00007954
7955 // Expand using declarations.
7956 if (isa<UsingDecl>(InstD)) {
7957 UsingDecl *UD = cast<UsingDecl>(InstD);
7958 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
7959 E = UD->shadow_end(); I != E; ++I)
7960 R.addDecl(*I);
7961 continue;
7962 }
7963
7964 R.addDecl(InstD);
7965 }
7966
7967 R.resolveKind();
7968
Douglas Gregorc96be1e2010-04-27 18:19:34 +00007969 // Determine the naming class.
Chandler Carruth042d6f92010-05-19 01:37:01 +00007970 if (Old->getNamingClass()) {
Sean Huntc3021132010-05-05 15:23:54 +00007971 CXXRecordDecl *NamingClass
Douglas Gregorc96be1e2010-04-27 18:19:34 +00007972 = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
Douglas Gregor66c45152010-04-27 16:10:10 +00007973 Old->getMemberLoc(),
7974 Old->getNamingClass()));
7975 if (!NamingClass)
John McCallf312b1e2010-08-26 23:41:50 +00007976 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00007977
Douglas Gregor66c45152010-04-27 16:10:10 +00007978 R.setNamingClass(NamingClass);
Douglas Gregorc96be1e2010-04-27 18:19:34 +00007979 }
Sean Huntc3021132010-05-05 15:23:54 +00007980
John McCall129e2df2009-11-30 22:42:35 +00007981 TemplateArgumentListInfo TransArgs;
7982 if (Old->hasExplicitTemplateArgs()) {
7983 TransArgs.setLAngleLoc(Old->getLAngleLoc());
7984 TransArgs.setRAngleLoc(Old->getRAngleLoc());
Douglas Gregorfcc12532010-12-20 17:31:10 +00007985 if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(),
7986 Old->getNumTemplateArgs(),
7987 TransArgs))
7988 return ExprError();
John McCall129e2df2009-11-30 22:42:35 +00007989 }
John McCallc2233c52010-01-15 08:34:02 +00007990
7991 // FIXME: to do this check properly, we will need to preserve the
7992 // first-qualifier-in-scope here, just in case we had a dependent
7993 // base (and therefore couldn't do the check) and a
7994 // nested-name-qualifier (and therefore could do the lookup).
7995 NamedDecl *FirstQualifierInScope = 0;
Sean Huntc3021132010-05-05 15:23:54 +00007996
John McCall9ae2f072010-08-23 23:25:46 +00007997 return getDerived().RebuildUnresolvedMemberExpr(Base.get(),
John McCallaa81e162009-12-01 22:10:20 +00007998 BaseType,
John McCall129e2df2009-11-30 22:42:35 +00007999 Old->getOperatorLoc(),
8000 Old->isArrow(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00008001 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008002 TemplateKWLoc,
John McCallc2233c52010-01-15 08:34:02 +00008003 FirstQualifierInScope,
John McCall129e2df2009-11-30 22:42:35 +00008004 R,
8005 (Old->hasExplicitTemplateArgs()
8006 ? &TransArgs : 0));
Douglas Gregorb98b1992009-08-11 05:31:07 +00008007}
8008
8009template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008010ExprResult
Sebastian Redl2e156222010-09-10 20:55:43 +00008011TreeTransform<Derived>::TransformCXXNoexceptExpr(CXXNoexceptExpr *E) {
Sean Hunteea06c62011-05-31 19:54:49 +00008012 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Sebastian Redl2e156222010-09-10 20:55:43 +00008013 ExprResult SubExpr = getDerived().TransformExpr(E->getOperand());
8014 if (SubExpr.isInvalid())
8015 return ExprError();
8016
8017 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getOperand())
John McCall3fa5cae2010-10-26 07:05:15 +00008018 return SemaRef.Owned(E);
Sebastian Redl2e156222010-09-10 20:55:43 +00008019
8020 return getDerived().RebuildCXXNoexceptExpr(E->getSourceRange(),SubExpr.get());
8021}
8022
8023template<typename Derived>
8024ExprResult
Douglas Gregorbe230c32011-01-03 17:17:50 +00008025TreeTransform<Derived>::TransformPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregor4f1d2822011-01-13 00:19:55 +00008026 ExprResult Pattern = getDerived().TransformExpr(E->getPattern());
8027 if (Pattern.isInvalid())
8028 return ExprError();
8029
8030 if (!getDerived().AlwaysRebuild() && Pattern.get() == E->getPattern())
8031 return SemaRef.Owned(E);
8032
Douglas Gregor67fd1252011-01-14 21:20:45 +00008033 return getDerived().RebuildPackExpansion(Pattern.get(), E->getEllipsisLoc(),
8034 E->getNumExpansions());
Douglas Gregorbe230c32011-01-03 17:17:50 +00008035}
Douglas Gregoree8aff02011-01-04 17:33:58 +00008036
8037template<typename Derived>
8038ExprResult
8039TreeTransform<Derived>::TransformSizeOfPackExpr(SizeOfPackExpr *E) {
8040 // If E is not value-dependent, then nothing will change when we transform it.
8041 // Note: This is an instantiation-centric view.
8042 if (!E->isValueDependent())
8043 return SemaRef.Owned(E);
8044
8045 // Note: None of the implementations of TryExpandParameterPacks can ever
8046 // produce a diagnostic when given only a single unexpanded parameter pack,
8047 // so
8048 UnexpandedParameterPack Unexpanded(E->getPack(), E->getPackLoc());
8049 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00008050 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00008051 llvm::Optional<unsigned> NumExpansions;
Douglas Gregoree8aff02011-01-04 17:33:58 +00008052 if (getDerived().TryExpandParameterPacks(E->getOperatorLoc(), E->getPackLoc(),
David Blaikiea71f9d02011-09-22 02:34:54 +00008053 Unexpanded,
Douglas Gregord3731192011-01-10 07:32:04 +00008054 ShouldExpand, RetainExpansion,
8055 NumExpansions))
Douglas Gregoree8aff02011-01-04 17:33:58 +00008056 return ExprError();
Douglas Gregorbe230c32011-01-03 17:17:50 +00008057
Douglas Gregor089e8932011-10-10 18:59:29 +00008058 if (RetainExpansion)
Douglas Gregoree8aff02011-01-04 17:33:58 +00008059 return SemaRef.Owned(E);
Douglas Gregor089e8932011-10-10 18:59:29 +00008060
8061 NamedDecl *Pack = E->getPack();
8062 if (!ShouldExpand) {
8063 Pack = cast_or_null<NamedDecl>(getDerived().TransformDecl(E->getPackLoc(),
8064 Pack));
8065 if (!Pack)
8066 return ExprError();
8067 }
8068
Douglas Gregoree8aff02011-01-04 17:33:58 +00008069
8070 // We now know the length of the parameter pack, so build a new expression
8071 // that stores that length.
Douglas Gregor089e8932011-10-10 18:59:29 +00008072 return getDerived().RebuildSizeOfPackExpr(E->getOperatorLoc(), Pack,
Douglas Gregoree8aff02011-01-04 17:33:58 +00008073 E->getPackLoc(), E->getRParenLoc(),
Douglas Gregor089e8932011-10-10 18:59:29 +00008074 NumExpansions);
Douglas Gregoree8aff02011-01-04 17:33:58 +00008075}
8076
Douglas Gregorbe230c32011-01-03 17:17:50 +00008077template<typename Derived>
8078ExprResult
Douglas Gregorc7793c72011-01-15 01:15:58 +00008079TreeTransform<Derived>::TransformSubstNonTypeTemplateParmPackExpr(
8080 SubstNonTypeTemplateParmPackExpr *E) {
8081 // Default behavior is to do nothing with this transformation.
8082 return SemaRef.Owned(E);
8083}
8084
8085template<typename Derived>
8086ExprResult
John McCall91a57552011-07-15 05:09:51 +00008087TreeTransform<Derived>::TransformSubstNonTypeTemplateParmExpr(
8088 SubstNonTypeTemplateParmExpr *E) {
8089 // Default behavior is to do nothing with this transformation.
8090 return SemaRef.Owned(E);
8091}
8092
8093template<typename Derived>
8094ExprResult
Douglas Gregor03e80032011-06-21 17:03:29 +00008095TreeTransform<Derived>::TransformMaterializeTemporaryExpr(
8096 MaterializeTemporaryExpr *E) {
8097 return getDerived().TransformExpr(E->GetTemporaryExpr());
8098}
8099
8100template<typename Derived>
8101ExprResult
John McCall454feb92009-12-08 09:21:05 +00008102TreeTransform<Derived>::TransformObjCStringLiteral(ObjCStringLiteral *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00008103 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008104}
8105
Mike Stump1eb44332009-09-09 15:08:12 +00008106template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008107ExprResult
John McCall454feb92009-12-08 09:21:05 +00008108TreeTransform<Derived>::TransformObjCEncodeExpr(ObjCEncodeExpr *E) {
Douglas Gregor81d34662010-04-20 15:39:42 +00008109 TypeSourceInfo *EncodedTypeInfo
8110 = getDerived().TransformType(E->getEncodedTypeSourceInfo());
8111 if (!EncodedTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00008112 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00008113
Douglas Gregorb98b1992009-08-11 05:31:07 +00008114 if (!getDerived().AlwaysRebuild() &&
Douglas Gregor81d34662010-04-20 15:39:42 +00008115 EncodedTypeInfo == E->getEncodedTypeSourceInfo())
John McCall3fa5cae2010-10-26 07:05:15 +00008116 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008117
8118 return getDerived().RebuildObjCEncodeExpr(E->getAtLoc(),
Douglas Gregor81d34662010-04-20 15:39:42 +00008119 EncodedTypeInfo,
Douglas Gregorb98b1992009-08-11 05:31:07 +00008120 E->getRParenLoc());
8121}
Mike Stump1eb44332009-09-09 15:08:12 +00008122
Douglas Gregorb98b1992009-08-11 05:31:07 +00008123template<typename Derived>
John McCallf85e1932011-06-15 23:02:42 +00008124ExprResult TreeTransform<Derived>::
8125TransformObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
8126 ExprResult result = getDerived().TransformExpr(E->getSubExpr());
8127 if (result.isInvalid()) return ExprError();
8128 Expr *subExpr = result.take();
8129
8130 if (!getDerived().AlwaysRebuild() &&
8131 subExpr == E->getSubExpr())
8132 return SemaRef.Owned(E);
8133
8134 return SemaRef.Owned(new(SemaRef.Context)
8135 ObjCIndirectCopyRestoreExpr(subExpr, E->getType(), E->shouldCopy()));
8136}
8137
8138template<typename Derived>
8139ExprResult TreeTransform<Derived>::
8140TransformObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
8141 TypeSourceInfo *TSInfo
8142 = getDerived().TransformType(E->getTypeInfoAsWritten());
8143 if (!TSInfo)
8144 return ExprError();
8145
8146 ExprResult Result = getDerived().TransformExpr(E->getSubExpr());
8147 if (Result.isInvalid())
8148 return ExprError();
8149
8150 if (!getDerived().AlwaysRebuild() &&
8151 TSInfo == E->getTypeInfoAsWritten() &&
8152 Result.get() == E->getSubExpr())
8153 return SemaRef.Owned(E);
8154
8155 return SemaRef.BuildObjCBridgedCast(E->getLParenLoc(), E->getBridgeKind(),
8156 E->getBridgeKeywordLoc(), TSInfo,
8157 Result.get());
8158}
8159
8160template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008161ExprResult
John McCall454feb92009-12-08 09:21:05 +00008162TreeTransform<Derived>::TransformObjCMessageExpr(ObjCMessageExpr *E) {
Douglas Gregor92e986e2010-04-22 16:44:27 +00008163 // Transform arguments.
8164 bool ArgChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00008165 ASTOwningVector<Expr*> Args(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00008166 Args.reserve(E->getNumArgs());
8167 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), false, Args,
8168 &ArgChanged))
8169 return ExprError();
8170
Douglas Gregor92e986e2010-04-22 16:44:27 +00008171 if (E->getReceiverKind() == ObjCMessageExpr::Class) {
8172 // Class message: transform the receiver type.
8173 TypeSourceInfo *ReceiverTypeInfo
8174 = getDerived().TransformType(E->getClassReceiverTypeInfo());
8175 if (!ReceiverTypeInfo)
John McCallf312b1e2010-08-26 23:41:50 +00008176 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00008177
Douglas Gregor92e986e2010-04-22 16:44:27 +00008178 // If nothing changed, just retain the existing message send.
8179 if (!getDerived().AlwaysRebuild() &&
8180 ReceiverTypeInfo == E->getClassReceiverTypeInfo() && !ArgChanged)
Douglas Gregor92be2a52011-12-10 00:23:21 +00008181 return SemaRef.MaybeBindToTemporary(E);
Douglas Gregor92e986e2010-04-22 16:44:27 +00008182
8183 // Build a new class message send.
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00008184 SmallVector<SourceLocation, 16> SelLocs;
8185 E->getSelectorLocs(SelLocs);
Douglas Gregor92e986e2010-04-22 16:44:27 +00008186 return getDerived().RebuildObjCMessageExpr(ReceiverTypeInfo,
8187 E->getSelector(),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00008188 SelLocs,
Douglas Gregor92e986e2010-04-22 16:44:27 +00008189 E->getMethodDecl(),
8190 E->getLeftLoc(),
8191 move_arg(Args),
8192 E->getRightLoc());
8193 }
8194
8195 // Instance message: transform the receiver
8196 assert(E->getReceiverKind() == ObjCMessageExpr::Instance &&
8197 "Only class and instance messages may be instantiated");
John McCall60d7b3a2010-08-24 06:29:42 +00008198 ExprResult Receiver
Douglas Gregor92e986e2010-04-22 16:44:27 +00008199 = getDerived().TransformExpr(E->getInstanceReceiver());
8200 if (Receiver.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008201 return ExprError();
Douglas Gregor92e986e2010-04-22 16:44:27 +00008202
8203 // If nothing changed, just retain the existing message send.
8204 if (!getDerived().AlwaysRebuild() &&
8205 Receiver.get() == E->getInstanceReceiver() && !ArgChanged)
Douglas Gregor92be2a52011-12-10 00:23:21 +00008206 return SemaRef.MaybeBindToTemporary(E);
Sean Huntc3021132010-05-05 15:23:54 +00008207
Douglas Gregor92e986e2010-04-22 16:44:27 +00008208 // Build a new instance message send.
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00008209 SmallVector<SourceLocation, 16> SelLocs;
8210 E->getSelectorLocs(SelLocs);
John McCall9ae2f072010-08-23 23:25:46 +00008211 return getDerived().RebuildObjCMessageExpr(Receiver.get(),
Douglas Gregor92e986e2010-04-22 16:44:27 +00008212 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());
Douglas Gregorb98b1992009-08-11 05:31:07 +00008218}
8219
Mike Stump1eb44332009-09-09 15:08:12 +00008220template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008221ExprResult
John McCall454feb92009-12-08 09:21:05 +00008222TreeTransform<Derived>::TransformObjCSelectorExpr(ObjCSelectorExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00008223 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008224}
8225
Mike Stump1eb44332009-09-09 15:08:12 +00008226template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008227ExprResult
John McCall454feb92009-12-08 09:21:05 +00008228TreeTransform<Derived>::TransformObjCProtocolExpr(ObjCProtocolExpr *E) {
John McCall3fa5cae2010-10-26 07:05:15 +00008229 return SemaRef.Owned(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008230}
8231
Mike Stump1eb44332009-09-09 15:08:12 +00008232template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008233ExprResult
John McCall454feb92009-12-08 09:21:05 +00008234TreeTransform<Derived>::TransformObjCIvarRefExpr(ObjCIvarRefExpr *E) {
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008235 // Transform the base expression.
John McCall60d7b3a2010-08-24 06:29:42 +00008236 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008237 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008238 return ExprError();
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008239
8240 // We don't need to transform the ivar; it will never change.
Sean Huntc3021132010-05-05 15:23:54 +00008241
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008242 // If nothing changed, just retain the existing expression.
8243 if (!getDerived().AlwaysRebuild() &&
8244 Base.get() == E->getBase())
John McCall3fa5cae2010-10-26 07:05:15 +00008245 return SemaRef.Owned(E);
Sean Huntc3021132010-05-05 15:23:54 +00008246
John McCall9ae2f072010-08-23 23:25:46 +00008247 return getDerived().RebuildObjCIvarRefExpr(Base.get(), E->getDecl(),
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008248 E->getLocation(),
8249 E->isArrow(), E->isFreeIvar());
Douglas Gregorb98b1992009-08-11 05:31:07 +00008250}
8251
Mike Stump1eb44332009-09-09 15:08:12 +00008252template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008253ExprResult
John McCall454feb92009-12-08 09:21:05 +00008254TreeTransform<Derived>::TransformObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
John McCall12f78a62010-12-02 01:19:52 +00008255 // 'super' and types never change. Property never changes. Just
8256 // retain the existing expression.
8257 if (!E->isObjectReceiver())
John McCall3fa5cae2010-10-26 07:05:15 +00008258 return SemaRef.Owned(E);
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00008259
Douglas Gregore3303542010-04-26 20:47:02 +00008260 // Transform the base expression.
John McCall60d7b3a2010-08-24 06:29:42 +00008261 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregore3303542010-04-26 20:47:02 +00008262 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008263 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00008264
Douglas Gregore3303542010-04-26 20:47:02 +00008265 // We don't need to transform the property; it will never change.
Sean Huntc3021132010-05-05 15:23:54 +00008266
Douglas Gregore3303542010-04-26 20:47:02 +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);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008271
John McCall12f78a62010-12-02 01:19:52 +00008272 if (E->isExplicitProperty())
8273 return getDerived().RebuildObjCPropertyRefExpr(Base.get(),
8274 E->getExplicitProperty(),
8275 E->getLocation());
8276
8277 return getDerived().RebuildObjCPropertyRefExpr(Base.get(),
John McCall3c3b7f92011-10-25 17:37:35 +00008278 SemaRef.Context.PseudoObjectTy,
John McCall12f78a62010-12-02 01:19:52 +00008279 E->getImplicitPropertyGetter(),
8280 E->getImplicitPropertySetter(),
8281 E->getLocation());
Douglas Gregorb98b1992009-08-11 05:31:07 +00008282}
8283
Mike Stump1eb44332009-09-09 15:08:12 +00008284template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008285ExprResult
John McCall454feb92009-12-08 09:21:05 +00008286TreeTransform<Derived>::TransformObjCIsaExpr(ObjCIsaExpr *E) {
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008287 // Transform the base expression.
John McCall60d7b3a2010-08-24 06:29:42 +00008288 ExprResult Base = getDerived().TransformExpr(E->getBase());
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008289 if (Base.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008290 return ExprError();
Sean Huntc3021132010-05-05 15:23:54 +00008291
Douglas Gregorf9b9eab2010-04-26 20:11:03 +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);
Sean Huntc3021132010-05-05 15:23:54 +00008296
John McCall9ae2f072010-08-23 23:25:46 +00008297 return getDerived().RebuildObjCIsaExpr(Base.get(), E->getIsaMemberLoc(),
Douglas Gregorf9b9eab2010-04-26 20:11:03 +00008298 E->isArrow());
Douglas Gregorb98b1992009-08-11 05:31:07 +00008299}
8300
Mike Stump1eb44332009-09-09 15:08:12 +00008301template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008302ExprResult
John McCall454feb92009-12-08 09:21:05 +00008303TreeTransform<Derived>::TransformShuffleVectorExpr(ShuffleVectorExpr *E) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00008304 bool ArgumentChanged = false;
John McCallca0408f2010-08-23 06:44:23 +00008305 ASTOwningVector<Expr*> SubExprs(SemaRef);
Douglas Gregoraa165f82011-01-03 19:04:46 +00008306 SubExprs.reserve(E->getNumSubExprs());
8307 if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false,
8308 SubExprs, &ArgumentChanged))
8309 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00008310
Douglas Gregorb98b1992009-08-11 05:31:07 +00008311 if (!getDerived().AlwaysRebuild() &&
8312 !ArgumentChanged)
John McCall3fa5cae2010-10-26 07:05:15 +00008313 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00008314
Douglas Gregorb98b1992009-08-11 05:31:07 +00008315 return getDerived().RebuildShuffleVectorExpr(E->getBuiltinLoc(),
8316 move_arg(SubExprs),
8317 E->getRParenLoc());
8318}
8319
Mike Stump1eb44332009-09-09 15:08:12 +00008320template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008321ExprResult
John McCall454feb92009-12-08 09:21:05 +00008322TreeTransform<Derived>::TransformBlockExpr(BlockExpr *E) {
John McCallc6ac9c32011-02-04 18:33:18 +00008323 BlockDecl *oldBlock = E->getBlockDecl();
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008324
John McCallc6ac9c32011-02-04 18:33:18 +00008325 SemaRef.ActOnBlockStart(E->getCaretLocation(), /*Scope=*/0);
8326 BlockScopeInfo *blockScope = SemaRef.getCurBlock();
8327
8328 blockScope->TheDecl->setIsVariadic(oldBlock->isVariadic());
Fariborz Jahanian05865202011-12-03 17:47:53 +00008329 blockScope->TheDecl->setBlockMissingReturnType(
8330 oldBlock->blockMissingReturnType());
Fariborz Jahanianff365592011-05-05 17:18:12 +00008331
Chris Lattner686775d2011-07-20 06:58:45 +00008332 SmallVector<ParmVarDecl*, 4> params;
8333 SmallVector<QualType, 4> paramTypes;
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008334
8335 // Parameter substitution.
John McCallc6ac9c32011-02-04 18:33:18 +00008336 if (getDerived().TransformFunctionTypeParams(E->getCaretLocation(),
8337 oldBlock->param_begin(),
8338 oldBlock->param_size(),
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008339 0, paramTypes, &params)) {
8340 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
Douglas Gregor92be2a52011-12-10 00:23:21 +00008341 return ExprError();
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008342 }
John McCallc6ac9c32011-02-04 18:33:18 +00008343
8344 const FunctionType *exprFunctionType = E->getFunctionType();
Eli Friedman84b007f2012-01-26 03:00:14 +00008345 QualType exprResultType =
8346 getDerived().TransformType(exprFunctionType->getResultType());
Douglas Gregora779d9c2011-01-19 21:32:01 +00008347
8348 // Don't allow returning a objc interface by value.
Eli Friedman84b007f2012-01-26 03:00:14 +00008349 if (exprResultType->isObjCObjectType()) {
John McCallc6ac9c32011-02-04 18:33:18 +00008350 getSema().Diag(E->getCaretLocation(),
Douglas Gregora779d9c2011-01-19 21:32:01 +00008351 diag::err_object_cannot_be_passed_returned_by_value)
Eli Friedman84b007f2012-01-26 03:00:14 +00008352 << 0 << exprResultType;
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008353 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
Douglas Gregora779d9c2011-01-19 21:32:01 +00008354 return ExprError();
8355 }
John McCall711c52b2011-01-05 12:14:39 +00008356
John McCallc6ac9c32011-02-04 18:33:18 +00008357 QualType functionType = getDerived().RebuildFunctionProtoType(
Eli Friedman84b007f2012-01-26 03:00:14 +00008358 exprResultType,
John McCallc6ac9c32011-02-04 18:33:18 +00008359 paramTypes.data(),
8360 paramTypes.size(),
8361 oldBlock->isVariadic(),
Richard Smitheefb3d52012-02-10 09:58:53 +00008362 false, 0, RQ_None,
John McCallc6ac9c32011-02-04 18:33:18 +00008363 exprFunctionType->getExtInfo());
8364 blockScope->FunctionType = functionType;
John McCall711c52b2011-01-05 12:14:39 +00008365
8366 // Set the parameters on the block decl.
John McCallc6ac9c32011-02-04 18:33:18 +00008367 if (!params.empty())
David Blaikie4278c652011-09-21 18:16:56 +00008368 blockScope->TheDecl->setParams(params);
Eli Friedman84b007f2012-01-26 03:00:14 +00008369
8370 if (!oldBlock->blockMissingReturnType()) {
8371 blockScope->HasImplicitReturnType = false;
8372 blockScope->ReturnType = exprResultType;
8373 }
Douglas Gregora779d9c2011-01-19 21:32:01 +00008374
John McCall711c52b2011-01-05 12:14:39 +00008375 // Transform the body
John McCallc6ac9c32011-02-04 18:33:18 +00008376 StmtResult body = getDerived().TransformStmt(E->getBody());
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008377 if (body.isInvalid()) {
8378 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
John McCall711c52b2011-01-05 12:14:39 +00008379 return ExprError();
Argyrios Kyrtzidis00b46572012-01-25 03:53:04 +00008380 }
John McCall711c52b2011-01-05 12:14:39 +00008381
John McCallc6ac9c32011-02-04 18:33:18 +00008382#ifndef NDEBUG
8383 // In builds with assertions, make sure that we captured everything we
8384 // captured before.
Douglas Gregorfc921372011-05-20 15:32:55 +00008385 if (!SemaRef.getDiagnostics().hasErrorOccurred()) {
8386 for (BlockDecl::capture_iterator i = oldBlock->capture_begin(),
8387 e = oldBlock->capture_end(); i != e; ++i) {
8388 VarDecl *oldCapture = i->getVariable();
John McCallc6ac9c32011-02-04 18:33:18 +00008389
Douglas Gregorfc921372011-05-20 15:32:55 +00008390 // Ignore parameter packs.
8391 if (isa<ParmVarDecl>(oldCapture) &&
8392 cast<ParmVarDecl>(oldCapture)->isParameterPack())
8393 continue;
John McCallc6ac9c32011-02-04 18:33:18 +00008394
Douglas Gregorfc921372011-05-20 15:32:55 +00008395 VarDecl *newCapture =
8396 cast<VarDecl>(getDerived().TransformDecl(E->getCaretLocation(),
8397 oldCapture));
8398 assert(blockScope->CaptureMap.count(newCapture));
8399 }
John McCallc6ac9c32011-02-04 18:33:18 +00008400 }
8401#endif
8402
8403 return SemaRef.ActOnBlockStmtExpr(E->getCaretLocation(), body.get(),
8404 /*Scope=*/0);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008405}
8406
Mike Stump1eb44332009-09-09 15:08:12 +00008407template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008408ExprResult
John McCall454feb92009-12-08 09:21:05 +00008409TreeTransform<Derived>::TransformBlockDeclRefExpr(BlockDeclRefExpr *E) {
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008410 ValueDecl *ND
8411 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getLocation(),
8412 E->getDecl()));
8413 if (!ND)
John McCallf312b1e2010-08-26 23:41:50 +00008414 return ExprError();
Abramo Bagnara25777432010-08-11 22:01:17 +00008415
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008416 if (!getDerived().AlwaysRebuild() &&
8417 ND == E->getDecl()) {
8418 // Mark it referenced in the new context regardless.
8419 // FIXME: this is a bit instantiation-specific.
Eli Friedman5f2987c2012-02-02 03:46:19 +00008420 SemaRef.MarkBlockDeclRefReferenced(E);
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008421
John McCall3fa5cae2010-10-26 07:05:15 +00008422 return SemaRef.Owned(E);
Fariborz Jahaniana729da22010-07-09 18:44:02 +00008423 }
8424
Abramo Bagnara25777432010-08-11 22:01:17 +00008425 DeclarationNameInfo NameInfo(E->getDecl()->getDeclName(), E->getLocation());
Douglas Gregor40d96a62011-02-28 21:54:11 +00008426 return getDerived().RebuildDeclRefExpr(NestedNameSpecifierLoc(),
Abramo Bagnara25777432010-08-11 22:01:17 +00008427 ND, NameInfo, 0);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008428}
Mike Stump1eb44332009-09-09 15:08:12 +00008429
Tanya Lattner61eee0c2011-06-04 00:47:47 +00008430template<typename Derived>
8431ExprResult
8432TreeTransform<Derived>::TransformAsTypeExpr(AsTypeExpr *E) {
David Blaikieb219cfc2011-09-23 05:06:16 +00008433 llvm_unreachable("Cannot transform asType expressions yet");
Tanya Lattner61eee0c2011-06-04 00:47:47 +00008434}
Eli Friedman276b0612011-10-11 02:20:01 +00008435
8436template<typename Derived>
8437ExprResult
8438TreeTransform<Derived>::TransformAtomicExpr(AtomicExpr *E) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00008439 QualType RetTy = getDerived().TransformType(E->getType());
8440 bool ArgumentChanged = false;
8441 ASTOwningVector<Expr*> SubExprs(SemaRef);
8442 SubExprs.reserve(E->getNumSubExprs());
8443 if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false,
8444 SubExprs, &ArgumentChanged))
8445 return ExprError();
8446
8447 if (!getDerived().AlwaysRebuild() &&
8448 !ArgumentChanged)
8449 return SemaRef.Owned(E);
8450
8451 return getDerived().RebuildAtomicExpr(E->getBuiltinLoc(), move_arg(SubExprs),
8452 RetTy, E->getOp(), E->getRParenLoc());
Eli Friedman276b0612011-10-11 02:20:01 +00008453}
Tanya Lattner61eee0c2011-06-04 00:47:47 +00008454
Douglas Gregorb98b1992009-08-11 05:31:07 +00008455//===----------------------------------------------------------------------===//
Douglas Gregor577f75a2009-08-04 16:50:30 +00008456// Type reconstruction
8457//===----------------------------------------------------------------------===//
8458
Mike Stump1eb44332009-09-09 15:08:12 +00008459template<typename Derived>
John McCall85737a72009-10-30 00:06:24 +00008460QualType TreeTransform<Derived>::RebuildPointerType(QualType PointeeType,
8461 SourceLocation Star) {
John McCall28654742010-06-05 06:41:15 +00008462 return SemaRef.BuildPointerType(PointeeType, Star,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008463 getDerived().getBaseEntity());
8464}
8465
Mike Stump1eb44332009-09-09 15:08:12 +00008466template<typename Derived>
John McCall85737a72009-10-30 00:06:24 +00008467QualType TreeTransform<Derived>::RebuildBlockPointerType(QualType PointeeType,
8468 SourceLocation Star) {
John McCall28654742010-06-05 06:41:15 +00008469 return SemaRef.BuildBlockPointerType(PointeeType, Star,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008470 getDerived().getBaseEntity());
8471}
8472
Mike Stump1eb44332009-09-09 15:08:12 +00008473template<typename Derived>
8474QualType
John McCall85737a72009-10-30 00:06:24 +00008475TreeTransform<Derived>::RebuildReferenceType(QualType ReferentType,
8476 bool WrittenAsLValue,
8477 SourceLocation Sigil) {
John McCall28654742010-06-05 06:41:15 +00008478 return SemaRef.BuildReferenceType(ReferentType, WrittenAsLValue,
John McCall85737a72009-10-30 00:06:24 +00008479 Sigil, getDerived().getBaseEntity());
Douglas Gregor577f75a2009-08-04 16:50:30 +00008480}
8481
8482template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008483QualType
John McCall85737a72009-10-30 00:06:24 +00008484TreeTransform<Derived>::RebuildMemberPointerType(QualType PointeeType,
8485 QualType ClassType,
8486 SourceLocation Sigil) {
John McCall28654742010-06-05 06:41:15 +00008487 return SemaRef.BuildMemberPointerType(PointeeType, ClassType,
John McCall85737a72009-10-30 00:06:24 +00008488 Sigil, getDerived().getBaseEntity());
Douglas Gregor577f75a2009-08-04 16:50:30 +00008489}
8490
8491template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008492QualType
Douglas Gregor577f75a2009-08-04 16:50:30 +00008493TreeTransform<Derived>::RebuildArrayType(QualType ElementType,
8494 ArrayType::ArraySizeModifier SizeMod,
8495 const llvm::APInt *Size,
8496 Expr *SizeExpr,
8497 unsigned IndexTypeQuals,
8498 SourceRange BracketsRange) {
8499 if (SizeExpr || !Size)
8500 return SemaRef.BuildArrayType(ElementType, SizeMod, SizeExpr,
8501 IndexTypeQuals, BracketsRange,
8502 getDerived().getBaseEntity());
Mike Stump1eb44332009-09-09 15:08:12 +00008503
8504 QualType Types[] = {
8505 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
8506 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
8507 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
Douglas Gregor577f75a2009-08-04 16:50:30 +00008508 };
8509 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
8510 QualType SizeType;
8511 for (unsigned I = 0; I != NumTypes; ++I)
8512 if (Size->getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
8513 SizeType = Types[I];
8514 break;
8515 }
Mike Stump1eb44332009-09-09 15:08:12 +00008516
Eli Friedman01f276d2012-01-25 23:20:27 +00008517 // Note that we can return a VariableArrayType here in the case where
8518 // the element type was a dependent VariableArrayType.
8519 IntegerLiteral *ArraySize
8520 = IntegerLiteral::Create(SemaRef.Context, *Size, SizeType,
8521 /*FIXME*/BracketsRange.getBegin());
8522 return SemaRef.BuildArrayType(ElementType, SizeMod, ArraySize,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008523 IndexTypeQuals, BracketsRange,
Mike Stump1eb44332009-09-09 15:08:12 +00008524 getDerived().getBaseEntity());
Douglas Gregor577f75a2009-08-04 16:50:30 +00008525}
Mike Stump1eb44332009-09-09 15:08:12 +00008526
Douglas Gregor577f75a2009-08-04 16:50:30 +00008527template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008528QualType
8529TreeTransform<Derived>::RebuildConstantArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008530 ArrayType::ArraySizeModifier SizeMod,
8531 const llvm::APInt &Size,
John McCall85737a72009-10-30 00:06:24 +00008532 unsigned IndexTypeQuals,
8533 SourceRange BracketsRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00008534 return getDerived().RebuildArrayType(ElementType, SizeMod, &Size, 0,
John McCall85737a72009-10-30 00:06:24 +00008535 IndexTypeQuals, BracketsRange);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008536}
8537
8538template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008539QualType
Mike Stump1eb44332009-09-09 15:08:12 +00008540TreeTransform<Derived>::RebuildIncompleteArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008541 ArrayType::ArraySizeModifier SizeMod,
John McCall85737a72009-10-30 00:06:24 +00008542 unsigned IndexTypeQuals,
8543 SourceRange BracketsRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00008544 return getDerived().RebuildArrayType(ElementType, SizeMod, 0, 0,
John McCall85737a72009-10-30 00:06:24 +00008545 IndexTypeQuals, BracketsRange);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008546}
Mike Stump1eb44332009-09-09 15:08:12 +00008547
Douglas Gregor577f75a2009-08-04 16:50:30 +00008548template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008549QualType
8550TreeTransform<Derived>::RebuildVariableArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008551 ArrayType::ArraySizeModifier SizeMod,
John McCall9ae2f072010-08-23 23:25:46 +00008552 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008553 unsigned IndexTypeQuals,
8554 SourceRange BracketsRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00008555 return getDerived().RebuildArrayType(ElementType, SizeMod, 0,
John McCall9ae2f072010-08-23 23:25:46 +00008556 SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008557 IndexTypeQuals, BracketsRange);
8558}
8559
8560template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008561QualType
8562TreeTransform<Derived>::RebuildDependentSizedArrayType(QualType ElementType,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008563 ArrayType::ArraySizeModifier SizeMod,
John McCall9ae2f072010-08-23 23:25:46 +00008564 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008565 unsigned IndexTypeQuals,
8566 SourceRange BracketsRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00008567 return getDerived().RebuildArrayType(ElementType, SizeMod, 0,
John McCall9ae2f072010-08-23 23:25:46 +00008568 SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008569 IndexTypeQuals, BracketsRange);
8570}
8571
8572template<typename Derived>
8573QualType TreeTransform<Derived>::RebuildVectorType(QualType ElementType,
Bob Wilsone86d78c2010-11-10 21:56:12 +00008574 unsigned NumElements,
8575 VectorType::VectorKind VecKind) {
Douglas Gregor577f75a2009-08-04 16:50:30 +00008576 // FIXME: semantic checking!
Bob Wilsone86d78c2010-11-10 21:56:12 +00008577 return SemaRef.Context.getVectorType(ElementType, NumElements, VecKind);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008578}
Mike Stump1eb44332009-09-09 15:08:12 +00008579
Douglas Gregor577f75a2009-08-04 16:50:30 +00008580template<typename Derived>
8581QualType TreeTransform<Derived>::RebuildExtVectorType(QualType ElementType,
8582 unsigned NumElements,
8583 SourceLocation AttributeLoc) {
8584 llvm::APInt numElements(SemaRef.Context.getIntWidth(SemaRef.Context.IntTy),
8585 NumElements, true);
8586 IntegerLiteral *VectorSize
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00008587 = IntegerLiteral::Create(SemaRef.Context, numElements, SemaRef.Context.IntTy,
8588 AttributeLoc);
John McCall9ae2f072010-08-23 23:25:46 +00008589 return SemaRef.BuildExtVectorType(ElementType, VectorSize, AttributeLoc);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008590}
Mike Stump1eb44332009-09-09 15:08:12 +00008591
Douglas Gregor577f75a2009-08-04 16:50:30 +00008592template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008593QualType
8594TreeTransform<Derived>::RebuildDependentSizedExtVectorType(QualType ElementType,
John McCall9ae2f072010-08-23 23:25:46 +00008595 Expr *SizeExpr,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008596 SourceLocation AttributeLoc) {
John McCall9ae2f072010-08-23 23:25:46 +00008597 return SemaRef.BuildExtVectorType(ElementType, SizeExpr, AttributeLoc);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008598}
Mike Stump1eb44332009-09-09 15:08:12 +00008599
Douglas Gregor577f75a2009-08-04 16:50:30 +00008600template<typename Derived>
8601QualType TreeTransform<Derived>::RebuildFunctionProtoType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00008602 QualType *ParamTypes,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008603 unsigned NumParamTypes,
Mike Stump1eb44332009-09-09 15:08:12 +00008604 bool Variadic,
Richard Smitheefb3d52012-02-10 09:58:53 +00008605 bool HasTrailingReturn,
Eli Friedmanfa869542010-08-05 02:54:05 +00008606 unsigned Quals,
Douglas Gregorc938c162011-01-26 05:01:58 +00008607 RefQualifierKind RefQualifier,
Eli Friedmanfa869542010-08-05 02:54:05 +00008608 const FunctionType::ExtInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00008609 return SemaRef.BuildFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Richard Smitheefb3d52012-02-10 09:58:53 +00008610 HasTrailingReturn, Quals, RefQualifier,
Douglas Gregor577f75a2009-08-04 16:50:30 +00008611 getDerived().getBaseLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00008612 getDerived().getBaseEntity(),
8613 Info);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008614}
Mike Stump1eb44332009-09-09 15:08:12 +00008615
Douglas Gregor577f75a2009-08-04 16:50:30 +00008616template<typename Derived>
John McCalla2becad2009-10-21 00:40:46 +00008617QualType TreeTransform<Derived>::RebuildFunctionNoProtoType(QualType T) {
8618 return SemaRef.Context.getFunctionNoProtoType(T);
8619}
8620
8621template<typename Derived>
John McCalled976492009-12-04 22:46:56 +00008622QualType TreeTransform<Derived>::RebuildUnresolvedUsingType(Decl *D) {
8623 assert(D && "no decl found");
8624 if (D->isInvalidDecl()) return QualType();
8625
Douglas Gregor92e986e2010-04-22 16:44:27 +00008626 // FIXME: Doesn't account for ObjCInterfaceDecl!
John McCalled976492009-12-04 22:46:56 +00008627 TypeDecl *Ty;
8628 if (isa<UsingDecl>(D)) {
8629 UsingDecl *Using = cast<UsingDecl>(D);
8630 assert(Using->isTypeName() &&
8631 "UnresolvedUsingTypenameDecl transformed to non-typename using");
8632
8633 // A valid resolved using typename decl points to exactly one type decl.
8634 assert(++Using->shadow_begin() == Using->shadow_end());
8635 Ty = cast<TypeDecl>((*Using->shadow_begin())->getTargetDecl());
Sean Huntc3021132010-05-05 15:23:54 +00008636
John McCalled976492009-12-04 22:46:56 +00008637 } else {
8638 assert(isa<UnresolvedUsingTypenameDecl>(D) &&
8639 "UnresolvedUsingTypenameDecl transformed to non-using decl");
8640 Ty = cast<UnresolvedUsingTypenameDecl>(D);
8641 }
8642
8643 return SemaRef.Context.getTypeDeclType(Ty);
8644}
8645
8646template<typename Derived>
John McCall2a984ca2010-10-12 00:20:44 +00008647QualType TreeTransform<Derived>::RebuildTypeOfExprType(Expr *E,
8648 SourceLocation Loc) {
8649 return SemaRef.BuildTypeofExprType(E, Loc);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008650}
8651
8652template<typename Derived>
8653QualType TreeTransform<Derived>::RebuildTypeOfType(QualType Underlying) {
8654 return SemaRef.Context.getTypeOfType(Underlying);
8655}
8656
8657template<typename Derived>
John McCall2a984ca2010-10-12 00:20:44 +00008658QualType TreeTransform<Derived>::RebuildDecltypeType(Expr *E,
8659 SourceLocation Loc) {
8660 return SemaRef.BuildDecltypeType(E, Loc);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008661}
8662
8663template<typename Derived>
Sean Huntca63c202011-05-24 22:41:36 +00008664QualType TreeTransform<Derived>::RebuildUnaryTransformType(QualType BaseType,
8665 UnaryTransformType::UTTKind UKind,
8666 SourceLocation Loc) {
8667 return SemaRef.BuildUnaryTransformType(BaseType, UKind, Loc);
8668}
8669
8670template<typename Derived>
Douglas Gregor577f75a2009-08-04 16:50:30 +00008671QualType TreeTransform<Derived>::RebuildTemplateSpecializationType(
John McCall833ca992009-10-29 08:12:44 +00008672 TemplateName Template,
8673 SourceLocation TemplateNameLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00008674 TemplateArgumentListInfo &TemplateArgs) {
John McCalld5532b62009-11-23 01:53:49 +00008675 return SemaRef.CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +00008676}
Mike Stump1eb44332009-09-09 15:08:12 +00008677
Douglas Gregordcee1a12009-08-06 05:28:30 +00008678template<typename Derived>
Eli Friedmanb001de72011-10-06 23:00:33 +00008679QualType TreeTransform<Derived>::RebuildAtomicType(QualType ValueType,
8680 SourceLocation KWLoc) {
8681 return SemaRef.BuildAtomicType(ValueType, KWLoc);
8682}
8683
8684template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008685TemplateName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008686TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
Douglas Gregord1067e52009-08-06 06:41:21 +00008687 bool TemplateKW,
8688 TemplateDecl *Template) {
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008689 return SemaRef.Context.getQualifiedTemplateName(SS.getScopeRep(), TemplateKW,
Douglas Gregord1067e52009-08-06 06:41:21 +00008690 Template);
8691}
8692
8693template<typename Derived>
Mike Stump1eb44332009-09-09 15:08:12 +00008694TemplateName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008695TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
8696 const IdentifierInfo &Name,
8697 SourceLocation NameLoc,
John McCall43fed0d2010-11-12 08:19:04 +00008698 QualType ObjectType,
8699 NamedDecl *FirstQualifierInScope) {
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008700 UnqualifiedId TemplateName;
8701 TemplateName.setIdentifier(&Name, NameLoc);
Douglas Gregord6ab2322010-06-16 23:00:59 +00008702 Sema::TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008703 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
Douglas Gregord6ab2322010-06-16 23:00:59 +00008704 getSema().ActOnDependentTemplateName(/*Scope=*/0,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008705 SS, TemplateKWLoc, TemplateName,
John McCallb3d87482010-08-24 05:47:05 +00008706 ParsedType::make(ObjectType),
Douglas Gregord6ab2322010-06-16 23:00:59 +00008707 /*EnteringContext=*/false,
8708 Template);
John McCall43fed0d2010-11-12 08:19:04 +00008709 return Template.get();
Douglas Gregord1067e52009-08-06 06:41:21 +00008710}
Mike Stump1eb44332009-09-09 15:08:12 +00008711
Douglas Gregorb98b1992009-08-11 05:31:07 +00008712template<typename Derived>
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008713TemplateName
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008714TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008715 OverloadedOperatorKind Operator,
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008716 SourceLocation NameLoc,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008717 QualType ObjectType) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008718 UnqualifiedId Name;
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008719 // FIXME: Bogus location information.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008720 SourceLocation SymbolLocations[3] = { NameLoc, NameLoc, NameLoc };
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00008721 Name.setOperatorFunctionId(NameLoc, Operator, SymbolLocations);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008722 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
Douglas Gregord6ab2322010-06-16 23:00:59 +00008723 Sema::TemplateTy Template;
8724 getSema().ActOnDependentTemplateName(/*Scope=*/0,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008725 SS, TemplateKWLoc, Name,
John McCallb3d87482010-08-24 05:47:05 +00008726 ParsedType::make(ObjectType),
Douglas Gregord6ab2322010-06-16 23:00:59 +00008727 /*EnteringContext=*/false,
8728 Template);
8729 return Template.template getAsVal<TemplateName>();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008730}
Sean Huntc3021132010-05-05 15:23:54 +00008731
Douglas Gregorca1bdd72009-11-04 00:56:37 +00008732template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008733ExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +00008734TreeTransform<Derived>::RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
8735 SourceLocation OpLoc,
John McCall9ae2f072010-08-23 23:25:46 +00008736 Expr *OrigCallee,
8737 Expr *First,
8738 Expr *Second) {
8739 Expr *Callee = OrigCallee->IgnoreParenCasts();
8740 bool isPostIncDec = Second && (Op == OO_PlusPlus || Op == OO_MinusMinus);
Mike Stump1eb44332009-09-09 15:08:12 +00008741
Douglas Gregorb98b1992009-08-11 05:31:07 +00008742 // Determine whether this should be a builtin operation.
Sebastian Redlf322ed62009-10-29 20:17:01 +00008743 if (Op == OO_Subscript) {
John McCall9ae2f072010-08-23 23:25:46 +00008744 if (!First->getType()->isOverloadableType() &&
8745 !Second->getType()->isOverloadableType())
8746 return getSema().CreateBuiltinArraySubscriptExpr(First,
8747 Callee->getLocStart(),
8748 Second, OpLoc);
Eli Friedman1a3c75f2009-11-16 19:13:03 +00008749 } else if (Op == OO_Arrow) {
8750 // -> is never a builtin operation.
John McCall9ae2f072010-08-23 23:25:46 +00008751 return SemaRef.BuildOverloadedArrowExpr(0, First, OpLoc);
8752 } else if (Second == 0 || isPostIncDec) {
8753 if (!First->getType()->isOverloadableType()) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00008754 // The argument is not of overloadable type, so try to create a
8755 // built-in unary operation.
John McCall2de56d12010-08-25 11:45:40 +00008756 UnaryOperatorKind Opc
Douglas Gregorb98b1992009-08-11 05:31:07 +00008757 = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec);
Mike Stump1eb44332009-09-09 15:08:12 +00008758
John McCall9ae2f072010-08-23 23:25:46 +00008759 return getSema().CreateBuiltinUnaryOp(OpLoc, Opc, First);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008760 }
8761 } else {
John McCall9ae2f072010-08-23 23:25:46 +00008762 if (!First->getType()->isOverloadableType() &&
8763 !Second->getType()->isOverloadableType()) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00008764 // Neither of the arguments is an overloadable type, so try to
8765 // create a built-in binary operation.
John McCall2de56d12010-08-25 11:45:40 +00008766 BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op);
John McCall60d7b3a2010-08-24 06:29:42 +00008767 ExprResult Result
John McCall9ae2f072010-08-23 23:25:46 +00008768 = SemaRef.CreateBuiltinBinOp(OpLoc, Opc, First, Second);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008769 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008770 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00008771
Douglas Gregorb98b1992009-08-11 05:31:07 +00008772 return move(Result);
8773 }
8774 }
Mike Stump1eb44332009-09-09 15:08:12 +00008775
8776 // Compute the transformed set of functions (and function templates) to be
Douglas Gregorb98b1992009-08-11 05:31:07 +00008777 // used during overload resolution.
John McCall6e266892010-01-26 03:27:55 +00008778 UnresolvedSet<16> Functions;
Mike Stump1eb44332009-09-09 15:08:12 +00008779
John McCall9ae2f072010-08-23 23:25:46 +00008780 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Callee)) {
John McCallba135432009-11-21 08:51:07 +00008781 assert(ULE->requiresADL());
8782
8783 // FIXME: Do we have to check
8784 // IsAcceptableNonMemberOperatorCandidate for each of these?
John McCall6e266892010-01-26 03:27:55 +00008785 Functions.append(ULE->decls_begin(), ULE->decls_end());
John McCallba135432009-11-21 08:51:07 +00008786 } else {
John McCall9ae2f072010-08-23 23:25:46 +00008787 Functions.addDecl(cast<DeclRefExpr>(Callee)->getDecl());
John McCallba135432009-11-21 08:51:07 +00008788 }
Mike Stump1eb44332009-09-09 15:08:12 +00008789
Douglas Gregorb98b1992009-08-11 05:31:07 +00008790 // Add any functions found via argument-dependent lookup.
John McCall9ae2f072010-08-23 23:25:46 +00008791 Expr *Args[2] = { First, Second };
8792 unsigned NumArgs = 1 + (Second != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00008793
Douglas Gregorb98b1992009-08-11 05:31:07 +00008794 // Create the overloaded operator invocation for unary operators.
8795 if (NumArgs == 1 || isPostIncDec) {
John McCall2de56d12010-08-25 11:45:40 +00008796 UnaryOperatorKind Opc
Douglas Gregorb98b1992009-08-11 05:31:07 +00008797 = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec);
John McCall9ae2f072010-08-23 23:25:46 +00008798 return SemaRef.CreateOverloadedUnaryOp(OpLoc, Opc, Functions, First);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008799 }
Mike Stump1eb44332009-09-09 15:08:12 +00008800
Douglas Gregor5b8968c2011-07-15 16:25:15 +00008801 if (Op == OO_Subscript) {
8802 SourceLocation LBrace;
8803 SourceLocation RBrace;
8804
8805 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) {
8806 DeclarationNameLoc &NameLoc = DRE->getNameInfo().getInfo();
8807 LBrace = SourceLocation::getFromRawEncoding(
8808 NameLoc.CXXOperatorName.BeginOpNameLoc);
8809 RBrace = SourceLocation::getFromRawEncoding(
8810 NameLoc.CXXOperatorName.EndOpNameLoc);
8811 } else {
8812 LBrace = Callee->getLocStart();
8813 RBrace = OpLoc;
8814 }
8815
8816 return SemaRef.CreateOverloadedArraySubscriptExpr(LBrace, RBrace,
8817 First, Second);
8818 }
Sebastian Redlf322ed62009-10-29 20:17:01 +00008819
Douglas Gregorb98b1992009-08-11 05:31:07 +00008820 // Create the overloaded operator invocation for binary operators.
John McCall2de56d12010-08-25 11:45:40 +00008821 BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op);
John McCall60d7b3a2010-08-24 06:29:42 +00008822 ExprResult Result
Douglas Gregorb98b1992009-08-11 05:31:07 +00008823 = SemaRef.CreateOverloadedBinOp(OpLoc, Opc, Functions, Args[0], Args[1]);
8824 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00008825 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00008826
Mike Stump1eb44332009-09-09 15:08:12 +00008827 return move(Result);
Douglas Gregorb98b1992009-08-11 05:31:07 +00008828}
Mike Stump1eb44332009-09-09 15:08:12 +00008829
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008830template<typename Derived>
John McCall60d7b3a2010-08-24 06:29:42 +00008831ExprResult
John McCall9ae2f072010-08-23 23:25:46 +00008832TreeTransform<Derived>::RebuildCXXPseudoDestructorExpr(Expr *Base,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008833 SourceLocation OperatorLoc,
8834 bool isArrow,
Douglas Gregorf3db29f2011-02-25 18:19:59 +00008835 CXXScopeSpec &SS,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008836 TypeSourceInfo *ScopeType,
8837 SourceLocation CCLoc,
Douglas Gregorfce46ee2010-02-24 23:50:37 +00008838 SourceLocation TildeLoc,
Douglas Gregora2e7dd22010-02-25 01:56:36 +00008839 PseudoDestructorTypeStorage Destroyed) {
John McCall9ae2f072010-08-23 23:25:46 +00008840 QualType BaseType = Base->getType();
8841 if (Base->isTypeDependent() || Destroyed.getIdentifier() ||
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008842 (!isArrow && !BaseType->getAs<RecordType>()) ||
Sean Huntc3021132010-05-05 15:23:54 +00008843 (isArrow && BaseType->getAs<PointerType>() &&
Gabor Greifbf2ca2f2010-02-25 13:04:33 +00008844 !BaseType->getAs<PointerType>()->getPointeeType()
8845 ->template getAs<RecordType>())){
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008846 // This pseudo-destructor expression is still a pseudo-destructor.
John McCall9ae2f072010-08-23 23:25:46 +00008847 return SemaRef.BuildPseudoDestructorExpr(Base, OperatorLoc,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008848 isArrow? tok::arrow : tok::period,
Douglas Gregorfce46ee2010-02-24 23:50:37 +00008849 SS, ScopeType, CCLoc, TildeLoc,
Douglas Gregora2e7dd22010-02-25 01:56:36 +00008850 Destroyed,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008851 /*FIXME?*/true);
8852 }
Abramo Bagnara25777432010-08-11 22:01:17 +00008853
Douglas Gregora2e7dd22010-02-25 01:56:36 +00008854 TypeSourceInfo *DestroyedType = Destroyed.getTypeSourceInfo();
Abramo Bagnara25777432010-08-11 22:01:17 +00008855 DeclarationName Name(SemaRef.Context.DeclarationNames.getCXXDestructorName(
8856 SemaRef.Context.getCanonicalType(DestroyedType->getType())));
8857 DeclarationNameInfo NameInfo(Name, Destroyed.getLocation());
8858 NameInfo.setNamedTypeInfo(DestroyedType);
8859
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008860 // FIXME: the ScopeType should be tacked onto SS.
Abramo Bagnara25777432010-08-11 22:01:17 +00008861
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008862 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
John McCall9ae2f072010-08-23 23:25:46 +00008863 return getSema().BuildMemberReferenceExpr(Base, BaseType,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008864 OperatorLoc, isArrow,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00008865 SS, TemplateKWLoc,
8866 /*FIXME: FirstQualifier*/ 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00008867 NameInfo,
Douglas Gregor26d4ac92010-02-24 23:40:28 +00008868 /*TemplateArgs*/ 0);
8869}
8870
Douglas Gregor577f75a2009-08-04 16:50:30 +00008871} // end namespace clang
8872
8873#endif // LLVM_CLANG_SEMA_TREETRANSFORM_H