1 //===------- TreeTransform.h - Semantic Tree Transformation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===//
9 // This file implements a semantic tree transformation that takes a given
10 // AST and rebuilds it, possibly transforming some nodes in the process.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SEMA_TREETRANSFORM_H
15 #define LLVM_CLANG_SEMA_TREETRANSFORM_H
17 #include "clang/Sema/SemaInternal.h"
18 #include "clang/Sema/Lookup.h"
19 #include "clang/Sema/ParsedTemplate.h"
20 #include "clang/Sema/SemaDiagnostic.h"
21 #include "clang/Sema/ScopeInfo.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/DeclTemplate.h"
25 #include "clang/AST/Expr.h"
26 #include "clang/AST/ExprCXX.h"
27 #include "clang/AST/ExprObjC.h"
28 #include "clang/AST/Stmt.h"
29 #include "clang/AST/StmtCXX.h"
30 #include "clang/AST/StmtObjC.h"
31 #include "clang/Sema/Ownership.h"
32 #include "clang/Sema/Designator.h"
33 #include "clang/Lex/Preprocessor.h"
34 #include "llvm/ADT/ArrayRef.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "TypeLocBuilder.h"
42 /// \brief A semantic tree transformation that allows one to transform one
43 /// abstract syntax tree into another.
45 /// 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
48 /// 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.
54 /// This tree-transformation template uses static polymorphism to allow
55 /// subclasses to customize any of its operations. Thus, a subclass can
56 /// 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.
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
70 /// Subclasses can customize the transformation at various levels. The
71 /// most coarse-grained transformations involve replacing TransformType(),
72 /// TransformExpr(), TransformDecl(), TransformNestedNameSpecifierLoc(),
73 /// TransformTemplateName(), or TransformTemplateArgument() with entirely
74 /// new implementations.
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.,
78 /// PointerType, StmtExpr) to alter the transformation. As mentioned previously,
79 /// replacing TransformTemplateTypeParmType() allows template instantiation
80 /// to substitute template arguments for their corresponding template
81 /// 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.
87 /// There are a handful of other functions that can be overridden, allowing one
88 /// to avoid traversing nodes that don't need any transformation
89 /// (\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()).
93 template<typename Derived>
95 /// \brief Private RAII object that helps us forget and then re-remember
96 /// the template argument corresponding to a partially-substituted parameter
98 class ForgetPartiallySubstitutedPackRAII {
100 TemplateArgument Old;
103 ForgetPartiallySubstitutedPackRAII(Derived &Self) : Self(Self) {
104 Old = Self.ForgetPartiallySubstitutedPack();
107 ~ForgetPartiallySubstitutedPackRAII() {
108 Self.RememberPartiallySubstitutedPack(Old);
115 /// \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;
121 /// \brief Initializes a new tree transformer.
122 TreeTransform(Sema &SemaRef) : SemaRef(SemaRef) { }
124 /// \brief Retrieves a reference to the derived class.
125 Derived &getDerived() { return static_cast<Derived&>(*this); }
127 /// \brief Retrieves a reference to the derived class.
128 const Derived &getDerived() const {
129 return static_cast<const Derived&>(*this);
132 static inline ExprResult Owned(Expr *E) { return E; }
133 static inline StmtResult Owned(Stmt *S) { return S; }
135 /// \brief Retrieves a reference to the semantic analysis object used for
136 /// this tree transform.
137 Sema &getSema() const { return SemaRef; }
139 /// \brief Whether the transformation should always rebuild AST nodes, even
140 /// if none of the children have changed.
142 /// Subclasses may override this function to specify when the transformation
143 /// should rebuild all AST nodes.
144 bool AlwaysRebuild() { return false; }
146 /// \brief Returns the location of the entity being transformed, if that
147 /// information was not available elsewhere in the AST.
149 /// By default, returns no source-location information. Subclasses can
150 /// provide an alternative implementation that provides better location
152 SourceLocation getBaseLocation() { return SourceLocation(); }
154 /// \brief Returns the name of the entity being transformed, if that
155 /// information was not available elsewhere in the AST.
157 /// By default, returns an empty name. Subclasses can provide an alternative
158 /// implementation with a more precise name.
159 DeclarationName getBaseEntity() { return DeclarationName(); }
161 /// \brief Sets the "base" location and entity when that
162 /// information is known based on another transformation.
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) { }
168 /// \brief RAII object that temporarily sets the base location and entity
169 /// used for reporting diagnostics in types.
170 class TemporaryBase {
172 SourceLocation OldLocation;
173 DeclarationName OldEntity;
176 TemporaryBase(TreeTransform &Self, SourceLocation Location,
177 DeclarationName Entity) : Self(Self) {
178 OldLocation = Self.getDerived().getBaseLocation();
179 OldEntity = Self.getDerived().getBaseEntity();
181 if (Location.isValid())
182 Self.getDerived().setBase(Location, Entity);
186 Self.getDerived().setBase(OldLocation, OldEntity);
190 /// \brief Determine whether the given type \p T has already been
193 /// Subclasses can provide an alternative implementation of this routine
194 /// to short-circuit evaluation when it is known that a given type will
195 /// not change. For example, template instantiation need not traverse
196 /// non-dependent types.
197 bool AlreadyTransformed(QualType T) {
201 /// \brief Determine whether the given call argument should be dropped, e.g.,
202 /// because it is a default argument.
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();
211 /// \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.
215 /// By default, the transformer never tries to expand pack expansions.
216 /// Subclasses can override this routine to provide different behavior.
218 /// \param EllipsisLoc The location of the ellipsis that identifies the
221 /// \param PatternRange The source range that covers the entire pattern of
222 /// the pack expansion.
224 /// \param Unexpanded The set of unexpanded parameter packs within the
227 /// \param ShouldExpand Will be set to \c true if the transformer should
228 /// expand the corresponding pack expansions into separate arguments. When
229 /// set, \c NumExpansions must also be set.
231 /// \param RetainExpansion Whether the caller should add an unexpanded
232 /// pack expansion after all of the expanded arguments. This is used
233 /// when extending explicitly-specified template argument packs per
234 /// C++0x [temp.arg.explicit]p9.
236 /// \param NumExpansions The number of separate arguments that will be in
237 /// the expanded form of the corresponding pack expansion. This is both an
238 /// input and an output parameter, which can be set by the caller if the
239 /// number of expansions is known a priori (e.g., due to a prior substitution)
240 /// and will be set by the callee when the number of expansions is known.
241 /// The callee must set this value when \c ShouldExpand is \c true; it may
242 /// set this value in other cases.
244 /// \returns true if an error occurred (e.g., because the parameter packs
245 /// are to be instantiated with arguments of different lengths), false
246 /// otherwise. If false, \c ShouldExpand (and possibly \c NumExpansions)
248 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
249 SourceRange PatternRange,
250 llvm::ArrayRef<UnexpandedParameterPack> Unexpanded,
252 bool &RetainExpansion,
253 llvm::Optional<unsigned> &NumExpansions) {
254 ShouldExpand = false;
258 /// \brief "Forget" about the partially-substituted pack template argument,
259 /// when performing an instantiation that must preserve the parameter pack
262 /// This routine is meant to be overridden by the template instantiator.
263 TemplateArgument ForgetPartiallySubstitutedPack() {
264 return TemplateArgument();
267 /// \brief "Remember" the partially-substituted pack template argument
268 /// after performing an instantiation that must preserve the parameter pack
271 /// This routine is meant to be overridden by the template instantiator.
272 void RememberPartiallySubstitutedPack(TemplateArgument Arg) { }
274 /// \brief Note to the derived class when a function parameter pack is
276 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { }
278 /// \brief Transforms the given type into another type.
280 /// By default, this routine transforms a type by creating a
281 /// TypeSourceInfo for it and delegating to the appropriate
282 /// function. This is expensive, but we don't mind, because
283 /// this method is deprecated anyway; all users should be
284 /// switched to storing TypeSourceInfos.
286 /// \returns the transformed type.
287 QualType TransformType(QualType T);
289 /// \brief Transforms the given type-with-location into a new
290 /// type-with-location.
292 /// By default, this routine transforms a type by delegating to the
293 /// appropriate TransformXXXType to build a new type. Subclasses
294 /// may override this function (to take over all type
295 /// transformations) or some set of the TransformXXXType functions
296 /// to alter the transformation.
297 TypeSourceInfo *TransformType(TypeSourceInfo *DI);
299 /// \brief Transform the given type-with-location into a new
300 /// type, collecting location information in the given builder
303 QualType TransformType(TypeLocBuilder &TLB, TypeLoc TL);
305 /// \brief Transform the given statement.
307 /// By default, this routine transforms a statement by delegating to the
308 /// appropriate TransformXXXStmt function to transform a specific kind of
309 /// statement or the TransformExpr() function to transform an expression.
310 /// Subclasses may override this function to transform statements using some
313 /// \returns the transformed statement.
314 StmtResult TransformStmt(Stmt *S);
316 /// \brief Transform the given expression.
318 /// By default, this routine transforms an expression by delegating to the
319 /// appropriate TransformXXXExpr function to build a new expression.
320 /// Subclasses may override this function to transform expressions using some
323 /// \returns the transformed expression.
324 ExprResult TransformExpr(Expr *E);
326 /// \brief Transform the given list of expressions.
328 /// This routine transforms a list of expressions by invoking
329 /// \c TransformExpr() for each subexpression. However, it also provides
330 /// support for variadic templates by expanding any pack expansions (if the
331 /// derived class permits such expansion) along the way. When pack expansions
332 /// are present, the number of outputs may not equal the number of inputs.
334 /// \param Inputs The set of expressions to be transformed.
336 /// \param NumInputs The number of expressions in \c Inputs.
338 /// \param IsCall If \c true, then this transform is being performed on
339 /// function-call arguments, and any arguments that should be dropped, will
342 /// \param Outputs The transformed input expressions will be added to this
345 /// \param ArgChanged If non-NULL, will be set \c true if any argument changed
346 /// due to transformation.
348 /// \returns true if an error occurred, false otherwise.
349 bool TransformExprs(Expr **Inputs, unsigned NumInputs, bool IsCall,
350 SmallVectorImpl<Expr *> &Outputs,
351 bool *ArgChanged = 0);
353 /// \brief Transform the given declaration, which is referenced from a type
356 /// By default, acts as the identity function on declarations, unless the
357 /// transformer has had to transform the declaration itself. Subclasses
358 /// may override this function to provide alternate behavior.
359 Decl *TransformDecl(SourceLocation Loc, Decl *D) {
360 llvm::DenseMap<Decl *, Decl *>::iterator Known
361 = TransformedLocalDecls.find(D);
362 if (Known != TransformedLocalDecls.end())
363 return Known->second;
368 /// \brief Transform the attributes associated with the given declaration and
369 /// place them on the new declaration.
371 /// By default, this operation does nothing. Subclasses may override this
372 /// behavior to transform attributes.
373 void transformAttrs(Decl *Old, Decl *New) { }
375 /// \brief Note that a local declaration has been transformed by this
378 /// Local declarations are typically transformed via a call to
379 /// TransformDefinition. However, in some cases (e.g., lambda expressions),
380 /// the transformer itself has to transform the declarations. This routine
381 /// can be overridden by a subclass that keeps track of such mappings.
382 void transformedLocalDecl(Decl *Old, Decl *New) {
383 TransformedLocalDecls[Old] = New;
386 /// \brief Transform the definition of the given declaration.
388 /// By default, invokes TransformDecl() to transform the declaration.
389 /// Subclasses may override this function to provide alternate behavior.
390 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
391 return getDerived().TransformDecl(Loc, D);
394 /// \brief Transform the given declaration, which was the first part of a
395 /// nested-name-specifier in a member access expression.
397 /// This specific declaration transformation only applies to the first
398 /// identifier in a nested-name-specifier of a member access expression, e.g.,
399 /// the \c T in \c x->T::member
401 /// By default, invokes TransformDecl() to transform the declaration.
402 /// Subclasses may override this function to provide alternate behavior.
403 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc) {
404 return cast_or_null<NamedDecl>(getDerived().TransformDecl(Loc, D));
407 /// \brief Transform the given nested-name-specifier with source-location
410 /// By default, transforms all of the types and declarations within the
411 /// nested-name-specifier. Subclasses may override this function to provide
412 /// alternate behavior.
413 NestedNameSpecifierLoc TransformNestedNameSpecifierLoc(
414 NestedNameSpecifierLoc NNS,
415 QualType ObjectType = QualType(),
416 NamedDecl *FirstQualifierInScope = 0);
418 /// \brief Transform the given declaration name.
420 /// By default, transforms the types of conversion function, constructor,
421 /// and destructor names and then (if needed) rebuilds the declaration name.
422 /// Identifiers and selectors are returned unmodified. Sublcasses may
423 /// override this function to provide alternate behavior.
425 TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
427 /// \brief Transform the given template name.
429 /// \param SS The nested-name-specifier that qualifies the template
430 /// name. This nested-name-specifier must already have been transformed.
432 /// \param Name The template name to transform.
434 /// \param NameLoc The source location of the template name.
436 /// \param ObjectType If we're translating a template name within a member
437 /// access expression, this is the type of the object whose member template
438 /// is being referenced.
440 /// \param FirstQualifierInScope If the first part of a nested-name-specifier
441 /// also refers to a name within the current (lexical) scope, this is the
442 /// declaration it refers to.
444 /// By default, transforms the template name by transforming the declarations
445 /// and nested-name-specifiers that occur within the template name.
446 /// Subclasses may override this function to provide alternate behavior.
447 TemplateName TransformTemplateName(CXXScopeSpec &SS,
449 SourceLocation NameLoc,
450 QualType ObjectType = QualType(),
451 NamedDecl *FirstQualifierInScope = 0);
453 /// \brief Transform the given template argument.
455 /// By default, this operation transforms the type, expression, or
456 /// declaration stored within the template argument and constructs a
457 /// new template argument from the transformed result. Subclasses may
458 /// override this function to provide alternate behavior.
460 /// Returns true if there was an error.
461 bool TransformTemplateArgument(const TemplateArgumentLoc &Input,
462 TemplateArgumentLoc &Output);
464 /// \brief Transform the given set of template arguments.
466 /// By default, this operation transforms all of the template arguments
467 /// in the input set using \c TransformTemplateArgument(), and appends
468 /// the transformed arguments to the output list.
470 /// Note that this overload of \c TransformTemplateArguments() is merely
471 /// a convenience function. Subclasses that wish to override this behavior
472 /// should override the iterator-based member template version.
474 /// \param Inputs The set of template arguments to be transformed.
476 /// \param NumInputs The number of template arguments in \p Inputs.
478 /// \param Outputs The set of transformed template arguments output by this
481 /// Returns true if an error occurred.
482 bool TransformTemplateArguments(const TemplateArgumentLoc *Inputs,
484 TemplateArgumentListInfo &Outputs) {
485 return TransformTemplateArguments(Inputs, Inputs + NumInputs, Outputs);
488 /// \brief Transform the given set of template arguments.
490 /// By default, this operation transforms all of the template arguments
491 /// in the input set using \c TransformTemplateArgument(), and appends
492 /// the transformed arguments to the output list.
494 /// \param First An iterator to the first template argument.
496 /// \param Last An iterator one step past the last template argument.
498 /// \param Outputs The set of transformed template arguments output by this
501 /// Returns true if an error occurred.
502 template<typename InputIterator>
503 bool TransformTemplateArguments(InputIterator First,
505 TemplateArgumentListInfo &Outputs);
507 /// \brief Fakes up a TemplateArgumentLoc for a given TemplateArgument.
508 void InventTemplateArgumentLoc(const TemplateArgument &Arg,
509 TemplateArgumentLoc &ArgLoc);
511 /// \brief Fakes up a TypeSourceInfo for a type.
512 TypeSourceInfo *InventTypeSourceInfo(QualType T) {
513 return SemaRef.Context.getTrivialTypeSourceInfo(T,
514 getDerived().getBaseLocation());
517 #define ABSTRACT_TYPELOC(CLASS, PARENT)
518 #define TYPELOC(CLASS, PARENT) \
519 QualType Transform##CLASS##Type(TypeLocBuilder &TLB, CLASS##TypeLoc T);
520 #include "clang/AST/TypeLocNodes.def"
522 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
523 FunctionProtoTypeLoc TL,
524 CXXRecordDecl *ThisContext,
525 unsigned ThisTypeQuals);
528 TransformSEHHandler(Stmt *Handler);
531 TransformTemplateSpecializationType(TypeLocBuilder &TLB,
532 TemplateSpecializationTypeLoc TL,
533 TemplateName Template);
536 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
537 DependentTemplateSpecializationTypeLoc TL,
538 TemplateName Template,
542 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
543 DependentTemplateSpecializationTypeLoc TL,
544 NestedNameSpecifierLoc QualifierLoc);
546 /// \brief Transforms the parameters of a function type into the
549 /// The result vectors should be kept in sync; null entries in the
550 /// variables vector are acceptable.
552 /// Return true on error.
553 bool TransformFunctionTypeParams(SourceLocation Loc,
554 ParmVarDecl **Params, unsigned NumParams,
555 const QualType *ParamTypes,
556 SmallVectorImpl<QualType> &PTypes,
557 SmallVectorImpl<ParmVarDecl*> *PVars);
559 /// \brief Transforms a single function-type parameter. Return null
562 /// \param indexAdjustment - A number to add to the parameter's
563 /// scope index; can be negative
564 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
566 llvm::Optional<unsigned> NumExpansions,
567 bool ExpectParameterPack);
569 QualType TransformReferenceType(TypeLocBuilder &TLB, ReferenceTypeLoc TL);
571 StmtResult TransformCompoundStmt(CompoundStmt *S, bool IsStmtExpr);
572 ExprResult TransformCXXNamedCastExpr(CXXNamedCastExpr *E);
574 /// \brief Transform the captures and body of a lambda expression.
575 ExprResult TransformLambdaScope(LambdaExpr *E, CXXMethodDecl *CallOperator);
577 ExprResult TransformAddressOfOperand(Expr *E);
578 ExprResult TransformDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E,
579 bool IsAddressOfOperand);
581 #define STMT(Node, Parent) \
582 StmtResult Transform##Node(Node *S);
583 #define EXPR(Node, Parent) \
584 ExprResult Transform##Node(Node *E);
585 #define ABSTRACT_STMT(Stmt)
586 #include "clang/AST/StmtNodes.inc"
588 /// \brief Build a new pointer type given its pointee type.
590 /// By default, performs semantic analysis when building the pointer type.
591 /// Subclasses may override this routine to provide different behavior.
592 QualType RebuildPointerType(QualType PointeeType, SourceLocation Sigil);
594 /// \brief Build a new block pointer type given its pointee type.
596 /// By default, performs semantic analysis when building the block pointer
597 /// type. Subclasses may override this routine to provide different behavior.
598 QualType RebuildBlockPointerType(QualType PointeeType, SourceLocation Sigil);
600 /// \brief Build a new reference type given the type it references.
602 /// By default, performs semantic analysis when building the
603 /// reference type. Subclasses may override this routine to provide
604 /// different behavior.
606 /// \param LValue whether the type was written with an lvalue sigil
607 /// or an rvalue sigil.
608 QualType RebuildReferenceType(QualType ReferentType,
610 SourceLocation Sigil);
612 /// \brief Build a new member pointer type given the pointee type and the
613 /// class type it refers into.
615 /// By default, performs semantic analysis when building the member pointer
616 /// type. Subclasses may override this routine to provide different behavior.
617 QualType RebuildMemberPointerType(QualType PointeeType, QualType ClassType,
618 SourceLocation Sigil);
620 /// \brief Build a new array type given the element type, size
621 /// modifier, size of the array (if known), size expression, and index type
624 /// By default, performs semantic analysis when building the array type.
625 /// Subclasses may override this routine to provide different behavior.
626 /// Also by default, all of the other Rebuild*Array
627 QualType RebuildArrayType(QualType ElementType,
628 ArrayType::ArraySizeModifier SizeMod,
629 const llvm::APInt *Size,
631 unsigned IndexTypeQuals,
632 SourceRange BracketsRange);
634 /// \brief Build a new constant array type given the element type, size
635 /// modifier, (known) size of the array, and index type qualifiers.
637 /// By default, performs semantic analysis when building the array type.
638 /// Subclasses may override this routine to provide different behavior.
639 QualType RebuildConstantArrayType(QualType ElementType,
640 ArrayType::ArraySizeModifier SizeMod,
641 const llvm::APInt &Size,
642 unsigned IndexTypeQuals,
643 SourceRange BracketsRange);
645 /// \brief Build a new incomplete array type given the element type, size
646 /// modifier, and index type qualifiers.
648 /// By default, performs semantic analysis when building the array type.
649 /// Subclasses may override this routine to provide different behavior.
650 QualType RebuildIncompleteArrayType(QualType ElementType,
651 ArrayType::ArraySizeModifier SizeMod,
652 unsigned IndexTypeQuals,
653 SourceRange BracketsRange);
655 /// \brief Build a new variable-length array type given the element type,
656 /// size modifier, size expression, and index type qualifiers.
658 /// By default, performs semantic analysis when building the array type.
659 /// Subclasses may override this routine to provide different behavior.
660 QualType RebuildVariableArrayType(QualType ElementType,
661 ArrayType::ArraySizeModifier SizeMod,
663 unsigned IndexTypeQuals,
664 SourceRange BracketsRange);
666 /// \brief Build a new dependent-sized array type given the element type,
667 /// size modifier, size expression, and index type qualifiers.
669 /// By default, performs semantic analysis when building the array type.
670 /// Subclasses may override this routine to provide different behavior.
671 QualType RebuildDependentSizedArrayType(QualType ElementType,
672 ArrayType::ArraySizeModifier SizeMod,
674 unsigned IndexTypeQuals,
675 SourceRange BracketsRange);
677 /// \brief Build a new vector type given the element type and
678 /// number of elements.
680 /// By default, performs semantic analysis when building the vector type.
681 /// Subclasses may override this routine to provide different behavior.
682 QualType RebuildVectorType(QualType ElementType, unsigned NumElements,
683 VectorType::VectorKind VecKind);
685 /// \brief Build a new extended vector type given the element type and
686 /// number of elements.
688 /// By default, performs semantic analysis when building the vector type.
689 /// Subclasses may override this routine to provide different behavior.
690 QualType RebuildExtVectorType(QualType ElementType, unsigned NumElements,
691 SourceLocation AttributeLoc);
693 /// \brief Build a new potentially dependently-sized extended vector type
694 /// given the element type and number of elements.
696 /// By default, performs semantic analysis when building the vector type.
697 /// Subclasses may override this routine to provide different behavior.
698 QualType RebuildDependentSizedExtVectorType(QualType ElementType,
700 SourceLocation AttributeLoc);
702 /// \brief Build a new function type.
704 /// By default, performs semantic analysis when building the function type.
705 /// Subclasses may override this routine to provide different behavior.
706 QualType RebuildFunctionProtoType(QualType T,
707 QualType *ParamTypes,
708 unsigned NumParamTypes,
709 bool Variadic, bool HasTrailingReturn,
711 RefQualifierKind RefQualifier,
712 const FunctionType::ExtInfo &Info);
714 /// \brief Build a new unprototyped function type.
715 QualType RebuildFunctionNoProtoType(QualType ResultType);
717 /// \brief Rebuild an unresolved typename type, given the decl that
718 /// the UnresolvedUsingTypenameDecl was transformed to.
719 QualType RebuildUnresolvedUsingType(Decl *D);
721 /// \brief Build a new typedef type.
722 QualType RebuildTypedefType(TypedefNameDecl *Typedef) {
723 return SemaRef.Context.getTypeDeclType(Typedef);
726 /// \brief Build a new class/struct/union type.
727 QualType RebuildRecordType(RecordDecl *Record) {
728 return SemaRef.Context.getTypeDeclType(Record);
731 /// \brief Build a new Enum type.
732 QualType RebuildEnumType(EnumDecl *Enum) {
733 return SemaRef.Context.getTypeDeclType(Enum);
736 /// \brief Build a new typeof(expr) type.
738 /// By default, performs semantic analysis when building the typeof type.
739 /// Subclasses may override this routine to provide different behavior.
740 QualType RebuildTypeOfExprType(Expr *Underlying, SourceLocation Loc);
742 /// \brief Build a new typeof(type) type.
744 /// By default, builds a new TypeOfType with the given underlying type.
745 QualType RebuildTypeOfType(QualType Underlying);
747 /// \brief Build a new unary transform type.
748 QualType RebuildUnaryTransformType(QualType BaseType,
749 UnaryTransformType::UTTKind UKind,
752 /// \brief Build a new C++0x decltype type.
754 /// By default, performs semantic analysis when building the decltype type.
755 /// Subclasses may override this routine to provide different behavior.
756 QualType RebuildDecltypeType(Expr *Underlying, SourceLocation Loc);
758 /// \brief Build a new C++0x auto type.
760 /// By default, builds a new AutoType with the given deduced type.
761 QualType RebuildAutoType(QualType Deduced) {
762 return SemaRef.Context.getAutoType(Deduced);
765 /// \brief Build a new template specialization type.
767 /// By default, performs semantic analysis when building the template
768 /// specialization type. Subclasses may override this routine to provide
769 /// different behavior.
770 QualType RebuildTemplateSpecializationType(TemplateName Template,
771 SourceLocation TemplateLoc,
772 TemplateArgumentListInfo &Args);
774 /// \brief Build a new parenthesized type.
776 /// By default, builds a new ParenType type from the inner type.
777 /// Subclasses may override this routine to provide different behavior.
778 QualType RebuildParenType(QualType InnerType) {
779 return SemaRef.Context.getParenType(InnerType);
782 /// \brief Build a new qualified name type.
784 /// By default, builds a new ElaboratedType type from the keyword,
785 /// the nested-name-specifier and the named type.
786 /// Subclasses may override this routine to provide different behavior.
787 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
788 ElaboratedTypeKeyword Keyword,
789 NestedNameSpecifierLoc QualifierLoc,
791 return SemaRef.Context.getElaboratedType(Keyword,
792 QualifierLoc.getNestedNameSpecifier(),
796 /// \brief Build a new typename type that refers to a template-id.
798 /// By default, builds a new DependentNameType type from the
799 /// nested-name-specifier and the given type. Subclasses may override
800 /// this routine to provide different behavior.
801 QualType RebuildDependentTemplateSpecializationType(
802 ElaboratedTypeKeyword Keyword,
803 NestedNameSpecifierLoc QualifierLoc,
804 const IdentifierInfo *Name,
805 SourceLocation NameLoc,
806 TemplateArgumentListInfo &Args) {
807 // Rebuild the template name.
808 // TODO: avoid TemplateName abstraction
810 SS.Adopt(QualifierLoc);
811 TemplateName InstName
812 = getDerived().RebuildTemplateName(SS, *Name, NameLoc, QualType(), 0);
814 if (InstName.isNull())
817 // If it's still dependent, make a dependent specialization.
818 if (InstName.getAsDependentTemplateName())
819 return SemaRef.Context.getDependentTemplateSpecializationType(Keyword,
820 QualifierLoc.getNestedNameSpecifier(),
824 // Otherwise, make an elaborated type wrapping a non-dependent
827 getDerived().RebuildTemplateSpecializationType(InstName, NameLoc, Args);
828 if (T.isNull()) return QualType();
830 if (Keyword == ETK_None && QualifierLoc.getNestedNameSpecifier() == 0)
833 return SemaRef.Context.getElaboratedType(Keyword,
834 QualifierLoc.getNestedNameSpecifier(),
838 /// \brief Build a new typename type that refers to an identifier.
840 /// By default, performs semantic analysis when building the typename type
841 /// (or elaborated type). Subclasses may override this routine to provide
842 /// different behavior.
843 QualType RebuildDependentNameType(ElaboratedTypeKeyword Keyword,
844 SourceLocation KeywordLoc,
845 NestedNameSpecifierLoc QualifierLoc,
846 const IdentifierInfo *Id,
847 SourceLocation IdLoc) {
849 SS.Adopt(QualifierLoc);
851 if (QualifierLoc.getNestedNameSpecifier()->isDependent()) {
852 // If the name is still dependent, just build a new dependent name type.
853 if (!SemaRef.computeDeclContext(SS))
854 return SemaRef.Context.getDependentNameType(Keyword,
855 QualifierLoc.getNestedNameSpecifier(),
859 if (Keyword == ETK_None || Keyword == ETK_Typename)
860 return SemaRef.CheckTypenameType(Keyword, KeywordLoc, QualifierLoc,
863 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
865 // We had a dependent elaborated-type-specifier that has been transformed
866 // into a non-dependent elaborated-type-specifier. Find the tag we're
868 LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
869 DeclContext *DC = SemaRef.computeDeclContext(SS, false);
873 if (SemaRef.RequireCompleteDeclContext(SS, DC))
877 SemaRef.LookupQualifiedName(Result, DC);
878 switch (Result.getResultKind()) {
879 case LookupResult::NotFound:
880 case LookupResult::NotFoundInCurrentInstantiation:
883 case LookupResult::Found:
884 Tag = Result.getAsSingle<TagDecl>();
887 case LookupResult::FoundOverloaded:
888 case LookupResult::FoundUnresolvedValue:
889 llvm_unreachable("Tag lookup cannot find non-tags");
891 case LookupResult::Ambiguous:
892 // Let the LookupResult structure handle ambiguities.
897 // Check where the name exists but isn't a tag type and use that to emit
898 // better diagnostics.
899 LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
900 SemaRef.LookupQualifiedName(Result, DC);
901 switch (Result.getResultKind()) {
902 case LookupResult::Found:
903 case LookupResult::FoundOverloaded:
904 case LookupResult::FoundUnresolvedValue: {
905 NamedDecl *SomeDecl = Result.getRepresentativeDecl();
907 if (isa<TypedefDecl>(SomeDecl)) Kind = 1;
908 else if (isa<TypeAliasDecl>(SomeDecl)) Kind = 2;
909 else if (isa<ClassTemplateDecl>(SomeDecl)) Kind = 3;
910 SemaRef.Diag(IdLoc, diag::err_tag_reference_non_tag) << Kind;
911 SemaRef.Diag(SomeDecl->getLocation(), diag::note_declared_at);
915 // FIXME: Would be nice to highlight just the source range.
916 SemaRef.Diag(IdLoc, diag::err_not_tag_in_scope)
923 if (!SemaRef.isAcceptableTagRedeclaration(Tag, Kind, /*isDefinition*/false,
925 SemaRef.Diag(KeywordLoc, diag::err_use_with_wrong_tag) << Id;
926 SemaRef.Diag(Tag->getLocation(), diag::note_previous_use);
930 // Build the elaborated-type-specifier type.
931 QualType T = SemaRef.Context.getTypeDeclType(Tag);
932 return SemaRef.Context.getElaboratedType(Keyword,
933 QualifierLoc.getNestedNameSpecifier(),
937 /// \brief Build a new pack expansion type.
939 /// By default, builds a new PackExpansionType type from the given pattern.
940 /// Subclasses may override this routine to provide different behavior.
941 QualType RebuildPackExpansionType(QualType Pattern,
942 SourceRange PatternRange,
943 SourceLocation EllipsisLoc,
944 llvm::Optional<unsigned> NumExpansions) {
945 return getSema().CheckPackExpansion(Pattern, PatternRange, EllipsisLoc,
949 /// \brief Build a new atomic type given its value type.
951 /// By default, performs semantic analysis when building the atomic type.
952 /// Subclasses may override this routine to provide different behavior.
953 QualType RebuildAtomicType(QualType ValueType, SourceLocation KWLoc);
955 /// \brief Build a new template name given a nested name specifier, a flag
956 /// indicating whether the "template" keyword was provided, and the template
957 /// that the template name refers to.
959 /// By default, builds the new template name directly. Subclasses may override
960 /// this routine to provide different behavior.
961 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
963 TemplateDecl *Template);
965 /// \brief Build a new template name given a nested name specifier and the
966 /// name that is referred to as a template.
968 /// By default, performs semantic analysis to determine whether the name can
969 /// be resolved to a specific template, then builds the appropriate kind of
970 /// template name. Subclasses may override this routine to provide different
972 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
973 const IdentifierInfo &Name,
974 SourceLocation NameLoc,
976 NamedDecl *FirstQualifierInScope);
978 /// \brief Build a new template name given a nested name specifier and the
979 /// overloaded operator name that is referred to as a template.
981 /// By default, performs semantic analysis to determine whether the name can
982 /// be resolved to a specific template, then builds the appropriate kind of
983 /// template name. Subclasses may override this routine to provide different
985 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
986 OverloadedOperatorKind Operator,
987 SourceLocation NameLoc,
988 QualType ObjectType);
990 /// \brief Build a new template name given a template template parameter pack
993 /// By default, performs semantic analysis to determine whether the name can
994 /// be resolved to a specific template, then builds the appropriate kind of
995 /// template name. Subclasses may override this routine to provide different
997 TemplateName RebuildTemplateName(TemplateTemplateParmDecl *Param,
998 const TemplateArgument &ArgPack) {
999 return getSema().Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
1002 /// \brief Build a new compound statement.
1004 /// By default, performs semantic analysis to build the new statement.
1005 /// Subclasses may override this routine to provide different behavior.
1006 StmtResult RebuildCompoundStmt(SourceLocation LBraceLoc,
1007 MultiStmtArg Statements,
1008 SourceLocation RBraceLoc,
1010 return getSema().ActOnCompoundStmt(LBraceLoc, RBraceLoc, Statements,
1014 /// \brief Build a new case statement.
1016 /// By default, performs semantic analysis to build the new statement.
1017 /// Subclasses may override this routine to provide different behavior.
1018 StmtResult RebuildCaseStmt(SourceLocation CaseLoc,
1020 SourceLocation EllipsisLoc,
1022 SourceLocation ColonLoc) {
1023 return getSema().ActOnCaseStmt(CaseLoc, LHS, EllipsisLoc, RHS,
1027 /// \brief Attach the body to a new case statement.
1029 /// By default, performs semantic analysis to build the new statement.
1030 /// Subclasses may override this routine to provide different behavior.
1031 StmtResult RebuildCaseStmtBody(Stmt *S, Stmt *Body) {
1032 getSema().ActOnCaseStmtBody(S, Body);
1036 /// \brief Build a new default statement.
1038 /// By default, performs semantic analysis to build the new statement.
1039 /// Subclasses may override this routine to provide different behavior.
1040 StmtResult RebuildDefaultStmt(SourceLocation DefaultLoc,
1041 SourceLocation ColonLoc,
1043 return getSema().ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt,
1047 /// \brief Build a new label statement.
1049 /// By default, performs semantic analysis to build the new statement.
1050 /// Subclasses may override this routine to provide different behavior.
1051 StmtResult RebuildLabelStmt(SourceLocation IdentLoc, LabelDecl *L,
1052 SourceLocation ColonLoc, Stmt *SubStmt) {
1053 return SemaRef.ActOnLabelStmt(IdentLoc, L, ColonLoc, SubStmt);
1056 /// \brief Build a new label statement.
1058 /// By default, performs semantic analysis to build the new statement.
1059 /// Subclasses may override this routine to provide different behavior.
1060 StmtResult RebuildAttributedStmt(SourceLocation AttrLoc,
1061 ArrayRef<const Attr*> Attrs,
1063 return SemaRef.ActOnAttributedStmt(AttrLoc, Attrs, SubStmt);
1066 /// \brief Build a new "if" statement.
1068 /// By default, performs semantic analysis to build the new statement.
1069 /// Subclasses may override this routine to provide different behavior.
1070 StmtResult RebuildIfStmt(SourceLocation IfLoc, Sema::FullExprArg Cond,
1071 VarDecl *CondVar, Stmt *Then,
1072 SourceLocation ElseLoc, Stmt *Else) {
1073 return getSema().ActOnIfStmt(IfLoc, Cond, CondVar, Then, ElseLoc, Else);
1076 /// \brief Start building a new switch statement.
1078 /// By default, performs semantic analysis to build the new statement.
1079 /// Subclasses may override this routine to provide different behavior.
1080 StmtResult RebuildSwitchStmtStart(SourceLocation SwitchLoc,
1081 Expr *Cond, VarDecl *CondVar) {
1082 return getSema().ActOnStartOfSwitchStmt(SwitchLoc, Cond,
1086 /// \brief Attach the body to the switch statement.
1088 /// By default, performs semantic analysis to build the new statement.
1089 /// Subclasses may override this routine to provide different behavior.
1090 StmtResult RebuildSwitchStmtBody(SourceLocation SwitchLoc,
1091 Stmt *Switch, Stmt *Body) {
1092 return getSema().ActOnFinishSwitchStmt(SwitchLoc, Switch, Body);
1095 /// \brief Build a new while statement.
1097 /// By default, performs semantic analysis to build the new statement.
1098 /// Subclasses may override this routine to provide different behavior.
1099 StmtResult RebuildWhileStmt(SourceLocation WhileLoc, Sema::FullExprArg Cond,
1100 VarDecl *CondVar, Stmt *Body) {
1101 return getSema().ActOnWhileStmt(WhileLoc, Cond, CondVar, Body);
1104 /// \brief Build a new do-while statement.
1106 /// By default, performs semantic analysis to build the new statement.
1107 /// Subclasses may override this routine to provide different behavior.
1108 StmtResult RebuildDoStmt(SourceLocation DoLoc, Stmt *Body,
1109 SourceLocation WhileLoc, SourceLocation LParenLoc,
1110 Expr *Cond, SourceLocation RParenLoc) {
1111 return getSema().ActOnDoStmt(DoLoc, Body, WhileLoc, LParenLoc,
1115 /// \brief Build a new for statement.
1117 /// By default, performs semantic analysis to build the new statement.
1118 /// Subclasses may override this routine to provide different behavior.
1119 StmtResult RebuildForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
1120 Stmt *Init, Sema::FullExprArg Cond,
1121 VarDecl *CondVar, Sema::FullExprArg Inc,
1122 SourceLocation RParenLoc, Stmt *Body) {
1123 return getSema().ActOnForStmt(ForLoc, LParenLoc, Init, Cond,
1124 CondVar, Inc, RParenLoc, Body);
1127 /// \brief Build a new goto statement.
1129 /// By default, performs semantic analysis to build the new statement.
1130 /// Subclasses may override this routine to provide different behavior.
1131 StmtResult RebuildGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
1133 return getSema().ActOnGotoStmt(GotoLoc, LabelLoc, Label);
1136 /// \brief Build a new indirect goto statement.
1138 /// By default, performs semantic analysis to build the new statement.
1139 /// Subclasses may override this routine to provide different behavior.
1140 StmtResult RebuildIndirectGotoStmt(SourceLocation GotoLoc,
1141 SourceLocation StarLoc,
1143 return getSema().ActOnIndirectGotoStmt(GotoLoc, StarLoc, Target);
1146 /// \brief Build a new return statement.
1148 /// By default, performs semantic analysis to build the new statement.
1149 /// Subclasses may override this routine to provide different behavior.
1150 StmtResult RebuildReturnStmt(SourceLocation ReturnLoc, Expr *Result) {
1151 return getSema().ActOnReturnStmt(ReturnLoc, Result);
1154 /// \brief Build a new declaration statement.
1156 /// By default, performs semantic analysis to build the new statement.
1157 /// Subclasses may override this routine to provide different behavior.
1158 StmtResult RebuildDeclStmt(Decl **Decls, unsigned NumDecls,
1159 SourceLocation StartLoc,
1160 SourceLocation EndLoc) {
1161 Sema::DeclGroupPtrTy DG = getSema().BuildDeclaratorGroup(Decls, NumDecls);
1162 return getSema().ActOnDeclStmt(DG, StartLoc, EndLoc);
1165 /// \brief Build a new inline asm statement.
1167 /// By default, performs semantic analysis to build the new statement.
1168 /// Subclasses may override this routine to provide different behavior.
1169 StmtResult RebuildGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
1170 bool IsVolatile, unsigned NumOutputs,
1171 unsigned NumInputs, IdentifierInfo **Names,
1172 MultiExprArg Constraints, MultiExprArg Exprs,
1173 Expr *AsmString, MultiExprArg Clobbers,
1174 SourceLocation RParenLoc) {
1175 return getSema().ActOnGCCAsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs,
1176 NumInputs, Names, Constraints, Exprs,
1177 AsmString, Clobbers, RParenLoc);
1180 /// \brief Build a new MS style inline asm statement.
1182 /// By default, performs semantic analysis to build the new statement.
1183 /// Subclasses may override this routine to provide different behavior.
1184 StmtResult RebuildMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc,
1185 ArrayRef<Token> AsmToks, SourceLocation EndLoc) {
1186 return getSema().ActOnMSAsmStmt(AsmLoc, LBraceLoc, AsmToks, EndLoc);
1189 /// \brief Build a new Objective-C \@try statement.
1191 /// By default, performs semantic analysis to build the new statement.
1192 /// Subclasses may override this routine to provide different behavior.
1193 StmtResult RebuildObjCAtTryStmt(SourceLocation AtLoc,
1195 MultiStmtArg CatchStmts,
1197 return getSema().ActOnObjCAtTryStmt(AtLoc, TryBody, CatchStmts,
1201 /// \brief Rebuild an Objective-C exception declaration.
1203 /// By default, performs semantic analysis to build the new declaration.
1204 /// Subclasses may override this routine to provide different behavior.
1205 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
1206 TypeSourceInfo *TInfo, QualType T) {
1207 return getSema().BuildObjCExceptionDecl(TInfo, T,
1208 ExceptionDecl->getInnerLocStart(),
1209 ExceptionDecl->getLocation(),
1210 ExceptionDecl->getIdentifier());
1213 /// \brief Build a new Objective-C \@catch statement.
1215 /// By default, performs semantic analysis to build the new statement.
1216 /// Subclasses may override this routine to provide different behavior.
1217 StmtResult RebuildObjCAtCatchStmt(SourceLocation AtLoc,
1218 SourceLocation RParenLoc,
1221 return getSema().ActOnObjCAtCatchStmt(AtLoc, RParenLoc,
1225 /// \brief Build a new Objective-C \@finally statement.
1227 /// By default, performs semantic analysis to build the new statement.
1228 /// Subclasses may override this routine to provide different behavior.
1229 StmtResult RebuildObjCAtFinallyStmt(SourceLocation AtLoc,
1231 return getSema().ActOnObjCAtFinallyStmt(AtLoc, Body);
1234 /// \brief Build a new Objective-C \@throw statement.
1236 /// By default, performs semantic analysis to build the new statement.
1237 /// Subclasses may override this routine to provide different behavior.
1238 StmtResult RebuildObjCAtThrowStmt(SourceLocation AtLoc,
1240 return getSema().BuildObjCAtThrowStmt(AtLoc, Operand);
1243 /// \brief Rebuild the operand to an Objective-C \@synchronized statement.
1245 /// By default, performs semantic analysis to build the new statement.
1246 /// Subclasses may override this routine to provide different behavior.
1247 ExprResult RebuildObjCAtSynchronizedOperand(SourceLocation atLoc,
1249 return getSema().ActOnObjCAtSynchronizedOperand(atLoc, object);
1252 /// \brief Build a new Objective-C \@synchronized statement.
1254 /// By default, performs semantic analysis to build the new statement.
1255 /// Subclasses may override this routine to provide different behavior.
1256 StmtResult RebuildObjCAtSynchronizedStmt(SourceLocation AtLoc,
1257 Expr *Object, Stmt *Body) {
1258 return getSema().ActOnObjCAtSynchronizedStmt(AtLoc, Object, Body);
1261 /// \brief Build a new Objective-C \@autoreleasepool statement.
1263 /// By default, performs semantic analysis to build the new statement.
1264 /// Subclasses may override this routine to provide different behavior.
1265 StmtResult RebuildObjCAutoreleasePoolStmt(SourceLocation AtLoc,
1267 return getSema().ActOnObjCAutoreleasePoolStmt(AtLoc, Body);
1270 /// \brief Build a new Objective-C fast enumeration statement.
1272 /// By default, performs semantic analysis to build the new statement.
1273 /// Subclasses may override this routine to provide different behavior.
1274 StmtResult RebuildObjCForCollectionStmt(SourceLocation ForLoc,
1277 SourceLocation RParenLoc,
1279 StmtResult ForEachStmt = getSema().ActOnObjCForCollectionStmt(ForLoc,
1283 if (ForEachStmt.isInvalid())
1286 return getSema().FinishObjCForCollectionStmt(ForEachStmt.take(), Body);
1289 /// \brief Build a new C++ exception declaration.
1291 /// By default, performs semantic analysis to build the new decaration.
1292 /// Subclasses may override this routine to provide different behavior.
1293 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
1294 TypeSourceInfo *Declarator,
1295 SourceLocation StartLoc,
1296 SourceLocation IdLoc,
1297 IdentifierInfo *Id) {
1298 VarDecl *Var = getSema().BuildExceptionDeclaration(0, Declarator,
1299 StartLoc, IdLoc, Id);
1301 getSema().CurContext->addDecl(Var);
1305 /// \brief Build a new C++ catch statement.
1307 /// By default, performs semantic analysis to build the new statement.
1308 /// Subclasses may override this routine to provide different behavior.
1309 StmtResult RebuildCXXCatchStmt(SourceLocation CatchLoc,
1310 VarDecl *ExceptionDecl,
1312 return Owned(new (getSema().Context) CXXCatchStmt(CatchLoc, ExceptionDecl,
1316 /// \brief Build a new C++ try statement.
1318 /// By default, performs semantic analysis to build the new statement.
1319 /// Subclasses may override this routine to provide different behavior.
1320 StmtResult RebuildCXXTryStmt(SourceLocation TryLoc,
1322 MultiStmtArg Handlers) {
1323 return getSema().ActOnCXXTryBlock(TryLoc, TryBlock, Handlers);
1326 /// \brief Build a new C++0x range-based for statement.
1328 /// By default, performs semantic analysis to build the new statement.
1329 /// Subclasses may override this routine to provide different behavior.
1330 StmtResult RebuildCXXForRangeStmt(SourceLocation ForLoc,
1331 SourceLocation ColonLoc,
1332 Stmt *Range, Stmt *BeginEnd,
1333 Expr *Cond, Expr *Inc,
1335 SourceLocation RParenLoc) {
1336 return getSema().BuildCXXForRangeStmt(ForLoc, ColonLoc, Range, BeginEnd,
1337 Cond, Inc, LoopVar, RParenLoc,
1338 Sema::BFRK_Rebuild);
1341 /// \brief Build a new C++0x range-based for statement.
1343 /// By default, performs semantic analysis to build the new statement.
1344 /// Subclasses may override this routine to provide different behavior.
1345 StmtResult RebuildMSDependentExistsStmt(SourceLocation KeywordLoc,
1347 NestedNameSpecifierLoc QualifierLoc,
1348 DeclarationNameInfo NameInfo,
1350 return getSema().BuildMSDependentExistsStmt(KeywordLoc, IsIfExists,
1351 QualifierLoc, NameInfo, Nested);
1354 /// \brief Attach body to a C++0x range-based for statement.
1356 /// By default, performs semantic analysis to finish the new statement.
1357 /// Subclasses may override this routine to provide different behavior.
1358 StmtResult FinishCXXForRangeStmt(Stmt *ForRange, Stmt *Body) {
1359 return getSema().FinishCXXForRangeStmt(ForRange, Body);
1362 StmtResult RebuildSEHTryStmt(bool IsCXXTry,
1363 SourceLocation TryLoc,
1366 return getSema().ActOnSEHTryBlock(IsCXXTry,TryLoc,TryBlock,Handler);
1369 StmtResult RebuildSEHExceptStmt(SourceLocation Loc,
1372 return getSema().ActOnSEHExceptBlock(Loc,FilterExpr,Block);
1375 StmtResult RebuildSEHFinallyStmt(SourceLocation Loc,
1377 return getSema().ActOnSEHFinallyBlock(Loc,Block);
1380 /// \brief Build a new expression that references a declaration.
1382 /// By default, performs semantic analysis to build the new expression.
1383 /// Subclasses may override this routine to provide different behavior.
1384 ExprResult RebuildDeclarationNameExpr(const CXXScopeSpec &SS,
1387 return getSema().BuildDeclarationNameExpr(SS, R, RequiresADL);
1391 /// \brief Build a new expression that references a declaration.
1393 /// By default, performs semantic analysis to build the new expression.
1394 /// Subclasses may override this routine to provide different behavior.
1395 ExprResult RebuildDeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
1397 const DeclarationNameInfo &NameInfo,
1398 TemplateArgumentListInfo *TemplateArgs) {
1400 SS.Adopt(QualifierLoc);
1402 // FIXME: loses template args.
1404 return getSema().BuildDeclarationNameExpr(SS, NameInfo, VD);
1407 /// \brief Build a new expression in parentheses.
1409 /// By default, performs semantic analysis to build the new expression.
1410 /// Subclasses may override this routine to provide different behavior.
1411 ExprResult RebuildParenExpr(Expr *SubExpr, SourceLocation LParen,
1412 SourceLocation RParen) {
1413 return getSema().ActOnParenExpr(LParen, RParen, SubExpr);
1416 /// \brief Build a new pseudo-destructor expression.
1418 /// By default, performs semantic analysis to build the new expression.
1419 /// Subclasses may override this routine to provide different behavior.
1420 ExprResult RebuildCXXPseudoDestructorExpr(Expr *Base,
1421 SourceLocation OperatorLoc,
1424 TypeSourceInfo *ScopeType,
1425 SourceLocation CCLoc,
1426 SourceLocation TildeLoc,
1427 PseudoDestructorTypeStorage Destroyed);
1429 /// \brief Build a new unary operator expression.
1431 /// By default, performs semantic analysis to build the new expression.
1432 /// Subclasses may override this routine to provide different behavior.
1433 ExprResult RebuildUnaryOperator(SourceLocation OpLoc,
1434 UnaryOperatorKind Opc,
1436 return getSema().BuildUnaryOp(/*Scope=*/0, OpLoc, Opc, SubExpr);
1439 /// \brief Build a new builtin offsetof expression.
1441 /// By default, performs semantic analysis to build the new expression.
1442 /// Subclasses may override this routine to provide different behavior.
1443 ExprResult RebuildOffsetOfExpr(SourceLocation OperatorLoc,
1444 TypeSourceInfo *Type,
1445 Sema::OffsetOfComponent *Components,
1446 unsigned NumComponents,
1447 SourceLocation RParenLoc) {
1448 return getSema().BuildBuiltinOffsetOf(OperatorLoc, Type, Components,
1449 NumComponents, RParenLoc);
1452 /// \brief Build a new sizeof, alignof or vec_step expression with a
1455 /// By default, performs semantic analysis to build the new expression.
1456 /// Subclasses may override this routine to provide different behavior.
1457 ExprResult RebuildUnaryExprOrTypeTrait(TypeSourceInfo *TInfo,
1458 SourceLocation OpLoc,
1459 UnaryExprOrTypeTrait ExprKind,
1461 return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
1464 /// \brief Build a new sizeof, alignof or vec step expression with an
1465 /// expression argument.
1467 /// By default, performs semantic analysis to build the new expression.
1468 /// Subclasses may override this routine to provide different behavior.
1469 ExprResult RebuildUnaryExprOrTypeTrait(Expr *SubExpr, SourceLocation OpLoc,
1470 UnaryExprOrTypeTrait ExprKind,
1473 = getSema().CreateUnaryExprOrTypeTraitExpr(SubExpr, OpLoc, ExprKind);
1474 if (Result.isInvalid())
1480 /// \brief Build a new array subscript expression.
1482 /// By default, performs semantic analysis to build the new expression.
1483 /// Subclasses may override this routine to provide different behavior.
1484 ExprResult RebuildArraySubscriptExpr(Expr *LHS,
1485 SourceLocation LBracketLoc,
1487 SourceLocation RBracketLoc) {
1488 return getSema().ActOnArraySubscriptExpr(/*Scope=*/0, LHS,
1493 /// \brief Build a new call expression.
1495 /// By default, performs semantic analysis to build the new expression.
1496 /// Subclasses may override this routine to provide different behavior.
1497 ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc,
1499 SourceLocation RParenLoc,
1500 Expr *ExecConfig = 0) {
1501 return getSema().ActOnCallExpr(/*Scope=*/0, Callee, LParenLoc,
1502 Args, RParenLoc, ExecConfig);
1505 /// \brief Build a new member access expression.
1507 /// By default, performs semantic analysis to build the new expression.
1508 /// Subclasses may override this routine to provide different behavior.
1509 ExprResult RebuildMemberExpr(Expr *Base, SourceLocation OpLoc,
1511 NestedNameSpecifierLoc QualifierLoc,
1512 SourceLocation TemplateKWLoc,
1513 const DeclarationNameInfo &MemberNameInfo,
1515 NamedDecl *FoundDecl,
1516 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1517 NamedDecl *FirstQualifierInScope) {
1518 ExprResult BaseResult = getSema().PerformMemberExprBaseConversion(Base,
1520 if (!Member->getDeclName()) {
1521 // We have a reference to an unnamed field. This is always the
1522 // base of an anonymous struct/union member access, i.e. the
1523 // field is always of record type.
1524 assert(!QualifierLoc && "Can't have an unnamed field with a qualifier!");
1525 assert(Member->getType()->isRecordType() &&
1526 "unnamed member not of record type?");
1529 getSema().PerformObjectMemberConversion(BaseResult.take(),
1530 QualifierLoc.getNestedNameSpecifier(),
1532 if (BaseResult.isInvalid())
1534 Base = BaseResult.take();
1535 ExprValueKind VK = isArrow ? VK_LValue : Base->getValueKind();
1537 new (getSema().Context) MemberExpr(Base, isArrow,
1538 Member, MemberNameInfo,
1539 cast<FieldDecl>(Member)->getType(),
1541 return getSema().Owned(ME);
1545 SS.Adopt(QualifierLoc);
1547 Base = BaseResult.take();
1548 QualType BaseType = Base->getType();
1550 // FIXME: this involves duplicating earlier analysis in a lot of
1551 // cases; we should avoid this when possible.
1552 LookupResult R(getSema(), MemberNameInfo, Sema::LookupMemberName);
1553 R.addDecl(FoundDecl);
1556 return getSema().BuildMemberReferenceExpr(Base, BaseType, OpLoc, isArrow,
1558 FirstQualifierInScope,
1559 R, ExplicitTemplateArgs);
1562 /// \brief Build a new binary operator expression.
1564 /// By default, performs semantic analysis to build the new expression.
1565 /// Subclasses may override this routine to provide different behavior.
1566 ExprResult RebuildBinaryOperator(SourceLocation OpLoc,
1567 BinaryOperatorKind Opc,
1568 Expr *LHS, Expr *RHS) {
1569 return getSema().BuildBinOp(/*Scope=*/0, OpLoc, Opc, LHS, RHS);
1572 /// \brief Build a new conditional operator expression.
1574 /// By default, performs semantic analysis to build the new expression.
1575 /// Subclasses may override this routine to provide different behavior.
1576 ExprResult RebuildConditionalOperator(Expr *Cond,
1577 SourceLocation QuestionLoc,
1579 SourceLocation ColonLoc,
1581 return getSema().ActOnConditionalOp(QuestionLoc, ColonLoc, Cond,
1585 /// \brief Build a new C-style cast expression.
1587 /// By default, performs semantic analysis to build the new expression.
1588 /// Subclasses may override this routine to provide different behavior.
1589 ExprResult RebuildCStyleCastExpr(SourceLocation LParenLoc,
1590 TypeSourceInfo *TInfo,
1591 SourceLocation RParenLoc,
1593 return getSema().BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc,
1597 /// \brief Build a new compound literal expression.
1599 /// By default, performs semantic analysis to build the new expression.
1600 /// Subclasses may override this routine to provide different behavior.
1601 ExprResult RebuildCompoundLiteralExpr(SourceLocation LParenLoc,
1602 TypeSourceInfo *TInfo,
1603 SourceLocation RParenLoc,
1605 return getSema().BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc,
1609 /// \brief Build a new extended vector element access expression.
1611 /// By default, performs semantic analysis to build the new expression.
1612 /// Subclasses may override this routine to provide different behavior.
1613 ExprResult RebuildExtVectorElementExpr(Expr *Base,
1614 SourceLocation OpLoc,
1615 SourceLocation AccessorLoc,
1616 IdentifierInfo &Accessor) {
1619 DeclarationNameInfo NameInfo(&Accessor, AccessorLoc);
1620 return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
1621 OpLoc, /*IsArrow*/ false,
1622 SS, SourceLocation(),
1623 /*FirstQualifierInScope*/ 0,
1625 /* TemplateArgs */ 0);
1628 /// \brief Build a new initializer list expression.
1630 /// By default, performs semantic analysis to build the new expression.
1631 /// Subclasses may override this routine to provide different behavior.
1632 ExprResult RebuildInitList(SourceLocation LBraceLoc,
1634 SourceLocation RBraceLoc,
1635 QualType ResultTy) {
1637 = SemaRef.ActOnInitList(LBraceLoc, Inits, RBraceLoc);
1638 if (Result.isInvalid() || ResultTy->isDependentType())
1641 // Patch in the result type we were given, which may have been computed
1642 // when the initial InitListExpr was built.
1643 InitListExpr *ILE = cast<InitListExpr>((Expr *)Result.get());
1644 ILE->setType(ResultTy);
1648 /// \brief Build a new designated initializer expression.
1650 /// By default, performs semantic analysis to build the new expression.
1651 /// Subclasses may override this routine to provide different behavior.
1652 ExprResult RebuildDesignatedInitExpr(Designation &Desig,
1653 MultiExprArg ArrayExprs,
1654 SourceLocation EqualOrColonLoc,
1658 = SemaRef.ActOnDesignatedInitializer(Desig, EqualOrColonLoc, GNUSyntax,
1660 if (Result.isInvalid())
1666 /// \brief Build a new value-initialized expression.
1668 /// By default, builds the implicit value initialization without performing
1669 /// any semantic analysis. Subclasses may override this routine to provide
1670 /// different behavior.
1671 ExprResult RebuildImplicitValueInitExpr(QualType T) {
1672 return SemaRef.Owned(new (SemaRef.Context) ImplicitValueInitExpr(T));
1675 /// \brief Build a new \c va_arg expression.
1677 /// By default, performs semantic analysis to build the new expression.
1678 /// Subclasses may override this routine to provide different behavior.
1679 ExprResult RebuildVAArgExpr(SourceLocation BuiltinLoc,
1680 Expr *SubExpr, TypeSourceInfo *TInfo,
1681 SourceLocation RParenLoc) {
1682 return getSema().BuildVAArgExpr(BuiltinLoc,
1687 /// \brief Build a new expression list in parentheses.
1689 /// By default, performs semantic analysis to build the new expression.
1690 /// Subclasses may override this routine to provide different behavior.
1691 ExprResult RebuildParenListExpr(SourceLocation LParenLoc,
1692 MultiExprArg SubExprs,
1693 SourceLocation RParenLoc) {
1694 return getSema().ActOnParenListExpr(LParenLoc, RParenLoc, SubExprs);
1697 /// \brief Build a new address-of-label expression.
1699 /// By default, performs semantic analysis, using the name of the label
1700 /// rather than attempting to map the label statement itself.
1701 /// Subclasses may override this routine to provide different behavior.
1702 ExprResult RebuildAddrLabelExpr(SourceLocation AmpAmpLoc,
1703 SourceLocation LabelLoc, LabelDecl *Label) {
1704 return getSema().ActOnAddrLabel(AmpAmpLoc, LabelLoc, Label);
1707 /// \brief Build a new GNU statement expression.
1709 /// By default, performs semantic analysis to build the new expression.
1710 /// Subclasses may override this routine to provide different behavior.
1711 ExprResult RebuildStmtExpr(SourceLocation LParenLoc,
1713 SourceLocation RParenLoc) {
1714 return getSema().ActOnStmtExpr(LParenLoc, SubStmt, RParenLoc);
1717 /// \brief Build a new __builtin_choose_expr expression.
1719 /// By default, performs semantic analysis to build the new expression.
1720 /// Subclasses may override this routine to provide different behavior.
1721 ExprResult RebuildChooseExpr(SourceLocation BuiltinLoc,
1722 Expr *Cond, Expr *LHS, Expr *RHS,
1723 SourceLocation RParenLoc) {
1724 return SemaRef.ActOnChooseExpr(BuiltinLoc,
1729 /// \brief Build a new generic selection expression.
1731 /// By default, performs semantic analysis to build the new expression.
1732 /// Subclasses may override this routine to provide different behavior.
1733 ExprResult RebuildGenericSelectionExpr(SourceLocation KeyLoc,
1734 SourceLocation DefaultLoc,
1735 SourceLocation RParenLoc,
1736 Expr *ControllingExpr,
1737 TypeSourceInfo **Types,
1739 unsigned NumAssocs) {
1740 return getSema().CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc,
1741 ControllingExpr, Types, Exprs,
1745 /// \brief Build a new overloaded operator call expression.
1747 /// By default, performs semantic analysis to build the new expression.
1748 /// The semantic analysis provides the behavior of template instantiation,
1749 /// copying with transformations that turn what looks like an overloaded
1750 /// operator call into a use of a builtin operator, performing
1751 /// argument-dependent lookup, etc. Subclasses may override this routine to
1752 /// provide different behavior.
1753 ExprResult RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
1754 SourceLocation OpLoc,
1759 /// \brief Build a new C++ "named" cast expression, such as static_cast or
1760 /// reinterpret_cast.
1762 /// By default, this routine dispatches to one of the more-specific routines
1763 /// for a particular named case, e.g., RebuildCXXStaticCastExpr().
1764 /// Subclasses may override this routine to provide different behavior.
1765 ExprResult RebuildCXXNamedCastExpr(SourceLocation OpLoc,
1766 Stmt::StmtClass Class,
1767 SourceLocation LAngleLoc,
1768 TypeSourceInfo *TInfo,
1769 SourceLocation RAngleLoc,
1770 SourceLocation LParenLoc,
1772 SourceLocation RParenLoc) {
1774 case Stmt::CXXStaticCastExprClass:
1775 return getDerived().RebuildCXXStaticCastExpr(OpLoc, LAngleLoc, TInfo,
1776 RAngleLoc, LParenLoc,
1777 SubExpr, RParenLoc);
1779 case Stmt::CXXDynamicCastExprClass:
1780 return getDerived().RebuildCXXDynamicCastExpr(OpLoc, LAngleLoc, TInfo,
1781 RAngleLoc, LParenLoc,
1782 SubExpr, RParenLoc);
1784 case Stmt::CXXReinterpretCastExprClass:
1785 return getDerived().RebuildCXXReinterpretCastExpr(OpLoc, LAngleLoc, TInfo,
1786 RAngleLoc, LParenLoc,
1790 case Stmt::CXXConstCastExprClass:
1791 return getDerived().RebuildCXXConstCastExpr(OpLoc, LAngleLoc, TInfo,
1792 RAngleLoc, LParenLoc,
1793 SubExpr, RParenLoc);
1796 llvm_unreachable("Invalid C++ named cast");
1800 /// \brief Build a new C++ static_cast expression.
1802 /// By default, performs semantic analysis to build the new expression.
1803 /// Subclasses may override this routine to provide different behavior.
1804 ExprResult RebuildCXXStaticCastExpr(SourceLocation OpLoc,
1805 SourceLocation LAngleLoc,
1806 TypeSourceInfo *TInfo,
1807 SourceLocation RAngleLoc,
1808 SourceLocation LParenLoc,
1810 SourceLocation RParenLoc) {
1811 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_static_cast,
1813 SourceRange(LAngleLoc, RAngleLoc),
1814 SourceRange(LParenLoc, RParenLoc));
1817 /// \brief Build a new C++ dynamic_cast expression.
1819 /// By default, performs semantic analysis to build the new expression.
1820 /// Subclasses may override this routine to provide different behavior.
1821 ExprResult RebuildCXXDynamicCastExpr(SourceLocation OpLoc,
1822 SourceLocation LAngleLoc,
1823 TypeSourceInfo *TInfo,
1824 SourceLocation RAngleLoc,
1825 SourceLocation LParenLoc,
1827 SourceLocation RParenLoc) {
1828 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_dynamic_cast,
1830 SourceRange(LAngleLoc, RAngleLoc),
1831 SourceRange(LParenLoc, RParenLoc));
1834 /// \brief Build a new C++ reinterpret_cast expression.
1836 /// By default, performs semantic analysis to build the new expression.
1837 /// Subclasses may override this routine to provide different behavior.
1838 ExprResult RebuildCXXReinterpretCastExpr(SourceLocation OpLoc,
1839 SourceLocation LAngleLoc,
1840 TypeSourceInfo *TInfo,
1841 SourceLocation RAngleLoc,
1842 SourceLocation LParenLoc,
1844 SourceLocation RParenLoc) {
1845 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_reinterpret_cast,
1847 SourceRange(LAngleLoc, RAngleLoc),
1848 SourceRange(LParenLoc, RParenLoc));
1851 /// \brief Build a new C++ const_cast expression.
1853 /// By default, performs semantic analysis to build the new expression.
1854 /// Subclasses may override this routine to provide different behavior.
1855 ExprResult RebuildCXXConstCastExpr(SourceLocation OpLoc,
1856 SourceLocation LAngleLoc,
1857 TypeSourceInfo *TInfo,
1858 SourceLocation RAngleLoc,
1859 SourceLocation LParenLoc,
1861 SourceLocation RParenLoc) {
1862 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_const_cast,
1864 SourceRange(LAngleLoc, RAngleLoc),
1865 SourceRange(LParenLoc, RParenLoc));
1868 /// \brief Build a new C++ functional-style cast expression.
1870 /// By default, performs semantic analysis to build the new expression.
1871 /// Subclasses may override this routine to provide different behavior.
1872 ExprResult RebuildCXXFunctionalCastExpr(TypeSourceInfo *TInfo,
1873 SourceLocation LParenLoc,
1875 SourceLocation RParenLoc) {
1876 return getSema().BuildCXXTypeConstructExpr(TInfo, LParenLoc,
1877 MultiExprArg(&Sub, 1),
1881 /// \brief Build a new C++ typeid(type) expression.
1883 /// By default, performs semantic analysis to build the new expression.
1884 /// Subclasses may override this routine to provide different behavior.
1885 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
1886 SourceLocation TypeidLoc,
1887 TypeSourceInfo *Operand,
1888 SourceLocation RParenLoc) {
1889 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
1894 /// \brief Build a new C++ typeid(expr) expression.
1896 /// By default, performs semantic analysis to build the new expression.
1897 /// Subclasses may override this routine to provide different behavior.
1898 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
1899 SourceLocation TypeidLoc,
1901 SourceLocation RParenLoc) {
1902 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
1906 /// \brief Build a new C++ __uuidof(type) expression.
1908 /// By default, performs semantic analysis to build the new expression.
1909 /// Subclasses may override this routine to provide different behavior.
1910 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1911 SourceLocation TypeidLoc,
1912 TypeSourceInfo *Operand,
1913 SourceLocation RParenLoc) {
1914 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1918 /// \brief Build a new C++ __uuidof(expr) expression.
1920 /// By default, performs semantic analysis to build the new expression.
1921 /// Subclasses may override this routine to provide different behavior.
1922 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1923 SourceLocation TypeidLoc,
1925 SourceLocation RParenLoc) {
1926 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1930 /// \brief Build a new C++ "this" expression.
1932 /// By default, builds a new "this" expression without performing any
1933 /// semantic analysis. Subclasses may override this routine to provide
1934 /// different behavior.
1935 ExprResult RebuildCXXThisExpr(SourceLocation ThisLoc,
1938 getSema().CheckCXXThisCapture(ThisLoc);
1939 return getSema().Owned(
1940 new (getSema().Context) CXXThisExpr(ThisLoc, ThisType,
1944 /// \brief Build a new C++ throw expression.
1946 /// By default, performs semantic analysis to build the new expression.
1947 /// Subclasses may override this routine to provide different behavior.
1948 ExprResult RebuildCXXThrowExpr(SourceLocation ThrowLoc, Expr *Sub,
1949 bool IsThrownVariableInScope) {
1950 return getSema().BuildCXXThrow(ThrowLoc, Sub, IsThrownVariableInScope);
1953 /// \brief Build a new C++ default-argument expression.
1955 /// By default, builds a new default-argument expression, which does not
1956 /// require any semantic analysis. Subclasses may override this routine to
1957 /// provide different behavior.
1958 ExprResult RebuildCXXDefaultArgExpr(SourceLocation Loc,
1959 ParmVarDecl *Param) {
1960 return getSema().Owned(CXXDefaultArgExpr::Create(getSema().Context, Loc,
1964 /// \brief Build a new C++ zero-initialization expression.
1966 /// By default, performs semantic analysis to build the new expression.
1967 /// Subclasses may override this routine to provide different behavior.
1968 ExprResult RebuildCXXScalarValueInitExpr(TypeSourceInfo *TSInfo,
1969 SourceLocation LParenLoc,
1970 SourceLocation RParenLoc) {
1971 return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc,
1972 MultiExprArg(), RParenLoc);
1975 /// \brief Build a new C++ "new" expression.
1977 /// By default, performs semantic analysis to build the new expression.
1978 /// Subclasses may override this routine to provide different behavior.
1979 ExprResult RebuildCXXNewExpr(SourceLocation StartLoc,
1981 SourceLocation PlacementLParen,
1982 MultiExprArg PlacementArgs,
1983 SourceLocation PlacementRParen,
1984 SourceRange TypeIdParens,
1985 QualType AllocatedType,
1986 TypeSourceInfo *AllocatedTypeInfo,
1988 SourceRange DirectInitRange,
1989 Expr *Initializer) {
1990 return getSema().BuildCXXNew(StartLoc, UseGlobal,
2002 /// \brief Build a new C++ "delete" expression.
2004 /// By default, performs semantic analysis to build the new expression.
2005 /// Subclasses may override this routine to provide different behavior.
2006 ExprResult RebuildCXXDeleteExpr(SourceLocation StartLoc,
2007 bool IsGlobalDelete,
2010 return getSema().ActOnCXXDelete(StartLoc, IsGlobalDelete, IsArrayForm,
2014 /// \brief Build a new unary type trait expression.
2016 /// By default, performs semantic analysis to build the new expression.
2017 /// Subclasses may override this routine to provide different behavior.
2018 ExprResult RebuildUnaryTypeTrait(UnaryTypeTrait Trait,
2019 SourceLocation StartLoc,
2021 SourceLocation RParenLoc) {
2022 return getSema().BuildUnaryTypeTrait(Trait, StartLoc, T, RParenLoc);
2025 /// \brief Build a new binary type trait expression.
2027 /// By default, performs semantic analysis to build the new expression.
2028 /// Subclasses may override this routine to provide different behavior.
2029 ExprResult RebuildBinaryTypeTrait(BinaryTypeTrait Trait,
2030 SourceLocation StartLoc,
2031 TypeSourceInfo *LhsT,
2032 TypeSourceInfo *RhsT,
2033 SourceLocation RParenLoc) {
2034 return getSema().BuildBinaryTypeTrait(Trait, StartLoc, LhsT, RhsT, RParenLoc);
2037 /// \brief Build a new type trait expression.
2039 /// By default, performs semantic analysis to build the new expression.
2040 /// Subclasses may override this routine to provide different behavior.
2041 ExprResult RebuildTypeTrait(TypeTrait Trait,
2042 SourceLocation StartLoc,
2043 ArrayRef<TypeSourceInfo *> Args,
2044 SourceLocation RParenLoc) {
2045 return getSema().BuildTypeTrait(Trait, StartLoc, Args, RParenLoc);
2048 /// \brief Build a new array type trait expression.
2050 /// By default, performs semantic analysis to build the new expression.
2051 /// Subclasses may override this routine to provide different behavior.
2052 ExprResult RebuildArrayTypeTrait(ArrayTypeTrait Trait,
2053 SourceLocation StartLoc,
2054 TypeSourceInfo *TSInfo,
2056 SourceLocation RParenLoc) {
2057 return getSema().BuildArrayTypeTrait(Trait, StartLoc, TSInfo, DimExpr, RParenLoc);
2060 /// \brief Build a new expression trait expression.
2062 /// By default, performs semantic analysis to build the new expression.
2063 /// Subclasses may override this routine to provide different behavior.
2064 ExprResult RebuildExpressionTrait(ExpressionTrait Trait,
2065 SourceLocation StartLoc,
2067 SourceLocation RParenLoc) {
2068 return getSema().BuildExpressionTrait(Trait, StartLoc, Queried, RParenLoc);
2071 /// \brief Build a new (previously unresolved) declaration reference
2074 /// By default, performs semantic analysis to build the new expression.
2075 /// Subclasses may override this routine to provide different behavior.
2076 ExprResult RebuildDependentScopeDeclRefExpr(
2077 NestedNameSpecifierLoc QualifierLoc,
2078 SourceLocation TemplateKWLoc,
2079 const DeclarationNameInfo &NameInfo,
2080 const TemplateArgumentListInfo *TemplateArgs,
2081 bool IsAddressOfOperand) {
2083 SS.Adopt(QualifierLoc);
2085 if (TemplateArgs || TemplateKWLoc.isValid())
2086 return getSema().BuildQualifiedTemplateIdExpr(SS, TemplateKWLoc,
2087 NameInfo, TemplateArgs);
2089 return getSema().BuildQualifiedDeclarationNameExpr(SS, NameInfo,
2090 IsAddressOfOperand);
2093 /// \brief Build a new template-id expression.
2095 /// By default, performs semantic analysis to build the new expression.
2096 /// Subclasses may override this routine to provide different behavior.
2097 ExprResult RebuildTemplateIdExpr(const CXXScopeSpec &SS,
2098 SourceLocation TemplateKWLoc,
2101 const TemplateArgumentListInfo *TemplateArgs) {
2102 return getSema().BuildTemplateIdExpr(SS, TemplateKWLoc, R, RequiresADL,
2106 /// \brief Build a new object-construction expression.
2108 /// By default, performs semantic analysis to build the new expression.
2109 /// Subclasses may override this routine to provide different behavior.
2110 ExprResult RebuildCXXConstructExpr(QualType T,
2112 CXXConstructorDecl *Constructor,
2115 bool HadMultipleCandidates,
2116 bool RequiresZeroInit,
2117 CXXConstructExpr::ConstructionKind ConstructKind,
2118 SourceRange ParenRange) {
2119 SmallVector<Expr*, 8> ConvertedArgs;
2120 if (getSema().CompleteConstructorCall(Constructor, Args, Loc,
2124 return getSema().BuildCXXConstructExpr(Loc, T, Constructor, IsElidable,
2126 HadMultipleCandidates,
2127 RequiresZeroInit, ConstructKind,
2131 /// \brief Build a new object-construction expression.
2133 /// By default, performs semantic analysis to build the new expression.
2134 /// Subclasses may override this routine to provide different behavior.
2135 ExprResult RebuildCXXTemporaryObjectExpr(TypeSourceInfo *TSInfo,
2136 SourceLocation LParenLoc,
2138 SourceLocation RParenLoc) {
2139 return getSema().BuildCXXTypeConstructExpr(TSInfo,
2145 /// \brief Build a new object-construction expression.
2147 /// By default, performs semantic analysis to build the new expression.
2148 /// Subclasses may override this routine to provide different behavior.
2149 ExprResult RebuildCXXUnresolvedConstructExpr(TypeSourceInfo *TSInfo,
2150 SourceLocation LParenLoc,
2152 SourceLocation RParenLoc) {
2153 return getSema().BuildCXXTypeConstructExpr(TSInfo,
2159 /// \brief Build a new member reference expression.
2161 /// By default, performs semantic analysis to build the new expression.
2162 /// Subclasses may override this routine to provide different behavior.
2163 ExprResult RebuildCXXDependentScopeMemberExpr(Expr *BaseE,
2166 SourceLocation OperatorLoc,
2167 NestedNameSpecifierLoc QualifierLoc,
2168 SourceLocation TemplateKWLoc,
2169 NamedDecl *FirstQualifierInScope,
2170 const DeclarationNameInfo &MemberNameInfo,
2171 const TemplateArgumentListInfo *TemplateArgs) {
2173 SS.Adopt(QualifierLoc);
2175 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
2176 OperatorLoc, IsArrow,
2178 FirstQualifierInScope,
2183 /// \brief Build a new member reference expression.
2185 /// By default, performs semantic analysis to build the new expression.
2186 /// Subclasses may override this routine to provide different behavior.
2187 ExprResult RebuildUnresolvedMemberExpr(Expr *BaseE, QualType BaseType,
2188 SourceLocation OperatorLoc,
2190 NestedNameSpecifierLoc QualifierLoc,
2191 SourceLocation TemplateKWLoc,
2192 NamedDecl *FirstQualifierInScope,
2194 const TemplateArgumentListInfo *TemplateArgs) {
2196 SS.Adopt(QualifierLoc);
2198 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
2199 OperatorLoc, IsArrow,
2201 FirstQualifierInScope,
2205 /// \brief Build a new noexcept expression.
2207 /// By default, performs semantic analysis to build the new expression.
2208 /// Subclasses may override this routine to provide different behavior.
2209 ExprResult RebuildCXXNoexceptExpr(SourceRange Range, Expr *Arg) {
2210 return SemaRef.BuildCXXNoexceptExpr(Range.getBegin(), Arg, Range.getEnd());
2213 /// \brief Build a new expression to compute the length of a parameter pack.
2214 ExprResult RebuildSizeOfPackExpr(SourceLocation OperatorLoc, NamedDecl *Pack,
2215 SourceLocation PackLoc,
2216 SourceLocation RParenLoc,
2217 llvm::Optional<unsigned> Length) {
2219 return new (SemaRef.Context) SizeOfPackExpr(SemaRef.Context.getSizeType(),
2220 OperatorLoc, Pack, PackLoc,
2221 RParenLoc, *Length);
2223 return new (SemaRef.Context) SizeOfPackExpr(SemaRef.Context.getSizeType(),
2224 OperatorLoc, Pack, PackLoc,
2228 /// \brief Build a new Objective-C boxed expression.
2230 /// By default, performs semantic analysis to build the new expression.
2231 /// Subclasses may override this routine to provide different behavior.
2232 ExprResult RebuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) {
2233 return getSema().BuildObjCBoxedExpr(SR, ValueExpr);
2236 /// \brief Build a new Objective-C array literal.
2238 /// By default, performs semantic analysis to build the new expression.
2239 /// Subclasses may override this routine to provide different behavior.
2240 ExprResult RebuildObjCArrayLiteral(SourceRange Range,
2241 Expr **Elements, unsigned NumElements) {
2242 return getSema().BuildObjCArrayLiteral(Range,
2243 MultiExprArg(Elements, NumElements));
2246 ExprResult RebuildObjCSubscriptRefExpr(SourceLocation RB,
2247 Expr *Base, Expr *Key,
2248 ObjCMethodDecl *getterMethod,
2249 ObjCMethodDecl *setterMethod) {
2250 return getSema().BuildObjCSubscriptExpression(RB, Base, Key,
2251 getterMethod, setterMethod);
2254 /// \brief Build a new Objective-C dictionary literal.
2256 /// By default, performs semantic analysis to build the new expression.
2257 /// Subclasses may override this routine to provide different behavior.
2258 ExprResult RebuildObjCDictionaryLiteral(SourceRange Range,
2259 ObjCDictionaryElement *Elements,
2260 unsigned NumElements) {
2261 return getSema().BuildObjCDictionaryLiteral(Range, Elements, NumElements);
2264 /// \brief Build a new Objective-C \@encode expression.
2266 /// By default, performs semantic analysis to build the new expression.
2267 /// Subclasses may override this routine to provide different behavior.
2268 ExprResult RebuildObjCEncodeExpr(SourceLocation AtLoc,
2269 TypeSourceInfo *EncodeTypeInfo,
2270 SourceLocation RParenLoc) {
2271 return SemaRef.Owned(SemaRef.BuildObjCEncodeExpression(AtLoc, EncodeTypeInfo,
2275 /// \brief Build a new Objective-C class message.
2276 ExprResult RebuildObjCMessageExpr(TypeSourceInfo *ReceiverTypeInfo,
2278 ArrayRef<SourceLocation> SelectorLocs,
2279 ObjCMethodDecl *Method,
2280 SourceLocation LBracLoc,
2282 SourceLocation RBracLoc) {
2283 return SemaRef.BuildClassMessage(ReceiverTypeInfo,
2284 ReceiverTypeInfo->getType(),
2285 /*SuperLoc=*/SourceLocation(),
2286 Sel, Method, LBracLoc, SelectorLocs,
2290 /// \brief Build a new Objective-C instance message.
2291 ExprResult RebuildObjCMessageExpr(Expr *Receiver,
2293 ArrayRef<SourceLocation> SelectorLocs,
2294 ObjCMethodDecl *Method,
2295 SourceLocation LBracLoc,
2297 SourceLocation RBracLoc) {
2298 return SemaRef.BuildInstanceMessage(Receiver,
2299 Receiver->getType(),
2300 /*SuperLoc=*/SourceLocation(),
2301 Sel, Method, LBracLoc, SelectorLocs,
2305 /// \brief Build a new Objective-C ivar reference expression.
2307 /// By default, performs semantic analysis to build the new expression.
2308 /// Subclasses may override this routine to provide different behavior.
2309 ExprResult RebuildObjCIvarRefExpr(Expr *BaseArg, ObjCIvarDecl *Ivar,
2310 SourceLocation IvarLoc,
2311 bool IsArrow, bool IsFreeIvar) {
2312 // FIXME: We lose track of the IsFreeIvar bit.
2314 ExprResult Base = getSema().Owned(BaseArg);
2315 LookupResult R(getSema(), Ivar->getDeclName(), IvarLoc,
2316 Sema::LookupMemberName);
2317 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
2321 if (Result.isInvalid() || Base.isInvalid())
2327 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
2328 /*FIXME:*/IvarLoc, IsArrow,
2329 SS, SourceLocation(),
2330 /*FirstQualifierInScope=*/0,
2332 /*TemplateArgs=*/0);
2335 /// \brief Build a new Objective-C property reference expression.
2337 /// By default, performs semantic analysis to build the new expression.
2338 /// Subclasses may override this routine to provide different behavior.
2339 ExprResult RebuildObjCPropertyRefExpr(Expr *BaseArg,
2340 ObjCPropertyDecl *Property,
2341 SourceLocation PropertyLoc) {
2343 ExprResult Base = getSema().Owned(BaseArg);
2344 LookupResult R(getSema(), Property->getDeclName(), PropertyLoc,
2345 Sema::LookupMemberName);
2346 bool IsArrow = false;
2347 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
2348 /*FIME:*/PropertyLoc,
2350 if (Result.isInvalid() || Base.isInvalid())
2356 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
2357 /*FIXME:*/PropertyLoc, IsArrow,
2358 SS, SourceLocation(),
2359 /*FirstQualifierInScope=*/0,
2361 /*TemplateArgs=*/0);
2364 /// \brief Build a new Objective-C property reference expression.
2366 /// By default, performs semantic analysis to build the new expression.
2367 /// Subclasses may override this routine to provide different behavior.
2368 ExprResult RebuildObjCPropertyRefExpr(Expr *Base, QualType T,
2369 ObjCMethodDecl *Getter,
2370 ObjCMethodDecl *Setter,
2371 SourceLocation PropertyLoc) {
2372 // Since these expressions can only be value-dependent, we do not
2373 // need to perform semantic analysis again.
2375 new (getSema().Context) ObjCPropertyRefExpr(Getter, Setter, T,
2376 VK_LValue, OK_ObjCProperty,
2377 PropertyLoc, Base));
2380 /// \brief Build a new Objective-C "isa" expression.
2382 /// By default, performs semantic analysis to build the new expression.
2383 /// Subclasses may override this routine to provide different behavior.
2384 ExprResult RebuildObjCIsaExpr(Expr *BaseArg, SourceLocation IsaLoc,
2387 ExprResult Base = getSema().Owned(BaseArg);
2388 LookupResult R(getSema(), &getSema().Context.Idents.get("isa"), IsaLoc,
2389 Sema::LookupMemberName);
2390 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
2393 if (Result.isInvalid() || Base.isInvalid())
2399 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
2400 /*FIXME:*/IsaLoc, IsArrow,
2401 SS, SourceLocation(),
2402 /*FirstQualifierInScope=*/0,
2404 /*TemplateArgs=*/0);
2407 /// \brief Build a new shuffle vector expression.
2409 /// By default, performs semantic analysis to build the new expression.
2410 /// Subclasses may override this routine to provide different behavior.
2411 ExprResult RebuildShuffleVectorExpr(SourceLocation BuiltinLoc,
2412 MultiExprArg SubExprs,
2413 SourceLocation RParenLoc) {
2414 // Find the declaration for __builtin_shufflevector
2415 const IdentifierInfo &Name
2416 = SemaRef.Context.Idents.get("__builtin_shufflevector");
2417 TranslationUnitDecl *TUDecl = SemaRef.Context.getTranslationUnitDecl();
2418 DeclContext::lookup_result Lookup = TUDecl->lookup(DeclarationName(&Name));
2419 assert(Lookup.first != Lookup.second && "No __builtin_shufflevector?");
2421 // Build a reference to the __builtin_shufflevector builtin
2422 FunctionDecl *Builtin = cast<FunctionDecl>(*Lookup.first);
2423 Expr *Callee = new (SemaRef.Context) DeclRefExpr(Builtin, false,
2424 SemaRef.Context.BuiltinFnTy,
2425 VK_RValue, BuiltinLoc);
2426 QualType CalleePtrTy = SemaRef.Context.getPointerType(Builtin->getType());
2427 Callee = SemaRef.ImpCastExprToType(Callee, CalleePtrTy,
2428 CK_BuiltinFnToFnPtr).take();
2430 // Build the CallExpr
2431 ExprResult TheCall = SemaRef.Owned(
2432 new (SemaRef.Context) CallExpr(SemaRef.Context, Callee, SubExprs,
2433 Builtin->getCallResultType(),
2434 Expr::getValueKindForType(Builtin->getResultType()),
2437 // Type-check the __builtin_shufflevector expression.
2438 return SemaRef.SemaBuiltinShuffleVector(cast<CallExpr>(TheCall.take()));
2441 /// \brief Build a new template argument pack expansion.
2443 /// By default, performs semantic analysis to build a new pack expansion
2444 /// for a template argument. Subclasses may override this routine to provide
2445 /// different behavior.
2446 TemplateArgumentLoc RebuildPackExpansion(TemplateArgumentLoc Pattern,
2447 SourceLocation EllipsisLoc,
2448 llvm::Optional<unsigned> NumExpansions) {
2449 switch (Pattern.getArgument().getKind()) {
2450 case TemplateArgument::Expression: {
2452 = getSema().CheckPackExpansion(Pattern.getSourceExpression(),
2453 EllipsisLoc, NumExpansions);
2454 if (Result.isInvalid())
2455 return TemplateArgumentLoc();
2457 return TemplateArgumentLoc(Result.get(), Result.get());
2460 case TemplateArgument::Template:
2461 return TemplateArgumentLoc(TemplateArgument(
2462 Pattern.getArgument().getAsTemplate(),
2464 Pattern.getTemplateQualifierLoc(),
2465 Pattern.getTemplateNameLoc(),
2468 case TemplateArgument::Null:
2469 case TemplateArgument::Integral:
2470 case TemplateArgument::Declaration:
2471 case TemplateArgument::Pack:
2472 case TemplateArgument::TemplateExpansion:
2473 case TemplateArgument::NullPtr:
2474 llvm_unreachable("Pack expansion pattern has no parameter packs");
2476 case TemplateArgument::Type:
2477 if (TypeSourceInfo *Expansion
2478 = getSema().CheckPackExpansion(Pattern.getTypeSourceInfo(),
2481 return TemplateArgumentLoc(TemplateArgument(Expansion->getType()),
2486 return TemplateArgumentLoc();
2489 /// \brief Build a new expression pack expansion.
2491 /// By default, performs semantic analysis to build a new pack expansion
2492 /// for an expression. Subclasses may override this routine to provide
2493 /// different behavior.
2494 ExprResult RebuildPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
2495 llvm::Optional<unsigned> NumExpansions) {
2496 return getSema().CheckPackExpansion(Pattern, EllipsisLoc, NumExpansions);
2499 /// \brief Build a new atomic operation expression.
2501 /// By default, performs semantic analysis to build the new expression.
2502 /// Subclasses may override this routine to provide different behavior.
2503 ExprResult RebuildAtomicExpr(SourceLocation BuiltinLoc,
2504 MultiExprArg SubExprs,
2506 AtomicExpr::AtomicOp Op,
2507 SourceLocation RParenLoc) {
2508 // Just create the expression; there is not any interesting semantic
2509 // analysis here because we can't actually build an AtomicExpr until
2510 // we are sure it is semantically sound.
2511 return new (SemaRef.Context) AtomicExpr(BuiltinLoc, SubExprs, RetTy, Op,
2516 TypeLoc TransformTypeInObjectScope(TypeLoc TL,
2517 QualType ObjectType,
2518 NamedDecl *FirstQualifierInScope,
2521 TypeSourceInfo *TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
2522 QualType ObjectType,
2523 NamedDecl *FirstQualifierInScope,
2527 template<typename Derived>
2528 StmtResult TreeTransform<Derived>::TransformStmt(Stmt *S) {
2530 return SemaRef.Owned(S);
2532 switch (S->getStmtClass()) {
2533 case Stmt::NoStmtClass: break;
2535 // Transform individual statement nodes
2536 #define STMT(Node, Parent) \
2537 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(S));
2538 #define ABSTRACT_STMT(Node)
2539 #define EXPR(Node, Parent)
2540 #include "clang/AST/StmtNodes.inc"
2542 // Transform expressions by calling TransformExpr.
2543 #define STMT(Node, Parent)
2544 #define ABSTRACT_STMT(Stmt)
2545 #define EXPR(Node, Parent) case Stmt::Node##Class:
2546 #include "clang/AST/StmtNodes.inc"
2548 ExprResult E = getDerived().TransformExpr(cast<Expr>(S));
2552 return getSema().ActOnExprStmt(getSema().MakeFullExpr(E.take()));
2556 return SemaRef.Owned(S);
2560 template<typename Derived>
2561 ExprResult TreeTransform<Derived>::TransformExpr(Expr *E) {
2563 return SemaRef.Owned(E);
2565 switch (E->getStmtClass()) {
2566 case Stmt::NoStmtClass: break;
2567 #define STMT(Node, Parent) case Stmt::Node##Class: break;
2568 #define ABSTRACT_STMT(Stmt)
2569 #define EXPR(Node, Parent) \
2570 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(E));
2571 #include "clang/AST/StmtNodes.inc"
2574 return SemaRef.Owned(E);
2577 template<typename Derived>
2578 bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
2581 SmallVectorImpl<Expr *> &Outputs,
2583 for (unsigned I = 0; I != NumInputs; ++I) {
2584 // If requested, drop call arguments that need to be dropped.
2585 if (IsCall && getDerived().DropCallArgument(Inputs[I])) {
2592 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(Inputs[I])) {
2593 Expr *Pattern = Expansion->getPattern();
2595 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2596 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
2597 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
2599 // Determine whether the set of unexpanded parameter packs can and should
2602 bool RetainExpansion = false;
2603 llvm::Optional<unsigned> OrigNumExpansions
2604 = Expansion->getNumExpansions();
2605 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
2606 if (getDerived().TryExpandParameterPacks(Expansion->getEllipsisLoc(),
2607 Pattern->getSourceRange(),
2609 Expand, RetainExpansion,
2614 // The transform has determined that we should perform a simple
2615 // transformation on the pack expansion, producing another pack
2617 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
2618 ExprResult OutPattern = getDerived().TransformExpr(Pattern);
2619 if (OutPattern.isInvalid())
2622 ExprResult Out = getDerived().RebuildPackExpansion(OutPattern.get(),
2623 Expansion->getEllipsisLoc(),
2625 if (Out.isInvalid())
2630 Outputs.push_back(Out.get());
2634 // Record right away that the argument was changed. This needs
2635 // to happen even if the array expands to nothing.
2636 if (ArgChanged) *ArgChanged = true;
2638 // The transform has determined that we should perform an elementwise
2639 // expansion of the pattern. Do so.
2640 for (unsigned I = 0; I != *NumExpansions; ++I) {
2641 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
2642 ExprResult Out = getDerived().TransformExpr(Pattern);
2643 if (Out.isInvalid())
2646 if (Out.get()->containsUnexpandedParameterPack()) {
2647 Out = RebuildPackExpansion(Out.get(), Expansion->getEllipsisLoc(),
2649 if (Out.isInvalid())
2653 Outputs.push_back(Out.get());
2659 ExprResult Result = getDerived().TransformExpr(Inputs[I]);
2660 if (Result.isInvalid())
2663 if (Result.get() != Inputs[I] && ArgChanged)
2666 Outputs.push_back(Result.get());
2672 template<typename Derived>
2673 NestedNameSpecifierLoc
2674 TreeTransform<Derived>::TransformNestedNameSpecifierLoc(
2675 NestedNameSpecifierLoc NNS,
2676 QualType ObjectType,
2677 NamedDecl *FirstQualifierInScope) {
2678 SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
2679 for (NestedNameSpecifierLoc Qualifier = NNS; Qualifier;
2680 Qualifier = Qualifier.getPrefix())
2681 Qualifiers.push_back(Qualifier);
2684 while (!Qualifiers.empty()) {
2685 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
2686 NestedNameSpecifier *QNNS = Q.getNestedNameSpecifier();
2688 switch (QNNS->getKind()) {
2689 case NestedNameSpecifier::Identifier:
2690 if (SemaRef.BuildCXXNestedNameSpecifier(/*Scope=*/0,
2691 *QNNS->getAsIdentifier(),
2692 Q.getLocalBeginLoc(),
2694 ObjectType, false, SS,
2695 FirstQualifierInScope, false))
2696 return NestedNameSpecifierLoc();
2700 case NestedNameSpecifier::Namespace: {
2702 = cast_or_null<NamespaceDecl>(
2703 getDerived().TransformDecl(
2704 Q.getLocalBeginLoc(),
2705 QNNS->getAsNamespace()));
2706 SS.Extend(SemaRef.Context, NS, Q.getLocalBeginLoc(), Q.getLocalEndLoc());
2710 case NestedNameSpecifier::NamespaceAlias: {
2711 NamespaceAliasDecl *Alias
2712 = cast_or_null<NamespaceAliasDecl>(
2713 getDerived().TransformDecl(Q.getLocalBeginLoc(),
2714 QNNS->getAsNamespaceAlias()));
2715 SS.Extend(SemaRef.Context, Alias, Q.getLocalBeginLoc(),
2716 Q.getLocalEndLoc());
2720 case NestedNameSpecifier::Global:
2721 // There is no meaningful transformation that one could perform on the
2723 SS.MakeGlobal(SemaRef.Context, Q.getBeginLoc());
2726 case NestedNameSpecifier::TypeSpecWithTemplate:
2727 case NestedNameSpecifier::TypeSpec: {
2728 TypeLoc TL = TransformTypeInObjectScope(Q.getTypeLoc(), ObjectType,
2729 FirstQualifierInScope, SS);
2732 return NestedNameSpecifierLoc();
2734 if (TL.getType()->isDependentType() || TL.getType()->isRecordType() ||
2735 (SemaRef.getLangOpts().CPlusPlus0x &&
2736 TL.getType()->isEnumeralType())) {
2737 assert(!TL.getType().hasLocalQualifiers() &&
2738 "Can't get cv-qualifiers here");
2739 if (TL.getType()->isEnumeralType())
2740 SemaRef.Diag(TL.getBeginLoc(),
2741 diag::warn_cxx98_compat_enum_nested_name_spec);
2742 SS.Extend(SemaRef.Context, /*FIXME:*/SourceLocation(), TL,
2743 Q.getLocalEndLoc());
2746 // If the nested-name-specifier is an invalid type def, don't emit an
2747 // error because a previous error should have already been emitted.
2748 TypedefTypeLoc* TTL = dyn_cast<TypedefTypeLoc>(&TL);
2749 if (!TTL || !TTL->getTypedefNameDecl()->isInvalidDecl()) {
2750 SemaRef.Diag(TL.getBeginLoc(), diag::err_nested_name_spec_non_tag)
2751 << TL.getType() << SS.getRange();
2753 return NestedNameSpecifierLoc();
2757 // The qualifier-in-scope and object type only apply to the leftmost entity.
2758 FirstQualifierInScope = 0;
2759 ObjectType = QualType();
2762 // Don't rebuild the nested-name-specifier if we don't have to.
2763 if (SS.getScopeRep() == NNS.getNestedNameSpecifier() &&
2764 !getDerived().AlwaysRebuild())
2767 // If we can re-use the source-location data from the original
2768 // nested-name-specifier, do so.
2769 if (SS.location_size() == NNS.getDataLength() &&
2770 memcmp(SS.location_data(), NNS.getOpaqueData(), SS.location_size()) == 0)
2771 return NestedNameSpecifierLoc(SS.getScopeRep(), NNS.getOpaqueData());
2773 // Allocate new nested-name-specifier location information.
2774 return SS.getWithLocInContext(SemaRef.Context);
2777 template<typename Derived>
2779 TreeTransform<Derived>
2780 ::TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo) {
2781 DeclarationName Name = NameInfo.getName();
2783 return DeclarationNameInfo();
2785 switch (Name.getNameKind()) {
2786 case DeclarationName::Identifier:
2787 case DeclarationName::ObjCZeroArgSelector:
2788 case DeclarationName::ObjCOneArgSelector:
2789 case DeclarationName::ObjCMultiArgSelector:
2790 case DeclarationName::CXXOperatorName:
2791 case DeclarationName::CXXLiteralOperatorName:
2792 case DeclarationName::CXXUsingDirective:
2795 case DeclarationName::CXXConstructorName:
2796 case DeclarationName::CXXDestructorName:
2797 case DeclarationName::CXXConversionFunctionName: {
2798 TypeSourceInfo *NewTInfo;
2799 CanQualType NewCanTy;
2800 if (TypeSourceInfo *OldTInfo = NameInfo.getNamedTypeInfo()) {
2801 NewTInfo = getDerived().TransformType(OldTInfo);
2803 return DeclarationNameInfo();
2804 NewCanTy = SemaRef.Context.getCanonicalType(NewTInfo->getType());
2808 TemporaryBase Rebase(*this, NameInfo.getLoc(), Name);
2809 QualType NewT = getDerived().TransformType(Name.getCXXNameType());
2811 return DeclarationNameInfo();
2812 NewCanTy = SemaRef.Context.getCanonicalType(NewT);
2815 DeclarationName NewName
2816 = SemaRef.Context.DeclarationNames.getCXXSpecialName(Name.getNameKind(),
2818 DeclarationNameInfo NewNameInfo(NameInfo);
2819 NewNameInfo.setName(NewName);
2820 NewNameInfo.setNamedTypeInfo(NewTInfo);
2825 llvm_unreachable("Unknown name kind.");
2828 template<typename Derived>
2830 TreeTransform<Derived>::TransformTemplateName(CXXScopeSpec &SS,
2832 SourceLocation NameLoc,
2833 QualType ObjectType,
2834 NamedDecl *FirstQualifierInScope) {
2835 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
2836 TemplateDecl *Template = QTN->getTemplateDecl();
2837 assert(Template && "qualified template name must refer to a template");
2839 TemplateDecl *TransTemplate
2840 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
2843 return TemplateName();
2845 if (!getDerived().AlwaysRebuild() &&
2846 SS.getScopeRep() == QTN->getQualifier() &&
2847 TransTemplate == Template)
2850 return getDerived().RebuildTemplateName(SS, QTN->hasTemplateKeyword(),
2854 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2855 if (SS.getScopeRep()) {
2856 // These apply to the scope specifier, not the template.
2857 ObjectType = QualType();
2858 FirstQualifierInScope = 0;
2861 if (!getDerived().AlwaysRebuild() &&
2862 SS.getScopeRep() == DTN->getQualifier() &&
2863 ObjectType.isNull())
2866 if (DTN->isIdentifier()) {
2867 return getDerived().RebuildTemplateName(SS,
2868 *DTN->getIdentifier(),
2871 FirstQualifierInScope);
2874 return getDerived().RebuildTemplateName(SS, DTN->getOperator(), NameLoc,
2878 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2879 TemplateDecl *TransTemplate
2880 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
2883 return TemplateName();
2885 if (!getDerived().AlwaysRebuild() &&
2886 TransTemplate == Template)
2889 return TemplateName(TransTemplate);
2892 if (SubstTemplateTemplateParmPackStorage *SubstPack
2893 = Name.getAsSubstTemplateTemplateParmPack()) {
2894 TemplateTemplateParmDecl *TransParam
2895 = cast_or_null<TemplateTemplateParmDecl>(
2896 getDerived().TransformDecl(NameLoc, SubstPack->getParameterPack()));
2898 return TemplateName();
2900 if (!getDerived().AlwaysRebuild() &&
2901 TransParam == SubstPack->getParameterPack())
2904 return getDerived().RebuildTemplateName(TransParam,
2905 SubstPack->getArgumentPack());
2908 // These should be getting filtered out before they reach the AST.
2909 llvm_unreachable("overloaded function decl survived to here");
2912 template<typename Derived>
2913 void TreeTransform<Derived>::InventTemplateArgumentLoc(
2914 const TemplateArgument &Arg,
2915 TemplateArgumentLoc &Output) {
2916 SourceLocation Loc = getDerived().getBaseLocation();
2917 switch (Arg.getKind()) {
2918 case TemplateArgument::Null:
2919 llvm_unreachable("null template argument in TreeTransform");
2922 case TemplateArgument::Type:
2923 Output = TemplateArgumentLoc(Arg,
2924 SemaRef.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
2928 case TemplateArgument::Template:
2929 case TemplateArgument::TemplateExpansion: {
2930 NestedNameSpecifierLocBuilder Builder;
2931 TemplateName Template = Arg.getAsTemplate();
2932 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
2933 Builder.MakeTrivial(SemaRef.Context, DTN->getQualifier(), Loc);
2934 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2935 Builder.MakeTrivial(SemaRef.Context, QTN->getQualifier(), Loc);
2937 if (Arg.getKind() == TemplateArgument::Template)
2938 Output = TemplateArgumentLoc(Arg,
2939 Builder.getWithLocInContext(SemaRef.Context),
2942 Output = TemplateArgumentLoc(Arg,
2943 Builder.getWithLocInContext(SemaRef.Context),
2949 case TemplateArgument::Expression:
2950 Output = TemplateArgumentLoc(Arg, Arg.getAsExpr());
2953 case TemplateArgument::Declaration:
2954 case TemplateArgument::Integral:
2955 case TemplateArgument::Pack:
2956 case TemplateArgument::NullPtr:
2957 Output = TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
2962 template<typename Derived>
2963 bool TreeTransform<Derived>::TransformTemplateArgument(
2964 const TemplateArgumentLoc &Input,
2965 TemplateArgumentLoc &Output) {
2966 const TemplateArgument &Arg = Input.getArgument();
2967 switch (Arg.getKind()) {
2968 case TemplateArgument::Null:
2969 case TemplateArgument::Integral:
2970 case TemplateArgument::Pack:
2971 case TemplateArgument::Declaration:
2972 case TemplateArgument::NullPtr:
2973 llvm_unreachable("Unexpected TemplateArgument");
2975 case TemplateArgument::Type: {
2976 TypeSourceInfo *DI = Input.getTypeSourceInfo();
2978 DI = InventTypeSourceInfo(Input.getArgument().getAsType());
2980 DI = getDerived().TransformType(DI);
2981 if (!DI) return true;
2983 Output = TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2987 case TemplateArgument::Template: {
2988 NestedNameSpecifierLoc QualifierLoc = Input.getTemplateQualifierLoc();
2990 QualifierLoc = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc);
2996 SS.Adopt(QualifierLoc);
2997 TemplateName Template
2998 = getDerived().TransformTemplateName(SS, Arg.getAsTemplate(),
2999 Input.getTemplateNameLoc());
3000 if (Template.isNull())
3003 Output = TemplateArgumentLoc(TemplateArgument(Template), QualifierLoc,
3004 Input.getTemplateNameLoc());
3008 case TemplateArgument::TemplateExpansion:
3009 llvm_unreachable("Caller should expand pack expansions");
3011 case TemplateArgument::Expression: {
3012 // Template argument expressions are constant expressions.
3013 EnterExpressionEvaluationContext Unevaluated(getSema(),
3014 Sema::ConstantEvaluated);
3016 Expr *InputExpr = Input.getSourceExpression();
3017 if (!InputExpr) InputExpr = Input.getArgument().getAsExpr();
3019 ExprResult E = getDerived().TransformExpr(InputExpr);
3020 E = SemaRef.ActOnConstantExpression(E);
3021 if (E.isInvalid()) return true;
3022 Output = TemplateArgumentLoc(TemplateArgument(E.take()), E.take());
3027 // Work around bogus GCC warning
3031 /// \brief Iterator adaptor that invents template argument location information
3032 /// for each of the template arguments in its underlying iterator.
3033 template<typename Derived, typename InputIterator>
3034 class TemplateArgumentLocInventIterator {
3035 TreeTransform<Derived> &Self;
3039 typedef TemplateArgumentLoc value_type;
3040 typedef TemplateArgumentLoc reference;
3041 typedef typename std::iterator_traits<InputIterator>::difference_type
3043 typedef std::input_iterator_tag iterator_category;
3046 TemplateArgumentLoc Arg;
3049 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
3051 const TemplateArgumentLoc *operator->() const { return &Arg; }
3054 TemplateArgumentLocInventIterator() { }
3056 explicit TemplateArgumentLocInventIterator(TreeTransform<Derived> &Self,
3058 : Self(Self), Iter(Iter) { }
3060 TemplateArgumentLocInventIterator &operator++() {
3065 TemplateArgumentLocInventIterator operator++(int) {
3066 TemplateArgumentLocInventIterator Old(*this);
3071 reference operator*() const {
3072 TemplateArgumentLoc Result;
3073 Self.InventTemplateArgumentLoc(*Iter, Result);
3077 pointer operator->() const { return pointer(**this); }
3079 friend bool operator==(const TemplateArgumentLocInventIterator &X,
3080 const TemplateArgumentLocInventIterator &Y) {
3081 return X.Iter == Y.Iter;
3084 friend bool operator!=(const TemplateArgumentLocInventIterator &X,
3085 const TemplateArgumentLocInventIterator &Y) {
3086 return X.Iter != Y.Iter;
3090 template<typename Derived>
3091 template<typename InputIterator>
3092 bool TreeTransform<Derived>::TransformTemplateArguments(InputIterator First,
3094 TemplateArgumentListInfo &Outputs) {
3095 for (; First != Last; ++First) {
3096 TemplateArgumentLoc Out;
3097 TemplateArgumentLoc In = *First;
3099 if (In.getArgument().getKind() == TemplateArgument::Pack) {
3100 // Unpack argument packs, which we translate them into separate
3102 // FIXME: We could do much better if we could guarantee that the
3103 // TemplateArgumentLocInfo for the pack expansion would be usable for
3104 // all of the template arguments in the argument pack.
3105 typedef TemplateArgumentLocInventIterator<Derived,
3106 TemplateArgument::pack_iterator>
3108 if (TransformTemplateArguments(PackLocIterator(*this,
3109 In.getArgument().pack_begin()),
3110 PackLocIterator(*this,
3111 In.getArgument().pack_end()),
3118 if (In.getArgument().isPackExpansion()) {
3119 // We have a pack expansion, for which we will be substituting into
3121 SourceLocation Ellipsis;
3122 llvm::Optional<unsigned> OrigNumExpansions;
3123 TemplateArgumentLoc Pattern
3124 = In.getPackExpansionPattern(Ellipsis, OrigNumExpansions,
3127 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
3128 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
3129 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
3131 // Determine whether the set of unexpanded parameter packs can and should
3134 bool RetainExpansion = false;
3135 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
3136 if (getDerived().TryExpandParameterPacks(Ellipsis,
3137 Pattern.getSourceRange(),
3145 // The transform has determined that we should perform a simple
3146 // transformation on the pack expansion, producing another pack
3148 TemplateArgumentLoc OutPattern;
3149 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
3150 if (getDerived().TransformTemplateArgument(Pattern, OutPattern))
3153 Out = getDerived().RebuildPackExpansion(OutPattern, Ellipsis,
3155 if (Out.getArgument().isNull())
3158 Outputs.addArgument(Out);
3162 // The transform has determined that we should perform an elementwise
3163 // expansion of the pattern. Do so.
3164 for (unsigned I = 0; I != *NumExpansions; ++I) {
3165 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
3167 if (getDerived().TransformTemplateArgument(Pattern, Out))
3170 if (Out.getArgument().containsUnexpandedParameterPack()) {
3171 Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
3173 if (Out.getArgument().isNull())
3177 Outputs.addArgument(Out);
3180 // If we're supposed to retain a pack expansion, do so by temporarily
3181 // forgetting the partially-substituted parameter pack.
3182 if (RetainExpansion) {
3183 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
3185 if (getDerived().TransformTemplateArgument(Pattern, Out))
3188 Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
3190 if (Out.getArgument().isNull())
3193 Outputs.addArgument(Out);
3200 if (getDerived().TransformTemplateArgument(In, Out))
3203 Outputs.addArgument(Out);
3210 //===----------------------------------------------------------------------===//
3211 // Type transformation
3212 //===----------------------------------------------------------------------===//
3214 template<typename Derived>
3215 QualType TreeTransform<Derived>::TransformType(QualType T) {
3216 if (getDerived().AlreadyTransformed(T))
3219 // Temporary workaround. All of these transformations should
3220 // eventually turn into transformations on TypeLocs.
3221 TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T,
3222 getDerived().getBaseLocation());
3224 TypeSourceInfo *NewDI = getDerived().TransformType(DI);
3229 return NewDI->getType();
3232 template<typename Derived>
3233 TypeSourceInfo *TreeTransform<Derived>::TransformType(TypeSourceInfo *DI) {
3234 // Refine the base location to the type's location.
3235 TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(),
3236 getDerived().getBaseEntity());
3237 if (getDerived().AlreadyTransformed(DI->getType()))
3242 TypeLoc TL = DI->getTypeLoc();
3243 TLB.reserve(TL.getFullDataSize());
3245 QualType Result = getDerived().TransformType(TLB, TL);
3246 if (Result.isNull())
3249 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
3252 template<typename Derived>
3254 TreeTransform<Derived>::TransformType(TypeLocBuilder &TLB, TypeLoc T) {
3255 switch (T.getTypeLocClass()) {
3256 #define ABSTRACT_TYPELOC(CLASS, PARENT)
3257 #define TYPELOC(CLASS, PARENT) \
3258 case TypeLoc::CLASS: \
3259 return getDerived().Transform##CLASS##Type(TLB, cast<CLASS##TypeLoc>(T));
3260 #include "clang/AST/TypeLocNodes.def"
3263 llvm_unreachable("unhandled type loc!");
3266 /// FIXME: By default, this routine adds type qualifiers only to types
3267 /// that can have qualifiers, and silently suppresses those qualifiers
3268 /// that are not permitted (e.g., qualifiers on reference or function
3269 /// types). This is the right thing for template instantiation, but
3270 /// probably not for other clients.
3271 template<typename Derived>
3273 TreeTransform<Derived>::TransformQualifiedType(TypeLocBuilder &TLB,
3274 QualifiedTypeLoc T) {
3275 Qualifiers Quals = T.getType().getLocalQualifiers();
3277 QualType Result = getDerived().TransformType(TLB, T.getUnqualifiedLoc());
3278 if (Result.isNull())
3281 // Silently suppress qualifiers if the result type can't be qualified.
3282 // FIXME: this is the right thing for template instantiation, but
3283 // probably not for other clients.
3284 if (Result->isFunctionType() || Result->isReferenceType())
3287 // Suppress Objective-C lifetime qualifiers if they don't make sense for the
3289 if (Quals.hasObjCLifetime()) {
3290 if (!Result->isObjCLifetimeType() && !Result->isDependentType())
3291 Quals.removeObjCLifetime();
3292 else if (Result.getObjCLifetime()) {
3294 // A lifetime qualifier applied to a substituted template parameter
3295 // overrides the lifetime qualifier from the template argument.
3296 if (const SubstTemplateTypeParmType *SubstTypeParam
3297 = dyn_cast<SubstTemplateTypeParmType>(Result)) {
3298 QualType Replacement = SubstTypeParam->getReplacementType();
3299 Qualifiers Qs = Replacement.getQualifiers();
3300 Qs.removeObjCLifetime();
3302 = SemaRef.Context.getQualifiedType(Replacement.getUnqualifiedType(),
3304 Result = SemaRef.Context.getSubstTemplateTypeParmType(
3305 SubstTypeParam->getReplacedParameter(),
3307 TLB.TypeWasModifiedSafely(Result);
3309 // Otherwise, complain about the addition of a qualifier to an
3310 // already-qualified type.
3311 SourceRange R = TLB.getTemporaryTypeLoc(Result).getSourceRange();
3312 SemaRef.Diag(R.getBegin(), diag::err_attr_objc_ownership_redundant)
3315 Quals.removeObjCLifetime();
3319 if (!Quals.empty()) {
3320 Result = SemaRef.BuildQualifiedType(Result, T.getBeginLoc(), Quals);
3321 TLB.push<QualifiedTypeLoc>(Result);
3322 // No location information to preserve.
3328 template<typename Derived>
3330 TreeTransform<Derived>::TransformTypeInObjectScope(TypeLoc TL,
3331 QualType ObjectType,
3332 NamedDecl *UnqualLookup,
3334 QualType T = TL.getType();
3335 if (getDerived().AlreadyTransformed(T))
3341 if (isa<TemplateSpecializationType>(T)) {
3342 TemplateSpecializationTypeLoc SpecTL
3343 = cast<TemplateSpecializationTypeLoc>(TL);
3345 TemplateName Template =
3346 getDerived().TransformTemplateName(SS,
3347 SpecTL.getTypePtr()->getTemplateName(),
3348 SpecTL.getTemplateNameLoc(),
3349 ObjectType, UnqualLookup);
3350 if (Template.isNull())
3353 Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
3355 } else if (isa<DependentTemplateSpecializationType>(T)) {
3356 DependentTemplateSpecializationTypeLoc SpecTL
3357 = cast<DependentTemplateSpecializationTypeLoc>(TL);
3359 TemplateName Template
3360 = getDerived().RebuildTemplateName(SS,
3361 *SpecTL.getTypePtr()->getIdentifier(),
3362 SpecTL.getTemplateNameLoc(),
3363 ObjectType, UnqualLookup);
3364 if (Template.isNull())
3367 Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
3372 // Nothing special needs to be done for these.
3373 Result = getDerived().TransformType(TLB, TL);
3376 if (Result.isNull())
3379 return TLB.getTypeSourceInfo(SemaRef.Context, Result)->getTypeLoc();
3382 template<typename Derived>
3384 TreeTransform<Derived>::TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
3385 QualType ObjectType,
3386 NamedDecl *UnqualLookup,
3388 // FIXME: Painfully copy-paste from the above!
3390 QualType T = TSInfo->getType();
3391 if (getDerived().AlreadyTransformed(T))
3397 TypeLoc TL = TSInfo->getTypeLoc();
3398 if (isa<TemplateSpecializationType>(T)) {
3399 TemplateSpecializationTypeLoc SpecTL
3400 = cast<TemplateSpecializationTypeLoc>(TL);
3402 TemplateName Template
3403 = getDerived().TransformTemplateName(SS,
3404 SpecTL.getTypePtr()->getTemplateName(),
3405 SpecTL.getTemplateNameLoc(),
3406 ObjectType, UnqualLookup);
3407 if (Template.isNull())
3410 Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
3412 } else if (isa<DependentTemplateSpecializationType>(T)) {
3413 DependentTemplateSpecializationTypeLoc SpecTL
3414 = cast<DependentTemplateSpecializationTypeLoc>(TL);
3416 TemplateName Template
3417 = getDerived().RebuildTemplateName(SS,
3418 *SpecTL.getTypePtr()->getIdentifier(),
3419 SpecTL.getTemplateNameLoc(),
3420 ObjectType, UnqualLookup);
3421 if (Template.isNull())
3424 Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
3429 // Nothing special needs to be done for these.
3430 Result = getDerived().TransformType(TLB, TL);
3433 if (Result.isNull())
3436 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
3439 template <class TyLoc> static inline
3440 QualType TransformTypeSpecType(TypeLocBuilder &TLB, TyLoc T) {
3441 TyLoc NewT = TLB.push<TyLoc>(T.getType());
3442 NewT.setNameLoc(T.getNameLoc());
3446 template<typename Derived>
3447 QualType TreeTransform<Derived>::TransformBuiltinType(TypeLocBuilder &TLB,
3449 BuiltinTypeLoc NewT = TLB.push<BuiltinTypeLoc>(T.getType());
3450 NewT.setBuiltinLoc(T.getBuiltinLoc());
3451 if (T.needsExtraLocalData())
3452 NewT.getWrittenBuiltinSpecs() = T.getWrittenBuiltinSpecs();
3456 template<typename Derived>
3457 QualType TreeTransform<Derived>::TransformComplexType(TypeLocBuilder &TLB,
3460 return TransformTypeSpecType(TLB, T);
3463 template<typename Derived>
3464 QualType TreeTransform<Derived>::TransformPointerType(TypeLocBuilder &TLB,
3465 PointerTypeLoc TL) {
3466 QualType PointeeType
3467 = getDerived().TransformType(TLB, TL.getPointeeLoc());
3468 if (PointeeType.isNull())
3471 QualType Result = TL.getType();
3472 if (PointeeType->getAs<ObjCObjectType>()) {
3473 // A dependent pointer type 'T *' has is being transformed such
3474 // that an Objective-C class type is being replaced for 'T'. The
3475 // resulting pointer type is an ObjCObjectPointerType, not a
3477 Result = SemaRef.Context.getObjCObjectPointerType(PointeeType);
3479 ObjCObjectPointerTypeLoc NewT = TLB.push<ObjCObjectPointerTypeLoc>(Result);
3480 NewT.setStarLoc(TL.getStarLoc());
3484 if (getDerived().AlwaysRebuild() ||
3485 PointeeType != TL.getPointeeLoc().getType()) {
3486 Result = getDerived().RebuildPointerType(PointeeType, TL.getSigilLoc());
3487 if (Result.isNull())
3491 // Objective-C ARC can add lifetime qualifiers to the type that we're
3493 TLB.TypeWasModifiedSafely(Result->getPointeeType());
3495 PointerTypeLoc NewT = TLB.push<PointerTypeLoc>(Result);
3496 NewT.setSigilLoc(TL.getSigilLoc());
3500 template<typename Derived>
3502 TreeTransform<Derived>::TransformBlockPointerType(TypeLocBuilder &TLB,
3503 BlockPointerTypeLoc TL) {
3504 QualType PointeeType
3505 = getDerived().TransformType(TLB, TL.getPointeeLoc());
3506 if (PointeeType.isNull())
3509 QualType Result = TL.getType();
3510 if (getDerived().AlwaysRebuild() ||
3511 PointeeType != TL.getPointeeLoc().getType()) {
3512 Result = getDerived().RebuildBlockPointerType(PointeeType,
3514 if (Result.isNull())
3518 BlockPointerTypeLoc NewT = TLB.push<BlockPointerTypeLoc>(Result);
3519 NewT.setSigilLoc(TL.getSigilLoc());
3523 /// Transforms a reference type. Note that somewhat paradoxically we
3524 /// don't care whether the type itself is an l-value type or an r-value
3525 /// type; we only care if the type was *written* as an l-value type
3526 /// or an r-value type.
3527 template<typename Derived>
3529 TreeTransform<Derived>::TransformReferenceType(TypeLocBuilder &TLB,
3530 ReferenceTypeLoc TL) {
3531 const ReferenceType *T = TL.getTypePtr();
3533 // Note that this works with the pointee-as-written.
3534 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
3535 if (PointeeType.isNull())
3538 QualType Result = TL.getType();
3539 if (getDerived().AlwaysRebuild() ||
3540 PointeeType != T->getPointeeTypeAsWritten()) {
3541 Result = getDerived().RebuildReferenceType(PointeeType,
3542 T->isSpelledAsLValue(),
3544 if (Result.isNull())
3548 // Objective-C ARC can add lifetime qualifiers to the type that we're
3550 TLB.TypeWasModifiedSafely(
3551 Result->getAs<ReferenceType>()->getPointeeTypeAsWritten());
3553 // r-value references can be rebuilt as l-value references.
3554 ReferenceTypeLoc NewTL;
3555 if (isa<LValueReferenceType>(Result))
3556 NewTL = TLB.push<LValueReferenceTypeLoc>(Result);
3558 NewTL = TLB.push<RValueReferenceTypeLoc>(Result);
3559 NewTL.setSigilLoc(TL.getSigilLoc());
3564 template<typename Derived>
3566 TreeTransform<Derived>::TransformLValueReferenceType(TypeLocBuilder &TLB,
3567 LValueReferenceTypeLoc TL) {
3568 return TransformReferenceType(TLB, TL);
3571 template<typename Derived>
3573 TreeTransform<Derived>::TransformRValueReferenceType(TypeLocBuilder &TLB,
3574 RValueReferenceTypeLoc TL) {
3575 return TransformReferenceType(TLB, TL);
3578 template<typename Derived>
3580 TreeTransform<Derived>::TransformMemberPointerType(TypeLocBuilder &TLB,
3581 MemberPointerTypeLoc TL) {
3582 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
3583 if (PointeeType.isNull())
3586 TypeSourceInfo* OldClsTInfo = TL.getClassTInfo();
3587 TypeSourceInfo* NewClsTInfo = 0;
3589 NewClsTInfo = getDerived().TransformType(OldClsTInfo);
3594 const MemberPointerType *T = TL.getTypePtr();
3595 QualType OldClsType = QualType(T->getClass(), 0);
3596 QualType NewClsType;
3598 NewClsType = NewClsTInfo->getType();
3600 NewClsType = getDerived().TransformType(OldClsType);
3601 if (NewClsType.isNull())
3605 QualType Result = TL.getType();
3606 if (getDerived().AlwaysRebuild() ||
3607 PointeeType != T->getPointeeType() ||
3608 NewClsType != OldClsType) {
3609 Result = getDerived().RebuildMemberPointerType(PointeeType, NewClsType,
3611 if (Result.isNull())
3615 MemberPointerTypeLoc NewTL = TLB.push<MemberPointerTypeLoc>(Result);
3616 NewTL.setSigilLoc(TL.getSigilLoc());
3617 NewTL.setClassTInfo(NewClsTInfo);
3622 template<typename Derived>
3624 TreeTransform<Derived>::TransformConstantArrayType(TypeLocBuilder &TLB,
3625 ConstantArrayTypeLoc TL) {
3626 const ConstantArrayType *T = TL.getTypePtr();
3627 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3628 if (ElementType.isNull())
3631 QualType Result = TL.getType();
3632 if (getDerived().AlwaysRebuild() ||
3633 ElementType != T->getElementType()) {
3634 Result = getDerived().RebuildConstantArrayType(ElementType,
3635 T->getSizeModifier(),
3637 T->getIndexTypeCVRQualifiers(),
3638 TL.getBracketsRange());
3639 if (Result.isNull())
3643 // We might have either a ConstantArrayType or a VariableArrayType now:
3644 // a ConstantArrayType is allowed to have an element type which is a
3645 // VariableArrayType if the type is dependent. Fortunately, all array
3646 // types have the same location layout.
3647 ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
3648 NewTL.setLBracketLoc(TL.getLBracketLoc());
3649 NewTL.setRBracketLoc(TL.getRBracketLoc());
3651 Expr *Size = TL.getSizeExpr();
3653 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3654 Sema::ConstantEvaluated);
3655 Size = getDerived().TransformExpr(Size).template takeAs<Expr>();
3656 Size = SemaRef.ActOnConstantExpression(Size).take();
3658 NewTL.setSizeExpr(Size);
3663 template<typename Derived>
3664 QualType TreeTransform<Derived>::TransformIncompleteArrayType(
3665 TypeLocBuilder &TLB,
3666 IncompleteArrayTypeLoc TL) {
3667 const IncompleteArrayType *T = TL.getTypePtr();
3668 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3669 if (ElementType.isNull())
3672 QualType Result = TL.getType();
3673 if (getDerived().AlwaysRebuild() ||
3674 ElementType != T->getElementType()) {
3675 Result = getDerived().RebuildIncompleteArrayType(ElementType,
3676 T->getSizeModifier(),
3677 T->getIndexTypeCVRQualifiers(),
3678 TL.getBracketsRange());
3679 if (Result.isNull())
3683 IncompleteArrayTypeLoc NewTL = TLB.push<IncompleteArrayTypeLoc>(Result);
3684 NewTL.setLBracketLoc(TL.getLBracketLoc());
3685 NewTL.setRBracketLoc(TL.getRBracketLoc());
3686 NewTL.setSizeExpr(0);
3691 template<typename Derived>
3693 TreeTransform<Derived>::TransformVariableArrayType(TypeLocBuilder &TLB,
3694 VariableArrayTypeLoc TL) {
3695 const VariableArrayType *T = TL.getTypePtr();
3696 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3697 if (ElementType.isNull())
3700 ExprResult SizeResult
3701 = getDerived().TransformExpr(T->getSizeExpr());
3702 if (SizeResult.isInvalid())
3705 Expr *Size = SizeResult.take();
3707 QualType Result = TL.getType();
3708 if (getDerived().AlwaysRebuild() ||
3709 ElementType != T->getElementType() ||
3710 Size != T->getSizeExpr()) {
3711 Result = getDerived().RebuildVariableArrayType(ElementType,
3712 T->getSizeModifier(),
3714 T->getIndexTypeCVRQualifiers(),
3715 TL.getBracketsRange());
3716 if (Result.isNull())
3720 VariableArrayTypeLoc NewTL = TLB.push<VariableArrayTypeLoc>(Result);
3721 NewTL.setLBracketLoc(TL.getLBracketLoc());
3722 NewTL.setRBracketLoc(TL.getRBracketLoc());
3723 NewTL.setSizeExpr(Size);
3728 template<typename Derived>
3730 TreeTransform<Derived>::TransformDependentSizedArrayType(TypeLocBuilder &TLB,
3731 DependentSizedArrayTypeLoc TL) {
3732 const DependentSizedArrayType *T = TL.getTypePtr();
3733 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3734 if (ElementType.isNull())
3737 // Array bounds are constant expressions.
3738 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3739 Sema::ConstantEvaluated);
3741 // Prefer the expression from the TypeLoc; the other may have been uniqued.
3742 Expr *origSize = TL.getSizeExpr();
3743 if (!origSize) origSize = T->getSizeExpr();
3745 ExprResult sizeResult
3746 = getDerived().TransformExpr(origSize);
3747 sizeResult = SemaRef.ActOnConstantExpression(sizeResult);
3748 if (sizeResult.isInvalid())
3751 Expr *size = sizeResult.get();
3753 QualType Result = TL.getType();
3754 if (getDerived().AlwaysRebuild() ||
3755 ElementType != T->getElementType() ||
3757 Result = getDerived().RebuildDependentSizedArrayType(ElementType,
3758 T->getSizeModifier(),
3760 T->getIndexTypeCVRQualifiers(),
3761 TL.getBracketsRange());
3762 if (Result.isNull())
3766 // We might have any sort of array type now, but fortunately they
3767 // all have the same location layout.
3768 ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
3769 NewTL.setLBracketLoc(TL.getLBracketLoc());
3770 NewTL.setRBracketLoc(TL.getRBracketLoc());
3771 NewTL.setSizeExpr(size);
3776 template<typename Derived>
3777 QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType(
3778 TypeLocBuilder &TLB,
3779 DependentSizedExtVectorTypeLoc TL) {
3780 const DependentSizedExtVectorType *T = TL.getTypePtr();
3782 // FIXME: ext vector locs should be nested
3783 QualType ElementType = getDerived().TransformType(T->getElementType());
3784 if (ElementType.isNull())
3787 // Vector sizes are constant expressions.
3788 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3789 Sema::ConstantEvaluated);
3791 ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
3792 Size = SemaRef.ActOnConstantExpression(Size);
3793 if (Size.isInvalid())
3796 QualType Result = TL.getType();
3797 if (getDerived().AlwaysRebuild() ||
3798 ElementType != T->getElementType() ||
3799 Size.get() != T->getSizeExpr()) {
3800 Result = getDerived().RebuildDependentSizedExtVectorType(ElementType,
3802 T->getAttributeLoc());
3803 if (Result.isNull())
3807 // Result might be dependent or not.
3808 if (isa<DependentSizedExtVectorType>(Result)) {
3809 DependentSizedExtVectorTypeLoc NewTL
3810 = TLB.push<DependentSizedExtVectorTypeLoc>(Result);
3811 NewTL.setNameLoc(TL.getNameLoc());
3813 ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
3814 NewTL.setNameLoc(TL.getNameLoc());
3820 template<typename Derived>
3821 QualType TreeTransform<Derived>::TransformVectorType(TypeLocBuilder &TLB,
3823 const VectorType *T = TL.getTypePtr();
3824 QualType ElementType = getDerived().TransformType(T->getElementType());
3825 if (ElementType.isNull())
3828 QualType Result = TL.getType();
3829 if (getDerived().AlwaysRebuild() ||
3830 ElementType != T->getElementType()) {
3831 Result = getDerived().RebuildVectorType(ElementType, T->getNumElements(),
3832 T->getVectorKind());
3833 if (Result.isNull())
3837 VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result);
3838 NewTL.setNameLoc(TL.getNameLoc());
3843 template<typename Derived>
3844 QualType TreeTransform<Derived>::TransformExtVectorType(TypeLocBuilder &TLB,
3845 ExtVectorTypeLoc TL) {
3846 const VectorType *T = TL.getTypePtr();
3847 QualType ElementType = getDerived().TransformType(T->getElementType());
3848 if (ElementType.isNull())
3851 QualType Result = TL.getType();
3852 if (getDerived().AlwaysRebuild() ||
3853 ElementType != T->getElementType()) {
3854 Result = getDerived().RebuildExtVectorType(ElementType,
3855 T->getNumElements(),
3856 /*FIXME*/ SourceLocation());
3857 if (Result.isNull())
3861 ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
3862 NewTL.setNameLoc(TL.getNameLoc());
3867 template<typename Derived>
3869 TreeTransform<Derived>::TransformFunctionTypeParam(ParmVarDecl *OldParm,
3870 int indexAdjustment,
3871 llvm::Optional<unsigned> NumExpansions,
3872 bool ExpectParameterPack) {
3873 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
3874 TypeSourceInfo *NewDI = 0;
3876 if (NumExpansions && isa<PackExpansionType>(OldDI->getType())) {
3877 // If we're substituting into a pack expansion type and we know the
3878 // length we want to expand to, just substitute for the pattern.
3879 TypeLoc OldTL = OldDI->getTypeLoc();
3880 PackExpansionTypeLoc OldExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
3883 TypeLoc NewTL = OldDI->getTypeLoc();
3884 TLB.reserve(NewTL.getFullDataSize());
3886 QualType Result = getDerived().TransformType(TLB,
3887 OldExpansionTL.getPatternLoc());
3888 if (Result.isNull())
3891 Result = RebuildPackExpansionType(Result,
3892 OldExpansionTL.getPatternLoc().getSourceRange(),
3893 OldExpansionTL.getEllipsisLoc(),
3895 if (Result.isNull())
3898 PackExpansionTypeLoc NewExpansionTL
3899 = TLB.push<PackExpansionTypeLoc>(Result);
3900 NewExpansionTL.setEllipsisLoc(OldExpansionTL.getEllipsisLoc());
3901 NewDI = TLB.getTypeSourceInfo(SemaRef.Context, Result);
3903 NewDI = getDerived().TransformType(OldDI);
3907 if (NewDI == OldDI && indexAdjustment == 0)
3910 ParmVarDecl *newParm = ParmVarDecl::Create(SemaRef.Context,
3911 OldParm->getDeclContext(),
3912 OldParm->getInnerLocStart(),
3913 OldParm->getLocation(),
3914 OldParm->getIdentifier(),
3917 OldParm->getStorageClass(),
3918 OldParm->getStorageClassAsWritten(),
3920 newParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
3921 OldParm->getFunctionScopeIndex() + indexAdjustment);
3925 template<typename Derived>
3926 bool TreeTransform<Derived>::
3927 TransformFunctionTypeParams(SourceLocation Loc,
3928 ParmVarDecl **Params, unsigned NumParams,
3929 const QualType *ParamTypes,
3930 SmallVectorImpl<QualType> &OutParamTypes,
3931 SmallVectorImpl<ParmVarDecl*> *PVars) {
3932 int indexAdjustment = 0;
3934 for (unsigned i = 0; i != NumParams; ++i) {
3935 if (ParmVarDecl *OldParm = Params[i]) {
3936 assert(OldParm->getFunctionScopeIndex() == i);
3938 llvm::Optional<unsigned> NumExpansions;
3939 ParmVarDecl *NewParm = 0;
3940 if (OldParm->isParameterPack()) {
3941 // We have a function parameter pack that may need to be expanded.
3942 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
3944 // Find the parameter packs that could be expanded.
3945 TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc();
3946 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(TL);
3947 TypeLoc Pattern = ExpansionTL.getPatternLoc();
3948 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
3949 assert(Unexpanded.size() > 0 && "Could not find parameter packs!");
3951 // Determine whether we should expand the parameter packs.
3952 bool ShouldExpand = false;
3953 bool RetainExpansion = false;
3954 llvm::Optional<unsigned> OrigNumExpansions
3955 = ExpansionTL.getTypePtr()->getNumExpansions();
3956 NumExpansions = OrigNumExpansions;
3957 if (getDerived().TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(),
3958 Pattern.getSourceRange(),
3967 // Expand the function parameter pack into multiple, separate
3969 getDerived().ExpandingFunctionParameterPack(OldParm);
3970 for (unsigned I = 0; I != *NumExpansions; ++I) {
3971 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
3972 ParmVarDecl *NewParm
3973 = getDerived().TransformFunctionTypeParam(OldParm,
3976 /*ExpectParameterPack=*/false);
3980 OutParamTypes.push_back(NewParm->getType());
3982 PVars->push_back(NewParm);
3985 // If we're supposed to retain a pack expansion, do so by temporarily
3986 // forgetting the partially-substituted parameter pack.
3987 if (RetainExpansion) {
3988 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
3989 ParmVarDecl *NewParm
3990 = getDerived().TransformFunctionTypeParam(OldParm,
3993 /*ExpectParameterPack=*/false);
3997 OutParamTypes.push_back(NewParm->getType());
3999 PVars->push_back(NewParm);
4002 // The next parameter should have the same adjustment as the
4003 // last thing we pushed, but we post-incremented indexAdjustment
4004 // on every push. Also, if we push nothing, the adjustment should
4008 // We're done with the pack expansion.
4012 // We'll substitute the parameter now without expanding the pack
4014 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
4015 NewParm = getDerived().TransformFunctionTypeParam(OldParm,
4018 /*ExpectParameterPack=*/true);
4020 NewParm = getDerived().TransformFunctionTypeParam(OldParm,
4022 llvm::Optional<unsigned>(),
4023 /*ExpectParameterPack=*/false);
4029 OutParamTypes.push_back(NewParm->getType());
4031 PVars->push_back(NewParm);
4035 // Deal with the possibility that we don't have a parameter
4036 // declaration for this parameter.
4037 QualType OldType = ParamTypes[i];
4038 bool IsPackExpansion = false;
4039 llvm::Optional<unsigned> NumExpansions;
4041 if (const PackExpansionType *Expansion
4042 = dyn_cast<PackExpansionType>(OldType)) {
4043 // We have a function parameter pack that may need to be expanded.
4044 QualType Pattern = Expansion->getPattern();
4045 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
4046 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
4048 // Determine whether we should expand the parameter packs.
4049 bool ShouldExpand = false;
4050 bool RetainExpansion = false;
4051 if (getDerived().TryExpandParameterPacks(Loc, SourceRange(),
4060 // Expand the function parameter pack into multiple, separate
4062 for (unsigned I = 0; I != *NumExpansions; ++I) {
4063 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
4064 QualType NewType = getDerived().TransformType(Pattern);
4065 if (NewType.isNull())
4068 OutParamTypes.push_back(NewType);
4070 PVars->push_back(0);
4073 // We're done with the pack expansion.
4077 // If we're supposed to retain a pack expansion, do so by temporarily
4078 // forgetting the partially-substituted parameter pack.
4079 if (RetainExpansion) {
4080 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
4081 QualType NewType = getDerived().TransformType(Pattern);
4082 if (NewType.isNull())
4085 OutParamTypes.push_back(NewType);
4087 PVars->push_back(0);
4090 // We'll substitute the parameter now without expanding the pack
4092 OldType = Expansion->getPattern();
4093 IsPackExpansion = true;
4094 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
4095 NewType = getDerived().TransformType(OldType);
4097 NewType = getDerived().TransformType(OldType);
4100 if (NewType.isNull())
4103 if (IsPackExpansion)
4104 NewType = getSema().Context.getPackExpansionType(NewType,
4107 OutParamTypes.push_back(NewType);
4109 PVars->push_back(0);
4114 for (unsigned i = 0, e = PVars->size(); i != e; ++i)
4115 if (ParmVarDecl *parm = (*PVars)[i])
4116 assert(parm->getFunctionScopeIndex() == i);
4123 template<typename Derived>
4125 TreeTransform<Derived>::TransformFunctionProtoType(TypeLocBuilder &TLB,
4126 FunctionProtoTypeLoc TL) {
4127 return getDerived().TransformFunctionProtoType(TLB, TL, 0, 0);
4130 template<typename Derived>
4132 TreeTransform<Derived>::TransformFunctionProtoType(TypeLocBuilder &TLB,
4133 FunctionProtoTypeLoc TL,
4134 CXXRecordDecl *ThisContext,
4135 unsigned ThisTypeQuals) {
4136 // Transform the parameters and return type.
4138 // We are required to instantiate the params and return type in source order.
4139 // When the function has a trailing return type, we instantiate the
4140 // parameters before the return type, since the return type can then refer
4141 // to the parameters themselves (via decltype, sizeof, etc.).
4143 SmallVector<QualType, 4> ParamTypes;
4144 SmallVector<ParmVarDecl*, 4> ParamDecls;
4145 const FunctionProtoType *T = TL.getTypePtr();
4147 QualType ResultType;
4149 if (T->hasTrailingReturn()) {
4150 if (getDerived().TransformFunctionTypeParams(TL.getBeginLoc(),
4153 TL.getTypePtr()->arg_type_begin(),
4154 ParamTypes, &ParamDecls))
4158 // C++11 [expr.prim.general]p3:
4159 // If a declaration declares a member function or member function
4160 // template of a class X, the expression this is a prvalue of type
4161 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4162 // and the end of the function-definition, member-declarator, or
4164 Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals);
4166 ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4167 if (ResultType.isNull())
4172 ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4173 if (ResultType.isNull())
4176 if (getDerived().TransformFunctionTypeParams(TL.getBeginLoc(),
4179 TL.getTypePtr()->arg_type_begin(),
4180 ParamTypes, &ParamDecls))
4184 // FIXME: Need to transform the exception-specification too.
4186 QualType Result = TL.getType();
4187 if (getDerived().AlwaysRebuild() ||
4188 ResultType != T->getResultType() ||
4189 T->getNumArgs() != ParamTypes.size() ||
4190 !std::equal(T->arg_type_begin(), T->arg_type_end(), ParamTypes.begin())) {
4191 Result = getDerived().RebuildFunctionProtoType(ResultType,
4195 T->hasTrailingReturn(),
4197 T->getRefQualifier(),
4199 if (Result.isNull())
4203 FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result);
4204 NewTL.setLocalRangeBegin(TL.getLocalRangeBegin());
4205 NewTL.setLParenLoc(TL.getLParenLoc());
4206 NewTL.setRParenLoc(TL.getRParenLoc());
4207 NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
4208 for (unsigned i = 0, e = NewTL.getNumArgs(); i != e; ++i)
4209 NewTL.setArg(i, ParamDecls[i]);
4214 template<typename Derived>
4215 QualType TreeTransform<Derived>::TransformFunctionNoProtoType(
4216 TypeLocBuilder &TLB,
4217 FunctionNoProtoTypeLoc TL) {
4218 const FunctionNoProtoType *T = TL.getTypePtr();
4219 QualType ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
4220 if (ResultType.isNull())
4223 QualType Result = TL.getType();
4224 if (getDerived().AlwaysRebuild() ||
4225 ResultType != T->getResultType())
4226 Result = getDerived().RebuildFunctionNoProtoType(ResultType);
4228 FunctionNoProtoTypeLoc NewTL = TLB.push<FunctionNoProtoTypeLoc>(Result);
4229 NewTL.setLocalRangeBegin(TL.getLocalRangeBegin());
4230 NewTL.setLParenLoc(TL.getLParenLoc());
4231 NewTL.setRParenLoc(TL.getRParenLoc());
4232 NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
4237 template<typename Derived> QualType
4238 TreeTransform<Derived>::TransformUnresolvedUsingType(TypeLocBuilder &TLB,
4239 UnresolvedUsingTypeLoc TL) {
4240 const UnresolvedUsingType *T = TL.getTypePtr();
4241 Decl *D = getDerived().TransformDecl(TL.getNameLoc(), T->getDecl());
4245 QualType Result = TL.getType();
4246 if (getDerived().AlwaysRebuild() || D != T->getDecl()) {
4247 Result = getDerived().RebuildUnresolvedUsingType(D);
4248 if (Result.isNull())
4252 // We might get an arbitrary type spec type back. We should at
4253 // least always get a type spec type, though.
4254 TypeSpecTypeLoc NewTL = TLB.pushTypeSpec(Result);
4255 NewTL.setNameLoc(TL.getNameLoc());
4260 template<typename Derived>
4261 QualType TreeTransform<Derived>::TransformTypedefType(TypeLocBuilder &TLB,
4262 TypedefTypeLoc TL) {
4263 const TypedefType *T = TL.getTypePtr();
4264 TypedefNameDecl *Typedef
4265 = cast_or_null<TypedefNameDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4270 QualType Result = TL.getType();
4271 if (getDerived().AlwaysRebuild() ||
4272 Typedef != T->getDecl()) {
4273 Result = getDerived().RebuildTypedefType(Typedef);
4274 if (Result.isNull())
4278 TypedefTypeLoc NewTL = TLB.push<TypedefTypeLoc>(Result);
4279 NewTL.setNameLoc(TL.getNameLoc());
4284 template<typename Derived>
4285 QualType TreeTransform<Derived>::TransformTypeOfExprType(TypeLocBuilder &TLB,
4286 TypeOfExprTypeLoc TL) {
4287 // typeof expressions are not potentially evaluated contexts
4288 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated,
4289 Sema::ReuseLambdaContextDecl);
4291 ExprResult E = getDerived().TransformExpr(TL.getUnderlyingExpr());
4295 E = SemaRef.HandleExprEvaluationContextForTypeof(E.get());
4299 QualType Result = TL.getType();
4300 if (getDerived().AlwaysRebuild() ||
4301 E.get() != TL.getUnderlyingExpr()) {
4302 Result = getDerived().RebuildTypeOfExprType(E.get(), TL.getTypeofLoc());
4303 if (Result.isNull())
4308 TypeOfExprTypeLoc NewTL = TLB.push<TypeOfExprTypeLoc>(Result);
4309 NewTL.setTypeofLoc(TL.getTypeofLoc());
4310 NewTL.setLParenLoc(TL.getLParenLoc());
4311 NewTL.setRParenLoc(TL.getRParenLoc());
4316 template<typename Derived>
4317 QualType TreeTransform<Derived>::TransformTypeOfType(TypeLocBuilder &TLB,
4319 TypeSourceInfo* Old_Under_TI = TL.getUnderlyingTInfo();
4320 TypeSourceInfo* New_Under_TI = getDerived().TransformType(Old_Under_TI);
4324 QualType Result = TL.getType();
4325 if (getDerived().AlwaysRebuild() || New_Under_TI != Old_Under_TI) {
4326 Result = getDerived().RebuildTypeOfType(New_Under_TI->getType());
4327 if (Result.isNull())
4331 TypeOfTypeLoc NewTL = TLB.push<TypeOfTypeLoc>(Result);
4332 NewTL.setTypeofLoc(TL.getTypeofLoc());
4333 NewTL.setLParenLoc(TL.getLParenLoc());
4334 NewTL.setRParenLoc(TL.getRParenLoc());
4335 NewTL.setUnderlyingTInfo(New_Under_TI);
4340 template<typename Derived>
4341 QualType TreeTransform<Derived>::TransformDecltypeType(TypeLocBuilder &TLB,
4342 DecltypeTypeLoc TL) {
4343 const DecltypeType *T = TL.getTypePtr();
4345 // decltype expressions are not potentially evaluated contexts
4346 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated, 0,
4347 /*IsDecltype=*/ true);
4349 ExprResult E = getDerived().TransformExpr(T->getUnderlyingExpr());
4353 E = getSema().ActOnDecltypeExpression(E.take());
4357 QualType Result = TL.getType();
4358 if (getDerived().AlwaysRebuild() ||
4359 E.get() != T->getUnderlyingExpr()) {
4360 Result = getDerived().RebuildDecltypeType(E.get(), TL.getNameLoc());
4361 if (Result.isNull())
4366 DecltypeTypeLoc NewTL = TLB.push<DecltypeTypeLoc>(Result);
4367 NewTL.setNameLoc(TL.getNameLoc());
4372 template<typename Derived>
4373 QualType TreeTransform<Derived>::TransformUnaryTransformType(
4374 TypeLocBuilder &TLB,
4375 UnaryTransformTypeLoc TL) {
4376 QualType Result = TL.getType();
4377 if (Result->isDependentType()) {
4378 const UnaryTransformType *T = TL.getTypePtr();
4380 getDerived().TransformType(TL.getUnderlyingTInfo())->getType();
4381 Result = getDerived().RebuildUnaryTransformType(NewBase,
4384 if (Result.isNull())
4388 UnaryTransformTypeLoc NewTL = TLB.push<UnaryTransformTypeLoc>(Result);
4389 NewTL.setKWLoc(TL.getKWLoc());
4390 NewTL.setParensRange(TL.getParensRange());
4391 NewTL.setUnderlyingTInfo(TL.getUnderlyingTInfo());
4395 template<typename Derived>
4396 QualType TreeTransform<Derived>::TransformAutoType(TypeLocBuilder &TLB,
4398 const AutoType *T = TL.getTypePtr();
4399 QualType OldDeduced = T->getDeducedType();
4400 QualType NewDeduced;
4401 if (!OldDeduced.isNull()) {
4402 NewDeduced = getDerived().TransformType(OldDeduced);
4403 if (NewDeduced.isNull())
4407 QualType Result = TL.getType();
4408 if (getDerived().AlwaysRebuild() || NewDeduced != OldDeduced) {
4409 Result = getDerived().RebuildAutoType(NewDeduced);
4410 if (Result.isNull())
4414 AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
4415 NewTL.setNameLoc(TL.getNameLoc());
4420 template<typename Derived>
4421 QualType TreeTransform<Derived>::TransformRecordType(TypeLocBuilder &TLB,
4423 const RecordType *T = TL.getTypePtr();
4425 = cast_or_null<RecordDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4430 QualType Result = TL.getType();
4431 if (getDerived().AlwaysRebuild() ||
4432 Record != T->getDecl()) {
4433 Result = getDerived().RebuildRecordType(Record);
4434 if (Result.isNull())
4438 RecordTypeLoc NewTL = TLB.push<RecordTypeLoc>(Result);
4439 NewTL.setNameLoc(TL.getNameLoc());
4444 template<typename Derived>
4445 QualType TreeTransform<Derived>::TransformEnumType(TypeLocBuilder &TLB,
4447 const EnumType *T = TL.getTypePtr();
4449 = cast_or_null<EnumDecl>(getDerived().TransformDecl(TL.getNameLoc(),
4454 QualType Result = TL.getType();
4455 if (getDerived().AlwaysRebuild() ||
4456 Enum != T->getDecl()) {
4457 Result = getDerived().RebuildEnumType(Enum);
4458 if (Result.isNull())
4462 EnumTypeLoc NewTL = TLB.push<EnumTypeLoc>(Result);
4463 NewTL.setNameLoc(TL.getNameLoc());
4468 template<typename Derived>
4469 QualType TreeTransform<Derived>::TransformInjectedClassNameType(
4470 TypeLocBuilder &TLB,
4471 InjectedClassNameTypeLoc TL) {
4472 Decl *D = getDerived().TransformDecl(TL.getNameLoc(),
4473 TL.getTypePtr()->getDecl());
4474 if (!D) return QualType();
4476 QualType T = SemaRef.Context.getTypeDeclType(cast<TypeDecl>(D));
4477 TLB.pushTypeSpec(T).setNameLoc(TL.getNameLoc());
4481 template<typename Derived>
4482 QualType TreeTransform<Derived>::TransformTemplateTypeParmType(
4483 TypeLocBuilder &TLB,
4484 TemplateTypeParmTypeLoc TL) {
4485 return TransformTypeSpecType(TLB, TL);
4488 template<typename Derived>
4489 QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmType(
4490 TypeLocBuilder &TLB,
4491 SubstTemplateTypeParmTypeLoc TL) {
4492 const SubstTemplateTypeParmType *T = TL.getTypePtr();
4494 // Substitute into the replacement type, which itself might involve something
4495 // that needs to be transformed. This only tends to occur with default
4496 // template arguments of template template parameters.
4497 TemporaryBase Rebase(*this, TL.getNameLoc(), DeclarationName());
4498 QualType Replacement = getDerived().TransformType(T->getReplacementType());
4499 if (Replacement.isNull())
4502 // Always canonicalize the replacement type.
4503 Replacement = SemaRef.Context.getCanonicalType(Replacement);
4505 = SemaRef.Context.getSubstTemplateTypeParmType(T->getReplacedParameter(),
4508 // Propagate type-source information.
4509 SubstTemplateTypeParmTypeLoc NewTL
4510 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
4511 NewTL.setNameLoc(TL.getNameLoc());
4516 template<typename Derived>
4517 QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmPackType(
4518 TypeLocBuilder &TLB,
4519 SubstTemplateTypeParmPackTypeLoc TL) {
4520 return TransformTypeSpecType(TLB, TL);
4523 template<typename Derived>
4524 QualType TreeTransform<Derived>::TransformTemplateSpecializationType(
4525 TypeLocBuilder &TLB,
4526 TemplateSpecializationTypeLoc TL) {
4527 const TemplateSpecializationType *T = TL.getTypePtr();
4529 // The nested-name-specifier never matters in a TemplateSpecializationType,
4530 // because we can't have a dependent nested-name-specifier anyway.
4532 TemplateName Template
4533 = getDerived().TransformTemplateName(SS, T->getTemplateName(),
4534 TL.getTemplateNameLoc());
4535 if (Template.isNull())
4538 return getDerived().TransformTemplateSpecializationType(TLB, TL, Template);
4541 template<typename Derived>
4542 QualType TreeTransform<Derived>::TransformAtomicType(TypeLocBuilder &TLB,
4544 QualType ValueType = getDerived().TransformType(TLB, TL.getValueLoc());
4545 if (ValueType.isNull())
4548 QualType Result = TL.getType();
4549 if (getDerived().AlwaysRebuild() ||
4550 ValueType != TL.getValueLoc().getType()) {
4551 Result = getDerived().RebuildAtomicType(ValueType, TL.getKWLoc());
4552 if (Result.isNull())
4556 AtomicTypeLoc NewTL = TLB.push<AtomicTypeLoc>(Result);
4557 NewTL.setKWLoc(TL.getKWLoc());
4558 NewTL.setLParenLoc(TL.getLParenLoc());
4559 NewTL.setRParenLoc(TL.getRParenLoc());
4565 /// \brief Simple iterator that traverses the template arguments in a
4566 /// container that provides a \c getArgLoc() member function.
4568 /// This iterator is intended to be used with the iterator form of
4569 /// \c TreeTransform<Derived>::TransformTemplateArguments().
4570 template<typename ArgLocContainer>
4571 class TemplateArgumentLocContainerIterator {
4572 ArgLocContainer *Container;
4576 typedef TemplateArgumentLoc value_type;
4577 typedef TemplateArgumentLoc reference;
4578 typedef int difference_type;
4579 typedef std::input_iterator_tag iterator_category;
4582 TemplateArgumentLoc Arg;
4585 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
4587 const TemplateArgumentLoc *operator->() const {
4593 TemplateArgumentLocContainerIterator() {}
4595 TemplateArgumentLocContainerIterator(ArgLocContainer &Container,
4597 : Container(&Container), Index(Index) { }
4599 TemplateArgumentLocContainerIterator &operator++() {
4604 TemplateArgumentLocContainerIterator operator++(int) {
4605 TemplateArgumentLocContainerIterator Old(*this);
4610 TemplateArgumentLoc operator*() const {
4611 return Container->getArgLoc(Index);
4614 pointer operator->() const {
4615 return pointer(Container->getArgLoc(Index));
4618 friend bool operator==(const TemplateArgumentLocContainerIterator &X,
4619 const TemplateArgumentLocContainerIterator &Y) {
4620 return X.Container == Y.Container && X.Index == Y.Index;
4623 friend bool operator!=(const TemplateArgumentLocContainerIterator &X,
4624 const TemplateArgumentLocContainerIterator &Y) {
4631 template <typename Derived>
4632 QualType TreeTransform<Derived>::TransformTemplateSpecializationType(
4633 TypeLocBuilder &TLB,
4634 TemplateSpecializationTypeLoc TL,
4635 TemplateName Template) {
4636 TemplateArgumentListInfo NewTemplateArgs;
4637 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4638 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
4639 typedef TemplateArgumentLocContainerIterator<TemplateSpecializationTypeLoc>
4641 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4642 ArgIterator(TL, TL.getNumArgs()),
4646 // FIXME: maybe don't rebuild if all the template arguments are the same.
4649 getDerived().RebuildTemplateSpecializationType(Template,
4650 TL.getTemplateNameLoc(),
4653 if (!Result.isNull()) {
4654 // Specializations of template template parameters are represented as
4655 // TemplateSpecializationTypes, and substitution of type alias templates
4656 // within a dependent context can transform them into
4657 // DependentTemplateSpecializationTypes.
4658 if (isa<DependentTemplateSpecializationType>(Result)) {
4659 DependentTemplateSpecializationTypeLoc NewTL
4660 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
4661 NewTL.setElaboratedKeywordLoc(SourceLocation());
4662 NewTL.setQualifierLoc(NestedNameSpecifierLoc());
4663 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
4664 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4665 NewTL.setLAngleLoc(TL.getLAngleLoc());
4666 NewTL.setRAngleLoc(TL.getRAngleLoc());
4667 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4668 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4672 TemplateSpecializationTypeLoc NewTL
4673 = TLB.push<TemplateSpecializationTypeLoc>(Result);
4674 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
4675 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4676 NewTL.setLAngleLoc(TL.getLAngleLoc());
4677 NewTL.setRAngleLoc(TL.getRAngleLoc());
4678 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4679 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4685 template <typename Derived>
4686 QualType TreeTransform<Derived>::TransformDependentTemplateSpecializationType(
4687 TypeLocBuilder &TLB,
4688 DependentTemplateSpecializationTypeLoc TL,
4689 TemplateName Template,
4691 TemplateArgumentListInfo NewTemplateArgs;
4692 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4693 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
4694 typedef TemplateArgumentLocContainerIterator<
4695 DependentTemplateSpecializationTypeLoc> ArgIterator;
4696 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4697 ArgIterator(TL, TL.getNumArgs()),
4701 // FIXME: maybe don't rebuild if all the template arguments are the same.
4703 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
4705 = getSema().Context.getDependentTemplateSpecializationType(
4706 TL.getTypePtr()->getKeyword(),
4707 DTN->getQualifier(),
4708 DTN->getIdentifier(),
4711 DependentTemplateSpecializationTypeLoc NewTL
4712 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
4713 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
4714 NewTL.setQualifierLoc(SS.getWithLocInContext(SemaRef.Context));
4715 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
4716 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4717 NewTL.setLAngleLoc(TL.getLAngleLoc());
4718 NewTL.setRAngleLoc(TL.getRAngleLoc());
4719 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4720 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4725 = getDerived().RebuildTemplateSpecializationType(Template,
4726 TL.getTemplateNameLoc(),
4729 if (!Result.isNull()) {
4730 /// FIXME: Wrap this in an elaborated-type-specifier?
4731 TemplateSpecializationTypeLoc NewTL
4732 = TLB.push<TemplateSpecializationTypeLoc>(Result);
4733 NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
4734 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4735 NewTL.setLAngleLoc(TL.getLAngleLoc());
4736 NewTL.setRAngleLoc(TL.getRAngleLoc());
4737 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
4738 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
4744 template<typename Derived>
4746 TreeTransform<Derived>::TransformElaboratedType(TypeLocBuilder &TLB,
4747 ElaboratedTypeLoc TL) {
4748 const ElaboratedType *T = TL.getTypePtr();
4750 NestedNameSpecifierLoc QualifierLoc;
4751 // NOTE: the qualifier in an ElaboratedType is optional.
4752 if (TL.getQualifierLoc()) {
4754 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4759 QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
4760 if (NamedT.isNull())
4763 // C++0x [dcl.type.elab]p2:
4764 // If the identifier resolves to a typedef-name or the simple-template-id
4765 // resolves to an alias template specialization, the
4766 // elaborated-type-specifier is ill-formed.
4767 if (T->getKeyword() != ETK_None && T->getKeyword() != ETK_Typename) {
4768 if (const TemplateSpecializationType *TST =
4769 NamedT->getAs<TemplateSpecializationType>()) {
4770 TemplateName Template = TST->getTemplateName();
4771 if (TypeAliasTemplateDecl *TAT =
4772 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
4773 SemaRef.Diag(TL.getNamedTypeLoc().getBeginLoc(),
4774 diag::err_tag_reference_non_tag) << 4;
4775 SemaRef.Diag(TAT->getLocation(), diag::note_declared_at);
4780 QualType Result = TL.getType();
4781 if (getDerived().AlwaysRebuild() ||
4782 QualifierLoc != TL.getQualifierLoc() ||
4783 NamedT != T->getNamedType()) {
4784 Result = getDerived().RebuildElaboratedType(TL.getElaboratedKeywordLoc(),
4786 QualifierLoc, NamedT);
4787 if (Result.isNull())
4791 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
4792 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
4793 NewTL.setQualifierLoc(QualifierLoc);
4797 template<typename Derived>
4798 QualType TreeTransform<Derived>::TransformAttributedType(
4799 TypeLocBuilder &TLB,
4800 AttributedTypeLoc TL) {
4801 const AttributedType *oldType = TL.getTypePtr();
4802 QualType modifiedType = getDerived().TransformType(TLB, TL.getModifiedLoc());
4803 if (modifiedType.isNull())
4806 QualType result = TL.getType();
4808 // FIXME: dependent operand expressions?
4809 if (getDerived().AlwaysRebuild() ||
4810 modifiedType != oldType->getModifiedType()) {
4811 // TODO: this is really lame; we should really be rebuilding the
4812 // equivalent type from first principles.
4813 QualType equivalentType
4814 = getDerived().TransformType(oldType->getEquivalentType());
4815 if (equivalentType.isNull())
4817 result = SemaRef.Context.getAttributedType(oldType->getAttrKind(),
4822 AttributedTypeLoc newTL = TLB.push<AttributedTypeLoc>(result);
4823 newTL.setAttrNameLoc(TL.getAttrNameLoc());
4824 if (TL.hasAttrOperand())
4825 newTL.setAttrOperandParensRange(TL.getAttrOperandParensRange());
4826 if (TL.hasAttrExprOperand())
4827 newTL.setAttrExprOperand(TL.getAttrExprOperand());
4828 else if (TL.hasAttrEnumOperand())
4829 newTL.setAttrEnumOperandLoc(TL.getAttrEnumOperandLoc());
4834 template<typename Derived>
4836 TreeTransform<Derived>::TransformParenType(TypeLocBuilder &TLB,
4838 QualType Inner = getDerived().TransformType(TLB, TL.getInnerLoc());
4842 QualType Result = TL.getType();
4843 if (getDerived().AlwaysRebuild() ||
4844 Inner != TL.getInnerLoc().getType()) {
4845 Result = getDerived().RebuildParenType(Inner);
4846 if (Result.isNull())
4850 ParenTypeLoc NewTL = TLB.push<ParenTypeLoc>(Result);
4851 NewTL.setLParenLoc(TL.getLParenLoc());
4852 NewTL.setRParenLoc(TL.getRParenLoc());
4856 template<typename Derived>
4857 QualType TreeTransform<Derived>::TransformDependentNameType(TypeLocBuilder &TLB,
4858 DependentNameTypeLoc TL) {
4859 const DependentNameType *T = TL.getTypePtr();
4861 NestedNameSpecifierLoc QualifierLoc
4862 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4867 = getDerived().RebuildDependentNameType(T->getKeyword(),
4868 TL.getElaboratedKeywordLoc(),
4872 if (Result.isNull())
4875 if (const ElaboratedType* ElabT = Result->getAs<ElaboratedType>()) {
4876 QualType NamedT = ElabT->getNamedType();
4877 TLB.pushTypeSpec(NamedT).setNameLoc(TL.getNameLoc());
4879 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
4880 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
4881 NewTL.setQualifierLoc(QualifierLoc);
4883 DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result);
4884 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
4885 NewTL.setQualifierLoc(QualifierLoc);
4886 NewTL.setNameLoc(TL.getNameLoc());
4891 template<typename Derived>
4892 QualType TreeTransform<Derived>::
4893 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
4894 DependentTemplateSpecializationTypeLoc TL) {
4895 NestedNameSpecifierLoc QualifierLoc;
4896 if (TL.getQualifierLoc()) {
4898 = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc());
4904 .TransformDependentTemplateSpecializationType(TLB, TL, QualifierLoc);
4907 template<typename Derived>
4908 QualType TreeTransform<Derived>::
4909 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
4910 DependentTemplateSpecializationTypeLoc TL,
4911 NestedNameSpecifierLoc QualifierLoc) {
4912 const DependentTemplateSpecializationType *T = TL.getTypePtr();
4914 TemplateArgumentListInfo NewTemplateArgs;
4915 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
4916 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
4918 typedef TemplateArgumentLocContainerIterator<
4919 DependentTemplateSpecializationTypeLoc> ArgIterator;
4920 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
4921 ArgIterator(TL, TL.getNumArgs()),
4926 = getDerived().RebuildDependentTemplateSpecializationType(T->getKeyword(),
4929 TL.getTemplateNameLoc(),
4931 if (Result.isNull())
4934 if (const ElaboratedType *ElabT = dyn_cast<ElaboratedType>(Result)) {
4935 QualType NamedT = ElabT->getNamedType();
4937 // Copy information relevant to the template specialization.
4938 TemplateSpecializationTypeLoc NamedTL
4939 = TLB.push<TemplateSpecializationTypeLoc>(NamedT);
4940 NamedTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
4941 NamedTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4942 NamedTL.setLAngleLoc(TL.getLAngleLoc());
4943 NamedTL.setRAngleLoc(TL.getRAngleLoc());
4944 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
4945 NamedTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
4947 // Copy information relevant to the elaborated type.
4948 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
4949 NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
4950 NewTL.setQualifierLoc(QualifierLoc);
4951 } else if (isa<DependentTemplateSpecializationType>(Result)) {
4952 DependentTemplateSpecializationTypeLoc SpecTL
4953 = TLB.push<DependentTemplateSpecializationTypeLoc>(Result);
4954 SpecTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc());
4955 SpecTL.setQualifierLoc(QualifierLoc);
4956 SpecTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
4957 SpecTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4958 SpecTL.setLAngleLoc(TL.getLAngleLoc());
4959 SpecTL.setRAngleLoc(TL.getRAngleLoc());
4960 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
4961 SpecTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
4963 TemplateSpecializationTypeLoc SpecTL
4964 = TLB.push<TemplateSpecializationTypeLoc>(Result);
4965 SpecTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc());
4966 SpecTL.setTemplateNameLoc(TL.getTemplateNameLoc());
4967 SpecTL.setLAngleLoc(TL.getLAngleLoc());
4968 SpecTL.setRAngleLoc(TL.getRAngleLoc());
4969 for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I)
4970 SpecTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo());
4975 template<typename Derived>
4976 QualType TreeTransform<Derived>::TransformPackExpansionType(TypeLocBuilder &TLB,
4977 PackExpansionTypeLoc TL) {
4979 = getDerived().TransformType(TLB, TL.getPatternLoc());
4980 if (Pattern.isNull())
4983 QualType Result = TL.getType();
4984 if (getDerived().AlwaysRebuild() ||
4985 Pattern != TL.getPatternLoc().getType()) {
4986 Result = getDerived().RebuildPackExpansionType(Pattern,
4987 TL.getPatternLoc().getSourceRange(),
4988 TL.getEllipsisLoc(),
4989 TL.getTypePtr()->getNumExpansions());
4990 if (Result.isNull())
4994 PackExpansionTypeLoc NewT = TLB.push<PackExpansionTypeLoc>(Result);
4995 NewT.setEllipsisLoc(TL.getEllipsisLoc());
4999 template<typename Derived>
5001 TreeTransform<Derived>::TransformObjCInterfaceType(TypeLocBuilder &TLB,
5002 ObjCInterfaceTypeLoc TL) {
5003 // ObjCInterfaceType is never dependent.
5004 TLB.pushFullCopy(TL);
5005 return TL.getType();
5008 template<typename Derived>
5010 TreeTransform<Derived>::TransformObjCObjectType(TypeLocBuilder &TLB,
5011 ObjCObjectTypeLoc TL) {
5012 // ObjCObjectType is never dependent.
5013 TLB.pushFullCopy(TL);
5014 return TL.getType();
5017 template<typename Derived>
5019 TreeTransform<Derived>::TransformObjCObjectPointerType(TypeLocBuilder &TLB,
5020 ObjCObjectPointerTypeLoc TL) {
5021 // ObjCObjectPointerType is never dependent.
5022 TLB.pushFullCopy(TL);
5023 return TL.getType();
5026 //===----------------------------------------------------------------------===//
5027 // Statement transformation
5028 //===----------------------------------------------------------------------===//
5029 template<typename Derived>
5031 TreeTransform<Derived>::TransformNullStmt(NullStmt *S) {
5032 return SemaRef.Owned(S);
5035 template<typename Derived>
5037 TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S) {
5038 return getDerived().TransformCompoundStmt(S, false);
5041 template<typename Derived>
5043 TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S,
5045 Sema::CompoundScopeRAII CompoundScope(getSema());
5047 bool SubStmtInvalid = false;
5048 bool SubStmtChanged = false;
5049 SmallVector<Stmt*, 8> Statements;
5050 for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end();
5052 StmtResult Result = getDerived().TransformStmt(*B);
5053 if (Result.isInvalid()) {
5054 // Immediately fail if this was a DeclStmt, since it's very
5055 // likely that this will cause problems for future statements.
5056 if (isa<DeclStmt>(*B))
5059 // Otherwise, just keep processing substatements and fail later.
5060 SubStmtInvalid = true;
5064 SubStmtChanged = SubStmtChanged || Result.get() != *B;
5065 Statements.push_back(Result.takeAs<Stmt>());
5071 if (!getDerived().AlwaysRebuild() &&
5073 return SemaRef.Owned(S);
5075 return getDerived().RebuildCompoundStmt(S->getLBracLoc(),
5081 template<typename Derived>
5083 TreeTransform<Derived>::TransformCaseStmt(CaseStmt *S) {
5084 ExprResult LHS, RHS;
5086 EnterExpressionEvaluationContext Unevaluated(SemaRef,
5087 Sema::ConstantEvaluated);
5089 // Transform the left-hand case value.
5090 LHS = getDerived().TransformExpr(S->getLHS());
5091 LHS = SemaRef.ActOnConstantExpression(LHS);
5092 if (LHS.isInvalid())
5095 // Transform the right-hand case value (for the GNU case-range extension).
5096 RHS = getDerived().TransformExpr(S->getRHS());
5097 RHS = SemaRef.ActOnConstantExpression(RHS);
5098 if (RHS.isInvalid())
5102 // Build the case statement.
5103 // Case statements are always rebuilt so that they will attached to their
5104 // transformed switch statement.
5105 StmtResult Case = getDerived().RebuildCaseStmt(S->getCaseLoc(),
5107 S->getEllipsisLoc(),
5110 if (Case.isInvalid())
5113 // Transform the statement following the case
5114 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
5115 if (SubStmt.isInvalid())
5118 // Attach the body to the case statement
5119 return getDerived().RebuildCaseStmtBody(Case.get(), SubStmt.get());
5122 template<typename Derived>
5124 TreeTransform<Derived>::TransformDefaultStmt(DefaultStmt *S) {
5125 // Transform the statement following the default case
5126 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
5127 if (SubStmt.isInvalid())
5130 // Default statements are always rebuilt
5131 return getDerived().RebuildDefaultStmt(S->getDefaultLoc(), S->getColonLoc(),
5135 template<typename Derived>
5137 TreeTransform<Derived>::TransformLabelStmt(LabelStmt *S) {
5138 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
5139 if (SubStmt.isInvalid())
5142 Decl *LD = getDerived().TransformDecl(S->getDecl()->getLocation(),
5148 // FIXME: Pass the real colon location in.
5149 return getDerived().RebuildLabelStmt(S->getIdentLoc(),
5150 cast<LabelDecl>(LD), SourceLocation(),
5154 template<typename Derived>
5156 TreeTransform<Derived>::TransformAttributedStmt(AttributedStmt *S) {
5157 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
5158 if (SubStmt.isInvalid())
5161 // TODO: transform attributes
5162 if (SubStmt.get() == S->getSubStmt() /* && attrs are the same */)
5165 return getDerived().RebuildAttributedStmt(S->getAttrLoc(),
5170 template<typename Derived>
5172 TreeTransform<Derived>::TransformIfStmt(IfStmt *S) {
5173 // Transform the condition
5175 VarDecl *ConditionVar = 0;
5176 if (S->getConditionVariable()) {
5178 = cast_or_null<VarDecl>(
5179 getDerived().TransformDefinition(
5180 S->getConditionVariable()->getLocation(),
5181 S->getConditionVariable()));
5185 Cond = getDerived().TransformExpr(S->getCond());
5187 if (Cond.isInvalid())
5190 // Convert the condition to a boolean value.
5192 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getIfLoc(),
5194 if (CondE.isInvalid())
5201 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5202 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
5205 // Transform the "then" branch.
5206 StmtResult Then = getDerived().TransformStmt(S->getThen());
5207 if (Then.isInvalid())
5210 // Transform the "else" branch.
5211 StmtResult Else = getDerived().TransformStmt(S->getElse());
5212 if (Else.isInvalid())
5215 if (!getDerived().AlwaysRebuild() &&
5216 FullCond.get() == S->getCond() &&
5217 ConditionVar == S->getConditionVariable() &&
5218 Then.get() == S->getThen() &&
5219 Else.get() == S->getElse())
5220 return SemaRef.Owned(S);
5222 return getDerived().RebuildIfStmt(S->getIfLoc(), FullCond, ConditionVar,
5224 S->getElseLoc(), Else.get());
5227 template<typename Derived>
5229 TreeTransform<Derived>::TransformSwitchStmt(SwitchStmt *S) {
5230 // Transform the condition.
5232 VarDecl *ConditionVar = 0;
5233 if (S->getConditionVariable()) {
5235 = cast_or_null<VarDecl>(
5236 getDerived().TransformDefinition(
5237 S->getConditionVariable()->getLocation(),
5238 S->getConditionVariable()));
5242 Cond = getDerived().TransformExpr(S->getCond());
5244 if (Cond.isInvalid())
5248 // Rebuild the switch statement.
5250 = getDerived().RebuildSwitchStmtStart(S->getSwitchLoc(), Cond.get(),
5252 if (Switch.isInvalid())
5255 // Transform the body of the switch statement.
5256 StmtResult Body = getDerived().TransformStmt(S->getBody());
5257 if (Body.isInvalid())
5260 // Complete the switch statement.
5261 return getDerived().RebuildSwitchStmtBody(S->getSwitchLoc(), Switch.get(),
5265 template<typename Derived>
5267 TreeTransform<Derived>::TransformWhileStmt(WhileStmt *S) {
5268 // Transform the condition
5270 VarDecl *ConditionVar = 0;
5271 if (S->getConditionVariable()) {
5273 = cast_or_null<VarDecl>(
5274 getDerived().TransformDefinition(
5275 S->getConditionVariable()->getLocation(),
5276 S->getConditionVariable()));
5280 Cond = getDerived().TransformExpr(S->getCond());
5282 if (Cond.isInvalid())
5286 // Convert the condition to a boolean value.
5287 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getWhileLoc(),
5289 if (CondE.isInvalid())
5295 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5296 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
5299 // Transform the body
5300 StmtResult Body = getDerived().TransformStmt(S->getBody());
5301 if (Body.isInvalid())
5304 if (!getDerived().AlwaysRebuild() &&
5305 FullCond.get() == S->getCond() &&
5306 ConditionVar == S->getConditionVariable() &&
5307 Body.get() == S->getBody())
5310 return getDerived().RebuildWhileStmt(S->getWhileLoc(), FullCond,
5311 ConditionVar, Body.get());
5314 template<typename Derived>
5316 TreeTransform<Derived>::TransformDoStmt(DoStmt *S) {
5317 // Transform the body
5318 StmtResult Body = getDerived().TransformStmt(S->getBody());
5319 if (Body.isInvalid())
5322 // Transform the condition
5323 ExprResult Cond = getDerived().TransformExpr(S->getCond());
5324 if (Cond.isInvalid())
5327 if (!getDerived().AlwaysRebuild() &&
5328 Cond.get() == S->getCond() &&
5329 Body.get() == S->getBody())
5330 return SemaRef.Owned(S);
5332 return getDerived().RebuildDoStmt(S->getDoLoc(), Body.get(), S->getWhileLoc(),
5333 /*FIXME:*/S->getWhileLoc(), Cond.get(),
5337 template<typename Derived>
5339 TreeTransform<Derived>::TransformForStmt(ForStmt *S) {
5340 // Transform the initialization statement
5341 StmtResult Init = getDerived().TransformStmt(S->getInit());
5342 if (Init.isInvalid())
5345 // Transform the condition
5347 VarDecl *ConditionVar = 0;
5348 if (S->getConditionVariable()) {
5350 = cast_or_null<VarDecl>(
5351 getDerived().TransformDefinition(
5352 S->getConditionVariable()->getLocation(),
5353 S->getConditionVariable()));
5357 Cond = getDerived().TransformExpr(S->getCond());
5359 if (Cond.isInvalid())
5363 // Convert the condition to a boolean value.
5364 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getForLoc(),
5366 if (CondE.isInvalid())
5373 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
5374 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
5377 // Transform the increment
5378 ExprResult Inc = getDerived().TransformExpr(S->getInc());
5379 if (Inc.isInvalid())
5382 Sema::FullExprArg FullInc(getSema().MakeFullExpr(Inc.get()));
5383 if (S->getInc() && !FullInc.get())
5386 // Transform the body
5387 StmtResult Body = getDerived().TransformStmt(S->getBody());
5388 if (Body.isInvalid())
5391 if (!getDerived().AlwaysRebuild() &&
5392 Init.get() == S->getInit() &&
5393 FullCond.get() == S->getCond() &&
5394 Inc.get() == S->getInc() &&
5395 Body.get() == S->getBody())
5396 return SemaRef.Owned(S);
5398 return getDerived().RebuildForStmt(S->getForLoc(), S->getLParenLoc(),
5399 Init.get(), FullCond, ConditionVar,
5400 FullInc, S->getRParenLoc(), Body.get());
5403 template<typename Derived>
5405 TreeTransform<Derived>::TransformGotoStmt(GotoStmt *S) {
5406 Decl *LD = getDerived().TransformDecl(S->getLabel()->getLocation(),
5411 // Goto statements must always be rebuilt, to resolve the label.
5412 return getDerived().RebuildGotoStmt(S->getGotoLoc(), S->getLabelLoc(),
5413 cast<LabelDecl>(LD));
5416 template<typename Derived>
5418 TreeTransform<Derived>::TransformIndirectGotoStmt(IndirectGotoStmt *S) {
5419 ExprResult Target = getDerived().TransformExpr(S->getTarget());
5420 if (Target.isInvalid())
5422 Target = SemaRef.MaybeCreateExprWithCleanups(Target.take());
5424 if (!getDerived().AlwaysRebuild() &&
5425 Target.get() == S->getTarget())
5426 return SemaRef.Owned(S);
5428 return getDerived().RebuildIndirectGotoStmt(S->getGotoLoc(), S->getStarLoc(),
5432 template<typename Derived>
5434 TreeTransform<Derived>::TransformContinueStmt(ContinueStmt *S) {
5435 return SemaRef.Owned(S);
5438 template<typename Derived>
5440 TreeTransform<Derived>::TransformBreakStmt(BreakStmt *S) {
5441 return SemaRef.Owned(S);
5444 template<typename Derived>
5446 TreeTransform<Derived>::TransformReturnStmt(ReturnStmt *S) {
5447 ExprResult Result = getDerived().TransformExpr(S->getRetValue());
5448 if (Result.isInvalid())
5451 // FIXME: We always rebuild the return statement because there is no way
5452 // to tell whether the return type of the function has changed.
5453 return getDerived().RebuildReturnStmt(S->getReturnLoc(), Result.get());
5456 template<typename Derived>
5458 TreeTransform<Derived>::TransformDeclStmt(DeclStmt *S) {
5459 bool DeclChanged = false;
5460 SmallVector<Decl *, 4> Decls;
5461 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
5463 Decl *Transformed = getDerived().TransformDefinition((*D)->getLocation(),
5468 if (Transformed != *D)
5471 Decls.push_back(Transformed);
5474 if (!getDerived().AlwaysRebuild() && !DeclChanged)
5475 return SemaRef.Owned(S);
5477 return getDerived().RebuildDeclStmt(Decls.data(), Decls.size(),
5478 S->getStartLoc(), S->getEndLoc());
5481 template<typename Derived>
5483 TreeTransform<Derived>::TransformGCCAsmStmt(GCCAsmStmt *S) {
5485 SmallVector<Expr*, 8> Constraints;
5486 SmallVector<Expr*, 8> Exprs;
5487 SmallVector<IdentifierInfo *, 4> Names;
5489 ExprResult AsmString;
5490 SmallVector<Expr*, 8> Clobbers;
5492 bool ExprsChanged = false;
5494 // Go through the outputs.
5495 for (unsigned I = 0, E = S->getNumOutputs(); I != E; ++I) {
5496 Names.push_back(S->getOutputIdentifier(I));
5498 // No need to transform the constraint literal.
5499 Constraints.push_back(S->getOutputConstraintLiteral(I));
5501 // Transform the output expr.
5502 Expr *OutputExpr = S->getOutputExpr(I);
5503 ExprResult Result = getDerived().TransformExpr(OutputExpr);
5504 if (Result.isInvalid())
5507 ExprsChanged |= Result.get() != OutputExpr;
5509 Exprs.push_back(Result.get());
5512 // Go through the inputs.
5513 for (unsigned I = 0, E = S->getNumInputs(); I != E; ++I) {
5514 Names.push_back(S->getInputIdentifier(I));
5516 // No need to transform the constraint literal.
5517 Constraints.push_back(S->getInputConstraintLiteral(I));
5519 // Transform the input expr.
5520 Expr *InputExpr = S->getInputExpr(I);
5521 ExprResult Result = getDerived().TransformExpr(InputExpr);
5522 if (Result.isInvalid())
5525 ExprsChanged |= Result.get() != InputExpr;
5527 Exprs.push_back(Result.get());
5530 if (!getDerived().AlwaysRebuild() && !ExprsChanged)
5531 return SemaRef.Owned(S);
5533 // Go through the clobbers.
5534 for (unsigned I = 0, E = S->getNumClobbers(); I != E; ++I)
5535 Clobbers.push_back(S->getClobberStringLiteral(I));
5537 // No need to transform the asm string literal.
5538 AsmString = SemaRef.Owned(S->getAsmString());
5539 return getDerived().RebuildGCCAsmStmt(S->getAsmLoc(), S->isSimple(),
5540 S->isVolatile(), S->getNumOutputs(),
5541 S->getNumInputs(), Names.data(),
5542 Constraints, Exprs, AsmString.get(),
5543 Clobbers, S->getRParenLoc());
5546 template<typename Derived>
5548 TreeTransform<Derived>::TransformMSAsmStmt(MSAsmStmt *S) {
5549 ArrayRef<Token> AsmToks =
5550 llvm::makeArrayRef(S->getAsmToks(), S->getNumAsmToks());
5552 return getDerived().RebuildMSAsmStmt(S->getAsmLoc(), S->getLBraceLoc(),
5553 AsmToks, S->getEndLoc());
5556 template<typename Derived>
5558 TreeTransform<Derived>::TransformObjCAtTryStmt(ObjCAtTryStmt *S) {
5559 // Transform the body of the @try.
5560 StmtResult TryBody = getDerived().TransformStmt(S->getTryBody());
5561 if (TryBody.isInvalid())
5564 // Transform the @catch statements (if present).
5565 bool AnyCatchChanged = false;
5566 SmallVector<Stmt*, 8> CatchStmts;
5567 for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
5568 StmtResult Catch = getDerived().TransformStmt(S->getCatchStmt(I));
5569 if (Catch.isInvalid())
5571 if (Catch.get() != S->getCatchStmt(I))
5572 AnyCatchChanged = true;
5573 CatchStmts.push_back(Catch.release());
5576 // Transform the @finally statement (if present).
5578 if (S->getFinallyStmt()) {
5579 Finally = getDerived().TransformStmt(S->getFinallyStmt());
5580 if (Finally.isInvalid())
5584 // If nothing changed, just retain this statement.
5585 if (!getDerived().AlwaysRebuild() &&
5586 TryBody.get() == S->getTryBody() &&
5588 Finally.get() == S->getFinallyStmt())
5589 return SemaRef.Owned(S);
5591 // Build a new statement.
5592 return getDerived().RebuildObjCAtTryStmt(S->getAtTryLoc(), TryBody.get(),
5593 CatchStmts, Finally.get());
5596 template<typename Derived>
5598 TreeTransform<Derived>::TransformObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5599 // Transform the @catch parameter, if there is one.
5601 if (VarDecl *FromVar = S->getCatchParamDecl()) {
5602 TypeSourceInfo *TSInfo = 0;
5603 if (FromVar->getTypeSourceInfo()) {
5604 TSInfo = getDerived().TransformType(FromVar->getTypeSourceInfo());
5611 T = TSInfo->getType();
5613 T = getDerived().TransformType(FromVar->getType());
5618 Var = getDerived().RebuildObjCExceptionDecl(FromVar, TSInfo, T);
5623 StmtResult Body = getDerived().TransformStmt(S->getCatchBody());
5624 if (Body.isInvalid())
5627 return getDerived().RebuildObjCAtCatchStmt(S->getAtCatchLoc(),
5632 template<typename Derived>
5634 TreeTransform<Derived>::TransformObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5635 // Transform the body.
5636 StmtResult Body = getDerived().TransformStmt(S->getFinallyBody());
5637 if (Body.isInvalid())
5640 // If nothing changed, just retain this statement.
5641 if (!getDerived().AlwaysRebuild() &&
5642 Body.get() == S->getFinallyBody())
5643 return SemaRef.Owned(S);
5645 // Build a new statement.
5646 return getDerived().RebuildObjCAtFinallyStmt(S->getAtFinallyLoc(),
5650 template<typename Derived>
5652 TreeTransform<Derived>::TransformObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5654 if (S->getThrowExpr()) {
5655 Operand = getDerived().TransformExpr(S->getThrowExpr());
5656 if (Operand.isInvalid())
5660 if (!getDerived().AlwaysRebuild() &&
5661 Operand.get() == S->getThrowExpr())
5662 return getSema().Owned(S);
5664 return getDerived().RebuildObjCAtThrowStmt(S->getThrowLoc(), Operand.get());
5667 template<typename Derived>
5669 TreeTransform<Derived>::TransformObjCAtSynchronizedStmt(
5670 ObjCAtSynchronizedStmt *S) {
5671 // Transform the object we are locking.
5672 ExprResult Object = getDerived().TransformExpr(S->getSynchExpr());
5673 if (Object.isInvalid())
5676 getDerived().RebuildObjCAtSynchronizedOperand(S->getAtSynchronizedLoc(),
5678 if (Object.isInvalid())
5681 // Transform the body.
5682 StmtResult Body = getDerived().TransformStmt(S->getSynchBody());
5683 if (Body.isInvalid())
5686 // If nothing change, just retain the current statement.
5687 if (!getDerived().AlwaysRebuild() &&
5688 Object.get() == S->getSynchExpr() &&
5689 Body.get() == S->getSynchBody())
5690 return SemaRef.Owned(S);
5692 // Build a new statement.
5693 return getDerived().RebuildObjCAtSynchronizedStmt(S->getAtSynchronizedLoc(),
5694 Object.get(), Body.get());
5697 template<typename Derived>
5699 TreeTransform<Derived>::TransformObjCAutoreleasePoolStmt(
5700 ObjCAutoreleasePoolStmt *S) {
5701 // Transform the body.
5702 StmtResult Body = getDerived().TransformStmt(S->getSubStmt());
5703 if (Body.isInvalid())
5706 // If nothing changed, just retain this statement.
5707 if (!getDerived().AlwaysRebuild() &&
5708 Body.get() == S->getSubStmt())
5709 return SemaRef.Owned(S);
5711 // Build a new statement.
5712 return getDerived().RebuildObjCAutoreleasePoolStmt(
5713 S->getAtLoc(), Body.get());
5716 template<typename Derived>
5718 TreeTransform<Derived>::TransformObjCForCollectionStmt(
5719 ObjCForCollectionStmt *S) {
5720 // Transform the element statement.
5721 StmtResult Element = getDerived().TransformStmt(S->getElement());
5722 if (Element.isInvalid())
5725 // Transform the collection expression.
5726 ExprResult Collection = getDerived().TransformExpr(S->getCollection());
5727 if (Collection.isInvalid())
5730 // Transform the body.
5731 StmtResult Body = getDerived().TransformStmt(S->getBody());
5732 if (Body.isInvalid())
5735 // If nothing changed, just retain this statement.
5736 if (!getDerived().AlwaysRebuild() &&
5737 Element.get() == S->getElement() &&
5738 Collection.get() == S->getCollection() &&
5739 Body.get() == S->getBody())
5740 return SemaRef.Owned(S);
5742 // Build a new statement.
5743 return getDerived().RebuildObjCForCollectionStmt(S->getForLoc(),
5751 template<typename Derived>
5753 TreeTransform<Derived>::TransformCXXCatchStmt(CXXCatchStmt *S) {
5754 // Transform the exception declaration, if any.
5756 if (S->getExceptionDecl()) {
5757 VarDecl *ExceptionDecl = S->getExceptionDecl();
5758 TypeSourceInfo *T = getDerived().TransformType(
5759 ExceptionDecl->getTypeSourceInfo());
5763 Var = getDerived().RebuildExceptionDecl(ExceptionDecl, T,
5764 ExceptionDecl->getInnerLocStart(),
5765 ExceptionDecl->getLocation(),
5766 ExceptionDecl->getIdentifier());
5767 if (!Var || Var->isInvalidDecl())
5771 // Transform the actual exception handler.
5772 StmtResult Handler = getDerived().TransformStmt(S->getHandlerBlock());
5773 if (Handler.isInvalid())
5776 if (!getDerived().AlwaysRebuild() &&
5778 Handler.get() == S->getHandlerBlock())
5779 return SemaRef.Owned(S);
5781 return getDerived().RebuildCXXCatchStmt(S->getCatchLoc(),
5786 template<typename Derived>
5788 TreeTransform<Derived>::TransformCXXTryStmt(CXXTryStmt *S) {
5789 // Transform the try block itself.
5791 = getDerived().TransformCompoundStmt(S->getTryBlock());
5792 if (TryBlock.isInvalid())
5795 // Transform the handlers.
5796 bool HandlerChanged = false;
5797 SmallVector<Stmt*, 8> Handlers;
5798 for (unsigned I = 0, N = S->getNumHandlers(); I != N; ++I) {
5800 = getDerived().TransformCXXCatchStmt(S->getHandler(I));
5801 if (Handler.isInvalid())
5804 HandlerChanged = HandlerChanged || Handler.get() != S->getHandler(I);
5805 Handlers.push_back(Handler.takeAs<Stmt>());
5808 if (!getDerived().AlwaysRebuild() &&
5809 TryBlock.get() == S->getTryBlock() &&
5811 return SemaRef.Owned(S);
5813 return getDerived().RebuildCXXTryStmt(S->getTryLoc(), TryBlock.get(),
5817 template<typename Derived>
5819 TreeTransform<Derived>::TransformCXXForRangeStmt(CXXForRangeStmt *S) {
5820 StmtResult Range = getDerived().TransformStmt(S->getRangeStmt());
5821 if (Range.isInvalid())
5824 StmtResult BeginEnd = getDerived().TransformStmt(S->getBeginEndStmt());
5825 if (BeginEnd.isInvalid())
5828 ExprResult Cond = getDerived().TransformExpr(S->getCond());
5829 if (Cond.isInvalid())
5832 Cond = SemaRef.CheckBooleanCondition(Cond.take(), S->getColonLoc());
5833 if (Cond.isInvalid())
5836 Cond = SemaRef.MaybeCreateExprWithCleanups(Cond.take());
5838 ExprResult Inc = getDerived().TransformExpr(S->getInc());
5839 if (Inc.isInvalid())
5842 Inc = SemaRef.MaybeCreateExprWithCleanups(Inc.take());
5844 StmtResult LoopVar = getDerived().TransformStmt(S->getLoopVarStmt());
5845 if (LoopVar.isInvalid())
5848 StmtResult NewStmt = S;
5849 if (getDerived().AlwaysRebuild() ||
5850 Range.get() != S->getRangeStmt() ||
5851 BeginEnd.get() != S->getBeginEndStmt() ||
5852 Cond.get() != S->getCond() ||
5853 Inc.get() != S->getInc() ||
5854 LoopVar.get() != S->getLoopVarStmt())
5855 NewStmt = getDerived().RebuildCXXForRangeStmt(S->getForLoc(),
5856 S->getColonLoc(), Range.get(),
5857 BeginEnd.get(), Cond.get(),
5858 Inc.get(), LoopVar.get(),
5861 StmtResult Body = getDerived().TransformStmt(S->getBody());
5862 if (Body.isInvalid())
5865 // Body has changed but we didn't rebuild the for-range statement. Rebuild
5866 // it now so we have a new statement to attach the body to.
5867 if (Body.get() != S->getBody() && NewStmt.get() == S)
5868 NewStmt = getDerived().RebuildCXXForRangeStmt(S->getForLoc(),
5869 S->getColonLoc(), Range.get(),
5870 BeginEnd.get(), Cond.get(),
5871 Inc.get(), LoopVar.get(),
5874 if (NewStmt.get() == S)
5875 return SemaRef.Owned(S);
5877 return FinishCXXForRangeStmt(NewStmt.get(), Body.get());
5880 template<typename Derived>
5882 TreeTransform<Derived>::TransformMSDependentExistsStmt(
5883 MSDependentExistsStmt *S) {
5884 // Transform the nested-name-specifier, if any.
5885 NestedNameSpecifierLoc QualifierLoc;
5886 if (S->getQualifierLoc()) {
5888 = getDerived().TransformNestedNameSpecifierLoc(S->getQualifierLoc());
5893 // Transform the declaration name.
5894 DeclarationNameInfo NameInfo = S->getNameInfo();
5895 if (NameInfo.getName()) {
5896 NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo);
5897 if (!NameInfo.getName())
5901 // Check whether anything changed.
5902 if (!getDerived().AlwaysRebuild() &&
5903 QualifierLoc == S->getQualifierLoc() &&
5904 NameInfo.getName() == S->getNameInfo().getName())
5907 // Determine whether this name exists, if we can.
5909 SS.Adopt(QualifierLoc);
5910 bool Dependent = false;
5911 switch (getSema().CheckMicrosoftIfExistsSymbol(/*S=*/0, SS, NameInfo)) {
5912 case Sema::IER_Exists:
5913 if (S->isIfExists())
5916 return new (getSema().Context) NullStmt(S->getKeywordLoc());
5918 case Sema::IER_DoesNotExist:
5919 if (S->isIfNotExists())
5922 return new (getSema().Context) NullStmt(S->getKeywordLoc());
5924 case Sema::IER_Dependent:
5928 case Sema::IER_Error:
5932 // We need to continue with the instantiation, so do so now.
5933 StmtResult SubStmt = getDerived().TransformCompoundStmt(S->getSubStmt());
5934 if (SubStmt.isInvalid())
5937 // If we have resolved the name, just transform to the substatement.
5941 // The name is still dependent, so build a dependent expression again.
5942 return getDerived().RebuildMSDependentExistsStmt(S->getKeywordLoc(),
5949 template<typename Derived>
5951 TreeTransform<Derived>::TransformSEHTryStmt(SEHTryStmt *S) {
5952 StmtResult TryBlock; // = getDerived().TransformCompoundStmt(S->getTryBlock());
5953 if(TryBlock.isInvalid()) return StmtError();
5955 StmtResult Handler = getDerived().TransformSEHHandler(S->getHandler());
5956 if(!getDerived().AlwaysRebuild() &&
5957 TryBlock.get() == S->getTryBlock() &&
5958 Handler.get() == S->getHandler())
5959 return SemaRef.Owned(S);
5961 return getDerived().RebuildSEHTryStmt(S->getIsCXXTry(),
5967 template<typename Derived>
5969 TreeTransform<Derived>::TransformSEHFinallyStmt(SEHFinallyStmt *S) {
5970 StmtResult Block; // = getDerived().TransformCompoundStatement(S->getBlock());
5971 if(Block.isInvalid()) return StmtError();
5973 return getDerived().RebuildSEHFinallyStmt(S->getFinallyLoc(),
5977 template<typename Derived>
5979 TreeTransform<Derived>::TransformSEHExceptStmt(SEHExceptStmt *S) {
5980 ExprResult FilterExpr = getDerived().TransformExpr(S->getFilterExpr());
5981 if(FilterExpr.isInvalid()) return StmtError();
5983 StmtResult Block; // = getDerived().TransformCompoundStatement(S->getBlock());
5984 if(Block.isInvalid()) return StmtError();
5986 return getDerived().RebuildSEHExceptStmt(S->getExceptLoc(),
5991 template<typename Derived>
5993 TreeTransform<Derived>::TransformSEHHandler(Stmt *Handler) {
5994 if(isa<SEHFinallyStmt>(Handler))
5995 return getDerived().TransformSEHFinallyStmt(cast<SEHFinallyStmt>(Handler));
5997 return getDerived().TransformSEHExceptStmt(cast<SEHExceptStmt>(Handler));
6000 //===----------------------------------------------------------------------===//
6001 // Expression transformation
6002 //===----------------------------------------------------------------------===//
6003 template<typename Derived>
6005 TreeTransform<Derived>::TransformPredefinedExpr(PredefinedExpr *E) {
6006 return SemaRef.Owned(E);
6009 template<typename Derived>
6011 TreeTransform<Derived>::TransformDeclRefExpr(DeclRefExpr *E) {
6012 NestedNameSpecifierLoc QualifierLoc;
6013 if (E->getQualifierLoc()) {
6015 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
6021 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getLocation(),
6026 DeclarationNameInfo NameInfo = E->getNameInfo();
6027 if (NameInfo.getName()) {
6028 NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo);
6029 if (!NameInfo.getName())
6033 if (!getDerived().AlwaysRebuild() &&
6034 QualifierLoc == E->getQualifierLoc() &&
6035 ND == E->getDecl() &&
6036 NameInfo.getName() == E->getDecl()->getDeclName() &&
6037 !E->hasExplicitTemplateArgs()) {
6039 // Mark it referenced in the new context regardless.
6040 // FIXME: this is a bit instantiation-specific.
6041 SemaRef.MarkDeclRefReferenced(E);
6043 return SemaRef.Owned(E);
6046 TemplateArgumentListInfo TransArgs, *TemplateArgs = 0;
6047 if (E->hasExplicitTemplateArgs()) {
6048 TemplateArgs = &TransArgs;
6049 TransArgs.setLAngleLoc(E->getLAngleLoc());
6050 TransArgs.setRAngleLoc(E->getRAngleLoc());
6051 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
6052 E->getNumTemplateArgs(),
6057 return getDerived().RebuildDeclRefExpr(QualifierLoc, ND, NameInfo,
6061 template<typename Derived>
6063 TreeTransform<Derived>::TransformIntegerLiteral(IntegerLiteral *E) {
6064 return SemaRef.Owned(E);
6067 template<typename Derived>
6069 TreeTransform<Derived>::TransformFloatingLiteral(FloatingLiteral *E) {
6070 return SemaRef.Owned(E);
6073 template<typename Derived>
6075 TreeTransform<Derived>::TransformImaginaryLiteral(ImaginaryLiteral *E) {
6076 return SemaRef.Owned(E);
6079 template<typename Derived>
6081 TreeTransform<Derived>::TransformStringLiteral(StringLiteral *E) {
6082 return SemaRef.Owned(E);
6085 template<typename Derived>
6087 TreeTransform<Derived>::TransformCharacterLiteral(CharacterLiteral *E) {
6088 return SemaRef.Owned(E);
6091 template<typename Derived>
6093 TreeTransform<Derived>::TransformUserDefinedLiteral(UserDefinedLiteral *E) {
6094 return SemaRef.MaybeBindToTemporary(E);
6097 template<typename Derived>
6099 TreeTransform<Derived>::TransformGenericSelectionExpr(GenericSelectionExpr *E) {
6100 ExprResult ControllingExpr =
6101 getDerived().TransformExpr(E->getControllingExpr());
6102 if (ControllingExpr.isInvalid())
6105 SmallVector<Expr *, 4> AssocExprs;
6106 SmallVector<TypeSourceInfo *, 4> AssocTypes;
6107 for (unsigned i = 0; i != E->getNumAssocs(); ++i) {
6108 TypeSourceInfo *TS = E->getAssocTypeSourceInfo(i);
6110 TypeSourceInfo *AssocType = getDerived().TransformType(TS);
6113 AssocTypes.push_back(AssocType);
6115 AssocTypes.push_back(0);
6118 ExprResult AssocExpr = getDerived().TransformExpr(E->getAssocExpr(i));
6119 if (AssocExpr.isInvalid())
6121 AssocExprs.push_back(AssocExpr.release());
6124 return getDerived().RebuildGenericSelectionExpr(E->getGenericLoc(),
6127 ControllingExpr.release(),
6133 template<typename Derived>
6135 TreeTransform<Derived>::TransformParenExpr(ParenExpr *E) {
6136 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
6137 if (SubExpr.isInvalid())
6140 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr())
6141 return SemaRef.Owned(E);
6143 return getDerived().RebuildParenExpr(SubExpr.get(), E->getLParen(),
6147 /// \brief The operand of a unary address-of operator has special rules: it's
6148 /// allowed to refer to a non-static member of a class even if there's no 'this'
6149 /// object available.
6150 template<typename Derived>
6152 TreeTransform<Derived>::TransformAddressOfOperand(Expr *E) {
6153 if (DependentScopeDeclRefExpr *DRE = dyn_cast<DependentScopeDeclRefExpr>(E))
6154 return getDerived().TransformDependentScopeDeclRefExpr(DRE, true);
6156 return getDerived().TransformExpr(E);
6159 template<typename Derived>
6161 TreeTransform<Derived>::TransformUnaryOperator(UnaryOperator *E) {
6162 ExprResult SubExpr = TransformAddressOfOperand(E->getSubExpr());
6163 if (SubExpr.isInvalid())
6166 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr())
6167 return SemaRef.Owned(E);
6169 return getDerived().RebuildUnaryOperator(E->getOperatorLoc(),
6174 template<typename Derived>
6176 TreeTransform<Derived>::TransformOffsetOfExpr(OffsetOfExpr *E) {
6177 // Transform the type.
6178 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeSourceInfo());
6182 // Transform all of the components into components similar to what the
6184 // FIXME: It would be slightly more efficient in the non-dependent case to
6185 // just map FieldDecls, rather than requiring the rebuilder to look for
6186 // the fields again. However, __builtin_offsetof is rare enough in
6187 // template code that we don't care.
6188 bool ExprChanged = false;
6189 typedef Sema::OffsetOfComponent Component;
6190 typedef OffsetOfExpr::OffsetOfNode Node;
6191 SmallVector<Component, 4> Components;
6192 for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
6193 const Node &ON = E->getComponent(I);
6195 Comp.isBrackets = true;
6196 Comp.LocStart = ON.getSourceRange().getBegin();
6197 Comp.LocEnd = ON.getSourceRange().getEnd();
6198 switch (ON.getKind()) {
6200 Expr *FromIndex = E->getIndexExpr(ON.getArrayExprIndex());
6201 ExprResult Index = getDerived().TransformExpr(FromIndex);
6202 if (Index.isInvalid())
6205 ExprChanged = ExprChanged || Index.get() != FromIndex;
6206 Comp.isBrackets = true;
6207 Comp.U.E = Index.get();
6212 case Node::Identifier:
6213 Comp.isBrackets = false;
6214 Comp.U.IdentInfo = ON.getFieldName();
6215 if (!Comp.U.IdentInfo)
6221 // Will be recomputed during the rebuild.
6225 Components.push_back(Comp);
6228 // If nothing changed, retain the existing expression.
6229 if (!getDerived().AlwaysRebuild() &&
6230 Type == E->getTypeSourceInfo() &&
6232 return SemaRef.Owned(E);
6234 // Build a new offsetof expression.
6235 return getDerived().RebuildOffsetOfExpr(E->getOperatorLoc(), Type,
6236 Components.data(), Components.size(),
6240 template<typename Derived>
6242 TreeTransform<Derived>::TransformOpaqueValueExpr(OpaqueValueExpr *E) {
6243 assert(getDerived().AlreadyTransformed(E->getType()) &&
6244 "opaque value expression requires transformation");
6245 return SemaRef.Owned(E);
6248 template<typename Derived>
6250 TreeTransform<Derived>::TransformPseudoObjectExpr(PseudoObjectExpr *E) {
6251 // Rebuild the syntactic form. The original syntactic form has
6252 // opaque-value expressions in it, so strip those away and rebuild
6253 // the result. This is a really awful way of doing this, but the
6254 // better solution (rebuilding the semantic expressions and
6255 // rebinding OVEs as necessary) doesn't work; we'd need
6256 // TreeTransform to not strip away implicit conversions.
6257 Expr *newSyntacticForm = SemaRef.recreateSyntacticForm(E);
6258 ExprResult result = getDerived().TransformExpr(newSyntacticForm);
6259 if (result.isInvalid()) return ExprError();
6261 // If that gives us a pseudo-object result back, the pseudo-object
6262 // expression must have been an lvalue-to-rvalue conversion which we
6264 if (result.get()->hasPlaceholderType(BuiltinType::PseudoObject))
6265 result = SemaRef.checkPseudoObjectRValue(result.take());
6270 template<typename Derived>
6272 TreeTransform<Derived>::TransformUnaryExprOrTypeTraitExpr(
6273 UnaryExprOrTypeTraitExpr *E) {
6274 if (E->isArgumentType()) {
6275 TypeSourceInfo *OldT = E->getArgumentTypeInfo();
6277 TypeSourceInfo *NewT = getDerived().TransformType(OldT);
6281 if (!getDerived().AlwaysRebuild() && OldT == NewT)
6282 return SemaRef.Owned(E);
6284 return getDerived().RebuildUnaryExprOrTypeTrait(NewT, E->getOperatorLoc(),
6286 E->getSourceRange());
6289 // C++0x [expr.sizeof]p1:
6290 // The operand is either an expression, which is an unevaluated operand
6292 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated,
6293 Sema::ReuseLambdaContextDecl);
6295 ExprResult SubExpr = getDerived().TransformExpr(E->getArgumentExpr());
6296 if (SubExpr.isInvalid())
6299 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getArgumentExpr())
6300 return SemaRef.Owned(E);
6302 return getDerived().RebuildUnaryExprOrTypeTrait(SubExpr.get(),
6303 E->getOperatorLoc(),
6305 E->getSourceRange());
6308 template<typename Derived>
6310 TreeTransform<Derived>::TransformArraySubscriptExpr(ArraySubscriptExpr *E) {
6311 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
6312 if (LHS.isInvalid())
6315 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
6316 if (RHS.isInvalid())
6320 if (!getDerived().AlwaysRebuild() &&
6321 LHS.get() == E->getLHS() &&
6322 RHS.get() == E->getRHS())
6323 return SemaRef.Owned(E);
6325 return getDerived().RebuildArraySubscriptExpr(LHS.get(),
6326 /*FIXME:*/E->getLHS()->getLocStart(),
6328 E->getRBracketLoc());
6331 template<typename Derived>
6333 TreeTransform<Derived>::TransformCallExpr(CallExpr *E) {
6334 // Transform the callee.
6335 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
6336 if (Callee.isInvalid())
6339 // Transform arguments.
6340 bool ArgChanged = false;
6341 SmallVector<Expr*, 8> Args;
6342 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
6346 if (!getDerived().AlwaysRebuild() &&
6347 Callee.get() == E->getCallee() &&
6349 return SemaRef.MaybeBindToTemporary(E);
6351 // FIXME: Wrong source location information for the '('.
6352 SourceLocation FakeLParenLoc
6353 = ((Expr *)Callee.get())->getSourceRange().getBegin();
6354 return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc,
6359 template<typename Derived>
6361 TreeTransform<Derived>::TransformMemberExpr(MemberExpr *E) {
6362 ExprResult Base = getDerived().TransformExpr(E->getBase());
6363 if (Base.isInvalid())
6366 NestedNameSpecifierLoc QualifierLoc;
6367 if (E->hasQualifier()) {
6369 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
6374 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
6377 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getMemberLoc(),
6378 E->getMemberDecl()));
6382 NamedDecl *FoundDecl = E->getFoundDecl();
6383 if (FoundDecl == E->getMemberDecl()) {
6386 FoundDecl = cast_or_null<NamedDecl>(
6387 getDerived().TransformDecl(E->getMemberLoc(), FoundDecl));
6392 if (!getDerived().AlwaysRebuild() &&
6393 Base.get() == E->getBase() &&
6394 QualifierLoc == E->getQualifierLoc() &&
6395 Member == E->getMemberDecl() &&
6396 FoundDecl == E->getFoundDecl() &&
6397 !E->hasExplicitTemplateArgs()) {
6399 // Mark it referenced in the new context regardless.
6400 // FIXME: this is a bit instantiation-specific.
6401 SemaRef.MarkMemberReferenced(E);
6403 return SemaRef.Owned(E);
6406 TemplateArgumentListInfo TransArgs;
6407 if (E->hasExplicitTemplateArgs()) {
6408 TransArgs.setLAngleLoc(E->getLAngleLoc());
6409 TransArgs.setRAngleLoc(E->getRAngleLoc());
6410 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
6411 E->getNumTemplateArgs(),
6416 // FIXME: Bogus source location for the operator
6417 SourceLocation FakeOperatorLoc
6418 = SemaRef.PP.getLocForEndOfToken(E->getBase()->getSourceRange().getEnd());
6420 // FIXME: to do this check properly, we will need to preserve the
6421 // first-qualifier-in-scope here, just in case we had a dependent
6422 // base (and therefore couldn't do the check) and a
6423 // nested-name-qualifier (and therefore could do the lookup).
6424 NamedDecl *FirstQualifierInScope = 0;
6426 return getDerived().RebuildMemberExpr(Base.get(), FakeOperatorLoc,
6430 E->getMemberNameInfo(),
6433 (E->hasExplicitTemplateArgs()
6435 FirstQualifierInScope);
6438 template<typename Derived>
6440 TreeTransform<Derived>::TransformBinaryOperator(BinaryOperator *E) {
6441 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
6442 if (LHS.isInvalid())
6445 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
6446 if (RHS.isInvalid())
6449 if (!getDerived().AlwaysRebuild() &&
6450 LHS.get() == E->getLHS() &&
6451 RHS.get() == E->getRHS())
6452 return SemaRef.Owned(E);
6454 Sema::FPContractStateRAII FPContractState(getSema());
6455 getSema().FPFeatures.fp_contract = E->isFPContractable();
6457 return getDerived().RebuildBinaryOperator(E->getOperatorLoc(), E->getOpcode(),
6458 LHS.get(), RHS.get());
6461 template<typename Derived>
6463 TreeTransform<Derived>::TransformCompoundAssignOperator(
6464 CompoundAssignOperator *E) {
6465 return getDerived().TransformBinaryOperator(E);
6468 template<typename Derived>
6469 ExprResult TreeTransform<Derived>::
6470 TransformBinaryConditionalOperator(BinaryConditionalOperator *e) {
6471 // Just rebuild the common and RHS expressions and see whether we
6474 ExprResult commonExpr = getDerived().TransformExpr(e->getCommon());
6475 if (commonExpr.isInvalid())
6478 ExprResult rhs = getDerived().TransformExpr(e->getFalseExpr());
6479 if (rhs.isInvalid())
6482 if (!getDerived().AlwaysRebuild() &&
6483 commonExpr.get() == e->getCommon() &&
6484 rhs.get() == e->getFalseExpr())
6485 return SemaRef.Owned(e);
6487 return getDerived().RebuildConditionalOperator(commonExpr.take(),
6488 e->getQuestionLoc(),
6494 template<typename Derived>
6496 TreeTransform<Derived>::TransformConditionalOperator(ConditionalOperator *E) {
6497 ExprResult Cond = getDerived().TransformExpr(E->getCond());
6498 if (Cond.isInvalid())
6501 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
6502 if (LHS.isInvalid())
6505 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
6506 if (RHS.isInvalid())
6509 if (!getDerived().AlwaysRebuild() &&
6510 Cond.get() == E->getCond() &&
6511 LHS.get() == E->getLHS() &&
6512 RHS.get() == E->getRHS())
6513 return SemaRef.Owned(E);
6515 return getDerived().RebuildConditionalOperator(Cond.get(),
6516 E->getQuestionLoc(),
6522 template<typename Derived>
6524 TreeTransform<Derived>::TransformImplicitCastExpr(ImplicitCastExpr *E) {
6525 // Implicit casts are eliminated during transformation, since they
6526 // will be recomputed by semantic analysis after transformation.
6527 return getDerived().TransformExpr(E->getSubExprAsWritten());
6530 template<typename Derived>
6532 TreeTransform<Derived>::TransformCStyleCastExpr(CStyleCastExpr *E) {
6533 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6538 = getDerived().TransformExpr(E->getSubExprAsWritten());
6539 if (SubExpr.isInvalid())
6542 if (!getDerived().AlwaysRebuild() &&
6543 Type == E->getTypeInfoAsWritten() &&
6544 SubExpr.get() == E->getSubExpr())
6545 return SemaRef.Owned(E);
6547 return getDerived().RebuildCStyleCastExpr(E->getLParenLoc(),
6553 template<typename Derived>
6555 TreeTransform<Derived>::TransformCompoundLiteralExpr(CompoundLiteralExpr *E) {
6556 TypeSourceInfo *OldT = E->getTypeSourceInfo();
6557 TypeSourceInfo *NewT = getDerived().TransformType(OldT);
6561 ExprResult Init = getDerived().TransformExpr(E->getInitializer());
6562 if (Init.isInvalid())
6565 if (!getDerived().AlwaysRebuild() &&
6567 Init.get() == E->getInitializer())
6568 return SemaRef.MaybeBindToTemporary(E);
6570 // Note: the expression type doesn't necessarily match the
6571 // type-as-written, but that's okay, because it should always be
6572 // derivable from the initializer.
6574 return getDerived().RebuildCompoundLiteralExpr(E->getLParenLoc(), NewT,
6575 /*FIXME:*/E->getInitializer()->getLocEnd(),
6579 template<typename Derived>
6581 TreeTransform<Derived>::TransformExtVectorElementExpr(ExtVectorElementExpr *E) {
6582 ExprResult Base = getDerived().TransformExpr(E->getBase());
6583 if (Base.isInvalid())
6586 if (!getDerived().AlwaysRebuild() &&
6587 Base.get() == E->getBase())
6588 return SemaRef.Owned(E);
6590 // FIXME: Bad source location
6591 SourceLocation FakeOperatorLoc
6592 = SemaRef.PP.getLocForEndOfToken(E->getBase()->getLocEnd());
6593 return getDerived().RebuildExtVectorElementExpr(Base.get(), FakeOperatorLoc,
6594 E->getAccessorLoc(),
6598 template<typename Derived>
6600 TreeTransform<Derived>::TransformInitListExpr(InitListExpr *E) {
6601 bool InitChanged = false;
6603 SmallVector<Expr*, 4> Inits;
6604 if (getDerived().TransformExprs(E->getInits(), E->getNumInits(), false,
6605 Inits, &InitChanged))
6608 if (!getDerived().AlwaysRebuild() && !InitChanged)
6609 return SemaRef.Owned(E);
6611 return getDerived().RebuildInitList(E->getLBraceLoc(), Inits,
6612 E->getRBraceLoc(), E->getType());
6615 template<typename Derived>
6617 TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
6620 // transform the initializer value
6621 ExprResult Init = getDerived().TransformExpr(E->getInit());
6622 if (Init.isInvalid())
6625 // transform the designators.
6626 SmallVector<Expr*, 4> ArrayExprs;
6627 bool ExprChanged = false;
6628 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
6629 DEnd = E->designators_end();
6631 if (D->isFieldDesignator()) {
6632 Desig.AddDesignator(Designator::getField(D->getFieldName(),
6638 if (D->isArrayDesignator()) {
6639 ExprResult Index = getDerived().TransformExpr(E->getArrayIndex(*D));
6640 if (Index.isInvalid())
6643 Desig.AddDesignator(Designator::getArray(Index.get(),
6644 D->getLBracketLoc()));
6646 ExprChanged = ExprChanged || Init.get() != E->getArrayIndex(*D);
6647 ArrayExprs.push_back(Index.release());
6651 assert(D->isArrayRangeDesignator() && "New kind of designator?");
6653 = getDerived().TransformExpr(E->getArrayRangeStart(*D));
6654 if (Start.isInvalid())
6657 ExprResult End = getDerived().TransformExpr(E->getArrayRangeEnd(*D));
6658 if (End.isInvalid())
6661 Desig.AddDesignator(Designator::getArrayRange(Start.get(),
6663 D->getLBracketLoc(),
6664 D->getEllipsisLoc()));
6666 ExprChanged = ExprChanged || Start.get() != E->getArrayRangeStart(*D) ||
6667 End.get() != E->getArrayRangeEnd(*D);
6669 ArrayExprs.push_back(Start.release());
6670 ArrayExprs.push_back(End.release());
6673 if (!getDerived().AlwaysRebuild() &&
6674 Init.get() == E->getInit() &&
6676 return SemaRef.Owned(E);
6678 return getDerived().RebuildDesignatedInitExpr(Desig, ArrayExprs,
6679 E->getEqualOrColonLoc(),
6680 E->usesGNUSyntax(), Init.get());
6683 template<typename Derived>
6685 TreeTransform<Derived>::TransformImplicitValueInitExpr(
6686 ImplicitValueInitExpr *E) {
6687 TemporaryBase Rebase(*this, E->getLocStart(), DeclarationName());
6689 // FIXME: Will we ever have proper type location here? Will we actually
6690 // need to transform the type?
6691 QualType T = getDerived().TransformType(E->getType());
6695 if (!getDerived().AlwaysRebuild() &&
6697 return SemaRef.Owned(E);
6699 return getDerived().RebuildImplicitValueInitExpr(T);
6702 template<typename Derived>
6704 TreeTransform<Derived>::TransformVAArgExpr(VAArgExpr *E) {
6705 TypeSourceInfo *TInfo = getDerived().TransformType(E->getWrittenTypeInfo());
6709 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
6710 if (SubExpr.isInvalid())
6713 if (!getDerived().AlwaysRebuild() &&
6714 TInfo == E->getWrittenTypeInfo() &&
6715 SubExpr.get() == E->getSubExpr())
6716 return SemaRef.Owned(E);
6718 return getDerived().RebuildVAArgExpr(E->getBuiltinLoc(), SubExpr.get(),
6719 TInfo, E->getRParenLoc());
6722 template<typename Derived>
6724 TreeTransform<Derived>::TransformParenListExpr(ParenListExpr *E) {
6725 bool ArgumentChanged = false;
6726 SmallVector<Expr*, 4> Inits;
6727 if (TransformExprs(E->getExprs(), E->getNumExprs(), true, Inits,
6731 return getDerived().RebuildParenListExpr(E->getLParenLoc(),
6736 /// \brief Transform an address-of-label expression.
6738 /// By default, the transformation of an address-of-label expression always
6739 /// rebuilds the expression, so that the label identifier can be resolved to
6740 /// the corresponding label statement by semantic analysis.
6741 template<typename Derived>
6743 TreeTransform<Derived>::TransformAddrLabelExpr(AddrLabelExpr *E) {
6744 Decl *LD = getDerived().TransformDecl(E->getLabel()->getLocation(),
6749 return getDerived().RebuildAddrLabelExpr(E->getAmpAmpLoc(), E->getLabelLoc(),
6750 cast<LabelDecl>(LD));
6753 template<typename Derived>
6755 TreeTransform<Derived>::TransformStmtExpr(StmtExpr *E) {
6756 SemaRef.ActOnStartStmtExpr();
6758 = getDerived().TransformCompoundStmt(E->getSubStmt(), true);
6759 if (SubStmt.isInvalid()) {
6760 SemaRef.ActOnStmtExprError();
6764 if (!getDerived().AlwaysRebuild() &&
6765 SubStmt.get() == E->getSubStmt()) {
6766 // Calling this an 'error' is unintuitive, but it does the right thing.
6767 SemaRef.ActOnStmtExprError();
6768 return SemaRef.MaybeBindToTemporary(E);
6771 return getDerived().RebuildStmtExpr(E->getLParenLoc(),
6776 template<typename Derived>
6778 TreeTransform<Derived>::TransformChooseExpr(ChooseExpr *E) {
6779 ExprResult Cond = getDerived().TransformExpr(E->getCond());
6780 if (Cond.isInvalid())
6783 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
6784 if (LHS.isInvalid())
6787 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
6788 if (RHS.isInvalid())
6791 if (!getDerived().AlwaysRebuild() &&
6792 Cond.get() == E->getCond() &&
6793 LHS.get() == E->getLHS() &&
6794 RHS.get() == E->getRHS())
6795 return SemaRef.Owned(E);
6797 return getDerived().RebuildChooseExpr(E->getBuiltinLoc(),
6798 Cond.get(), LHS.get(), RHS.get(),
6802 template<typename Derived>
6804 TreeTransform<Derived>::TransformGNUNullExpr(GNUNullExpr *E) {
6805 return SemaRef.Owned(E);
6808 template<typename Derived>
6810 TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
6811 switch (E->getOperator()) {
6815 case OO_Array_Delete:
6816 llvm_unreachable("new and delete operators cannot use CXXOperatorCallExpr");
6819 // This is a call to an object's operator().
6820 assert(E->getNumArgs() >= 1 && "Object call is missing arguments");
6822 // Transform the object itself.
6823 ExprResult Object = getDerived().TransformExpr(E->getArg(0));
6824 if (Object.isInvalid())
6827 // FIXME: Poor location information
6828 SourceLocation FakeLParenLoc
6829 = SemaRef.PP.getLocForEndOfToken(
6830 static_cast<Expr *>(Object.get())->getLocEnd());
6832 // Transform the call arguments.
6833 SmallVector<Expr*, 8> Args;
6834 if (getDerived().TransformExprs(E->getArgs() + 1, E->getNumArgs() - 1, true,
6838 return getDerived().RebuildCallExpr(Object.get(), FakeLParenLoc,
6843 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
6845 #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
6846 #include "clang/Basic/OperatorKinds.def"
6851 case OO_Conditional:
6852 llvm_unreachable("conditional operator is not actually overloadable");
6855 case NUM_OVERLOADED_OPERATORS:
6856 llvm_unreachable("not an overloaded operator?");
6859 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
6860 if (Callee.isInvalid())
6864 if (E->getOperator() == OO_Amp)
6865 First = getDerived().TransformAddressOfOperand(E->getArg(0));
6867 First = getDerived().TransformExpr(E->getArg(0));
6868 if (First.isInvalid())
6872 if (E->getNumArgs() == 2) {
6873 Second = getDerived().TransformExpr(E->getArg(1));
6874 if (Second.isInvalid())
6878 if (!getDerived().AlwaysRebuild() &&
6879 Callee.get() == E->getCallee() &&
6880 First.get() == E->getArg(0) &&
6881 (E->getNumArgs() != 2 || Second.get() == E->getArg(1)))
6882 return SemaRef.MaybeBindToTemporary(E);
6884 Sema::FPContractStateRAII FPContractState(getSema());
6885 getSema().FPFeatures.fp_contract = E->isFPContractable();
6887 return getDerived().RebuildCXXOperatorCallExpr(E->getOperator(),
6888 E->getOperatorLoc(),
6894 template<typename Derived>
6896 TreeTransform<Derived>::TransformCXXMemberCallExpr(CXXMemberCallExpr *E) {
6897 return getDerived().TransformCallExpr(E);
6900 template<typename Derived>
6902 TreeTransform<Derived>::TransformCUDAKernelCallExpr(CUDAKernelCallExpr *E) {
6903 // Transform the callee.
6904 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
6905 if (Callee.isInvalid())
6908 // Transform exec config.
6909 ExprResult EC = getDerived().TransformCallExpr(E->getConfig());
6913 // Transform arguments.
6914 bool ArgChanged = false;
6915 SmallVector<Expr*, 8> Args;
6916 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
6920 if (!getDerived().AlwaysRebuild() &&
6921 Callee.get() == E->getCallee() &&
6923 return SemaRef.MaybeBindToTemporary(E);
6925 // FIXME: Wrong source location information for the '('.
6926 SourceLocation FakeLParenLoc
6927 = ((Expr *)Callee.get())->getSourceRange().getBegin();
6928 return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc,
6930 E->getRParenLoc(), EC.get());
6933 template<typename Derived>
6935 TreeTransform<Derived>::TransformCXXNamedCastExpr(CXXNamedCastExpr *E) {
6936 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6941 = getDerived().TransformExpr(E->getSubExprAsWritten());
6942 if (SubExpr.isInvalid())
6945 if (!getDerived().AlwaysRebuild() &&
6946 Type == E->getTypeInfoAsWritten() &&
6947 SubExpr.get() == E->getSubExpr())
6948 return SemaRef.Owned(E);
6950 // FIXME: Poor source location information here.
6951 SourceLocation FakeLAngleLoc
6952 = SemaRef.PP.getLocForEndOfToken(E->getOperatorLoc());
6953 SourceLocation FakeRAngleLoc = E->getSubExpr()->getSourceRange().getBegin();
6954 return getDerived().RebuildCXXNamedCastExpr(E->getOperatorLoc(),
6964 template<typename Derived>
6966 TreeTransform<Derived>::TransformCXXStaticCastExpr(CXXStaticCastExpr *E) {
6967 return getDerived().TransformCXXNamedCastExpr(E);
6970 template<typename Derived>
6972 TreeTransform<Derived>::TransformCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
6973 return getDerived().TransformCXXNamedCastExpr(E);
6976 template<typename Derived>
6978 TreeTransform<Derived>::TransformCXXReinterpretCastExpr(
6979 CXXReinterpretCastExpr *E) {
6980 return getDerived().TransformCXXNamedCastExpr(E);
6983 template<typename Derived>
6985 TreeTransform<Derived>::TransformCXXConstCastExpr(CXXConstCastExpr *E) {
6986 return getDerived().TransformCXXNamedCastExpr(E);
6989 template<typename Derived>
6991 TreeTransform<Derived>::TransformCXXFunctionalCastExpr(
6992 CXXFunctionalCastExpr *E) {
6993 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
6998 = getDerived().TransformExpr(E->getSubExprAsWritten());
6999 if (SubExpr.isInvalid())
7002 if (!getDerived().AlwaysRebuild() &&
7003 Type == E->getTypeInfoAsWritten() &&
7004 SubExpr.get() == E->getSubExpr())
7005 return SemaRef.Owned(E);
7007 return getDerived().RebuildCXXFunctionalCastExpr(Type,
7008 /*FIXME:*/E->getSubExpr()->getLocStart(),
7013 template<typename Derived>
7015 TreeTransform<Derived>::TransformCXXTypeidExpr(CXXTypeidExpr *E) {
7016 if (E->isTypeOperand()) {
7017 TypeSourceInfo *TInfo
7018 = getDerived().TransformType(E->getTypeOperandSourceInfo());
7022 if (!getDerived().AlwaysRebuild() &&
7023 TInfo == E->getTypeOperandSourceInfo())
7024 return SemaRef.Owned(E);
7026 return getDerived().RebuildCXXTypeidExpr(E->getType(),
7032 // We don't know whether the subexpression is potentially evaluated until
7033 // after we perform semantic analysis. We speculatively assume it is
7034 // unevaluated; it will get fixed later if the subexpression is in fact
7035 // potentially evaluated.
7036 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated,
7037 Sema::ReuseLambdaContextDecl);
7039 ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
7040 if (SubExpr.isInvalid())
7043 if (!getDerived().AlwaysRebuild() &&
7044 SubExpr.get() == E->getExprOperand())
7045 return SemaRef.Owned(E);
7047 return getDerived().RebuildCXXTypeidExpr(E->getType(),
7053 template<typename Derived>
7055 TreeTransform<Derived>::TransformCXXUuidofExpr(CXXUuidofExpr *E) {
7056 if (E->isTypeOperand()) {
7057 TypeSourceInfo *TInfo
7058 = getDerived().TransformType(E->getTypeOperandSourceInfo());
7062 if (!getDerived().AlwaysRebuild() &&
7063 TInfo == E->getTypeOperandSourceInfo())
7064 return SemaRef.Owned(E);
7066 return getDerived().RebuildCXXUuidofExpr(E->getType(),
7072 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
7074 ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
7075 if (SubExpr.isInvalid())
7078 if (!getDerived().AlwaysRebuild() &&
7079 SubExpr.get() == E->getExprOperand())
7080 return SemaRef.Owned(E);
7082 return getDerived().RebuildCXXUuidofExpr(E->getType(),
7088 template<typename Derived>
7090 TreeTransform<Derived>::TransformCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7091 return SemaRef.Owned(E);
7094 template<typename Derived>
7096 TreeTransform<Derived>::TransformCXXNullPtrLiteralExpr(
7097 CXXNullPtrLiteralExpr *E) {
7098 return SemaRef.Owned(E);
7101 template<typename Derived>
7103 TreeTransform<Derived>::TransformCXXThisExpr(CXXThisExpr *E) {
7104 DeclContext *DC = getSema().getFunctionLevelDeclContext();
7106 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
7107 T = MD->getThisType(getSema().Context);
7109 T = getSema().Context.getPointerType(
7110 getSema().Context.getRecordType(cast<CXXRecordDecl>(DC)));
7112 if (!getDerived().AlwaysRebuild() && T == E->getType()) {
7113 // Make sure that we capture 'this'.
7114 getSema().CheckCXXThisCapture(E->getLocStart());
7115 return SemaRef.Owned(E);
7118 return getDerived().RebuildCXXThisExpr(E->getLocStart(), T, E->isImplicit());
7121 template<typename Derived>
7123 TreeTransform<Derived>::TransformCXXThrowExpr(CXXThrowExpr *E) {
7124 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
7125 if (SubExpr.isInvalid())
7128 if (!getDerived().AlwaysRebuild() &&
7129 SubExpr.get() == E->getSubExpr())
7130 return SemaRef.Owned(E);
7132 return getDerived().RebuildCXXThrowExpr(E->getThrowLoc(), SubExpr.get(),
7133 E->isThrownVariableInScope());
7136 template<typename Derived>
7138 TreeTransform<Derived>::TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
7140 = cast_or_null<ParmVarDecl>(getDerived().TransformDecl(E->getLocStart(),
7145 if (!getDerived().AlwaysRebuild() &&
7146 Param == E->getParam())
7147 return SemaRef.Owned(E);
7149 return getDerived().RebuildCXXDefaultArgExpr(E->getUsedLocation(), Param);
7152 template<typename Derived>
7154 TreeTransform<Derived>::TransformCXXScalarValueInitExpr(
7155 CXXScalarValueInitExpr *E) {
7156 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
7160 if (!getDerived().AlwaysRebuild() &&
7161 T == E->getTypeSourceInfo())
7162 return SemaRef.Owned(E);
7164 return getDerived().RebuildCXXScalarValueInitExpr(T,
7165 /*FIXME:*/T->getTypeLoc().getEndLoc(),
7169 template<typename Derived>
7171 TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
7172 // Transform the type that we're allocating
7173 TypeSourceInfo *AllocTypeInfo
7174 = getDerived().TransformType(E->getAllocatedTypeSourceInfo());
7178 // Transform the size of the array we're allocating (if any).
7179 ExprResult ArraySize = getDerived().TransformExpr(E->getArraySize());
7180 if (ArraySize.isInvalid())
7183 // Transform the placement arguments (if any).
7184 bool ArgumentChanged = false;
7185 SmallVector<Expr*, 8> PlacementArgs;
7186 if (getDerived().TransformExprs(E->getPlacementArgs(),
7187 E->getNumPlacementArgs(), true,
7188 PlacementArgs, &ArgumentChanged))
7191 // Transform the initializer (if any).
7192 Expr *OldInit = E->getInitializer();
7195 NewInit = getDerived().TransformExpr(OldInit);
7196 if (NewInit.isInvalid())
7199 // Transform new operator and delete operator.
7200 FunctionDecl *OperatorNew = 0;
7201 if (E->getOperatorNew()) {
7202 OperatorNew = cast_or_null<FunctionDecl>(
7203 getDerived().TransformDecl(E->getLocStart(),
7204 E->getOperatorNew()));
7209 FunctionDecl *OperatorDelete = 0;
7210 if (E->getOperatorDelete()) {
7211 OperatorDelete = cast_or_null<FunctionDecl>(
7212 getDerived().TransformDecl(E->getLocStart(),
7213 E->getOperatorDelete()));
7214 if (!OperatorDelete)
7218 if (!getDerived().AlwaysRebuild() &&
7219 AllocTypeInfo == E->getAllocatedTypeSourceInfo() &&
7220 ArraySize.get() == E->getArraySize() &&
7221 NewInit.get() == OldInit &&
7222 OperatorNew == E->getOperatorNew() &&
7223 OperatorDelete == E->getOperatorDelete() &&
7225 // Mark any declarations we need as referenced.
7226 // FIXME: instantiation-specific.
7228 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorNew);
7230 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorDelete);
7232 if (E->isArray() && !E->getAllocatedType()->isDependentType()) {
7233 QualType ElementType
7234 = SemaRef.Context.getBaseElementType(E->getAllocatedType());
7235 if (const RecordType *RecordT = ElementType->getAs<RecordType>()) {
7236 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordT->getDecl());
7237 if (CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(Record)) {
7238 SemaRef.MarkFunctionReferenced(E->getLocStart(), Destructor);
7243 return SemaRef.Owned(E);
7246 QualType AllocType = AllocTypeInfo->getType();
7247 if (!ArraySize.get()) {
7248 // If no array size was specified, but the new expression was
7249 // instantiated with an array type (e.g., "new T" where T is
7250 // instantiated with "int[4]"), extract the outer bound from the
7251 // array type as our array size. We do this with constant and
7252 // dependently-sized array types.
7253 const ArrayType *ArrayT = SemaRef.Context.getAsArrayType(AllocType);
7256 } else if (const ConstantArrayType *ConsArrayT
7257 = dyn_cast<ConstantArrayType>(ArrayT)) {
7259 = SemaRef.Owned(IntegerLiteral::Create(SemaRef.Context,
7260 ConsArrayT->getSize(),
7261 SemaRef.Context.getSizeType(),
7262 /*FIXME:*/E->getLocStart()));
7263 AllocType = ConsArrayT->getElementType();
7264 } else if (const DependentSizedArrayType *DepArrayT
7265 = dyn_cast<DependentSizedArrayType>(ArrayT)) {
7266 if (DepArrayT->getSizeExpr()) {
7267 ArraySize = SemaRef.Owned(DepArrayT->getSizeExpr());
7268 AllocType = DepArrayT->getElementType();
7273 return getDerived().RebuildCXXNewExpr(E->getLocStart(),
7275 /*FIXME:*/E->getLocStart(),
7277 /*FIXME:*/E->getLocStart(),
7278 E->getTypeIdParens(),
7282 E->getDirectInitRange(),
7286 template<typename Derived>
7288 TreeTransform<Derived>::TransformCXXDeleteExpr(CXXDeleteExpr *E) {
7289 ExprResult Operand = getDerived().TransformExpr(E->getArgument());
7290 if (Operand.isInvalid())
7293 // Transform the delete operator, if known.
7294 FunctionDecl *OperatorDelete = 0;
7295 if (E->getOperatorDelete()) {
7296 OperatorDelete = cast_or_null<FunctionDecl>(
7297 getDerived().TransformDecl(E->getLocStart(),
7298 E->getOperatorDelete()));
7299 if (!OperatorDelete)
7303 if (!getDerived().AlwaysRebuild() &&
7304 Operand.get() == E->getArgument() &&
7305 OperatorDelete == E->getOperatorDelete()) {
7306 // Mark any declarations we need as referenced.
7307 // FIXME: instantiation-specific.
7309 SemaRef.MarkFunctionReferenced(E->getLocStart(), OperatorDelete);
7311 if (!E->getArgument()->isTypeDependent()) {
7312 QualType Destroyed = SemaRef.Context.getBaseElementType(
7313 E->getDestroyedType());
7314 if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) {
7315 CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl());
7316 SemaRef.MarkFunctionReferenced(E->getLocStart(),
7317 SemaRef.LookupDestructor(Record));
7321 return SemaRef.Owned(E);
7324 return getDerived().RebuildCXXDeleteExpr(E->getLocStart(),
7325 E->isGlobalDelete(),
7330 template<typename Derived>
7332 TreeTransform<Derived>::TransformCXXPseudoDestructorExpr(
7333 CXXPseudoDestructorExpr *E) {
7334 ExprResult Base = getDerived().TransformExpr(E->getBase());
7335 if (Base.isInvalid())
7338 ParsedType ObjectTypePtr;
7339 bool MayBePseudoDestructor = false;
7340 Base = SemaRef.ActOnStartCXXMemberReference(0, Base.get(),
7341 E->getOperatorLoc(),
7342 E->isArrow()? tok::arrow : tok::period,
7344 MayBePseudoDestructor);
7345 if (Base.isInvalid())
7348 QualType ObjectType = ObjectTypePtr.get();
7349 NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc();
7352 = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc, ObjectType);
7357 SS.Adopt(QualifierLoc);
7359 PseudoDestructorTypeStorage Destroyed;
7360 if (E->getDestroyedTypeInfo()) {
7361 TypeSourceInfo *DestroyedTypeInfo
7362 = getDerived().TransformTypeInObjectScope(E->getDestroyedTypeInfo(),
7364 if (!DestroyedTypeInfo)
7366 Destroyed = DestroyedTypeInfo;
7367 } else if (!ObjectType.isNull() && ObjectType->isDependentType()) {
7368 // We aren't likely to be able to resolve the identifier down to a type
7369 // now anyway, so just retain the identifier.
7370 Destroyed = PseudoDestructorTypeStorage(E->getDestroyedTypeIdentifier(),
7371 E->getDestroyedTypeLoc());
7373 // Look for a destructor known with the given name.
7374 ParsedType T = SemaRef.getDestructorName(E->getTildeLoc(),
7375 *E->getDestroyedTypeIdentifier(),
7376 E->getDestroyedTypeLoc(),
7384 = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.GetTypeFromParser(T),
7385 E->getDestroyedTypeLoc());
7388 TypeSourceInfo *ScopeTypeInfo = 0;
7389 if (E->getScopeTypeInfo()) {
7390 ScopeTypeInfo = getDerived().TransformType(E->getScopeTypeInfo());
7395 return getDerived().RebuildCXXPseudoDestructorExpr(Base.get(),
7396 E->getOperatorLoc(),
7400 E->getColonColonLoc(),
7405 template<typename Derived>
7407 TreeTransform<Derived>::TransformUnresolvedLookupExpr(
7408 UnresolvedLookupExpr *Old) {
7409 LookupResult R(SemaRef, Old->getName(), Old->getNameLoc(),
7410 Sema::LookupOrdinaryName);
7412 // Transform all the decls.
7413 for (UnresolvedLookupExpr::decls_iterator I = Old->decls_begin(),
7414 E = Old->decls_end(); I != E; ++I) {
7415 NamedDecl *InstD = static_cast<NamedDecl*>(
7416 getDerived().TransformDecl(Old->getNameLoc(),
7419 // Silently ignore these if a UsingShadowDecl instantiated to nothing.
7420 // This can happen because of dependent hiding.
7421 if (isa<UsingShadowDecl>(*I))
7427 // Expand using declarations.
7428 if (isa<UsingDecl>(InstD)) {
7429 UsingDecl *UD = cast<UsingDecl>(InstD);
7430 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
7431 E = UD->shadow_end(); I != E; ++I)
7439 // Resolve a kind, but don't do any further analysis. If it's
7440 // ambiguous, the callee needs to deal with it.
7443 // Rebuild the nested-name qualifier, if present.
7445 if (Old->getQualifierLoc()) {
7446 NestedNameSpecifierLoc QualifierLoc
7447 = getDerived().TransformNestedNameSpecifierLoc(Old->getQualifierLoc());
7451 SS.Adopt(QualifierLoc);
7454 if (Old->getNamingClass()) {
7455 CXXRecordDecl *NamingClass
7456 = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
7458 Old->getNamingClass()));
7462 R.setNamingClass(NamingClass);
7465 SourceLocation TemplateKWLoc = Old->getTemplateKeywordLoc();
7467 // If we have neither explicit template arguments, nor the template keyword,
7468 // it's a normal declaration name.
7469 if (!Old->hasExplicitTemplateArgs() && !TemplateKWLoc.isValid())
7470 return getDerived().RebuildDeclarationNameExpr(SS, R, Old->requiresADL());
7472 // If we have template arguments, rebuild them, then rebuild the
7473 // templateid expression.
7474 TemplateArgumentListInfo TransArgs(Old->getLAngleLoc(), Old->getRAngleLoc());
7475 if (Old->hasExplicitTemplateArgs() &&
7476 getDerived().TransformTemplateArguments(Old->getTemplateArgs(),
7477 Old->getNumTemplateArgs(),
7481 return getDerived().RebuildTemplateIdExpr(SS, TemplateKWLoc, R,
7482 Old->requiresADL(), &TransArgs);
7485 template<typename Derived>
7487 TreeTransform<Derived>::TransformUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
7488 TypeSourceInfo *T = getDerived().TransformType(E->getQueriedTypeSourceInfo());
7492 if (!getDerived().AlwaysRebuild() &&
7493 T == E->getQueriedTypeSourceInfo())
7494 return SemaRef.Owned(E);
7496 return getDerived().RebuildUnaryTypeTrait(E->getTrait(),
7502 template<typename Derived>
7504 TreeTransform<Derived>::TransformBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
7505 TypeSourceInfo *LhsT = getDerived().TransformType(E->getLhsTypeSourceInfo());
7509 TypeSourceInfo *RhsT = getDerived().TransformType(E->getRhsTypeSourceInfo());
7513 if (!getDerived().AlwaysRebuild() &&
7514 LhsT == E->getLhsTypeSourceInfo() && RhsT == E->getRhsTypeSourceInfo())
7515 return SemaRef.Owned(E);
7517 return getDerived().RebuildBinaryTypeTrait(E->getTrait(),
7523 template<typename Derived>
7525 TreeTransform<Derived>::TransformTypeTraitExpr(TypeTraitExpr *E) {
7526 bool ArgChanged = false;
7527 llvm::SmallVector<TypeSourceInfo *, 4> Args;
7528 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
7529 TypeSourceInfo *From = E->getArg(I);
7530 TypeLoc FromTL = From->getTypeLoc();
7531 if (!isa<PackExpansionTypeLoc>(FromTL)) {
7533 TLB.reserve(FromTL.getFullDataSize());
7534 QualType To = getDerived().TransformType(TLB, FromTL);
7538 if (To == From->getType())
7539 Args.push_back(From);
7541 Args.push_back(TLB.getTypeSourceInfo(SemaRef.Context, To));
7549 // We have a pack expansion. Instantiate it.
7550 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(FromTL);
7551 TypeLoc PatternTL = ExpansionTL.getPatternLoc();
7552 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
7553 SemaRef.collectUnexpandedParameterPacks(PatternTL, Unexpanded);
7555 // Determine whether the set of unexpanded parameter packs can and should
7558 bool RetainExpansion = false;
7559 llvm::Optional<unsigned> OrigNumExpansions
7560 = ExpansionTL.getTypePtr()->getNumExpansions();
7561 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
7562 if (getDerived().TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(),
7563 PatternTL.getSourceRange(),
7565 Expand, RetainExpansion,
7570 // The transform has determined that we should perform a simple
7571 // transformation on the pack expansion, producing another pack
7573 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
7576 TLB.reserve(From->getTypeLoc().getFullDataSize());
7578 QualType To = getDerived().TransformType(TLB, PatternTL);
7582 To = getDerived().RebuildPackExpansionType(To,
7583 PatternTL.getSourceRange(),
7584 ExpansionTL.getEllipsisLoc(),
7589 PackExpansionTypeLoc ToExpansionTL
7590 = TLB.push<PackExpansionTypeLoc>(To);
7591 ToExpansionTL.setEllipsisLoc(ExpansionTL.getEllipsisLoc());
7592 Args.push_back(TLB.getTypeSourceInfo(SemaRef.Context, To));
7596 // Expand the pack expansion by substituting for each argument in the
7598 for (unsigned I = 0; I != *NumExpansions; ++I) {
7599 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
7601 TLB.reserve(PatternTL.getFullDataSize());
7602 QualType To = getDerived().TransformType(TLB, PatternTL);
7606 Args.push_back(TLB.getTypeSourceInfo(SemaRef.Context, To));
7609 if (!RetainExpansion)
7612 // If we're supposed to retain a pack expansion, do so by temporarily
7613 // forgetting the partially-substituted parameter pack.
7614 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
7617 TLB.reserve(From->getTypeLoc().getFullDataSize());
7619 QualType To = getDerived().TransformType(TLB, PatternTL);
7623 To = getDerived().RebuildPackExpansionType(To,
7624 PatternTL.getSourceRange(),
7625 ExpansionTL.getEllipsisLoc(),
7630 PackExpansionTypeLoc ToExpansionTL
7631 = TLB.push<PackExpansionTypeLoc>(To);
7632 ToExpansionTL.setEllipsisLoc(ExpansionTL.getEllipsisLoc());
7633 Args.push_back(TLB.getTypeSourceInfo(SemaRef.Context, To));
7636 if (!getDerived().AlwaysRebuild() && !ArgChanged)
7637 return SemaRef.Owned(E);
7639 return getDerived().RebuildTypeTrait(E->getTrait(),
7645 template<typename Derived>
7647 TreeTransform<Derived>::TransformArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
7648 TypeSourceInfo *T = getDerived().TransformType(E->getQueriedTypeSourceInfo());
7652 if (!getDerived().AlwaysRebuild() &&
7653 T == E->getQueriedTypeSourceInfo())
7654 return SemaRef.Owned(E);
7658 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
7659 SubExpr = getDerived().TransformExpr(E->getDimensionExpression());
7660 if (SubExpr.isInvalid())
7663 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getDimensionExpression())
7664 return SemaRef.Owned(E);
7667 return getDerived().RebuildArrayTypeTrait(E->getTrait(),
7674 template<typename Derived>
7676 TreeTransform<Derived>::TransformExpressionTraitExpr(ExpressionTraitExpr *E) {
7679 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
7680 SubExpr = getDerived().TransformExpr(E->getQueriedExpression());
7681 if (SubExpr.isInvalid())
7684 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getQueriedExpression())
7685 return SemaRef.Owned(E);
7688 return getDerived().RebuildExpressionTrait(
7689 E->getTrait(), E->getLocStart(), SubExpr.get(), E->getLocEnd());
7692 template<typename Derived>
7694 TreeTransform<Derived>::TransformDependentScopeDeclRefExpr(
7695 DependentScopeDeclRefExpr *E) {
7696 return TransformDependentScopeDeclRefExpr(E, /*IsAddressOfOperand*/false);
7699 template<typename Derived>
7701 TreeTransform<Derived>::TransformDependentScopeDeclRefExpr(
7702 DependentScopeDeclRefExpr *E,
7703 bool IsAddressOfOperand) {
7704 NestedNameSpecifierLoc QualifierLoc
7705 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc());
7708 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
7710 // TODO: If this is a conversion-function-id, verify that the
7711 // destination type name (if present) resolves the same way after
7712 // instantiation as it did in the local scope.
7714 DeclarationNameInfo NameInfo
7715 = getDerived().TransformDeclarationNameInfo(E->getNameInfo());
7716 if (!NameInfo.getName())
7719 if (!E->hasExplicitTemplateArgs()) {
7720 if (!getDerived().AlwaysRebuild() &&
7721 QualifierLoc == E->getQualifierLoc() &&
7722 // Note: it is sufficient to compare the Name component of NameInfo:
7723 // if name has not changed, DNLoc has not changed either.
7724 NameInfo.getName() == E->getDeclName())
7725 return SemaRef.Owned(E);
7727 return getDerived().RebuildDependentScopeDeclRefExpr(QualifierLoc,
7731 IsAddressOfOperand);
7734 TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc());
7735 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
7736 E->getNumTemplateArgs(),
7740 return getDerived().RebuildDependentScopeDeclRefExpr(QualifierLoc,
7744 IsAddressOfOperand);
7747 template<typename Derived>
7749 TreeTransform<Derived>::TransformCXXConstructExpr(CXXConstructExpr *E) {
7750 // CXXConstructExprs are always implicit, so when we have a
7751 // 1-argument construction we just transform that argument.
7752 if (E->getNumArgs() == 1 ||
7753 (E->getNumArgs() > 1 && getDerived().DropCallArgument(E->getArg(1))))
7754 return getDerived().TransformExpr(E->getArg(0));
7756 TemporaryBase Rebase(*this, /*FIXME*/E->getLocStart(), DeclarationName());
7758 QualType T = getDerived().TransformType(E->getType());
7762 CXXConstructorDecl *Constructor
7763 = cast_or_null<CXXConstructorDecl>(
7764 getDerived().TransformDecl(E->getLocStart(),
7765 E->getConstructor()));
7769 bool ArgumentChanged = false;
7770 SmallVector<Expr*, 8> Args;
7771 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
7775 if (!getDerived().AlwaysRebuild() &&
7776 T == E->getType() &&
7777 Constructor == E->getConstructor() &&
7779 // Mark the constructor as referenced.
7780 // FIXME: Instantiation-specific
7781 SemaRef.MarkFunctionReferenced(E->getLocStart(), Constructor);
7782 return SemaRef.Owned(E);
7785 return getDerived().RebuildCXXConstructExpr(T, /*FIXME:*/E->getLocStart(),
7786 Constructor, E->isElidable(),
7788 E->hadMultipleCandidates(),
7789 E->requiresZeroInitialization(),
7790 E->getConstructionKind(),
7791 E->getParenRange());
7794 /// \brief Transform a C++ temporary-binding expression.
7796 /// Since CXXBindTemporaryExpr nodes are implicitly generated, we just
7797 /// transform the subexpression and return that.
7798 template<typename Derived>
7800 TreeTransform<Derived>::TransformCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
7801 return getDerived().TransformExpr(E->getSubExpr());
7804 /// \brief Transform a C++ expression that contains cleanups that should
7805 /// be run after the expression is evaluated.
7807 /// Since ExprWithCleanups nodes are implicitly generated, we
7808 /// just transform the subexpression and return that.
7809 template<typename Derived>
7811 TreeTransform<Derived>::TransformExprWithCleanups(ExprWithCleanups *E) {
7812 return getDerived().TransformExpr(E->getSubExpr());
7815 template<typename Derived>
7817 TreeTransform<Derived>::TransformCXXTemporaryObjectExpr(
7818 CXXTemporaryObjectExpr *E) {
7819 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
7823 CXXConstructorDecl *Constructor
7824 = cast_or_null<CXXConstructorDecl>(
7825 getDerived().TransformDecl(E->getLocStart(),
7826 E->getConstructor()));
7830 bool ArgumentChanged = false;
7831 SmallVector<Expr*, 8> Args;
7832 Args.reserve(E->getNumArgs());
7833 if (TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
7837 if (!getDerived().AlwaysRebuild() &&
7838 T == E->getTypeSourceInfo() &&
7839 Constructor == E->getConstructor() &&
7841 // FIXME: Instantiation-specific
7842 SemaRef.MarkFunctionReferenced(E->getLocStart(), Constructor);
7843 return SemaRef.MaybeBindToTemporary(E);
7846 return getDerived().RebuildCXXTemporaryObjectExpr(T,
7847 /*FIXME:*/T->getTypeLoc().getEndLoc(),
7852 template<typename Derived>
7854 TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
7855 // Transform the type of the lambda parameters and start the definition of
7856 // the lambda itself.
7857 TypeSourceInfo *MethodTy
7858 = TransformType(E->getCallOperator()->getTypeSourceInfo());
7862 // Create the local class that will describe the lambda.
7863 CXXRecordDecl *Class
7864 = getSema().createLambdaClosureType(E->getIntroducerRange(),
7866 /*KnownDependent=*/false);
7867 getDerived().transformedLocalDecl(E->getLambdaClass(), Class);
7869 // Transform lambda parameters.
7870 llvm::SmallVector<QualType, 4> ParamTypes;
7871 llvm::SmallVector<ParmVarDecl *, 4> Params;
7872 if (getDerived().TransformFunctionTypeParams(E->getLocStart(),
7873 E->getCallOperator()->param_begin(),
7874 E->getCallOperator()->param_size(),
7875 0, ParamTypes, &Params))
7878 // Build the call operator.
7879 CXXMethodDecl *CallOperator
7880 = getSema().startLambdaDefinition(Class, E->getIntroducerRange(),
7882 E->getCallOperator()->getLocEnd(),
7884 getDerived().transformAttrs(E->getCallOperator(), CallOperator);
7886 return getDerived().TransformLambdaScope(E, CallOperator);
7889 template<typename Derived>
7891 TreeTransform<Derived>::TransformLambdaScope(LambdaExpr *E,
7892 CXXMethodDecl *CallOperator) {
7893 // Introduce the context of the call operator.
7894 Sema::ContextRAII SavedContext(getSema(), CallOperator);
7896 // Enter the scope of the lambda.
7897 sema::LambdaScopeInfo *LSI
7898 = getSema().enterLambdaScope(CallOperator, E->getIntroducerRange(),
7899 E->getCaptureDefault(),
7900 E->hasExplicitParameters(),
7901 E->hasExplicitResultType(),
7904 // Transform captures.
7905 bool Invalid = false;
7906 bool FinishedExplicitCaptures = false;
7907 for (LambdaExpr::capture_iterator C = E->capture_begin(),
7908 CEnd = E->capture_end();
7910 // When we hit the first implicit capture, tell Sema that we've finished
7911 // the list of explicit captures.
7912 if (!FinishedExplicitCaptures && C->isImplicit()) {
7913 getSema().finishLambdaExplicitCaptures(LSI);
7914 FinishedExplicitCaptures = true;
7917 // Capturing 'this' is trivial.
7918 if (C->capturesThis()) {
7919 getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit());
7923 // Determine the capture kind for Sema.
7924 Sema::TryCaptureKind Kind
7925 = C->isImplicit()? Sema::TryCapture_Implicit
7926 : C->getCaptureKind() == LCK_ByCopy
7927 ? Sema::TryCapture_ExplicitByVal
7928 : Sema::TryCapture_ExplicitByRef;
7929 SourceLocation EllipsisLoc;
7930 if (C->isPackExpansion()) {
7931 UnexpandedParameterPack Unexpanded(C->getCapturedVar(), C->getLocation());
7932 bool ShouldExpand = false;
7933 bool RetainExpansion = false;
7934 llvm::Optional<unsigned> NumExpansions;
7935 if (getDerived().TryExpandParameterPacks(C->getEllipsisLoc(),
7938 ShouldExpand, RetainExpansion,
7943 // The transform has determined that we should perform an expansion;
7944 // transform and capture each of the arguments.
7945 // expansion of the pattern. Do so.
7946 VarDecl *Pack = C->getCapturedVar();
7947 for (unsigned I = 0; I != *NumExpansions; ++I) {
7948 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
7949 VarDecl *CapturedVar
7950 = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(),
7957 // Capture the transformed variable.
7958 getSema().tryCaptureVariable(CapturedVar, C->getLocation(), Kind);
7963 EllipsisLoc = C->getEllipsisLoc();
7966 // Transform the captured variable.
7967 VarDecl *CapturedVar
7968 = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(),
7969 C->getCapturedVar()));
7975 // Capture the transformed variable.
7976 getSema().tryCaptureVariable(CapturedVar, C->getLocation(), Kind);
7978 if (!FinishedExplicitCaptures)
7979 getSema().finishLambdaExplicitCaptures(LSI);
7982 // Enter a new evaluation context to insulate the lambda from any
7983 // cleanups from the enclosing full-expression.
7984 getSema().PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
7987 getSema().ActOnLambdaError(E->getLocStart(), /*CurScope=*/0,
7988 /*IsInstantiation=*/true);
7992 // Instantiate the body of the lambda expression.
7993 StmtResult Body = getDerived().TransformStmt(E->getBody());
7994 if (Body.isInvalid()) {
7995 getSema().ActOnLambdaError(E->getLocStart(), /*CurScope=*/0,
7996 /*IsInstantiation=*/true);
8000 return getSema().ActOnLambdaExpr(E->getLocStart(), Body.take(),
8001 /*CurScope=*/0, /*IsInstantiation=*/true);
8004 template<typename Derived>
8006 TreeTransform<Derived>::TransformCXXUnresolvedConstructExpr(
8007 CXXUnresolvedConstructExpr *E) {
8008 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
8012 bool ArgumentChanged = false;
8013 SmallVector<Expr*, 8> Args;
8014 Args.reserve(E->arg_size());
8015 if (getDerived().TransformExprs(E->arg_begin(), E->arg_size(), true, Args,
8019 if (!getDerived().AlwaysRebuild() &&
8020 T == E->getTypeSourceInfo() &&
8022 return SemaRef.Owned(E);
8024 // FIXME: we're faking the locations of the commas
8025 return getDerived().RebuildCXXUnresolvedConstructExpr(T,
8031 template<typename Derived>
8033 TreeTransform<Derived>::TransformCXXDependentScopeMemberExpr(
8034 CXXDependentScopeMemberExpr *E) {
8035 // Transform the base of the expression.
8036 ExprResult Base((Expr*) 0);
8039 QualType ObjectType;
8040 if (!E->isImplicitAccess()) {
8041 OldBase = E->getBase();
8042 Base = getDerived().TransformExpr(OldBase);
8043 if (Base.isInvalid())
8046 // Start the member reference and compute the object's type.
8047 ParsedType ObjectTy;
8048 bool MayBePseudoDestructor = false;
8049 Base = SemaRef.ActOnStartCXXMemberReference(0, Base.get(),
8050 E->getOperatorLoc(),
8051 E->isArrow()? tok::arrow : tok::period,
8053 MayBePseudoDestructor);
8054 if (Base.isInvalid())
8057 ObjectType = ObjectTy.get();
8058 BaseType = ((Expr*) Base.get())->getType();
8061 BaseType = getDerived().TransformType(E->getBaseType());
8062 ObjectType = BaseType->getAs<PointerType>()->getPointeeType();
8065 // Transform the first part of the nested-name-specifier that qualifies
8067 NamedDecl *FirstQualifierInScope
8068 = getDerived().TransformFirstQualifierInScope(
8069 E->getFirstQualifierFoundInScope(),
8070 E->getQualifierLoc().getBeginLoc());
8072 NestedNameSpecifierLoc QualifierLoc;
8073 if (E->getQualifier()) {
8075 = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc(),
8077 FirstQualifierInScope);
8082 SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc();
8084 // TODO: If this is a conversion-function-id, verify that the
8085 // destination type name (if present) resolves the same way after
8086 // instantiation as it did in the local scope.
8088 DeclarationNameInfo NameInfo
8089 = getDerived().TransformDeclarationNameInfo(E->getMemberNameInfo());
8090 if (!NameInfo.getName())
8093 if (!E->hasExplicitTemplateArgs()) {
8094 // This is a reference to a member without an explicitly-specified
8095 // template argument list. Optimize for this common case.
8096 if (!getDerived().AlwaysRebuild() &&
8097 Base.get() == OldBase &&
8098 BaseType == E->getBaseType() &&
8099 QualifierLoc == E->getQualifierLoc() &&
8100 NameInfo.getName() == E->getMember() &&
8101 FirstQualifierInScope == E->getFirstQualifierFoundInScope())
8102 return SemaRef.Owned(E);
8104 return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(),
8107 E->getOperatorLoc(),
8110 FirstQualifierInScope,
8112 /*TemplateArgs*/ 0);
8115 TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc());
8116 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
8117 E->getNumTemplateArgs(),
8121 return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(),
8124 E->getOperatorLoc(),
8127 FirstQualifierInScope,
8132 template<typename Derived>
8134 TreeTransform<Derived>::TransformUnresolvedMemberExpr(UnresolvedMemberExpr *Old) {
8135 // Transform the base of the expression.
8136 ExprResult Base((Expr*) 0);
8138 if (!Old->isImplicitAccess()) {
8139 Base = getDerived().TransformExpr(Old->getBase());
8140 if (Base.isInvalid())
8142 Base = getSema().PerformMemberExprBaseConversion(Base.take(),
8144 if (Base.isInvalid())
8146 BaseType = Base.get()->getType();
8148 BaseType = getDerived().TransformType(Old->getBaseType());
8151 NestedNameSpecifierLoc QualifierLoc;
8152 if (Old->getQualifierLoc()) {
8154 = getDerived().TransformNestedNameSpecifierLoc(Old->getQualifierLoc());
8159 SourceLocation TemplateKWLoc = Old->getTemplateKeywordLoc();
8161 LookupResult R(SemaRef, Old->getMemberNameInfo(),
8162 Sema::LookupOrdinaryName);
8164 // Transform all the decls.
8165 for (UnresolvedMemberExpr::decls_iterator I = Old->decls_begin(),
8166 E = Old->decls_end(); I != E; ++I) {
8167 NamedDecl *InstD = static_cast<NamedDecl*>(
8168 getDerived().TransformDecl(Old->getMemberLoc(),
8171 // Silently ignore these if a UsingShadowDecl instantiated to nothing.
8172 // This can happen because of dependent hiding.
8173 if (isa<UsingShadowDecl>(*I))
8181 // Expand using declarations.
8182 if (isa<UsingDecl>(InstD)) {
8183 UsingDecl *UD = cast<UsingDecl>(InstD);
8184 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
8185 E = UD->shadow_end(); I != E; ++I)
8195 // Determine the naming class.
8196 if (Old->getNamingClass()) {
8197 CXXRecordDecl *NamingClass
8198 = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
8199 Old->getMemberLoc(),
8200 Old->getNamingClass()));
8204 R.setNamingClass(NamingClass);
8207 TemplateArgumentListInfo TransArgs;
8208 if (Old->hasExplicitTemplateArgs()) {
8209 TransArgs.setLAngleLoc(Old->getLAngleLoc());
8210 TransArgs.setRAngleLoc(Old->getRAngleLoc());
8211 if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(),
8212 Old->getNumTemplateArgs(),
8217 // FIXME: to do this check properly, we will need to preserve the
8218 // first-qualifier-in-scope here, just in case we had a dependent
8219 // base (and therefore couldn't do the check) and a
8220 // nested-name-qualifier (and therefore could do the lookup).
8221 NamedDecl *FirstQualifierInScope = 0;
8223 return getDerived().RebuildUnresolvedMemberExpr(Base.get(),
8225 Old->getOperatorLoc(),
8229 FirstQualifierInScope,
8231 (Old->hasExplicitTemplateArgs()
8235 template<typename Derived>
8237 TreeTransform<Derived>::TransformCXXNoexceptExpr(CXXNoexceptExpr *E) {
8238 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
8239 ExprResult SubExpr = getDerived().TransformExpr(E->getOperand());
8240 if (SubExpr.isInvalid())
8243 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getOperand())
8244 return SemaRef.Owned(E);
8246 return getDerived().RebuildCXXNoexceptExpr(E->getSourceRange(),SubExpr.get());
8249 template<typename Derived>
8251 TreeTransform<Derived>::TransformPackExpansionExpr(PackExpansionExpr *E) {
8252 ExprResult Pattern = getDerived().TransformExpr(E->getPattern());
8253 if (Pattern.isInvalid())
8256 if (!getDerived().AlwaysRebuild() && Pattern.get() == E->getPattern())
8257 return SemaRef.Owned(E);
8259 return getDerived().RebuildPackExpansion(Pattern.get(), E->getEllipsisLoc(),
8260 E->getNumExpansions());
8263 template<typename Derived>
8265 TreeTransform<Derived>::TransformSizeOfPackExpr(SizeOfPackExpr *E) {
8266 // If E is not value-dependent, then nothing will change when we transform it.
8267 // Note: This is an instantiation-centric view.
8268 if (!E->isValueDependent())
8269 return SemaRef.Owned(E);
8271 // Note: None of the implementations of TryExpandParameterPacks can ever
8272 // produce a diagnostic when given only a single unexpanded parameter pack,
8274 UnexpandedParameterPack Unexpanded(E->getPack(), E->getPackLoc());
8275 bool ShouldExpand = false;
8276 bool RetainExpansion = false;
8277 llvm::Optional<unsigned> NumExpansions;
8278 if (getDerived().TryExpandParameterPacks(E->getOperatorLoc(), E->getPackLoc(),
8280 ShouldExpand, RetainExpansion,
8284 if (RetainExpansion)
8285 return SemaRef.Owned(E);
8287 NamedDecl *Pack = E->getPack();
8288 if (!ShouldExpand) {
8289 Pack = cast_or_null<NamedDecl>(getDerived().TransformDecl(E->getPackLoc(),
8296 // We now know the length of the parameter pack, so build a new expression
8297 // that stores that length.
8298 return getDerived().RebuildSizeOfPackExpr(E->getOperatorLoc(), Pack,
8299 E->getPackLoc(), E->getRParenLoc(),
8303 template<typename Derived>
8305 TreeTransform<Derived>::TransformSubstNonTypeTemplateParmPackExpr(
8306 SubstNonTypeTemplateParmPackExpr *E) {
8307 // Default behavior is to do nothing with this transformation.
8308 return SemaRef.Owned(E);
8311 template<typename Derived>
8313 TreeTransform<Derived>::TransformSubstNonTypeTemplateParmExpr(
8314 SubstNonTypeTemplateParmExpr *E) {
8315 // Default behavior is to do nothing with this transformation.
8316 return SemaRef.Owned(E);
8319 template<typename Derived>
8321 TreeTransform<Derived>::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) {
8322 // Default behavior is to do nothing with this transformation.
8323 return SemaRef.Owned(E);
8326 template<typename Derived>
8328 TreeTransform<Derived>::TransformMaterializeTemporaryExpr(
8329 MaterializeTemporaryExpr *E) {
8330 return getDerived().TransformExpr(E->GetTemporaryExpr());
8333 template<typename Derived>
8335 TreeTransform<Derived>::TransformObjCStringLiteral(ObjCStringLiteral *E) {
8336 return SemaRef.MaybeBindToTemporary(E);
8339 template<typename Derived>
8341 TreeTransform<Derived>::TransformObjCBoolLiteralExpr(ObjCBoolLiteralExpr *E) {
8342 return SemaRef.Owned(E);
8345 template<typename Derived>
8347 TreeTransform<Derived>::TransformObjCBoxedExpr(ObjCBoxedExpr *E) {
8348 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
8349 if (SubExpr.isInvalid())
8352 if (!getDerived().AlwaysRebuild() &&
8353 SubExpr.get() == E->getSubExpr())
8354 return SemaRef.Owned(E);
8356 return getDerived().RebuildObjCBoxedExpr(E->getSourceRange(), SubExpr.get());
8359 template<typename Derived>
8361 TreeTransform<Derived>::TransformObjCArrayLiteral(ObjCArrayLiteral *E) {
8362 // Transform each of the elements.
8363 llvm::SmallVector<Expr *, 8> Elements;
8364 bool ArgChanged = false;
8365 if (getDerived().TransformExprs(E->getElements(), E->getNumElements(),
8366 /*IsCall=*/false, Elements, &ArgChanged))
8369 if (!getDerived().AlwaysRebuild() && !ArgChanged)
8370 return SemaRef.MaybeBindToTemporary(E);
8372 return getDerived().RebuildObjCArrayLiteral(E->getSourceRange(),
8377 template<typename Derived>
8379 TreeTransform<Derived>::TransformObjCDictionaryLiteral(
8380 ObjCDictionaryLiteral *E) {
8381 // Transform each of the elements.
8382 llvm::SmallVector<ObjCDictionaryElement, 8> Elements;
8383 bool ArgChanged = false;
8384 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
8385 ObjCDictionaryElement OrigElement = E->getKeyValueElement(I);
8387 if (OrigElement.isPackExpansion()) {
8388 // This key/value element is a pack expansion.
8389 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
8390 getSema().collectUnexpandedParameterPacks(OrigElement.Key, Unexpanded);
8391 getSema().collectUnexpandedParameterPacks(OrigElement.Value, Unexpanded);
8392 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
8394 // Determine whether the set of unexpanded parameter packs can
8395 // and should be expanded.
8397 bool RetainExpansion = false;
8398 llvm::Optional<unsigned> OrigNumExpansions = OrigElement.NumExpansions;
8399 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
8400 SourceRange PatternRange(OrigElement.Key->getLocStart(),
8401 OrigElement.Value->getLocEnd());
8402 if (getDerived().TryExpandParameterPacks(OrigElement.EllipsisLoc,
8405 Expand, RetainExpansion,
8410 // The transform has determined that we should perform a simple
8411 // transformation on the pack expansion, producing another pack
8413 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
8414 ExprResult Key = getDerived().TransformExpr(OrigElement.Key);
8415 if (Key.isInvalid())
8418 if (Key.get() != OrigElement.Key)
8421 ExprResult Value = getDerived().TransformExpr(OrigElement.Value);
8422 if (Value.isInvalid())
8425 if (Value.get() != OrigElement.Value)
8428 ObjCDictionaryElement Expansion = {
8429 Key.get(), Value.get(), OrigElement.EllipsisLoc, NumExpansions
8431 Elements.push_back(Expansion);
8435 // Record right away that the argument was changed. This needs
8436 // to happen even if the array expands to nothing.
8439 // The transform has determined that we should perform an elementwise
8440 // expansion of the pattern. Do so.
8441 for (unsigned I = 0; I != *NumExpansions; ++I) {
8442 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
8443 ExprResult Key = getDerived().TransformExpr(OrigElement.Key);
8444 if (Key.isInvalid())
8447 ExprResult Value = getDerived().TransformExpr(OrigElement.Value);
8448 if (Value.isInvalid())
8451 ObjCDictionaryElement Element = {
8452 Key.get(), Value.get(), SourceLocation(), NumExpansions
8455 // If any unexpanded parameter packs remain, we still have a
8457 if (Key.get()->containsUnexpandedParameterPack() ||
8458 Value.get()->containsUnexpandedParameterPack())
8459 Element.EllipsisLoc = OrigElement.EllipsisLoc;
8461 Elements.push_back(Element);
8464 // We've finished with this pack expansion.
8468 // Transform and check key.
8469 ExprResult Key = getDerived().TransformExpr(OrigElement.Key);
8470 if (Key.isInvalid())
8473 if (Key.get() != OrigElement.Key)
8476 // Transform and check value.
8478 = getDerived().TransformExpr(OrigElement.Value);
8479 if (Value.isInvalid())
8482 if (Value.get() != OrigElement.Value)
8485 ObjCDictionaryElement Element = {
8486 Key.get(), Value.get(), SourceLocation(), llvm::Optional<unsigned>()
8488 Elements.push_back(Element);
8491 if (!getDerived().AlwaysRebuild() && !ArgChanged)
8492 return SemaRef.MaybeBindToTemporary(E);
8494 return getDerived().RebuildObjCDictionaryLiteral(E->getSourceRange(),
8499 template<typename Derived>
8501 TreeTransform<Derived>::TransformObjCEncodeExpr(ObjCEncodeExpr *E) {
8502 TypeSourceInfo *EncodedTypeInfo
8503 = getDerived().TransformType(E->getEncodedTypeSourceInfo());
8504 if (!EncodedTypeInfo)
8507 if (!getDerived().AlwaysRebuild() &&
8508 EncodedTypeInfo == E->getEncodedTypeSourceInfo())
8509 return SemaRef.Owned(E);
8511 return getDerived().RebuildObjCEncodeExpr(E->getAtLoc(),
8516 template<typename Derived>
8517 ExprResult TreeTransform<Derived>::
8518 TransformObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
8519 ExprResult result = getDerived().TransformExpr(E->getSubExpr());
8520 if (result.isInvalid()) return ExprError();
8521 Expr *subExpr = result.take();
8523 if (!getDerived().AlwaysRebuild() &&
8524 subExpr == E->getSubExpr())
8525 return SemaRef.Owned(E);
8527 return SemaRef.Owned(new(SemaRef.Context)
8528 ObjCIndirectCopyRestoreExpr(subExpr, E->getType(), E->shouldCopy()));
8531 template<typename Derived>
8532 ExprResult TreeTransform<Derived>::
8533 TransformObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
8534 TypeSourceInfo *TSInfo
8535 = getDerived().TransformType(E->getTypeInfoAsWritten());
8539 ExprResult Result = getDerived().TransformExpr(E->getSubExpr());
8540 if (Result.isInvalid())
8543 if (!getDerived().AlwaysRebuild() &&
8544 TSInfo == E->getTypeInfoAsWritten() &&
8545 Result.get() == E->getSubExpr())
8546 return SemaRef.Owned(E);
8548 return SemaRef.BuildObjCBridgedCast(E->getLParenLoc(), E->getBridgeKind(),
8549 E->getBridgeKeywordLoc(), TSInfo,
8553 template<typename Derived>
8555 TreeTransform<Derived>::TransformObjCMessageExpr(ObjCMessageExpr *E) {
8556 // Transform arguments.
8557 bool ArgChanged = false;
8558 SmallVector<Expr*, 8> Args;
8559 Args.reserve(E->getNumArgs());
8560 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), false, Args,
8564 if (E->getReceiverKind() == ObjCMessageExpr::Class) {
8565 // Class message: transform the receiver type.
8566 TypeSourceInfo *ReceiverTypeInfo
8567 = getDerived().TransformType(E->getClassReceiverTypeInfo());
8568 if (!ReceiverTypeInfo)
8571 // If nothing changed, just retain the existing message send.
8572 if (!getDerived().AlwaysRebuild() &&
8573 ReceiverTypeInfo == E->getClassReceiverTypeInfo() && !ArgChanged)
8574 return SemaRef.MaybeBindToTemporary(E);
8576 // Build a new class message send.
8577 SmallVector<SourceLocation, 16> SelLocs;
8578 E->getSelectorLocs(SelLocs);
8579 return getDerived().RebuildObjCMessageExpr(ReceiverTypeInfo,
8588 // Instance message: transform the receiver
8589 assert(E->getReceiverKind() == ObjCMessageExpr::Instance &&
8590 "Only class and instance messages may be instantiated");
8592 = getDerived().TransformExpr(E->getInstanceReceiver());
8593 if (Receiver.isInvalid())
8596 // If nothing changed, just retain the existing message send.
8597 if (!getDerived().AlwaysRebuild() &&
8598 Receiver.get() == E->getInstanceReceiver() && !ArgChanged)
8599 return SemaRef.MaybeBindToTemporary(E);
8601 // Build a new instance message send.
8602 SmallVector<SourceLocation, 16> SelLocs;
8603 E->getSelectorLocs(SelLocs);
8604 return getDerived().RebuildObjCMessageExpr(Receiver.get(),
8613 template<typename Derived>
8615 TreeTransform<Derived>::TransformObjCSelectorExpr(ObjCSelectorExpr *E) {
8616 return SemaRef.Owned(E);
8619 template<typename Derived>
8621 TreeTransform<Derived>::TransformObjCProtocolExpr(ObjCProtocolExpr *E) {
8622 return SemaRef.Owned(E);
8625 template<typename Derived>
8627 TreeTransform<Derived>::TransformObjCIvarRefExpr(ObjCIvarRefExpr *E) {
8628 // Transform the base expression.
8629 ExprResult Base = getDerived().TransformExpr(E->getBase());
8630 if (Base.isInvalid())
8633 // We don't need to transform the ivar; it will never change.
8635 // If nothing changed, just retain the existing expression.
8636 if (!getDerived().AlwaysRebuild() &&
8637 Base.get() == E->getBase())
8638 return SemaRef.Owned(E);
8640 return getDerived().RebuildObjCIvarRefExpr(Base.get(), E->getDecl(),
8642 E->isArrow(), E->isFreeIvar());
8645 template<typename Derived>
8647 TreeTransform<Derived>::TransformObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
8648 // 'super' and types never change. Property never changes. Just
8649 // retain the existing expression.
8650 if (!E->isObjectReceiver())
8651 return SemaRef.Owned(E);
8653 // Transform the base expression.
8654 ExprResult Base = getDerived().TransformExpr(E->getBase());
8655 if (Base.isInvalid())
8658 // We don't need to transform the property; it will never change.
8660 // If nothing changed, just retain the existing expression.
8661 if (!getDerived().AlwaysRebuild() &&
8662 Base.get() == E->getBase())
8663 return SemaRef.Owned(E);
8665 if (E->isExplicitProperty())
8666 return getDerived().RebuildObjCPropertyRefExpr(Base.get(),
8667 E->getExplicitProperty(),
8670 return getDerived().RebuildObjCPropertyRefExpr(Base.get(),
8671 SemaRef.Context.PseudoObjectTy,
8672 E->getImplicitPropertyGetter(),
8673 E->getImplicitPropertySetter(),
8677 template<typename Derived>
8679 TreeTransform<Derived>::TransformObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) {
8680 // Transform the base expression.
8681 ExprResult Base = getDerived().TransformExpr(E->getBaseExpr());
8682 if (Base.isInvalid())
8685 // Transform the key expression.
8686 ExprResult Key = getDerived().TransformExpr(E->getKeyExpr());
8687 if (Key.isInvalid())
8690 // If nothing changed, just retain the existing expression.
8691 if (!getDerived().AlwaysRebuild() &&
8692 Key.get() == E->getKeyExpr() && Base.get() == E->getBaseExpr())
8693 return SemaRef.Owned(E);
8695 return getDerived().RebuildObjCSubscriptRefExpr(E->getRBracket(),
8696 Base.get(), Key.get(),
8697 E->getAtIndexMethodDecl(),
8698 E->setAtIndexMethodDecl());
8701 template<typename Derived>
8703 TreeTransform<Derived>::TransformObjCIsaExpr(ObjCIsaExpr *E) {
8704 // Transform the base expression.
8705 ExprResult Base = getDerived().TransformExpr(E->getBase());
8706 if (Base.isInvalid())
8709 // If nothing changed, just retain the existing expression.
8710 if (!getDerived().AlwaysRebuild() &&
8711 Base.get() == E->getBase())
8712 return SemaRef.Owned(E);
8714 return getDerived().RebuildObjCIsaExpr(Base.get(), E->getIsaMemberLoc(),
8718 template<typename Derived>
8720 TreeTransform<Derived>::TransformShuffleVectorExpr(ShuffleVectorExpr *E) {
8721 bool ArgumentChanged = false;
8722 SmallVector<Expr*, 8> SubExprs;
8723 SubExprs.reserve(E->getNumSubExprs());
8724 if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false,
8725 SubExprs, &ArgumentChanged))
8728 if (!getDerived().AlwaysRebuild() &&
8730 return SemaRef.Owned(E);
8732 return getDerived().RebuildShuffleVectorExpr(E->getBuiltinLoc(),
8737 template<typename Derived>
8739 TreeTransform<Derived>::TransformBlockExpr(BlockExpr *E) {
8740 BlockDecl *oldBlock = E->getBlockDecl();
8742 SemaRef.ActOnBlockStart(E->getCaretLocation(), /*Scope=*/0);
8743 BlockScopeInfo *blockScope = SemaRef.getCurBlock();
8745 blockScope->TheDecl->setIsVariadic(oldBlock->isVariadic());
8746 blockScope->TheDecl->setBlockMissingReturnType(
8747 oldBlock->blockMissingReturnType());
8749 SmallVector<ParmVarDecl*, 4> params;
8750 SmallVector<QualType, 4> paramTypes;
8752 // Parameter substitution.
8753 if (getDerived().TransformFunctionTypeParams(E->getCaretLocation(),
8754 oldBlock->param_begin(),
8755 oldBlock->param_size(),
8756 0, paramTypes, ¶ms)) {
8757 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
8761 const FunctionType *exprFunctionType = E->getFunctionType();
8762 QualType exprResultType =
8763 getDerived().TransformType(exprFunctionType->getResultType());
8765 // Don't allow returning a objc interface by value.
8766 if (exprResultType->isObjCObjectType()) {
8767 getSema().Diag(E->getCaretLocation(),
8768 diag::err_object_cannot_be_passed_returned_by_value)
8769 << 0 << exprResultType;
8770 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
8774 QualType functionType = getDerived().RebuildFunctionProtoType(
8778 oldBlock->isVariadic(),
8780 exprFunctionType->getExtInfo());
8781 blockScope->FunctionType = functionType;
8783 // Set the parameters on the block decl.
8784 if (!params.empty())
8785 blockScope->TheDecl->setParams(params);
8787 if (!oldBlock->blockMissingReturnType()) {
8788 blockScope->HasImplicitReturnType = false;
8789 blockScope->ReturnType = exprResultType;
8792 // Transform the body
8793 StmtResult body = getDerived().TransformStmt(E->getBody());
8794 if (body.isInvalid()) {
8795 getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/0);
8800 // In builds with assertions, make sure that we captured everything we
8802 if (!SemaRef.getDiagnostics().hasErrorOccurred()) {
8803 for (BlockDecl::capture_iterator i = oldBlock->capture_begin(),
8804 e = oldBlock->capture_end(); i != e; ++i) {
8805 VarDecl *oldCapture = i->getVariable();
8807 // Ignore parameter packs.
8808 if (isa<ParmVarDecl>(oldCapture) &&
8809 cast<ParmVarDecl>(oldCapture)->isParameterPack())
8812 VarDecl *newCapture =
8813 cast<VarDecl>(getDerived().TransformDecl(E->getCaretLocation(),
8815 assert(blockScope->CaptureMap.count(newCapture));
8817 assert(oldBlock->capturesCXXThis() == blockScope->isCXXThisCaptured());
8821 return SemaRef.ActOnBlockStmtExpr(E->getCaretLocation(), body.get(),
8825 template<typename Derived>
8827 TreeTransform<Derived>::TransformAsTypeExpr(AsTypeExpr *E) {
8828 llvm_unreachable("Cannot transform asType expressions yet");
8831 template<typename Derived>
8833 TreeTransform<Derived>::TransformAtomicExpr(AtomicExpr *E) {
8834 QualType RetTy = getDerived().TransformType(E->getType());
8835 bool ArgumentChanged = false;
8836 SmallVector<Expr*, 8> SubExprs;
8837 SubExprs.reserve(E->getNumSubExprs());
8838 if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false,
8839 SubExprs, &ArgumentChanged))
8842 if (!getDerived().AlwaysRebuild() &&
8844 return SemaRef.Owned(E);
8846 return getDerived().RebuildAtomicExpr(E->getBuiltinLoc(), SubExprs,
8847 RetTy, E->getOp(), E->getRParenLoc());
8850 //===----------------------------------------------------------------------===//
8851 // Type reconstruction
8852 //===----------------------------------------------------------------------===//
8854 template<typename Derived>
8855 QualType TreeTransform<Derived>::RebuildPointerType(QualType PointeeType,
8856 SourceLocation Star) {
8857 return SemaRef.BuildPointerType(PointeeType, Star,
8858 getDerived().getBaseEntity());
8861 template<typename Derived>
8862 QualType TreeTransform<Derived>::RebuildBlockPointerType(QualType PointeeType,
8863 SourceLocation Star) {
8864 return SemaRef.BuildBlockPointerType(PointeeType, Star,
8865 getDerived().getBaseEntity());
8868 template<typename Derived>
8870 TreeTransform<Derived>::RebuildReferenceType(QualType ReferentType,
8871 bool WrittenAsLValue,
8872 SourceLocation Sigil) {
8873 return SemaRef.BuildReferenceType(ReferentType, WrittenAsLValue,
8874 Sigil, getDerived().getBaseEntity());
8877 template<typename Derived>
8879 TreeTransform<Derived>::RebuildMemberPointerType(QualType PointeeType,
8881 SourceLocation Sigil) {
8882 return SemaRef.BuildMemberPointerType(PointeeType, ClassType,
8883 Sigil, getDerived().getBaseEntity());
8886 template<typename Derived>
8888 TreeTransform<Derived>::RebuildArrayType(QualType ElementType,
8889 ArrayType::ArraySizeModifier SizeMod,
8890 const llvm::APInt *Size,
8892 unsigned IndexTypeQuals,
8893 SourceRange BracketsRange) {
8894 if (SizeExpr || !Size)
8895 return SemaRef.BuildArrayType(ElementType, SizeMod, SizeExpr,
8896 IndexTypeQuals, BracketsRange,
8897 getDerived().getBaseEntity());
8899 QualType Types[] = {
8900 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
8901 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
8902 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
8904 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
8906 for (unsigned I = 0; I != NumTypes; ++I)
8907 if (Size->getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
8908 SizeType = Types[I];
8912 // Note that we can return a VariableArrayType here in the case where
8913 // the element type was a dependent VariableArrayType.
8914 IntegerLiteral *ArraySize
8915 = IntegerLiteral::Create(SemaRef.Context, *Size, SizeType,
8916 /*FIXME*/BracketsRange.getBegin());
8917 return SemaRef.BuildArrayType(ElementType, SizeMod, ArraySize,
8918 IndexTypeQuals, BracketsRange,
8919 getDerived().getBaseEntity());
8922 template<typename Derived>
8924 TreeTransform<Derived>::RebuildConstantArrayType(QualType ElementType,
8925 ArrayType::ArraySizeModifier SizeMod,
8926 const llvm::APInt &Size,
8927 unsigned IndexTypeQuals,
8928 SourceRange BracketsRange) {
8929 return getDerived().RebuildArrayType(ElementType, SizeMod, &Size, 0,
8930 IndexTypeQuals, BracketsRange);
8933 template<typename Derived>
8935 TreeTransform<Derived>::RebuildIncompleteArrayType(QualType ElementType,
8936 ArrayType::ArraySizeModifier SizeMod,
8937 unsigned IndexTypeQuals,
8938 SourceRange BracketsRange) {
8939 return getDerived().RebuildArrayType(ElementType, SizeMod, 0, 0,
8940 IndexTypeQuals, BracketsRange);
8943 template<typename Derived>
8945 TreeTransform<Derived>::RebuildVariableArrayType(QualType ElementType,
8946 ArrayType::ArraySizeModifier SizeMod,
8948 unsigned IndexTypeQuals,
8949 SourceRange BracketsRange) {
8950 return getDerived().RebuildArrayType(ElementType, SizeMod, 0,
8952 IndexTypeQuals, BracketsRange);
8955 template<typename Derived>
8957 TreeTransform<Derived>::RebuildDependentSizedArrayType(QualType ElementType,
8958 ArrayType::ArraySizeModifier SizeMod,
8960 unsigned IndexTypeQuals,
8961 SourceRange BracketsRange) {
8962 return getDerived().RebuildArrayType(ElementType, SizeMod, 0,
8964 IndexTypeQuals, BracketsRange);
8967 template<typename Derived>
8968 QualType TreeTransform<Derived>::RebuildVectorType(QualType ElementType,
8969 unsigned NumElements,
8970 VectorType::VectorKind VecKind) {
8971 // FIXME: semantic checking!
8972 return SemaRef.Context.getVectorType(ElementType, NumElements, VecKind);
8975 template<typename Derived>
8976 QualType TreeTransform<Derived>::RebuildExtVectorType(QualType ElementType,
8977 unsigned NumElements,
8978 SourceLocation AttributeLoc) {
8979 llvm::APInt numElements(SemaRef.Context.getIntWidth(SemaRef.Context.IntTy),
8981 IntegerLiteral *VectorSize
8982 = IntegerLiteral::Create(SemaRef.Context, numElements, SemaRef.Context.IntTy,
8984 return SemaRef.BuildExtVectorType(ElementType, VectorSize, AttributeLoc);
8987 template<typename Derived>
8989 TreeTransform<Derived>::RebuildDependentSizedExtVectorType(QualType ElementType,
8991 SourceLocation AttributeLoc) {
8992 return SemaRef.BuildExtVectorType(ElementType, SizeExpr, AttributeLoc);
8995 template<typename Derived>
8996 QualType TreeTransform<Derived>::RebuildFunctionProtoType(QualType T,
8997 QualType *ParamTypes,
8998 unsigned NumParamTypes,
9000 bool HasTrailingReturn,
9002 RefQualifierKind RefQualifier,
9003 const FunctionType::ExtInfo &Info) {
9004 return SemaRef.BuildFunctionType(T, ParamTypes, NumParamTypes, Variadic,
9005 HasTrailingReturn, Quals, RefQualifier,
9006 getDerived().getBaseLocation(),
9007 getDerived().getBaseEntity(),
9011 template<typename Derived>
9012 QualType TreeTransform<Derived>::RebuildFunctionNoProtoType(QualType T) {
9013 return SemaRef.Context.getFunctionNoProtoType(T);
9016 template<typename Derived>
9017 QualType TreeTransform<Derived>::RebuildUnresolvedUsingType(Decl *D) {
9018 assert(D && "no decl found");
9019 if (D->isInvalidDecl()) return QualType();
9021 // FIXME: Doesn't account for ObjCInterfaceDecl!
9023 if (isa<UsingDecl>(D)) {
9024 UsingDecl *Using = cast<UsingDecl>(D);
9025 assert(Using->isTypeName() &&
9026 "UnresolvedUsingTypenameDecl transformed to non-typename using");
9028 // A valid resolved using typename decl points to exactly one type decl.
9029 assert(++Using->shadow_begin() == Using->shadow_end());
9030 Ty = cast<TypeDecl>((*Using->shadow_begin())->getTargetDecl());
9033 assert(isa<UnresolvedUsingTypenameDecl>(D) &&
9034 "UnresolvedUsingTypenameDecl transformed to non-using decl");
9035 Ty = cast<UnresolvedUsingTypenameDecl>(D);
9038 return SemaRef.Context.getTypeDeclType(Ty);
9041 template<typename Derived>
9042 QualType TreeTransform<Derived>::RebuildTypeOfExprType(Expr *E,
9043 SourceLocation Loc) {
9044 return SemaRef.BuildTypeofExprType(E, Loc);
9047 template<typename Derived>
9048 QualType TreeTransform<Derived>::RebuildTypeOfType(QualType Underlying) {
9049 return SemaRef.Context.getTypeOfType(Underlying);
9052 template<typename Derived>
9053 QualType TreeTransform<Derived>::RebuildDecltypeType(Expr *E,
9054 SourceLocation Loc) {
9055 return SemaRef.BuildDecltypeType(E, Loc);
9058 template<typename Derived>
9059 QualType TreeTransform<Derived>::RebuildUnaryTransformType(QualType BaseType,
9060 UnaryTransformType::UTTKind UKind,
9061 SourceLocation Loc) {
9062 return SemaRef.BuildUnaryTransformType(BaseType, UKind, Loc);
9065 template<typename Derived>
9066 QualType TreeTransform<Derived>::RebuildTemplateSpecializationType(
9067 TemplateName Template,
9068 SourceLocation TemplateNameLoc,
9069 TemplateArgumentListInfo &TemplateArgs) {
9070 return SemaRef.CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
9073 template<typename Derived>
9074 QualType TreeTransform<Derived>::RebuildAtomicType(QualType ValueType,
9075 SourceLocation KWLoc) {
9076 return SemaRef.BuildAtomicType(ValueType, KWLoc);
9079 template<typename Derived>
9081 TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
9083 TemplateDecl *Template) {
9084 return SemaRef.Context.getQualifiedTemplateName(SS.getScopeRep(), TemplateKW,
9088 template<typename Derived>
9090 TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
9091 const IdentifierInfo &Name,
9092 SourceLocation NameLoc,
9093 QualType ObjectType,
9094 NamedDecl *FirstQualifierInScope) {
9095 UnqualifiedId TemplateName;
9096 TemplateName.setIdentifier(&Name, NameLoc);
9097 Sema::TemplateTy Template;
9098 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
9099 getSema().ActOnDependentTemplateName(/*Scope=*/0,
9100 SS, TemplateKWLoc, TemplateName,
9101 ParsedType::make(ObjectType),
9102 /*EnteringContext=*/false,
9104 return Template.get();
9107 template<typename Derived>
9109 TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
9110 OverloadedOperatorKind Operator,
9111 SourceLocation NameLoc,
9112 QualType ObjectType) {
9114 // FIXME: Bogus location information.
9115 SourceLocation SymbolLocations[3] = { NameLoc, NameLoc, NameLoc };
9116 Name.setOperatorFunctionId(NameLoc, Operator, SymbolLocations);
9117 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
9118 Sema::TemplateTy Template;
9119 getSema().ActOnDependentTemplateName(/*Scope=*/0,
9120 SS, TemplateKWLoc, Name,
9121 ParsedType::make(ObjectType),
9122 /*EnteringContext=*/false,
9124 return Template.template getAsVal<TemplateName>();
9127 template<typename Derived>
9129 TreeTransform<Derived>::RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
9130 SourceLocation OpLoc,
9134 Expr *Callee = OrigCallee->IgnoreParenCasts();
9135 bool isPostIncDec = Second && (Op == OO_PlusPlus || Op == OO_MinusMinus);
9137 // Determine whether this should be a builtin operation.
9138 if (Op == OO_Subscript) {
9139 if (!First->getType()->isOverloadableType() &&
9140 !Second->getType()->isOverloadableType())
9141 return getSema().CreateBuiltinArraySubscriptExpr(First,
9142 Callee->getLocStart(),
9144 } else if (Op == OO_Arrow) {
9145 // -> is never a builtin operation.
9146 return SemaRef.BuildOverloadedArrowExpr(0, First, OpLoc);
9147 } else if (Second == 0 || isPostIncDec) {
9148 if (!First->getType()->isOverloadableType()) {
9149 // The argument is not of overloadable type, so try to create a
9150 // built-in unary operation.
9151 UnaryOperatorKind Opc
9152 = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec);
9154 return getSema().CreateBuiltinUnaryOp(OpLoc, Opc, First);
9157 if (!First->getType()->isOverloadableType() &&
9158 !Second->getType()->isOverloadableType()) {
9159 // Neither of the arguments is an overloadable type, so try to
9160 // create a built-in binary operation.
9161 BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op);
9163 = SemaRef.CreateBuiltinBinOp(OpLoc, Opc, First, Second);
9164 if (Result.isInvalid())
9171 // Compute the transformed set of functions (and function templates) to be
9172 // used during overload resolution.
9173 UnresolvedSet<16> Functions;
9175 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Callee)) {
9176 assert(ULE->requiresADL());
9178 // FIXME: Do we have to check
9179 // IsAcceptableNonMemberOperatorCandidate for each of these?
9180 Functions.append(ULE->decls_begin(), ULE->decls_end());
9182 // If we've resolved this to a particular non-member function, just call
9183 // that function. If we resolved it to a member function,
9184 // CreateOverloaded* will find that function for us.
9185 NamedDecl *ND = cast<DeclRefExpr>(Callee)->getDecl();
9186 if (!isa<CXXMethodDecl>(ND))
9187 Functions.addDecl(ND);
9190 // Add any functions found via argument-dependent lookup.
9191 Expr *Args[2] = { First, Second };
9192 unsigned NumArgs = 1 + (Second != 0);
9194 // Create the overloaded operator invocation for unary operators.
9195 if (NumArgs == 1 || isPostIncDec) {
9196 UnaryOperatorKind Opc
9197 = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec);
9198 return SemaRef.CreateOverloadedUnaryOp(OpLoc, Opc, Functions, First);
9201 if (Op == OO_Subscript) {
9202 SourceLocation LBrace;
9203 SourceLocation RBrace;
9205 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) {
9206 DeclarationNameLoc &NameLoc = DRE->getNameInfo().getInfo();
9207 LBrace = SourceLocation::getFromRawEncoding(
9208 NameLoc.CXXOperatorName.BeginOpNameLoc);
9209 RBrace = SourceLocation::getFromRawEncoding(
9210 NameLoc.CXXOperatorName.EndOpNameLoc);
9212 LBrace = Callee->getLocStart();
9216 return SemaRef.CreateOverloadedArraySubscriptExpr(LBrace, RBrace,
9220 // Create the overloaded operator invocation for binary operators.
9221 BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op);
9223 = SemaRef.CreateOverloadedBinOp(OpLoc, Opc, Functions, Args[0], Args[1]);
9224 if (Result.isInvalid())
9230 template<typename Derived>
9232 TreeTransform<Derived>::RebuildCXXPseudoDestructorExpr(Expr *Base,
9233 SourceLocation OperatorLoc,
9236 TypeSourceInfo *ScopeType,
9237 SourceLocation CCLoc,
9238 SourceLocation TildeLoc,
9239 PseudoDestructorTypeStorage Destroyed) {
9240 QualType BaseType = Base->getType();
9241 if (Base->isTypeDependent() || Destroyed.getIdentifier() ||
9242 (!isArrow && !BaseType->getAs<RecordType>()) ||
9243 (isArrow && BaseType->getAs<PointerType>() &&
9244 !BaseType->getAs<PointerType>()->getPointeeType()
9245 ->template getAs<RecordType>())){
9246 // This pseudo-destructor expression is still a pseudo-destructor.
9247 return SemaRef.BuildPseudoDestructorExpr(Base, OperatorLoc,
9248 isArrow? tok::arrow : tok::period,
9249 SS, ScopeType, CCLoc, TildeLoc,
9254 TypeSourceInfo *DestroyedType = Destroyed.getTypeSourceInfo();
9255 DeclarationName Name(SemaRef.Context.DeclarationNames.getCXXDestructorName(
9256 SemaRef.Context.getCanonicalType(DestroyedType->getType())));
9257 DeclarationNameInfo NameInfo(Name, Destroyed.getLocation());
9258 NameInfo.setNamedTypeInfo(DestroyedType);
9260 // The scope type is now known to be a valid nested name specifier
9261 // component. Tack it on to the end of the nested name specifier.
9263 SS.Extend(SemaRef.Context, SourceLocation(),
9264 ScopeType->getTypeLoc(), CCLoc);
9266 SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller.
9267 return getSema().BuildMemberReferenceExpr(Base, BaseType,
9268 OperatorLoc, isArrow,
9270 /*FIXME: FirstQualifier*/ 0,
9272 /*TemplateArgs*/ 0);
9275 } // end namespace clang
9277 #endif // LLVM_CLANG_SEMA_TREETRANSFORM_H