clang  8.0.0svn
TreeTransform.h
Go to the documentation of this file.
1 //===------- TreeTransform.h - Semantic Tree Transformation -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a semantic tree transformation that takes a given
10 // AST and rebuilds it, possibly transforming some nodes in the process.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_SEMA_TREETRANSFORM_H
15 #define LLVM_CLANG_LIB_SEMA_TREETRANSFORM_H
16 
17 #include "CoroutineStmtBuilder.h"
18 #include "TypeLocBuilder.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25 #include "clang/AST/ExprOpenMP.h"
26 #include "clang/AST/Stmt.h"
27 #include "clang/AST/StmtCXX.h"
28 #include "clang/AST/StmtObjC.h"
29 #include "clang/AST/StmtOpenMP.h"
30 #include "clang/Sema/Designator.h"
31 #include "clang/Sema/Lookup.h"
32 #include "clang/Sema/Ownership.h"
34 #include "clang/Sema/ScopeInfo.h"
37 #include "llvm/ADT/ArrayRef.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include <algorithm>
40 
41 namespace clang {
42 using namespace sema;
43 
44 /// A semantic tree transformation that allows one to transform one
45 /// abstract syntax tree into another.
46 ///
47 /// A new tree transformation is defined by creating a new subclass \c X of
48 /// \c TreeTransform<X> and then overriding certain operations to provide
49 /// behavior specific to that transformation. For example, template
50 /// instantiation is implemented as a tree transformation where the
51 /// transformation of TemplateTypeParmType nodes involves substituting the
52 /// template arguments for their corresponding template parameters; a similar
53 /// transformation is performed for non-type template parameters and
54 /// template template parameters.
55 ///
56 /// This tree-transformation template uses static polymorphism to allow
57 /// subclasses to customize any of its operations. Thus, a subclass can
58 /// override any of the transformation or rebuild operators by providing an
59 /// operation with the same signature as the default implementation. The
60 /// overriding function should not be virtual.
61 ///
62 /// Semantic tree transformations are split into two stages, either of which
63 /// can be replaced by a subclass. The "transform" step transforms an AST node
64 /// or the parts of an AST node using the various transformation functions,
65 /// then passes the pieces on to the "rebuild" step, which constructs a new AST
66 /// node of the appropriate kind from the pieces. The default transformation
67 /// routines recursively transform the operands to composite AST nodes (e.g.,
68 /// the pointee type of a PointerType node) and, if any of those operand nodes
69 /// were changed by the transformation, invokes the rebuild operation to create
70 /// a new AST node.
71 ///
72 /// Subclasses can customize the transformation at various levels. The
73 /// most coarse-grained transformations involve replacing TransformType(),
74 /// TransformExpr(), TransformDecl(), TransformNestedNameSpecifierLoc(),
75 /// TransformTemplateName(), or TransformTemplateArgument() with entirely
76 /// new implementations.
77 ///
78 /// For more fine-grained transformations, subclasses can replace any of the
79 /// \c TransformXXX functions (where XXX is the name of an AST node, e.g.,
80 /// PointerType, StmtExpr) to alter the transformation. As mentioned previously,
81 /// replacing TransformTemplateTypeParmType() allows template instantiation
82 /// to substitute template arguments for their corresponding template
83 /// parameters. Additionally, subclasses can override the \c RebuildXXX
84 /// functions to control how AST nodes are rebuilt when their operands change.
85 /// By default, \c TreeTransform will invoke semantic analysis to rebuild
86 /// AST nodes. However, certain other tree transformations (e.g, cloning) may
87 /// be able to use more efficient rebuild steps.
88 ///
89 /// There are a handful of other functions that can be overridden, allowing one
90 /// to avoid traversing nodes that don't need any transformation
91 /// (\c AlreadyTransformed()), force rebuilding AST nodes even when their
92 /// operands have not changed (\c AlwaysRebuild()), and customize the
93 /// default locations and entity names used for type-checking
94 /// (\c getBaseLocation(), \c getBaseEntity()).
95 template<typename Derived>
97  /// Private RAII object that helps us forget and then re-remember
98  /// the template argument corresponding to a partially-substituted parameter
99  /// pack.
100  class ForgetPartiallySubstitutedPackRAII {
101  Derived &Self;
102  TemplateArgument Old;
103 
104  public:
105  ForgetPartiallySubstitutedPackRAII(Derived &Self) : Self(Self) {
106  Old = Self.ForgetPartiallySubstitutedPack();
107  }
108 
109  ~ForgetPartiallySubstitutedPackRAII() {
110  Self.RememberPartiallySubstitutedPack(Old);
111  }
112  };
113 
114 protected:
116 
117  /// The set of local declarations that have been transformed, for
118  /// cases where we are forced to build new declarations within the transformer
119  /// rather than in the subclass (e.g., lambda closure types).
120  llvm::DenseMap<Decl *, Decl *> TransformedLocalDecls;
121 
122 public:
123  /// Initializes a new tree transformer.
124  TreeTransform(Sema &SemaRef) : SemaRef(SemaRef) { }
125 
126  /// Retrieves a reference to the derived class.
127  Derived &getDerived() { return static_cast<Derived&>(*this); }
128 
129  /// Retrieves a reference to the derived class.
130  const Derived &getDerived() const {
131  return static_cast<const Derived&>(*this);
132  }
133 
134  static inline ExprResult Owned(Expr *E) { return E; }
135  static inline StmtResult Owned(Stmt *S) { return S; }
136 
137  /// Retrieves a reference to the semantic analysis object used for
138  /// this tree transform.
139  Sema &getSema() const { return SemaRef; }
140 
141  /// Whether the transformation should always rebuild AST nodes, even
142  /// if none of the children have changed.
143  ///
144  /// Subclasses may override this function to specify when the transformation
145  /// should rebuild all AST nodes.
146  ///
147  /// We must always rebuild all AST nodes when performing variadic template
148  /// pack expansion, in order to avoid violating the AST invariant that each
149  /// statement node appears at most once in its containing declaration.
150  bool AlwaysRebuild() { return SemaRef.ArgumentPackSubstitutionIndex != -1; }
151 
152  /// Returns the location of the entity being transformed, if that
153  /// information was not available elsewhere in the AST.
154  ///
155  /// By default, returns no source-location information. Subclasses can
156  /// provide an alternative implementation that provides better location
157  /// information.
159 
160  /// Returns the name of the entity being transformed, if that
161  /// information was not available elsewhere in the AST.
162  ///
163  /// By default, returns an empty name. Subclasses can provide an alternative
164  /// implementation with a more precise name.
166 
167  /// Sets the "base" location and entity when that
168  /// information is known based on another transformation.
169  ///
170  /// By default, the source location and entity are ignored. Subclasses can
171  /// override this function to provide a customized implementation.
172  void setBase(SourceLocation Loc, DeclarationName Entity) { }
173 
174  /// RAII object that temporarily sets the base location and entity
175  /// used for reporting diagnostics in types.
177  TreeTransform &Self;
178  SourceLocation OldLocation;
179  DeclarationName OldEntity;
180 
181  public:
183  DeclarationName Entity) : Self(Self) {
184  OldLocation = Self.getDerived().getBaseLocation();
185  OldEntity = Self.getDerived().getBaseEntity();
186 
187  if (Location.isValid())
188  Self.getDerived().setBase(Location, Entity);
189  }
190 
192  Self.getDerived().setBase(OldLocation, OldEntity);
193  }
194  };
195 
196  /// Determine whether the given type \p T has already been
197  /// transformed.
198  ///
199  /// Subclasses can provide an alternative implementation of this routine
200  /// to short-circuit evaluation when it is known that a given type will
201  /// not change. For example, template instantiation need not traverse
202  /// non-dependent types.
204  return T.isNull();
205  }
206 
207  /// Determine whether the given call argument should be dropped, e.g.,
208  /// because it is a default argument.
209  ///
210  /// Subclasses can provide an alternative implementation of this routine to
211  /// determine which kinds of call arguments get dropped. By default,
212  /// CXXDefaultArgument nodes are dropped (prior to transformation).
214  return E->isDefaultArgument();
215  }
216 
217  /// Determine whether we should expand a pack expansion with the
218  /// given set of parameter packs into separate arguments by repeatedly
219  /// transforming the pattern.
220  ///
221  /// By default, the transformer never tries to expand pack expansions.
222  /// Subclasses can override this routine to provide different behavior.
223  ///
224  /// \param EllipsisLoc The location of the ellipsis that identifies the
225  /// pack expansion.
226  ///
227  /// \param PatternRange The source range that covers the entire pattern of
228  /// the pack expansion.
229  ///
230  /// \param Unexpanded The set of unexpanded parameter packs within the
231  /// pattern.
232  ///
233  /// \param ShouldExpand Will be set to \c true if the transformer should
234  /// expand the corresponding pack expansions into separate arguments. When
235  /// set, \c NumExpansions must also be set.
236  ///
237  /// \param RetainExpansion Whether the caller should add an unexpanded
238  /// pack expansion after all of the expanded arguments. This is used
239  /// when extending explicitly-specified template argument packs per
240  /// C++0x [temp.arg.explicit]p9.
241  ///
242  /// \param NumExpansions The number of separate arguments that will be in
243  /// the expanded form of the corresponding pack expansion. This is both an
244  /// input and an output parameter, which can be set by the caller if the
245  /// number of expansions is known a priori (e.g., due to a prior substitution)
246  /// and will be set by the callee when the number of expansions is known.
247  /// The callee must set this value when \c ShouldExpand is \c true; it may
248  /// set this value in other cases.
249  ///
250  /// \returns true if an error occurred (e.g., because the parameter packs
251  /// are to be instantiated with arguments of different lengths), false
252  /// otherwise. If false, \c ShouldExpand (and possibly \c NumExpansions)
253  /// must be set.
255  SourceRange PatternRange,
257  bool &ShouldExpand,
258  bool &RetainExpansion,
259  Optional<unsigned> &NumExpansions) {
260  ShouldExpand = false;
261  return false;
262  }
263 
264  /// "Forget" about the partially-substituted pack template argument,
265  /// when performing an instantiation that must preserve the parameter pack
266  /// use.
267  ///
268  /// This routine is meant to be overridden by the template instantiator.
270  return TemplateArgument();
271  }
272 
273  /// "Remember" the partially-substituted pack template argument
274  /// after performing an instantiation that must preserve the parameter pack
275  /// use.
276  ///
277  /// This routine is meant to be overridden by the template instantiator.
279 
280  /// Note to the derived class when a function parameter pack is
281  /// being expanded.
283 
284  /// Transforms the given type into another type.
285  ///
286  /// By default, this routine transforms a type by creating a
287  /// TypeSourceInfo for it and delegating to the appropriate
288  /// function. This is expensive, but we don't mind, because
289  /// this method is deprecated anyway; all users should be
290  /// switched to storing TypeSourceInfos.
291  ///
292  /// \returns the transformed type.
293  QualType TransformType(QualType T);
294 
295  /// Transforms the given type-with-location into a new
296  /// type-with-location.
297  ///
298  /// By default, this routine transforms a type by delegating to the
299  /// appropriate TransformXXXType to build a new type. Subclasses
300  /// may override this function (to take over all type
301  /// transformations) or some set of the TransformXXXType functions
302  /// to alter the transformation.
303  TypeSourceInfo *TransformType(TypeSourceInfo *DI);
304 
305  /// Transform the given type-with-location into a new
306  /// type, collecting location information in the given builder
307  /// as necessary.
308  ///
309  QualType TransformType(TypeLocBuilder &TLB, TypeLoc TL);
310 
311  /// Transform a type that is permitted to produce a
312  /// DeducedTemplateSpecializationType.
313  ///
314  /// This is used in the (relatively rare) contexts where it is acceptable
315  /// for transformation to produce a class template type with deduced
316  /// template arguments.
317  /// @{
318  QualType TransformTypeWithDeducedTST(QualType T);
319  TypeSourceInfo *TransformTypeWithDeducedTST(TypeSourceInfo *DI);
320  /// @}
321 
322  /// Transform the given statement.
323  ///
324  /// By default, this routine transforms a statement by delegating to the
325  /// appropriate TransformXXXStmt function to transform a specific kind of
326  /// statement or the TransformExpr() function to transform an expression.
327  /// Subclasses may override this function to transform statements using some
328  /// other mechanism.
329  ///
330  /// \returns the transformed statement.
331  StmtResult TransformStmt(Stmt *S);
332 
333  /// Transform the given statement.
334  ///
335  /// By default, this routine transforms a statement by delegating to the
336  /// appropriate TransformOMPXXXClause function to transform a specific kind
337  /// of clause. Subclasses may override this function to transform statements
338  /// using some other mechanism.
339  ///
340  /// \returns the transformed OpenMP clause.
341  OMPClause *TransformOMPClause(OMPClause *S);
342 
343  /// Transform the given attribute.
344  ///
345  /// By default, this routine transforms a statement by delegating to the
346  /// appropriate TransformXXXAttr function to transform a specific kind
347  /// of attribute. Subclasses may override this function to transform
348  /// attributed statements using some other mechanism.
349  ///
350  /// \returns the transformed attribute
351  const Attr *TransformAttr(const Attr *S);
352 
353 /// Transform the specified attribute.
354 ///
355 /// Subclasses should override the transformation of attributes with a pragma
356 /// spelling to transform expressions stored within the attribute.
357 ///
358 /// \returns the transformed attribute.
359 #define ATTR(X)
360 #define PRAGMA_SPELLING_ATTR(X) \
361  const X##Attr *Transform##X##Attr(const X##Attr *R) { return R; }
362 #include "clang/Basic/AttrList.inc"
363 
364  /// Transform the given expression.
365  ///
366  /// By default, this routine transforms an expression by delegating to the
367  /// appropriate TransformXXXExpr function to build a new expression.
368  /// Subclasses may override this function to transform expressions using some
369  /// other mechanism.
370  ///
371  /// \returns the transformed expression.
372  ExprResult TransformExpr(Expr *E);
373 
374  /// Transform the given initializer.
375  ///
376  /// By default, this routine transforms an initializer by stripping off the
377  /// semantic nodes added by initialization, then passing the result to
378  /// TransformExpr or TransformExprs.
379  ///
380  /// \returns the transformed initializer.
381  ExprResult TransformInitializer(Expr *Init, bool NotCopyInit);
382 
383  /// Transform the given list of expressions.
384  ///
385  /// This routine transforms a list of expressions by invoking
386  /// \c TransformExpr() for each subexpression. However, it also provides
387  /// support for variadic templates by expanding any pack expansions (if the
388  /// derived class permits such expansion) along the way. When pack expansions
389  /// are present, the number of outputs may not equal the number of inputs.
390  ///
391  /// \param Inputs The set of expressions to be transformed.
392  ///
393  /// \param NumInputs The number of expressions in \c Inputs.
394  ///
395  /// \param IsCall If \c true, then this transform is being performed on
396  /// function-call arguments, and any arguments that should be dropped, will
397  /// be.
398  ///
399  /// \param Outputs The transformed input expressions will be added to this
400  /// vector.
401  ///
402  /// \param ArgChanged If non-NULL, will be set \c true if any argument changed
403  /// due to transformation.
404  ///
405  /// \returns true if an error occurred, false otherwise.
406  bool TransformExprs(Expr *const *Inputs, unsigned NumInputs, bool IsCall,
407  SmallVectorImpl<Expr *> &Outputs,
408  bool *ArgChanged = nullptr);
409 
410  /// Transform the given declaration, which is referenced from a type
411  /// or expression.
412  ///
413  /// By default, acts as the identity function on declarations, unless the
414  /// transformer has had to transform the declaration itself. Subclasses
415  /// may override this function to provide alternate behavior.
417  llvm::DenseMap<Decl *, Decl *>::iterator Known
418  = TransformedLocalDecls.find(D);
419  if (Known != TransformedLocalDecls.end())
420  return Known->second;
421 
422  return D;
423  }
424 
425  /// Transform the specified condition.
426  ///
427  /// By default, this transforms the variable and expression and rebuilds
428  /// the condition.
429  Sema::ConditionResult TransformCondition(SourceLocation Loc, VarDecl *Var,
430  Expr *Expr,
432 
433  /// Transform the attributes associated with the given declaration and
434  /// place them on the new declaration.
435  ///
436  /// By default, this operation does nothing. Subclasses may override this
437  /// behavior to transform attributes.
438  void transformAttrs(Decl *Old, Decl *New) { }
439 
440  /// Note that a local declaration has been transformed by this
441  /// transformer.
442  ///
443  /// Local declarations are typically transformed via a call to
444  /// TransformDefinition. However, in some cases (e.g., lambda expressions),
445  /// the transformer itself has to transform the declarations. This routine
446  /// can be overridden by a subclass that keeps track of such mappings.
447  void transformedLocalDecl(Decl *Old, Decl *New) {
448  TransformedLocalDecls[Old] = New;
449  }
450 
451  /// Transform the definition of the given declaration.
452  ///
453  /// By default, invokes TransformDecl() to transform the declaration.
454  /// Subclasses may override this function to provide alternate behavior.
456  return getDerived().TransformDecl(Loc, D);
457  }
458 
459  /// Transform the given declaration, which was the first part of a
460  /// nested-name-specifier in a member access expression.
461  ///
462  /// This specific declaration transformation only applies to the first
463  /// identifier in a nested-name-specifier of a member access expression, e.g.,
464  /// the \c T in \c x->T::member
465  ///
466  /// By default, invokes TransformDecl() to transform the declaration.
467  /// Subclasses may override this function to provide alternate behavior.
469  return cast_or_null<NamedDecl>(getDerived().TransformDecl(Loc, D));
470  }
471 
472  /// Transform the set of declarations in an OverloadExpr.
473  bool TransformOverloadExprDecls(OverloadExpr *Old, bool RequiresADL,
474  LookupResult &R);
475 
476  /// Transform the given nested-name-specifier with source-location
477  /// information.
478  ///
479  /// By default, transforms all of the types and declarations within the
480  /// nested-name-specifier. Subclasses may override this function to provide
481  /// alternate behavior.
483  TransformNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
484  QualType ObjectType = QualType(),
485  NamedDecl *FirstQualifierInScope = nullptr);
486 
487  /// Transform the given declaration name.
488  ///
489  /// By default, transforms the types of conversion function, constructor,
490  /// and destructor names and then (if needed) rebuilds the declaration name.
491  /// Identifiers and selectors are returned unmodified. Sublcasses may
492  /// override this function to provide alternate behavior.
494  TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
495 
496  /// Transform the given template name.
497  ///
498  /// \param SS The nested-name-specifier that qualifies the template
499  /// name. This nested-name-specifier must already have been transformed.
500  ///
501  /// \param Name The template name to transform.
502  ///
503  /// \param NameLoc The source location of the template name.
504  ///
505  /// \param ObjectType If we're translating a template name within a member
506  /// access expression, this is the type of the object whose member template
507  /// is being referenced.
508  ///
509  /// \param FirstQualifierInScope If the first part of a nested-name-specifier
510  /// also refers to a name within the current (lexical) scope, this is the
511  /// declaration it refers to.
512  ///
513  /// By default, transforms the template name by transforming the declarations
514  /// and nested-name-specifiers that occur within the template name.
515  /// Subclasses may override this function to provide alternate behavior.
517  TransformTemplateName(CXXScopeSpec &SS, TemplateName Name,
518  SourceLocation NameLoc,
519  QualType ObjectType = QualType(),
520  NamedDecl *FirstQualifierInScope = nullptr,
521  bool AllowInjectedClassName = false);
522 
523  /// Transform the given template argument.
524  ///
525  /// By default, this operation transforms the type, expression, or
526  /// declaration stored within the template argument and constructs a
527  /// new template argument from the transformed result. Subclasses may
528  /// override this function to provide alternate behavior.
529  ///
530  /// Returns true if there was an error.
531  bool TransformTemplateArgument(const TemplateArgumentLoc &Input,
532  TemplateArgumentLoc &Output,
533  bool Uneval = false);
534 
535  /// Transform the given set of template arguments.
536  ///
537  /// By default, this operation transforms all of the template arguments
538  /// in the input set using \c TransformTemplateArgument(), and appends
539  /// the transformed arguments to the output list.
540  ///
541  /// Note that this overload of \c TransformTemplateArguments() is merely
542  /// a convenience function. Subclasses that wish to override this behavior
543  /// should override the iterator-based member template version.
544  ///
545  /// \param Inputs The set of template arguments to be transformed.
546  ///
547  /// \param NumInputs The number of template arguments in \p Inputs.
548  ///
549  /// \param Outputs The set of transformed template arguments output by this
550  /// routine.
551  ///
552  /// Returns true if an error occurred.
554  unsigned NumInputs,
555  TemplateArgumentListInfo &Outputs,
556  bool Uneval = false) {
557  return TransformTemplateArguments(Inputs, Inputs + NumInputs, Outputs,
558  Uneval);
559  }
560 
561  /// Transform the given set of template arguments.
562  ///
563  /// By default, this operation transforms all of the template arguments
564  /// in the input set using \c TransformTemplateArgument(), and appends
565  /// the transformed arguments to the output list.
566  ///
567  /// \param First An iterator to the first template argument.
568  ///
569  /// \param Last An iterator one step past the last template argument.
570  ///
571  /// \param Outputs The set of transformed template arguments output by this
572  /// routine.
573  ///
574  /// Returns true if an error occurred.
575  template<typename InputIterator>
576  bool TransformTemplateArguments(InputIterator First,
577  InputIterator Last,
578  TemplateArgumentListInfo &Outputs,
579  bool Uneval = false);
580 
581  /// Fakes up a TemplateArgumentLoc for a given TemplateArgument.
582  void InventTemplateArgumentLoc(const TemplateArgument &Arg,
583  TemplateArgumentLoc &ArgLoc);
584 
585  /// Fakes up a TypeSourceInfo for a type.
587  return SemaRef.Context.getTrivialTypeSourceInfo(T,
588  getDerived().getBaseLocation());
589  }
590 
591 #define ABSTRACT_TYPELOC(CLASS, PARENT)
592 #define TYPELOC(CLASS, PARENT) \
593  QualType Transform##CLASS##Type(TypeLocBuilder &TLB, CLASS##TypeLoc T);
594 #include "clang/AST/TypeLocNodes.def"
595 
596  template<typename Fn>
597  QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
598  FunctionProtoTypeLoc TL,
599  CXXRecordDecl *ThisContext,
600  unsigned ThisTypeQuals,
601  Fn TransformExceptionSpec);
602 
603  bool TransformExceptionSpec(SourceLocation Loc,
604  FunctionProtoType::ExceptionSpecInfo &ESI,
605  SmallVectorImpl<QualType> &Exceptions,
606  bool &Changed);
607 
608  StmtResult TransformSEHHandler(Stmt *Handler);
609 
610  QualType
611  TransformTemplateSpecializationType(TypeLocBuilder &TLB,
612  TemplateSpecializationTypeLoc TL,
613  TemplateName Template);
614 
615  QualType
616  TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
617  DependentTemplateSpecializationTypeLoc TL,
618  TemplateName Template,
619  CXXScopeSpec &SS);
620 
621  QualType TransformDependentTemplateSpecializationType(
622  TypeLocBuilder &TLB, DependentTemplateSpecializationTypeLoc TL,
623  NestedNameSpecifierLoc QualifierLoc);
624 
625  /// Transforms the parameters of a function type into the
626  /// given vectors.
627  ///
628  /// The result vectors should be kept in sync; null entries in the
629  /// variables vector are acceptable.
630  ///
631  /// Return true on error.
632  bool TransformFunctionTypeParams(
633  SourceLocation Loc, ArrayRef<ParmVarDecl *> Params,
634  const QualType *ParamTypes,
635  const FunctionProtoType::ExtParameterInfo *ParamInfos,
636  SmallVectorImpl<QualType> &PTypes, SmallVectorImpl<ParmVarDecl *> *PVars,
637  Sema::ExtParameterInfoBuilder &PInfos);
638 
639  /// Transforms a single function-type parameter. Return null
640  /// on error.
641  ///
642  /// \param indexAdjustment - A number to add to the parameter's
643  /// scope index; can be negative
644  ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
645  int indexAdjustment,
646  Optional<unsigned> NumExpansions,
647  bool ExpectParameterPack);
648 
649  QualType TransformReferenceType(TypeLocBuilder &TLB, ReferenceTypeLoc TL);
650 
651  StmtResult TransformCompoundStmt(CompoundStmt *S, bool IsStmtExpr);
652  ExprResult TransformCXXNamedCastExpr(CXXNamedCastExpr *E);
653 
655  TemplateParameterList *TPL) {
656  return TPL;
657  }
658 
659  ExprResult TransformAddressOfOperand(Expr *E);
660 
661  ExprResult TransformDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E,
662  bool IsAddressOfOperand,
663  TypeSourceInfo **RecoveryTSI);
664 
665  ExprResult TransformParenDependentScopeDeclRefExpr(
666  ParenExpr *PE, DependentScopeDeclRefExpr *DRE, bool IsAddressOfOperand,
667  TypeSourceInfo **RecoveryTSI);
668 
669  StmtResult TransformOMPExecutableDirective(OMPExecutableDirective *S);
670 
671 // FIXME: We use LLVM_ATTRIBUTE_NOINLINE because inlining causes a ridiculous
672 // amount of stack usage with clang.
673 #define STMT(Node, Parent) \
674  LLVM_ATTRIBUTE_NOINLINE \
675  StmtResult Transform##Node(Node *S);
676 #define EXPR(Node, Parent) \
677  LLVM_ATTRIBUTE_NOINLINE \
678  ExprResult Transform##Node(Node *E);
679 #define ABSTRACT_STMT(Stmt)
680 #include "clang/AST/StmtNodes.inc"
681 
682 #define OPENMP_CLAUSE(Name, Class) \
683  LLVM_ATTRIBUTE_NOINLINE \
684  OMPClause *Transform ## Class(Class *S);
685 #include "clang/Basic/OpenMPKinds.def"
686 
687  /// Build a new qualified type given its unqualified type and type
688  /// qualifiers.
689  ///
690  /// By default, this routine adds type qualifiers only to types that can
691  /// have qualifiers, and silently suppresses those qualifiers that are not
692  /// permitted. Subclasses may override this routine to provide different
693  /// behavior.
694  QualType RebuildQualifiedType(QualType T, SourceLocation Loc,
695  Qualifiers Quals);
696 
697  /// Build a new pointer type given its pointee type.
698  ///
699  /// By default, performs semantic analysis when building the pointer type.
700  /// Subclasses may override this routine to provide different behavior.
701  QualType RebuildPointerType(QualType PointeeType, SourceLocation Sigil);
702 
703  /// Build a new block pointer type given its pointee type.
704  ///
705  /// By default, performs semantic analysis when building the block pointer
706  /// type. Subclasses may override this routine to provide different behavior.
707  QualType RebuildBlockPointerType(QualType PointeeType, SourceLocation Sigil);
708 
709  /// Build a new reference type given the type it references.
710  ///
711  /// By default, performs semantic analysis when building the
712  /// reference type. Subclasses may override this routine to provide
713  /// different behavior.
714  ///
715  /// \param LValue whether the type was written with an lvalue sigil
716  /// or an rvalue sigil.
717  QualType RebuildReferenceType(QualType ReferentType,
718  bool LValue,
719  SourceLocation Sigil);
720 
721  /// Build a new member pointer type given the pointee type and the
722  /// class type it refers into.
723  ///
724  /// By default, performs semantic analysis when building the member pointer
725  /// type. Subclasses may override this routine to provide different behavior.
726  QualType RebuildMemberPointerType(QualType PointeeType, QualType ClassType,
727  SourceLocation Sigil);
728 
729  QualType RebuildObjCTypeParamType(const ObjCTypeParamDecl *Decl,
730  SourceLocation ProtocolLAngleLoc,
731  ArrayRef<ObjCProtocolDecl *> Protocols,
732  ArrayRef<SourceLocation> ProtocolLocs,
733  SourceLocation ProtocolRAngleLoc);
734 
735  /// Build an Objective-C object type.
736  ///
737  /// By default, performs semantic analysis when building the object type.
738  /// Subclasses may override this routine to provide different behavior.
739  QualType RebuildObjCObjectType(QualType BaseType,
740  SourceLocation Loc,
741  SourceLocation TypeArgsLAngleLoc,
742  ArrayRef<TypeSourceInfo *> TypeArgs,
743  SourceLocation TypeArgsRAngleLoc,
744  SourceLocation ProtocolLAngleLoc,
745  ArrayRef<ObjCProtocolDecl *> Protocols,
746  ArrayRef<SourceLocation> ProtocolLocs,
747  SourceLocation ProtocolRAngleLoc);
748 
749  /// Build a new Objective-C object pointer type given the pointee type.
750  ///
751  /// By default, directly builds the pointer type, with no additional semantic
752  /// analysis.
753  QualType RebuildObjCObjectPointerType(QualType PointeeType,
754  SourceLocation Star);
755 
756  /// Build a new array type given the element type, size
757  /// modifier, size of the array (if known), size expression, and index type
758  /// qualifiers.
759  ///
760  /// By default, performs semantic analysis when building the array type.
761  /// Subclasses may override this routine to provide different behavior.
762  /// Also by default, all of the other Rebuild*Array
763  QualType RebuildArrayType(QualType ElementType,
765  const llvm::APInt *Size,
766  Expr *SizeExpr,
767  unsigned IndexTypeQuals,
768  SourceRange BracketsRange);
769 
770  /// Build a new constant array type given the element type, size
771  /// modifier, (known) size of the array, and index type qualifiers.
772  ///
773  /// By default, performs semantic analysis when building the array type.
774  /// Subclasses may override this routine to provide different behavior.
775  QualType RebuildConstantArrayType(QualType ElementType,
777  const llvm::APInt &Size,
778  unsigned IndexTypeQuals,
779  SourceRange BracketsRange);
780 
781  /// Build a new incomplete array type given the element type, size
782  /// modifier, and index type qualifiers.
783  ///
784  /// By default, performs semantic analysis when building the array type.
785  /// Subclasses may override this routine to provide different behavior.
786  QualType RebuildIncompleteArrayType(QualType ElementType,
788  unsigned IndexTypeQuals,
789  SourceRange BracketsRange);
790 
791  /// Build a new variable-length array type given the element type,
792  /// size modifier, size expression, and index type qualifiers.
793  ///
794  /// By default, performs semantic analysis when building the array type.
795  /// Subclasses may override this routine to provide different behavior.
796  QualType RebuildVariableArrayType(QualType ElementType,
798  Expr *SizeExpr,
799  unsigned IndexTypeQuals,
800  SourceRange BracketsRange);
801 
802  /// Build a new dependent-sized array type given the element type,
803  /// size modifier, size expression, and index type qualifiers.
804  ///
805  /// By default, performs semantic analysis when building the array type.
806  /// Subclasses may override this routine to provide different behavior.
807  QualType RebuildDependentSizedArrayType(QualType ElementType,
809  Expr *SizeExpr,
810  unsigned IndexTypeQuals,
811  SourceRange BracketsRange);
812 
813  /// Build a new vector type given the element type and
814  /// number of elements.
815  ///
816  /// By default, performs semantic analysis when building the vector type.
817  /// Subclasses may override this routine to provide different behavior.
818  QualType RebuildVectorType(QualType ElementType, unsigned NumElements,
819  VectorType::VectorKind VecKind);
820 
821  /// Build a new potentially dependently-sized extended vector type
822  /// given the element type and number of elements.
823  ///
824  /// By default, performs semantic analysis when building the vector type.
825  /// Subclasses may override this routine to provide different behavior.
826  QualType RebuildDependentVectorType(QualType ElementType, Expr *SizeExpr,
827  SourceLocation AttributeLoc,
829 
830  /// Build a new extended vector type given the element type and
831  /// number of elements.
832  ///
833  /// By default, performs semantic analysis when building the vector type.
834  /// Subclasses may override this routine to provide different behavior.
835  QualType RebuildExtVectorType(QualType ElementType, unsigned NumElements,
836  SourceLocation AttributeLoc);
837 
838  /// Build a new potentially dependently-sized extended vector type
839  /// given the element type and number of elements.
840  ///
841  /// By default, performs semantic analysis when building the vector type.
842  /// Subclasses may override this routine to provide different behavior.
843  QualType RebuildDependentSizedExtVectorType(QualType ElementType,
844  Expr *SizeExpr,
845  SourceLocation AttributeLoc);
846 
847  /// Build a new DependentAddressSpaceType or return the pointee
848  /// type variable with the correct address space (retrieved from
849  /// AddrSpaceExpr) applied to it. The former will be returned in cases
850  /// where the address space remains dependent.
851  ///
852  /// By default, performs semantic analysis when building the type with address
853  /// space applied. Subclasses may override this routine to provide different
854  /// behavior.
855  QualType RebuildDependentAddressSpaceType(QualType PointeeType,
856  Expr *AddrSpaceExpr,
857  SourceLocation AttributeLoc);
858 
859  /// Build a new function type.
860  ///
861  /// By default, performs semantic analysis when building the function type.
862  /// Subclasses may override this routine to provide different behavior.
863  QualType RebuildFunctionProtoType(QualType T,
864  MutableArrayRef<QualType> ParamTypes,
865  const FunctionProtoType::ExtProtoInfo &EPI);
866 
867  /// Build a new unprototyped function type.
868  QualType RebuildFunctionNoProtoType(QualType ResultType);
869 
870  /// Rebuild an unresolved typename type, given the decl that
871  /// the UnresolvedUsingTypenameDecl was transformed to.
872  QualType RebuildUnresolvedUsingType(SourceLocation NameLoc, Decl *D);
873 
874  /// Build a new typedef type.
876  return SemaRef.Context.getTypeDeclType(Typedef);
877  }
878 
879  /// Build a new class/struct/union type.
881  return SemaRef.Context.getTypeDeclType(Record);
882  }
883 
884  /// Build a new Enum type.
886  return SemaRef.Context.getTypeDeclType(Enum);
887  }
888 
889  /// Build a new typeof(expr) type.
890  ///
891  /// By default, performs semantic analysis when building the typeof type.
892  /// Subclasses may override this routine to provide different behavior.
893  QualType RebuildTypeOfExprType(Expr *Underlying, SourceLocation Loc);
894 
895  /// Build a new typeof(type) type.
896  ///
897  /// By default, builds a new TypeOfType with the given underlying type.
898  QualType RebuildTypeOfType(QualType Underlying);
899 
900  /// Build a new unary transform type.
901  QualType RebuildUnaryTransformType(QualType BaseType,
903  SourceLocation Loc);
904 
905  /// Build a new C++11 decltype type.
906  ///
907  /// By default, performs semantic analysis when building the decltype type.
908  /// Subclasses may override this routine to provide different behavior.
909  QualType RebuildDecltypeType(Expr *Underlying, SourceLocation Loc);
910 
911  /// Build a new C++11 auto type.
912  ///
913  /// By default, builds a new AutoType with the given deduced type.
915  // Note, IsDependent is always false here: we implicitly convert an 'auto'
916  // which has been deduced to a dependent type into an undeduced 'auto', so
917  // that we'll retry deduction after the transformation.
918  return SemaRef.Context.getAutoType(Deduced, Keyword,
919  /*IsDependent*/ false);
920  }
921 
922  /// By default, builds a new DeducedTemplateSpecializationType with the given
923  /// deduced type.
925  QualType Deduced) {
927  Template, Deduced, /*IsDependent*/ false);
928  }
929 
930  /// Build a new template specialization type.
931  ///
932  /// By default, performs semantic analysis when building the template
933  /// specialization type. Subclasses may override this routine to provide
934  /// different behavior.
935  QualType RebuildTemplateSpecializationType(TemplateName Template,
936  SourceLocation TemplateLoc,
938 
939  /// Build a new parenthesized type.
940  ///
941  /// By default, builds a new ParenType type from the inner type.
942  /// Subclasses may override this routine to provide different behavior.
944  return SemaRef.BuildParenType(InnerType);
945  }
946 
947  /// Build a new qualified name type.
948  ///
949  /// By default, builds a new ElaboratedType type from the keyword,
950  /// the nested-name-specifier and the named type.
951  /// Subclasses may override this routine to provide different behavior.
953  ElaboratedTypeKeyword Keyword,
954  NestedNameSpecifierLoc QualifierLoc,
955  QualType Named) {
956  return SemaRef.Context.getElaboratedType(Keyword,
957  QualifierLoc.getNestedNameSpecifier(),
958  Named);
959  }
960 
961  /// Build a new typename type that refers to a template-id.
962  ///
963  /// By default, builds a new DependentNameType type from the
964  /// nested-name-specifier and the given type. Subclasses may override
965  /// this routine to provide different behavior.
967  ElaboratedTypeKeyword Keyword,
968  NestedNameSpecifierLoc QualifierLoc,
969  SourceLocation TemplateKWLoc,
970  const IdentifierInfo *Name,
971  SourceLocation NameLoc,
973  bool AllowInjectedClassName) {
974  // Rebuild the template name.
975  // TODO: avoid TemplateName abstraction
976  CXXScopeSpec SS;
977  SS.Adopt(QualifierLoc);
978  TemplateName InstName = getDerived().RebuildTemplateName(
979  SS, TemplateKWLoc, *Name, NameLoc, QualType(), nullptr,
980  AllowInjectedClassName);
981 
982  if (InstName.isNull())
983  return QualType();
984 
985  // If it's still dependent, make a dependent specialization.
986  if (InstName.getAsDependentTemplateName())
987  return SemaRef.Context.getDependentTemplateSpecializationType(Keyword,
988  QualifierLoc.getNestedNameSpecifier(),
989  Name,
990  Args);
991 
992  // Otherwise, make an elaborated type wrapping a non-dependent
993  // specialization.
994  QualType T =
995  getDerived().RebuildTemplateSpecializationType(InstName, NameLoc, Args);
996  if (T.isNull()) return QualType();
997 
998  if (Keyword == ETK_None && QualifierLoc.getNestedNameSpecifier() == nullptr)
999  return T;
1000 
1001  return SemaRef.Context.getElaboratedType(Keyword,
1002  QualifierLoc.getNestedNameSpecifier(),
1003  T);
1004  }
1005 
1006  /// Build a new typename type that refers to an identifier.
1007  ///
1008  /// By default, performs semantic analysis when building the typename type
1009  /// (or elaborated type). Subclasses may override this routine to provide
1010  /// different behavior.
1012  SourceLocation KeywordLoc,
1013  NestedNameSpecifierLoc QualifierLoc,
1014  const IdentifierInfo *Id,
1015  SourceLocation IdLoc,
1016  bool DeducedTSTContext) {
1017  CXXScopeSpec SS;
1018  SS.Adopt(QualifierLoc);
1019 
1020  if (QualifierLoc.getNestedNameSpecifier()->isDependent()) {
1021  // If the name is still dependent, just build a new dependent name type.
1022  if (!SemaRef.computeDeclContext(SS))
1023  return SemaRef.Context.getDependentNameType(Keyword,
1024  QualifierLoc.getNestedNameSpecifier(),
1025  Id);
1026  }
1027 
1028  if (Keyword == ETK_None || Keyword == ETK_Typename) {
1029  QualType T = SemaRef.CheckTypenameType(Keyword, KeywordLoc, QualifierLoc,
1030  *Id, IdLoc);
1031  // If a dependent name resolves to a deduced template specialization type,
1032  // check that we're in one of the syntactic contexts permitting it.
1033  if (!DeducedTSTContext) {
1034  if (auto *Deduced = dyn_cast_or_null<DeducedTemplateSpecializationType>(
1035  T.isNull() ? nullptr : T->getContainedDeducedType())) {
1036  SemaRef.Diag(IdLoc, diag::err_dependent_deduced_tst)
1037  << (int)SemaRef.getTemplateNameKindForDiagnostics(
1038  Deduced->getTemplateName())
1039  << QualType(QualifierLoc.getNestedNameSpecifier()->getAsType(), 0);
1040  if (auto *TD = Deduced->getTemplateName().getAsTemplateDecl())
1041  SemaRef.Diag(TD->getLocation(), diag::note_template_decl_here);
1042  return QualType();
1043  }
1044  }
1045  return T;
1046  }
1047 
1049 
1050  // We had a dependent elaborated-type-specifier that has been transformed
1051  // into a non-dependent elaborated-type-specifier. Find the tag we're
1052  // referring to.
1053  LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
1054  DeclContext *DC = SemaRef.computeDeclContext(SS, false);
1055  if (!DC)
1056  return QualType();
1057 
1058  if (SemaRef.RequireCompleteDeclContext(SS, DC))
1059  return QualType();
1060 
1061  TagDecl *Tag = nullptr;
1062  SemaRef.LookupQualifiedName(Result, DC);
1063  switch (Result.getResultKind()) {
1066  break;
1067 
1068  case LookupResult::Found:
1069  Tag = Result.getAsSingle<TagDecl>();
1070  break;
1071 
1074  llvm_unreachable("Tag lookup cannot find non-tags");
1075 
1077  // Let the LookupResult structure handle ambiguities.
1078  return QualType();
1079  }
1080 
1081  if (!Tag) {
1082  // Check where the name exists but isn't a tag type and use that to emit
1083  // better diagnostics.
1084  LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
1085  SemaRef.LookupQualifiedName(Result, DC);
1086  switch (Result.getResultKind()) {
1087  case LookupResult::Found:
1090  NamedDecl *SomeDecl = Result.getRepresentativeDecl();
1091  Sema::NonTagKind NTK = SemaRef.getNonTagTypeDeclKind(SomeDecl, Kind);
1092  SemaRef.Diag(IdLoc, diag::err_tag_reference_non_tag) << SomeDecl
1093  << NTK << Kind;
1094  SemaRef.Diag(SomeDecl->getLocation(), diag::note_declared_at);
1095  break;
1096  }
1097  default:
1098  SemaRef.Diag(IdLoc, diag::err_not_tag_in_scope)
1099  << Kind << Id << DC << QualifierLoc.getSourceRange();
1100  break;
1101  }
1102  return QualType();
1103  }
1104 
1105  if (!SemaRef.isAcceptableTagRedeclaration(Tag, Kind, /*isDefinition*/false,
1106  IdLoc, Id)) {
1107  SemaRef.Diag(KeywordLoc, diag::err_use_with_wrong_tag) << Id;
1108  SemaRef.Diag(Tag->getLocation(), diag::note_previous_use);
1109  return QualType();
1110  }
1111 
1112  // Build the elaborated-type-specifier type.
1113  QualType T = SemaRef.Context.getTypeDeclType(Tag);
1114  return SemaRef.Context.getElaboratedType(Keyword,
1115  QualifierLoc.getNestedNameSpecifier(),
1116  T);
1117  }
1118 
1119  /// Build a new pack expansion type.
1120  ///
1121  /// By default, builds a new PackExpansionType type from the given pattern.
1122  /// Subclasses may override this routine to provide different behavior.
1124  SourceRange PatternRange,
1125  SourceLocation EllipsisLoc,
1126  Optional<unsigned> NumExpansions) {
1127  return getSema().CheckPackExpansion(Pattern, PatternRange, EllipsisLoc,
1128  NumExpansions);
1129  }
1130 
1131  /// Build a new atomic type given its value type.
1132  ///
1133  /// By default, performs semantic analysis when building the atomic type.
1134  /// Subclasses may override this routine to provide different behavior.
1135  QualType RebuildAtomicType(QualType ValueType, SourceLocation KWLoc);
1136 
1137  /// Build a new pipe type given its value type.
1138  QualType RebuildPipeType(QualType ValueType, SourceLocation KWLoc,
1139  bool isReadPipe);
1140 
1141  /// Build a new template name given a nested name specifier, a flag
1142  /// indicating whether the "template" keyword was provided, and the template
1143  /// that the template name refers to.
1144  ///
1145  /// By default, builds the new template name directly. Subclasses may override
1146  /// this routine to provide different behavior.
1147  TemplateName RebuildTemplateName(CXXScopeSpec &SS,
1148  bool TemplateKW,
1149  TemplateDecl *Template);
1150 
1151  /// Build a new template name given a nested name specifier and the
1152  /// name that is referred to as a template.
1153  ///
1154  /// By default, performs semantic analysis to determine whether the name can
1155  /// be resolved to a specific template, then builds the appropriate kind of
1156  /// template name. Subclasses may override this routine to provide different
1157  /// behavior.
1158  TemplateName RebuildTemplateName(CXXScopeSpec &SS,
1159  SourceLocation TemplateKWLoc,
1160  const IdentifierInfo &Name,
1161  SourceLocation NameLoc, QualType ObjectType,
1162  NamedDecl *FirstQualifierInScope,
1163  bool AllowInjectedClassName);
1164 
1165  /// Build a new template name given a nested name specifier and the
1166  /// overloaded operator name that is referred to as a template.
1167  ///
1168  /// By default, performs semantic analysis to determine whether the name can
1169  /// be resolved to a specific template, then builds the appropriate kind of
1170  /// template name. Subclasses may override this routine to provide different
1171  /// behavior.
1172  TemplateName RebuildTemplateName(CXXScopeSpec &SS,
1173  SourceLocation TemplateKWLoc,
1174  OverloadedOperatorKind Operator,
1175  SourceLocation NameLoc, QualType ObjectType,
1176  bool AllowInjectedClassName);
1177 
1178  /// Build a new template name given a template template parameter pack
1179  /// and the
1180  ///
1181  /// By default, performs semantic analysis to determine whether the name can
1182  /// be resolved to a specific template, then builds the appropriate kind of
1183  /// template name. Subclasses may override this routine to provide different
1184  /// behavior.
1186  const TemplateArgument &ArgPack) {
1187  return getSema().Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
1188  }
1189 
1190  /// Build a new compound statement.
1191  ///
1192  /// By default, performs semantic analysis to build the new statement.
1193  /// Subclasses may override this routine to provide different behavior.
1195  MultiStmtArg Statements,
1196  SourceLocation RBraceLoc,
1197  bool IsStmtExpr) {
1198  return getSema().ActOnCompoundStmt(LBraceLoc, RBraceLoc, Statements,
1199  IsStmtExpr);
1200  }
1201 
1202  /// Build a new case statement.
1203  ///
1204  /// By default, performs semantic analysis to build the new statement.
1205  /// Subclasses may override this routine to provide different behavior.
1207  Expr *LHS,
1208  SourceLocation EllipsisLoc,
1209  Expr *RHS,
1211  return getSema().ActOnCaseStmt(CaseLoc, LHS, EllipsisLoc, RHS,
1212  ColonLoc);
1213  }
1214 
1215  /// Attach the body to a new case statement.
1216  ///
1217  /// By default, performs semantic analysis to build the new statement.
1218  /// Subclasses may override this routine to provide different behavior.
1220  getSema().ActOnCaseStmtBody(S, Body);
1221  return S;
1222  }
1223 
1224  /// Build a new default statement.
1225  ///
1226  /// By default, performs semantic analysis to build the new statement.
1227  /// Subclasses may override this routine to provide different behavior.
1230  Stmt *SubStmt) {
1231  return getSema().ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt,
1232  /*CurScope=*/nullptr);
1233  }
1234 
1235  /// Build a new label statement.
1236  ///
1237  /// By default, performs semantic analysis to build the new statement.
1238  /// Subclasses may override this routine to provide different behavior.
1240  SourceLocation ColonLoc, Stmt *SubStmt) {
1241  return SemaRef.ActOnLabelStmt(IdentLoc, L, ColonLoc, SubStmt);
1242  }
1243 
1244  /// Build a new label statement.
1245  ///
1246  /// By default, performs semantic analysis to build the new statement.
1247  /// Subclasses may override this routine to provide different behavior.
1249  ArrayRef<const Attr*> Attrs,
1250  Stmt *SubStmt) {
1251  return SemaRef.ActOnAttributedStmt(AttrLoc, Attrs, SubStmt);
1252  }
1253 
1254  /// Build a new "if" statement.
1255  ///
1256  /// By default, performs semantic analysis to build the new statement.
1257  /// Subclasses may override this routine to provide different behavior.
1258  StmtResult RebuildIfStmt(SourceLocation IfLoc, bool IsConstexpr,
1259  Sema::ConditionResult Cond, Stmt *Init, Stmt *Then,
1260  SourceLocation ElseLoc, Stmt *Else) {
1261  return getSema().ActOnIfStmt(IfLoc, IsConstexpr, Init, Cond, Then,
1262  ElseLoc, Else);
1263  }
1264 
1265  /// Start building a new switch statement.
1266  ///
1267  /// By default, performs semantic analysis to build the new statement.
1268  /// Subclasses may override this routine to provide different behavior.
1270  Sema::ConditionResult Cond) {
1271  return getSema().ActOnStartOfSwitchStmt(SwitchLoc, Init, Cond);
1272  }
1273 
1274  /// Attach the body to the switch statement.
1275  ///
1276  /// By default, performs semantic analysis to build the new statement.
1277  /// Subclasses may override this routine to provide different behavior.
1279  Stmt *Switch, Stmt *Body) {
1280  return getSema().ActOnFinishSwitchStmt(SwitchLoc, Switch, Body);
1281  }
1282 
1283  /// Build a new while statement.
1284  ///
1285  /// By default, performs semantic analysis to build the new statement.
1286  /// Subclasses may override this routine to provide different behavior.
1288  Sema::ConditionResult Cond, Stmt *Body) {
1289  return getSema().ActOnWhileStmt(WhileLoc, Cond, Body);
1290  }
1291 
1292  /// Build a new do-while statement.
1293  ///
1294  /// By default, performs semantic analysis to build the new statement.
1295  /// Subclasses may override this routine to provide different behavior.
1297  SourceLocation WhileLoc, SourceLocation LParenLoc,
1298  Expr *Cond, SourceLocation RParenLoc) {
1299  return getSema().ActOnDoStmt(DoLoc, Body, WhileLoc, LParenLoc,
1300  Cond, RParenLoc);
1301  }
1302 
1303  /// Build a new for statement.
1304  ///
1305  /// By default, performs semantic analysis to build the new statement.
1306  /// Subclasses may override this routine to provide different behavior.
1308  Stmt *Init, Sema::ConditionResult Cond,
1309  Sema::FullExprArg Inc, SourceLocation RParenLoc,
1310  Stmt *Body) {
1311  return getSema().ActOnForStmt(ForLoc, LParenLoc, Init, Cond,
1312  Inc, RParenLoc, Body);
1313  }
1314 
1315  /// Build a new goto statement.
1316  ///
1317  /// By default, performs semantic analysis to build the new statement.
1318  /// Subclasses may override this routine to provide different behavior.
1320  LabelDecl *Label) {
1321  return getSema().ActOnGotoStmt(GotoLoc, LabelLoc, Label);
1322  }
1323 
1324  /// Build a new indirect goto statement.
1325  ///
1326  /// By default, performs semantic analysis to build the new statement.
1327  /// Subclasses may override this routine to provide different behavior.
1329  SourceLocation StarLoc,
1330  Expr *Target) {
1331  return getSema().ActOnIndirectGotoStmt(GotoLoc, StarLoc, Target);
1332  }
1333 
1334  /// Build a new return statement.
1335  ///
1336  /// By default, performs semantic analysis to build the new statement.
1337  /// Subclasses may override this routine to provide different behavior.
1339  return getSema().BuildReturnStmt(ReturnLoc, Result);
1340  }
1341 
1342  /// Build a new declaration statement.
1343  ///
1344  /// By default, performs semantic analysis to build the new statement.
1345  /// Subclasses may override this routine to provide different behavior.
1347  SourceLocation StartLoc, SourceLocation EndLoc) {
1348  Sema::DeclGroupPtrTy DG = getSema().BuildDeclaratorGroup(Decls);
1349  return getSema().ActOnDeclStmt(DG, StartLoc, EndLoc);
1350  }
1351 
1352  /// Build a new inline asm statement.
1353  ///
1354  /// By default, performs semantic analysis to build the new statement.
1355  /// Subclasses may override this routine to provide different behavior.
1357  bool IsVolatile, unsigned NumOutputs,
1358  unsigned NumInputs, IdentifierInfo **Names,
1359  MultiExprArg Constraints, MultiExprArg Exprs,
1360  Expr *AsmString, MultiExprArg Clobbers,
1361  SourceLocation RParenLoc) {
1362  return getSema().ActOnGCCAsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs,
1363  NumInputs, Names, Constraints, Exprs,
1364  AsmString, Clobbers, RParenLoc);
1365  }
1366 
1367  /// Build a new MS style inline asm statement.
1368  ///
1369  /// By default, performs semantic analysis to build the new statement.
1370  /// Subclasses may override this routine to provide different behavior.
1372  ArrayRef<Token> AsmToks,
1373  StringRef AsmString,
1374  unsigned NumOutputs, unsigned NumInputs,
1375  ArrayRef<StringRef> Constraints,
1376  ArrayRef<StringRef> Clobbers,
1377  ArrayRef<Expr*> Exprs,
1378  SourceLocation EndLoc) {
1379  return getSema().ActOnMSAsmStmt(AsmLoc, LBraceLoc, AsmToks, AsmString,
1380  NumOutputs, NumInputs,
1381  Constraints, Clobbers, Exprs, EndLoc);
1382  }
1383 
1384  /// Build a new co_return statement.
1385  ///
1386  /// By default, performs semantic analysis to build the new statement.
1387  /// Subclasses may override this routine to provide different behavior.
1389  bool IsImplicit) {
1390  return getSema().BuildCoreturnStmt(CoreturnLoc, Result, IsImplicit);
1391  }
1392 
1393  /// Build a new co_await expression.
1394  ///
1395  /// By default, performs semantic analysis to build the new expression.
1396  /// Subclasses may override this routine to provide different behavior.
1398  bool IsImplicit) {
1399  return getSema().BuildResolvedCoawaitExpr(CoawaitLoc, Result, IsImplicit);
1400  }
1401 
1402  /// Build a new co_await expression.
1403  ///
1404  /// By default, performs semantic analysis to build the new expression.
1405  /// Subclasses may override this routine to provide different behavior.
1407  Expr *Result,
1408  UnresolvedLookupExpr *Lookup) {
1409  return getSema().BuildUnresolvedCoawaitExpr(CoawaitLoc, Result, Lookup);
1410  }
1411 
1412  /// Build a new co_yield expression.
1413  ///
1414  /// By default, performs semantic analysis to build the new expression.
1415  /// Subclasses may override this routine to provide different behavior.
1417  return getSema().BuildCoyieldExpr(CoyieldLoc, Result);
1418  }
1419 
1421  return getSema().BuildCoroutineBodyStmt(Args);
1422  }
1423 
1424  /// Build a new Objective-C \@try statement.
1425  ///
1426  /// By default, performs semantic analysis to build the new statement.
1427  /// Subclasses may override this routine to provide different behavior.
1429  Stmt *TryBody,
1430  MultiStmtArg CatchStmts,
1431  Stmt *Finally) {
1432  return getSema().ActOnObjCAtTryStmt(AtLoc, TryBody, CatchStmts,
1433  Finally);
1434  }
1435 
1436  /// Rebuild an Objective-C exception declaration.
1437  ///
1438  /// By default, performs semantic analysis to build the new declaration.
1439  /// Subclasses may override this routine to provide different behavior.
1441  TypeSourceInfo *TInfo, QualType T) {
1442  return getSema().BuildObjCExceptionDecl(TInfo, T,
1443  ExceptionDecl->getInnerLocStart(),
1444  ExceptionDecl->getLocation(),
1445  ExceptionDecl->getIdentifier());
1446  }
1447 
1448  /// Build a new Objective-C \@catch statement.
1449  ///
1450  /// By default, performs semantic analysis to build the new statement.
1451  /// Subclasses may override this routine to provide different behavior.
1453  SourceLocation RParenLoc,
1454  VarDecl *Var,
1455  Stmt *Body) {
1456  return getSema().ActOnObjCAtCatchStmt(AtLoc, RParenLoc,
1457  Var, Body);
1458  }
1459 
1460  /// Build a new Objective-C \@finally statement.
1461  ///
1462  /// By default, performs semantic analysis to build the new statement.
1463  /// Subclasses may override this routine to provide different behavior.
1465  Stmt *Body) {
1466  return getSema().ActOnObjCAtFinallyStmt(AtLoc, Body);
1467  }
1468 
1469  /// Build a new Objective-C \@throw statement.
1470  ///
1471  /// By default, performs semantic analysis to build the new statement.
1472  /// Subclasses may override this routine to provide different behavior.
1474  Expr *Operand) {
1475  return getSema().BuildObjCAtThrowStmt(AtLoc, Operand);
1476  }
1477 
1478  /// Build a new OpenMP executable directive.
1479  ///
1480  /// By default, performs semantic analysis to build the new statement.
1481  /// Subclasses may override this routine to provide different behavior.
1483  DeclarationNameInfo DirName,
1484  OpenMPDirectiveKind CancelRegion,
1485  ArrayRef<OMPClause *> Clauses,
1486  Stmt *AStmt, SourceLocation StartLoc,
1487  SourceLocation EndLoc) {
1488  return getSema().ActOnOpenMPExecutableDirective(
1489  Kind, DirName, CancelRegion, Clauses, AStmt, StartLoc, EndLoc);
1490  }
1491 
1492  /// Build a new OpenMP 'if' clause.
1493  ///
1494  /// By default, performs semantic analysis to build the new OpenMP clause.
1495  /// Subclasses may override this routine to provide different behavior.
1497  Expr *Condition, SourceLocation StartLoc,
1498  SourceLocation LParenLoc,
1499  SourceLocation NameModifierLoc,
1501  SourceLocation EndLoc) {
1502  return getSema().ActOnOpenMPIfClause(NameModifier, Condition, StartLoc,
1503  LParenLoc, NameModifierLoc, ColonLoc,
1504  EndLoc);
1505  }
1506 
1507  /// Build a new OpenMP 'final' clause.
1508  ///
1509  /// By default, performs semantic analysis to build the new OpenMP clause.
1510  /// Subclasses may override this routine to provide different behavior.
1512  SourceLocation LParenLoc,
1513  SourceLocation EndLoc) {
1514  return getSema().ActOnOpenMPFinalClause(Condition, StartLoc, LParenLoc,
1515  EndLoc);
1516  }
1517 
1518  /// Build a new OpenMP 'num_threads' clause.
1519  ///
1520  /// By default, performs semantic analysis to build the new OpenMP clause.
1521  /// Subclasses may override this routine to provide different behavior.
1523  SourceLocation StartLoc,
1524  SourceLocation LParenLoc,
1525  SourceLocation EndLoc) {
1526  return getSema().ActOnOpenMPNumThreadsClause(NumThreads, StartLoc,
1527  LParenLoc, EndLoc);
1528  }
1529 
1530  /// Build a new OpenMP 'safelen' clause.
1531  ///
1532  /// By default, performs semantic analysis to build the new OpenMP clause.
1533  /// Subclasses may override this routine to provide different behavior.
1535  SourceLocation LParenLoc,
1536  SourceLocation EndLoc) {
1537  return getSema().ActOnOpenMPSafelenClause(Len, StartLoc, LParenLoc, EndLoc);
1538  }
1539 
1540  /// Build a new OpenMP 'simdlen' clause.
1541  ///
1542  /// By default, performs semantic analysis to build the new OpenMP clause.
1543  /// Subclasses may override this routine to provide different behavior.
1545  SourceLocation LParenLoc,
1546  SourceLocation EndLoc) {
1547  return getSema().ActOnOpenMPSimdlenClause(Len, StartLoc, LParenLoc, EndLoc);
1548  }
1549 
1550  /// Build a new OpenMP 'collapse' clause.
1551  ///
1552  /// By default, performs semantic analysis to build the new OpenMP clause.
1553  /// Subclasses may override this routine to provide different behavior.
1555  SourceLocation LParenLoc,
1556  SourceLocation EndLoc) {
1557  return getSema().ActOnOpenMPCollapseClause(Num, StartLoc, LParenLoc,
1558  EndLoc);
1559  }
1560 
1561  /// Build a new OpenMP 'default' clause.
1562  ///
1563  /// By default, performs semantic analysis to build the new OpenMP clause.
1564  /// Subclasses may override this routine to provide different behavior.
1566  SourceLocation KindKwLoc,
1567  SourceLocation StartLoc,
1568  SourceLocation LParenLoc,
1569  SourceLocation EndLoc) {
1570  return getSema().ActOnOpenMPDefaultClause(Kind, KindKwLoc,
1571  StartLoc, LParenLoc, EndLoc);
1572  }
1573 
1574  /// Build a new OpenMP 'proc_bind' clause.
1575  ///
1576  /// By default, performs semantic analysis to build the new OpenMP clause.
1577  /// Subclasses may override this routine to provide different behavior.
1579  SourceLocation KindKwLoc,
1580  SourceLocation StartLoc,
1581  SourceLocation LParenLoc,
1582  SourceLocation EndLoc) {
1583  return getSema().ActOnOpenMPProcBindClause(Kind, KindKwLoc,
1584  StartLoc, LParenLoc, EndLoc);
1585  }
1586 
1587  /// Build a new OpenMP 'schedule' clause.
1588  ///
1589  /// By default, performs semantic analysis to build the new OpenMP clause.
1590  /// Subclasses may override this routine to provide different behavior.
1593  OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
1594  SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
1595  SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
1596  return getSema().ActOnOpenMPScheduleClause(
1597  M1, M2, Kind, ChunkSize, StartLoc, LParenLoc, M1Loc, M2Loc, KindLoc,
1598  CommaLoc, EndLoc);
1599  }
1600 
1601  /// Build a new OpenMP 'ordered' clause.
1602  ///
1603  /// By default, performs semantic analysis to build the new OpenMP clause.
1604  /// Subclasses may override this routine to provide different behavior.
1606  SourceLocation EndLoc,
1607  SourceLocation LParenLoc, Expr *Num) {
1608  return getSema().ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Num);
1609  }
1610 
1611  /// Build a new OpenMP 'private' clause.
1612  ///
1613  /// By default, performs semantic analysis to build the new OpenMP clause.
1614  /// Subclasses may override this routine to provide different behavior.
1616  SourceLocation StartLoc,
1617  SourceLocation LParenLoc,
1618  SourceLocation EndLoc) {
1619  return getSema().ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc,
1620  EndLoc);
1621  }
1622 
1623  /// Build a new OpenMP 'firstprivate' clause.
1624  ///
1625  /// By default, performs semantic analysis to build the new OpenMP clause.
1626  /// Subclasses may override this routine to provide different behavior.
1628  SourceLocation StartLoc,
1629  SourceLocation LParenLoc,
1630  SourceLocation EndLoc) {
1631  return getSema().ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc,
1632  EndLoc);
1633  }
1634 
1635  /// Build a new OpenMP 'lastprivate' clause.
1636  ///
1637  /// By default, performs semantic analysis to build the new OpenMP clause.
1638  /// Subclasses may override this routine to provide different behavior.
1640  SourceLocation StartLoc,
1641  SourceLocation LParenLoc,
1642  SourceLocation EndLoc) {
1643  return getSema().ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc,
1644  EndLoc);
1645  }
1646 
1647  /// Build a new OpenMP 'shared' clause.
1648  ///
1649  /// By default, performs semantic analysis to build the new OpenMP clause.
1650  /// Subclasses may override this routine to provide different behavior.
1652  SourceLocation StartLoc,
1653  SourceLocation LParenLoc,
1654  SourceLocation EndLoc) {
1655  return getSema().ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc,
1656  EndLoc);
1657  }
1658 
1659  /// Build a new OpenMP 'reduction' clause.
1660  ///
1661  /// By default, performs semantic analysis to build the new statement.
1662  /// Subclasses may override this routine to provide different behavior.
1664  SourceLocation StartLoc,
1665  SourceLocation LParenLoc,
1667  SourceLocation EndLoc,
1668  CXXScopeSpec &ReductionIdScopeSpec,
1669  const DeclarationNameInfo &ReductionId,
1670  ArrayRef<Expr *> UnresolvedReductions) {
1671  return getSema().ActOnOpenMPReductionClause(
1672  VarList, StartLoc, LParenLoc, ColonLoc, EndLoc, ReductionIdScopeSpec,
1673  ReductionId, UnresolvedReductions);
1674  }
1675 
1676  /// Build a new OpenMP 'task_reduction' clause.
1677  ///
1678  /// By default, performs semantic analysis to build the new statement.
1679  /// Subclasses may override this routine to provide different behavior.
1681  ArrayRef<Expr *> VarList, SourceLocation StartLoc,
1683  CXXScopeSpec &ReductionIdScopeSpec,
1684  const DeclarationNameInfo &ReductionId,
1685  ArrayRef<Expr *> UnresolvedReductions) {
1686  return getSema().ActOnOpenMPTaskReductionClause(
1687  VarList, StartLoc, LParenLoc, ColonLoc, EndLoc, ReductionIdScopeSpec,
1688  ReductionId, UnresolvedReductions);
1689  }
1690 
1691  /// Build a new OpenMP 'in_reduction' clause.
1692  ///
1693  /// By default, performs semantic analysis to build the new statement.
1694  /// Subclasses may override this routine to provide different behavior.
1695  OMPClause *
1698  SourceLocation EndLoc,
1699  CXXScopeSpec &ReductionIdScopeSpec,
1700  const DeclarationNameInfo &ReductionId,
1701  ArrayRef<Expr *> UnresolvedReductions) {
1702  return getSema().ActOnOpenMPInReductionClause(
1703  VarList, StartLoc, LParenLoc, ColonLoc, EndLoc, ReductionIdScopeSpec,
1704  ReductionId, UnresolvedReductions);
1705  }
1706 
1707  /// Build a new OpenMP 'linear' clause.
1708  ///
1709  /// By default, performs semantic analysis to build the new OpenMP clause.
1710  /// Subclasses may override this routine to provide different behavior.
1712  SourceLocation StartLoc,
1713  SourceLocation LParenLoc,
1717  SourceLocation EndLoc) {
1718  return getSema().ActOnOpenMPLinearClause(VarList, Step, StartLoc, LParenLoc,
1719  Modifier, ModifierLoc, ColonLoc,
1720  EndLoc);
1721  }
1722 
1723  /// Build a new OpenMP 'aligned' clause.
1724  ///
1725  /// By default, performs semantic analysis to build the new OpenMP clause.
1726  /// Subclasses may override this routine to provide different behavior.
1728  SourceLocation StartLoc,
1729  SourceLocation LParenLoc,
1731  SourceLocation EndLoc) {
1732  return getSema().ActOnOpenMPAlignedClause(VarList, Alignment, StartLoc,
1733  LParenLoc, ColonLoc, EndLoc);
1734  }
1735 
1736  /// Build a new OpenMP 'copyin' clause.
1737  ///
1738  /// By default, performs semantic analysis to build the new OpenMP clause.
1739  /// Subclasses may override this routine to provide different behavior.
1741  SourceLocation StartLoc,
1742  SourceLocation LParenLoc,
1743  SourceLocation EndLoc) {
1744  return getSema().ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc,
1745  EndLoc);
1746  }
1747 
1748  /// Build a new OpenMP 'copyprivate' clause.
1749  ///
1750  /// By default, performs semantic analysis to build the new OpenMP clause.
1751  /// Subclasses may override this routine to provide different behavior.
1753  SourceLocation StartLoc,
1754  SourceLocation LParenLoc,
1755  SourceLocation EndLoc) {
1756  return getSema().ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc,
1757  EndLoc);
1758  }
1759 
1760  /// Build a new OpenMP 'flush' pseudo clause.
1761  ///
1762  /// By default, performs semantic analysis to build the new OpenMP clause.
1763  /// Subclasses may override this routine to provide different behavior.
1765  SourceLocation StartLoc,
1766  SourceLocation LParenLoc,
1767  SourceLocation EndLoc) {
1768  return getSema().ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc,
1769  EndLoc);
1770  }
1771 
1772  /// Build a new OpenMP 'depend' pseudo clause.
1773  ///
1774  /// By default, performs semantic analysis to build the new OpenMP clause.
1775  /// Subclasses may override this routine to provide different behavior.
1776  OMPClause *
1779  SourceLocation StartLoc, SourceLocation LParenLoc,
1780  SourceLocation EndLoc) {
1781  return getSema().ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList,
1782  StartLoc, LParenLoc, EndLoc);
1783  }
1784 
1785  /// Build a new OpenMP 'device' clause.
1786  ///
1787  /// By default, performs semantic analysis to build the new statement.
1788  /// Subclasses may override this routine to provide different behavior.
1790  SourceLocation LParenLoc,
1791  SourceLocation EndLoc) {
1792  return getSema().ActOnOpenMPDeviceClause(Device, StartLoc, LParenLoc,
1793  EndLoc);
1794  }
1795 
1796  /// Build a new OpenMP 'map' clause.
1797  ///
1798  /// By default, performs semantic analysis to build the new OpenMP clause.
1799  /// Subclasses may override this routine to provide different behavior.
1800  OMPClause *
1802  OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
1804  ArrayRef<Expr *> VarList, SourceLocation StartLoc,
1805  SourceLocation LParenLoc, SourceLocation EndLoc) {
1806  return getSema().ActOnOpenMPMapClause(MapTypeModifier, MapType,
1807  IsMapTypeImplicit, MapLoc, ColonLoc,
1808  VarList, StartLoc, LParenLoc, EndLoc);
1809  }
1810 
1811  /// Build a new OpenMP 'num_teams' clause.
1812  ///
1813  /// By default, performs semantic analysis to build the new statement.
1814  /// Subclasses may override this routine to provide different behavior.
1816  SourceLocation LParenLoc,
1817  SourceLocation EndLoc) {
1818  return getSema().ActOnOpenMPNumTeamsClause(NumTeams, StartLoc, LParenLoc,
1819  EndLoc);
1820  }
1821 
1822  /// Build a new OpenMP 'thread_limit' clause.
1823  ///
1824  /// By default, performs semantic analysis to build the new statement.
1825  /// Subclasses may override this routine to provide different behavior.
1827  SourceLocation StartLoc,
1828  SourceLocation LParenLoc,
1829  SourceLocation EndLoc) {
1830  return getSema().ActOnOpenMPThreadLimitClause(ThreadLimit, StartLoc,
1831  LParenLoc, EndLoc);
1832  }
1833 
1834  /// Build a new OpenMP 'priority' clause.
1835  ///
1836  /// By default, performs semantic analysis to build the new statement.
1837  /// Subclasses may override this routine to provide different behavior.
1839  SourceLocation LParenLoc,
1840  SourceLocation EndLoc) {
1841  return getSema().ActOnOpenMPPriorityClause(Priority, StartLoc, LParenLoc,
1842  EndLoc);
1843  }
1844 
1845  /// Build a new OpenMP 'grainsize' clause.
1846  ///
1847  /// By default, performs semantic analysis to build the new statement.
1848  /// Subclasses may override this routine to provide different behavior.
1850  SourceLocation LParenLoc,
1851  SourceLocation EndLoc) {
1852  return getSema().ActOnOpenMPGrainsizeClause(Grainsize, StartLoc, LParenLoc,
1853  EndLoc);
1854  }
1855 
1856  /// Build a new OpenMP 'num_tasks' clause.
1857  ///
1858  /// By default, performs semantic analysis to build the new statement.
1859  /// Subclasses may override this routine to provide different behavior.
1861  SourceLocation LParenLoc,
1862  SourceLocation EndLoc) {
1863  return getSema().ActOnOpenMPNumTasksClause(NumTasks, StartLoc, LParenLoc,
1864  EndLoc);
1865  }
1866 
1867  /// Build a new OpenMP 'hint' clause.
1868  ///
1869  /// By default, performs semantic analysis to build the new statement.
1870  /// Subclasses may override this routine to provide different behavior.
1872  SourceLocation LParenLoc,
1873  SourceLocation EndLoc) {
1874  return getSema().ActOnOpenMPHintClause(Hint, StartLoc, LParenLoc, EndLoc);
1875  }
1876 
1877  /// Build a new OpenMP 'dist_schedule' clause.
1878  ///
1879  /// By default, performs semantic analysis to build the new OpenMP clause.
1880  /// Subclasses may override this routine to provide different behavior.
1881  OMPClause *
1883  Expr *ChunkSize, SourceLocation StartLoc,
1884  SourceLocation LParenLoc, SourceLocation KindLoc,
1885  SourceLocation CommaLoc, SourceLocation EndLoc) {
1886  return getSema().ActOnOpenMPDistScheduleClause(
1887  Kind, ChunkSize, StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc);
1888  }
1889 
1890  /// Build a new OpenMP 'to' clause.
1891  ///
1892  /// By default, performs semantic analysis to build the new statement.
1893  /// Subclasses may override this routine to provide different behavior.
1895  SourceLocation StartLoc,
1896  SourceLocation LParenLoc,
1897  SourceLocation EndLoc) {
1898  return getSema().ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
1899  }
1900 
1901  /// Build a new OpenMP 'from' clause.
1902  ///
1903  /// By default, performs semantic analysis to build the new statement.
1904  /// Subclasses may override this routine to provide different behavior.
1906  SourceLocation StartLoc,
1907  SourceLocation LParenLoc,
1908  SourceLocation EndLoc) {
1909  return getSema().ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc,
1910  EndLoc);
1911  }
1912 
1913  /// Build a new OpenMP 'use_device_ptr' clause.
1914  ///
1915  /// By default, performs semantic analysis to build the new OpenMP clause.
1916  /// Subclasses may override this routine to provide different behavior.
1918  SourceLocation StartLoc,
1919  SourceLocation LParenLoc,
1920  SourceLocation EndLoc) {
1921  return getSema().ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc,
1922  EndLoc);
1923  }
1924 
1925  /// Build a new OpenMP 'is_device_ptr' clause.
1926  ///
1927  /// By default, performs semantic analysis to build the new OpenMP clause.
1928  /// Subclasses may override this routine to provide different behavior.
1930  SourceLocation StartLoc,
1931  SourceLocation LParenLoc,
1932  SourceLocation EndLoc) {
1933  return getSema().ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc,
1934  EndLoc);
1935  }
1936 
1937  /// Rebuild the operand to an Objective-C \@synchronized statement.
1938  ///
1939  /// By default, performs semantic analysis to build the new statement.
1940  /// Subclasses may override this routine to provide different behavior.
1942  Expr *object) {
1943  return getSema().ActOnObjCAtSynchronizedOperand(atLoc, object);
1944  }
1945 
1946  /// Build a new Objective-C \@synchronized statement.
1947  ///
1948  /// By default, performs semantic analysis to build the new statement.
1949  /// Subclasses may override this routine to provide different behavior.
1951  Expr *Object, Stmt *Body) {
1952  return getSema().ActOnObjCAtSynchronizedStmt(AtLoc, Object, Body);
1953  }
1954 
1955  /// Build a new Objective-C \@autoreleasepool statement.
1956  ///
1957  /// By default, performs semantic analysis to build the new statement.
1958  /// Subclasses may override this routine to provide different behavior.
1960  Stmt *Body) {
1961  return getSema().ActOnObjCAutoreleasePoolStmt(AtLoc, Body);
1962  }
1963 
1964  /// Build a new Objective-C fast enumeration statement.
1965  ///
1966  /// By default, performs semantic analysis to build the new statement.
1967  /// Subclasses may override this routine to provide different behavior.
1969  Stmt *Element,
1970  Expr *Collection,
1971  SourceLocation RParenLoc,
1972  Stmt *Body) {
1973  StmtResult ForEachStmt = getSema().ActOnObjCForCollectionStmt(ForLoc,
1974  Element,
1975  Collection,
1976  RParenLoc);
1977  if (ForEachStmt.isInvalid())
1978  return StmtError();
1979 
1980  return getSema().FinishObjCForCollectionStmt(ForEachStmt.get(), Body);
1981  }
1982 
1983  /// Build a new C++ exception declaration.
1984  ///
1985  /// By default, performs semantic analysis to build the new decaration.
1986  /// Subclasses may override this routine to provide different behavior.
1989  SourceLocation StartLoc,
1990  SourceLocation IdLoc,
1991  IdentifierInfo *Id) {
1992  VarDecl *Var = getSema().BuildExceptionDeclaration(nullptr, Declarator,
1993  StartLoc, IdLoc, Id);
1994  if (Var)
1995  getSema().CurContext->addDecl(Var);
1996  return Var;
1997  }
1998 
1999  /// Build a new C++ catch statement.
2000  ///
2001  /// By default, performs semantic analysis to build the new statement.
2002  /// Subclasses may override this routine to provide different behavior.
2004  VarDecl *ExceptionDecl,
2005  Stmt *Handler) {
2006  return Owned(new (getSema().Context) CXXCatchStmt(CatchLoc, ExceptionDecl,
2007  Handler));
2008  }
2009 
2010  /// Build a new C++ try statement.
2011  ///
2012  /// By default, performs semantic analysis to build the new statement.
2013  /// Subclasses may override this routine to provide different behavior.
2015  ArrayRef<Stmt *> Handlers) {
2016  return getSema().ActOnCXXTryBlock(TryLoc, TryBlock, Handlers);
2017  }
2018 
2019  /// Build a new C++0x range-based for statement.
2020  ///
2021  /// By default, performs semantic analysis to build the new statement.
2022  /// Subclasses may override this routine to provide different behavior.
2024  SourceLocation CoawaitLoc,
2026  Stmt *Range, Stmt *Begin, Stmt *End,
2027  Expr *Cond, Expr *Inc,
2028  Stmt *LoopVar,
2029  SourceLocation RParenLoc) {
2030  // If we've just learned that the range is actually an Objective-C
2031  // collection, treat this as an Objective-C fast enumeration loop.
2032  if (DeclStmt *RangeStmt = dyn_cast<DeclStmt>(Range)) {
2033  if (RangeStmt->isSingleDecl()) {
2034  if (VarDecl *RangeVar = dyn_cast<VarDecl>(RangeStmt->getSingleDecl())) {
2035  if (RangeVar->isInvalidDecl())
2036  return StmtError();
2037 
2038  Expr *RangeExpr = RangeVar->getInit();
2039  if (!RangeExpr->isTypeDependent() &&
2040  RangeExpr->getType()->isObjCObjectPointerType())
2041  return getSema().ActOnObjCForCollectionStmt(ForLoc, LoopVar, RangeExpr,
2042  RParenLoc);
2043  }
2044  }
2045  }
2046 
2047  return getSema().BuildCXXForRangeStmt(ForLoc, CoawaitLoc, ColonLoc,
2048  Range, Begin, End,
2049  Cond, Inc, LoopVar, RParenLoc,
2051  }
2052 
2053  /// Build a new C++0x range-based for statement.
2054  ///
2055  /// By default, performs semantic analysis to build the new statement.
2056  /// Subclasses may override this routine to provide different behavior.
2058  bool IsIfExists,
2059  NestedNameSpecifierLoc QualifierLoc,
2060  DeclarationNameInfo NameInfo,
2061  Stmt *Nested) {
2062  return getSema().BuildMSDependentExistsStmt(KeywordLoc, IsIfExists,
2063  QualifierLoc, NameInfo, Nested);
2064  }
2065 
2066  /// Attach body to a C++0x range-based for statement.
2067  ///
2068  /// By default, performs semantic analysis to finish the new statement.
2069  /// Subclasses may override this routine to provide different behavior.
2071  return getSema().FinishCXXForRangeStmt(ForRange, Body);
2072  }
2073 
2075  Stmt *TryBlock, Stmt *Handler) {
2076  return getSema().ActOnSEHTryBlock(IsCXXTry, TryLoc, TryBlock, Handler);
2077  }
2078 
2080  Stmt *Block) {
2081  return getSema().ActOnSEHExceptBlock(Loc, FilterExpr, Block);
2082  }
2083 
2085  return SEHFinallyStmt::Create(getSema().getASTContext(), Loc, Block);
2086  }
2087 
2088  /// Build a new predefined expression.
2089  ///
2090  /// By default, performs semantic analysis to build the new expression.
2091  /// Subclasses may override this routine to provide different behavior.
2094  return getSema().BuildPredefinedExpr(Loc, IT);
2095  }
2096 
2097  /// Build a new expression that references a declaration.
2098  ///
2099  /// By default, performs semantic analysis to build the new expression.
2100  /// Subclasses may override this routine to provide different behavior.
2102  LookupResult &R,
2103  bool RequiresADL) {
2104  return getSema().BuildDeclarationNameExpr(SS, R, RequiresADL);
2105  }
2106 
2107 
2108  /// Build a new expression that references a declaration.
2109  ///
2110  /// By default, performs semantic analysis to build the new expression.
2111  /// Subclasses may override this routine to provide different behavior.
2113  ValueDecl *VD,
2114  const DeclarationNameInfo &NameInfo,
2115  TemplateArgumentListInfo *TemplateArgs) {
2116  CXXScopeSpec SS;
2117  SS.Adopt(QualifierLoc);
2118 
2119  // FIXME: loses template args.
2120 
2121  return getSema().BuildDeclarationNameExpr(SS, NameInfo, VD);
2122  }
2123 
2124  /// Build a new expression in parentheses.
2125  ///
2126  /// By default, performs semantic analysis to build the new expression.
2127  /// Subclasses may override this routine to provide different behavior.
2129  SourceLocation RParen) {
2130  return getSema().ActOnParenExpr(LParen, RParen, SubExpr);
2131  }
2132 
2133  /// Build a new pseudo-destructor expression.
2134  ///
2135  /// By default, performs semantic analysis to build the new expression.
2136  /// Subclasses may override this routine to provide different behavior.
2137  ExprResult RebuildCXXPseudoDestructorExpr(Expr *Base,
2138  SourceLocation OperatorLoc,
2139  bool isArrow,
2140  CXXScopeSpec &SS,
2141  TypeSourceInfo *ScopeType,
2142  SourceLocation CCLoc,
2143  SourceLocation TildeLoc,
2144  PseudoDestructorTypeStorage Destroyed);
2145 
2146  /// Build a new unary operator expression.
2147  ///
2148  /// By default, performs semantic analysis to build the new expression.
2149  /// Subclasses may override this routine to provide different behavior.
2151  UnaryOperatorKind Opc,
2152  Expr *SubExpr) {
2153  return getSema().BuildUnaryOp(/*Scope=*/nullptr, OpLoc, Opc, SubExpr);
2154  }
2155 
2156  /// Build a new builtin offsetof expression.
2157  ///
2158  /// By default, performs semantic analysis to build the new expression.
2159  /// Subclasses may override this routine to provide different behavior.
2163  SourceLocation RParenLoc) {
2164  return getSema().BuildBuiltinOffsetOf(OperatorLoc, Type, Components,
2165  RParenLoc);
2166  }
2167 
2168  /// Build a new sizeof, alignof or vec_step expression with a
2169  /// type argument.
2170  ///
2171  /// By default, performs semantic analysis to build the new expression.
2172  /// Subclasses may override this routine to provide different behavior.
2174  SourceLocation OpLoc,
2175  UnaryExprOrTypeTrait ExprKind,
2176  SourceRange R) {
2177  return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
2178  }
2179 
2180  /// Build a new sizeof, alignof or vec step expression with an
2181  /// expression argument.
2182  ///
2183  /// By default, performs semantic analysis to build the new expression.
2184  /// Subclasses may override this routine to provide different behavior.
2186  UnaryExprOrTypeTrait ExprKind,
2187  SourceRange R) {
2188  ExprResult Result
2189  = getSema().CreateUnaryExprOrTypeTraitExpr(SubExpr, OpLoc, ExprKind);
2190  if (Result.isInvalid())
2191  return ExprError();
2192 
2193  return Result;
2194  }
2195 
2196  /// Build a new array subscript expression.
2197  ///
2198  /// By default, performs semantic analysis to build the new expression.
2199  /// Subclasses may override this routine to provide different behavior.
2201  SourceLocation LBracketLoc,
2202  Expr *RHS,
2203  SourceLocation RBracketLoc) {
2204  return getSema().ActOnArraySubscriptExpr(/*Scope=*/nullptr, LHS,
2205  LBracketLoc, RHS,
2206  RBracketLoc);
2207  }
2208 
2209  /// Build a new array section expression.
2210  ///
2211  /// By default, performs semantic analysis to build the new expression.
2212  /// Subclasses may override this routine to provide different behavior.
2214  Expr *LowerBound,
2215  SourceLocation ColonLoc, Expr *Length,
2216  SourceLocation RBracketLoc) {
2217  return getSema().ActOnOMPArraySectionExpr(Base, LBracketLoc, LowerBound,
2218  ColonLoc, Length, RBracketLoc);
2219  }
2220 
2221  /// Build a new call expression.
2222  ///
2223  /// By default, performs semantic analysis to build the new expression.
2224  /// Subclasses may override this routine to provide different behavior.
2226  MultiExprArg Args,
2227  SourceLocation RParenLoc,
2228  Expr *ExecConfig = nullptr) {
2229  return getSema().ActOnCallExpr(/*Scope=*/nullptr, Callee, LParenLoc,
2230  Args, RParenLoc, ExecConfig);
2231  }
2232 
2233  /// Build a new member access expression.
2234  ///
2235  /// By default, performs semantic analysis to build the new expression.
2236  /// Subclasses may override this routine to provide different behavior.
2238  bool isArrow,
2239  NestedNameSpecifierLoc QualifierLoc,
2240  SourceLocation TemplateKWLoc,
2241  const DeclarationNameInfo &MemberNameInfo,
2242  ValueDecl *Member,
2243  NamedDecl *FoundDecl,
2244  const TemplateArgumentListInfo *ExplicitTemplateArgs,
2245  NamedDecl *FirstQualifierInScope) {
2246  ExprResult BaseResult = getSema().PerformMemberExprBaseConversion(Base,
2247  isArrow);
2248  if (!Member->getDeclName()) {
2249  // We have a reference to an unnamed field. This is always the
2250  // base of an anonymous struct/union member access, i.e. the
2251  // field is always of record type.
2252  assert(Member->getType()->isRecordType() &&
2253  "unnamed member not of record type?");
2254 
2255  BaseResult =
2256  getSema().PerformObjectMemberConversion(BaseResult.get(),
2257  QualifierLoc.getNestedNameSpecifier(),
2258  FoundDecl, Member);
2259  if (BaseResult.isInvalid())
2260  return ExprError();
2261  Base = BaseResult.get();
2262 
2263  CXXScopeSpec EmptySS;
2264  return getSema().BuildFieldReferenceExpr(
2265  Base, isArrow, OpLoc, EmptySS, cast<FieldDecl>(Member),
2266  DeclAccessPair::make(FoundDecl, FoundDecl->getAccess()), MemberNameInfo);
2267  }
2268 
2269  CXXScopeSpec SS;
2270  SS.Adopt(QualifierLoc);
2271 
2272  Base = BaseResult.get();
2273  QualType BaseType = Base->getType();
2274 
2275  if (isArrow && !BaseType->isPointerType())
2276  return ExprError();
2277 
2278  // FIXME: this involves duplicating earlier analysis in a lot of
2279  // cases; we should avoid this when possible.
2280  LookupResult R(getSema(), MemberNameInfo, Sema::LookupMemberName);
2281  R.addDecl(FoundDecl);
2282  R.resolveKind();
2283 
2284  return getSema().BuildMemberReferenceExpr(Base, BaseType, OpLoc, isArrow,
2285  SS, TemplateKWLoc,
2286  FirstQualifierInScope,
2287  R, ExplicitTemplateArgs,
2288  /*S*/nullptr);
2289  }
2290 
2291  /// Build a new binary operator expression.
2292  ///
2293  /// By default, performs semantic analysis to build the new expression.
2294  /// Subclasses may override this routine to provide different behavior.
2296  BinaryOperatorKind Opc,
2297  Expr *LHS, Expr *RHS) {
2298  return getSema().BuildBinOp(/*Scope=*/nullptr, OpLoc, Opc, LHS, RHS);
2299  }
2300 
2301  /// Build a new conditional operator expression.
2302  ///
2303  /// By default, performs semantic analysis to build the new expression.
2304  /// Subclasses may override this routine to provide different behavior.
2306  SourceLocation QuestionLoc,
2307  Expr *LHS,
2309  Expr *RHS) {
2310  return getSema().ActOnConditionalOp(QuestionLoc, ColonLoc, Cond,
2311  LHS, RHS);
2312  }
2313 
2314  /// Build a new C-style cast expression.
2315  ///
2316  /// By default, performs semantic analysis to build the new expression.
2317  /// Subclasses may override this routine to provide different behavior.
2319  TypeSourceInfo *TInfo,
2320  SourceLocation RParenLoc,
2321  Expr *SubExpr) {
2322  return getSema().BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc,
2323  SubExpr);
2324  }
2325 
2326  /// Build a new compound literal expression.
2327  ///
2328  /// By default, performs semantic analysis to build the new expression.
2329  /// Subclasses may override this routine to provide different behavior.
2331  TypeSourceInfo *TInfo,
2332  SourceLocation RParenLoc,
2333  Expr *Init) {
2334  return getSema().BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc,
2335  Init);
2336  }
2337 
2338  /// Build a new extended vector element access expression.
2339  ///
2340  /// By default, performs semantic analysis to build the new expression.
2341  /// Subclasses may override this routine to provide different behavior.
2343  SourceLocation OpLoc,
2344  SourceLocation AccessorLoc,
2345  IdentifierInfo &Accessor) {
2346 
2347  CXXScopeSpec SS;
2348  DeclarationNameInfo NameInfo(&Accessor, AccessorLoc);
2349  return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
2350  OpLoc, /*IsArrow*/ false,
2351  SS, SourceLocation(),
2352  /*FirstQualifierInScope*/ nullptr,
2353  NameInfo,
2354  /* TemplateArgs */ nullptr,
2355  /*S*/ nullptr);
2356  }
2357 
2358  /// Build a new initializer list expression.
2359  ///
2360  /// By default, performs semantic analysis to build the new expression.
2361  /// Subclasses may override this routine to provide different behavior.
2364  SourceLocation RBraceLoc) {
2365  return SemaRef.ActOnInitList(LBraceLoc, Inits, RBraceLoc);
2366  }
2367 
2368  /// Build a new designated initializer expression.
2369  ///
2370  /// By default, performs semantic analysis to build the new expression.
2371  /// Subclasses may override this routine to provide different behavior.
2373  MultiExprArg ArrayExprs,
2374  SourceLocation EqualOrColonLoc,
2375  bool GNUSyntax,
2376  Expr *Init) {
2377  ExprResult Result
2378  = SemaRef.ActOnDesignatedInitializer(Desig, EqualOrColonLoc, GNUSyntax,
2379  Init);
2380  if (Result.isInvalid())
2381  return ExprError();
2382 
2383  return Result;
2384  }
2385 
2386  /// Build a new value-initialized expression.
2387  ///
2388  /// By default, builds the implicit value initialization without performing
2389  /// any semantic analysis. Subclasses may override this routine to provide
2390  /// different behavior.
2392  return new (SemaRef.Context) ImplicitValueInitExpr(T);
2393  }
2394 
2395  /// Build a new \c va_arg expression.
2396  ///
2397  /// By default, performs semantic analysis to build the new expression.
2398  /// Subclasses may override this routine to provide different behavior.
2400  Expr *SubExpr, TypeSourceInfo *TInfo,
2401  SourceLocation RParenLoc) {
2402  return getSema().BuildVAArgExpr(BuiltinLoc,
2403  SubExpr, TInfo,
2404  RParenLoc);
2405  }
2406 
2407  /// Build a new expression list in parentheses.
2408  ///
2409  /// By default, performs semantic analysis to build the new expression.
2410  /// Subclasses may override this routine to provide different behavior.
2412  MultiExprArg SubExprs,
2413  SourceLocation RParenLoc) {
2414  return getSema().ActOnParenListExpr(LParenLoc, RParenLoc, SubExprs);
2415  }
2416 
2417  /// Build a new address-of-label expression.
2418  ///
2419  /// By default, performs semantic analysis, using the name of the label
2420  /// rather than attempting to map the label statement itself.
2421  /// Subclasses may override this routine to provide different behavior.
2423  SourceLocation LabelLoc, LabelDecl *Label) {
2424  return getSema().ActOnAddrLabel(AmpAmpLoc, LabelLoc, Label);
2425  }
2426 
2427  /// Build a new GNU statement expression.
2428  ///
2429  /// By default, performs semantic analysis to build the new expression.
2430  /// Subclasses may override this routine to provide different behavior.
2432  Stmt *SubStmt,
2433  SourceLocation RParenLoc) {
2434  return getSema().ActOnStmtExpr(LParenLoc, SubStmt, RParenLoc);
2435  }
2436 
2437  /// Build a new __builtin_choose_expr expression.
2438  ///
2439  /// By default, performs semantic analysis to build the new expression.
2440  /// Subclasses may override this routine to provide different behavior.
2442  Expr *Cond, Expr *LHS, Expr *RHS,
2443  SourceLocation RParenLoc) {
2444  return SemaRef.ActOnChooseExpr(BuiltinLoc,
2445  Cond, LHS, RHS,
2446  RParenLoc);
2447  }
2448 
2449  /// Build a new generic selection expression.
2450  ///
2451  /// By default, performs semantic analysis to build the new expression.
2452  /// Subclasses may override this routine to provide different behavior.
2454  SourceLocation DefaultLoc,
2455  SourceLocation RParenLoc,
2456  Expr *ControllingExpr,
2458  ArrayRef<Expr *> Exprs) {
2459  return getSema().CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc,
2460  ControllingExpr, Types, Exprs);
2461  }
2462 
2463  /// Build a new overloaded operator call expression.
2464  ///
2465  /// By default, performs semantic analysis to build the new expression.
2466  /// The semantic analysis provides the behavior of template instantiation,
2467  /// copying with transformations that turn what looks like an overloaded
2468  /// operator call into a use of a builtin operator, performing
2469  /// argument-dependent lookup, etc. Subclasses may override this routine to
2470  /// provide different behavior.
2471  ExprResult RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
2472  SourceLocation OpLoc,
2473  Expr *Callee,
2474  Expr *First,
2475  Expr *Second);
2476 
2477  /// Build a new C++ "named" cast expression, such as static_cast or
2478  /// reinterpret_cast.
2479  ///
2480  /// By default, this routine dispatches to one of the more-specific routines
2481  /// for a particular named case, e.g., RebuildCXXStaticCastExpr().
2482  /// Subclasses may override this routine to provide different behavior.
2484  Stmt::StmtClass Class,
2485  SourceLocation LAngleLoc,
2486  TypeSourceInfo *TInfo,
2487  SourceLocation RAngleLoc,
2488  SourceLocation LParenLoc,
2489  Expr *SubExpr,
2490  SourceLocation RParenLoc) {
2491  switch (Class) {
2492  case Stmt::CXXStaticCastExprClass:
2493  return getDerived().RebuildCXXStaticCastExpr(OpLoc, LAngleLoc, TInfo,
2494  RAngleLoc, LParenLoc,
2495  SubExpr, RParenLoc);
2496 
2497  case Stmt::CXXDynamicCastExprClass:
2498  return getDerived().RebuildCXXDynamicCastExpr(OpLoc, LAngleLoc, TInfo,
2499  RAngleLoc, LParenLoc,
2500  SubExpr, RParenLoc);
2501 
2502  case Stmt::CXXReinterpretCastExprClass:
2503  return getDerived().RebuildCXXReinterpretCastExpr(OpLoc, LAngleLoc, TInfo,
2504  RAngleLoc, LParenLoc,
2505  SubExpr,
2506  RParenLoc);
2507 
2508  case Stmt::CXXConstCastExprClass:
2509  return getDerived().RebuildCXXConstCastExpr(OpLoc, LAngleLoc, TInfo,
2510  RAngleLoc, LParenLoc,
2511  SubExpr, RParenLoc);
2512 
2513  default:
2514  llvm_unreachable("Invalid C++ named cast");
2515  }
2516  }
2517 
2518  /// Build a new C++ static_cast expression.
2519  ///
2520  /// By default, performs semantic analysis to build the new expression.
2521  /// Subclasses may override this routine to provide different behavior.
2523  SourceLocation LAngleLoc,
2524  TypeSourceInfo *TInfo,
2525  SourceLocation RAngleLoc,
2526  SourceLocation LParenLoc,
2527  Expr *SubExpr,
2528  SourceLocation RParenLoc) {
2529  return getSema().BuildCXXNamedCast(OpLoc, tok::kw_static_cast,
2530  TInfo, SubExpr,
2531  SourceRange(LAngleLoc, RAngleLoc),
2532  SourceRange(LParenLoc, RParenLoc));
2533  }
2534 
2535  /// Build a new C++ dynamic_cast expression.
2536  ///
2537  /// By default, performs semantic analysis to build the new expression.
2538  /// Subclasses may override this routine to provide different behavior.
2540  SourceLocation LAngleLoc,
2541  TypeSourceInfo *TInfo,
2542  SourceLocation RAngleLoc,
2543  SourceLocation LParenLoc,
2544  Expr *SubExpr,
2545  SourceLocation RParenLoc) {
2546  return getSema().BuildCXXNamedCast(OpLoc, tok::kw_dynamic_cast,
2547  TInfo, SubExpr,
2548  SourceRange(LAngleLoc, RAngleLoc),
2549  SourceRange(LParenLoc, RParenLoc));
2550  }
2551 
2552  /// Build a new C++ reinterpret_cast expression.
2553  ///
2554  /// By default, performs semantic analysis to build the new expression.
2555  /// Subclasses may override this routine to provide different behavior.
2557  SourceLocation LAngleLoc,
2558  TypeSourceInfo *TInfo,
2559  SourceLocation RAngleLoc,
2560  SourceLocation LParenLoc,
2561  Expr *SubExpr,
2562  SourceLocation RParenLoc) {
2563  return getSema().BuildCXXNamedCast(OpLoc, tok::kw_reinterpret_cast,
2564  TInfo, SubExpr,
2565  SourceRange(LAngleLoc, RAngleLoc),
2566  SourceRange(LParenLoc, RParenLoc));
2567  }
2568 
2569  /// Build a new C++ const_cast expression.
2570  ///
2571  /// By default, performs semantic analysis to build the new expression.
2572  /// Subclasses may override this routine to provide different behavior.
2574  SourceLocation LAngleLoc,
2575  TypeSourceInfo *TInfo,
2576  SourceLocation RAngleLoc,
2577  SourceLocation LParenLoc,
2578  Expr *SubExpr,
2579  SourceLocation RParenLoc) {
2580  return getSema().BuildCXXNamedCast(OpLoc, tok::kw_const_cast,
2581  TInfo, SubExpr,
2582  SourceRange(LAngleLoc, RAngleLoc),
2583  SourceRange(LParenLoc, RParenLoc));
2584  }
2585 
2586  /// Build a new C++ functional-style cast expression.
2587  ///
2588  /// By default, performs semantic analysis to build the new expression.
2589  /// Subclasses may override this routine to provide different behavior.
2591  SourceLocation LParenLoc,
2592  Expr *Sub,
2593  SourceLocation RParenLoc,
2594  bool ListInitialization) {
2595  return getSema().BuildCXXTypeConstructExpr(TInfo, LParenLoc,
2596  MultiExprArg(&Sub, 1), RParenLoc,
2597  ListInitialization);
2598  }
2599 
2600  /// Build a new C++ typeid(type) expression.
2601  ///
2602  /// By default, performs semantic analysis to build the new expression.
2603  /// Subclasses may override this routine to provide different behavior.
2605  SourceLocation TypeidLoc,
2606  TypeSourceInfo *Operand,
2607  SourceLocation RParenLoc) {
2608  return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
2609  RParenLoc);
2610  }
2611 
2612 
2613  /// Build a new C++ typeid(expr) expression.
2614  ///
2615  /// By default, performs semantic analysis to build the new expression.
2616  /// Subclasses may override this routine to provide different behavior.
2618  SourceLocation TypeidLoc,
2619  Expr *Operand,
2620  SourceLocation RParenLoc) {
2621  return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
2622  RParenLoc);
2623  }
2624 
2625  /// Build a new C++ __uuidof(type) expression.
2626  ///
2627  /// By default, performs semantic analysis to build the new expression.
2628  /// Subclasses may override this routine to provide different behavior.
2630  SourceLocation TypeidLoc,
2631  TypeSourceInfo *Operand,
2632  SourceLocation RParenLoc) {
2633  return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
2634  RParenLoc);
2635  }
2636 
2637  /// Build a new C++ __uuidof(expr) expression.
2638  ///
2639  /// By default, performs semantic analysis to build the new expression.
2640  /// Subclasses may override this routine to provide different behavior.
2642  SourceLocation TypeidLoc,
2643  Expr *Operand,
2644  SourceLocation RParenLoc) {
2645  return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
2646  RParenLoc);
2647  }
2648 
2649  /// Build a new C++ "this" expression.
2650  ///
2651  /// By default, builds a new "this" expression without performing any
2652  /// semantic analysis. Subclasses may override this routine to provide
2653  /// different behavior.
2655  QualType ThisType,
2656  bool isImplicit) {
2657  getSema().CheckCXXThisCapture(ThisLoc);
2658  return new (getSema().Context) CXXThisExpr(ThisLoc, ThisType, isImplicit);
2659  }
2660 
2661  /// Build a new C++ throw expression.
2662  ///
2663  /// By default, performs semantic analysis to build the new expression.
2664  /// Subclasses may override this routine to provide different behavior.
2666  bool IsThrownVariableInScope) {
2667  return getSema().BuildCXXThrow(ThrowLoc, Sub, IsThrownVariableInScope);
2668  }
2669 
2670  /// Build a new C++ default-argument expression.
2671  ///
2672  /// By default, builds a new default-argument expression, which does not
2673  /// require any semantic analysis. Subclasses may override this routine to
2674  /// provide different behavior.
2676  ParmVarDecl *Param) {
2677  return CXXDefaultArgExpr::Create(getSema().Context, Loc, Param);
2678  }
2679 
2680  /// Build a new C++11 default-initialization expression.
2681  ///
2682  /// By default, builds a new default field initialization expression, which
2683  /// does not require any semantic analysis. Subclasses may override this
2684  /// routine to provide different behavior.
2686  FieldDecl *Field) {
2687  return CXXDefaultInitExpr::Create(getSema().Context, Loc, Field);
2688  }
2689 
2690  /// Build a new C++ zero-initialization expression.
2691  ///
2692  /// By default, performs semantic analysis to build the new expression.
2693  /// Subclasses may override this routine to provide different behavior.
2695  SourceLocation LParenLoc,
2696  SourceLocation RParenLoc) {
2697  return getSema().BuildCXXTypeConstructExpr(
2698  TSInfo, LParenLoc, None, RParenLoc, /*ListInitialization=*/false);
2699  }
2700 
2701  /// Build a new C++ "new" expression.
2702  ///
2703  /// By default, performs semantic analysis to build the new expression.
2704  /// Subclasses may override this routine to provide different behavior.
2706  bool UseGlobal,
2707  SourceLocation PlacementLParen,
2708  MultiExprArg PlacementArgs,
2709  SourceLocation PlacementRParen,
2710  SourceRange TypeIdParens,
2711  QualType AllocatedType,
2712  TypeSourceInfo *AllocatedTypeInfo,
2713  Expr *ArraySize,
2714  SourceRange DirectInitRange,
2715  Expr *Initializer) {
2716  return getSema().BuildCXXNew(StartLoc, UseGlobal,
2717  PlacementLParen,
2718  PlacementArgs,
2719  PlacementRParen,
2720  TypeIdParens,
2721  AllocatedType,
2722  AllocatedTypeInfo,
2723  ArraySize,
2724  DirectInitRange,
2725  Initializer);
2726  }
2727 
2728  /// Build a new C++ "delete" expression.
2729  ///
2730  /// By default, performs semantic analysis to build the new expression.
2731  /// Subclasses may override this routine to provide different behavior.
2733  bool IsGlobalDelete,
2734  bool IsArrayForm,
2735  Expr *Operand) {
2736  return getSema().ActOnCXXDelete(StartLoc, IsGlobalDelete, IsArrayForm,
2737  Operand);
2738  }
2739 
2740  /// Build a new type trait expression.
2741  ///
2742  /// By default, performs semantic analysis to build the new expression.
2743  /// Subclasses may override this routine to provide different behavior.
2745  SourceLocation StartLoc,
2747  SourceLocation RParenLoc) {
2748  return getSema().BuildTypeTrait(Trait, StartLoc, Args, RParenLoc);
2749  }
2750 
2751  /// Build a new array type trait expression.
2752  ///
2753  /// By default, performs semantic analysis to build the new expression.
2754  /// Subclasses may override this routine to provide different behavior.
2756  SourceLocation StartLoc,
2757  TypeSourceInfo *TSInfo,
2758  Expr *DimExpr,
2759  SourceLocation RParenLoc) {
2760  return getSema().BuildArrayTypeTrait(Trait, StartLoc, TSInfo, DimExpr, RParenLoc);
2761  }
2762 
2763  /// Build a new expression trait expression.
2764  ///
2765  /// By default, performs semantic analysis to build the new expression.
2766  /// Subclasses may override this routine to provide different behavior.
2768  SourceLocation StartLoc,
2769  Expr *Queried,
2770  SourceLocation RParenLoc) {
2771  return getSema().BuildExpressionTrait(Trait, StartLoc, Queried, RParenLoc);
2772  }
2773 
2774  /// Build a new (previously unresolved) declaration reference
2775  /// expression.
2776  ///
2777  /// By default, performs semantic analysis to build the new expression.
2778  /// Subclasses may override this routine to provide different behavior.
2780  NestedNameSpecifierLoc QualifierLoc,
2781  SourceLocation TemplateKWLoc,
2782  const DeclarationNameInfo &NameInfo,
2783  const TemplateArgumentListInfo *TemplateArgs,
2784  bool IsAddressOfOperand,
2785  TypeSourceInfo **RecoveryTSI) {
2786  CXXScopeSpec SS;
2787  SS.Adopt(QualifierLoc);
2788 
2789  if (TemplateArgs || TemplateKWLoc.isValid())
2790  return getSema().BuildQualifiedTemplateIdExpr(SS, TemplateKWLoc, NameInfo,
2791  TemplateArgs);
2792 
2793  return getSema().BuildQualifiedDeclarationNameExpr(
2794  SS, NameInfo, IsAddressOfOperand, /*S*/nullptr, RecoveryTSI);
2795  }
2796 
2797  /// Build a new template-id expression.
2798  ///
2799  /// By default, performs semantic analysis to build the new expression.
2800  /// Subclasses may override this routine to provide different behavior.
2802  SourceLocation TemplateKWLoc,
2803  LookupResult &R,
2804  bool RequiresADL,
2805  const TemplateArgumentListInfo *TemplateArgs) {
2806  return getSema().BuildTemplateIdExpr(SS, TemplateKWLoc, R, RequiresADL,
2807  TemplateArgs);
2808  }
2809 
2810  /// Build a new object-construction expression.
2811  ///
2812  /// By default, performs semantic analysis to build the new expression.
2813  /// Subclasses may override this routine to provide different behavior.
2815  SourceLocation Loc,
2816  CXXConstructorDecl *Constructor,
2817  bool IsElidable,
2818  MultiExprArg Args,
2819  bool HadMultipleCandidates,
2820  bool ListInitialization,
2821  bool StdInitListInitialization,
2822  bool RequiresZeroInit,
2823  CXXConstructExpr::ConstructionKind ConstructKind,
2824  SourceRange ParenRange) {
2825  SmallVector<Expr*, 8> ConvertedArgs;
2826  if (getSema().CompleteConstructorCall(Constructor, Args, Loc,
2827  ConvertedArgs))
2828  return ExprError();
2829 
2830  return getSema().BuildCXXConstructExpr(Loc, T, Constructor,
2831  IsElidable,
2832  ConvertedArgs,
2833  HadMultipleCandidates,
2834  ListInitialization,
2835  StdInitListInitialization,
2836  RequiresZeroInit, ConstructKind,
2837  ParenRange);
2838  }
2839 
2840  /// Build a new implicit construction via inherited constructor
2841  /// expression.
2843  CXXConstructorDecl *Constructor,
2844  bool ConstructsVBase,
2845  bool InheritedFromVBase) {
2846  return new (getSema().Context) CXXInheritedCtorInitExpr(
2847  Loc, T, Constructor, ConstructsVBase, InheritedFromVBase);
2848  }
2849 
2850  /// Build a new object-construction expression.
2851  ///
2852  /// By default, performs semantic analysis to build the new expression.
2853  /// Subclasses may override this routine to provide different behavior.
2855  SourceLocation LParenOrBraceLoc,
2856  MultiExprArg Args,
2857  SourceLocation RParenOrBraceLoc,
2858  bool ListInitialization) {
2859  return getSema().BuildCXXTypeConstructExpr(
2860  TSInfo, LParenOrBraceLoc, Args, RParenOrBraceLoc, ListInitialization);
2861  }
2862 
2863  /// Build a new object-construction expression.
2864  ///
2865  /// By default, performs semantic analysis to build the new expression.
2866  /// Subclasses may override this routine to provide different behavior.
2868  SourceLocation LParenLoc,
2869  MultiExprArg Args,
2870  SourceLocation RParenLoc,
2871  bool ListInitialization) {
2872  return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc, Args,
2873  RParenLoc, ListInitialization);
2874  }
2875 
2876  /// Build a new member reference expression.
2877  ///
2878  /// By default, performs semantic analysis to build the new expression.
2879  /// Subclasses may override this routine to provide different behavior.
2881  QualType BaseType,
2882  bool IsArrow,
2883  SourceLocation OperatorLoc,
2884  NestedNameSpecifierLoc QualifierLoc,
2885  SourceLocation TemplateKWLoc,
2886  NamedDecl *FirstQualifierInScope,
2887  const DeclarationNameInfo &MemberNameInfo,
2888  const TemplateArgumentListInfo *TemplateArgs) {
2889  CXXScopeSpec SS;
2890  SS.Adopt(QualifierLoc);
2891 
2892  return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
2893  OperatorLoc, IsArrow,
2894  SS, TemplateKWLoc,
2895  FirstQualifierInScope,
2896  MemberNameInfo,
2897  TemplateArgs, /*S*/nullptr);
2898  }
2899 
2900  /// Build a new member reference expression.
2901  ///
2902  /// By default, performs semantic analysis to build the new expression.
2903  /// Subclasses may override this routine to provide different behavior.
2905  SourceLocation OperatorLoc,
2906  bool IsArrow,
2907  NestedNameSpecifierLoc QualifierLoc,
2908  SourceLocation TemplateKWLoc,
2909  NamedDecl *FirstQualifierInScope,
2910  LookupResult &R,
2911  const TemplateArgumentListInfo *TemplateArgs) {
2912  CXXScopeSpec SS;
2913  SS.Adopt(QualifierLoc);
2914 
2915  return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
2916  OperatorLoc, IsArrow,
2917  SS, TemplateKWLoc,
2918  FirstQualifierInScope,
2919  R, TemplateArgs, /*S*/nullptr);
2920  }
2921 
2922  /// Build a new noexcept expression.
2923  ///
2924  /// By default, performs semantic analysis to build the new expression.
2925  /// Subclasses may override this routine to provide different behavior.
2927  return SemaRef.BuildCXXNoexceptExpr(Range.getBegin(), Arg, Range.getEnd());
2928  }
2929 
2930  /// Build a new expression to compute the length of a parameter pack.
2932  NamedDecl *Pack,
2933  SourceLocation PackLoc,
2934  SourceLocation RParenLoc,
2935  Optional<unsigned> Length,
2936  ArrayRef<TemplateArgument> PartialArgs) {
2937  return SizeOfPackExpr::Create(SemaRef.Context, OperatorLoc, Pack, PackLoc,
2938  RParenLoc, Length, PartialArgs);
2939  }
2940 
2941  /// Build a new Objective-C boxed expression.
2942  ///
2943  /// By default, performs semantic analysis to build the new expression.
2944  /// Subclasses may override this routine to provide different behavior.
2946  return getSema().BuildObjCBoxedExpr(SR, ValueExpr);
2947  }
2948 
2949  /// Build a new Objective-C array literal.
2950  ///
2951  /// By default, performs semantic analysis to build the new expression.
2952  /// Subclasses may override this routine to provide different behavior.
2954  Expr **Elements, unsigned NumElements) {
2955  return getSema().BuildObjCArrayLiteral(Range,
2956  MultiExprArg(Elements, NumElements));
2957  }
2958 
2960  Expr *Base, Expr *Key,
2961  ObjCMethodDecl *getterMethod,
2962  ObjCMethodDecl *setterMethod) {
2963  return getSema().BuildObjCSubscriptExpression(RB, Base, Key,
2964  getterMethod, setterMethod);
2965  }
2966 
2967  /// Build a new Objective-C dictionary literal.
2968  ///
2969  /// By default, performs semantic analysis to build the new expression.
2970  /// Subclasses may override this routine to provide different behavior.
2973  return getSema().BuildObjCDictionaryLiteral(Range, Elements);
2974  }
2975 
2976  /// Build a new Objective-C \@encode expression.
2977  ///
2978  /// By default, performs semantic analysis to build the new expression.
2979  /// Subclasses may override this routine to provide different behavior.
2981  TypeSourceInfo *EncodeTypeInfo,
2982  SourceLocation RParenLoc) {
2983  return SemaRef.BuildObjCEncodeExpression(AtLoc, EncodeTypeInfo, RParenLoc);
2984  }
2985 
2986  /// Build a new Objective-C class message.
2988  Selector Sel,
2989  ArrayRef<SourceLocation> SelectorLocs,
2990  ObjCMethodDecl *Method,
2991  SourceLocation LBracLoc,
2992  MultiExprArg Args,
2993  SourceLocation RBracLoc) {
2994  return SemaRef.BuildClassMessage(ReceiverTypeInfo,
2995  ReceiverTypeInfo->getType(),
2996  /*SuperLoc=*/SourceLocation(),
2997  Sel, Method, LBracLoc, SelectorLocs,
2998  RBracLoc, Args);
2999  }
3000 
3001  /// Build a new Objective-C instance message.
3003  Selector Sel,
3004  ArrayRef<SourceLocation> SelectorLocs,
3005  ObjCMethodDecl *Method,
3006  SourceLocation LBracLoc,
3007  MultiExprArg Args,
3008  SourceLocation RBracLoc) {
3009  return SemaRef.BuildInstanceMessage(Receiver,
3010  Receiver->getType(),
3011  /*SuperLoc=*/SourceLocation(),
3012  Sel, Method, LBracLoc, SelectorLocs,
3013  RBracLoc, Args);
3014  }
3015 
3016  /// Build a new Objective-C instance/class message to 'super'.
3018  Selector Sel,
3019  ArrayRef<SourceLocation> SelectorLocs,
3020  QualType SuperType,
3021  ObjCMethodDecl *Method,
3022  SourceLocation LBracLoc,
3023  MultiExprArg Args,
3024  SourceLocation RBracLoc) {
3025  return Method->isInstanceMethod() ? SemaRef.BuildInstanceMessage(nullptr,
3026  SuperType,
3027  SuperLoc,
3028  Sel, Method, LBracLoc, SelectorLocs,
3029  RBracLoc, Args)
3030  : SemaRef.BuildClassMessage(nullptr,
3031  SuperType,
3032  SuperLoc,
3033  Sel, Method, LBracLoc, SelectorLocs,
3034  RBracLoc, Args);
3035 
3036 
3037  }
3038 
3039  /// Build a new Objective-C ivar reference expression.
3040  ///
3041  /// By default, performs semantic analysis to build the new expression.
3042  /// Subclasses may override this routine to provide different behavior.
3044  SourceLocation IvarLoc,
3045  bool IsArrow, bool IsFreeIvar) {
3046  CXXScopeSpec SS;
3047  DeclarationNameInfo NameInfo(Ivar->getDeclName(), IvarLoc);
3048  ExprResult Result = getSema().BuildMemberReferenceExpr(
3049  BaseArg, BaseArg->getType(),
3050  /*FIXME:*/ IvarLoc, IsArrow, SS, SourceLocation(),
3051  /*FirstQualifierInScope=*/nullptr, NameInfo,
3052  /*TemplateArgs=*/nullptr,
3053  /*S=*/nullptr);
3054  if (IsFreeIvar && Result.isUsable())
3055  cast<ObjCIvarRefExpr>(Result.get())->setIsFreeIvar(IsFreeIvar);
3056  return Result;
3057  }
3058 
3059  /// Build a new Objective-C property reference expression.
3060  ///
3061  /// By default, performs semantic analysis to build the new expression.
3062  /// Subclasses may override this routine to provide different behavior.
3064  ObjCPropertyDecl *Property,
3065  SourceLocation PropertyLoc) {
3066  CXXScopeSpec SS;
3067  DeclarationNameInfo NameInfo(Property->getDeclName(), PropertyLoc);
3068  return getSema().BuildMemberReferenceExpr(BaseArg, BaseArg->getType(),
3069  /*FIXME:*/PropertyLoc,
3070  /*IsArrow=*/false,
3071  SS, SourceLocation(),
3072  /*FirstQualifierInScope=*/nullptr,
3073  NameInfo,
3074  /*TemplateArgs=*/nullptr,
3075  /*S=*/nullptr);
3076  }
3077 
3078  /// Build a new Objective-C property reference expression.
3079  ///
3080  /// By default, performs semantic analysis to build the new expression.
3081  /// Subclasses may override this routine to provide different behavior.
3083  ObjCMethodDecl *Getter,
3084  ObjCMethodDecl *Setter,
3085  SourceLocation PropertyLoc) {
3086  // Since these expressions can only be value-dependent, we do not
3087  // need to perform semantic analysis again.
3088  return Owned(
3089  new (getSema().Context) ObjCPropertyRefExpr(Getter, Setter, T,
3091  PropertyLoc, Base));
3092  }
3093 
3094  /// Build a new Objective-C "isa" expression.
3095  ///
3096  /// By default, performs semantic analysis to build the new expression.
3097  /// Subclasses may override this routine to provide different behavior.
3099  SourceLocation OpLoc, bool IsArrow) {
3100  CXXScopeSpec SS;
3101  DeclarationNameInfo NameInfo(&getSema().Context.Idents.get("isa"), IsaLoc);
3102  return getSema().BuildMemberReferenceExpr(BaseArg, BaseArg->getType(),
3103  OpLoc, IsArrow,
3104  SS, SourceLocation(),
3105  /*FirstQualifierInScope=*/nullptr,
3106  NameInfo,
3107  /*TemplateArgs=*/nullptr,
3108  /*S=*/nullptr);
3109  }
3110 
3111  /// Build a new shuffle vector expression.
3112  ///
3113  /// By default, performs semantic analysis to build the new expression.
3114  /// Subclasses may override this routine to provide different behavior.
3116  MultiExprArg SubExprs,
3117  SourceLocation RParenLoc) {
3118  // Find the declaration for __builtin_shufflevector
3119  const IdentifierInfo &Name
3120  = SemaRef.Context.Idents.get("__builtin_shufflevector");
3122  DeclContext::lookup_result Lookup = TUDecl->lookup(DeclarationName(&Name));
3123  assert(!Lookup.empty() && "No __builtin_shufflevector?");
3124 
3125  // Build a reference to the __builtin_shufflevector builtin
3126  FunctionDecl *Builtin = cast<FunctionDecl>(Lookup.front());
3127  Expr *Callee = new (SemaRef.Context) DeclRefExpr(Builtin, false,
3128  SemaRef.Context.BuiltinFnTy,
3129  VK_RValue, BuiltinLoc);
3130  QualType CalleePtrTy = SemaRef.Context.getPointerType(Builtin->getType());
3131  Callee = SemaRef.ImpCastExprToType(Callee, CalleePtrTy,
3132  CK_BuiltinFnToFnPtr).get();
3133 
3134  // Build the CallExpr
3135  ExprResult TheCall = new (SemaRef.Context) CallExpr(
3136  SemaRef.Context, Callee, SubExprs, Builtin->getCallResultType(),
3137  Expr::getValueKindForType(Builtin->getReturnType()), RParenLoc);
3138 
3139  // Type-check the __builtin_shufflevector expression.
3140  return SemaRef.SemaBuiltinShuffleVector(cast<CallExpr>(TheCall.get()));
3141  }
3142 
3143  /// Build a new convert vector expression.
3145  Expr *SrcExpr, TypeSourceInfo *DstTInfo,
3146  SourceLocation RParenLoc) {
3147  return SemaRef.SemaConvertVectorExpr(SrcExpr, DstTInfo,
3148  BuiltinLoc, RParenLoc);
3149  }
3150 
3151  /// Build a new template argument pack expansion.
3152  ///
3153  /// By default, performs semantic analysis to build a new pack expansion
3154  /// for a template argument. Subclasses may override this routine to provide
3155  /// different behavior.
3157  SourceLocation EllipsisLoc,
3158  Optional<unsigned> NumExpansions) {
3159  switch (Pattern.getArgument().getKind()) {
3161  ExprResult Result
3162  = getSema().CheckPackExpansion(Pattern.getSourceExpression(),
3163  EllipsisLoc, NumExpansions);
3164  if (Result.isInvalid())
3165  return TemplateArgumentLoc();
3166 
3167  return TemplateArgumentLoc(Result.get(), Result.get());
3168  }
3169 
3172  Pattern.getArgument().getAsTemplate(),
3173  NumExpansions),
3174  Pattern.getTemplateQualifierLoc(),
3175  Pattern.getTemplateNameLoc(),
3176  EllipsisLoc);
3177 
3184  llvm_unreachable("Pack expansion pattern has no parameter packs");
3185 
3187  if (TypeSourceInfo *Expansion
3188  = getSema().CheckPackExpansion(Pattern.getTypeSourceInfo(),
3189  EllipsisLoc,
3190  NumExpansions))
3191  return TemplateArgumentLoc(TemplateArgument(Expansion->getType()),
3192  Expansion);
3193  break;
3194  }
3195 
3196  return TemplateArgumentLoc();
3197  }
3198 
3199  /// Build a new expression pack expansion.
3200  ///
3201  /// By default, performs semantic analysis to build a new pack expansion
3202  /// for an expression. Subclasses may override this routine to provide
3203  /// different behavior.
3205  Optional<unsigned> NumExpansions) {
3206  return getSema().CheckPackExpansion(Pattern, EllipsisLoc, NumExpansions);
3207  }
3208 
3209  /// Build a new C++1z fold-expression.
3210  ///
3211  /// By default, performs semantic analysis in order to build a new fold
3212  /// expression.
3214  BinaryOperatorKind Operator,
3215  SourceLocation EllipsisLoc, Expr *RHS,
3216  SourceLocation RParenLoc) {
3217  return getSema().BuildCXXFoldExpr(LParenLoc, LHS, Operator, EllipsisLoc,
3218  RHS, RParenLoc);
3219  }
3220 
3221  /// Build an empty C++1z fold-expression with the given operator.
3222  ///
3223  /// By default, produces the fallback value for the fold-expression, or
3224  /// produce an error if there is no fallback value.
3226  BinaryOperatorKind Operator) {
3227  return getSema().BuildEmptyCXXFoldExpr(EllipsisLoc, Operator);
3228  }
3229 
3230  /// Build a new atomic operation expression.
3231  ///
3232  /// By default, performs semantic analysis to build the new expression.
3233  /// Subclasses may override this routine to provide different behavior.
3235  MultiExprArg SubExprs,
3236  QualType RetTy,
3238  SourceLocation RParenLoc) {
3239  // Just create the expression; there is not any interesting semantic
3240  // analysis here because we can't actually build an AtomicExpr until
3241  // we are sure it is semantically sound.
3242  return new (SemaRef.Context) AtomicExpr(BuiltinLoc, SubExprs, RetTy, Op,
3243  RParenLoc);
3244  }
3245 
3246 private:
3247  TypeLoc TransformTypeInObjectScope(TypeLoc TL,
3248  QualType ObjectType,
3249  NamedDecl *FirstQualifierInScope,
3250  CXXScopeSpec &SS);
3251 
3252  TypeSourceInfo *TransformTypeInObjectScope(TypeSourceInfo *TSInfo,
3253  QualType ObjectType,
3254  NamedDecl *FirstQualifierInScope,
3255  CXXScopeSpec &SS);
3256 
3257  TypeSourceInfo *TransformTSIInObjectScope(TypeLoc TL, QualType ObjectType,
3258  NamedDecl *FirstQualifierInScope,
3259  CXXScopeSpec &SS);
3260 
3261  QualType TransformDependentNameType(TypeLocBuilder &TLB,
3263  bool DeducibleTSTContext);
3264 };
3265 
3266 template<typename Derived>
3268  if (!S)
3269  return S;
3270 
3271  switch (S->getStmtClass()) {
3272  case Stmt::NoStmtClass: break;
3273 
3274  // Transform individual statement nodes
3275 #define STMT(Node, Parent) \
3276  case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(S));
3277 #define ABSTRACT_STMT(Node)
3278 #define EXPR(Node, Parent)
3279 #include "clang/AST/StmtNodes.inc"
3280 
3281  // Transform expressions by calling TransformExpr.
3282 #define STMT(Node, Parent)
3283 #define ABSTRACT_STMT(Stmt)
3284 #define EXPR(Node, Parent) case Stmt::Node##Class:
3285 #include "clang/AST/StmtNodes.inc"
3286  {
3287  ExprResult E = getDerived().TransformExpr(cast<Expr>(S));
3288  if (E.isInvalid())
3289  return StmtError();
3290 
3291  return getSema().ActOnExprStmt(E);
3292  }
3293  }
3294 
3295  return S;
3296 }
3297 
3298 template<typename Derived>
3300  if (!S)
3301  return S;
3302 
3303  switch (S->getClauseKind()) {
3304  default: break;
3305  // Transform individual clause nodes
3306 #define OPENMP_CLAUSE(Name, Class) \
3307  case OMPC_ ## Name : \
3308  return getDerived().Transform ## Class(cast<Class>(S));
3309 #include "clang/Basic/OpenMPKinds.def"
3310  }
3311 
3312  return S;
3313 }
3314 
3315 
3316 template<typename Derived>
3318  if (!E)
3319  return E;
3320 
3321  switch (E->getStmtClass()) {
3322  case Stmt::NoStmtClass: break;
3323 #define STMT(Node, Parent) case Stmt::Node##Class: break;
3324 #define ABSTRACT_STMT(Stmt)
3325 #define EXPR(Node, Parent) \
3326  case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(E));
3327 #include "clang/AST/StmtNodes.inc"
3328  }
3329 
3330  return E;
3331 }
3332 
3333 template<typename Derived>
3335  bool NotCopyInit) {
3336  // Initializers are instantiated like expressions, except that various outer
3337  // layers are stripped.
3338  if (!Init)
3339  return Init;
3340 
3341  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
3342  Init = ExprTemp->getSubExpr();
3343 
3344  if (auto *AIL = dyn_cast<ArrayInitLoopExpr>(Init))
3345  Init = AIL->getCommonExpr();
3346 
3347  if (MaterializeTemporaryExpr *MTE = dyn_cast<MaterializeTemporaryExpr>(Init))
3348  Init = MTE->GetTemporaryExpr();
3349 
3350  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
3351  Init = Binder->getSubExpr();
3352 
3353  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
3354  Init = ICE->getSubExprAsWritten();
3355 
3356  if (CXXStdInitializerListExpr *ILE =
3357  dyn_cast<CXXStdInitializerListExpr>(Init))
3358  return TransformInitializer(ILE->getSubExpr(), NotCopyInit);
3359 
3360  // If this is copy-initialization, we only need to reconstruct
3361  // InitListExprs. Other forms of copy-initialization will be a no-op if
3362  // the initializer is already the right type.
3363  CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init);
3364  if (!NotCopyInit && !(Construct && Construct->isListInitialization()))
3365  return getDerived().TransformExpr(Init);
3366 
3367  // Revert value-initialization back to empty parens.
3368  if (CXXScalarValueInitExpr *VIE = dyn_cast<CXXScalarValueInitExpr>(Init)) {
3369  SourceRange Parens = VIE->getSourceRange();
3370  return getDerived().RebuildParenListExpr(Parens.getBegin(), None,
3371  Parens.getEnd());
3372  }
3373 
3374  // FIXME: We shouldn't build ImplicitValueInitExprs for direct-initialization.
3375  if (isa<ImplicitValueInitExpr>(Init))
3376  return getDerived().RebuildParenListExpr(SourceLocation(), None,
3377  SourceLocation());
3378 
3379  // Revert initialization by constructor back to a parenthesized or braced list
3380  // of expressions. Any other form of initializer can just be reused directly.
3381  if (!Construct || isa<CXXTemporaryObjectExpr>(Construct))
3382  return getDerived().TransformExpr(Init);
3383 
3384  // If the initialization implicitly converted an initializer list to a
3385  // std::initializer_list object, unwrap the std::initializer_list too.
3386  if (Construct && Construct->isStdInitListInitialization())
3387  return TransformInitializer(Construct->getArg(0), NotCopyInit);
3388 
3389  SmallVector<Expr*, 8> NewArgs;
3390  bool ArgChanged = false;
3391  if (getDerived().TransformExprs(Construct->getArgs(), Construct->getNumArgs(),
3392  /*IsCall*/true, NewArgs, &ArgChanged))
3393  return ExprError();
3394 
3395  // If this was list initialization, revert to syntactic list form.
3396  if (Construct->isListInitialization())
3397  return getDerived().RebuildInitList(Construct->getBeginLoc(), NewArgs,
3398  Construct->getEndLoc());
3399 
3400  // Build a ParenListExpr to represent anything else.
3401  SourceRange Parens = Construct->getParenOrBraceRange();
3402  if (Parens.isInvalid()) {
3403  // This was a variable declaration's initialization for which no initializer
3404  // was specified.
3405  assert(NewArgs.empty() &&
3406  "no parens or braces but have direct init with arguments?");
3407  return ExprEmpty();
3408  }
3409  return getDerived().RebuildParenListExpr(Parens.getBegin(), NewArgs,
3410  Parens.getEnd());
3411 }
3412 
3413 template<typename Derived>
3415  unsigned NumInputs,
3416  bool IsCall,
3417  SmallVectorImpl<Expr *> &Outputs,
3418  bool *ArgChanged) {
3419  for (unsigned I = 0; I != NumInputs; ++I) {
3420  // If requested, drop call arguments that need to be dropped.
3421  if (IsCall && getDerived().DropCallArgument(Inputs[I])) {
3422  if (ArgChanged)
3423  *ArgChanged = true;
3424 
3425  break;
3426  }
3427 
3428  if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(Inputs[I])) {
3429  Expr *Pattern = Expansion->getPattern();
3430 
3432  getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
3433  assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
3434 
3435  // Determine whether the set of unexpanded parameter packs can and should
3436  // be expanded.
3437  bool Expand = true;
3438  bool RetainExpansion = false;
3439  Optional<unsigned> OrigNumExpansions = Expansion->getNumExpansions();
3440  Optional<unsigned> NumExpansions = OrigNumExpansions;
3441  if (getDerived().TryExpandParameterPacks(Expansion->getEllipsisLoc(),
3442  Pattern->getSourceRange(),
3443  Unexpanded,
3444  Expand, RetainExpansion,
3445  NumExpansions))
3446  return true;
3447 
3448  if (!Expand) {
3449  // The transform has determined that we should perform a simple
3450  // transformation on the pack expansion, producing another pack
3451  // expansion.
3452  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
3453  ExprResult OutPattern = getDerived().TransformExpr(Pattern);
3454  if (OutPattern.isInvalid())
3455  return true;
3456 
3457  ExprResult Out = getDerived().RebuildPackExpansion(OutPattern.get(),
3458  Expansion->getEllipsisLoc(),
3459  NumExpansions);
3460  if (Out.isInvalid())
3461  return true;
3462 
3463  if (ArgChanged)
3464  *ArgChanged = true;
3465  Outputs.push_back(Out.get());
3466  continue;
3467  }
3468 
3469  // Record right away that the argument was changed. This needs
3470  // to happen even if the array expands to nothing.
3471  if (ArgChanged) *ArgChanged = true;
3472 
3473  // The transform has determined that we should perform an elementwise
3474  // expansion of the pattern. Do so.
3475  for (unsigned I = 0; I != *NumExpansions; ++I) {
3476  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
3477  ExprResult Out = getDerived().TransformExpr(Pattern);
3478  if (Out.isInvalid())
3479  return true;
3480 
3481  if (Out.get()->containsUnexpandedParameterPack()) {
3482  Out = getDerived().RebuildPackExpansion(
3483  Out.get(), Expansion->getEllipsisLoc(), OrigNumExpansions);
3484  if (Out.isInvalid())
3485  return true;
3486  }
3487 
3488  Outputs.push_back(Out.get());
3489  }
3490 
3491  // If we're supposed to retain a pack expansion, do so by temporarily
3492  // forgetting the partially-substituted parameter pack.
3493  if (RetainExpansion) {
3494  ForgetPartiallySubstitutedPackRAII Forget(getDerived());
3495 
3496  ExprResult Out = getDerived().TransformExpr(Pattern);
3497  if (Out.isInvalid())
3498  return true;
3499 
3500  Out = getDerived().RebuildPackExpansion(
3501  Out.get(), Expansion->getEllipsisLoc(), OrigNumExpansions);
3502  if (Out.isInvalid())
3503  return true;
3504 
3505  Outputs.push_back(Out.get());
3506  }
3507 
3508  continue;
3509  }
3510 
3511  ExprResult Result =
3512  IsCall ? getDerived().TransformInitializer(Inputs[I], /*DirectInit*/false)
3513  : getDerived().TransformExpr(Inputs[I]);
3514  if (Result.isInvalid())
3515  return true;
3516 
3517  if (Result.get() != Inputs[I] && ArgChanged)
3518  *ArgChanged = true;
3519 
3520  Outputs.push_back(Result.get());
3521  }
3522 
3523  return false;
3524 }
3525 
3526 template <typename Derived>
3529  if (Var) {
3530  VarDecl *ConditionVar = cast_or_null<VarDecl>(
3531  getDerived().TransformDefinition(Var->getLocation(), Var));
3532 
3533  if (!ConditionVar)
3534  return Sema::ConditionError();
3535 
3536  return getSema().ActOnConditionVariable(ConditionVar, Loc, Kind);
3537  }
3538 
3539  if (Expr) {
3540  ExprResult CondExpr = getDerived().TransformExpr(Expr);
3541 
3542  if (CondExpr.isInvalid())
3543  return Sema::ConditionError();
3544 
3545  return getSema().ActOnCondition(nullptr, Loc, CondExpr.get(), Kind);
3546  }
3547 
3548  return Sema::ConditionResult();
3549 }
3550 
3551 template<typename Derived>
3555  QualType ObjectType,
3556  NamedDecl *FirstQualifierInScope) {
3558  for (NestedNameSpecifierLoc Qualifier = NNS; Qualifier;
3559  Qualifier = Qualifier.getPrefix())
3560  Qualifiers.push_back(Qualifier);
3561 
3562  CXXScopeSpec SS;
3563  while (!Qualifiers.empty()) {
3564  NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
3566 
3567  switch (QNNS->getKind()) {
3570  Q.getLocalBeginLoc(), Q.getLocalEndLoc(), ObjectType);
3571  if (SemaRef.BuildCXXNestedNameSpecifier(/*Scope=*/nullptr, IdInfo, false,
3572  SS, FirstQualifierInScope, false))
3573  return NestedNameSpecifierLoc();
3574  }
3575  break;
3576 
3578  NamespaceDecl *NS
3579  = cast_or_null<NamespaceDecl>(
3580  getDerived().TransformDecl(
3581  Q.getLocalBeginLoc(),
3582  QNNS->getAsNamespace()));
3583  SS.Extend(SemaRef.Context, NS, Q.getLocalBeginLoc(), Q.getLocalEndLoc());
3584  break;
3585  }
3586 
3588  NamespaceAliasDecl *Alias
3589  = cast_or_null<NamespaceAliasDecl>(
3590  getDerived().TransformDecl(Q.getLocalBeginLoc(),
3591  QNNS->getAsNamespaceAlias()));
3592  SS.Extend(SemaRef.Context, Alias, Q.getLocalBeginLoc(),
3593  Q.getLocalEndLoc());
3594  break;
3595  }
3596 
3598  // There is no meaningful transformation that one could perform on the
3599  // global scope.
3600  SS.MakeGlobal(SemaRef.Context, Q.getBeginLoc());
3601  break;
3602 
3604  CXXRecordDecl *RD =
3605  cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
3606  SourceLocation(), QNNS->getAsRecordDecl()));
3607  SS.MakeSuper(SemaRef.Context, RD, Q.getBeginLoc(), Q.getEndLoc());
3608  break;
3609  }
3610 
3613  TypeLoc TL = TransformTypeInObjectScope(Q.getTypeLoc(), ObjectType,
3614  FirstQualifierInScope, SS);
3615 
3616  if (!TL)
3617  return NestedNameSpecifierLoc();
3618 
3619  if (TL.getType()->isDependentType() || TL.getType()->isRecordType() ||
3620  (SemaRef.getLangOpts().CPlusPlus11 &&
3621  TL.getType()->isEnumeralType())) {
3622  assert(!TL.getType().hasLocalQualifiers() &&
3623  "Can't get cv-qualifiers here");
3624  if (TL.getType()->isEnumeralType())
3625  SemaRef.Diag(TL.getBeginLoc(),
3626  diag::warn_cxx98_compat_enum_nested_name_spec);
3627  SS.Extend(SemaRef.Context, /*FIXME:*/SourceLocation(), TL,
3628  Q.getLocalEndLoc());
3629  break;
3630  }
3631  // If the nested-name-specifier is an invalid type def, don't emit an
3632  // error because a previous error should have already been emitted.
3633  TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>();
3634  if (!TTL || !TTL.getTypedefNameDecl()->isInvalidDecl()) {
3635  SemaRef.Diag(TL.getBeginLoc(), diag::err_nested_name_spec_non_tag)
3636  << TL.getType() << SS.getRange();
3637  }
3638  return NestedNameSpecifierLoc();
3639  }
3640  }
3641 
3642  // The qualifier-in-scope and object type only apply to the leftmost entity.
3643  FirstQualifierInScope = nullptr;
3644  ObjectType = QualType();
3645  }
3646 
3647  // Don't rebuild the nested-name-specifier if we don't have to.
3648  if (SS.getScopeRep() == NNS.getNestedNameSpecifier() &&
3649  !getDerived().AlwaysRebuild())
3650  return NNS;
3651 
3652  // If we can re-use the source-location data from the original
3653  // nested-name-specifier, do so.
3654  if (SS.location_size() == NNS.getDataLength() &&
3655  memcmp(SS.location_data(), NNS.getOpaqueData(), SS.location_size()) == 0)
3656  return NestedNameSpecifierLoc(SS.getScopeRep(), NNS.getOpaqueData());
3657 
3658  // Allocate new nested-name-specifier location information.
3659  return SS.getWithLocInContext(SemaRef.Context);
3660 }
3661 
3662 template<typename Derived>
3666  DeclarationName Name = NameInfo.getName();
3667  if (!Name)
3668  return DeclarationNameInfo();
3669 
3670  switch (Name.getNameKind()) {
3678  return NameInfo;
3679 
3681  TemplateDecl *OldTemplate = Name.getCXXDeductionGuideTemplate();
3682  TemplateDecl *NewTemplate = cast_or_null<TemplateDecl>(
3683  getDerived().TransformDecl(NameInfo.getLoc(), OldTemplate));
3684  if (!NewTemplate)
3685  return DeclarationNameInfo();
3686 
3687  DeclarationNameInfo NewNameInfo(NameInfo);
3688  NewNameInfo.setName(
3689  SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(NewTemplate));
3690  return NewNameInfo;
3691  }
3692 
3696  TypeSourceInfo *NewTInfo;
3697  CanQualType NewCanTy;
3698  if (TypeSourceInfo *OldTInfo = NameInfo.getNamedTypeInfo()) {
3699  NewTInfo = getDerived().TransformType(OldTInfo);
3700  if (!NewTInfo)
3701  return DeclarationNameInfo();
3702  NewCanTy = SemaRef.Context.getCanonicalType(NewTInfo->getType());
3703  }
3704  else {
3705  NewTInfo = nullptr;
3706  TemporaryBase Rebase(*this, NameInfo.getLoc(), Name);
3707  QualType NewT = getDerived().TransformType(Name.getCXXNameType());
3708  if (NewT.isNull())
3709  return DeclarationNameInfo();
3710  NewCanTy = SemaRef.Context.getCanonicalType(NewT);
3711  }
3712 
3713  DeclarationName NewName
3714  = SemaRef.Context.DeclarationNames.getCXXSpecialName(Name.getNameKind(),
3715  NewCanTy);
3716  DeclarationNameInfo NewNameInfo(NameInfo);
3717  NewNameInfo.setName(NewName);
3718  NewNameInfo.setNamedTypeInfo(NewTInfo);
3719  return NewNameInfo;
3720  }
3721  }
3722 
3723  llvm_unreachable("Unknown name kind.");
3724 }
3725 
3726 template<typename Derived>
3729  TemplateName Name,
3730  SourceLocation NameLoc,
3731  QualType ObjectType,
3732  NamedDecl *FirstQualifierInScope,
3733  bool AllowInjectedClassName) {
3735  TemplateDecl *Template = QTN->getTemplateDecl();
3736  assert(Template && "qualified template name must refer to a template");
3737 
3738  TemplateDecl *TransTemplate
3739  = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
3740  Template));
3741  if (!TransTemplate)
3742  return TemplateName();
3743 
3744  if (!getDerived().AlwaysRebuild() &&
3745  SS.getScopeRep() == QTN->getQualifier() &&
3746  TransTemplate == Template)
3747  return Name;
3748 
3749  return getDerived().RebuildTemplateName(SS, QTN->hasTemplateKeyword(),
3750  TransTemplate);
3751  }
3752 
3754  if (SS.getScopeRep()) {
3755  // These apply to the scope specifier, not the template.
3756  ObjectType = QualType();
3757  FirstQualifierInScope = nullptr;
3758  }
3759 
3760  if (!getDerived().AlwaysRebuild() &&
3761  SS.getScopeRep() == DTN->getQualifier() &&
3762  ObjectType.isNull())
3763  return Name;
3764 
3765  // FIXME: Preserve the location of the "template" keyword.
3766  SourceLocation TemplateKWLoc = NameLoc;
3767 
3768  if (DTN->isIdentifier()) {
3769  return getDerived().RebuildTemplateName(SS,
3770  TemplateKWLoc,
3771  *DTN->getIdentifier(),
3772  NameLoc,
3773  ObjectType,
3774  FirstQualifierInScope,
3775  AllowInjectedClassName);
3776  }
3777 
3778  return getDerived().RebuildTemplateName(SS, TemplateKWLoc,
3779  DTN->getOperator(), NameLoc,
3780  ObjectType, AllowInjectedClassName);
3781  }
3782 
3783  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
3784  TemplateDecl *TransTemplate
3785  = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc,
3786  Template));
3787  if (!TransTemplate)
3788  return TemplateName();
3789 
3790  if (!getDerived().AlwaysRebuild() &&
3791  TransTemplate == Template)
3792  return Name;
3793 
3794  return TemplateName(TransTemplate);
3795  }
3796 
3799  TemplateTemplateParmDecl *TransParam
3800  = cast_or_null<TemplateTemplateParmDecl>(
3801  getDerived().TransformDecl(NameLoc, SubstPack->getParameterPack()));
3802  if (!TransParam)
3803  return TemplateName();
3804 
3805  if (!getDerived().AlwaysRebuild() &&
3806  TransParam == SubstPack->getParameterPack())
3807  return Name;
3808 
3809  return getDerived().RebuildTemplateName(TransParam,
3810  SubstPack->getArgumentPack());
3811  }
3812 
3813  // These should be getting filtered out before they reach the AST.
3814  llvm_unreachable("overloaded function decl survived to here");
3815 }
3816 
3817 template<typename Derived>
3819  const TemplateArgument &Arg,
3820  TemplateArgumentLoc &Output) {
3821  SourceLocation Loc = getDerived().getBaseLocation();
3822  switch (Arg.getKind()) {
3824  llvm_unreachable("null template argument in TreeTransform");
3825  break;
3826 
3828  Output = TemplateArgumentLoc(Arg,
3829  SemaRef.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
3830 
3831  break;
3832 
3837  if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
3838  Builder.MakeTrivial(SemaRef.Context, DTN->getQualifier(), Loc);
3839  else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
3840  Builder.MakeTrivial(SemaRef.Context, QTN->getQualifier(), Loc);
3841 
3842  if (Arg.getKind() == TemplateArgument::Template)
3843  Output = TemplateArgumentLoc(Arg,
3844  Builder.getWithLocInContext(SemaRef.Context),
3845  Loc);
3846  else
3847  Output = TemplateArgumentLoc(Arg,
3848  Builder.getWithLocInContext(SemaRef.Context),
3849  Loc, Loc);
3850 
3851  break;
3852  }
3853 
3855  Output = TemplateArgumentLoc(Arg, Arg.getAsExpr());
3856  break;
3857 
3863  break;
3864  }
3865 }
3866 
3867 template<typename Derived>
3869  const TemplateArgumentLoc &Input,
3870  TemplateArgumentLoc &Output, bool Uneval) {
3873  /*LambdaContextDecl=*/nullptr, /*ExprContext=*/
3875  const TemplateArgument &Arg = Input.getArgument();
3876  switch (Arg.getKind()) {
3882  llvm_unreachable("Unexpected TemplateArgument");
3883 
3884  case TemplateArgument::Type: {
3885  TypeSourceInfo *DI = Input.getTypeSourceInfo();
3886  if (!DI)
3887  DI = InventTypeSourceInfo(Input.getArgument().getAsType());
3888 
3889  DI = getDerived().TransformType(DI);
3890  if (!DI) return true;
3891 
3892  Output = TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
3893  return false;
3894  }
3895 
3897  NestedNameSpecifierLoc QualifierLoc = Input.getTemplateQualifierLoc();
3898  if (QualifierLoc) {
3899  QualifierLoc = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc);
3900  if (!QualifierLoc)
3901  return true;
3902  }
3903 
3904  CXXScopeSpec SS;
3905  SS.Adopt(QualifierLoc);
3906  TemplateName Template
3907  = getDerived().TransformTemplateName(SS, Arg.getAsTemplate(),
3908  Input.getTemplateNameLoc());
3909  if (Template.isNull())
3910  return true;
3911 
3912  Output = TemplateArgumentLoc(TemplateArgument(Template), QualifierLoc,
3913  Input.getTemplateNameLoc());
3914  return false;
3915  }
3916 
3918  llvm_unreachable("Caller should expand pack expansions");
3919 
3921  // Template argument expressions are constant expressions.
3923  getSema(), Uneval
3926 
3927  Expr *InputExpr = Input.getSourceExpression();
3928  if (!InputExpr) InputExpr = Input.getArgument().getAsExpr();
3929 
3930  ExprResult E = getDerived().TransformExpr(InputExpr);
3931  E = SemaRef.ActOnConstantExpression(E);
3932  if (E.isInvalid()) return true;
3933  Output = TemplateArgumentLoc(TemplateArgument(E.get()), E.get());
3934  return false;
3935  }
3936  }
3937 
3938  // Work around bogus GCC warning
3939  return true;
3940 }
3941 
3942 /// Iterator adaptor that invents template argument location information
3943 /// for each of the template arguments in its underlying iterator.
3944 template<typename Derived, typename InputIterator>
3946  TreeTransform<Derived> &Self;
3947  InputIterator Iter;
3948 
3949 public:
3952  typedef typename std::iterator_traits<InputIterator>::difference_type
3954  typedef std::input_iterator_tag iterator_category;
3955 
3956  class pointer {
3957  TemplateArgumentLoc Arg;
3958 
3959  public:
3960  explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
3961 
3962  const TemplateArgumentLoc *operator->() const { return &Arg; }
3963  };
3964 
3966 
3968  InputIterator Iter)
3969  : Self(Self), Iter(Iter) { }
3970 
3972  ++Iter;
3973  return *this;
3974  }
3975 
3978  ++(*this);
3979  return Old;
3980  }
3981 
3982  reference operator*() const {
3983  TemplateArgumentLoc Result;
3984  Self.InventTemplateArgumentLoc(*Iter, Result);
3985  return Result;
3986  }
3987 
3988  pointer operator->() const { return pointer(**this); }
3989 
3992  return X.Iter == Y.Iter;
3993  }
3994 
3997  return X.Iter != Y.Iter;
3998  }
3999 };
4000 
4001 template<typename Derived>
4002 template<typename InputIterator>
4004  InputIterator First, InputIterator Last, TemplateArgumentListInfo &Outputs,
4005  bool Uneval) {
4006  for (; First != Last; ++First) {
4007  TemplateArgumentLoc Out;
4008  TemplateArgumentLoc In = *First;
4009 
4010  if (In.getArgument().getKind() == TemplateArgument::Pack) {
4011  // Unpack argument packs, which we translate them into separate
4012  // arguments.
4013  // FIXME: We could do much better if we could guarantee that the
4014  // TemplateArgumentLocInfo for the pack expansion would be usable for
4015  // all of the template arguments in the argument pack.
4016  typedef TemplateArgumentLocInventIterator<Derived,
4018  PackLocIterator;
4019  if (TransformTemplateArguments(PackLocIterator(*this,
4020  In.getArgument().pack_begin()),
4021  PackLocIterator(*this,
4022  In.getArgument().pack_end()),
4023  Outputs, Uneval))
4024  return true;
4025 
4026  continue;
4027  }
4028 
4029  if (In.getArgument().isPackExpansion()) {
4030  // We have a pack expansion, for which we will be substituting into
4031  // the pattern.
4032  SourceLocation Ellipsis;
4033  Optional<unsigned> OrigNumExpansions;
4034  TemplateArgumentLoc Pattern
4035  = getSema().getTemplateArgumentPackExpansionPattern(
4036  In, Ellipsis, OrigNumExpansions);
4037 
4039  getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
4040  assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
4041 
4042  // Determine whether the set of unexpanded parameter packs can and should
4043  // be expanded.
4044  bool Expand = true;
4045  bool RetainExpansion = false;
4046  Optional<unsigned> NumExpansions = OrigNumExpansions;
4047  if (getDerived().TryExpandParameterPacks(Ellipsis,
4048  Pattern.getSourceRange(),
4049  Unexpanded,
4050  Expand,
4051  RetainExpansion,
4052  NumExpansions))
4053  return true;
4054 
4055  if (!Expand) {
4056  // The transform has determined that we should perform a simple
4057  // transformation on the pack expansion, producing another pack
4058  // expansion.
4059  TemplateArgumentLoc OutPattern;
4060  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
4061  if (getDerived().TransformTemplateArgument(Pattern, OutPattern, Uneval))
4062  return true;
4063 
4064  Out = getDerived().RebuildPackExpansion(OutPattern, Ellipsis,
4065  NumExpansions);
4066  if (Out.getArgument().isNull())
4067  return true;
4068 
4069  Outputs.addArgument(Out);
4070  continue;
4071  }
4072 
4073  // The transform has determined that we should perform an elementwise
4074  // expansion of the pattern. Do so.
4075  for (unsigned I = 0; I != *NumExpansions; ++I) {
4076  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
4077 
4078  if (getDerived().TransformTemplateArgument(Pattern, Out, Uneval))
4079  return true;
4080 
4082  Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
4083  OrigNumExpansions);
4084  if (Out.getArgument().isNull())
4085  return true;
4086  }
4087 
4088  Outputs.addArgument(Out);
4089  }
4090 
4091  // If we're supposed to retain a pack expansion, do so by temporarily
4092  // forgetting the partially-substituted parameter pack.
4093  if (RetainExpansion) {
4094  ForgetPartiallySubstitutedPackRAII Forget(getDerived());
4095 
4096  if (getDerived().TransformTemplateArgument(Pattern, Out, Uneval))
4097  return true;
4098 
4099  Out = getDerived().RebuildPackExpansion(Out, Ellipsis,
4100  OrigNumExpansions);
4101  if (Out.getArgument().isNull())
4102  return true;
4103 
4104  Outputs.addArgument(Out);
4105  }
4106 
4107  continue;
4108  }
4109 
4110  // The simple case:
4111  if (getDerived().TransformTemplateArgument(In, Out, Uneval))
4112  return true;
4113 
4114  Outputs.addArgument(Out);
4115  }
4116 
4117  return false;
4118 
4119 }
4120 
4121 //===----------------------------------------------------------------------===//
4122 // Type transformation
4123 //===----------------------------------------------------------------------===//
4124 
4125 template<typename Derived>
4127  if (getDerived().AlreadyTransformed(T))
4128  return T;
4129 
4130  // Temporary workaround. All of these transformations should
4131  // eventually turn into transformations on TypeLocs.
4132  TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T,
4133  getDerived().getBaseLocation());
4134 
4135  TypeSourceInfo *NewDI = getDerived().TransformType(DI);
4136 
4137  if (!NewDI)
4138  return QualType();
4139 
4140  return NewDI->getType();
4141 }
4142 
4143 template<typename Derived>
4145  // Refine the base location to the type's location.
4146  TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(),
4147  getDerived().getBaseEntity());
4148  if (getDerived().AlreadyTransformed(DI->getType()))
4149  return DI;
4150 
4151  TypeLocBuilder TLB;
4152 
4153  TypeLoc TL = DI->getTypeLoc();
4154  TLB.reserve(TL.getFullDataSize());
4155 
4156  QualType Result = getDerived().TransformType(TLB, TL);
4157  if (Result.isNull())
4158  return nullptr;
4159 
4160  return TLB.getTypeSourceInfo(SemaRef.Context, Result);
4161 }
4162 
4163 template<typename Derived>
4164 QualType
4166  switch (T.getTypeLocClass()) {
4167 #define ABSTRACT_TYPELOC(CLASS, PARENT)
4168 #define TYPELOC(CLASS, PARENT) \
4169  case TypeLoc::CLASS: \
4170  return getDerived().Transform##CLASS##Type(TLB, \
4171  T.castAs<CLASS##TypeLoc>());
4172 #include "clang/AST/TypeLocNodes.def"
4173  }
4174 
4175  llvm_unreachable("unhandled type loc!");
4176 }
4177 
4178 template<typename Derived>
4180  if (!isa<DependentNameType>(T))
4181  return TransformType(T);
4182 
4183  if (getDerived().AlreadyTransformed(T))
4184  return T;
4185  TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T,
4186  getDerived().getBaseLocation());
4187  TypeSourceInfo *NewDI = getDerived().TransformTypeWithDeducedTST(DI);
4188  return NewDI ? NewDI->getType() : QualType();
4189 }
4190 
4191 template<typename Derived>
4194  if (!isa<DependentNameType>(DI->getType()))
4195  return TransformType(DI);
4196 
4197  // Refine the base location to the type's location.
4198  TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(),
4199  getDerived().getBaseEntity());
4200  if (getDerived().AlreadyTransformed(DI->getType()))
4201  return DI;
4202 
4203  TypeLocBuilder TLB;
4204 
4205  TypeLoc TL = DI->getTypeLoc();
4206  TLB.reserve(TL.getFullDataSize());
4207 
4208  auto QTL = TL.getAs<QualifiedTypeLoc>();
4209  if (QTL)
4210  TL = QTL.getUnqualifiedLoc();
4211 
4212  auto DNTL = TL.castAs<DependentNameTypeLoc>();
4213 
4214  QualType Result = getDerived().TransformDependentNameType(
4215  TLB, DNTL, /*DeducedTSTContext*/true);
4216  if (Result.isNull())
4217  return nullptr;
4218 
4219  if (QTL) {
4220  Result = getDerived().RebuildQualifiedType(
4221  Result, QTL.getBeginLoc(), QTL.getType().getLocalQualifiers());
4222  TLB.TypeWasModifiedSafely(Result);
4223  }
4224 
4225  return TLB.getTypeSourceInfo(SemaRef.Context, Result);
4226 }
4227 
4228 template<typename Derived>
4229 QualType
4231  QualifiedTypeLoc T) {
4232  Qualifiers Quals = T.getType().getLocalQualifiers();
4233 
4234  QualType Result = getDerived().TransformType(TLB, T.getUnqualifiedLoc());
4235  if (Result.isNull())
4236  return QualType();
4237 
4238  Result = getDerived().RebuildQualifiedType(Result, T.getBeginLoc(), Quals);
4239 
4240  // RebuildQualifiedType might have updated the type, but not in a way
4241  // that invalidates the TypeLoc. (There's no location information for
4242  // qualifiers.)
4243  TLB.TypeWasModifiedSafely(Result);
4244 
4245  return Result;
4246 }
4247 
4248 template<typename Derived>
4250  SourceLocation Loc,
4251  Qualifiers Quals) {
4252  // C++ [dcl.fct]p7:
4253  // [When] adding cv-qualifications on top of the function type [...] the
4254  // cv-qualifiers are ignored.
4255  // C++ [dcl.ref]p1:
4256  // when the cv-qualifiers are introduced through the use of a typedef-name
4257  // or decltype-specifier [...] the cv-qualifiers are ignored.
4258  // Note that [dcl.ref]p1 lists all cases in which cv-qualifiers can be
4259  // applied to a reference type.
4260  // FIXME: This removes all qualifiers, not just cv-qualifiers!
4261  if (T->isFunctionType() || T->isReferenceType())
4262  return T;
4263 
4264  // Suppress Objective-C lifetime qualifiers if they don't make sense for the
4265  // resulting type.
4266  if (Quals.hasObjCLifetime()) {
4267  if (!T->isObjCLifetimeType() && !T->isDependentType())
4268  Quals.removeObjCLifetime();
4269  else if (T.getObjCLifetime()) {
4270  // Objective-C ARC:
4271  // A lifetime qualifier applied to a substituted template parameter
4272  // overrides the lifetime qualifier from the template argument.
4273  const AutoType *AutoTy;
4274  if (const SubstTemplateTypeParmType *SubstTypeParam
4275  = dyn_cast<SubstTemplateTypeParmType>(T)) {
4276  QualType Replacement = SubstTypeParam->getReplacementType();
4277  Qualifiers Qs = Replacement.getQualifiers();
4278  Qs.removeObjCLifetime();
4279  Replacement = SemaRef.Context.getQualifiedType(
4280  Replacement.getUnqualifiedType(), Qs);
4281  T = SemaRef.Context.getSubstTemplateTypeParmType(
4282  SubstTypeParam->getReplacedParameter(), Replacement);
4283  } else if ((AutoTy = dyn_cast<AutoType>(T)) && AutoTy->isDeduced()) {
4284  // 'auto' types behave the same way as template parameters.
4285  QualType Deduced = AutoTy->getDeducedType();
4286  Qualifiers Qs = Deduced.getQualifiers();
4287  Qs.removeObjCLifetime();
4288  Deduced =
4289  SemaRef.Context.getQualifiedType(Deduced.getUnqualifiedType(), Qs);
4290  T = SemaRef.Context.getAutoType(Deduced, AutoTy->getKeyword(),
4291  AutoTy->isDependentType());
4292  } else {
4293  // Otherwise, complain about the addition of a qualifier to an
4294  // already-qualified type.
4295  // FIXME: Why is this check not in Sema::BuildQualifiedType?
4296  SemaRef.Diag(Loc, diag::err_attr_objc_ownership_redundant) << T;
4297  Quals.removeObjCLifetime();
4298  }
4299  }
4300  }
4301 
4302  return SemaRef.BuildQualifiedType(T, Loc, Quals);
4303 }
4304 
4305 template<typename Derived>
4306 TypeLoc
4308  QualType ObjectType,
4309  NamedDecl *UnqualLookup,
4310  CXXScopeSpec &SS) {
4311  if (getDerived().AlreadyTransformed(TL.getType()))
4312  return TL;
4313 
4314  TypeSourceInfo *TSI =
4315  TransformTSIInObjectScope(TL, ObjectType, UnqualLookup, SS);
4316  if (TSI)
4317  return TSI->getTypeLoc();
4318  return TypeLoc();
4319 }
4320 
4321 template<typename Derived>
4324  QualType ObjectType,
4325  NamedDecl *UnqualLookup,
4326  CXXScopeSpec &SS) {
4327  if (getDerived().AlreadyTransformed(TSInfo->getType()))
4328  return TSInfo;
4329 
4330  return TransformTSIInObjectScope(TSInfo->getTypeLoc(), ObjectType,
4331  UnqualLookup, SS);
4332 }
4333 
4334 template <typename Derived>
4336  TypeLoc TL, QualType ObjectType, NamedDecl *UnqualLookup,
4337  CXXScopeSpec &SS) {
4338  QualType T = TL.getType();
4339  assert(!getDerived().AlreadyTransformed(T));
4340 
4341  TypeLocBuilder TLB;
4342  QualType Result;
4343 
4344  if (isa<TemplateSpecializationType>(T)) {
4347 
4348  TemplateName Template = getDerived().TransformTemplateName(
4349  SS, SpecTL.getTypePtr()->getTemplateName(), SpecTL.getTemplateNameLoc(),
4350  ObjectType, UnqualLookup, /*AllowInjectedClassName*/true);
4351  if (Template.isNull())
4352  return nullptr;
4353 
4354  Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL,
4355  Template);
4356  } else if (isa<DependentTemplateSpecializationType>(T)) {
4359 
4360  TemplateName Template
4361  = getDerived().RebuildTemplateName(SS,
4362  SpecTL.getTemplateKeywordLoc(),
4363  *SpecTL.getTypePtr()->getIdentifier(),
4364  SpecTL.getTemplateNameLoc(),
4365  ObjectType, UnqualLookup,
4366  /*AllowInjectedClassName*/true);
4367  if (Template.isNull())
4368  return nullptr;
4369 
4370  Result = getDerived().TransformDependentTemplateSpecializationType(TLB,
4371  SpecTL,
4372  Template,
4373  SS);
4374  } else {
4375  // Nothing special needs to be done for these.
4376  Result = getDerived().TransformType(TLB, TL);
4377  }
4378 
4379  if (Result.isNull())
4380  return nullptr;
4381 
4382  return TLB.getTypeSourceInfo(SemaRef.Context, Result);
4383 }
4384 
4385 template <class TyLoc> static inline
4387  TyLoc NewT = TLB.push<TyLoc>(T.getType());
4388  NewT.setNameLoc(T.getNameLoc());
4389  return T.getType();
4390 }
4391 
4392 template<typename Derived>
4394  BuiltinTypeLoc T) {
4395  BuiltinTypeLoc NewT = TLB.push<BuiltinTypeLoc>(T.getType());
4396  NewT.setBuiltinLoc(T.getBuiltinLoc());
4397  if (T.needsExtraLocalData())
4399  return T.getType();
4400 }
4401 
4402 template<typename Derived>
4404  ComplexTypeLoc T) {
4405  // FIXME: recurse?
4406  return TransformTypeSpecType(TLB, T);
4407 }
4408 
4409 template <typename Derived>
4411  AdjustedTypeLoc TL) {
4412  // Adjustments applied during transformation are handled elsewhere.
4413  return getDerived().TransformType(TLB, TL.getOriginalLoc());
4414 }
4415 
4416 template<typename Derived>
4418  DecayedTypeLoc TL) {
4419  QualType OriginalType = getDerived().TransformType(TLB, TL.getOriginalLoc());
4420  if (OriginalType.isNull())
4421  return QualType();
4422 
4423  QualType Result = TL.getType();
4424  if (getDerived().AlwaysRebuild() ||
4425  OriginalType != TL.getOriginalLoc().getType())
4426  Result = SemaRef.Context.getDecayedType(OriginalType);
4427  TLB.push<DecayedTypeLoc>(Result);
4428  // Nothing to set for DecayedTypeLoc.
4429  return Result;
4430 }
4431 
4432 template<typename Derived>
4434  PointerTypeLoc TL) {
4435  QualType PointeeType
4436  = getDerived().TransformType(TLB, TL.getPointeeLoc());
4437  if (PointeeType.isNull())
4438  return QualType();
4439 
4440  QualType Result = TL.getType();
4441  if (PointeeType->getAs<ObjCObjectType>()) {
4442  // A dependent pointer type 'T *' has is being transformed such
4443  // that an Objective-C class type is being replaced for 'T'. The
4444  // resulting pointer type is an ObjCObjectPointerType, not a
4445  // PointerType.
4446  Result = SemaRef.Context.getObjCObjectPointerType(PointeeType);
4447 
4449  NewT.setStarLoc(TL.getStarLoc());
4450  return Result;
4451  }
4452 
4453  if (getDerived().AlwaysRebuild() ||
4454  PointeeType != TL.getPointeeLoc().getType()) {
4455  Result = getDerived().RebuildPointerType(PointeeType, TL.getSigilLoc());
4456  if (Result.isNull())
4457  return QualType();
4458  }
4459 
4460  // Objective-C ARC can add lifetime qualifiers to the type that we're
4461  // pointing to.
4462  TLB.TypeWasModifiedSafely(Result->getPointeeType());
4463 
4464  PointerTypeLoc NewT = TLB.push<PointerTypeLoc>(Result);
4465  NewT.setSigilLoc(TL.getSigilLoc());
4466  return Result;
4467 }
4468 
4469 template<typename Derived>
4470 QualType
4472  BlockPointerTypeLoc TL) {
4473  QualType PointeeType
4474  = getDerived().TransformType(TLB, TL.getPointeeLoc());
4475  if (PointeeType.isNull())
4476  return QualType();
4477 
4478  QualType Result = TL.getType();
4479  if (getDerived().AlwaysRebuild() ||
4480  PointeeType != TL.getPointeeLoc().getType()) {
4481  Result = getDerived().RebuildBlockPointerType(PointeeType,
4482  TL.getSigilLoc());
4483  if (Result.isNull())
4484  return QualType();
4485  }
4486 
4487  BlockPointerTypeLoc NewT = TLB.push<BlockPointerTypeLoc>(Result);
4488  NewT.setSigilLoc(TL.getSigilLoc());
4489  return Result;
4490 }
4491 
4492 /// Transforms a reference type. Note that somewhat paradoxically we
4493 /// don't care whether the type itself is an l-value type or an r-value
4494 /// type; we only care if the type was *written* as an l-value type
4495 /// or an r-value type.
4496 template<typename Derived>
4497 QualType
4499  ReferenceTypeLoc TL) {
4500  const ReferenceType *T = TL.getTypePtr();
4501 
4502  // Note that this works with the pointee-as-written.
4503  QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
4504  if (PointeeType.isNull())
4505  return QualType();
4506 
4507  QualType Result = TL.getType();
4508  if (getDerived().AlwaysRebuild() ||
4509  PointeeType != T->getPointeeTypeAsWritten()) {
4510  Result = getDerived().RebuildReferenceType(PointeeType,
4511  T->isSpelledAsLValue(),
4512  TL.getSigilLoc());
4513  if (Result.isNull())
4514  return QualType();
4515  }
4516 
4517  // Objective-C ARC can add lifetime qualifiers to the type that we're
4518  // referring to.
4521 
4522  // r-value references can be rebuilt as l-value references.
4523  ReferenceTypeLoc NewTL;
4524  if (isa<LValueReferenceType>(Result))
4525  NewTL = TLB.push<LValueReferenceTypeLoc>(Result);
4526  else
4527  NewTL = TLB.push<RValueReferenceTypeLoc>(Result);
4528  NewTL.setSigilLoc(TL.getSigilLoc());
4529 
4530  return Result;
4531 }
4532 
4533 template<typename Derived>
4534 QualType
4537  return TransformReferenceType(TLB, TL);
4538 }
4539 
4540 template<typename Derived>
4541 QualType
4544  return TransformReferenceType(TLB, TL);
4545 }
4546 
4547 template<typename Derived>
4548 QualType
4550  MemberPointerTypeLoc TL) {
4551  QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
4552  if (PointeeType.isNull())
4553  return QualType();
4554 
4555  TypeSourceInfo* OldClsTInfo = TL.getClassTInfo();
4556  TypeSourceInfo *NewClsTInfo = nullptr;
4557  if (OldClsTInfo) {
4558  NewClsTInfo = getDerived().TransformType(OldClsTInfo);
4559  if (!NewClsTInfo)
4560  return QualType();
4561  }
4562 
4563  const MemberPointerType *T = TL.getTypePtr();
4564  QualType OldClsType = QualType(T->getClass(), 0);
4565  QualType NewClsType;
4566  if (NewClsTInfo)
4567  NewClsType = NewClsTInfo->getType();
4568  else {
4569  NewClsType = getDerived().TransformType(OldClsType);
4570  if (NewClsType.isNull())
4571  return QualType();
4572  }
4573 
4574  QualType Result = TL.getType();
4575  if (getDerived().AlwaysRebuild() ||
4576  PointeeType != T->getPointeeType() ||
4577  NewClsType != OldClsType) {
4578  Result = getDerived().RebuildMemberPointerType(PointeeType, NewClsType,
4579  TL.getStarLoc());
4580  if (Result.isNull())
4581  return QualType();
4582  }
4583 
4584  // If we had to adjust the pointee type when building a member pointer, make
4585  // sure to push TypeLoc info for it.
4586  const MemberPointerType *MPT = Result->getAs<MemberPointerType>();
4587  if (MPT && PointeeType != MPT->getPointeeType()) {
4588  assert(isa<AdjustedType>(MPT->getPointeeType()));
4589  TLB.push<AdjustedTypeLoc>(MPT->getPointeeType());
4590  }
4591 
4592  MemberPointerTypeLoc NewTL = TLB.push<MemberPointerTypeLoc>(Result);
4593  NewTL.setSigilLoc(TL.getSigilLoc());
4594  NewTL.setClassTInfo(NewClsTInfo);
4595 
4596  return Result;
4597 }
4598 
4599 template<typename Derived>
4600 QualType
4602  ConstantArrayTypeLoc TL) {
4603  const ConstantArrayType *T = TL.getTypePtr();
4604  QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
4605  if (ElementType.isNull())
4606  return QualType();
4607 
4608  QualType Result = TL.getType();
4609  if (getDerived().AlwaysRebuild() ||
4610  ElementType != T->getElementType()) {
4611  Result = getDerived().RebuildConstantArrayType(ElementType,
4612  T->getSizeModifier(),
4613  T->getSize(),
4615  TL.getBracketsRange());
4616  if (Result.isNull())
4617  return QualType();
4618  }
4619 
4620  // We might have either a ConstantArrayType or a VariableArrayType now:
4621  // a ConstantArrayType is allowed to have an element type which is a
4622  // VariableArrayType if the type is dependent. Fortunately, all array
4623  // types have the same location layout.
4624  ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
4625  NewTL.setLBracketLoc(TL.getLBracketLoc());
4626  NewTL.setRBracketLoc(TL.getRBracketLoc());
4627 
4628  Expr *Size = TL.getSizeExpr();
4629  if (Size) {
4632  Size = getDerived().TransformExpr(Size).template getAs<Expr>();
4633  Size = SemaRef.ActOnConstantExpression(Size).get();
4634  }
4635  NewTL.setSizeExpr(Size);
4636 
4637  return Result;
4638 }
4639 
4640 template<typename Derived>
4642  TypeLocBuilder &TLB,
4644  const IncompleteArrayType *T = TL.getTypePtr();
4645  QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
4646  if (ElementType.isNull())
4647  return QualType();
4648 
4649  QualType Result = TL.getType();
4650  if (getDerived().AlwaysRebuild() ||
4651  ElementType != T->getElementType()) {
4652  Result = getDerived().RebuildIncompleteArrayType(ElementType,
4653  T->getSizeModifier(),
4655  TL.getBracketsRange());
4656  if (Result.isNull())
4657  return QualType();
4658  }
4659 
4660  IncompleteArrayTypeLoc NewTL = TLB.push<IncompleteArrayTypeLoc>(Result);
4661  NewTL.setLBracketLoc(TL.getLBracketLoc());
4662  NewTL.setRBracketLoc(TL.getRBracketLoc());
4663  NewTL.setSizeExpr(nullptr);
4664 
4665  return Result;
4666 }
4667 
4668 template<typename Derived>
4669 QualType
4671  VariableArrayTypeLoc TL) {
4672  const VariableArrayType *T = TL.getTypePtr();
4673  QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
4674  if (ElementType.isNull())
4675  return QualType();
4676 
4677  ExprResult SizeResult;
4678  {
4681  SizeResult = getDerived().TransformExpr(T->getSizeExpr());
4682  }
4683  if (SizeResult.isInvalid())
4684  return QualType();
4685  SizeResult = SemaRef.ActOnFinishFullExpr(SizeResult.get());
4686  if (SizeResult.isInvalid())
4687  return QualType();
4688 
4689  Expr *Size = SizeResult.get();
4690 
4691  QualType Result = TL.getType();
4692  if (getDerived().AlwaysRebuild() ||
4693  ElementType != T->getElementType() ||
4694  Size != T->getSizeExpr()) {
4695  Result = getDerived().RebuildVariableArrayType(ElementType,
4696  T->getSizeModifier(),
4697  Size,
4699  TL.getBracketsRange());
4700  if (Result.isNull())
4701  return QualType();
4702  }
4703 
4704  // We might have constant size array now, but fortunately it has the same
4705  // location layout.
4706  ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
4707  NewTL.setLBracketLoc(TL.getLBracketLoc());
4708  NewTL.setRBracketLoc(TL.getRBracketLoc());
4709  NewTL.setSizeExpr(Size);
4710 
4711  return Result;
4712 }
4713 
4714 template<typename Derived>
4715 QualType
4718  const DependentSizedArrayType *T = TL.getTypePtr();
4719  QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
4720  if (ElementType.isNull())
4721  return QualType();
4722 
4723  // Array bounds are constant expressions.
4726 
4727  // Prefer the expression from the TypeLoc; the other may have been uniqued.
4728  Expr *origSize = TL.getSizeExpr();
4729  if (!origSize) origSize = T->getSizeExpr();
4730 
4731  ExprResult sizeResult
4732  = getDerived().TransformExpr(origSize);
4733  sizeResult = SemaRef.ActOnConstantExpression(sizeResult);
4734  if (sizeResult.isInvalid())
4735  return QualType();
4736 
4737  Expr *size = sizeResult.get();
4738 
4739  QualType Result = TL.getType();
4740  if (getDerived().AlwaysRebuild() ||
4741  ElementType != T->getElementType() ||
4742  size != origSize) {
4743  Result = getDerived().RebuildDependentSizedArrayType(ElementType,
4744  T->getSizeModifier(),
4745  size,
4747  TL.getBracketsRange());
4748  if (Result.isNull())
4749  return QualType();
4750  }
4751 
4752  // We might have any sort of array type now, but fortunately they
4753  // all have the same location layout.
4754  ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
4755  NewTL.setLBracketLoc(TL.getLBracketLoc());
4756  NewTL.setRBracketLoc(TL.getRBracketLoc());
4757  NewTL.setSizeExpr(size);
4758 
4759  return Result;
4760 }
4761 
4762 template <typename Derived>
4765  const DependentVectorType *T = TL.getTypePtr();
4766  QualType ElementType = getDerived().TransformType(T->getElementType());
4767  if (ElementType.isNull())
4768  return QualType();
4769 
4772 
4773  ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
4774  Size = SemaRef.ActOnConstantExpression(Size);
4775  if (Size.isInvalid())
4776  return QualType();
4777 
4778  QualType Result = TL.getType();
4779  if (getDerived().AlwaysRebuild() || ElementType != T->getElementType() ||
4780  Size.get() != T->getSizeExpr()) {
4781  Result = getDerived().RebuildDependentVectorType(
4782  ElementType, Size.get(), T->getAttributeLoc(), T->getVectorKind());
4783  if (Result.isNull())
4784  return QualType();
4785  }
4786 
4787  // Result might be dependent or not.
4788  if (isa<DependentVectorType>(Result)) {
4789  DependentVectorTypeLoc NewTL =
4790  TLB.push<DependentVectorTypeLoc>(Result);
4791  NewTL.setNameLoc(TL.getNameLoc());
4792  } else {
4793  VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result);
4794  NewTL.setNameLoc(TL.getNameLoc());
4795  }
4796 
4797  return Result;
4798 }
4799 
4800 template<typename Derived>
4802  TypeLocBuilder &TLB,
4804  const DependentSizedExtVectorType *T = TL.getTypePtr();
4805 
4806  // FIXME: ext vector locs should be nested
4807  QualType ElementType = getDerived().TransformType(T->getElementType());
4808  if (ElementType.isNull())
4809  return QualType();
4810 
4811  // Vector sizes are constant expressions.
4814 
4815  ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
4816  Size = SemaRef.ActOnConstantExpression(Size);
4817  if (Size.isInvalid())
4818  return QualType();
4819 
4820  QualType Result = TL.getType();
4821  if (getDerived().AlwaysRebuild() ||
4822  ElementType != T->getElementType() ||
4823  Size.get() != T->getSizeExpr()) {
4824  Result = getDerived().RebuildDependentSizedExtVectorType(ElementType,
4825  Size.get(),
4826  T->getAttributeLoc());
4827  if (Result.isNull())
4828  return QualType();
4829  }
4830 
4831  // Result might be dependent or not.
4832  if (isa<DependentSizedExtVectorType>(Result)) {
4834  = TLB.push<DependentSizedExtVectorTypeLoc>(Result);
4835  NewTL.setNameLoc(TL.getNameLoc());
4836  } else {
4837  ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
4838  NewTL.setNameLoc(TL.getNameLoc());
4839  }
4840 
4841  return Result;
4842 }
4843 
4844 template <typename Derived>
4847  const DependentAddressSpaceType *T = TL.getTypePtr();
4848 
4849  QualType pointeeType = getDerived().TransformType(T->getPointeeType());
4850 
4851  if (pointeeType.isNull())
4852  return QualType();
4853 
4854  // Address spaces are constant expressions.
4857 
4858  ExprResult AddrSpace = getDerived().TransformExpr(T->getAddrSpaceExpr());
4859  AddrSpace = SemaRef.ActOnConstantExpression(AddrSpace);
4860  if (AddrSpace.isInvalid())
4861  return QualType();
4862 
4863  QualType Result = TL.getType();
4864  if (getDerived().AlwaysRebuild() || pointeeType != T->getPointeeType() ||
4865  AddrSpace.get() != T->getAddrSpaceExpr()) {
4866  Result = getDerived().RebuildDependentAddressSpaceType(
4867  pointeeType, AddrSpace.get(), T->getAttributeLoc());
4868  if (Result.isNull())
4869  return QualType();
4870  }
4871 
4872  // Result might be dependent or not.
4873  if (isa<DependentAddressSpaceType>(Result)) {
4875  TLB.push<DependentAddressSpaceTypeLoc>(Result);
4876 
4879  NewTL.setAttrNameLoc(TL.getAttrNameLoc());
4880 
4881  } else {
4882  TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(
4883  Result, getDerived().getBaseLocation());
4884  TransformType(TLB, DI->getTypeLoc());
4885  }
4886 
4887  return Result;
4888 }
4889 
4890 template <typename Derived>
4892  VectorTypeLoc TL) {
4893  const VectorType *T = TL.getTypePtr();
4894  QualType ElementType = getDerived().TransformType(T->getElementType());
4895  if (ElementType.isNull())
4896  return QualType();
4897 
4898  QualType Result = TL.getType();
4899  if (getDerived().AlwaysRebuild() ||
4900  ElementType != T->getElementType()) {
4901  Result = getDerived().RebuildVectorType(ElementType, T->getNumElements(),
4902  T->getVectorKind());
4903  if (Result.isNull())
4904  return QualType();
4905  }
4906 
4907  VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result);
4908  NewTL.setNameLoc(TL.getNameLoc());
4909 
4910  return Result;
4911 }
4912 
4913 template<typename Derived>
4915  ExtVectorTypeLoc TL) {
4916  const VectorType *T = TL.getTypePtr();
4917  QualType ElementType = getDerived().TransformType(T->getElementType());
4918  if (ElementType.isNull())
4919  return QualType();
4920 
4921  QualType Result = TL.getType();
4922  if (getDerived().AlwaysRebuild() ||
4923  ElementType != T->getElementType()) {
4924  Result = getDerived().RebuildExtVectorType(ElementType,
4925  T->getNumElements(),
4926  /*FIXME*/ SourceLocation());
4927  if (Result.isNull())
4928  return QualType();
4929  }
4930 
4931  ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
4932  NewTL.setNameLoc(TL.getNameLoc());
4933 
4934  return Result;
4935 }
4936 
4937 template <typename Derived>
4939  ParmVarDecl *OldParm, int indexAdjustment, Optional<unsigned> NumExpansions,
4940  bool ExpectParameterPack) {
4941  TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
4942  TypeSourceInfo *NewDI = nullptr;
4943 
4944  if (NumExpansions && isa<PackExpansionType>(OldDI->getType())) {
4945  // If we're substituting into a pack expansion type and we know the
4946  // length we want to expand to, just substitute for the pattern.
4947  TypeLoc OldTL = OldDI->getTypeLoc();
4948  PackExpansionTypeLoc OldExpansionTL = OldTL.castAs<PackExpansionTypeLoc>();
4949 
4950  TypeLocBuilder TLB;
4951  TypeLoc NewTL = OldDI->getTypeLoc();
4952  TLB.reserve(NewTL.getFullDataSize());
4953 
4954  QualType Result = getDerived().TransformType(TLB,
4955  OldExpansionTL.getPatternLoc());
4956  if (Result.isNull())
4957  return nullptr;
4958 
4959  Result = RebuildPackExpansionType(Result,
4960  OldExpansionTL.getPatternLoc().getSourceRange(),
4961  OldExpansionTL.getEllipsisLoc(),
4962  NumExpansions);
4963  if (Result.isNull())
4964  return nullptr;
4965 
4966  PackExpansionTypeLoc NewExpansionTL
4967  = TLB.push<PackExpansionTypeLoc>(Result);
4968  NewExpansionTL.setEllipsisLoc(OldExpansionTL.getEllipsisLoc());
4969  NewDI = TLB.getTypeSourceInfo(SemaRef.Context, Result);
4970  } else
4971  NewDI = getDerived().TransformType(OldDI);
4972  if (!NewDI)
4973  return nullptr;
4974 
4975  if (NewDI == OldDI && indexAdjustment == 0)
4976  return OldParm;
4977 
4978  ParmVarDecl *newParm = ParmVarDecl::Create(SemaRef.Context,
4979  OldParm->getDeclContext(),
4980  OldParm->getInnerLocStart(),
4981  OldParm->getLocation(),
4982  OldParm->getIdentifier(),
4983  NewDI->getType(),
4984  NewDI,
4985  OldParm->getStorageClass(),
4986  /* DefArg */ nullptr);
4987  newParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
4988  OldParm->getFunctionScopeIndex() + indexAdjustment);
4989  return newParm;
4990 }
4991 
4992 template <typename Derived>
4995  const QualType *ParamTypes,
4996  const FunctionProtoType::ExtParameterInfo *ParamInfos,
4997  SmallVectorImpl<QualType> &OutParamTypes,
5000  int indexAdjustment = 0;
5001 
5002  unsigned NumParams = Params.size();
5003  for (unsigned i = 0; i != NumParams; ++i) {
5004  if (ParmVarDecl *OldParm = Params[i]) {
5005  assert(OldParm->getFunctionScopeIndex() == i);
5006 
5007  Optional<unsigned> NumExpansions;
5008  ParmVarDecl *NewParm = nullptr;
5009  if (OldParm->isParameterPack()) {
5010  // We have a function parameter pack that may need to be expanded.
5012 
5013  // Find the parameter packs that could be expanded.
5014  TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc();
5015  PackExpansionTypeLoc ExpansionTL = TL.castAs<PackExpansionTypeLoc>();
5016  TypeLoc Pattern = ExpansionTL.getPatternLoc();
5017  SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
5018  assert(Unexpanded.size() > 0 && "Could not find parameter packs!");
5019 
5020  // Determine whether we should expand the parameter packs.
5021  bool ShouldExpand = false;
5022  bool RetainExpansion = false;
5023  Optional<unsigned> OrigNumExpansions =
5024  ExpansionTL.getTypePtr()->getNumExpansions();
5025  NumExpansions = OrigNumExpansions;
5026  if (getDerived().TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(),
5027  Pattern.getSourceRange(),
5028  Unexpanded,
5029  ShouldExpand,
5030  RetainExpansion,
5031  NumExpansions)) {
5032  return true;
5033  }
5034 
5035  if (ShouldExpand) {
5036  // Expand the function parameter pack into multiple, separate
5037  // parameters.
5038  getDerived().ExpandingFunctionParameterPack(OldParm);
5039  for (unsigned I = 0; I != *NumExpansions; ++I) {
5040  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
5041  ParmVarDecl *NewParm
5042  = getDerived().TransformFunctionTypeParam(OldParm,
5043  indexAdjustment++,
5044  OrigNumExpansions,
5045  /*ExpectParameterPack=*/false);
5046  if (!NewParm)
5047  return true;
5048 
5049  if (ParamInfos)
5050  PInfos.set(OutParamTypes.size(), ParamInfos[i]);
5051  OutParamTypes.push_back(NewParm->getType());
5052  if (PVars)
5053  PVars->push_back(NewParm);
5054  }
5055 
5056  // If we're supposed to retain a pack expansion, do so by temporarily
5057  // forgetting the partially-substituted parameter pack.
5058  if (RetainExpansion) {
5059  ForgetPartiallySubstitutedPackRAII Forget(getDerived());
5060  ParmVarDecl *NewParm
5061  = getDerived().TransformFunctionTypeParam(OldParm,
5062  indexAdjustment++,
5063  OrigNumExpansions,
5064  /*ExpectParameterPack=*/false);
5065  if (!NewParm)
5066  return true;
5067 
5068  if (ParamInfos)
5069  PInfos.set(OutParamTypes.size(), ParamInfos[i]);
5070  OutParamTypes.push_back(NewParm->getType());
5071  if (PVars)
5072  PVars->push_back(NewParm);
5073  }
5074 
5075  // The next parameter should have the same adjustment as the
5076  // last thing we pushed, but we post-incremented indexAdjustment
5077  // on every push. Also, if we push nothing, the adjustment should
5078  // go down by one.
5079  indexAdjustment--;
5080 
5081  // We're done with the pack expansion.
5082  continue;
5083  }
5084 
5085  // We'll substitute the parameter now without expanding the pack
5086  // expansion.
5087  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
5088  NewParm = getDerived().TransformFunctionTypeParam(OldParm,
5089  indexAdjustment,
5090  NumExpansions,
5091  /*ExpectParameterPack=*/true);
5092  } else {
5093  NewParm = getDerived().TransformFunctionTypeParam(
5094  OldParm, indexAdjustment, None, /*ExpectParameterPack=*/ false);
5095  }
5096 
5097  if (!NewParm)
5098  return true;
5099 
5100  if (ParamInfos)
5101  PInfos.set(OutParamTypes.size(), ParamInfos[i]);
5102  OutParamTypes.push_back(NewParm->getType());
5103  if (PVars)
5104  PVars->push_back(NewParm);
5105  continue;
5106  }
5107 
5108  // Deal with the possibility that we don't have a parameter
5109  // declaration for this parameter.
5110  QualType OldType = ParamTypes[i];
5111  bool IsPackExpansion = false;
5112  Optional<unsigned> NumExpansions;
5113  QualType NewType;
5114  if (const PackExpansionType *Expansion
5115  = dyn_cast<PackExpansionType>(OldType)) {
5116  // We have a function parameter pack that may need to be expanded.
5117  QualType Pattern = Expansion->getPattern();
5119  getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
5120 
5121  // Determine whether we should expand the parameter packs.
5122  bool ShouldExpand = false;
5123  bool RetainExpansion = false;
5124  if (getDerived().TryExpandParameterPacks(Loc, SourceRange(),
5125  Unexpanded,
5126  ShouldExpand,
5127  RetainExpansion,
5128  NumExpansions)) {
5129  return true;
5130  }
5131 
5132  if (ShouldExpand) {
5133  // Expand the function parameter pack into multiple, separate
5134  // parameters.
5135  for (unsigned I = 0; I != *NumExpansions; ++I) {
5136  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
5137  QualType NewType = getDerived().TransformType(Pattern);
5138  if (NewType.isNull())
5139  return true;
5140 
5141  if (NewType->containsUnexpandedParameterPack()) {
5142  NewType =
5143  getSema().getASTContext().getPackExpansionType(NewType, None);
5144 
5145  if (NewType.isNull())
5146  return true;
5147  }
5148 
5149  if (ParamInfos)
5150  PInfos.set(OutParamTypes.size(), ParamInfos[i]);
5151  OutParamTypes.push_back(NewType);
5152  if (PVars)
5153  PVars->push_back(nullptr);
5154  }
5155 
5156  // We're done with the pack expansion.
5157  continue;
5158  }
5159 
5160  // If we're supposed to retain a pack expansion, do so by temporarily
5161  // forgetting the partially-substituted parameter pack.
5162  if (RetainExpansion) {
5163  ForgetPartiallySubstitutedPackRAII Forget(getDerived());
5164  QualType NewType = getDerived().TransformType(Pattern);
5165  if (NewType.isNull())
5166  return true;
5167 
5168  if (ParamInfos)
5169  PInfos.set(OutParamTypes.size(), ParamInfos[i]);
5170  OutParamTypes.push_back(NewType);
5171  if (PVars)
5172  PVars->push_back(nullptr);
5173  }
5174 
5175  // We'll substitute the parameter now without expanding the pack
5176  // expansion.
5177  OldType = Expansion->getPattern();
5178  IsPackExpansion = true;
5179  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
5180  NewType = getDerived().TransformType(OldType);
5181  } else {
5182  NewType = getDerived().TransformType(OldType);
5183  }
5184 
5185  if (NewType.isNull())
5186  return true;
5187 
5188  if (IsPackExpansion)
5189  NewType = getSema().Context.getPackExpansionType(NewType,
5190  NumExpansions);
5191 
5192  if (ParamInfos)
5193  PInfos.set(OutParamTypes.size(), ParamInfos[i]);
5194  OutParamTypes.push_back(NewType);
5195  if (PVars)
5196  PVars->push_back(nullptr);
5197  }
5198 
5199 #ifndef NDEBUG
5200  if (PVars) {
5201  for (unsigned i = 0, e = PVars->size(); i != e; ++i)
5202  if (ParmVarDecl *parm = (*PVars)[i])
5203  assert(parm->getFunctionScopeIndex() == i);
5204  }
5205 #endif
5206 
5207  return false;
5208 }
5209 
5210 template<typename Derived>
5211 QualType
5213  FunctionProtoTypeLoc TL) {
5214  SmallVector<QualType, 4> ExceptionStorage;
5215  TreeTransform *This = this; // Work around gcc.gnu.org/PR56135.
5216  return getDerived().TransformFunctionProtoType(
5217  TLB, TL, nullptr, 0,
5218  [&](FunctionProtoType::ExceptionSpecInfo &ESI, bool &Changed) {
5219  return This->TransformExceptionSpec(TL.getBeginLoc(), ESI,
5220  ExceptionStorage, Changed);
5221  });
5222 }
5223 
5224 template<typename Derived> template<typename Fn>
5226  TypeLocBuilder &TLB, FunctionProtoTypeLoc TL, CXXRecordDecl *ThisContext,
5227  unsigned ThisTypeQuals, Fn TransformExceptionSpec) {
5228 
5229  // Transform the parameters and return type.
5230  //
5231  // We are required to instantiate the params and return type in source order.
5232  // When the function has a trailing return type, we instantiate the
5233  // parameters before the return type, since the return type can then refer
5234  // to the parameters themselves (via decltype, sizeof, etc.).
5235  //
5236  SmallVector<QualType, 4> ParamTypes;
5237  SmallVector<ParmVarDecl*, 4> ParamDecls;
5238  Sema::ExtParameterInfoBuilder ExtParamInfos;
5239  const FunctionProtoType *T = TL.getTypePtr();
5240 
5241  QualType ResultType;
5242 
5243  if (T->hasTrailingReturn()) {
5244  if (getDerived().TransformFunctionTypeParams(
5245  TL.getBeginLoc(), TL.getParams(),
5246  TL.getTypePtr()->param_type_begin(),
5248  ParamTypes, &ParamDecls, ExtParamInfos))
5249  return QualType();
5250 
5251  {
5252  // C++11 [expr.prim.general]p3:
5253  // If a declaration declares a member function or member function
5254  // template of a class X, the expression this is a prvalue of type
5255  // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
5256  // and the end of the function-definition, member-declarator, or
5257  // declarator.
5258  Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals);
5259 
5260  ResultType = getDerived().TransformType(TLB, TL.getReturnLoc());
5261  if (ResultType.isNull())
5262  return QualType();
5263  }
5264  }
5265  else {
5266  ResultType = getDerived().TransformType(TLB, TL.getReturnLoc());
5267  if (ResultType.isNull())
5268  return QualType();
5269 
5270  if (getDerived().TransformFunctionTypeParams(
5271  TL.getBeginLoc(), TL.getParams(),
5272  TL.getTypePtr()->param_type_begin(),
5274  ParamTypes, &ParamDecls, ExtParamInfos))
5275  return QualType();
5276  }
5277 
5279 
5280  bool EPIChanged = false;
5281  if (TransformExceptionSpec(EPI.ExceptionSpec, EPIChanged))
5282  return QualType();
5283 
5284  // Handle extended parameter information.
5285  if (auto NewExtParamInfos =
5286  ExtParamInfos.getPointerOrNull(ParamTypes.size())) {
5287  if (!EPI.ExtParameterInfos ||
5288  llvm::makeArrayRef(EPI.ExtParameterInfos, TL.getNumParams())
5289  != llvm::makeArrayRef(NewExtParamInfos, ParamTypes.size())) {
5290  EPIChanged = true;
5291  }
5292  EPI.ExtParameterInfos = NewExtParamInfos;
5293  } else if (EPI.ExtParameterInfos) {
5294  EPIChanged = true;
5295  EPI.ExtParameterInfos = nullptr;
5296  }
5297 
5298  QualType Result = TL.getType();
5299  if (getDerived().AlwaysRebuild() || ResultType != T->getReturnType() ||
5300  T->getParamTypes() != llvm::makeArrayRef(ParamTypes) || EPIChanged) {
5301  Result = getDerived().RebuildFunctionProtoType(ResultType, ParamTypes, EPI);
5302  if (Result.isNull())
5303  return QualType();
5304  }
5305 
5306  FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result);
5308  NewTL.setLParenLoc(TL.getLParenLoc());
5309  NewTL.setRParenLoc(TL.getRParenLoc());
5311  NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
5312  for (unsigned i = 0, e = NewTL.getNumParams(); i != e; ++i)
5313  NewTL.setParam(i, ParamDecls[i]);
5314 
5315  return Result;
5316 }
5317 
5318 template<typename Derived>
5321  SmallVectorImpl<QualType> &Exceptions, bool &Changed) {
5322  assert(ESI.Type != EST_Uninstantiated && ESI.Type != EST_Unevaluated);
5323 
5324  // Instantiate a dynamic noexcept expression, if any.
5325  if (isComputedNoexcept(ESI.Type)) {
5328  ExprResult NoexceptExpr = getDerived().TransformExpr(ESI.NoexceptExpr);
5329  if (NoexceptExpr.isInvalid())
5330  return true;
5331 
5332  ExceptionSpecificationType EST = ESI.Type;
5333  NoexceptExpr =
5334  getSema().ActOnNoexceptSpec(Loc, NoexceptExpr.get(), EST);
5335  if (NoexceptExpr.isInvalid())
5336  return true;
5337 
5338  if (ESI.NoexceptExpr != NoexceptExpr.get() || EST != ESI.Type)
5339  Changed = true;
5340  ESI.NoexceptExpr = NoexceptExpr.get();
5341  ESI.Type = EST;
5342  }
5343 
5344  if (ESI.Type != EST_Dynamic)
5345  return false;
5346 
5347  // Instantiate a dynamic exception specification's type.
5348  for (QualType T : ESI.Exceptions) {
5349  if (const PackExpansionType *PackExpansion =
5350  T->getAs<PackExpansionType>()) {
5351  Changed = true;
5352 
5353  // We have a pack expansion. Instantiate it.
5355  SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
5356  Unexpanded);
5357  assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
5358 
5359  // Determine whether the set of unexpanded parameter packs can and
5360  // should
5361  // be expanded.
5362  bool Expand = false;
5363  bool RetainExpansion = false;
5364  Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions();
5365  // FIXME: Track the location of the ellipsis (and track source location
5366  // information for the types in the exception specification in general).
5367  if (getDerived().TryExpandParameterPacks(
5368  Loc, SourceRange(), Unexpanded, Expand,
5369  RetainExpansion, NumExpansions))
5370  return true;
5371 
5372  if (!Expand) {
5373  // We can't expand this pack expansion into separate arguments yet;
5374  // just substitute into the pattern and create a new pack expansion
5375  // type.
5376  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
5377  QualType U = getDerived().TransformType(PackExpansion->getPattern());
5378  if (U.isNull())
5379  return true;
5380 
5381  U = SemaRef.Context.getPackExpansionType(U, NumExpansions);
5382  Exceptions.push_back(U);
5383  continue;
5384  }
5385 
5386  // Substitute into the pack expansion pattern for each slice of the
5387  // pack.
5388  for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
5389  Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), ArgIdx);
5390 
5391  QualType U = getDerived().TransformType(PackExpansion->getPattern());
5392  if (U.isNull() || SemaRef.CheckSpecifiedExceptionType(U, Loc))
5393  return true;
5394 
5395  Exceptions.push_back(U);
5396  }
5397  } else {
5398  QualType U = getDerived().TransformType(T);
5399  if (U.isNull() || SemaRef.CheckSpecifiedExceptionType(U, Loc))
5400  return true;
5401  if (T != U)
5402  Changed = true;
5403 
5404  Exceptions.push_back(U);
5405  }
5406  }
5407 
5408  ESI.Exceptions = Exceptions;
5409  if (ESI.Exceptions.empty())
5410  ESI.Type = EST_DynamicNone;
5411  return false;
5412 }
5413 
5414 template<typename Derived>
5416  TypeLocBuilder &TLB,
5418  const FunctionNoProtoType *T = TL.getTypePtr();
5419  QualType ResultType = getDerived().TransformType(TLB, TL.getReturnLoc());
5420  if (ResultType.isNull())
5421  return QualType();
5422 
5423  QualType Result = TL.getType();
5424  if (getDerived().AlwaysRebuild() || ResultType != T->getReturnType())
5425  Result = getDerived().RebuildFunctionNoProtoType(ResultType);
5426 
5427  FunctionNoProtoTypeLoc NewTL = TLB.push<FunctionNoProtoTypeLoc>(Result);
5429  NewTL.setLParenLoc(TL.getLParenLoc());
5430  NewTL.setRParenLoc(TL.getRParenLoc());
5431  NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
5432 
5433  return Result;
5434 }
5435 
5436 template<typename Derived> QualType
5439  const UnresolvedUsingType *T = TL.getTypePtr();
5440  Decl *D = getDerived().TransformDecl(TL.getNameLoc(), T->getDecl());
5441  if (!D)
5442  return QualType();
5443 
5444  QualType Result = TL.getType();
5445  if (getDerived().AlwaysRebuild() || D != T->getDecl()) {
5446  Result = getDerived().RebuildUnresolvedUsingType(TL.getNameLoc(), D);
5447  if (Result.isNull())
5448  return QualType();
5449  }
5450 
5451  // We might get an arbitrary type spec type back. We should at
5452  // least always get a type spec type, though.
5453  TypeSpecTypeLoc NewTL = TLB.pushTypeSpec(Result);
5454  NewTL.setNameLoc(TL.getNameLoc());
5455 
5456  return Result;
5457 }
5458 
5459 template<typename Derived>
5461  TypedefTypeLoc TL) {
5462  const TypedefType *T = TL.getTypePtr();
5463  TypedefNameDecl *Typedef
5464  = cast_or_null<TypedefNameDecl>(getDerived().TransformDecl(TL.getNameLoc(),
5465  T->getDecl()));
5466  if (!Typedef)
5467  return QualType();
5468 
5469  QualType Result = TL.getType();
5470  if (getDerived().AlwaysRebuild() ||
5471  Typedef != T->getDecl()) {
5472  Result = getDerived().RebuildTypedefType(Typedef);
5473  if (Result.isNull())
5474  return QualType();
5475  }
5476 
5477  TypedefTypeLoc NewTL = TLB.push<TypedefTypeLoc>(Result);
5478  NewTL.setNameLoc(TL.getNameLoc());
5479 
5480  return Result;
5481 }
5482 
5483 template<typename Derived>
5485  TypeOfExprTypeLoc TL) {
5486  // typeof expressions are not potentially evaluated contexts
5490 
5491  ExprResult E = getDerived().TransformExpr(TL.getUnderlyingExpr());
5492  if (E.isInvalid())
5493  return QualType();
5494 
5495  E = SemaRef.HandleExprEvaluationContextForTypeof(E.get());
5496  if (E.isInvalid())
5497  return QualType();
5498 
5499  QualType Result = TL.getType();
5500  if (getDerived().AlwaysRebuild() ||
5501  E.get() != TL.getUnderlyingExpr()) {
5502  Result = getDerived().RebuildTypeOfExprType(E.get(), TL.getTypeofLoc());
5503  if (Result.isNull())
5504  return QualType();
5505  }
5506  else E.get();
5507 
5508  TypeOfExprTypeLoc NewTL = TLB.push<TypeOfExprTypeLoc>(Result);
5509  NewTL.setTypeofLoc(TL.getTypeofLoc());
5510  NewTL.setLParenLoc(TL.getLParenLoc());
5511  NewTL.setRParenLoc(TL.getRParenLoc());
5512 
5513  return Result;
5514 }
5515 
5516 template<typename Derived>
5518  TypeOfTypeLoc TL) {
5519  TypeSourceInfo* Old_Under_TI = TL.getUnderlyingTInfo();
5520  TypeSourceInfo* New_Under_TI = getDerived().TransformType(Old_Under_TI);
5521  if (!New_Under_TI)
5522  return QualType();
5523 
5524  QualType Result = TL.getType();
5525  if (getDerived().AlwaysRebuild() || New_Under_TI != Old_Under_TI) {
5526  Result = getDerived().RebuildTypeOfType(New_Under_TI->getType());
5527  if (Result.isNull())
5528  return QualType();
5529  }
5530 
5531  TypeOfTypeLoc NewTL = TLB.push<TypeOfTypeLoc>(Result);
5532  NewTL.setTypeofLoc(TL.getTypeofLoc());
5533  NewTL.setLParenLoc(TL.getLParenLoc());
5534  NewTL.setRParenLoc(TL.getRParenLoc());
5535  NewTL.setUnderlyingTInfo(New_Under_TI);
5536 
5537  return Result;
5538 }
5539 
5540 template<typename Derived>
5542  DecltypeTypeLoc TL) {
5543  const DecltypeType *T = TL.getTypePtr();
5544 
5545  // decltype expressions are not potentially evaluated contexts
5549 
5550  ExprResult E = getDerived().TransformExpr(T->getUnderlyingExpr());
5551  if (E.isInvalid())
5552  return QualType();
5553 
5554  E = getSema().ActOnDecltypeExpression(E.get());
5555  if (E.isInvalid())
5556  return QualType();
5557 
5558  Q