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