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 #define STMT(Node, Parent) \
578 StmtResult Transform##Node(Node *S);
579 #define EXPR(Node, Parent) \
580 ExprResult Transform##Node(Node *E);
581 #define ABSTRACT_STMT(Stmt)
582 #include "clang/AST/StmtNodes.inc"
584 /// \brief Build a new pointer type given its pointee type.
586 /// By default, performs semantic analysis when building the pointer type.
587 /// Subclasses may override this routine to provide different behavior.
588 QualType RebuildPointerType(QualType PointeeType, SourceLocation Sigil);
590 /// \brief Build a new block pointer type given its pointee type.
592 /// By default, performs semantic analysis when building the block pointer
593 /// type. Subclasses may override this routine to provide different behavior.
594 QualType RebuildBlockPointerType(QualType PointeeType, SourceLocation Sigil);
596 /// \brief Build a new reference type given the type it references.
598 /// By default, performs semantic analysis when building the
599 /// reference type. Subclasses may override this routine to provide
600 /// different behavior.
602 /// \param LValue whether the type was written with an lvalue sigil
603 /// or an rvalue sigil.
604 QualType RebuildReferenceType(QualType ReferentType,
606 SourceLocation Sigil);
608 /// \brief Build a new member pointer type given the pointee type and the
609 /// class type it refers into.
611 /// By default, performs semantic analysis when building the member pointer
612 /// type. Subclasses may override this routine to provide different behavior.
613 QualType RebuildMemberPointerType(QualType PointeeType, QualType ClassType,
614 SourceLocation Sigil);
616 /// \brief Build a new array type given the element type, size
617 /// modifier, size of the array (if known), size expression, and index type
620 /// By default, performs semantic analysis when building the array type.
621 /// Subclasses may override this routine to provide different behavior.
622 /// Also by default, all of the other Rebuild*Array
623 QualType RebuildArrayType(QualType ElementType,
624 ArrayType::ArraySizeModifier SizeMod,
625 const llvm::APInt *Size,
627 unsigned IndexTypeQuals,
628 SourceRange BracketsRange);
630 /// \brief Build a new constant array type given the element type, size
631 /// modifier, (known) size of the array, and index type qualifiers.
633 /// By default, performs semantic analysis when building the array type.
634 /// Subclasses may override this routine to provide different behavior.
635 QualType RebuildConstantArrayType(QualType ElementType,
636 ArrayType::ArraySizeModifier SizeMod,
637 const llvm::APInt &Size,
638 unsigned IndexTypeQuals,
639 SourceRange BracketsRange);
641 /// \brief Build a new incomplete array type given the element type, size
642 /// modifier, and index type qualifiers.
644 /// By default, performs semantic analysis when building the array type.
645 /// Subclasses may override this routine to provide different behavior.
646 QualType RebuildIncompleteArrayType(QualType ElementType,
647 ArrayType::ArraySizeModifier SizeMod,
648 unsigned IndexTypeQuals,
649 SourceRange BracketsRange);
651 /// \brief Build a new variable-length array type given the element type,
652 /// size modifier, size expression, and index type qualifiers.
654 /// By default, performs semantic analysis when building the array type.
655 /// Subclasses may override this routine to provide different behavior.
656 QualType RebuildVariableArrayType(QualType ElementType,
657 ArrayType::ArraySizeModifier SizeMod,
659 unsigned IndexTypeQuals,
660 SourceRange BracketsRange);
662 /// \brief Build a new dependent-sized array type given the element type,
663 /// size modifier, size expression, and index type qualifiers.
665 /// By default, performs semantic analysis when building the array type.
666 /// Subclasses may override this routine to provide different behavior.
667 QualType RebuildDependentSizedArrayType(QualType ElementType,
668 ArrayType::ArraySizeModifier SizeMod,
670 unsigned IndexTypeQuals,
671 SourceRange BracketsRange);
673 /// \brief Build a new vector type given the element type and
674 /// number of elements.
676 /// By default, performs semantic analysis when building the vector type.
677 /// Subclasses may override this routine to provide different behavior.
678 QualType RebuildVectorType(QualType ElementType, unsigned NumElements,
679 VectorType::VectorKind VecKind);
681 /// \brief Build a new extended vector type given the element type and
682 /// number of elements.
684 /// By default, performs semantic analysis when building the vector type.
685 /// Subclasses may override this routine to provide different behavior.
686 QualType RebuildExtVectorType(QualType ElementType, unsigned NumElements,
687 SourceLocation AttributeLoc);
689 /// \brief Build a new potentially dependently-sized extended vector type
690 /// given the element type and number of elements.
692 /// By default, performs semantic analysis when building the vector type.
693 /// Subclasses may override this routine to provide different behavior.
694 QualType RebuildDependentSizedExtVectorType(QualType ElementType,
696 SourceLocation AttributeLoc);
698 /// \brief Build a new function type.
700 /// By default, performs semantic analysis when building the function type.
701 /// Subclasses may override this routine to provide different behavior.
702 QualType RebuildFunctionProtoType(QualType T,
703 QualType *ParamTypes,
704 unsigned NumParamTypes,
705 bool Variadic, bool HasTrailingReturn,
707 RefQualifierKind RefQualifier,
708 const FunctionType::ExtInfo &Info);
710 /// \brief Build a new unprototyped function type.
711 QualType RebuildFunctionNoProtoType(QualType ResultType);
713 /// \brief Rebuild an unresolved typename type, given the decl that
714 /// the UnresolvedUsingTypenameDecl was transformed to.
715 QualType RebuildUnresolvedUsingType(Decl *D);
717 /// \brief Build a new typedef type.
718 QualType RebuildTypedefType(TypedefNameDecl *Typedef) {
719 return SemaRef.Context.getTypeDeclType(Typedef);
722 /// \brief Build a new class/struct/union type.
723 QualType RebuildRecordType(RecordDecl *Record) {
724 return SemaRef.Context.getTypeDeclType(Record);
727 /// \brief Build a new Enum type.
728 QualType RebuildEnumType(EnumDecl *Enum) {
729 return SemaRef.Context.getTypeDeclType(Enum);
732 /// \brief Build a new typeof(expr) type.
734 /// By default, performs semantic analysis when building the typeof type.
735 /// Subclasses may override this routine to provide different behavior.
736 QualType RebuildTypeOfExprType(Expr *Underlying, SourceLocation Loc);
738 /// \brief Build a new typeof(type) type.
740 /// By default, builds a new TypeOfType with the given underlying type.
741 QualType RebuildTypeOfType(QualType Underlying);
743 /// \brief Build a new unary transform type.
744 QualType RebuildUnaryTransformType(QualType BaseType,
745 UnaryTransformType::UTTKind UKind,
748 /// \brief Build a new C++0x decltype type.
750 /// By default, performs semantic analysis when building the decltype type.
751 /// Subclasses may override this routine to provide different behavior.
752 QualType RebuildDecltypeType(Expr *Underlying, SourceLocation Loc);
754 /// \brief Build a new C++0x auto type.
756 /// By default, builds a new AutoType with the given deduced type.
757 QualType RebuildAutoType(QualType Deduced) {
758 return SemaRef.Context.getAutoType(Deduced);
761 /// \brief Build a new template specialization type.
763 /// By default, performs semantic analysis when building the template
764 /// specialization type. Subclasses may override this routine to provide
765 /// different behavior.
766 QualType RebuildTemplateSpecializationType(TemplateName Template,
767 SourceLocation TemplateLoc,
768 TemplateArgumentListInfo &Args);
770 /// \brief Build a new parenthesized type.
772 /// By default, builds a new ParenType type from the inner type.
773 /// Subclasses may override this routine to provide different behavior.
774 QualType RebuildParenType(QualType InnerType) {
775 return SemaRef.Context.getParenType(InnerType);
778 /// \brief Build a new qualified name type.
780 /// By default, builds a new ElaboratedType type from the keyword,
781 /// the nested-name-specifier and the named type.
782 /// Subclasses may override this routine to provide different behavior.
783 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
784 ElaboratedTypeKeyword Keyword,
785 NestedNameSpecifierLoc QualifierLoc,
787 return SemaRef.Context.getElaboratedType(Keyword,
788 QualifierLoc.getNestedNameSpecifier(),
792 /// \brief Build a new typename type that refers to a template-id.
794 /// By default, builds a new DependentNameType type from the
795 /// nested-name-specifier and the given type. Subclasses may override
796 /// this routine to provide different behavior.
797 QualType RebuildDependentTemplateSpecializationType(
798 ElaboratedTypeKeyword Keyword,
799 NestedNameSpecifierLoc QualifierLoc,
800 const IdentifierInfo *Name,
801 SourceLocation NameLoc,
802 TemplateArgumentListInfo &Args) {
803 // Rebuild the template name.
804 // TODO: avoid TemplateName abstraction
806 SS.Adopt(QualifierLoc);
807 TemplateName InstName
808 = getDerived().RebuildTemplateName(SS, *Name, NameLoc, QualType(), 0);
810 if (InstName.isNull())
813 // If it's still dependent, make a dependent specialization.
814 if (InstName.getAsDependentTemplateName())
815 return SemaRef.Context.getDependentTemplateSpecializationType(Keyword,
816 QualifierLoc.getNestedNameSpecifier(),
820 // Otherwise, make an elaborated type wrapping a non-dependent
823 getDerived().RebuildTemplateSpecializationType(InstName, NameLoc, Args);
824 if (T.isNull()) return QualType();
826 if (Keyword == ETK_None && QualifierLoc.getNestedNameSpecifier() == 0)
829 return SemaRef.Context.getElaboratedType(Keyword,
830 QualifierLoc.getNestedNameSpecifier(),
834 /// \brief Build a new typename type that refers to an identifier.
836 /// By default, performs semantic analysis when building the typename type
837 /// (or elaborated type). Subclasses may override this routine to provide
838 /// different behavior.
839 QualType RebuildDependentNameType(ElaboratedTypeKeyword Keyword,
840 SourceLocation KeywordLoc,
841 NestedNameSpecifierLoc QualifierLoc,
842 const IdentifierInfo *Id,
843 SourceLocation IdLoc) {
845 SS.Adopt(QualifierLoc);
847 if (QualifierLoc.getNestedNameSpecifier()->isDependent()) {
848 // If the name is still dependent, just build a new dependent name type.
849 if (!SemaRef.computeDeclContext(SS))
850 return SemaRef.Context.getDependentNameType(Keyword,
851 QualifierLoc.getNestedNameSpecifier(),
855 if (Keyword == ETK_None || Keyword == ETK_Typename)
856 return SemaRef.CheckTypenameType(Keyword, KeywordLoc, QualifierLoc,
859 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
861 // We had a dependent elaborated-type-specifier that has been transformed
862 // into a non-dependent elaborated-type-specifier. Find the tag we're
864 LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
865 DeclContext *DC = SemaRef.computeDeclContext(SS, false);
869 if (SemaRef.RequireCompleteDeclContext(SS, DC))
873 SemaRef.LookupQualifiedName(Result, DC);
874 switch (Result.getResultKind()) {
875 case LookupResult::NotFound:
876 case LookupResult::NotFoundInCurrentInstantiation:
879 case LookupResult::Found:
880 Tag = Result.getAsSingle<TagDecl>();
883 case LookupResult::FoundOverloaded:
884 case LookupResult::FoundUnresolvedValue:
885 llvm_unreachable("Tag lookup cannot find non-tags");
887 case LookupResult::Ambiguous:
888 // Let the LookupResult structure handle ambiguities.
893 // Check where the name exists but isn't a tag type and use that to emit
894 // better diagnostics.
895 LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
896 SemaRef.LookupQualifiedName(Result, DC);
897 switch (Result.getResultKind()) {
898 case LookupResult::Found:
899 case LookupResult::FoundOverloaded:
900 case LookupResult::FoundUnresolvedValue: {
901 NamedDecl *SomeDecl = Result.getRepresentativeDecl();
903 if (isa<TypedefDecl>(SomeDecl)) Kind = 1;
904 else if (isa<TypeAliasDecl>(SomeDecl)) Kind = 2;
905 else if (isa<ClassTemplateDecl>(SomeDecl)) Kind = 3;
906 SemaRef.Diag(IdLoc, diag::err_tag_reference_non_tag) << Kind;
907 SemaRef.Diag(SomeDecl->getLocation(), diag::note_declared_at);
911 // FIXME: Would be nice to highlight just the source range.
912 SemaRef.Diag(IdLoc, diag::err_not_tag_in_scope)
919 if (!SemaRef.isAcceptableTagRedeclaration(Tag, Kind, /*isDefinition*/false,
921 SemaRef.Diag(KeywordLoc, diag::err_use_with_wrong_tag) << Id;
922 SemaRef.Diag(Tag->getLocation(), diag::note_previous_use);
926 // Build the elaborated-type-specifier type.
927 QualType T = SemaRef.Context.getTypeDeclType(Tag);
928 return SemaRef.Context.getElaboratedType(Keyword,
929 QualifierLoc.getNestedNameSpecifier(),
933 /// \brief Build a new pack expansion type.
935 /// By default, builds a new PackExpansionType type from the given pattern.
936 /// Subclasses may override this routine to provide different behavior.
937 QualType RebuildPackExpansionType(QualType Pattern,
938 SourceRange PatternRange,
939 SourceLocation EllipsisLoc,
940 llvm::Optional<unsigned> NumExpansions) {
941 return getSema().CheckPackExpansion(Pattern, PatternRange, EllipsisLoc,
945 /// \brief Build a new atomic type given its value type.
947 /// By default, performs semantic analysis when building the atomic type.
948 /// Subclasses may override this routine to provide different behavior.
949 QualType RebuildAtomicType(QualType ValueType, SourceLocation KWLoc);
951 /// \brief Build a new template name given a nested name specifier, a flag
952 /// indicating whether the "template" keyword was provided, and the template
953 /// that the template name refers to.
955 /// By default, builds the new template name directly. Subclasses may override
956 /// this routine to provide different behavior.
957 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
959 TemplateDecl *Template);
961 /// \brief Build a new template name given a nested name specifier and the
962 /// name that is referred to as a template.
964 /// By default, performs semantic analysis to determine whether the name can
965 /// be resolved to a specific template, then builds the appropriate kind of
966 /// template name. Subclasses may override this routine to provide different
968 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
969 const IdentifierInfo &Name,
970 SourceLocation NameLoc,
972 NamedDecl *FirstQualifierInScope);
974 /// \brief Build a new template name given a nested name specifier and the
975 /// overloaded operator name that is referred to as a template.
977 /// By default, performs semantic analysis to determine whether the name can
978 /// be resolved to a specific template, then builds the appropriate kind of
979 /// template name. Subclasses may override this routine to provide different
981 TemplateName RebuildTemplateName(CXXScopeSpec &SS,
982 OverloadedOperatorKind Operator,
983 SourceLocation NameLoc,
984 QualType ObjectType);
986 /// \brief Build a new template name given a template template parameter pack
989 /// By default, performs semantic analysis to determine whether the name can
990 /// be resolved to a specific template, then builds the appropriate kind of
991 /// template name. Subclasses may override this routine to provide different
993 TemplateName RebuildTemplateName(TemplateTemplateParmDecl *Param,
994 const TemplateArgument &ArgPack) {
995 return getSema().Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
998 /// \brief Build a new compound statement.
1000 /// By default, performs semantic analysis to build the new statement.
1001 /// Subclasses may override this routine to provide different behavior.
1002 StmtResult RebuildCompoundStmt(SourceLocation LBraceLoc,
1003 MultiStmtArg Statements,
1004 SourceLocation RBraceLoc,
1006 return getSema().ActOnCompoundStmt(LBraceLoc, RBraceLoc, Statements,
1010 /// \brief Build a new case statement.
1012 /// By default, performs semantic analysis to build the new statement.
1013 /// Subclasses may override this routine to provide different behavior.
1014 StmtResult RebuildCaseStmt(SourceLocation CaseLoc,
1016 SourceLocation EllipsisLoc,
1018 SourceLocation ColonLoc) {
1019 return getSema().ActOnCaseStmt(CaseLoc, LHS, EllipsisLoc, RHS,
1023 /// \brief Attach the body to a new case statement.
1025 /// By default, performs semantic analysis to build the new statement.
1026 /// Subclasses may override this routine to provide different behavior.
1027 StmtResult RebuildCaseStmtBody(Stmt *S, Stmt *Body) {
1028 getSema().ActOnCaseStmtBody(S, Body);
1032 /// \brief Build a new default statement.
1034 /// By default, performs semantic analysis to build the new statement.
1035 /// Subclasses may override this routine to provide different behavior.
1036 StmtResult RebuildDefaultStmt(SourceLocation DefaultLoc,
1037 SourceLocation ColonLoc,
1039 return getSema().ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt,
1043 /// \brief Build a new label statement.
1045 /// By default, performs semantic analysis to build the new statement.
1046 /// Subclasses may override this routine to provide different behavior.
1047 StmtResult RebuildLabelStmt(SourceLocation IdentLoc, LabelDecl *L,
1048 SourceLocation ColonLoc, Stmt *SubStmt) {
1049 return SemaRef.ActOnLabelStmt(IdentLoc, L, ColonLoc, SubStmt);
1052 /// \brief Build a new label statement.
1054 /// By default, performs semantic analysis to build the new statement.
1055 /// Subclasses may override this routine to provide different behavior.
1056 StmtResult RebuildAttributedStmt(SourceLocation AttrLoc,
1057 ArrayRef<const Attr*> Attrs,
1059 return SemaRef.ActOnAttributedStmt(AttrLoc, Attrs, SubStmt);
1062 /// \brief Build a new "if" statement.
1064 /// By default, performs semantic analysis to build the new statement.
1065 /// Subclasses may override this routine to provide different behavior.
1066 StmtResult RebuildIfStmt(SourceLocation IfLoc, Sema::FullExprArg Cond,
1067 VarDecl *CondVar, Stmt *Then,
1068 SourceLocation ElseLoc, Stmt *Else) {
1069 return getSema().ActOnIfStmt(IfLoc, Cond, CondVar, Then, ElseLoc, Else);
1072 /// \brief Start building a new switch statement.
1074 /// By default, performs semantic analysis to build the new statement.
1075 /// Subclasses may override this routine to provide different behavior.
1076 StmtResult RebuildSwitchStmtStart(SourceLocation SwitchLoc,
1077 Expr *Cond, VarDecl *CondVar) {
1078 return getSema().ActOnStartOfSwitchStmt(SwitchLoc, Cond,
1082 /// \brief Attach the body to the switch statement.
1084 /// By default, performs semantic analysis to build the new statement.
1085 /// Subclasses may override this routine to provide different behavior.
1086 StmtResult RebuildSwitchStmtBody(SourceLocation SwitchLoc,
1087 Stmt *Switch, Stmt *Body) {
1088 return getSema().ActOnFinishSwitchStmt(SwitchLoc, Switch, Body);
1091 /// \brief Build a new while statement.
1093 /// By default, performs semantic analysis to build the new statement.
1094 /// Subclasses may override this routine to provide different behavior.
1095 StmtResult RebuildWhileStmt(SourceLocation WhileLoc, Sema::FullExprArg Cond,
1096 VarDecl *CondVar, Stmt *Body) {
1097 return getSema().ActOnWhileStmt(WhileLoc, Cond, CondVar, Body);
1100 /// \brief Build a new do-while statement.
1102 /// By default, performs semantic analysis to build the new statement.
1103 /// Subclasses may override this routine to provide different behavior.
1104 StmtResult RebuildDoStmt(SourceLocation DoLoc, Stmt *Body,
1105 SourceLocation WhileLoc, SourceLocation LParenLoc,
1106 Expr *Cond, SourceLocation RParenLoc) {
1107 return getSema().ActOnDoStmt(DoLoc, Body, WhileLoc, LParenLoc,
1111 /// \brief Build a new for statement.
1113 /// By default, performs semantic analysis to build the new statement.
1114 /// Subclasses may override this routine to provide different behavior.
1115 StmtResult RebuildForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
1116 Stmt *Init, Sema::FullExprArg Cond,
1117 VarDecl *CondVar, Sema::FullExprArg Inc,
1118 SourceLocation RParenLoc, Stmt *Body) {
1119 return getSema().ActOnForStmt(ForLoc, LParenLoc, Init, Cond,
1120 CondVar, Inc, RParenLoc, Body);
1123 /// \brief Build a new goto statement.
1125 /// By default, performs semantic analysis to build the new statement.
1126 /// Subclasses may override this routine to provide different behavior.
1127 StmtResult RebuildGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
1129 return getSema().ActOnGotoStmt(GotoLoc, LabelLoc, Label);
1132 /// \brief Build a new indirect goto statement.
1134 /// By default, performs semantic analysis to build the new statement.
1135 /// Subclasses may override this routine to provide different behavior.
1136 StmtResult RebuildIndirectGotoStmt(SourceLocation GotoLoc,
1137 SourceLocation StarLoc,
1139 return getSema().ActOnIndirectGotoStmt(GotoLoc, StarLoc, Target);
1142 /// \brief Build a new return statement.
1144 /// By default, performs semantic analysis to build the new statement.
1145 /// Subclasses may override this routine to provide different behavior.
1146 StmtResult RebuildReturnStmt(SourceLocation ReturnLoc, Expr *Result) {
1147 return getSema().ActOnReturnStmt(ReturnLoc, Result);
1150 /// \brief Build a new declaration statement.
1152 /// By default, performs semantic analysis to build the new statement.
1153 /// Subclasses may override this routine to provide different behavior.
1154 StmtResult RebuildDeclStmt(Decl **Decls, unsigned NumDecls,
1155 SourceLocation StartLoc,
1156 SourceLocation EndLoc) {
1157 Sema::DeclGroupPtrTy DG = getSema().BuildDeclaratorGroup(Decls, NumDecls);
1158 return getSema().ActOnDeclStmt(DG, StartLoc, EndLoc);
1161 /// \brief Build a new inline asm statement.
1163 /// By default, performs semantic analysis to build the new statement.
1164 /// Subclasses may override this routine to provide different behavior.
1165 StmtResult RebuildAsmStmt(SourceLocation AsmLoc,
1168 unsigned NumOutputs,
1170 IdentifierInfo **Names,
1171 MultiExprArg Constraints,
1174 MultiExprArg Clobbers,
1175 SourceLocation RParenLoc,
1177 return getSema().ActOnAsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs,
1178 NumInputs, Names, move(Constraints),
1179 Exprs, AsmString, Clobbers,
1183 /// \brief Build a new MS style inline asm statement.
1185 /// By default, performs semantic analysis to build the new statement.
1186 /// Subclasses may override this routine to provide different behavior.
1187 StmtResult RebuildMSAsmStmt(SourceLocation AsmLoc,
1188 SourceLocation LBraceLoc,
1189 ArrayRef<Token> AsmToks,
1190 SourceLocation EndLoc) {
1191 return getSema().ActOnMSAsmStmt(AsmLoc, LBraceLoc, AsmToks, EndLoc);
1194 /// \brief Build a new Objective-C \@try statement.
1196 /// By default, performs semantic analysis to build the new statement.
1197 /// Subclasses may override this routine to provide different behavior.
1198 StmtResult RebuildObjCAtTryStmt(SourceLocation AtLoc,
1200 MultiStmtArg CatchStmts,
1202 return getSema().ActOnObjCAtTryStmt(AtLoc, TryBody, move(CatchStmts),
1206 /// \brief Rebuild an Objective-C exception declaration.
1208 /// By default, performs semantic analysis to build the new declaration.
1209 /// Subclasses may override this routine to provide different behavior.
1210 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
1211 TypeSourceInfo *TInfo, QualType T) {
1212 return getSema().BuildObjCExceptionDecl(TInfo, T,
1213 ExceptionDecl->getInnerLocStart(),
1214 ExceptionDecl->getLocation(),
1215 ExceptionDecl->getIdentifier());
1218 /// \brief Build a new Objective-C \@catch statement.
1220 /// By default, performs semantic analysis to build the new statement.
1221 /// Subclasses may override this routine to provide different behavior.
1222 StmtResult RebuildObjCAtCatchStmt(SourceLocation AtLoc,
1223 SourceLocation RParenLoc,
1226 return getSema().ActOnObjCAtCatchStmt(AtLoc, RParenLoc,
1230 /// \brief Build a new Objective-C \@finally statement.
1232 /// By default, performs semantic analysis to build the new statement.
1233 /// Subclasses may override this routine to provide different behavior.
1234 StmtResult RebuildObjCAtFinallyStmt(SourceLocation AtLoc,
1236 return getSema().ActOnObjCAtFinallyStmt(AtLoc, Body);
1239 /// \brief Build a new Objective-C \@throw statement.
1241 /// By default, performs semantic analysis to build the new statement.
1242 /// Subclasses may override this routine to provide different behavior.
1243 StmtResult RebuildObjCAtThrowStmt(SourceLocation AtLoc,
1245 return getSema().BuildObjCAtThrowStmt(AtLoc, Operand);
1248 /// \brief Rebuild the operand to an Objective-C \@synchronized statement.
1250 /// By default, performs semantic analysis to build the new statement.
1251 /// Subclasses may override this routine to provide different behavior.
1252 ExprResult RebuildObjCAtSynchronizedOperand(SourceLocation atLoc,
1254 return getSema().ActOnObjCAtSynchronizedOperand(atLoc, object);
1257 /// \brief Build a new Objective-C \@synchronized statement.
1259 /// By default, performs semantic analysis to build the new statement.
1260 /// Subclasses may override this routine to provide different behavior.
1261 StmtResult RebuildObjCAtSynchronizedStmt(SourceLocation AtLoc,
1262 Expr *Object, Stmt *Body) {
1263 return getSema().ActOnObjCAtSynchronizedStmt(AtLoc, Object, Body);
1266 /// \brief Build a new Objective-C \@autoreleasepool statement.
1268 /// By default, performs semantic analysis to build the new statement.
1269 /// Subclasses may override this routine to provide different behavior.
1270 StmtResult RebuildObjCAutoreleasePoolStmt(SourceLocation AtLoc,
1272 return getSema().ActOnObjCAutoreleasePoolStmt(AtLoc, Body);
1275 /// \brief Build a new Objective-C fast enumeration statement.
1277 /// By default, performs semantic analysis to build the new statement.
1278 /// Subclasses may override this routine to provide different behavior.
1279 StmtResult RebuildObjCForCollectionStmt(SourceLocation ForLoc,
1280 SourceLocation LParenLoc,
1283 SourceLocation RParenLoc,
1285 StmtResult ForEachStmt = getSema().ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1289 if (ForEachStmt.isInvalid())
1292 return getSema().FinishObjCForCollectionStmt(ForEachStmt.take(), Body);
1295 /// \brief Build a new C++ exception declaration.
1297 /// By default, performs semantic analysis to build the new decaration.
1298 /// Subclasses may override this routine to provide different behavior.
1299 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
1300 TypeSourceInfo *Declarator,
1301 SourceLocation StartLoc,
1302 SourceLocation IdLoc,
1303 IdentifierInfo *Id) {
1304 VarDecl *Var = getSema().BuildExceptionDeclaration(0, Declarator,
1305 StartLoc, IdLoc, Id);
1307 getSema().CurContext->addDecl(Var);
1311 /// \brief Build a new C++ catch statement.
1313 /// By default, performs semantic analysis to build the new statement.
1314 /// Subclasses may override this routine to provide different behavior.
1315 StmtResult RebuildCXXCatchStmt(SourceLocation CatchLoc,
1316 VarDecl *ExceptionDecl,
1318 return Owned(new (getSema().Context) CXXCatchStmt(CatchLoc, ExceptionDecl,
1322 /// \brief Build a new C++ try statement.
1324 /// By default, performs semantic analysis to build the new statement.
1325 /// Subclasses may override this routine to provide different behavior.
1326 StmtResult RebuildCXXTryStmt(SourceLocation TryLoc,
1328 MultiStmtArg Handlers) {
1329 return getSema().ActOnCXXTryBlock(TryLoc, TryBlock, move(Handlers));
1332 /// \brief Build a new C++0x range-based for statement.
1334 /// By default, performs semantic analysis to build the new statement.
1335 /// Subclasses may override this routine to provide different behavior.
1336 StmtResult RebuildCXXForRangeStmt(SourceLocation ForLoc,
1337 SourceLocation ColonLoc,
1338 Stmt *Range, Stmt *BeginEnd,
1339 Expr *Cond, Expr *Inc,
1341 SourceLocation RParenLoc) {
1342 return getSema().BuildCXXForRangeStmt(ForLoc, ColonLoc, Range, BeginEnd,
1343 Cond, Inc, LoopVar, RParenLoc);
1346 /// \brief Build a new C++0x range-based for statement.
1348 /// By default, performs semantic analysis to build the new statement.
1349 /// Subclasses may override this routine to provide different behavior.
1350 StmtResult RebuildMSDependentExistsStmt(SourceLocation KeywordLoc,
1352 NestedNameSpecifierLoc QualifierLoc,
1353 DeclarationNameInfo NameInfo,
1355 return getSema().BuildMSDependentExistsStmt(KeywordLoc, IsIfExists,
1356 QualifierLoc, NameInfo, Nested);
1359 /// \brief Attach body to a C++0x range-based for statement.
1361 /// By default, performs semantic analysis to finish the new statement.
1362 /// Subclasses may override this routine to provide different behavior.
1363 StmtResult FinishCXXForRangeStmt(Stmt *ForRange, Stmt *Body) {
1364 return getSema().FinishCXXForRangeStmt(ForRange, Body);
1367 StmtResult RebuildSEHTryStmt(bool IsCXXTry,
1368 SourceLocation TryLoc,
1371 return getSema().ActOnSEHTryBlock(IsCXXTry,TryLoc,TryBlock,Handler);
1374 StmtResult RebuildSEHExceptStmt(SourceLocation Loc,
1377 return getSema().ActOnSEHExceptBlock(Loc,FilterExpr,Block);
1380 StmtResult RebuildSEHFinallyStmt(SourceLocation Loc,
1382 return getSema().ActOnSEHFinallyBlock(Loc,Block);
1385 /// \brief Build a new expression that references a declaration.
1387 /// By default, performs semantic analysis to build the new expression.
1388 /// Subclasses may override this routine to provide different behavior.
1389 ExprResult RebuildDeclarationNameExpr(const CXXScopeSpec &SS,
1392 return getSema().BuildDeclarationNameExpr(SS, R, RequiresADL);
1396 /// \brief Build a new expression that references a declaration.
1398 /// By default, performs semantic analysis to build the new expression.
1399 /// Subclasses may override this routine to provide different behavior.
1400 ExprResult RebuildDeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
1402 const DeclarationNameInfo &NameInfo,
1403 TemplateArgumentListInfo *TemplateArgs) {
1405 SS.Adopt(QualifierLoc);
1407 // FIXME: loses template args.
1409 return getSema().BuildDeclarationNameExpr(SS, NameInfo, VD);
1412 /// \brief Build a new expression in parentheses.
1414 /// By default, performs semantic analysis to build the new expression.
1415 /// Subclasses may override this routine to provide different behavior.
1416 ExprResult RebuildParenExpr(Expr *SubExpr, SourceLocation LParen,
1417 SourceLocation RParen) {
1418 return getSema().ActOnParenExpr(LParen, RParen, SubExpr);
1421 /// \brief Build a new pseudo-destructor expression.
1423 /// By default, performs semantic analysis to build the new expression.
1424 /// Subclasses may override this routine to provide different behavior.
1425 ExprResult RebuildCXXPseudoDestructorExpr(Expr *Base,
1426 SourceLocation OperatorLoc,
1429 TypeSourceInfo *ScopeType,
1430 SourceLocation CCLoc,
1431 SourceLocation TildeLoc,
1432 PseudoDestructorTypeStorage Destroyed);
1434 /// \brief Build a new unary operator expression.
1436 /// By default, performs semantic analysis to build the new expression.
1437 /// Subclasses may override this routine to provide different behavior.
1438 ExprResult RebuildUnaryOperator(SourceLocation OpLoc,
1439 UnaryOperatorKind Opc,
1441 return getSema().BuildUnaryOp(/*Scope=*/0, OpLoc, Opc, SubExpr);
1444 /// \brief Build a new builtin offsetof expression.
1446 /// By default, performs semantic analysis to build the new expression.
1447 /// Subclasses may override this routine to provide different behavior.
1448 ExprResult RebuildOffsetOfExpr(SourceLocation OperatorLoc,
1449 TypeSourceInfo *Type,
1450 Sema::OffsetOfComponent *Components,
1451 unsigned NumComponents,
1452 SourceLocation RParenLoc) {
1453 return getSema().BuildBuiltinOffsetOf(OperatorLoc, Type, Components,
1454 NumComponents, RParenLoc);
1457 /// \brief Build a new sizeof, alignof or vec_step expression with a
1460 /// By default, performs semantic analysis to build the new expression.
1461 /// Subclasses may override this routine to provide different behavior.
1462 ExprResult RebuildUnaryExprOrTypeTrait(TypeSourceInfo *TInfo,
1463 SourceLocation OpLoc,
1464 UnaryExprOrTypeTrait ExprKind,
1466 return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
1469 /// \brief Build a new sizeof, alignof or vec step expression with an
1470 /// expression argument.
1472 /// By default, performs semantic analysis to build the new expression.
1473 /// Subclasses may override this routine to provide different behavior.
1474 ExprResult RebuildUnaryExprOrTypeTrait(Expr *SubExpr, SourceLocation OpLoc,
1475 UnaryExprOrTypeTrait ExprKind,
1478 = getSema().CreateUnaryExprOrTypeTraitExpr(SubExpr, OpLoc, ExprKind);
1479 if (Result.isInvalid())
1482 return move(Result);
1485 /// \brief Build a new array subscript expression.
1487 /// By default, performs semantic analysis to build the new expression.
1488 /// Subclasses may override this routine to provide different behavior.
1489 ExprResult RebuildArraySubscriptExpr(Expr *LHS,
1490 SourceLocation LBracketLoc,
1492 SourceLocation RBracketLoc) {
1493 return getSema().ActOnArraySubscriptExpr(/*Scope=*/0, LHS,
1498 /// \brief Build a new call expression.
1500 /// By default, performs semantic analysis to build the new expression.
1501 /// Subclasses may override this routine to provide different behavior.
1502 ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc,
1504 SourceLocation RParenLoc,
1505 Expr *ExecConfig = 0) {
1506 return getSema().ActOnCallExpr(/*Scope=*/0, Callee, LParenLoc,
1507 move(Args), RParenLoc, ExecConfig);
1510 /// \brief Build a new member access expression.
1512 /// By default, performs semantic analysis to build the new expression.
1513 /// Subclasses may override this routine to provide different behavior.
1514 ExprResult RebuildMemberExpr(Expr *Base, SourceLocation OpLoc,
1516 NestedNameSpecifierLoc QualifierLoc,
1517 SourceLocation TemplateKWLoc,
1518 const DeclarationNameInfo &MemberNameInfo,
1520 NamedDecl *FoundDecl,
1521 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1522 NamedDecl *FirstQualifierInScope) {
1523 ExprResult BaseResult = getSema().PerformMemberExprBaseConversion(Base,
1525 if (!Member->getDeclName()) {
1526 // We have a reference to an unnamed field. This is always the
1527 // base of an anonymous struct/union member access, i.e. the
1528 // field is always of record type.
1529 assert(!QualifierLoc && "Can't have an unnamed field with a qualifier!");
1530 assert(Member->getType()->isRecordType() &&
1531 "unnamed member not of record type?");
1534 getSema().PerformObjectMemberConversion(BaseResult.take(),
1535 QualifierLoc.getNestedNameSpecifier(),
1537 if (BaseResult.isInvalid())
1539 Base = BaseResult.take();
1540 ExprValueKind VK = isArrow ? VK_LValue : Base->getValueKind();
1542 new (getSema().Context) MemberExpr(Base, isArrow,
1543 Member, MemberNameInfo,
1544 cast<FieldDecl>(Member)->getType(),
1546 return getSema().Owned(ME);
1550 SS.Adopt(QualifierLoc);
1552 Base = BaseResult.take();
1553 QualType BaseType = Base->getType();
1555 // FIXME: this involves duplicating earlier analysis in a lot of
1556 // cases; we should avoid this when possible.
1557 LookupResult R(getSema(), MemberNameInfo, Sema::LookupMemberName);
1558 R.addDecl(FoundDecl);
1561 return getSema().BuildMemberReferenceExpr(Base, BaseType, OpLoc, isArrow,
1563 FirstQualifierInScope,
1564 R, ExplicitTemplateArgs);
1567 /// \brief Build a new binary operator expression.
1569 /// By default, performs semantic analysis to build the new expression.
1570 /// Subclasses may override this routine to provide different behavior.
1571 ExprResult RebuildBinaryOperator(SourceLocation OpLoc,
1572 BinaryOperatorKind Opc,
1573 Expr *LHS, Expr *RHS) {
1574 return getSema().BuildBinOp(/*Scope=*/0, OpLoc, Opc, LHS, RHS);
1577 /// \brief Build a new conditional operator expression.
1579 /// By default, performs semantic analysis to build the new expression.
1580 /// Subclasses may override this routine to provide different behavior.
1581 ExprResult RebuildConditionalOperator(Expr *Cond,
1582 SourceLocation QuestionLoc,
1584 SourceLocation ColonLoc,
1586 return getSema().ActOnConditionalOp(QuestionLoc, ColonLoc, Cond,
1590 /// \brief Build a new C-style cast expression.
1592 /// By default, performs semantic analysis to build the new expression.
1593 /// Subclasses may override this routine to provide different behavior.
1594 ExprResult RebuildCStyleCastExpr(SourceLocation LParenLoc,
1595 TypeSourceInfo *TInfo,
1596 SourceLocation RParenLoc,
1598 return getSema().BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc,
1602 /// \brief Build a new compound literal expression.
1604 /// By default, performs semantic analysis to build the new expression.
1605 /// Subclasses may override this routine to provide different behavior.
1606 ExprResult RebuildCompoundLiteralExpr(SourceLocation LParenLoc,
1607 TypeSourceInfo *TInfo,
1608 SourceLocation RParenLoc,
1610 return getSema().BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc,
1614 /// \brief Build a new extended vector element access expression.
1616 /// By default, performs semantic analysis to build the new expression.
1617 /// Subclasses may override this routine to provide different behavior.
1618 ExprResult RebuildExtVectorElementExpr(Expr *Base,
1619 SourceLocation OpLoc,
1620 SourceLocation AccessorLoc,
1621 IdentifierInfo &Accessor) {
1624 DeclarationNameInfo NameInfo(&Accessor, AccessorLoc);
1625 return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
1626 OpLoc, /*IsArrow*/ false,
1627 SS, SourceLocation(),
1628 /*FirstQualifierInScope*/ 0,
1630 /* TemplateArgs */ 0);
1633 /// \brief Build a new initializer list expression.
1635 /// By default, performs semantic analysis to build the new expression.
1636 /// Subclasses may override this routine to provide different behavior.
1637 ExprResult RebuildInitList(SourceLocation LBraceLoc,
1639 SourceLocation RBraceLoc,
1640 QualType ResultTy) {
1642 = SemaRef.ActOnInitList(LBraceLoc, move(Inits), RBraceLoc);
1643 if (Result.isInvalid() || ResultTy->isDependentType())
1644 return move(Result);
1646 // Patch in the result type we were given, which may have been computed
1647 // when the initial InitListExpr was built.
1648 InitListExpr *ILE = cast<InitListExpr>((Expr *)Result.get());
1649 ILE->setType(ResultTy);
1650 return move(Result);
1653 /// \brief Build a new designated initializer expression.
1655 /// By default, performs semantic analysis to build the new expression.
1656 /// Subclasses may override this routine to provide different behavior.
1657 ExprResult RebuildDesignatedInitExpr(Designation &Desig,
1658 MultiExprArg ArrayExprs,
1659 SourceLocation EqualOrColonLoc,
1663 = SemaRef.ActOnDesignatedInitializer(Desig, EqualOrColonLoc, GNUSyntax,
1665 if (Result.isInvalid())
1668 ArrayExprs.release();
1669 return move(Result);
1672 /// \brief Build a new value-initialized expression.
1674 /// By default, builds the implicit value initialization without performing
1675 /// any semantic analysis. Subclasses may override this routine to provide
1676 /// different behavior.
1677 ExprResult RebuildImplicitValueInitExpr(QualType T) {
1678 return SemaRef.Owned(new (SemaRef.Context) ImplicitValueInitExpr(T));
1681 /// \brief Build a new \c va_arg expression.
1683 /// By default, performs semantic analysis to build the new expression.
1684 /// Subclasses may override this routine to provide different behavior.
1685 ExprResult RebuildVAArgExpr(SourceLocation BuiltinLoc,
1686 Expr *SubExpr, TypeSourceInfo *TInfo,
1687 SourceLocation RParenLoc) {
1688 return getSema().BuildVAArgExpr(BuiltinLoc,
1693 /// \brief Build a new expression list in parentheses.
1695 /// By default, performs semantic analysis to build the new expression.
1696 /// Subclasses may override this routine to provide different behavior.
1697 ExprResult RebuildParenListExpr(SourceLocation LParenLoc,
1698 MultiExprArg SubExprs,
1699 SourceLocation RParenLoc) {
1700 return getSema().ActOnParenListExpr(LParenLoc, RParenLoc, move(SubExprs));
1703 /// \brief Build a new address-of-label expression.
1705 /// By default, performs semantic analysis, using the name of the label
1706 /// rather than attempting to map the label statement itself.
1707 /// Subclasses may override this routine to provide different behavior.
1708 ExprResult RebuildAddrLabelExpr(SourceLocation AmpAmpLoc,
1709 SourceLocation LabelLoc, LabelDecl *Label) {
1710 return getSema().ActOnAddrLabel(AmpAmpLoc, LabelLoc, Label);
1713 /// \brief Build a new GNU statement expression.
1715 /// By default, performs semantic analysis to build the new expression.
1716 /// Subclasses may override this routine to provide different behavior.
1717 ExprResult RebuildStmtExpr(SourceLocation LParenLoc,
1719 SourceLocation RParenLoc) {
1720 return getSema().ActOnStmtExpr(LParenLoc, SubStmt, RParenLoc);
1723 /// \brief Build a new __builtin_choose_expr expression.
1725 /// By default, performs semantic analysis to build the new expression.
1726 /// Subclasses may override this routine to provide different behavior.
1727 ExprResult RebuildChooseExpr(SourceLocation BuiltinLoc,
1728 Expr *Cond, Expr *LHS, Expr *RHS,
1729 SourceLocation RParenLoc) {
1730 return SemaRef.ActOnChooseExpr(BuiltinLoc,
1735 /// \brief Build a new generic selection expression.
1737 /// By default, performs semantic analysis to build the new expression.
1738 /// Subclasses may override this routine to provide different behavior.
1739 ExprResult RebuildGenericSelectionExpr(SourceLocation KeyLoc,
1740 SourceLocation DefaultLoc,
1741 SourceLocation RParenLoc,
1742 Expr *ControllingExpr,
1743 TypeSourceInfo **Types,
1745 unsigned NumAssocs) {
1746 return getSema().CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc,
1747 ControllingExpr, Types, Exprs,
1751 /// \brief Build a new overloaded operator call expression.
1753 /// By default, performs semantic analysis to build the new expression.
1754 /// The semantic analysis provides the behavior of template instantiation,
1755 /// copying with transformations that turn what looks like an overloaded
1756 /// operator call into a use of a builtin operator, performing
1757 /// argument-dependent lookup, etc. Subclasses may override this routine to
1758 /// provide different behavior.
1759 ExprResult RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
1760 SourceLocation OpLoc,
1765 /// \brief Build a new C++ "named" cast expression, such as static_cast or
1766 /// reinterpret_cast.
1768 /// By default, this routine dispatches to one of the more-specific routines
1769 /// for a particular named case, e.g., RebuildCXXStaticCastExpr().
1770 /// Subclasses may override this routine to provide different behavior.
1771 ExprResult RebuildCXXNamedCastExpr(SourceLocation OpLoc,
1772 Stmt::StmtClass Class,
1773 SourceLocation LAngleLoc,
1774 TypeSourceInfo *TInfo,
1775 SourceLocation RAngleLoc,
1776 SourceLocation LParenLoc,
1778 SourceLocation RParenLoc) {
1780 case Stmt::CXXStaticCastExprClass:
1781 return getDerived().RebuildCXXStaticCastExpr(OpLoc, LAngleLoc, TInfo,
1782 RAngleLoc, LParenLoc,
1783 SubExpr, RParenLoc);
1785 case Stmt::CXXDynamicCastExprClass:
1786 return getDerived().RebuildCXXDynamicCastExpr(OpLoc, LAngleLoc, TInfo,
1787 RAngleLoc, LParenLoc,
1788 SubExpr, RParenLoc);
1790 case Stmt::CXXReinterpretCastExprClass:
1791 return getDerived().RebuildCXXReinterpretCastExpr(OpLoc, LAngleLoc, TInfo,
1792 RAngleLoc, LParenLoc,
1796 case Stmt::CXXConstCastExprClass:
1797 return getDerived().RebuildCXXConstCastExpr(OpLoc, LAngleLoc, TInfo,
1798 RAngleLoc, LParenLoc,
1799 SubExpr, RParenLoc);
1802 llvm_unreachable("Invalid C++ named cast");
1806 /// \brief Build a new C++ static_cast expression.
1808 /// By default, performs semantic analysis to build the new expression.
1809 /// Subclasses may override this routine to provide different behavior.
1810 ExprResult RebuildCXXStaticCastExpr(SourceLocation OpLoc,
1811 SourceLocation LAngleLoc,
1812 TypeSourceInfo *TInfo,
1813 SourceLocation RAngleLoc,
1814 SourceLocation LParenLoc,
1816 SourceLocation RParenLoc) {
1817 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_static_cast,
1819 SourceRange(LAngleLoc, RAngleLoc),
1820 SourceRange(LParenLoc, RParenLoc));
1823 /// \brief Build a new C++ dynamic_cast expression.
1825 /// By default, performs semantic analysis to build the new expression.
1826 /// Subclasses may override this routine to provide different behavior.
1827 ExprResult RebuildCXXDynamicCastExpr(SourceLocation OpLoc,
1828 SourceLocation LAngleLoc,
1829 TypeSourceInfo *TInfo,
1830 SourceLocation RAngleLoc,
1831 SourceLocation LParenLoc,
1833 SourceLocation RParenLoc) {
1834 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_dynamic_cast,
1836 SourceRange(LAngleLoc, RAngleLoc),
1837 SourceRange(LParenLoc, RParenLoc));
1840 /// \brief Build a new C++ reinterpret_cast expression.
1842 /// By default, performs semantic analysis to build the new expression.
1843 /// Subclasses may override this routine to provide different behavior.
1844 ExprResult RebuildCXXReinterpretCastExpr(SourceLocation OpLoc,
1845 SourceLocation LAngleLoc,
1846 TypeSourceInfo *TInfo,
1847 SourceLocation RAngleLoc,
1848 SourceLocation LParenLoc,
1850 SourceLocation RParenLoc) {
1851 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_reinterpret_cast,
1853 SourceRange(LAngleLoc, RAngleLoc),
1854 SourceRange(LParenLoc, RParenLoc));
1857 /// \brief Build a new C++ const_cast expression.
1859 /// By default, performs semantic analysis to build the new expression.
1860 /// Subclasses may override this routine to provide different behavior.
1861 ExprResult RebuildCXXConstCastExpr(SourceLocation OpLoc,
1862 SourceLocation LAngleLoc,
1863 TypeSourceInfo *TInfo,
1864 SourceLocation RAngleLoc,
1865 SourceLocation LParenLoc,
1867 SourceLocation RParenLoc) {
1868 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_const_cast,
1870 SourceRange(LAngleLoc, RAngleLoc),
1871 SourceRange(LParenLoc, RParenLoc));
1874 /// \brief Build a new C++ functional-style cast expression.
1876 /// By default, performs semantic analysis to build the new expression.
1877 /// Subclasses may override this routine to provide different behavior.
1878 ExprResult RebuildCXXFunctionalCastExpr(TypeSourceInfo *TInfo,
1879 SourceLocation LParenLoc,
1881 SourceLocation RParenLoc) {
1882 return getSema().BuildCXXTypeConstructExpr(TInfo, LParenLoc,
1883 MultiExprArg(&Sub, 1),
1887 /// \brief Build a new C++ typeid(type) expression.
1889 /// By default, performs semantic analysis to build the new expression.
1890 /// Subclasses may override this routine to provide different behavior.
1891 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
1892 SourceLocation TypeidLoc,
1893 TypeSourceInfo *Operand,
1894 SourceLocation RParenLoc) {
1895 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
1900 /// \brief Build a new C++ typeid(expr) expression.
1902 /// By default, performs semantic analysis to build the new expression.
1903 /// Subclasses may override this routine to provide different behavior.
1904 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
1905 SourceLocation TypeidLoc,
1907 SourceLocation RParenLoc) {
1908 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
1912 /// \brief Build a new C++ __uuidof(type) expression.
1914 /// By default, performs semantic analysis to build the new expression.
1915 /// Subclasses may override this routine to provide different behavior.
1916 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1917 SourceLocation TypeidLoc,
1918 TypeSourceInfo *Operand,
1919 SourceLocation RParenLoc) {
1920 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1924 /// \brief Build a new C++ __uuidof(expr) expression.
1926 /// By default, performs semantic analysis to build the new expression.
1927 /// Subclasses may override this routine to provide different behavior.
1928 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1929 SourceLocation TypeidLoc,
1931 SourceLocation RParenLoc) {
1932 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1936 /// \brief Build a new C++ "this" expression.
1938 /// By default, builds a new "this" expression without performing any
1939 /// semantic analysis. Subclasses may override this routine to provide
1940 /// different behavior.
1941 ExprResult RebuildCXXThisExpr(SourceLocation ThisLoc,
1944 getSema().CheckCXXThisCapture(ThisLoc);
1945 return getSema().Owned(
1946 new (getSema().Context) CXXThisExpr(ThisLoc, ThisType,
1950 /// \brief Build a new C++ throw expression.
1952 /// By default, performs semantic analysis to build the new expression.
1953 /// Subclasses may override this routine to provide different behavior.
1954 ExprResult RebuildCXXThrowExpr(SourceLocation ThrowLoc, Expr *Sub,
1955 bool IsThrownVariableInScope) {
1956 return getSema().BuildCXXThrow(ThrowLoc, Sub, IsThrownVariableInScope);
1959 /// \brief Build a new C++ default-argument expression.
1961 /// By default, builds a new default-argument expression, which does not
1962 /// require any semantic analysis. Subclasses may override this routine to
1963 /// provide different behavior.
1964 ExprResult RebuildCXXDefaultArgExpr(SourceLocation Loc,
1965 ParmVarDecl *Param) {
1966 return getSema().Owned(CXXDefaultArgExpr::Create(getSema().Context, Loc,
1970 /// \brief Build a new C++ zero-initialization expression.
1972 /// By default, performs semantic analysis to build the new expression.
1973 /// Subclasses may override this routine to provide different behavior.
1974 ExprResult RebuildCXXScalarValueInitExpr(TypeSourceInfo *TSInfo,
1975 SourceLocation LParenLoc,
1976 SourceLocation RParenLoc) {
1977 return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc,
1978 MultiExprArg(getSema(), 0, 0),
1982 /// \brief Build a new C++ "new" expression.
1984 /// By default, performs semantic analysis to build the new expression.
1985 /// Subclasses may override this routine to provide different behavior.
1986 ExprResult RebuildCXXNewExpr(SourceLocation StartLoc,
1988 SourceLocation PlacementLParen,
1989 MultiExprArg PlacementArgs,
1990 SourceLocation PlacementRParen,
1991 SourceRange TypeIdParens,
1992 QualType AllocatedType,
1993 TypeSourceInfo *AllocatedTypeInfo,
1995 SourceRange DirectInitRange,
1996 Expr *Initializer) {
1997 return getSema().BuildCXXNew(StartLoc, UseGlobal,
1999 move(PlacementArgs),
2009 /// \brief Build a new C++ "delete" expression.
2011 /// By default, performs semantic analysis to build the new expression.
2012 /// Subclasses may override this routine to provide different behavior.
2013 ExprResult RebuildCXXDeleteExpr(SourceLocation StartLoc,
2014 bool IsGlobalDelete,
2017 return getSema().ActOnCXXDelete(StartLoc, IsGlobalDelete, IsArrayForm,
2021 /// \brief Build a new unary type trait expression.
2023 /// By default, performs semantic analysis to build the new expression.
2024 /// Subclasses may override this routine to provide different behavior.
2025 ExprResult RebuildUnaryTypeTrait(UnaryTypeTrait Trait,
2026 SourceLocation StartLoc,
2028 SourceLocation RParenLoc) {
2029 return getSema().BuildUnaryTypeTrait(Trait, StartLoc, T, RParenLoc);
2032 /// \brief Build a new binary type trait expression.
2034 /// By default, performs semantic analysis to build the new expression.
2035 /// Subclasses may override this routine to provide different behavior.
2036 ExprResult RebuildBinaryTypeTrait(BinaryTypeTrait Trait,
2037 SourceLocation StartLoc,
2038 TypeSourceInfo *LhsT,
2039 TypeSourceInfo *RhsT,
2040 SourceLocation RParenLoc) {
2041 return getSema().BuildBinaryTypeTrait(Trait, StartLoc, LhsT, RhsT, RParenLoc);
2044 /// \brief Build a new type trait expression.
2046 /// By default, performs semantic analysis to build the new expression.
2047 /// Subclasses may override this routine to provide different behavior.
2048 ExprResult RebuildTypeTrait(TypeTrait Trait,
2049 SourceLocation StartLoc,
2050 ArrayRef<TypeSourceInfo *> Args,
2051 SourceLocation RParenLoc) {
2052 return getSema().BuildTypeTrait(Trait, StartLoc, Args, RParenLoc);
2055 /// \brief Build a new array type trait expression.
2057 /// By default, performs semantic analysis to build the new expression.
2058 /// Subclasses may override this routine to provide different behavior.
2059 ExprResult RebuildArrayTypeTrait(ArrayTypeTrait Trait,
2060 SourceLocation StartLoc,
2061 TypeSourceInfo *TSInfo,
2063 SourceLocation RParenLoc) {
2064 return getSema().BuildArrayTypeTrait(Trait, StartLoc, TSInfo, DimExpr, RParenLoc);
2067 /// \brief Build a new expression trait expression.
2069 /// By default, performs semantic analysis to build the new expression.
2070 /// Subclasses may override this routine to provide different behavior.
2071 ExprResult RebuildExpressionTrait(ExpressionTrait Trait,
2072 SourceLocation StartLoc,
2074 SourceLocation RParenLoc) {
2075 return getSema().BuildExpressionTrait(Trait, StartLoc, Queried, RParenLoc);
2078 /// \brief Build a new (previously unresolved) declaration reference
2081 /// By default, performs semantic analysis to build the new expression.
2082 /// Subclasses may override this routine to provide different behavior.
2083 ExprResult RebuildDependentScopeDeclRefExpr(
2084 NestedNameSpecifierLoc QualifierLoc,
2085 SourceLocation TemplateKWLoc,
2086 const DeclarationNameInfo &NameInfo,
2087 const TemplateArgumentListInfo *TemplateArgs) {
2089 SS.Adopt(QualifierLoc);
2091 if (TemplateArgs || TemplateKWLoc.isValid())
2092 return getSema().BuildQualifiedTemplateIdExpr(SS, TemplateKWLoc,
2093 NameInfo, TemplateArgs);
2095 return getSema().BuildQualifiedDeclarationNameExpr(SS, NameInfo);
2098 /// \brief Build a new template-id expression.
2100 /// By default, performs semantic analysis to build the new expression.
2101 /// Subclasses may override this routine to provide different behavior.
2102 ExprResult RebuildTemplateIdExpr(const CXXScopeSpec &SS,
2103 SourceLocation TemplateKWLoc,
2106 const TemplateArgumentListInfo *TemplateArgs) {
2107 return getSema().BuildTemplateIdExpr(SS, TemplateKWLoc, R, RequiresADL,
2111 /// \brief Build a new object-construction expression.
2113 /// By default, performs semantic analysis to build the new expression.
2114 /// Subclasses may override this routine to provide different behavior.
2115 ExprResult RebuildCXXConstructExpr(QualType T,
2117 CXXConstructorDecl *Constructor,
2120 bool HadMultipleCandidates,
2121 bool RequiresZeroInit,
2122 CXXConstructExpr::ConstructionKind ConstructKind,
2123 SourceRange ParenRange) {
2124 ASTOwningVector<Expr*> ConvertedArgs(SemaRef);
2125 if (getSema().CompleteConstructorCall(Constructor, move(Args), Loc,
2129 return getSema().BuildCXXConstructExpr(Loc, T, Constructor, IsElidable,
2130 move_arg(ConvertedArgs),
2131 HadMultipleCandidates,
2132 RequiresZeroInit, ConstructKind,
2136 /// \brief Build a new object-construction expression.
2138 /// By default, performs semantic analysis to build the new expression.
2139 /// Subclasses may override this routine to provide different behavior.
2140 ExprResult RebuildCXXTemporaryObjectExpr(TypeSourceInfo *TSInfo,
2141 SourceLocation LParenLoc,
2143 SourceLocation RParenLoc) {
2144 return getSema().BuildCXXTypeConstructExpr(TSInfo,
2150 /// \brief Build a new object-construction expression.
2152 /// By default, performs semantic analysis to build the new expression.
2153 /// Subclasses may override this routine to provide different behavior.
2154 ExprResult RebuildCXXUnresolvedConstructExpr(TypeSourceInfo *TSInfo,
2155 SourceLocation LParenLoc,
2157 SourceLocation RParenLoc) {
2158 return getSema().BuildCXXTypeConstructExpr(TSInfo,
2164 /// \brief Build a new member reference expression.
2166 /// By default, performs semantic analysis to build the new expression.
2167 /// Subclasses may override this routine to provide different behavior.
2168 ExprResult RebuildCXXDependentScopeMemberExpr(Expr *BaseE,
2171 SourceLocation OperatorLoc,
2172 NestedNameSpecifierLoc QualifierLoc,
2173 SourceLocation TemplateKWLoc,
2174 NamedDecl *FirstQualifierInScope,
2175 const DeclarationNameInfo &MemberNameInfo,
2176 const TemplateArgumentListInfo *TemplateArgs) {
2178 SS.Adopt(QualifierLoc);
2180 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
2181 OperatorLoc, IsArrow,
2183 FirstQualifierInScope,
2188 /// \brief Build a new member reference expression.
2190 /// By default, performs semantic analysis to build the new expression.
2191 /// Subclasses may override this routine to provide different behavior.
2192 ExprResult RebuildUnresolvedMemberExpr(Expr *BaseE, QualType BaseType,
2193 SourceLocation OperatorLoc,
2195 NestedNameSpecifierLoc QualifierLoc,
2196 SourceLocation TemplateKWLoc,
2197 NamedDecl *FirstQualifierInScope,
2199 const TemplateArgumentListInfo *TemplateArgs) {
2201 SS.Adopt(QualifierLoc);
2203 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
2204 OperatorLoc, IsArrow,
2206 FirstQualifierInScope,
2210 /// \brief Build a new noexcept expression.
2212 /// By default, performs semantic analysis to build the new expression.
2213 /// Subclasses may override this routine to provide different behavior.
2214 ExprResult RebuildCXXNoexceptExpr(SourceRange Range, Expr *Arg) {
2215 return SemaRef.BuildCXXNoexceptExpr(Range.getBegin(), Arg, Range.getEnd());
2218 /// \brief Build a new expression to compute the length of a parameter pack.
2219 ExprResult RebuildSizeOfPackExpr(SourceLocation OperatorLoc, NamedDecl *Pack,
2220 SourceLocation PackLoc,
2221 SourceLocation RParenLoc,
2222 llvm::Optional<unsigned> Length) {
2224 return new (SemaRef.Context) SizeOfPackExpr(SemaRef.Context.getSizeType(),
2225 OperatorLoc, Pack, PackLoc,
2226 RParenLoc, *Length);
2228 return new (SemaRef.Context) SizeOfPackExpr(SemaRef.Context.getSizeType(),
2229 OperatorLoc, Pack, PackLoc,
2233 /// \brief Build a new Objective-C boxed expression.
2235 /// By default, performs semantic analysis to build the new expression.
2236 /// Subclasses may override this routine to provide different behavior.
2237 ExprResult RebuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) {
2238 return getSema().BuildObjCBoxedExpr(SR, ValueExpr);
2241 /// \brief Build a new Objective-C array literal.
2243 /// By default, performs semantic analysis to build the new expression.
2244 /// Subclasses may override this routine to provide different behavior.
2245 ExprResult RebuildObjCArrayLiteral(SourceRange Range,
2246 Expr **Elements, unsigned NumElements) {
2247 return getSema().BuildObjCArrayLiteral(Range,
2248 MultiExprArg(Elements, NumElements));
2251 ExprResult RebuildObjCSubscriptRefExpr(SourceLocation RB,
2252 Expr *Base, Expr *Key,
2253 ObjCMethodDecl *getterMethod,
2254 ObjCMethodDecl *setterMethod) {
2255 return getSema().BuildObjCSubscriptExpression(RB, Base, Key,
2256 getterMethod, setterMethod);
2259 /// \brief Build a new Objective-C dictionary literal.
2261 /// By default, performs semantic analysis to build the new expression.
2262 /// Subclasses may override this routine to provide different behavior.
2263 ExprResult RebuildObjCDictionaryLiteral(SourceRange Range,
2264 ObjCDictionaryElement *Elements,
2265 unsigned NumElements) {
2266 return getSema().BuildObjCDictionaryLiteral(Range, Elements, NumElements);
2269 /// \brief Build a new Objective-C \@encode expression.
2271 /// By default, performs semantic analysis to build the new expression.
2272 /// Subclasses may override this routine to provide different behavior.
2273 ExprResult RebuildObjCEncodeExpr(SourceLocation AtLoc,
2274 TypeSourceInfo *EncodeTypeInfo,
2275 SourceLocation RParenLoc) {
2276 return SemaRef.Owned(SemaRef.BuildObjCEncodeExpression(AtLoc, EncodeTypeInfo,
2280 /// \brief Build a new Objective-C class message.
2281 ExprResult RebuildObjCMessageExpr(TypeSourceInfo *ReceiverTypeInfo,
2283 ArrayRef<SourceLocation> SelectorLocs,
2284 ObjCMethodDecl *Method,
2285 SourceLocation LBracLoc,
2287 SourceLocation RBracLoc) {
2288 return SemaRef.BuildClassMessage(ReceiverTypeInfo,
2289 ReceiverTypeInfo->getType(),
2290 /*SuperLoc=*/SourceLocation(),
2291 Sel, Method, LBracLoc, SelectorLocs,
2292 RBracLoc, move(Args));
2295 /// \brief Build a new Objective-C instance message.
2296 ExprResult RebuildObjCMessageExpr(Expr *Receiver,
2298 ArrayRef<SourceLocation> SelectorLocs,
2299 ObjCMethodDecl *Method,
2300 SourceLocation LBracLoc,
2302 SourceLocation RBracLoc) {
2303 return SemaRef.BuildInstanceMessage(Receiver,
2304 Receiver->getType(),
2305 /*SuperLoc=*/SourceLocation(),
2306 Sel, Method, LBracLoc, SelectorLocs,
2307 RBracLoc, move(Args));
2310 /// \brief Build a new Objective-C ivar reference expression.
2312 /// By default, performs semantic analysis to build the new expression.
2313 /// Subclasses may override this routine to provide different behavior.
2314 ExprResult RebuildObjCIvarRefExpr(Expr *BaseArg, ObjCIvarDecl *Ivar,
2315 SourceLocation IvarLoc,
2316 bool IsArrow, bool IsFreeIvar) {
2317 // FIXME: We lose track of the IsFreeIvar bit.
2319 ExprResult Base = getSema().Owned(BaseArg);
2320 LookupResult R(getSema(), Ivar->getDeclName(), IvarLoc,
2321 Sema::LookupMemberName);
2322 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
2326 if (Result.isInvalid() || Base.isInvalid())
2330 return move(Result);
2332 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
2333 /*FIXME:*/IvarLoc, IsArrow,
2334 SS, SourceLocation(),
2335 /*FirstQualifierInScope=*/0,
2337 /*TemplateArgs=*/0);
2340 /// \brief Build a new Objective-C property reference expression.
2342 /// By default, performs semantic analysis to build the new expression.
2343 /// Subclasses may override this routine to provide different behavior.
2344 ExprResult RebuildObjCPropertyRefExpr(Expr *BaseArg,
2345 ObjCPropertyDecl *Property,
2346 SourceLocation PropertyLoc) {
2348 ExprResult Base = getSema().Owned(BaseArg);
2349 LookupResult R(getSema(), Property->getDeclName(), PropertyLoc,
2350 Sema::LookupMemberName);
2351 bool IsArrow = false;
2352 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
2353 /*FIME:*/PropertyLoc,
2355 if (Result.isInvalid() || Base.isInvalid())
2359 return move(Result);
2361 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
2362 /*FIXME:*/PropertyLoc, IsArrow,
2363 SS, SourceLocation(),
2364 /*FirstQualifierInScope=*/0,
2366 /*TemplateArgs=*/0);
2369 /// \brief Build a new Objective-C property reference expression.
2371 /// By default, performs semantic analysis to build the new expression.
2372 /// Subclasses may override this routine to provide different behavior.
2373 ExprResult RebuildObjCPropertyRefExpr(Expr *Base, QualType T,
2374 ObjCMethodDecl *Getter,
2375 ObjCMethodDecl *Setter,
2376 SourceLocation PropertyLoc) {
2377 // Since these expressions can only be value-dependent, we do not
2378 // need to perform semantic analysis again.
2380 new (getSema().Context) ObjCPropertyRefExpr(Getter, Setter, T,
2381 VK_LValue, OK_ObjCProperty,
2382 PropertyLoc, Base));
2385 /// \brief Build a new Objective-C "isa" expression.
2387 /// By default, performs semantic analysis to build the new expression.
2388 /// Subclasses may override this routine to provide different behavior.
2389 ExprResult RebuildObjCIsaExpr(Expr *BaseArg, SourceLocation IsaLoc,
2392 ExprResult Base = getSema().Owned(BaseArg);
2393 LookupResult R(getSema(), &getSema().Context.Idents.get("isa"), IsaLoc,
2394 Sema::LookupMemberName);
2395 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
2398 if (Result.isInvalid() || Base.isInvalid())
2402 return move(Result);
2404 return getSema().BuildMemberReferenceExpr(Base.get(), Base.get()->getType(),
2405 /*FIXME:*/IsaLoc, IsArrow,
2406 SS, SourceLocation(),
2407 /*FirstQualifierInScope=*/0,
2409 /*TemplateArgs=*/0);
2412 /// \brief Build a new shuffle vector expression.
2414 /// By default, performs semantic analysis to build the new expression.
2415 /// Subclasses may override this routine to provide different behavior.
2416 ExprResult RebuildShuffleVectorExpr(SourceLocation BuiltinLoc,
2417 MultiExprArg SubExprs,
2418 SourceLocation RParenLoc) {
2419 // Find the declaration for __builtin_shufflevector
2420 const IdentifierInfo &Name
2421 = SemaRef.Context.Idents.get("__builtin_shufflevector");
2422 TranslationUnitDecl *TUDecl = SemaRef.Context.getTranslationUnitDecl();
2423 DeclContext::lookup_result Lookup = TUDecl->lookup(DeclarationName(&Name));
2424 assert(Lookup.first != Lookup.second && "No __builtin_shufflevector?");
2426 // Build a reference to the __builtin_shufflevector builtin
2427 FunctionDecl *Builtin = cast<FunctionDecl>(*Lookup.first);
2429 = SemaRef.Owned(new (SemaRef.Context) DeclRefExpr(Builtin, false,
2431 VK_LValue, BuiltinLoc));
2432 Callee = SemaRef.UsualUnaryConversions(Callee.take());
2433 if (Callee.isInvalid())
2436 // Build the CallExpr
2437 unsigned NumSubExprs = SubExprs.size();
2438 Expr **Subs = (Expr **)SubExprs.release();
2439 ExprResult TheCall = SemaRef.Owned(
2440 new (SemaRef.Context) CallExpr(SemaRef.Context, Callee.take(),
2442 Builtin->getCallResultType(),
2443 Expr::getValueKindForType(Builtin->getResultType()),
2446 // Type-check the __builtin_shufflevector expression.
2447 return SemaRef.SemaBuiltinShuffleVector(cast<CallExpr>(TheCall.take()));
2450 /// \brief Build a new template argument pack expansion.
2452 /// By default, performs semantic analysis to build a new pack expansion
2453 /// for a template argument. Subclasses may override this routine to provide
2454 /// different behavior.
2455 TemplateArgumentLoc RebuildPackExpansion(TemplateArgumentLoc Pattern,
2456 SourceLocation EllipsisLoc,
2457 llvm::Optional<unsigned> NumExpansions) {
2458 switch (Pattern.getArgument().getKind()) {
2459 case TemplateArgument::Expression: {
2461 = getSema().CheckPackExpansion(Pattern.getSourceExpression(),
2462 EllipsisLoc, NumExpansions);
2463 if (Result.isInvalid())
2464 return TemplateArgumentLoc();
2466 return TemplateArgumentLoc(Result.get(), Result.get());
2469 case TemplateArgument::Template:
2470 return TemplateArgumentLoc(TemplateArgument(
2471 Pattern.getArgument().getAsTemplate(),
2473 Pattern.getTemplateQualifierLoc(),
2474 Pattern.getTemplateNameLoc(),
2477 case TemplateArgument::Null:
2478 case TemplateArgument::Integral:
2479 case TemplateArgument::Declaration:
2480 case TemplateArgument::Pack:
2481 case TemplateArgument::TemplateExpansion:
2482 llvm_unreachable("Pack expansion pattern has no parameter packs");
2484 case TemplateArgument::Type:
2485 if (TypeSourceInfo *Expansion
2486 = getSema().CheckPackExpansion(Pattern.getTypeSourceInfo(),
2489 return TemplateArgumentLoc(TemplateArgument(Expansion->getType()),
2494 return TemplateArgumentLoc();
2497 /// \brief Build a new expression pack expansion.
2499 /// By default, performs semantic analysis to build a new pack expansion
2500 /// for an expression. Subclasses may override this routine to provide
2501 /// different behavior.
2502 ExprResult RebuildPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
2503 llvm::Optional<unsigned> NumExpansions) {
2504 return getSema().CheckPackExpansion(Pattern, EllipsisLoc, NumExpansions);
2507 /// \brief Build a new atomic operation expression.
2509 /// By default, performs semantic analysis to build the new expression.
2510 /// Subclasses may override this routine to provide different behavior.
2511 ExprResult RebuildAtomicExpr(SourceLocation BuiltinLoc,
2512 MultiExprArg SubExprs,
2514 AtomicExpr::AtomicOp Op,
2515 SourceLocation RParenLoc) {
2516 // Just create the expression; there is not any interesting semantic
2517 // analysis here because we can't actually build an AtomicExpr until
2518 // we are sure it is semantically sound.
2519 unsigned NumSubExprs = SubExprs.size();
2520 Expr **Subs = (Expr **)SubExprs.release();
2521 return new (SemaRef.Context) AtomicExpr(BuiltinLoc, Subs,
2522 NumSubExprs, RetTy, Op,
2527 TypeLoc TransformTypeInObjectScope(TypeLoc TL,
2528 QualType ObjectType,
2529 NamedDecl *FirstQualifierInScope,
2532 TypeSourceInfo *TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
2533 QualType ObjectType,
2534 NamedDecl *FirstQualifierInScope,
2538 template<typename Derived>
2539 StmtResult TreeTransform<Derived>::TransformStmt(Stmt *S) {
2541 return SemaRef.Owned(S);
2543 switch (S->getStmtClass()) {
2544 case Stmt::NoStmtClass: break;
2546 // Transform individual statement nodes
2547 #define STMT(Node, Parent) \
2548 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(S));
2549 #define ABSTRACT_STMT(Node)
2550 #define EXPR(Node, Parent)
2551 #include "clang/AST/StmtNodes.inc"
2553 // Transform expressions by calling TransformExpr.
2554 #define STMT(Node, Parent)
2555 #define ABSTRACT_STMT(Stmt)
2556 #define EXPR(Node, Parent) case Stmt::Node##Class:
2557 #include "clang/AST/StmtNodes.inc"
2559 ExprResult E = getDerived().TransformExpr(cast<Expr>(S));
2563 return getSema().ActOnExprStmt(getSema().MakeFullExpr(E.take()));
2567 return SemaRef.Owned(S);
2571 template<typename Derived>
2572 ExprResult TreeTransform<Derived>::TransformExpr(Expr *E) {
2574 return SemaRef.Owned(E);
2576 switch (E->getStmtClass()) {
2577 case Stmt::NoStmtClass: break;
2578 #define STMT(Node, Parent) case Stmt::Node##Class: break;
2579 #define ABSTRACT_STMT(Stmt)
2580 #define EXPR(Node, Parent) \
2581 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(E));
2582 #include "clang/AST/StmtNodes.inc"
2585 return SemaRef.Owned(E);
2588 template<typename Derived>
2589 bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
2592 SmallVectorImpl<Expr *> &Outputs,
2594 for (unsigned I = 0; I != NumInputs; ++I) {
2595 // If requested, drop call arguments that need to be dropped.
2596 if (IsCall && getDerived().DropCallArgument(Inputs[I])) {
2603 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(Inputs[I])) {
2604 Expr *Pattern = Expansion->getPattern();
2606 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2607 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
2608 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
2610 // Determine whether the set of unexpanded parameter packs can and should
2613 bool RetainExpansion = false;
2614 llvm::Optional<unsigned> OrigNumExpansions
2615 = Expansion->getNumExpansions();
2616 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
2617 if (getDerived().TryExpandParameterPacks(Expansion->getEllipsisLoc(),
2618 Pattern->getSourceRange(),
2620 Expand, RetainExpansion,
2625 // The transform has determined that we should perform a simple
2626 // transformation on the pack expansion, producing another pack
2628 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
2629 ExprResult OutPattern = getDerived().TransformExpr(Pattern);
2630 if (OutPattern.isInvalid())
2633 ExprResult Out = getDerived().RebuildPackExpansion(OutPattern.get(),
2634 Expansion->getEllipsisLoc(),
2636 if (Out.isInvalid())
2641 Outputs.push_back(Out.get());
2645 // Record right away that the argument was changed. This needs
2646 // to happen even if the array expands to nothing.
2647 if (ArgChanged) *ArgChanged = true;
2649 // The transform has determined that we should perform an elementwise
2650 // expansion of the pattern. Do so.
2651 for (unsigned I = 0; I != *NumExpansions; ++I) {
2652 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
2653 ExprResult Out = getDerived().TransformExpr(Pattern);
2654 if (Out.isInvalid())
2657 if (Out.get()->containsUnexpandedParameterPack()) {
2658 Out = RebuildPackExpansion(Out.get(), Expansion->getEllipsisLoc(),
2660 if (Out.isInvalid())
2664 Outputs.push_back(Out.get());
2670 ExprResult Result = getDerived().TransformExpr(Inputs[I]);
2671 if (Result.isInvalid())
2674 if (Result.get() != Inputs[I] && ArgChanged)
2677 Outputs.push_back(Result.get());
2683 template<typename Derived>
2684 NestedNameSpecifierLoc
2685 TreeTransform<Derived>::TransformNestedNameSpecifierLoc(
2686 NestedNameSpecifierLoc NNS,
2687 QualType ObjectType,
2688 NamedDecl *FirstQualifierInScope) {
2689 SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
2690 for (NestedNameSpecifierLoc Qualifier = NNS; Qualifier;
2691 Qualifier = Qualifier.getPrefix())
2692 Qualifiers.push_back(Qualifier);
2695 while (!Qualifiers.empty()) {
2696 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
2697 NestedNameSpecifier *QNNS = Q.getNestedNameSpecifier();
2699 switch (QNNS->getKind()) {
2700 case NestedNameSpecifier::Identifier:
2701 if (SemaRef.BuildCXXNestedNameSpecifier(/*Scope=*/0,
2702 *QNNS->getAsIdentifier(),
2703 Q.getLocalBeginLoc(),
2705 ObjectType, false, SS,
2706 FirstQualifierInScope, false))
2707 return NestedNameSpecifierLoc();
2711 case NestedNameSpecifier::Namespace: {
2713 = cast_or_null<NamespaceDecl>(
2714 getDerived().TransformDecl(
2715 Q.getLocalBeginLoc(),
2716 QNNS->getAsNamespace()));
2717 SS.Extend(SemaRef.Context, NS, Q.getLocalBeginLoc(), Q.getLocalEndLoc());
2721 case NestedNameSpecifier::NamespaceAlias: {
2722 NamespaceAliasDecl *Alias
2723 = cast_or_null<NamespaceAliasDecl>(
2724 getDerived().TransformDecl(Q.getLocalBeginLoc(),
2725 QNNS->getAsNamespaceAlias()));
2726 SS.Extend(SemaRef.Context, Alias, Q.getLocalBeginLoc(),
2727 Q.getLocalEndLoc());
2731 case NestedNameSpecifier::Global:
2732 // There is no meaningful transformation that one could perform on the
2734 SS.MakeGlobal(SemaRef.Context, Q.getBeginLoc());
2737 case NestedNameSpecifier::TypeSpecWithTemplate:
2738 case NestedNameSpecifier::TypeSpec: {
2739 TypeLoc TL = TransformTypeInObjectScope(Q.getTypeLoc(), ObjectType,
2740 FirstQualifierInScope, SS);
2743 return NestedNameSpecifierLoc();
2745 if (TL.getType()->isDependentType() || TL.getType()->isRecordType() ||
2746 (SemaRef.getLangOpts().CPlusPlus0x &&
2747 TL.getType()->isEnumeralType())) {
2748 assert(!TL.getType().hasLocalQualifiers() &&
2749 "Can't get cv-qualifiers here");
2750 if (TL.getType()->isEnumeralType())
2751 SemaRef.Diag(TL.getBeginLoc(),
2752 diag::warn_cxx98_compat_enum_nested_name_spec);
2753 SS.Extend(SemaRef.Context, /*FIXME:*/SourceLocation(), TL,
2754 Q.getLocalEndLoc());
2757 // If the nested-name-specifier is an invalid type def, don't emit an
2758 // error because a previous error should have already been emitted.
2759 TypedefTypeLoc* TTL = dyn_cast<TypedefTypeLoc>(&TL);
2760 if (!TTL || !TTL->getTypedefNameDecl()->isInvalidDecl()) {
2761 SemaRef.Diag(TL.getBeginLoc(), diag::err_nested_name_spec_non_tag)
2762 << TL.getType() << SS.getRange();
2764 return NestedNameSpecifierLoc();
2768 // The qualifier-in-scope and object type only apply to the leftmost entity.
2769 FirstQualifierInScope = 0;
2770 ObjectType = QualType();
2773 // Don't rebuild the nested-name-specifier if we don't have to.
2774 if (SS.getScopeRep() == NNS.getNestedNameSpecifier() &&
2775 !getDerived().AlwaysRebuild())
2778 // If we can re-use the source-location data from the original
2779 // nested-name-specifier, do so.
2780 if (SS.location_size() == NNS.getDataLength() &&
2781 memcmp(SS.location_data(), NNS.getOpaqueData(), SS.location_size()) == 0)
2782 return NestedNameSpecifierLoc(SS.getScopeRep(), NNS.getOpaqueData());
2784 // Allocate new nested-name-specifier location information.
2785 return SS.getWithLocInContext(SemaRef.Context);
2788 template<typename Derived>
2790 TreeTransform<Derived>
2791 ::TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo) {
2792 DeclarationName Name = NameInfo.getName();
2794 return DeclarationNameInfo();
2796 switch (Name.getNameKind()) {
2797 case DeclarationName::Identifier:
2798 case DeclarationName::ObjCZeroArgSelector:
2799 case DeclarationName::ObjCOneArgSelector:
2800 case DeclarationName::ObjCMultiArgSelector:
2801 case DeclarationName::CXXOperatorName:
2802 case DeclarationName::CXXLiteralOperatorName:
2803 case DeclarationName::CXXUsingDirective:
2806 case DeclarationName::CXXConstructorName:
2807 case DeclarationName::CXXDestructorName:
2808 case DeclarationName::CXXConversionFunctionName: {
2809 TypeSourceInfo *NewTInfo;
2810 CanQualType NewCanTy;
2811 if (TypeSourceInfo *OldTInfo = NameInfo.getNamedTypeInfo()) {
2812 NewTInfo = getDerived().TransformType(OldTInfo);
2814 return DeclarationNameInfo();
2815 NewCanTy = SemaRef.Context.getCanonicalType(NewTInfo->getType());
2819 TemporaryBase Rebase(*this, NameInfo.getLoc(), Name);
2820 QualType NewT = getDerived().TransformType(Name.getCXXNameType());
2822 return DeclarationNameInfo();
2823 NewCanTy = SemaRef.Context.getCanonicalType(NewT);
2826 DeclarationName NewName
2827 = SemaRef.Context.DeclarationNames.getCXXSpecialName(Name.getNameKind(),
2829 DeclarationNameInfo NewNameInfo(NameInfo);
2830 NewNameInfo.setName(NewName);
2831 NewNameInfo.setNamedTypeInfo(NewTInfo);
2836 llvm_unreachable("Unknown name kind.");
2839 template<typename Derived>
2841 TreeTransform<Derived>::TransformTemplateName(CXXScopeSpec &SS,
2843 SourceLocation NameLoc,
2844 QualType ObjectType,
2845 NamedDecl *FirstQualifierInScope) {
2846 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
2847 TemplateDecl *Template = QTN->getTemplateDecl();
2848 assert(Template && "qualified template name must refer to a template");
2850 TemplateDecl *TransTemplate
2851 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
2854 return TemplateName();
2856 if (!getDerived().AlwaysRebuild() &&
2857 SS.getScopeRep() == QTN->getQualifier() &&
2858 TransTemplate == Template)
2861 return getDerived().RebuildTemplateName(SS, QTN->hasTemplateKeyword(),
2865 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2866 if (SS.getScopeRep()) {
2867 // These apply to the scope specifier, not the template.
2868 ObjectType = QualType();
2869 FirstQualifierInScope = 0;
2872 if (!getDerived().AlwaysRebuild() &&
2873 SS.getScopeRep() == DTN->getQualifier() &&
2874 ObjectType.isNull())
2877 if (DTN->isIdentifier()) {
2878 return getDerived().RebuildTemplateName(SS,
2879 *DTN->getIdentifier(),
2882 FirstQualifierInScope);
2885 return getDerived().RebuildTemplateName(SS, DTN->getOperator(), NameLoc,
2889 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2890 TemplateDecl *TransTemplate
2891 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
2894 return TemplateName();
2896 if (!getDerived().AlwaysRebuild() &&
2897 TransTemplate == Template)
2900 return TemplateName(TransTemplate);
2903 if (SubstTemplateTemplateParmPackStorage *SubstPack
2904 = Name.getAsSubstTemplateTemplateParmPack()) {
2905 TemplateTemplateParmDecl *TransParam
2906 = cast_or_null<TemplateTemplateParmDecl>(
2907 getDerived().TransformDecl(NameLoc, SubstPack->getParameterPack()));
2909 return TemplateName();
2911 if (!getDerived().AlwaysRebuild() &&
2912 TransParam == SubstPack->getParameterPack())
2915 return getDerived().RebuildTemplateName(TransParam,
2916 SubstPack->getArgumentPack());
2919 // These should be getting filtered out before they reach the AST.
2920 llvm_unreachable("overloaded function decl survived to here");
2923 template<typename Derived>
2924 void TreeTransform<Derived>::InventTemplateArgumentLoc(
2925 const TemplateArgument &Arg,
2926 TemplateArgumentLoc &Output) {
2927 SourceLocation Loc = getDerived().getBaseLocation();
2928 switch (Arg.getKind()) {
2929 case TemplateArgument::Null:
2930 llvm_unreachable("null template argument in TreeTransform");
2933 case TemplateArgument::Type:
2934 Output = TemplateArgumentLoc(Arg,
2935 SemaRef.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
2939 case TemplateArgument::Template:
2940 case TemplateArgument::TemplateExpansion: {
2941 NestedNameSpecifierLocBuilder Builder;
2942 TemplateName Template = Arg.getAsTemplate();
2943 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
2944 Builder.MakeTrivial(SemaRef.Context, DTN->getQualifier(), Loc);
2945 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2946 Builder.MakeTrivial(SemaRef.Context, QTN->getQualifier(), Loc);
2948 if (Arg.getKind() == TemplateArgument::Template)
2949 Output = TemplateArgumentLoc(Arg,
2950 Builder.getWithLocInContext(SemaRef.Context),
2953 Output = TemplateArgumentLoc(Arg,
2954 Builder.getWithLocInContext(SemaRef.Context),
2960 case TemplateArgument::Expression:
2961 Output = TemplateArgumentLoc(Arg, Arg.getAsExpr());
2964 case TemplateArgument::Declaration:
2965 case TemplateArgument::Integral:
2966 case TemplateArgument::Pack:
2967 Output = TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
2972 template<typename Derived>
2973 bool TreeTransform<Derived>::TransformTemplateArgument(
2974 const TemplateArgumentLoc &Input,
2975 TemplateArgumentLoc &Output) {
2976 const TemplateArgument &Arg = Input.getArgument();
2977 switch (Arg.getKind()) {
2978 case TemplateArgument::Null:
2979 case TemplateArgument::Integral:
2983 case TemplateArgument::Type: {
2984 TypeSourceInfo *DI = Input.getTypeSourceInfo();
2986 DI = InventTypeSourceInfo(Input.getArgument().getAsType());
2988 DI = getDerived().TransformType(DI);
2989 if (!DI) return true;
2991 Output = TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2995 case TemplateArgument::Declaration: {
2996 // FIXME: we should never have to transform one of these.
2997 DeclarationName Name;
2998 if (NamedDecl *ND = dyn_cast<NamedDecl>(Arg.getAsDecl()))
2999 Name = ND->getDeclName();
3000 TemporaryBase Rebase(*this, Input.getLocation(), Name);
3001 Decl *D = getDerived().TransformDecl(Input.getLocation(), Arg.getAsDecl());
3002 if (!D) return true;
3004 Expr *SourceExpr = Input.getSourceDeclExpression();
3006 EnterExpressionEvaluationContext Unevaluated(getSema(),
3007 Sema::ConstantEvaluated);
3008 ExprResult E = getDerived().TransformExpr(SourceExpr);
3009 E = SemaRef.ActOnConstantExpression(E);
3010 SourceExpr = (E.isInvalid() ? 0 : E.take());
3013 Output = TemplateArgumentLoc(TemplateArgument(D), SourceExpr);
3017 case TemplateArgument::Template: {
3018 NestedNameSpecifierLoc QualifierLoc = Input.getTemplateQualifierLoc();
3020 QualifierLoc = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc);
3026 SS.Adopt(QualifierLoc);
3027 TemplateName Template
3028 = getDerived().TransformTemplateName(SS, Arg.getAsTemplate(),
3029 Input.getTemplateNameLoc());
3030 if (Template.isNull())
3033 Output = TemplateArgumentLoc(TemplateArgument(Template), QualifierLoc,
3034 Input.getTemplateNameLoc());
3038 case TemplateArgument::TemplateExpansion:
3039 llvm_unreachable("Caller should expand pack expansions");
3041 case TemplateArgument::Expression: {
3042 // Template argument expressions are constant expressions.
3043 EnterExpressionEvaluationContext Unevaluated(getSema(),
3044 Sema::ConstantEvaluated);
3046 Expr *InputExpr = Input.getSourceExpression();
3047 if (!InputExpr) InputExpr = Input.getArgument().getAsExpr();
3049 ExprResult E = getDerived().TransformExpr(InputExpr);
3050 E = SemaRef.ActOnConstantExpression(E);
3051 if (E.isInvalid()) return true;
3052 Output = TemplateArgumentLoc(TemplateArgument(E.take()), E.take());
3056 case TemplateArgument::Pack: {
3057 SmallVector<TemplateArgument, 4> TransformedArgs;
3058 TransformedArgs.reserve(Arg.pack_size());
3059 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
3060 AEnd = Arg.pack_end();
3063 // FIXME: preserve source information here when we start
3064 // caring about parameter packs.
3066 TemplateArgumentLoc InputArg;
3067 TemplateArgumentLoc OutputArg;
3068 getDerived().InventTemplateArgumentLoc(*A, InputArg);
3069 if (getDerived().TransformTemplateArgument(InputArg, OutputArg))
3072 TransformedArgs.push_back(OutputArg.getArgument());
3075 TemplateArgument *TransformedArgsPtr
3076 = new (getSema().Context) TemplateArgument[TransformedArgs.size()];
3077 std::copy(TransformedArgs.begin(), TransformedArgs.end(),
3078 TransformedArgsPtr);
3079 Output = TemplateArgumentLoc(TemplateArgument(TransformedArgsPtr,
3080 TransformedArgs.size()),
3081 Input.getLocInfo());
3086 // Work around bogus GCC warning
3090 /// \brief Iterator adaptor that invents template argument location information
3091 /// for each of the template arguments in its underlying iterator.
3092 template<typename Derived, typename InputIterator>
3093 class TemplateArgumentLocInventIterator {
3094 TreeTransform<Derived> &Self;
3098 typedef TemplateArgumentLoc value_type;
3099 typedef TemplateArgumentLoc reference;
3100 typedef typename std::iterator_traits<InputIterator>::difference_type
3102 typedef std::input_iterator_tag iterator_category;
3105 TemplateArgumentLoc Arg;
3108 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
3110 const TemplateArgumentLoc *operator->() const { return &Arg; }
3113 TemplateArgumentLocInventIterator() { }
3115 explicit TemplateArgumentLocInventIterator(TreeTransform<Derived> &Self,
3117 : Self(Self), Iter(Iter) { }
3119 TemplateArgumentLocInventIterator &operator++() {
3124 TemplateArgumentLocInventIterator operator++(int) {
3125 TemplateArgumentLocInventIterator Old(*this);
3130 reference operator*() const {
3131 TemplateArgumentLoc Result;
3132 Self.InventTemplateArgumentLoc(*Iter, Result);
3136 pointer operator->() const { return pointer(**this); }
3138 friend bool operator==(const TemplateArgumentLocInventIterator &X,
3139 const TemplateArgumentLocInventIterator &Y) {
3140 return X.Iter == Y.Iter;
3143 friend bool operator!=(const TemplateArgumentLocInventIterator &X,
3144 const TemplateArgumentLocInventIterator &Y) {
3145 return X.Iter != Y.Iter;
3149 template<typename Derived>
3150 template<typename InputIterator>
3151 bool TreeTransform<Derived>::TransformTemplateArguments(InputIterator First,
3153 TemplateArgumentListInfo &Outputs) {
3154 for (; First != Last; ++First) {
3155 TemplateArgumentLoc Out;
3156 TemplateArgumentLoc In = *First;
3158 if (In.getArgument().getKind() == TemplateArgument::Pack) {
3159 // Unpack argument packs, which we translate them into separate
3161 // FIXME: We could do much better if we could guarantee that the
3162 // TemplateArgumentLocInfo for the pack expansion would be usable for
3163 // all of the template arguments in the argument pack.
3164 typedef TemplateArgumentLocInventIterator<Derived,
3165 TemplateArgument::pack_iterator>
3167 if (TransformTemplateArguments(PackLocIterator(*this,
3168 In.getArgument().pack_begin()),
3169 PackLocIterator(*this,
3170 In.getArgument().pack_end()),
3177 if (In.getArgument().isPackExpansion()) {
3178 // We have a pack expansion, for which we will be substituting into
3180 SourceLocation Ellipsis;
3181 llvm::Optional<unsigned> OrigNumExpansions;
3182 TemplateArgumentLoc Pattern
3183 = In.getPackExpansionPattern(Ellipsis, OrigNumExpansions,
3186 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
3187 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
3188 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
3190 // Determine whether the set of unexpanded parameter packs can and should
3193 bool RetainExpansion = false;
3194 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
3195 if (getDerived().TryExpandParameterPacks(Ellipsis,
3196 Pattern.getSourceRange(),
3204 // The transform has determined that we should perform a simple
3205 // transformation on the pack expansion, producing another pack
3207 TemplateArgumentLoc OutPattern;
3208 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
3209 if (getDerived().TransformTemplateArgument(Pattern, OutPattern))
3212 Out = getDerived().RebuildPackExpansion(OutPattern, Ellipsis,
3214 if (Out.getArgument().isNull())
3217 Outputs.addArgument(Out);
3221 // The transform has determined that we should perform an elementwise
3222 // expansion of the pattern. Do so.
3223 for (unsigned I = 0; I != *NumExpansions; ++I) {
3224 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
3226 if (getDerived().TransformTemplateArgument(Pattern, Out))
3229 if (Out.getArgument().containsUnexpandedParameterPack()) {
3230 Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
3232 if (Out.getArgument().isNull())
3236 Outputs.addArgument(Out);
3239 // If we're supposed to retain a pack expansion, do so by temporarily
3240 // forgetting the partially-substituted parameter pack.
3241 if (RetainExpansion) {
3242 ForgetPartiallySubstitutedPackRAII Forget(getDerived());
3244 if (getDerived().TransformTemplateArgument(Pattern, Out))
3247 Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
3249 if (Out.getArgument().isNull())
3252 Outputs.addArgument(Out);
3259 if (getDerived().TransformTemplateArgument(In, Out))
3262 Outputs.addArgument(Out);
3269 //===----------------------------------------------------------------------===//
3270 // Type transformation
3271 //===----------------------------------------------------------------------===//
3273 template<typename Derived>
3274 QualType TreeTransform<Derived>::TransformType(QualType T) {
3275 if (getDerived().AlreadyTransformed(T))
3278 // Temporary workaround. All of these transformations should
3279 // eventually turn into transformations on TypeLocs.
3280 TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T,
3281 getDerived().getBaseLocation());
3283 TypeSourceInfo *NewDI = getDerived().TransformType(DI);
3288 return NewDI->getType();
3291 template<typename Derived>
3292 TypeSourceInfo *TreeTransform<Derived>::TransformType(TypeSourceInfo *DI) {
3293 // Refine the base location to the type's location.
3294 TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(),
3295 getDerived().getBaseEntity());
3296 if (getDerived().AlreadyTransformed(DI->getType()))
3301 TypeLoc TL = DI->getTypeLoc();
3302 TLB.reserve(TL.getFullDataSize());
3304 QualType Result = getDerived().TransformType(TLB, TL);
3305 if (Result.isNull())
3308 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
3311 template<typename Derived>
3313 TreeTransform<Derived>::TransformType(TypeLocBuilder &TLB, TypeLoc T) {
3314 switch (T.getTypeLocClass()) {
3315 #define ABSTRACT_TYPELOC(CLASS, PARENT)
3316 #define TYPELOC(CLASS, PARENT) \
3317 case TypeLoc::CLASS: \
3318 return getDerived().Transform##CLASS##Type(TLB, cast<CLASS##TypeLoc>(T));
3319 #include "clang/AST/TypeLocNodes.def"
3322 llvm_unreachable("unhandled type loc!");
3325 /// FIXME: By default, this routine adds type qualifiers only to types
3326 /// that can have qualifiers, and silently suppresses those qualifiers
3327 /// that are not permitted (e.g., qualifiers on reference or function
3328 /// types). This is the right thing for template instantiation, but
3329 /// probably not for other clients.
3330 template<typename Derived>
3332 TreeTransform<Derived>::TransformQualifiedType(TypeLocBuilder &TLB,
3333 QualifiedTypeLoc T) {
3334 Qualifiers Quals = T.getType().getLocalQualifiers();
3336 QualType Result = getDerived().TransformType(TLB, T.getUnqualifiedLoc());
3337 if (Result.isNull())
3340 // Silently suppress qualifiers if the result type can't be qualified.
3341 // FIXME: this is the right thing for template instantiation, but
3342 // probably not for other clients.
3343 if (Result->isFunctionType() || Result->isReferenceType())
3346 // Suppress Objective-C lifetime qualifiers if they don't make sense for the
3348 if (Quals.hasObjCLifetime()) {
3349 if (!Result->isObjCLifetimeType() && !Result->isDependentType())
3350 Quals.removeObjCLifetime();
3351 else if (Result.getObjCLifetime()) {
3353 // A lifetime qualifier applied to a substituted template parameter
3354 // overrides the lifetime qualifier from the template argument.
3355 if (const SubstTemplateTypeParmType *SubstTypeParam
3356 = dyn_cast<SubstTemplateTypeParmType>(Result)) {
3357 QualType Replacement = SubstTypeParam->getReplacementType();
3358 Qualifiers Qs = Replacement.getQualifiers();
3359 Qs.removeObjCLifetime();
3361 = SemaRef.Context.getQualifiedType(Replacement.getUnqualifiedType(),
3363 Result = SemaRef.Context.getSubstTemplateTypeParmType(
3364 SubstTypeParam->getReplacedParameter(),
3366 TLB.TypeWasModifiedSafely(Result);
3368 // Otherwise, complain about the addition of a qualifier to an
3369 // already-qualified type.
3370 SourceRange R = TLB.getTemporaryTypeLoc(Result).getSourceRange();
3371 SemaRef.Diag(R.getBegin(), diag::err_attr_objc_ownership_redundant)
3374 Quals.removeObjCLifetime();
3378 if (!Quals.empty()) {
3379 Result = SemaRef.BuildQualifiedType(Result, T.getBeginLoc(), Quals);
3380 TLB.push<QualifiedTypeLoc>(Result);
3381 // No location information to preserve.
3387 template<typename Derived>
3389 TreeTransform<Derived>::TransformTypeInObjectScope(TypeLoc TL,
3390 QualType ObjectType,
3391 NamedDecl *UnqualLookup,
3393 QualType T = TL.getType();
3394 if (getDerived().AlreadyTransformed(T))
3400 if (isa<TemplateSpecializationType>(T)) {
3401 TemplateSpecializationTypeLoc SpecTL
3402 = cast<TemplateSpecializationTypeLoc>(TL);
3404 TemplateName Template =
3405 getDerived().TransformTemplateName(SS,
3406 SpecTL.getTypePtr()->getTemplateName(),
3407 SpecTL.getTemplateNameLoc(),
3408 ObjectType, UnqualLookup);
3409 if (Template.isNull())
3412 Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
3414 } else if (isa<DependentTemplateSpecializationType>(T)) {
3415 DependentTemplateSpecializationTypeLoc SpecTL
3416 = cast<DependentTemplateSpecializationTypeLoc>(TL);
3418 TemplateName Template
3419 = getDerived().RebuildTemplateName(SS,
3420 *SpecTL.getTypePtr()->getIdentifier(),
3421 SpecTL.getTemplateNameLoc(),
3422 ObjectType, UnqualLookup);
3423 if (Template.isNull())
3426 Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
3431 // Nothing special needs to be done for these.
3432 Result = getDerived().TransformType(TLB, TL);
3435 if (Result.isNull())
3438 return TLB.getTypeSourceInfo(SemaRef.Context, Result)->getTypeLoc();
3441 template<typename Derived>
3443 TreeTransform<Derived>::TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
3444 QualType ObjectType,
3445 NamedDecl *UnqualLookup,
3447 // FIXME: Painfully copy-paste from the above!
3449 QualType T = TSInfo->getType();
3450 if (getDerived().AlreadyTransformed(T))
3456 TypeLoc TL = TSInfo->getTypeLoc();
3457 if (isa<TemplateSpecializationType>(T)) {
3458 TemplateSpecializationTypeLoc SpecTL
3459 = cast<TemplateSpecializationTypeLoc>(TL);
3461 TemplateName Template
3462 = getDerived().TransformTemplateName(SS,
3463 SpecTL.getTypePtr()->getTemplateName(),
3464 SpecTL.getTemplateNameLoc(),
3465 ObjectType, UnqualLookup);
3466 if (Template.isNull())
3469 Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
3471 } else if (isa<DependentTemplateSpecializationType>(T)) {
3472 DependentTemplateSpecializationTypeLoc SpecTL
3473 = cast<DependentTemplateSpecializationTypeLoc>(TL);
3475 TemplateName Template
3476 = getDerived().RebuildTemplateName(SS,
3477 *SpecTL.getTypePtr()->getIdentifier(),
3478 SpecTL.getTemplateNameLoc(),
3479 ObjectType, UnqualLookup);
3480 if (Template.isNull())
3483 Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
3488 // Nothing special needs to be done for these.
3489 Result = getDerived().TransformType(TLB, TL);
3492 if (Result.isNull())
3495 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
3498 template <class TyLoc> static inline
3499 QualType TransformTypeSpecType(TypeLocBuilder &TLB, TyLoc T) {
3500 TyLoc NewT = TLB.push<TyLoc>(T.getType());
3501 NewT.setNameLoc(T.getNameLoc());
3505 template<typename Derived>
3506 QualType TreeTransform<Derived>::TransformBuiltinType(TypeLocBuilder &TLB,
3508 BuiltinTypeLoc NewT = TLB.push<BuiltinTypeLoc>(T.getType());
3509 NewT.setBuiltinLoc(T.getBuiltinLoc());
3510 if (T.needsExtraLocalData())
3511 NewT.getWrittenBuiltinSpecs() = T.getWrittenBuiltinSpecs();
3515 template<typename Derived>
3516 QualType TreeTransform<Derived>::TransformComplexType(TypeLocBuilder &TLB,
3519 return TransformTypeSpecType(TLB, T);
3522 template<typename Derived>
3523 QualType TreeTransform<Derived>::TransformPointerType(TypeLocBuilder &TLB,
3524 PointerTypeLoc TL) {
3525 QualType PointeeType
3526 = getDerived().TransformType(TLB, TL.getPointeeLoc());
3527 if (PointeeType.isNull())
3530 QualType Result = TL.getType();
3531 if (PointeeType->getAs<ObjCObjectType>()) {
3532 // A dependent pointer type 'T *' has is being transformed such
3533 // that an Objective-C class type is being replaced for 'T'. The
3534 // resulting pointer type is an ObjCObjectPointerType, not a
3536 Result = SemaRef.Context.getObjCObjectPointerType(PointeeType);
3538 ObjCObjectPointerTypeLoc NewT = TLB.push<ObjCObjectPointerTypeLoc>(Result);
3539 NewT.setStarLoc(TL.getStarLoc());
3543 if (getDerived().AlwaysRebuild() ||
3544 PointeeType != TL.getPointeeLoc().getType()) {
3545 Result = getDerived().RebuildPointerType(PointeeType, TL.getSigilLoc());
3546 if (Result.isNull())
3550 // Objective-C ARC can add lifetime qualifiers to the type that we're
3552 TLB.TypeWasModifiedSafely(Result->getPointeeType());
3554 PointerTypeLoc NewT = TLB.push<PointerTypeLoc>(Result);
3555 NewT.setSigilLoc(TL.getSigilLoc());
3559 template<typename Derived>
3561 TreeTransform<Derived>::TransformBlockPointerType(TypeLocBuilder &TLB,
3562 BlockPointerTypeLoc TL) {
3563 QualType PointeeType
3564 = getDerived().TransformType(TLB, TL.getPointeeLoc());
3565 if (PointeeType.isNull())
3568 QualType Result = TL.getType();
3569 if (getDerived().AlwaysRebuild() ||
3570 PointeeType != TL.getPointeeLoc().getType()) {
3571 Result = getDerived().RebuildBlockPointerType(PointeeType,
3573 if (Result.isNull())
3577 BlockPointerTypeLoc NewT = TLB.push<BlockPointerTypeLoc>(Result);
3578 NewT.setSigilLoc(TL.getSigilLoc());
3582 /// Transforms a reference type. Note that somewhat paradoxically we
3583 /// don't care whether the type itself is an l-value type or an r-value
3584 /// type; we only care if the type was *written* as an l-value type
3585 /// or an r-value type.
3586 template<typename Derived>
3588 TreeTransform<Derived>::TransformReferenceType(TypeLocBuilder &TLB,
3589 ReferenceTypeLoc TL) {
3590 const ReferenceType *T = TL.getTypePtr();
3592 // Note that this works with the pointee-as-written.
3593 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
3594 if (PointeeType.isNull())
3597 QualType Result = TL.getType();
3598 if (getDerived().AlwaysRebuild() ||
3599 PointeeType != T->getPointeeTypeAsWritten()) {
3600 Result = getDerived().RebuildReferenceType(PointeeType,
3601 T->isSpelledAsLValue(),
3603 if (Result.isNull())
3607 // Objective-C ARC can add lifetime qualifiers to the type that we're
3609 TLB.TypeWasModifiedSafely(
3610 Result->getAs<ReferenceType>()->getPointeeTypeAsWritten());
3612 // r-value references can be rebuilt as l-value references.
3613 ReferenceTypeLoc NewTL;
3614 if (isa<LValueReferenceType>(Result))
3615 NewTL = TLB.push<LValueReferenceTypeLoc>(Result);
3617 NewTL = TLB.push<RValueReferenceTypeLoc>(Result);
3618 NewTL.setSigilLoc(TL.getSigilLoc());
3623 template<typename Derived>
3625 TreeTransform<Derived>::TransformLValueReferenceType(TypeLocBuilder &TLB,
3626 LValueReferenceTypeLoc TL) {
3627 return TransformReferenceType(TLB, TL);
3630 template<typename Derived>
3632 TreeTransform<Derived>::TransformRValueReferenceType(TypeLocBuilder &TLB,
3633 RValueReferenceTypeLoc TL) {
3634 return TransformReferenceType(TLB, TL);
3637 template<typename Derived>
3639 TreeTransform<Derived>::TransformMemberPointerType(TypeLocBuilder &TLB,
3640 MemberPointerTypeLoc TL) {
3641 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
3642 if (PointeeType.isNull())
3645 TypeSourceInfo* OldClsTInfo = TL.getClassTInfo();
3646 TypeSourceInfo* NewClsTInfo = 0;
3648 NewClsTInfo = getDerived().TransformType(OldClsTInfo);
3653 const MemberPointerType *T = TL.getTypePtr();
3654 QualType OldClsType = QualType(T->getClass(), 0);
3655 QualType NewClsType;
3657 NewClsType = NewClsTInfo->getType();
3659 NewClsType = getDerived().TransformType(OldClsType);
3660 if (NewClsType.isNull())
3664 QualType Result = TL.getType();
3665 if (getDerived().AlwaysRebuild() ||
3666 PointeeType != T->getPointeeType() ||
3667 NewClsType != OldClsType) {
3668 Result = getDerived().RebuildMemberPointerType(PointeeType, NewClsType,
3670 if (Result.isNull())
3674 MemberPointerTypeLoc NewTL = TLB.push<MemberPointerTypeLoc>(Result);
3675 NewTL.setSigilLoc(TL.getSigilLoc());
3676 NewTL.setClassTInfo(NewClsTInfo);
3681 template<typename Derived>
3683 TreeTransform<Derived>::TransformConstantArrayType(TypeLocBuilder &TLB,
3684 ConstantArrayTypeLoc TL) {
3685 const ConstantArrayType *T = TL.getTypePtr();
3686 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3687 if (ElementType.isNull())
3690 QualType Result = TL.getType();
3691 if (getDerived().AlwaysRebuild() ||
3692 ElementType != T->getElementType()) {
3693 Result = getDerived().RebuildConstantArrayType(ElementType,
3694 T->getSizeModifier(),
3696 T->getIndexTypeCVRQualifiers(),
3697 TL.getBracketsRange());
3698 if (Result.isNull())
3702 // We might have either a ConstantArrayType or a VariableArrayType now:
3703 // a ConstantArrayType is allowed to have an element type which is a
3704 // VariableArrayType if the type is dependent. Fortunately, all array
3705 // types have the same location layout.
3706 ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
3707 NewTL.setLBracketLoc(TL.getLBracketLoc());
3708 NewTL.setRBracketLoc(TL.getRBracketLoc());
3710 Expr *Size = TL.getSizeExpr();
3712 EnterExpressionEvaluationContext Unevaluated(SemaRef,
3713 Sema::ConstantEvaluated);
3714 Size = getDerived().TransformExpr(Size).template takeAs<Expr>();
3715 Size = SemaRef.ActOnConstantExpression(Size).take();
3717 NewTL.setSizeExpr(Size);
3722 template<typename Derived>
3723 QualType TreeTransform<Derived>::TransformIncompleteArrayType(
3724 TypeLocBuilder &TLB,
3725 IncompleteArrayTypeLoc TL) {
3726 const IncompleteArrayType *T = TL.getTypePtr();
3727 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3728 if (ElementType.isNull())
3731 QualType Result = TL.getType();
3732 if (getDerived().AlwaysRebuild() ||
3733 ElementType != T->getElementType()) {
3734 Result = getDerived().RebuildIncompleteArrayType(ElementType,
3735 T->getSizeModifier(),
3736 T->getIndexTypeCVRQualifiers(),
3737 TL.getBracketsRange());
3738 if (Result.isNull())
3742 IncompleteArrayTypeLoc NewTL = TLB.push<IncompleteArrayTypeLoc>(Result);
3743 NewTL.setLBracketLoc(TL.getLBracketLoc());
3744 NewTL.setRBracketLoc(TL.getRBracketLoc());
3745 NewTL.setSizeExpr(0);
3750 template<typename Derived>
3752 TreeTransform<Derived>::TransformVariableArrayType(TypeLocBuilder &TLB,
3753 VariableArrayTypeLoc TL) {
3754 const VariableArrayType *T = TL.getTypePtr();
3755 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3756 if (ElementType.isNull())
3759 ExprResult SizeResult
3760 = getDerived().TransformExpr(T->getSizeExpr());
3761 if (SizeResult.isInvalid())
3764 Expr *Size = SizeResult.take();