Implement support for pack expansions in initializer lists and
[clang.git] / lib / Sema / TreeTransform.h
blob9088ae0fef26b8c6555b18b114b5c3bd6c4cd77e
1 //===------- TreeTransform.h - Semantic Tree Transformation -----*- C++ -*-===/
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===/
8 //
9 // This file implements a semantic tree transformation that takes a given
10 // AST and rebuilds it, possibly transforming some nodes in the process.
12 //===----------------------------------------------------------------------===/
13 #ifndef LLVM_CLANG_SEMA_TREETRANSFORM_H
14 #define LLVM_CLANG_SEMA_TREETRANSFORM_H
16 #include "clang/Sema/SemaInternal.h"
17 #include "clang/Sema/Lookup.h"
18 #include "clang/Sema/ParsedTemplate.h"
19 #include "clang/Sema/SemaDiagnostic.h"
20 #include "clang/Sema/ScopeInfo.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang/AST/ExprObjC.h"
26 #include "clang/AST/Stmt.h"
27 #include "clang/AST/StmtCXX.h"
28 #include "clang/AST/StmtObjC.h"
29 #include "clang/Sema/Ownership.h"
30 #include "clang/Sema/Designator.h"
31 #include "clang/Lex/Preprocessor.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "TypeLocBuilder.h"
34 #include <algorithm>
36 namespace clang {
37 using namespace sema;
39 /// \brief A semantic tree transformation that allows one to transform one
40 /// abstract syntax tree into another.
41 ///
42 /// A new tree transformation is defined by creating a new subclass \c X of
43 /// \c TreeTransform<X> and then overriding certain operations to provide
44 /// behavior specific to that transformation. For example, template
45 /// instantiation is implemented as a tree transformation where the
46 /// transformation of TemplateTypeParmType nodes involves substituting the
47 /// template arguments for their corresponding template parameters; a similar
48 /// transformation is performed for non-type template parameters and
49 /// template template parameters.
50 ///
51 /// This tree-transformation template uses static polymorphism to allow
52 /// subclasses to customize any of its operations. Thus, a subclass can
53 /// override any of the transformation or rebuild operators by providing an
54 /// operation with the same signature as the default implementation. The
55 /// overridding function should not be virtual.
56 ///
57 /// Semantic tree transformations are split into two stages, either of which
58 /// can be replaced by a subclass. The "transform" step transforms an AST node
59 /// or the parts of an AST node using the various transformation functions,
60 /// then passes the pieces on to the "rebuild" step, which constructs a new AST
61 /// node of the appropriate kind from the pieces. The default transformation
62 /// routines recursively transform the operands to composite AST nodes (e.g.,
63 /// the pointee type of a PointerType node) and, if any of those operand nodes
64 /// were changed by the transformation, invokes the rebuild operation to create
65 /// a new AST node.
66 ///
67 /// Subclasses can customize the transformation at various levels. The
68 /// most coarse-grained transformations involve replacing TransformType(),
69 /// TransformExpr(), TransformDecl(), TransformNestedNameSpecifier(),
70 /// TransformTemplateName(), or TransformTemplateArgument() with entirely
71 /// new implementations.
72 ///
73 /// For more fine-grained transformations, subclasses can replace any of the
74 /// \c TransformXXX functions (where XXX is the name of an AST node, e.g.,
75 /// PointerType, StmtExpr) to alter the transformation. As mentioned previously,
76 /// replacing TransformTemplateTypeParmType() allows template instantiation
77 /// to substitute template arguments for their corresponding template
78 /// parameters. Additionally, subclasses can override the \c RebuildXXX
79 /// functions to control how AST nodes are rebuilt when their operands change.
80 /// By default, \c TreeTransform will invoke semantic analysis to rebuild
81 /// AST nodes. However, certain other tree transformations (e.g, cloning) may
82 /// be able to use more efficient rebuild steps.
83 ///
84 /// There are a handful of other functions that can be overridden, allowing one
85 /// to avoid traversing nodes that don't need any transformation
86 /// (\c AlreadyTransformed()), force rebuilding AST nodes even when their
87 /// operands have not changed (\c AlwaysRebuild()), and customize the
88 /// default locations and entity names used for type-checking
89 /// (\c getBaseLocation(), \c getBaseEntity()).
90 template<typename Derived>
91 class TreeTransform {
92 protected:
93 Sema &SemaRef;
95 public:
96 /// \brief Initializes a new tree transformer.
97 TreeTransform(Sema &SemaRef) : SemaRef(SemaRef) { }
99 /// \brief Retrieves a reference to the derived class.
100 Derived &getDerived() { return static_cast<Derived&>(*this); }
102 /// \brief Retrieves a reference to the derived class.
103 const Derived &getDerived() const {
104 return static_cast<const Derived&>(*this);
107 static inline ExprResult Owned(Expr *E) { return E; }
108 static inline StmtResult Owned(Stmt *S) { return S; }
110 /// \brief Retrieves a reference to the semantic analysis object used for
111 /// this tree transform.
112 Sema &getSema() const { return SemaRef; }
114 /// \brief Whether the transformation should always rebuild AST nodes, even
115 /// if none of the children have changed.
117 /// Subclasses may override this function to specify when the transformation
118 /// should rebuild all AST nodes.
119 bool AlwaysRebuild() { return false; }
121 /// \brief Returns the location of the entity being transformed, if that
122 /// information was not available elsewhere in the AST.
124 /// By default, returns no source-location information. Subclasses can
125 /// provide an alternative implementation that provides better location
126 /// information.
127 SourceLocation getBaseLocation() { return SourceLocation(); }
129 /// \brief Returns the name of the entity being transformed, if that
130 /// information was not available elsewhere in the AST.
132 /// By default, returns an empty name. Subclasses can provide an alternative
133 /// implementation with a more precise name.
134 DeclarationName getBaseEntity() { return DeclarationName(); }
136 /// \brief Sets the "base" location and entity when that
137 /// information is known based on another transformation.
139 /// By default, the source location and entity are ignored. Subclasses can
140 /// override this function to provide a customized implementation.
141 void setBase(SourceLocation Loc, DeclarationName Entity) { }
143 /// \brief RAII object that temporarily sets the base location and entity
144 /// used for reporting diagnostics in types.
145 class TemporaryBase {
146 TreeTransform &Self;
147 SourceLocation OldLocation;
148 DeclarationName OldEntity;
150 public:
151 TemporaryBase(TreeTransform &Self, SourceLocation Location,
152 DeclarationName Entity) : Self(Self) {
153 OldLocation = Self.getDerived().getBaseLocation();
154 OldEntity = Self.getDerived().getBaseEntity();
155 Self.getDerived().setBase(Location, Entity);
158 ~TemporaryBase() {
159 Self.getDerived().setBase(OldLocation, OldEntity);
163 /// \brief Determine whether the given type \p T has already been
164 /// transformed.
166 /// Subclasses can provide an alternative implementation of this routine
167 /// to short-circuit evaluation when it is known that a given type will
168 /// not change. For example, template instantiation need not traverse
169 /// non-dependent types.
170 bool AlreadyTransformed(QualType T) {
171 return T.isNull();
174 /// \brief Determine whether the given call argument should be dropped, e.g.,
175 /// because it is a default argument.
177 /// Subclasses can provide an alternative implementation of this routine to
178 /// determine which kinds of call arguments get dropped. By default,
179 /// CXXDefaultArgument nodes are dropped (prior to transformation).
180 bool DropCallArgument(Expr *E) {
181 return E->isDefaultArgument();
184 /// \brief Determine whether we should expand a pack expansion with the
185 /// given set of parameter packs into separate arguments by repeatedly
186 /// transforming the pattern.
188 /// By default, the transformer never tries to expand pack expansions.
189 /// Subclasses can override this routine to provide different behavior.
191 /// \param EllipsisLoc The location of the ellipsis that identifies the
192 /// pack expansion.
194 /// \param PatternRange The source range that covers the entire pattern of
195 /// the pack expansion.
197 /// \param Unexpanded The set of unexpanded parameter packs within the
198 /// pattern.
200 /// \param NumUnexpanded The number of unexpanded parameter packs in
201 /// \p Unexpanded.
203 /// \param ShouldExpand Will be set to \c true if the transformer should
204 /// expand the corresponding pack expansions into separate arguments. When
205 /// set, \c NumExpansions must also be set.
207 /// \param NumExpansions The number of separate arguments that will be in
208 /// the expanded form of the corresponding pack expansion. Must be set when
209 /// \c ShouldExpand is \c true.
211 /// \returns true if an error occurred (e.g., because the parameter packs
212 /// are to be instantiated with arguments of different lengths), false
213 /// otherwise. If false, \c ShouldExpand (and possibly \c NumExpansions)
214 /// must be set.
215 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
216 SourceRange PatternRange,
217 const UnexpandedParameterPack *Unexpanded,
218 unsigned NumUnexpanded,
219 bool &ShouldExpand,
220 unsigned &NumExpansions) {
221 ShouldExpand = false;
222 return false;
225 /// \brief Transforms the given type into another type.
227 /// By default, this routine transforms a type by creating a
228 /// TypeSourceInfo for it and delegating to the appropriate
229 /// function. This is expensive, but we don't mind, because
230 /// this method is deprecated anyway; all users should be
231 /// switched to storing TypeSourceInfos.
233 /// \returns the transformed type.
234 QualType TransformType(QualType T);
236 /// \brief Transforms the given type-with-location into a new
237 /// type-with-location.
239 /// By default, this routine transforms a type by delegating to the
240 /// appropriate TransformXXXType to build a new type. Subclasses
241 /// may override this function (to take over all type
242 /// transformations) or some set of the TransformXXXType functions
243 /// to alter the transformation.
244 TypeSourceInfo *TransformType(TypeSourceInfo *DI);
246 /// \brief Transform the given type-with-location into a new
247 /// type, collecting location information in the given builder
248 /// as necessary.
250 QualType TransformType(TypeLocBuilder &TLB, TypeLoc TL);
252 /// \brief Transform the given statement.
254 /// By default, this routine transforms a statement by delegating to the
255 /// appropriate TransformXXXStmt function to transform a specific kind of
256 /// statement or the TransformExpr() function to transform an expression.
257 /// Subclasses may override this function to transform statements using some
258 /// other mechanism.
260 /// \returns the transformed statement.
261 StmtResult TransformStmt(Stmt *S);
263 /// \brief Transform the given expression.
265 /// By default, this routine transforms an expression by delegating to the
266 /// appropriate TransformXXXExpr function to build a new expression.
267 /// Subclasses may override this function to transform expressions using some
268 /// other mechanism.
270 /// \returns the transformed expression.
271 ExprResult TransformExpr(Expr *E);
273 /// \brief Transform the given list of expressions.
275 /// This routine transforms a list of expressions by invoking
276 /// \c TransformExpr() for each subexpression. However, it also provides
277 /// support for variadic templates by expanding any pack expansions (if the
278 /// derived class permits such expansion) along the way. When pack expansions
279 /// are present, the number of outputs may not equal the number of inputs.
281 /// \param Inputs The set of expressions to be transformed.
283 /// \param NumInputs The number of expressions in \c Inputs.
285 /// \param IsCall If \c true, then this transform is being performed on
286 /// function-call arguments, and any arguments that should be dropped, will
287 /// be.
289 /// \param Outputs The transformed input expressions will be added to this
290 /// vector.
292 /// \param ArgChanged If non-NULL, will be set \c true if any argument changed
293 /// due to transformation.
295 /// \returns true if an error occurred, false otherwise.
296 bool TransformExprs(Expr **Inputs, unsigned NumInputs, bool IsCall,
297 llvm::SmallVectorImpl<Expr *> &Outputs,
298 bool *ArgChanged = 0);
300 /// \brief Transform the given declaration, which is referenced from a type
301 /// or expression.
303 /// By default, acts as the identity function on declarations. Subclasses
304 /// may override this function to provide alternate behavior.
305 Decl *TransformDecl(SourceLocation Loc, Decl *D) { return D; }
307 /// \brief Transform the definition of the given declaration.
309 /// By default, invokes TransformDecl() to transform the declaration.
310 /// Subclasses may override this function to provide alternate behavior.
311 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
312 return getDerived().TransformDecl(Loc, D);
315 /// \brief Transform the given declaration, which was the first part of a
316 /// nested-name-specifier in a member access expression.
318 /// This specific declaration transformation only applies to the first
319 /// identifier in a nested-name-specifier of a member access expression, e.g.,
320 /// the \c T in \c x->T::member
322 /// By default, invokes TransformDecl() to transform the declaration.
323 /// Subclasses may override this function to provide alternate behavior.
324 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc) {
325 return cast_or_null<NamedDecl>(getDerived().TransformDecl(Loc, D));
328 /// \brief Transform the given nested-name-specifier.
330 /// By default, transforms all of the types and declarations within the
331 /// nested-name-specifier. Subclasses may override this function to provide
332 /// alternate behavior.
333 NestedNameSpecifier *TransformNestedNameSpecifier(NestedNameSpecifier *NNS,
334 SourceRange Range,
335 QualType ObjectType = QualType(),
336 NamedDecl *FirstQualifierInScope = 0);
338 /// \brief Transform the given declaration name.
340 /// By default, transforms the types of conversion function, constructor,
341 /// and destructor names and then (if needed) rebuilds the declaration name.
342 /// Identifiers and selectors are returned unmodified. Sublcasses may
343 /// override this function to provide alternate behavior.
344 DeclarationNameInfo
345 TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
347 /// \brief Transform the given template name.
349 /// By default, transforms the template name by transforming the declarations
350 /// and nested-name-specifiers that occur within the template name.
351 /// Subclasses may override this function to provide alternate behavior.
352 TemplateName TransformTemplateName(TemplateName Name,
353 QualType ObjectType = QualType(),
354 NamedDecl *FirstQualifierInScope = 0);
356 /// \brief Transform the given template argument.
358 /// By default, this operation transforms the type, expression, or
359 /// declaration stored within the template argument and constructs a
360 /// new template argument from the transformed result. Subclasses may
361 /// override this function to provide alternate behavior.
363 /// Returns true if there was an error.
364 bool TransformTemplateArgument(const TemplateArgumentLoc &Input,
365 TemplateArgumentLoc &Output);
367 /// \brief Transform the given set of template arguments.
369 /// By default, this operation transforms all of the template arguments
370 /// in the input set using \c TransformTemplateArgument(), and appends
371 /// the transformed arguments to the output list.
373 /// Note that this overload of \c TransformTemplateArguments() is merely
374 /// a convenience function. Subclasses that wish to override this behavior
375 /// should override the iterator-based member template version.
377 /// \param Inputs The set of template arguments to be transformed.
379 /// \param NumInputs The number of template arguments in \p Inputs.
381 /// \param Outputs The set of transformed template arguments output by this
382 /// routine.
384 /// Returns true if an error occurred.
385 bool TransformTemplateArguments(const TemplateArgumentLoc *Inputs,
386 unsigned NumInputs,
387 TemplateArgumentListInfo &Outputs) {
388 return TransformTemplateArguments(Inputs, Inputs + NumInputs, Outputs);
391 /// \brief Transform the given set of template arguments.
393 /// By default, this operation transforms all of the template arguments
394 /// in the input set using \c TransformTemplateArgument(), and appends
395 /// the transformed arguments to the output list.
397 /// \param First An iterator to the first template argument.
399 /// \param Last An iterator one step past the last template argument.
401 /// \param Outputs The set of transformed template arguments output by this
402 /// routine.
404 /// Returns true if an error occurred.
405 template<typename InputIterator>
406 bool TransformTemplateArguments(InputIterator First,
407 InputIterator Last,
408 TemplateArgumentListInfo &Outputs);
410 /// \brief Fakes up a TemplateArgumentLoc for a given TemplateArgument.
411 void InventTemplateArgumentLoc(const TemplateArgument &Arg,
412 TemplateArgumentLoc &ArgLoc);
414 /// \brief Fakes up a TypeSourceInfo for a type.
415 TypeSourceInfo *InventTypeSourceInfo(QualType T) {
416 return SemaRef.Context.getTrivialTypeSourceInfo(T,
417 getDerived().getBaseLocation());
420 #define ABSTRACT_TYPELOC(CLASS, PARENT)
421 #define TYPELOC(CLASS, PARENT) \
422 QualType Transform##CLASS##Type(TypeLocBuilder &TLB, CLASS##TypeLoc T);
423 #include "clang/AST/TypeLocNodes.def"
425 QualType
426 TransformTemplateSpecializationType(TypeLocBuilder &TLB,
427 TemplateSpecializationTypeLoc TL,
428 TemplateName Template);
430 QualType
431 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
432 DependentTemplateSpecializationTypeLoc TL,
433 NestedNameSpecifier *Prefix);
435 /// \brief Transforms the parameters of a function type into the
436 /// given vectors.
438 /// The result vectors should be kept in sync; null entries in the
439 /// variables vector are acceptable.
441 /// Return true on error.
442 bool TransformFunctionTypeParams(FunctionProtoTypeLoc TL,
443 llvm::SmallVectorImpl<QualType> &PTypes,
444 llvm::SmallVectorImpl<ParmVarDecl*> &PVars);
446 /// \brief Transforms a single function-type parameter. Return null
447 /// on error.
448 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
450 QualType TransformReferenceType(TypeLocBuilder &TLB, ReferenceTypeLoc TL);
452 StmtResult TransformCompoundStmt(CompoundStmt *S, bool IsStmtExpr);
453 ExprResult TransformCXXNamedCastExpr(CXXNamedCastExpr *E);
455 #define STMT(Node, Parent) \
456 StmtResult Transform##Node(Node *S);
457 #define EXPR(Node, Parent) \
458 ExprResult Transform##Node(Node *E);
459 #define ABSTRACT_STMT(Stmt)
460 #include "clang/AST/StmtNodes.inc"
462 /// \brief Build a new pointer type given its pointee type.
464 /// By default, performs semantic analysis when building the pointer type.
465 /// Subclasses may override this routine to provide different behavior.
466 QualType RebuildPointerType(QualType PointeeType, SourceLocation Sigil);
468 /// \brief Build a new block pointer type given its pointee type.
470 /// By default, performs semantic analysis when building the block pointer
471 /// type. Subclasses may override this routine to provide different behavior.
472 QualType RebuildBlockPointerType(QualType PointeeType, SourceLocation Sigil);
474 /// \brief Build a new reference type given the type it references.
476 /// By default, performs semantic analysis when building the
477 /// reference type. Subclasses may override this routine to provide
478 /// different behavior.
480 /// \param LValue whether the type was written with an lvalue sigil
481 /// or an rvalue sigil.
482 QualType RebuildReferenceType(QualType ReferentType,
483 bool LValue,
484 SourceLocation Sigil);
486 /// \brief Build a new member pointer type given the pointee type and the
487 /// class type it refers into.
489 /// By default, performs semantic analysis when building the member pointer
490 /// type. Subclasses may override this routine to provide different behavior.
491 QualType RebuildMemberPointerType(QualType PointeeType, QualType ClassType,
492 SourceLocation Sigil);
494 /// \brief Build a new array type given the element type, size
495 /// modifier, size of the array (if known), size expression, and index type
496 /// qualifiers.
498 /// By default, performs semantic analysis when building the array type.
499 /// Subclasses may override this routine to provide different behavior.
500 /// Also by default, all of the other Rebuild*Array
501 QualType RebuildArrayType(QualType ElementType,
502 ArrayType::ArraySizeModifier SizeMod,
503 const llvm::APInt *Size,
504 Expr *SizeExpr,
505 unsigned IndexTypeQuals,
506 SourceRange BracketsRange);
508 /// \brief Build a new constant array type given the element type, size
509 /// modifier, (known) size of the array, and index type qualifiers.
511 /// By default, performs semantic analysis when building the array type.
512 /// Subclasses may override this routine to provide different behavior.
513 QualType RebuildConstantArrayType(QualType ElementType,
514 ArrayType::ArraySizeModifier SizeMod,
515 const llvm::APInt &Size,
516 unsigned IndexTypeQuals,
517 SourceRange BracketsRange);
519 /// \brief Build a new incomplete array type given the element type, size
520 /// modifier, and index type qualifiers.
522 /// By default, performs semantic analysis when building the array type.
523 /// Subclasses may override this routine to provide different behavior.
524 QualType RebuildIncompleteArrayType(QualType ElementType,
525 ArrayType::ArraySizeModifier SizeMod,
526 unsigned IndexTypeQuals,
527 SourceRange BracketsRange);
529 /// \brief Build a new variable-length array type given the element type,
530 /// size modifier, size expression, and index type qualifiers.
532 /// By default, performs semantic analysis when building the array type.
533 /// Subclasses may override this routine to provide different behavior.
534 QualType RebuildVariableArrayType(QualType ElementType,
535 ArrayType::ArraySizeModifier SizeMod,
536 Expr *SizeExpr,
537 unsigned IndexTypeQuals,
538 SourceRange BracketsRange);
540 /// \brief Build a new dependent-sized array type given the element type,
541 /// size modifier, size expression, and index type qualifiers.
543 /// By default, performs semantic analysis when building the array type.
544 /// Subclasses may override this routine to provide different behavior.
545 QualType RebuildDependentSizedArrayType(QualType ElementType,
546 ArrayType::ArraySizeModifier SizeMod,
547 Expr *SizeExpr,
548 unsigned IndexTypeQuals,
549 SourceRange BracketsRange);
551 /// \brief Build a new vector type given the element type and
552 /// number of elements.
554 /// By default, performs semantic analysis when building the vector type.
555 /// Subclasses may override this routine to provide different behavior.
556 QualType RebuildVectorType(QualType ElementType, unsigned NumElements,
557 VectorType::VectorKind VecKind);
559 /// \brief Build a new extended vector type given the element type and
560 /// number of elements.
562 /// By default, performs semantic analysis when building the vector type.
563 /// Subclasses may override this routine to provide different behavior.
564 QualType RebuildExtVectorType(QualType ElementType, unsigned NumElements,
565 SourceLocation AttributeLoc);
567 /// \brief Build a new potentially dependently-sized extended vector type
568 /// given the element type and number of elements.
570 /// By default, performs semantic analysis when building the vector type.
571 /// Subclasses may override this routine to provide different behavior.
572 QualType RebuildDependentSizedExtVectorType(QualType ElementType,
573 Expr *SizeExpr,
574 SourceLocation AttributeLoc);
576 /// \brief Build a new function type.
578 /// By default, performs semantic analysis when building the function type.
579 /// Subclasses may override this routine to provide different behavior.
580 QualType RebuildFunctionProtoType(QualType T,
581 QualType *ParamTypes,
582 unsigned NumParamTypes,
583 bool Variadic, unsigned Quals,
584 const FunctionType::ExtInfo &Info);
586 /// \brief Build a new unprototyped function type.
587 QualType RebuildFunctionNoProtoType(QualType ResultType);
589 /// \brief Rebuild an unresolved typename type, given the decl that
590 /// the UnresolvedUsingTypenameDecl was transformed to.
591 QualType RebuildUnresolvedUsingType(Decl *D);
593 /// \brief Build a new typedef type.
594 QualType RebuildTypedefType(TypedefDecl *Typedef) {
595 return SemaRef.Context.getTypeDeclType(Typedef);
598 /// \brief Build a new class/struct/union type.
599 QualType RebuildRecordType(RecordDecl *Record) {
600 return SemaRef.Context.getTypeDeclType(Record);
603 /// \brief Build a new Enum type.
604 QualType RebuildEnumType(EnumDecl *Enum) {
605 return SemaRef.Context.getTypeDeclType(Enum);
608 /// \brief Build a new typeof(expr) type.
610 /// By default, performs semantic analysis when building the typeof type.
611 /// Subclasses may override this routine to provide different behavior.
612 QualType RebuildTypeOfExprType(Expr *Underlying, SourceLocation Loc);
614 /// \brief Build a new typeof(type) type.
616 /// By default, builds a new TypeOfType with the given underlying type.
617 QualType RebuildTypeOfType(QualType Underlying);
619 /// \brief Build a new C++0x decltype type.
621 /// By default, performs semantic analysis when building the decltype type.
622 /// Subclasses may override this routine to provide different behavior.
623 QualType RebuildDecltypeType(Expr *Underlying, SourceLocation Loc);
625 /// \brief Build a new template specialization type.
627 /// By default, performs semantic analysis when building the template
628 /// specialization type. Subclasses may override this routine to provide
629 /// different behavior.
630 QualType RebuildTemplateSpecializationType(TemplateName Template,
631 SourceLocation TemplateLoc,
632 const TemplateArgumentListInfo &Args);
634 /// \brief Build a new parenthesized type.
636 /// By default, builds a new ParenType type from the inner type.
637 /// Subclasses may override this routine to provide different behavior.
638 QualType RebuildParenType(QualType InnerType) {
639 return SemaRef.Context.getParenType(InnerType);
642 /// \brief Build a new qualified name type.
644 /// By default, builds a new ElaboratedType type from the keyword,
645 /// the nested-name-specifier and the named type.
646 /// Subclasses may override this routine to provide different behavior.
647 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
648 ElaboratedTypeKeyword Keyword,
649 NestedNameSpecifier *NNS, QualType Named) {
650 return SemaRef.Context.getElaboratedType(Keyword, NNS, Named);
653 /// \brief Build a new typename type that refers to a template-id.
655 /// By default, builds a new DependentNameType type from the
656 /// nested-name-specifier and the given type. Subclasses may override
657 /// this routine to provide different behavior.
658 QualType RebuildDependentTemplateSpecializationType(
659 ElaboratedTypeKeyword Keyword,
660 NestedNameSpecifier *Qualifier,
661 SourceRange QualifierRange,
662 const IdentifierInfo *Name,
663 SourceLocation NameLoc,
664 const TemplateArgumentListInfo &Args) {
665 // Rebuild the template name.
666 // TODO: avoid TemplateName abstraction
667 TemplateName InstName =
668 getDerived().RebuildTemplateName(Qualifier, QualifierRange, *Name,
669 QualType(), 0);
671 if (InstName.isNull())
672 return QualType();
674 // If it's still dependent, make a dependent specialization.
675 if (InstName.getAsDependentTemplateName())
676 return SemaRef.Context.getDependentTemplateSpecializationType(
677 Keyword, Qualifier, Name, Args);
679 // Otherwise, make an elaborated type wrapping a non-dependent
680 // specialization.
681 QualType T =
682 getDerived().RebuildTemplateSpecializationType(InstName, NameLoc, Args);
683 if (T.isNull()) return QualType();
685 // NOTE: NNS is already recorded in template specialization type T.
686 return SemaRef.Context.getElaboratedType(Keyword, /*NNS=*/0, T);
689 /// \brief Build a new typename type that refers to an identifier.
691 /// By default, performs semantic analysis when building the typename type
692 /// (or elaborated type). Subclasses may override this routine to provide
693 /// different behavior.
694 QualType RebuildDependentNameType(ElaboratedTypeKeyword Keyword,
695 NestedNameSpecifier *NNS,
696 const IdentifierInfo *Id,
697 SourceLocation KeywordLoc,
698 SourceRange NNSRange,
699 SourceLocation IdLoc) {
700 CXXScopeSpec SS;
701 SS.setScopeRep(NNS);
702 SS.setRange(NNSRange);
704 if (NNS->isDependent()) {
705 // If the name is still dependent, just build a new dependent name type.
706 if (!SemaRef.computeDeclContext(SS))
707 return SemaRef.Context.getDependentNameType(Keyword, NNS, Id);
710 if (Keyword == ETK_None || Keyword == ETK_Typename)
711 return SemaRef.CheckTypenameType(Keyword, NNS, *Id,
712 KeywordLoc, NNSRange, IdLoc);
714 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
716 // We had a dependent elaborated-type-specifier that has been transformed
717 // into a non-dependent elaborated-type-specifier. Find the tag we're
718 // referring to.
719 LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName);
720 DeclContext *DC = SemaRef.computeDeclContext(SS, false);
721 if (!DC)
722 return QualType();
724 if (SemaRef.RequireCompleteDeclContext(SS, DC))
725 return QualType();
727 TagDecl *Tag = 0;
728 SemaRef.LookupQualifiedName(Result, DC);
729 switch (Result.getResultKind()) {
730 case LookupResult::NotFound:
731 case LookupResult::NotFoundInCurrentInstantiation:
732 break;
734 case LookupResult::Found:
735 Tag = Result.getAsSingle<TagDecl>();
736 break;
738 case LookupResult::FoundOverloaded:
739 case LookupResult::FoundUnresolvedValue:
740 llvm_unreachable("Tag lookup cannot find non-tags");
741 return QualType();
743 case LookupResult::Ambiguous:
744 // Let the LookupResult structure handle ambiguities.
745 return QualType();
748 if (!Tag) {
749 // FIXME: Would be nice to highlight just the source range.
750 SemaRef.Diag(IdLoc, diag::err_not_tag_in_scope)
751 << Kind << Id << DC;
752 return QualType();
755 if (!SemaRef.isAcceptableTagRedeclaration(Tag, Kind, IdLoc, *Id)) {
756 SemaRef.Diag(KeywordLoc, diag::err_use_with_wrong_tag) << Id;
757 SemaRef.Diag(Tag->getLocation(), diag::note_previous_use);
758 return QualType();
761 // Build the elaborated-type-specifier type.
762 QualType T = SemaRef.Context.getTypeDeclType(Tag);
763 return SemaRef.Context.getElaboratedType(Keyword, NNS, T);
766 /// \brief Build a new nested-name-specifier given the prefix and an
767 /// identifier that names the next step in the nested-name-specifier.
769 /// By default, performs semantic analysis when building the new
770 /// nested-name-specifier. Subclasses may override this routine to provide
771 /// different behavior.
772 NestedNameSpecifier *RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
773 SourceRange Range,
774 IdentifierInfo &II,
775 QualType ObjectType,
776 NamedDecl *FirstQualifierInScope);
778 /// \brief Build a new nested-name-specifier given the prefix and the
779 /// namespace named in the next step in the nested-name-specifier.
781 /// By default, performs semantic analysis when building the new
782 /// nested-name-specifier. Subclasses may override this routine to provide
783 /// different behavior.
784 NestedNameSpecifier *RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
785 SourceRange Range,
786 NamespaceDecl *NS);
788 /// \brief Build a new nested-name-specifier given the prefix and the
789 /// type named in the next step in the nested-name-specifier.
791 /// By default, performs semantic analysis when building the new
792 /// nested-name-specifier. Subclasses may override this routine to provide
793 /// different behavior.
794 NestedNameSpecifier *RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
795 SourceRange Range,
796 bool TemplateKW,
797 QualType T);
799 /// \brief Build a new template name given a nested name specifier, a flag
800 /// indicating whether the "template" keyword was provided, and the template
801 /// that the template name refers to.
803 /// By default, builds the new template name directly. Subclasses may override
804 /// this routine to provide different behavior.
805 TemplateName RebuildTemplateName(NestedNameSpecifier *Qualifier,
806 bool TemplateKW,
807 TemplateDecl *Template);
809 /// \brief Build a new template name given a nested name specifier and the
810 /// name that is referred to as a template.
812 /// By default, performs semantic analysis to determine whether the name can
813 /// be resolved to a specific template, then builds the appropriate kind of
814 /// template name. Subclasses may override this routine to provide different
815 /// behavior.
816 TemplateName RebuildTemplateName(NestedNameSpecifier *Qualifier,
817 SourceRange QualifierRange,
818 const IdentifierInfo &II,
819 QualType ObjectType,
820 NamedDecl *FirstQualifierInScope);
822 /// \brief Build a new template name given a nested name specifier and the
823 /// overloaded operator name that is referred to as a template.
825 /// By default, performs semantic analysis to determine whether the name can
826 /// be resolved to a specific template, then builds the appropriate kind of
827 /// template name. Subclasses may override this routine to provide different
828 /// behavior.
829 TemplateName RebuildTemplateName(NestedNameSpecifier *Qualifier,
830 OverloadedOperatorKind Operator,
831 QualType ObjectType);
833 /// \brief Build a new compound statement.
835 /// By default, performs semantic analysis to build the new statement.
836 /// Subclasses may override this routine to provide different behavior.
837 StmtResult RebuildCompoundStmt(SourceLocation LBraceLoc,
838 MultiStmtArg Statements,
839 SourceLocation RBraceLoc,
840 bool IsStmtExpr) {
841 return getSema().ActOnCompoundStmt(LBraceLoc, RBraceLoc, Statements,
842 IsStmtExpr);
845 /// \brief Build a new case statement.
847 /// By default, performs semantic analysis to build the new statement.
848 /// Subclasses may override this routine to provide different behavior.
849 StmtResult RebuildCaseStmt(SourceLocation CaseLoc,
850 Expr *LHS,
851 SourceLocation EllipsisLoc,
852 Expr *RHS,
853 SourceLocation ColonLoc) {
854 return getSema().ActOnCaseStmt(CaseLoc, LHS, EllipsisLoc, RHS,
855 ColonLoc);
858 /// \brief Attach the body to a new case statement.
860 /// By default, performs semantic analysis to build the new statement.
861 /// Subclasses may override this routine to provide different behavior.
862 StmtResult RebuildCaseStmtBody(Stmt *S, Stmt *Body) {
863 getSema().ActOnCaseStmtBody(S, Body);
864 return S;
867 /// \brief Build a new default statement.
869 /// By default, performs semantic analysis to build the new statement.
870 /// Subclasses may override this routine to provide different behavior.
871 StmtResult RebuildDefaultStmt(SourceLocation DefaultLoc,
872 SourceLocation ColonLoc,
873 Stmt *SubStmt) {
874 return getSema().ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt,
875 /*CurScope=*/0);
878 /// \brief Build a new label statement.
880 /// By default, performs semantic analysis to build the new statement.
881 /// Subclasses may override this routine to provide different behavior.
882 StmtResult RebuildLabelStmt(SourceLocation IdentLoc,
883 IdentifierInfo *Id,
884 SourceLocation ColonLoc,
885 Stmt *SubStmt, bool HasUnusedAttr) {
886 return SemaRef.ActOnLabelStmt(IdentLoc, Id, ColonLoc, SubStmt,
887 HasUnusedAttr);
890 /// \brief Build a new "if" statement.
892 /// By default, performs semantic analysis to build the new statement.
893 /// Subclasses may override this routine to provide different behavior.
894 StmtResult RebuildIfStmt(SourceLocation IfLoc, Sema::FullExprArg Cond,
895 VarDecl *CondVar, Stmt *Then,
896 SourceLocation ElseLoc, Stmt *Else) {
897 return getSema().ActOnIfStmt(IfLoc, Cond, CondVar, Then, ElseLoc, Else);
900 /// \brief Start building a new switch statement.
902 /// By default, performs semantic analysis to build the new statement.
903 /// Subclasses may override this routine to provide different behavior.
904 StmtResult RebuildSwitchStmtStart(SourceLocation SwitchLoc,
905 Expr *Cond, VarDecl *CondVar) {
906 return getSema().ActOnStartOfSwitchStmt(SwitchLoc, Cond,
907 CondVar);
910 /// \brief Attach the body to the switch statement.
912 /// By default, performs semantic analysis to build the new statement.
913 /// Subclasses may override this routine to provide different behavior.
914 StmtResult RebuildSwitchStmtBody(SourceLocation SwitchLoc,
915 Stmt *Switch, Stmt *Body) {
916 return getSema().ActOnFinishSwitchStmt(SwitchLoc, Switch, Body);
919 /// \brief Build a new while statement.
921 /// By default, performs semantic analysis to build the new statement.
922 /// Subclasses may override this routine to provide different behavior.
923 StmtResult RebuildWhileStmt(SourceLocation WhileLoc,
924 Sema::FullExprArg Cond,
925 VarDecl *CondVar,
926 Stmt *Body) {
927 return getSema().ActOnWhileStmt(WhileLoc, Cond, CondVar, Body);
930 /// \brief Build a new do-while statement.
932 /// By default, performs semantic analysis to build the new statement.
933 /// Subclasses may override this routine to provide different behavior.
934 StmtResult RebuildDoStmt(SourceLocation DoLoc, Stmt *Body,
935 SourceLocation WhileLoc,
936 SourceLocation LParenLoc,
937 Expr *Cond,
938 SourceLocation RParenLoc) {
939 return getSema().ActOnDoStmt(DoLoc, Body, WhileLoc, LParenLoc,
940 Cond, RParenLoc);
943 /// \brief Build a new for statement.
945 /// By default, performs semantic analysis to build the new statement.
946 /// Subclasses may override this routine to provide different behavior.
947 StmtResult RebuildForStmt(SourceLocation ForLoc,
948 SourceLocation LParenLoc,
949 Stmt *Init, Sema::FullExprArg Cond,
950 VarDecl *CondVar, Sema::FullExprArg Inc,
951 SourceLocation RParenLoc, Stmt *Body) {
952 return getSema().ActOnForStmt(ForLoc, LParenLoc, Init, Cond,
953 CondVar,
954 Inc, RParenLoc, Body);
957 /// \brief Build a new goto statement.
959 /// By default, performs semantic analysis to build the new statement.
960 /// Subclasses may override this routine to provide different behavior.
961 StmtResult RebuildGotoStmt(SourceLocation GotoLoc,
962 SourceLocation LabelLoc,
963 LabelStmt *Label) {
964 return getSema().ActOnGotoStmt(GotoLoc, LabelLoc, Label->getID());
967 /// \brief Build a new indirect goto statement.
969 /// By default, performs semantic analysis to build the new statement.
970 /// Subclasses may override this routine to provide different behavior.
971 StmtResult RebuildIndirectGotoStmt(SourceLocation GotoLoc,
972 SourceLocation StarLoc,
973 Expr *Target) {
974 return getSema().ActOnIndirectGotoStmt(GotoLoc, StarLoc, Target);
977 /// \brief Build a new return statement.
979 /// By default, performs semantic analysis to build the new statement.
980 /// Subclasses may override this routine to provide different behavior.
981 StmtResult RebuildReturnStmt(SourceLocation ReturnLoc,
982 Expr *Result) {
984 return getSema().ActOnReturnStmt(ReturnLoc, Result);
987 /// \brief Build a new declaration statement.
989 /// By default, performs semantic analysis to build the new statement.
990 /// Subclasses may override this routine to provide different behavior.
991 StmtResult RebuildDeclStmt(Decl **Decls, unsigned NumDecls,
992 SourceLocation StartLoc,
993 SourceLocation EndLoc) {
994 return getSema().Owned(
995 new (getSema().Context) DeclStmt(
996 DeclGroupRef::Create(getSema().Context,
997 Decls, NumDecls),
998 StartLoc, EndLoc));
1001 /// \brief Build a new inline asm statement.
1003 /// By default, performs semantic analysis to build the new statement.
1004 /// Subclasses may override this routine to provide different behavior.
1005 StmtResult RebuildAsmStmt(SourceLocation AsmLoc,
1006 bool IsSimple,
1007 bool IsVolatile,
1008 unsigned NumOutputs,
1009 unsigned NumInputs,
1010 IdentifierInfo **Names,
1011 MultiExprArg Constraints,
1012 MultiExprArg Exprs,
1013 Expr *AsmString,
1014 MultiExprArg Clobbers,
1015 SourceLocation RParenLoc,
1016 bool MSAsm) {
1017 return getSema().ActOnAsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs,
1018 NumInputs, Names, move(Constraints),
1019 Exprs, AsmString, Clobbers,
1020 RParenLoc, MSAsm);
1023 /// \brief Build a new Objective-C @try statement.
1025 /// By default, performs semantic analysis to build the new statement.
1026 /// Subclasses may override this routine to provide different behavior.
1027 StmtResult RebuildObjCAtTryStmt(SourceLocation AtLoc,
1028 Stmt *TryBody,
1029 MultiStmtArg CatchStmts,
1030 Stmt *Finally) {
1031 return getSema().ActOnObjCAtTryStmt(AtLoc, TryBody, move(CatchStmts),
1032 Finally);
1035 /// \brief Rebuild an Objective-C exception declaration.
1037 /// By default, performs semantic analysis to build the new declaration.
1038 /// Subclasses may override this routine to provide different behavior.
1039 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
1040 TypeSourceInfo *TInfo, QualType T) {
1041 return getSema().BuildObjCExceptionDecl(TInfo, T,
1042 ExceptionDecl->getIdentifier(),
1043 ExceptionDecl->getLocation());
1046 /// \brief Build a new Objective-C @catch statement.
1048 /// By default, performs semantic analysis to build the new statement.
1049 /// Subclasses may override this routine to provide different behavior.
1050 StmtResult RebuildObjCAtCatchStmt(SourceLocation AtLoc,
1051 SourceLocation RParenLoc,
1052 VarDecl *Var,
1053 Stmt *Body) {
1054 return getSema().ActOnObjCAtCatchStmt(AtLoc, RParenLoc,
1055 Var, Body);
1058 /// \brief Build a new Objective-C @finally statement.
1060 /// By default, performs semantic analysis to build the new statement.
1061 /// Subclasses may override this routine to provide different behavior.
1062 StmtResult RebuildObjCAtFinallyStmt(SourceLocation AtLoc,
1063 Stmt *Body) {
1064 return getSema().ActOnObjCAtFinallyStmt(AtLoc, Body);
1067 /// \brief Build a new Objective-C @throw statement.
1069 /// By default, performs semantic analysis to build the new statement.
1070 /// Subclasses may override this routine to provide different behavior.
1071 StmtResult RebuildObjCAtThrowStmt(SourceLocation AtLoc,
1072 Expr *Operand) {
1073 return getSema().BuildObjCAtThrowStmt(AtLoc, Operand);
1076 /// \brief Build a new Objective-C @synchronized statement.
1078 /// By default, performs semantic analysis to build the new statement.
1079 /// Subclasses may override this routine to provide different behavior.
1080 StmtResult RebuildObjCAtSynchronizedStmt(SourceLocation AtLoc,
1081 Expr *Object,
1082 Stmt *Body) {
1083 return getSema().ActOnObjCAtSynchronizedStmt(AtLoc, Object,
1084 Body);
1087 /// \brief Build a new Objective-C fast enumeration statement.
1089 /// By default, performs semantic analysis to build the new statement.
1090 /// Subclasses may override this routine to provide different behavior.
1091 StmtResult RebuildObjCForCollectionStmt(SourceLocation ForLoc,
1092 SourceLocation LParenLoc,
1093 Stmt *Element,
1094 Expr *Collection,
1095 SourceLocation RParenLoc,
1096 Stmt *Body) {
1097 return getSema().ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1098 Element,
1099 Collection,
1100 RParenLoc,
1101 Body);
1104 /// \brief Build a new C++ exception declaration.
1106 /// By default, performs semantic analysis to build the new decaration.
1107 /// Subclasses may override this routine to provide different behavior.
1108 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
1109 TypeSourceInfo *Declarator,
1110 IdentifierInfo *Name,
1111 SourceLocation Loc) {
1112 return getSema().BuildExceptionDeclaration(0, Declarator, Name, Loc);
1115 /// \brief Build a new C++ catch statement.
1117 /// By default, performs semantic analysis to build the new statement.
1118 /// Subclasses may override this routine to provide different behavior.
1119 StmtResult RebuildCXXCatchStmt(SourceLocation CatchLoc,
1120 VarDecl *ExceptionDecl,
1121 Stmt *Handler) {
1122 return Owned(new (getSema().Context) CXXCatchStmt(CatchLoc, ExceptionDecl,
1123 Handler));
1126 /// \brief Build a new C++ try statement.
1128 /// By default, performs semantic analysis to build the new statement.
1129 /// Subclasses may override this routine to provide different behavior.
1130 StmtResult RebuildCXXTryStmt(SourceLocation TryLoc,
1131 Stmt *TryBlock,
1132 MultiStmtArg Handlers) {
1133 return getSema().ActOnCXXTryBlock(TryLoc, TryBlock, move(Handlers));
1136 /// \brief Build a new expression that references a declaration.
1138 /// By default, performs semantic analysis to build the new expression.
1139 /// Subclasses may override this routine to provide different behavior.
1140 ExprResult RebuildDeclarationNameExpr(const CXXScopeSpec &SS,
1141 LookupResult &R,
1142 bool RequiresADL) {
1143 return getSema().BuildDeclarationNameExpr(SS, R, RequiresADL);
1147 /// \brief Build a new expression that references a declaration.
1149 /// By default, performs semantic analysis to build the new expression.
1150 /// Subclasses may override this routine to provide different behavior.
1151 ExprResult RebuildDeclRefExpr(NestedNameSpecifier *Qualifier,
1152 SourceRange QualifierRange,
1153 ValueDecl *VD,
1154 const DeclarationNameInfo &NameInfo,
1155 TemplateArgumentListInfo *TemplateArgs) {
1156 CXXScopeSpec SS;
1157 SS.setScopeRep(Qualifier);
1158 SS.setRange(QualifierRange);
1160 // FIXME: loses template args.
1162 return getSema().BuildDeclarationNameExpr(SS, NameInfo, VD);
1165 /// \brief Build a new expression in parentheses.
1167 /// By default, performs semantic analysis to build the new expression.
1168 /// Subclasses may override this routine to provide different behavior.
1169 ExprResult RebuildParenExpr(Expr *SubExpr, SourceLocation LParen,
1170 SourceLocation RParen) {
1171 return getSema().ActOnParenExpr(LParen, RParen, SubExpr);
1174 /// \brief Build a new pseudo-destructor expression.
1176 /// By default, performs semantic analysis to build the new expression.
1177 /// Subclasses may override this routine to provide different behavior.
1178 ExprResult RebuildCXXPseudoDestructorExpr(Expr *Base,
1179 SourceLocation OperatorLoc,
1180 bool isArrow,
1181 NestedNameSpecifier *Qualifier,
1182 SourceRange QualifierRange,
1183 TypeSourceInfo *ScopeType,
1184 SourceLocation CCLoc,
1185 SourceLocation TildeLoc,
1186 PseudoDestructorTypeStorage Destroyed);
1188 /// \brief Build a new unary operator expression.
1190 /// By default, performs semantic analysis to build the new expression.
1191 /// Subclasses may override this routine to provide different behavior.
1192 ExprResult RebuildUnaryOperator(SourceLocation OpLoc,
1193 UnaryOperatorKind Opc,
1194 Expr *SubExpr) {
1195 return getSema().BuildUnaryOp(/*Scope=*/0, OpLoc, Opc, SubExpr);
1198 /// \brief Build a new builtin offsetof expression.
1200 /// By default, performs semantic analysis to build the new expression.
1201 /// Subclasses may override this routine to provide different behavior.
1202 ExprResult RebuildOffsetOfExpr(SourceLocation OperatorLoc,
1203 TypeSourceInfo *Type,
1204 Sema::OffsetOfComponent *Components,
1205 unsigned NumComponents,
1206 SourceLocation RParenLoc) {
1207 return getSema().BuildBuiltinOffsetOf(OperatorLoc, Type, Components,
1208 NumComponents, RParenLoc);
1211 /// \brief Build a new sizeof or alignof expression with a type argument.
1213 /// By default, performs semantic analysis to build the new expression.
1214 /// Subclasses may override this routine to provide different behavior.
1215 ExprResult RebuildSizeOfAlignOf(TypeSourceInfo *TInfo,
1216 SourceLocation OpLoc,
1217 bool isSizeOf, SourceRange R) {
1218 return getSema().CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeOf, R);
1221 /// \brief Build a new sizeof or alignof expression with an expression
1222 /// argument.
1224 /// By default, performs semantic analysis to build the new expression.
1225 /// Subclasses may override this routine to provide different behavior.
1226 ExprResult RebuildSizeOfAlignOf(Expr *SubExpr, SourceLocation OpLoc,
1227 bool isSizeOf, SourceRange R) {
1228 ExprResult Result
1229 = getSema().CreateSizeOfAlignOfExpr(SubExpr, OpLoc, isSizeOf, R);
1230 if (Result.isInvalid())
1231 return ExprError();
1233 return move(Result);
1236 /// \brief Build a new array subscript expression.
1238 /// By default, performs semantic analysis to build the new expression.
1239 /// Subclasses may override this routine to provide different behavior.
1240 ExprResult RebuildArraySubscriptExpr(Expr *LHS,
1241 SourceLocation LBracketLoc,
1242 Expr *RHS,
1243 SourceLocation RBracketLoc) {
1244 return getSema().ActOnArraySubscriptExpr(/*Scope=*/0, LHS,
1245 LBracketLoc, RHS,
1246 RBracketLoc);
1249 /// \brief Build a new call expression.
1251 /// By default, performs semantic analysis to build the new expression.
1252 /// Subclasses may override this routine to provide different behavior.
1253 ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc,
1254 MultiExprArg Args,
1255 SourceLocation RParenLoc) {
1256 return getSema().ActOnCallExpr(/*Scope=*/0, Callee, LParenLoc,
1257 move(Args), RParenLoc);
1260 /// \brief Build a new member access expression.
1262 /// By default, performs semantic analysis to build the new expression.
1263 /// Subclasses may override this routine to provide different behavior.
1264 ExprResult RebuildMemberExpr(Expr *Base, SourceLocation OpLoc,
1265 bool isArrow,
1266 NestedNameSpecifier *Qualifier,
1267 SourceRange QualifierRange,
1268 const DeclarationNameInfo &MemberNameInfo,
1269 ValueDecl *Member,
1270 NamedDecl *FoundDecl,
1271 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1272 NamedDecl *FirstQualifierInScope) {
1273 if (!Member->getDeclName()) {
1274 // We have a reference to an unnamed field. This is always the
1275 // base of an anonymous struct/union member access, i.e. the
1276 // field is always of record type.
1277 assert(!Qualifier && "Can't have an unnamed field with a qualifier!");
1278 assert(Member->getType()->isRecordType() &&
1279 "unnamed member not of record type?");
1281 if (getSema().PerformObjectMemberConversion(Base, Qualifier,
1282 FoundDecl, Member))
1283 return ExprError();
1285 ExprValueKind VK = isArrow ? VK_LValue : Base->getValueKind();
1286 MemberExpr *ME =
1287 new (getSema().Context) MemberExpr(Base, isArrow,
1288 Member, MemberNameInfo,
1289 cast<FieldDecl>(Member)->getType(),
1290 VK, OK_Ordinary);
1291 return getSema().Owned(ME);
1294 CXXScopeSpec SS;
1295 if (Qualifier) {
1296 SS.setRange(QualifierRange);
1297 SS.setScopeRep(Qualifier);
1300 getSema().DefaultFunctionArrayConversion(Base);
1301 QualType BaseType = Base->getType();
1303 // FIXME: this involves duplicating earlier analysis in a lot of
1304 // cases; we should avoid this when possible.
1305 LookupResult R(getSema(), MemberNameInfo, Sema::LookupMemberName);
1306 R.addDecl(FoundDecl);
1307 R.resolveKind();
1309 return getSema().BuildMemberReferenceExpr(Base, BaseType, OpLoc, isArrow,
1310 SS, FirstQualifierInScope,
1311 R, ExplicitTemplateArgs);
1314 /// \brief Build a new binary operator expression.
1316 /// By default, performs semantic analysis to build the new expression.
1317 /// Subclasses may override this routine to provide different behavior.
1318 ExprResult RebuildBinaryOperator(SourceLocation OpLoc,
1319 BinaryOperatorKind Opc,
1320 Expr *LHS, Expr *RHS) {
1321 return getSema().BuildBinOp(/*Scope=*/0, OpLoc, Opc, LHS, RHS);
1324 /// \brief Build a new conditional operator expression.
1326 /// By default, performs semantic analysis to build the new expression.
1327 /// Subclasses may override this routine to provide different behavior.
1328 ExprResult RebuildConditionalOperator(Expr *Cond,
1329 SourceLocation QuestionLoc,
1330 Expr *LHS,
1331 SourceLocation ColonLoc,
1332 Expr *RHS) {
1333 return getSema().ActOnConditionalOp(QuestionLoc, ColonLoc, Cond,
1334 LHS, RHS);
1337 /// \brief Build a new C-style cast expression.
1339 /// By default, performs semantic analysis to build the new expression.
1340 /// Subclasses may override this routine to provide different behavior.
1341 ExprResult RebuildCStyleCastExpr(SourceLocation LParenLoc,
1342 TypeSourceInfo *TInfo,
1343 SourceLocation RParenLoc,
1344 Expr *SubExpr) {
1345 return getSema().BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc,
1346 SubExpr);
1349 /// \brief Build a new compound literal expression.
1351 /// By default, performs semantic analysis to build the new expression.
1352 /// Subclasses may override this routine to provide different behavior.
1353 ExprResult RebuildCompoundLiteralExpr(SourceLocation LParenLoc,
1354 TypeSourceInfo *TInfo,
1355 SourceLocation RParenLoc,
1356 Expr *Init) {
1357 return getSema().BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc,
1358 Init);
1361 /// \brief Build a new extended vector element access expression.
1363 /// By default, performs semantic analysis to build the new expression.
1364 /// Subclasses may override this routine to provide different behavior.
1365 ExprResult RebuildExtVectorElementExpr(Expr *Base,
1366 SourceLocation OpLoc,
1367 SourceLocation AccessorLoc,
1368 IdentifierInfo &Accessor) {
1370 CXXScopeSpec SS;
1371 DeclarationNameInfo NameInfo(&Accessor, AccessorLoc);
1372 return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
1373 OpLoc, /*IsArrow*/ false,
1374 SS, /*FirstQualifierInScope*/ 0,
1375 NameInfo,
1376 /* TemplateArgs */ 0);
1379 /// \brief Build a new initializer list expression.
1381 /// By default, performs semantic analysis to build the new expression.
1382 /// Subclasses may override this routine to provide different behavior.
1383 ExprResult RebuildInitList(SourceLocation LBraceLoc,
1384 MultiExprArg Inits,
1385 SourceLocation RBraceLoc,
1386 QualType ResultTy) {
1387 ExprResult Result
1388 = SemaRef.ActOnInitList(LBraceLoc, move(Inits), RBraceLoc);
1389 if (Result.isInvalid() || ResultTy->isDependentType())
1390 return move(Result);
1392 // Patch in the result type we were given, which may have been computed
1393 // when the initial InitListExpr was built.
1394 InitListExpr *ILE = cast<InitListExpr>((Expr *)Result.get());
1395 ILE->setType(ResultTy);
1396 return move(Result);
1399 /// \brief Build a new designated initializer expression.
1401 /// By default, performs semantic analysis to build the new expression.
1402 /// Subclasses may override this routine to provide different behavior.
1403 ExprResult RebuildDesignatedInitExpr(Designation &Desig,
1404 MultiExprArg ArrayExprs,
1405 SourceLocation EqualOrColonLoc,
1406 bool GNUSyntax,
1407 Expr *Init) {
1408 ExprResult Result
1409 = SemaRef.ActOnDesignatedInitializer(Desig, EqualOrColonLoc, GNUSyntax,
1410 Init);
1411 if (Result.isInvalid())
1412 return ExprError();
1414 ArrayExprs.release();
1415 return move(Result);
1418 /// \brief Build a new value-initialized expression.
1420 /// By default, builds the implicit value initialization without performing
1421 /// any semantic analysis. Subclasses may override this routine to provide
1422 /// different behavior.
1423 ExprResult RebuildImplicitValueInitExpr(QualType T) {
1424 return SemaRef.Owned(new (SemaRef.Context) ImplicitValueInitExpr(T));
1427 /// \brief Build a new \c va_arg expression.
1429 /// By default, performs semantic analysis to build the new expression.
1430 /// Subclasses may override this routine to provide different behavior.
1431 ExprResult RebuildVAArgExpr(SourceLocation BuiltinLoc,
1432 Expr *SubExpr, TypeSourceInfo *TInfo,
1433 SourceLocation RParenLoc) {
1434 return getSema().BuildVAArgExpr(BuiltinLoc,
1435 SubExpr, TInfo,
1436 RParenLoc);
1439 /// \brief Build a new expression list in parentheses.
1441 /// By default, performs semantic analysis to build the new expression.
1442 /// Subclasses may override this routine to provide different behavior.
1443 ExprResult RebuildParenListExpr(SourceLocation LParenLoc,
1444 MultiExprArg SubExprs,
1445 SourceLocation RParenLoc) {
1446 return getSema().ActOnParenOrParenListExpr(LParenLoc, RParenLoc,
1447 move(SubExprs));
1450 /// \brief Build a new address-of-label expression.
1452 /// By default, performs semantic analysis, using the name of the label
1453 /// rather than attempting to map the label statement itself.
1454 /// Subclasses may override this routine to provide different behavior.
1455 ExprResult RebuildAddrLabelExpr(SourceLocation AmpAmpLoc,
1456 SourceLocation LabelLoc,
1457 LabelStmt *Label) {
1458 return getSema().ActOnAddrLabel(AmpAmpLoc, LabelLoc, Label->getID());
1461 /// \brief Build a new GNU statement expression.
1463 /// By default, performs semantic analysis to build the new expression.
1464 /// Subclasses may override this routine to provide different behavior.
1465 ExprResult RebuildStmtExpr(SourceLocation LParenLoc,
1466 Stmt *SubStmt,
1467 SourceLocation RParenLoc) {
1468 return getSema().ActOnStmtExpr(LParenLoc, SubStmt, RParenLoc);
1471 /// \brief Build a new __builtin_choose_expr expression.
1473 /// By default, performs semantic analysis to build the new expression.
1474 /// Subclasses may override this routine to provide different behavior.
1475 ExprResult RebuildChooseExpr(SourceLocation BuiltinLoc,
1476 Expr *Cond, Expr *LHS, Expr *RHS,
1477 SourceLocation RParenLoc) {
1478 return SemaRef.ActOnChooseExpr(BuiltinLoc,
1479 Cond, LHS, RHS,
1480 RParenLoc);
1483 /// \brief Build a new overloaded operator call expression.
1485 /// By default, performs semantic analysis to build the new expression.
1486 /// The semantic analysis provides the behavior of template instantiation,
1487 /// copying with transformations that turn what looks like an overloaded
1488 /// operator call into a use of a builtin operator, performing
1489 /// argument-dependent lookup, etc. Subclasses may override this routine to
1490 /// provide different behavior.
1491 ExprResult RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
1492 SourceLocation OpLoc,
1493 Expr *Callee,
1494 Expr *First,
1495 Expr *Second);
1497 /// \brief Build a new C++ "named" cast expression, such as static_cast or
1498 /// reinterpret_cast.
1500 /// By default, this routine dispatches to one of the more-specific routines
1501 /// for a particular named case, e.g., RebuildCXXStaticCastExpr().
1502 /// Subclasses may override this routine to provide different behavior.
1503 ExprResult RebuildCXXNamedCastExpr(SourceLocation OpLoc,
1504 Stmt::StmtClass Class,
1505 SourceLocation LAngleLoc,
1506 TypeSourceInfo *TInfo,
1507 SourceLocation RAngleLoc,
1508 SourceLocation LParenLoc,
1509 Expr *SubExpr,
1510 SourceLocation RParenLoc) {
1511 switch (Class) {
1512 case Stmt::CXXStaticCastExprClass:
1513 return getDerived().RebuildCXXStaticCastExpr(OpLoc, LAngleLoc, TInfo,
1514 RAngleLoc, LParenLoc,
1515 SubExpr, RParenLoc);
1517 case Stmt::CXXDynamicCastExprClass:
1518 return getDerived().RebuildCXXDynamicCastExpr(OpLoc, LAngleLoc, TInfo,
1519 RAngleLoc, LParenLoc,
1520 SubExpr, RParenLoc);
1522 case Stmt::CXXReinterpretCastExprClass:
1523 return getDerived().RebuildCXXReinterpretCastExpr(OpLoc, LAngleLoc, TInfo,
1524 RAngleLoc, LParenLoc,
1525 SubExpr,
1526 RParenLoc);
1528 case Stmt::CXXConstCastExprClass:
1529 return getDerived().RebuildCXXConstCastExpr(OpLoc, LAngleLoc, TInfo,
1530 RAngleLoc, LParenLoc,
1531 SubExpr, RParenLoc);
1533 default:
1534 assert(false && "Invalid C++ named cast");
1535 break;
1538 return ExprError();
1541 /// \brief Build a new C++ static_cast expression.
1543 /// By default, performs semantic analysis to build the new expression.
1544 /// Subclasses may override this routine to provide different behavior.
1545 ExprResult RebuildCXXStaticCastExpr(SourceLocation OpLoc,
1546 SourceLocation LAngleLoc,
1547 TypeSourceInfo *TInfo,
1548 SourceLocation RAngleLoc,
1549 SourceLocation LParenLoc,
1550 Expr *SubExpr,
1551 SourceLocation RParenLoc) {
1552 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_static_cast,
1553 TInfo, SubExpr,
1554 SourceRange(LAngleLoc, RAngleLoc),
1555 SourceRange(LParenLoc, RParenLoc));
1558 /// \brief Build a new C++ dynamic_cast expression.
1560 /// By default, performs semantic analysis to build the new expression.
1561 /// Subclasses may override this routine to provide different behavior.
1562 ExprResult RebuildCXXDynamicCastExpr(SourceLocation OpLoc,
1563 SourceLocation LAngleLoc,
1564 TypeSourceInfo *TInfo,
1565 SourceLocation RAngleLoc,
1566 SourceLocation LParenLoc,
1567 Expr *SubExpr,
1568 SourceLocation RParenLoc) {
1569 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_dynamic_cast,
1570 TInfo, SubExpr,
1571 SourceRange(LAngleLoc, RAngleLoc),
1572 SourceRange(LParenLoc, RParenLoc));
1575 /// \brief Build a new C++ reinterpret_cast expression.
1577 /// By default, performs semantic analysis to build the new expression.
1578 /// Subclasses may override this routine to provide different behavior.
1579 ExprResult RebuildCXXReinterpretCastExpr(SourceLocation OpLoc,
1580 SourceLocation LAngleLoc,
1581 TypeSourceInfo *TInfo,
1582 SourceLocation RAngleLoc,
1583 SourceLocation LParenLoc,
1584 Expr *SubExpr,
1585 SourceLocation RParenLoc) {
1586 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_reinterpret_cast,
1587 TInfo, SubExpr,
1588 SourceRange(LAngleLoc, RAngleLoc),
1589 SourceRange(LParenLoc, RParenLoc));
1592 /// \brief Build a new C++ const_cast expression.
1594 /// By default, performs semantic analysis to build the new expression.
1595 /// Subclasses may override this routine to provide different behavior.
1596 ExprResult RebuildCXXConstCastExpr(SourceLocation OpLoc,
1597 SourceLocation LAngleLoc,
1598 TypeSourceInfo *TInfo,
1599 SourceLocation RAngleLoc,
1600 SourceLocation LParenLoc,
1601 Expr *SubExpr,
1602 SourceLocation RParenLoc) {
1603 return getSema().BuildCXXNamedCast(OpLoc, tok::kw_const_cast,
1604 TInfo, SubExpr,
1605 SourceRange(LAngleLoc, RAngleLoc),
1606 SourceRange(LParenLoc, RParenLoc));
1609 /// \brief Build a new C++ functional-style cast expression.
1611 /// By default, performs semantic analysis to build the new expression.
1612 /// Subclasses may override this routine to provide different behavior.
1613 ExprResult RebuildCXXFunctionalCastExpr(TypeSourceInfo *TInfo,
1614 SourceLocation LParenLoc,
1615 Expr *Sub,
1616 SourceLocation RParenLoc) {
1617 return getSema().BuildCXXTypeConstructExpr(TInfo, LParenLoc,
1618 MultiExprArg(&Sub, 1),
1619 RParenLoc);
1622 /// \brief Build a new C++ typeid(type) expression.
1624 /// By default, performs semantic analysis to build the new expression.
1625 /// Subclasses may override this routine to provide different behavior.
1626 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
1627 SourceLocation TypeidLoc,
1628 TypeSourceInfo *Operand,
1629 SourceLocation RParenLoc) {
1630 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
1631 RParenLoc);
1635 /// \brief Build a new C++ typeid(expr) expression.
1637 /// By default, performs semantic analysis to build the new expression.
1638 /// Subclasses may override this routine to provide different behavior.
1639 ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType,
1640 SourceLocation TypeidLoc,
1641 Expr *Operand,
1642 SourceLocation RParenLoc) {
1643 return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand,
1644 RParenLoc);
1647 /// \brief Build a new C++ __uuidof(type) expression.
1649 /// By default, performs semantic analysis to build the new expression.
1650 /// Subclasses may override this routine to provide different behavior.
1651 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1652 SourceLocation TypeidLoc,
1653 TypeSourceInfo *Operand,
1654 SourceLocation RParenLoc) {
1655 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1656 RParenLoc);
1659 /// \brief Build a new C++ __uuidof(expr) expression.
1661 /// By default, performs semantic analysis to build the new expression.
1662 /// Subclasses may override this routine to provide different behavior.
1663 ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType,
1664 SourceLocation TypeidLoc,
1665 Expr *Operand,
1666 SourceLocation RParenLoc) {
1667 return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand,
1668 RParenLoc);
1671 /// \brief Build a new C++ "this" expression.
1673 /// By default, builds a new "this" expression without performing any
1674 /// semantic analysis. Subclasses may override this routine to provide
1675 /// different behavior.
1676 ExprResult RebuildCXXThisExpr(SourceLocation ThisLoc,
1677 QualType ThisType,
1678 bool isImplicit) {
1679 return getSema().Owned(
1680 new (getSema().Context) CXXThisExpr(ThisLoc, ThisType,
1681 isImplicit));
1684 /// \brief Build a new C++ throw expression.
1686 /// By default, performs semantic analysis to build the new expression.
1687 /// Subclasses may override this routine to provide different behavior.
1688 ExprResult RebuildCXXThrowExpr(SourceLocation ThrowLoc, Expr *Sub) {
1689 return getSema().ActOnCXXThrow(ThrowLoc, Sub);
1692 /// \brief Build a new C++ default-argument expression.
1694 /// By default, builds a new default-argument expression, which does not
1695 /// require any semantic analysis. Subclasses may override this routine to
1696 /// provide different behavior.
1697 ExprResult RebuildCXXDefaultArgExpr(SourceLocation Loc,
1698 ParmVarDecl *Param) {
1699 return getSema().Owned(CXXDefaultArgExpr::Create(getSema().Context, Loc,
1700 Param));
1703 /// \brief Build a new C++ zero-initialization expression.
1705 /// By default, performs semantic analysis to build the new expression.
1706 /// Subclasses may override this routine to provide different behavior.
1707 ExprResult RebuildCXXScalarValueInitExpr(TypeSourceInfo *TSInfo,
1708 SourceLocation LParenLoc,
1709 SourceLocation RParenLoc) {
1710 return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc,
1711 MultiExprArg(getSema(), 0, 0),
1712 RParenLoc);
1715 /// \brief Build a new C++ "new" expression.
1717 /// By default, performs semantic analysis to build the new expression.
1718 /// Subclasses may override this routine to provide different behavior.
1719 ExprResult RebuildCXXNewExpr(SourceLocation StartLoc,
1720 bool UseGlobal,
1721 SourceLocation PlacementLParen,
1722 MultiExprArg PlacementArgs,
1723 SourceLocation PlacementRParen,
1724 SourceRange TypeIdParens,
1725 QualType AllocatedType,
1726 TypeSourceInfo *AllocatedTypeInfo,
1727 Expr *ArraySize,
1728 SourceLocation ConstructorLParen,
1729 MultiExprArg ConstructorArgs,
1730 SourceLocation ConstructorRParen) {
1731 return getSema().BuildCXXNew(StartLoc, UseGlobal,
1732 PlacementLParen,
1733 move(PlacementArgs),
1734 PlacementRParen,
1735 TypeIdParens,
1736 AllocatedType,
1737 AllocatedTypeInfo,
1738 ArraySize,
1739 ConstructorLParen,
1740 move(ConstructorArgs),
1741 ConstructorRParen);
1744 /// \brief Build a new C++ "delete" expression.
1746 /// By default, performs semantic analysis to build the new expression.
1747 /// Subclasses may override this routine to provide different behavior.
1748 ExprResult RebuildCXXDeleteExpr(SourceLocation StartLoc,
1749 bool IsGlobalDelete,
1750 bool IsArrayForm,
1751 Expr *Operand) {
1752 return getSema().ActOnCXXDelete(StartLoc, IsGlobalDelete, IsArrayForm,
1753 Operand);
1756 /// \brief Build a new unary type trait expression.
1758 /// By default, performs semantic analysis to build the new expression.
1759 /// Subclasses may override this routine to provide different behavior.
1760 ExprResult RebuildUnaryTypeTrait(UnaryTypeTrait Trait,
1761 SourceLocation StartLoc,
1762 TypeSourceInfo *T,
1763 SourceLocation RParenLoc) {
1764 return getSema().BuildUnaryTypeTrait(Trait, StartLoc, T, RParenLoc);
1767 /// \brief Build a new binary type trait expression.
1769 /// By default, performs semantic analysis to build the new expression.
1770 /// Subclasses may override this routine to provide different behavior.
1771 ExprResult RebuildBinaryTypeTrait(BinaryTypeTrait Trait,
1772 SourceLocation StartLoc,
1773 TypeSourceInfo *LhsT,
1774 TypeSourceInfo *RhsT,
1775 SourceLocation RParenLoc) {
1776 return getSema().BuildBinaryTypeTrait(Trait, StartLoc, LhsT, RhsT, RParenLoc);
1779 /// \brief Build a new (previously unresolved) declaration reference
1780 /// expression.
1782 /// By default, performs semantic analysis to build the new expression.
1783 /// Subclasses may override this routine to provide different behavior.
1784 ExprResult RebuildDependentScopeDeclRefExpr(NestedNameSpecifier *NNS,
1785 SourceRange QualifierRange,
1786 const DeclarationNameInfo &NameInfo,
1787 const TemplateArgumentListInfo *TemplateArgs) {
1788 CXXScopeSpec SS;
1789 SS.setRange(QualifierRange);
1790 SS.setScopeRep(NNS);
1792 if (TemplateArgs)
1793 return getSema().BuildQualifiedTemplateIdExpr(SS, NameInfo,
1794 *TemplateArgs);
1796 return getSema().BuildQualifiedDeclarationNameExpr(SS, NameInfo);
1799 /// \brief Build a new template-id expression.
1801 /// By default, performs semantic analysis to build the new expression.
1802 /// Subclasses may override this routine to provide different behavior.
1803 ExprResult RebuildTemplateIdExpr(const CXXScopeSpec &SS,
1804 LookupResult &R,
1805 bool RequiresADL,
1806 const TemplateArgumentListInfo &TemplateArgs) {
1807 return getSema().BuildTemplateIdExpr(SS, R, RequiresADL, TemplateArgs);
1810 /// \brief Build a new object-construction expression.
1812 /// By default, performs semantic analysis to build the new expression.
1813 /// Subclasses may override this routine to provide different behavior.
1814 ExprResult RebuildCXXConstructExpr(QualType T,
1815 SourceLocation Loc,
1816 CXXConstructorDecl *Constructor,
1817 bool IsElidable,
1818 MultiExprArg Args,
1819 bool RequiresZeroInit,
1820 CXXConstructExpr::ConstructionKind ConstructKind,
1821 SourceRange ParenRange) {
1822 ASTOwningVector<Expr*> ConvertedArgs(SemaRef);
1823 if (getSema().CompleteConstructorCall(Constructor, move(Args), Loc,
1824 ConvertedArgs))
1825 return ExprError();
1827 return getSema().BuildCXXConstructExpr(Loc, T, Constructor, IsElidable,
1828 move_arg(ConvertedArgs),
1829 RequiresZeroInit, ConstructKind,
1830 ParenRange);
1833 /// \brief Build a new object-construction expression.
1835 /// By default, performs semantic analysis to build the new expression.
1836 /// Subclasses may override this routine to provide different behavior.
1837 ExprResult RebuildCXXTemporaryObjectExpr(TypeSourceInfo *TSInfo,
1838 SourceLocation LParenLoc,
1839 MultiExprArg Args,
1840 SourceLocation RParenLoc) {
1841 return getSema().BuildCXXTypeConstructExpr(TSInfo,
1842 LParenLoc,
1843 move(Args),
1844 RParenLoc);
1847 /// \brief Build a new object-construction expression.
1849 /// By default, performs semantic analysis to build the new expression.
1850 /// Subclasses may override this routine to provide different behavior.
1851 ExprResult RebuildCXXUnresolvedConstructExpr(TypeSourceInfo *TSInfo,
1852 SourceLocation LParenLoc,
1853 MultiExprArg Args,
1854 SourceLocation RParenLoc) {
1855 return getSema().BuildCXXTypeConstructExpr(TSInfo,
1856 LParenLoc,
1857 move(Args),
1858 RParenLoc);
1861 /// \brief Build a new member reference expression.
1863 /// By default, performs semantic analysis to build the new expression.
1864 /// Subclasses may override this routine to provide different behavior.
1865 ExprResult RebuildCXXDependentScopeMemberExpr(Expr *BaseE,
1866 QualType BaseType,
1867 bool IsArrow,
1868 SourceLocation OperatorLoc,
1869 NestedNameSpecifier *Qualifier,
1870 SourceRange QualifierRange,
1871 NamedDecl *FirstQualifierInScope,
1872 const DeclarationNameInfo &MemberNameInfo,
1873 const TemplateArgumentListInfo *TemplateArgs) {
1874 CXXScopeSpec SS;
1875 SS.setRange(QualifierRange);
1876 SS.setScopeRep(Qualifier);
1878 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
1879 OperatorLoc, IsArrow,
1880 SS, FirstQualifierInScope,
1881 MemberNameInfo,
1882 TemplateArgs);
1885 /// \brief Build a new member reference expression.
1887 /// By default, performs semantic analysis to build the new expression.
1888 /// Subclasses may override this routine to provide different behavior.
1889 ExprResult RebuildUnresolvedMemberExpr(Expr *BaseE,
1890 QualType BaseType,
1891 SourceLocation OperatorLoc,
1892 bool IsArrow,
1893 NestedNameSpecifier *Qualifier,
1894 SourceRange QualifierRange,
1895 NamedDecl *FirstQualifierInScope,
1896 LookupResult &R,
1897 const TemplateArgumentListInfo *TemplateArgs) {
1898 CXXScopeSpec SS;
1899 SS.setRange(QualifierRange);
1900 SS.setScopeRep(Qualifier);
1902 return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType,
1903 OperatorLoc, IsArrow,
1904 SS, FirstQualifierInScope,
1905 R, TemplateArgs);
1908 /// \brief Build a new noexcept expression.
1910 /// By default, performs semantic analysis to build the new expression.
1911 /// Subclasses may override this routine to provide different behavior.
1912 ExprResult RebuildCXXNoexceptExpr(SourceRange Range, Expr *Arg) {
1913 return SemaRef.BuildCXXNoexceptExpr(Range.getBegin(), Arg, Range.getEnd());
1916 /// \brief Build a new Objective-C @encode expression.
1918 /// By default, performs semantic analysis to build the new expression.
1919 /// Subclasses may override this routine to provide different behavior.
1920 ExprResult RebuildObjCEncodeExpr(SourceLocation AtLoc,
1921 TypeSourceInfo *EncodeTypeInfo,
1922 SourceLocation RParenLoc) {
1923 return SemaRef.Owned(SemaRef.BuildObjCEncodeExpression(AtLoc, EncodeTypeInfo,
1924 RParenLoc));
1927 /// \brief Build a new Objective-C class message.
1928 ExprResult RebuildObjCMessageExpr(TypeSourceInfo *ReceiverTypeInfo,
1929 Selector Sel,
1930 SourceLocation SelectorLoc,
1931 ObjCMethodDecl *Method,
1932 SourceLocation LBracLoc,
1933 MultiExprArg Args,
1934 SourceLocation RBracLoc) {
1935 return SemaRef.BuildClassMessage(ReceiverTypeInfo,
1936 ReceiverTypeInfo->getType(),
1937 /*SuperLoc=*/SourceLocation(),
1938 Sel, Method, LBracLoc, SelectorLoc,
1939 RBracLoc, move(Args));
1942 /// \brief Build a new Objective-C instance message.
1943 ExprResult RebuildObjCMessageExpr(Expr *Receiver,
1944 Selector Sel,
1945 SourceLocation SelectorLoc,
1946 ObjCMethodDecl *Method,
1947 SourceLocation LBracLoc,
1948 MultiExprArg Args,
1949 SourceLocation RBracLoc) {
1950 return SemaRef.BuildInstanceMessage(Receiver,
1951 Receiver->getType(),
1952 /*SuperLoc=*/SourceLocation(),
1953 Sel, Method, LBracLoc, SelectorLoc,
1954 RBracLoc, move(Args));
1957 /// \brief Build a new Objective-C ivar reference expression.
1959 /// By default, performs semantic analysis to build the new expression.
1960 /// Subclasses may override this routine to provide different behavior.
1961 ExprResult RebuildObjCIvarRefExpr(Expr *BaseArg, ObjCIvarDecl *Ivar,
1962 SourceLocation IvarLoc,
1963 bool IsArrow, bool IsFreeIvar) {
1964 // FIXME: We lose track of the IsFreeIvar bit.
1965 CXXScopeSpec SS;
1966 Expr *Base = BaseArg;
1967 LookupResult R(getSema(), Ivar->getDeclName(), IvarLoc,
1968 Sema::LookupMemberName);
1969 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
1970 /*FIME:*/IvarLoc,
1971 SS, 0,
1972 false);
1973 if (Result.isInvalid())
1974 return ExprError();
1976 if (Result.get())
1977 return move(Result);
1979 return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
1980 /*FIXME:*/IvarLoc, IsArrow, SS,
1981 /*FirstQualifierInScope=*/0,
1983 /*TemplateArgs=*/0);
1986 /// \brief Build a new Objective-C property reference expression.
1988 /// By default, performs semantic analysis to build the new expression.
1989 /// Subclasses may override this routine to provide different behavior.
1990 ExprResult RebuildObjCPropertyRefExpr(Expr *BaseArg,
1991 ObjCPropertyDecl *Property,
1992 SourceLocation PropertyLoc) {
1993 CXXScopeSpec SS;
1994 Expr *Base = BaseArg;
1995 LookupResult R(getSema(), Property->getDeclName(), PropertyLoc,
1996 Sema::LookupMemberName);
1997 bool IsArrow = false;
1998 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
1999 /*FIME:*/PropertyLoc,
2000 SS, 0, false);
2001 if (Result.isInvalid())
2002 return ExprError();
2004 if (Result.get())
2005 return move(Result);
2007 return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
2008 /*FIXME:*/PropertyLoc, IsArrow,
2009 SS,
2010 /*FirstQualifierInScope=*/0,
2012 /*TemplateArgs=*/0);
2015 /// \brief Build a new Objective-C property reference expression.
2017 /// By default, performs semantic analysis to build the new expression.
2018 /// Subclasses may override this routine to provide different behavior.
2019 ExprResult RebuildObjCPropertyRefExpr(Expr *Base, QualType T,
2020 ObjCMethodDecl *Getter,
2021 ObjCMethodDecl *Setter,
2022 SourceLocation PropertyLoc) {
2023 // Since these expressions can only be value-dependent, we do not
2024 // need to perform semantic analysis again.
2025 return Owned(
2026 new (getSema().Context) ObjCPropertyRefExpr(Getter, Setter, T,
2027 VK_LValue, OK_ObjCProperty,
2028 PropertyLoc, Base));
2031 /// \brief Build a new Objective-C "isa" expression.
2033 /// By default, performs semantic analysis to build the new expression.
2034 /// Subclasses may override this routine to provide different behavior.
2035 ExprResult RebuildObjCIsaExpr(Expr *BaseArg, SourceLocation IsaLoc,
2036 bool IsArrow) {
2037 CXXScopeSpec SS;
2038 Expr *Base = BaseArg;
2039 LookupResult R(getSema(), &getSema().Context.Idents.get("isa"), IsaLoc,
2040 Sema::LookupMemberName);
2041 ExprResult Result = getSema().LookupMemberExpr(R, Base, IsArrow,
2042 /*FIME:*/IsaLoc,
2043 SS, 0, false);
2044 if (Result.isInvalid())
2045 return ExprError();
2047 if (Result.get())
2048 return move(Result);
2050 return getSema().BuildMemberReferenceExpr(Base, Base->getType(),
2051 /*FIXME:*/IsaLoc, IsArrow, SS,
2052 /*FirstQualifierInScope=*/0,
2054 /*TemplateArgs=*/0);
2057 /// \brief Build a new shuffle vector expression.
2059 /// By default, performs semantic analysis to build the new expression.
2060 /// Subclasses may override this routine to provide different behavior.
2061 ExprResult RebuildShuffleVectorExpr(SourceLocation BuiltinLoc,
2062 MultiExprArg SubExprs,
2063 SourceLocation RParenLoc) {
2064 // Find the declaration for __builtin_shufflevector
2065 const IdentifierInfo &Name
2066 = SemaRef.Context.Idents.get("__builtin_shufflevector");
2067 TranslationUnitDecl *TUDecl = SemaRef.Context.getTranslationUnitDecl();
2068 DeclContext::lookup_result Lookup = TUDecl->lookup(DeclarationName(&Name));
2069 assert(Lookup.first != Lookup.second && "No __builtin_shufflevector?");
2071 // Build a reference to the __builtin_shufflevector builtin
2072 FunctionDecl *Builtin = cast<FunctionDecl>(*Lookup.first);
2073 Expr *Callee
2074 = new (SemaRef.Context) DeclRefExpr(Builtin, Builtin->getType(),
2075 VK_LValue, BuiltinLoc);
2076 SemaRef.UsualUnaryConversions(Callee);
2078 // Build the CallExpr
2079 unsigned NumSubExprs = SubExprs.size();
2080 Expr **Subs = (Expr **)SubExprs.release();
2081 CallExpr *TheCall = new (SemaRef.Context) CallExpr(SemaRef.Context, Callee,
2082 Subs, NumSubExprs,
2083 Builtin->getCallResultType(),
2084 Expr::getValueKindForType(Builtin->getResultType()),
2085 RParenLoc);
2086 ExprResult OwnedCall(SemaRef.Owned(TheCall));
2088 // Type-check the __builtin_shufflevector expression.
2089 ExprResult Result = SemaRef.SemaBuiltinShuffleVector(TheCall);
2090 if (Result.isInvalid())
2091 return ExprError();
2093 OwnedCall.release();
2094 return move(Result);
2097 /// \brief Build a new template argument pack expansion.
2099 /// By default, performs semantic analysis to build a new pack expansion
2100 /// for a template argument. Subclasses may override this routine to provide
2101 /// different behavior.
2102 TemplateArgumentLoc RebuildPackExpansion(TemplateArgumentLoc Pattern,
2103 SourceLocation EllipsisLoc) {
2104 switch (Pattern.getArgument().getKind()) {
2105 case TemplateArgument::Expression:
2106 // FIXME: We should be able to handle this now!
2108 case TemplateArgument::Template:
2109 llvm_unreachable("Unsupported pack expansion of expressions/templates");
2111 case TemplateArgument::Null:
2112 case TemplateArgument::Integral:
2113 case TemplateArgument::Declaration:
2114 case TemplateArgument::Pack:
2115 llvm_unreachable("Pack expansion pattern has no parameter packs");
2117 case TemplateArgument::Type:
2118 if (TypeSourceInfo *Expansion
2119 = getSema().CheckPackExpansion(Pattern.getTypeSourceInfo(),
2120 EllipsisLoc))
2121 return TemplateArgumentLoc(TemplateArgument(Expansion->getType()),
2122 Expansion);
2123 break;
2126 return TemplateArgumentLoc();
2129 /// \brief Build a new expression pack expansion.
2131 /// By default, performs semantic analysis to build a new pack expansion
2132 /// for an expression. Subclasses may override this routine to provide
2133 /// different behavior.
2134 ExprResult RebuildPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
2135 return getSema().ActOnPackExpansion(Pattern, EllipsisLoc);
2138 private:
2139 QualType TransformTypeInObjectScope(QualType T,
2140 QualType ObjectType,
2141 NamedDecl *FirstQualifierInScope,
2142 NestedNameSpecifier *Prefix);
2144 TypeSourceInfo *TransformTypeInObjectScope(TypeSourceInfo *T,
2145 QualType ObjectType,
2146 NamedDecl *FirstQualifierInScope,
2147 NestedNameSpecifier *Prefix);
2150 template<typename Derived>
2151 StmtResult TreeTransform<Derived>::TransformStmt(Stmt *S) {
2152 if (!S)
2153 return SemaRef.Owned(S);
2155 switch (S->getStmtClass()) {
2156 case Stmt::NoStmtClass: break;
2158 // Transform individual statement nodes
2159 #define STMT(Node, Parent) \
2160 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(S));
2161 #define EXPR(Node, Parent)
2162 #include "clang/AST/StmtNodes.inc"
2164 // Transform expressions by calling TransformExpr.
2165 #define STMT(Node, Parent)
2166 #define ABSTRACT_STMT(Stmt)
2167 #define EXPR(Node, Parent) case Stmt::Node##Class:
2168 #include "clang/AST/StmtNodes.inc"
2170 ExprResult E = getDerived().TransformExpr(cast<Expr>(S));
2171 if (E.isInvalid())
2172 return StmtError();
2174 return getSema().ActOnExprStmt(getSema().MakeFullExpr(E.take()));
2178 return SemaRef.Owned(S);
2182 template<typename Derived>
2183 ExprResult TreeTransform<Derived>::TransformExpr(Expr *E) {
2184 if (!E)
2185 return SemaRef.Owned(E);
2187 switch (E->getStmtClass()) {
2188 case Stmt::NoStmtClass: break;
2189 #define STMT(Node, Parent) case Stmt::Node##Class: break;
2190 #define ABSTRACT_STMT(Stmt)
2191 #define EXPR(Node, Parent) \
2192 case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(E));
2193 #include "clang/AST/StmtNodes.inc"
2196 return SemaRef.Owned(E);
2199 template<typename Derived>
2200 bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
2201 unsigned NumInputs,
2202 bool IsCall,
2203 llvm::SmallVectorImpl<Expr *> &Outputs,
2204 bool *ArgChanged) {
2205 for (unsigned I = 0; I != NumInputs; ++I) {
2206 // If requested, drop call arguments that need to be dropped.
2207 if (IsCall && getDerived().DropCallArgument(Inputs[I])) {
2208 if (ArgChanged)
2209 *ArgChanged = true;
2211 break;
2214 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(Inputs[I])) {
2215 Expr *Pattern = Expansion->getPattern();
2217 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2218 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
2219 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
2221 // Determine whether the set of unexpanded parameter packs can and should
2222 // be expanded.
2223 bool Expand = true;
2224 unsigned NumExpansions = 0;
2225 if (getDerived().TryExpandParameterPacks(Expansion->getEllipsisLoc(),
2226 Pattern->getSourceRange(),
2227 Unexpanded.data(),
2228 Unexpanded.size(),
2229 Expand, NumExpansions))
2230 return true;
2232 if (!Expand) {
2233 // The transform has determined that we should perform a simple
2234 // transformation on the pack expansion, producing another pack
2235 // expansion.
2236 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
2237 ExprResult OutPattern = getDerived().TransformExpr(Pattern);
2238 if (OutPattern.isInvalid())
2239 return true;
2241 ExprResult Out = getDerived().RebuildPackExpansion(OutPattern.get(),
2242 Expansion->getEllipsisLoc());
2243 if (Out.isInvalid())
2244 return true;
2246 if (ArgChanged)
2247 *ArgChanged = true;
2248 Outputs.push_back(Out.get());
2249 continue;
2252 // The transform has determined that we should perform an elementwise
2253 // expansion of the pattern. Do so.
2254 for (unsigned I = 0; I != NumExpansions; ++I) {
2255 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
2256 ExprResult Out = getDerived().TransformExpr(Pattern);
2257 if (Out.isInvalid())
2258 return true;
2260 if (ArgChanged)
2261 *ArgChanged = true;
2262 Outputs.push_back(Out.get());
2265 continue;
2268 ExprResult Result = getDerived().TransformExpr(Inputs[I]);
2269 if (Result.isInvalid())
2270 return true;
2272 if (Result.get() != Inputs[I] && ArgChanged)
2273 *ArgChanged = true;
2275 Outputs.push_back(Result.get());
2278 return false;
2281 template<typename Derived>
2282 NestedNameSpecifier *
2283 TreeTransform<Derived>::TransformNestedNameSpecifier(NestedNameSpecifier *NNS,
2284 SourceRange Range,
2285 QualType ObjectType,
2286 NamedDecl *FirstQualifierInScope) {
2287 NestedNameSpecifier *Prefix = NNS->getPrefix();
2289 // Transform the prefix of this nested name specifier.
2290 if (Prefix) {
2291 Prefix = getDerived().TransformNestedNameSpecifier(Prefix, Range,
2292 ObjectType,
2293 FirstQualifierInScope);
2294 if (!Prefix)
2295 return 0;
2298 switch (NNS->getKind()) {
2299 case NestedNameSpecifier::Identifier:
2300 if (Prefix) {
2301 // The object type and qualifier-in-scope really apply to the
2302 // leftmost entity.
2303 ObjectType = QualType();
2304 FirstQualifierInScope = 0;
2307 assert((Prefix || !ObjectType.isNull()) &&
2308 "Identifier nested-name-specifier with no prefix or object type");
2309 if (!getDerived().AlwaysRebuild() && Prefix == NNS->getPrefix() &&
2310 ObjectType.isNull())
2311 return NNS;
2313 return getDerived().RebuildNestedNameSpecifier(Prefix, Range,
2314 *NNS->getAsIdentifier(),
2315 ObjectType,
2316 FirstQualifierInScope);
2318 case NestedNameSpecifier::Namespace: {
2319 NamespaceDecl *NS
2320 = cast_or_null<NamespaceDecl>(
2321 getDerived().TransformDecl(Range.getBegin(),
2322 NNS->getAsNamespace()));
2323 if (!getDerived().AlwaysRebuild() &&
2324 Prefix == NNS->getPrefix() &&
2325 NS == NNS->getAsNamespace())
2326 return NNS;
2328 return getDerived().RebuildNestedNameSpecifier(Prefix, Range, NS);
2331 case NestedNameSpecifier::Global:
2332 // There is no meaningful transformation that one could perform on the
2333 // global scope.
2334 return NNS;
2336 case NestedNameSpecifier::TypeSpecWithTemplate:
2337 case NestedNameSpecifier::TypeSpec: {
2338 TemporaryBase Rebase(*this, Range.getBegin(), DeclarationName());
2339 QualType T = TransformTypeInObjectScope(QualType(NNS->getAsType(), 0),
2340 ObjectType,
2341 FirstQualifierInScope,
2342 Prefix);
2343 if (T.isNull())
2344 return 0;
2346 if (!getDerived().AlwaysRebuild() &&
2347 Prefix == NNS->getPrefix() &&
2348 T == QualType(NNS->getAsType(), 0))
2349 return NNS;
2351 return getDerived().RebuildNestedNameSpecifier(Prefix, Range,
2352 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2357 // Required to silence a GCC warning
2358 return 0;
2361 template<typename Derived>
2362 DeclarationNameInfo
2363 TreeTransform<Derived>
2364 ::TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo) {
2365 DeclarationName Name = NameInfo.getName();
2366 if (!Name)
2367 return DeclarationNameInfo();
2369 switch (Name.getNameKind()) {
2370 case DeclarationName::Identifier:
2371 case DeclarationName::ObjCZeroArgSelector:
2372 case DeclarationName::ObjCOneArgSelector:
2373 case DeclarationName::ObjCMultiArgSelector:
2374 case DeclarationName::CXXOperatorName:
2375 case DeclarationName::CXXLiteralOperatorName:
2376 case DeclarationName::CXXUsingDirective:
2377 return NameInfo;
2379 case DeclarationName::CXXConstructorName:
2380 case DeclarationName::CXXDestructorName:
2381 case DeclarationName::CXXConversionFunctionName: {
2382 TypeSourceInfo *NewTInfo;
2383 CanQualType NewCanTy;
2384 if (TypeSourceInfo *OldTInfo = NameInfo.getNamedTypeInfo()) {
2385 NewTInfo = getDerived().TransformType(OldTInfo);
2386 if (!NewTInfo)
2387 return DeclarationNameInfo();
2388 NewCanTy = SemaRef.Context.getCanonicalType(NewTInfo->getType());
2390 else {
2391 NewTInfo = 0;
2392 TemporaryBase Rebase(*this, NameInfo.getLoc(), Name);
2393 QualType NewT = getDerived().TransformType(Name.getCXXNameType());
2394 if (NewT.isNull())
2395 return DeclarationNameInfo();
2396 NewCanTy = SemaRef.Context.getCanonicalType(NewT);
2399 DeclarationName NewName
2400 = SemaRef.Context.DeclarationNames.getCXXSpecialName(Name.getNameKind(),
2401 NewCanTy);
2402 DeclarationNameInfo NewNameInfo(NameInfo);
2403 NewNameInfo.setName(NewName);
2404 NewNameInfo.setNamedTypeInfo(NewTInfo);
2405 return NewNameInfo;
2409 assert(0 && "Unknown name kind.");
2410 return DeclarationNameInfo();
2413 template<typename Derived>
2414 TemplateName
2415 TreeTransform<Derived>::TransformTemplateName(TemplateName Name,
2416 QualType ObjectType,
2417 NamedDecl *FirstQualifierInScope) {
2418 SourceLocation Loc = getDerived().getBaseLocation();
2420 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
2421 NestedNameSpecifier *NNS
2422 = getDerived().TransformNestedNameSpecifier(QTN->getQualifier(),
2423 /*FIXME*/ SourceRange(Loc),
2424 ObjectType,
2425 FirstQualifierInScope);
2426 if (!NNS)
2427 return TemplateName();
2429 if (TemplateDecl *Template = QTN->getTemplateDecl()) {
2430 TemplateDecl *TransTemplate
2431 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(Loc, Template));
2432 if (!TransTemplate)
2433 return TemplateName();
2435 if (!getDerived().AlwaysRebuild() &&
2436 NNS == QTN->getQualifier() &&
2437 TransTemplate == Template)
2438 return Name;
2440 return getDerived().RebuildTemplateName(NNS, QTN->hasTemplateKeyword(),
2441 TransTemplate);
2444 // These should be getting filtered out before they make it into the AST.
2445 llvm_unreachable("overloaded template name survived to here");
2448 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2449 NestedNameSpecifier *NNS = DTN->getQualifier();
2450 if (NNS) {
2451 NNS = getDerived().TransformNestedNameSpecifier(NNS,
2452 /*FIXME:*/SourceRange(Loc),
2453 ObjectType,
2454 FirstQualifierInScope);
2455 if (!NNS) return TemplateName();
2457 // These apply to the scope specifier, not the template.
2458 ObjectType = QualType();
2459 FirstQualifierInScope = 0;
2462 if (!getDerived().AlwaysRebuild() &&
2463 NNS == DTN->getQualifier() &&
2464 ObjectType.isNull())
2465 return Name;
2467 if (DTN->isIdentifier()) {
2468 // FIXME: Bad range
2469 SourceRange QualifierRange(getDerived().getBaseLocation());
2470 return getDerived().RebuildTemplateName(NNS, QualifierRange,
2471 *DTN->getIdentifier(),
2472 ObjectType,
2473 FirstQualifierInScope);
2476 return getDerived().RebuildTemplateName(NNS, DTN->getOperator(),
2477 ObjectType);
2480 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2481 TemplateDecl *TransTemplate
2482 = cast_or_null<TemplateDecl>(getDerived().TransformDecl(Loc, Template));
2483 if (!TransTemplate)
2484 return TemplateName();
2486 if (!getDerived().AlwaysRebuild() &&
2487 TransTemplate == Template)
2488 return Name;
2490 return TemplateName(TransTemplate);
2493 // These should be getting filtered out before they reach the AST.
2494 llvm_unreachable("overloaded function decl survived to here");
2495 return TemplateName();
2498 template<typename Derived>
2499 void TreeTransform<Derived>::InventTemplateArgumentLoc(
2500 const TemplateArgument &Arg,
2501 TemplateArgumentLoc &Output) {
2502 SourceLocation Loc = getDerived().getBaseLocation();
2503 switch (Arg.getKind()) {
2504 case TemplateArgument::Null:
2505 llvm_unreachable("null template argument in TreeTransform");
2506 break;
2508 case TemplateArgument::Type:
2509 Output = TemplateArgumentLoc(Arg,
2510 SemaRef.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
2512 break;
2514 case TemplateArgument::Template:
2515 Output = TemplateArgumentLoc(Arg, SourceRange(), Loc);
2516 break;
2518 case TemplateArgument::Expression:
2519 Output = TemplateArgumentLoc(Arg, Arg.getAsExpr());
2520 break;
2522 case TemplateArgument::Declaration:
2523 case TemplateArgument::Integral:
2524 case TemplateArgument::Pack:
2525 Output = TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
2526 break;
2530 template<typename Derived>
2531 bool TreeTransform<Derived>::TransformTemplateArgument(
2532 const TemplateArgumentLoc &Input,
2533 TemplateArgumentLoc &Output) {
2534 const TemplateArgument &Arg = Input.getArgument();
2535 switch (Arg.getKind()) {
2536 case TemplateArgument::Null:
2537 case TemplateArgument::Integral:
2538 Output = Input;
2539 return false;
2541 case TemplateArgument::Type: {
2542 TypeSourceInfo *DI = Input.getTypeSourceInfo();
2543 if (DI == NULL)
2544 DI = InventTypeSourceInfo(Input.getArgument().getAsType());
2546 DI = getDerived().TransformType(DI);
2547 if (!DI) return true;
2549 Output = TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2550 return false;
2553 case TemplateArgument::Declaration: {
2554 // FIXME: we should never have to transform one of these.
2555 DeclarationName Name;
2556 if (NamedDecl *ND = dyn_cast<NamedDecl>(Arg.getAsDecl()))
2557 Name = ND->getDeclName();
2558 TemporaryBase Rebase(*this, Input.getLocation(), Name);
2559 Decl *D = getDerived().TransformDecl(Input.getLocation(), Arg.getAsDecl());
2560 if (!D) return true;
2562 Expr *SourceExpr = Input.getSourceDeclExpression();
2563 if (SourceExpr) {
2564 EnterExpressionEvaluationContext Unevaluated(getSema(),
2565 Sema::Unevaluated);
2566 ExprResult E = getDerived().TransformExpr(SourceExpr);
2567 SourceExpr = (E.isInvalid() ? 0 : E.take());
2570 Output = TemplateArgumentLoc(TemplateArgument(D), SourceExpr);
2571 return false;
2574 case TemplateArgument::Template: {
2575 TemporaryBase Rebase(*this, Input.getLocation(), DeclarationName());
2576 TemplateName Template
2577 = getDerived().TransformTemplateName(Arg.getAsTemplate());
2578 if (Template.isNull())
2579 return true;
2581 Output = TemplateArgumentLoc(TemplateArgument(Template),
2582 Input.getTemplateQualifierRange(),
2583 Input.getTemplateNameLoc());
2584 return false;
2587 case TemplateArgument::Expression: {
2588 // Template argument expressions are not potentially evaluated.
2589 EnterExpressionEvaluationContext Unevaluated(getSema(),
2590 Sema::Unevaluated);
2592 Expr *InputExpr = Input.getSourceExpression();
2593 if (!InputExpr) InputExpr = Input.getArgument().getAsExpr();
2595 ExprResult E
2596 = getDerived().TransformExpr(InputExpr);
2597 if (E.isInvalid()) return true;
2598 Output = TemplateArgumentLoc(TemplateArgument(E.take()), E.take());
2599 return false;
2602 case TemplateArgument::Pack: {
2603 llvm::SmallVector<TemplateArgument, 4> TransformedArgs;
2604 TransformedArgs.reserve(Arg.pack_size());
2605 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
2606 AEnd = Arg.pack_end();
2607 A != AEnd; ++A) {
2609 // FIXME: preserve source information here when we start
2610 // caring about parameter packs.
2612 TemplateArgumentLoc InputArg;
2613 TemplateArgumentLoc OutputArg;
2614 getDerived().InventTemplateArgumentLoc(*A, InputArg);
2615 if (getDerived().TransformTemplateArgument(InputArg, OutputArg))
2616 return true;
2618 TransformedArgs.push_back(OutputArg.getArgument());
2621 TemplateArgument *TransformedArgsPtr
2622 = new (getSema().Context) TemplateArgument[TransformedArgs.size()];
2623 std::copy(TransformedArgs.begin(), TransformedArgs.end(),
2624 TransformedArgsPtr);
2625 Output = TemplateArgumentLoc(TemplateArgument(TransformedArgsPtr,
2626 TransformedArgs.size()),
2627 Input.getLocInfo());
2628 return false;
2632 // Work around bogus GCC warning
2633 return true;
2636 /// \brief Iterator adaptor that invents template argument location information
2637 /// for each of the template arguments in its underlying iterator.
2638 template<typename Derived, typename InputIterator>
2639 class TemplateArgumentLocInventIterator {
2640 TreeTransform<Derived> &Self;
2641 InputIterator Iter;
2643 public:
2644 typedef TemplateArgumentLoc value_type;
2645 typedef TemplateArgumentLoc reference;
2646 typedef typename std::iterator_traits<InputIterator>::difference_type
2647 difference_type;
2648 typedef std::input_iterator_tag iterator_category;
2650 class pointer {
2651 TemplateArgumentLoc Arg;
2653 public:
2654 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
2656 const TemplateArgumentLoc *operator->() const { return &Arg; }
2659 TemplateArgumentLocInventIterator() { }
2661 explicit TemplateArgumentLocInventIterator(TreeTransform<Derived> &Self,
2662 InputIterator Iter)
2663 : Self(Self), Iter(Iter) { }
2665 TemplateArgumentLocInventIterator &operator++() {
2666 ++Iter;
2667 return *this;
2670 TemplateArgumentLocInventIterator operator++(int) {
2671 TemplateArgumentLocInventIterator Old(*this);
2672 ++(*this);
2673 return Old;
2676 reference operator*() const {
2677 TemplateArgumentLoc Result;
2678 Self.InventTemplateArgumentLoc(*Iter, Result);
2679 return Result;
2682 pointer operator->() const { return pointer(**this); }
2684 friend bool operator==(const TemplateArgumentLocInventIterator &X,
2685 const TemplateArgumentLocInventIterator &Y) {
2686 return X.Iter == Y.Iter;
2689 friend bool operator!=(const TemplateArgumentLocInventIterator &X,
2690 const TemplateArgumentLocInventIterator &Y) {
2691 return X.Iter != Y.Iter;
2695 template<typename Derived>
2696 template<typename InputIterator>
2697 bool TreeTransform<Derived>::TransformTemplateArguments(InputIterator First,
2698 InputIterator Last,
2699 TemplateArgumentListInfo &Outputs) {
2700 for (; First != Last; ++First) {
2701 TemplateArgumentLoc Out;
2702 TemplateArgumentLoc In = *First;
2704 if (In.getArgument().getKind() == TemplateArgument::Pack) {
2705 // Unpack argument packs, which we translate them into separate
2706 // arguments.
2707 // FIXME: We could do much better if we could guarantee that the
2708 // TemplateArgumentLocInfo for the pack expansion would be usable for
2709 // all of the template arguments in the argument pack.
2710 typedef TemplateArgumentLocInventIterator<Derived,
2711 TemplateArgument::pack_iterator>
2712 PackLocIterator;
2713 if (TransformTemplateArguments(PackLocIterator(*this,
2714 In.getArgument().pack_begin()),
2715 PackLocIterator(*this,
2716 In.getArgument().pack_end()),
2717 Outputs))
2718 return true;
2720 continue;
2723 if (In.getArgument().isPackExpansion()) {
2724 // We have a pack expansion, for which we will be substituting into
2725 // the pattern.
2726 SourceLocation Ellipsis;
2727 TemplateArgumentLoc Pattern
2728 = In.getPackExpansionPattern(Ellipsis, getSema().Context);
2730 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2731 getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded);
2732 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
2734 // Determine whether the set of unexpanded parameter packs can and should
2735 // be expanded.
2736 bool Expand = true;
2737 unsigned NumExpansions = 0;
2738 if (getDerived().TryExpandParameterPacks(Ellipsis,
2739 Pattern.getSourceRange(),
2740 Unexpanded.data(),
2741 Unexpanded.size(),
2742 Expand, NumExpansions))
2743 return true;
2745 if (!Expand) {
2746 // The transform has determined that we should perform a simple
2747 // transformation on the pack expansion, producing another pack
2748 // expansion.
2749 TemplateArgumentLoc OutPattern;
2750 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
2751 if (getDerived().TransformTemplateArgument(Pattern, OutPattern))
2752 return true;
2754 Out = getDerived().RebuildPackExpansion(OutPattern, Ellipsis);
2755 if (Out.getArgument().isNull())
2756 return true;
2758 Outputs.addArgument(Out);
2759 continue;
2762 // The transform has determined that we should perform an elementwise
2763 // expansion of the pattern. Do so.
2764 for (unsigned I = 0; I != NumExpansions; ++I) {
2765 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
2767 if (getDerived().TransformTemplateArgument(Pattern, Out))
2768 return true;
2770 Outputs.addArgument(Out);
2773 continue;
2776 // The simple case:
2777 if (getDerived().TransformTemplateArgument(In, Out))
2778 return true;
2780 Outputs.addArgument(Out);
2783 return false;
2787 //===----------------------------------------------------------------------===//
2788 // Type transformation
2789 //===----------------------------------------------------------------------===//
2791 template<typename Derived>
2792 QualType TreeTransform<Derived>::TransformType(QualType T) {
2793 if (getDerived().AlreadyTransformed(T))
2794 return T;
2796 // Temporary workaround. All of these transformations should
2797 // eventually turn into transformations on TypeLocs.
2798 TypeSourceInfo *DI = getSema().Context.CreateTypeSourceInfo(T);
2799 DI->getTypeLoc().initialize(getDerived().getBaseLocation());
2801 TypeSourceInfo *NewDI = getDerived().TransformType(DI);
2803 if (!NewDI)
2804 return QualType();
2806 return NewDI->getType();
2809 template<typename Derived>
2810 TypeSourceInfo *TreeTransform<Derived>::TransformType(TypeSourceInfo *DI) {
2811 if (getDerived().AlreadyTransformed(DI->getType()))
2812 return DI;
2814 TypeLocBuilder TLB;
2816 TypeLoc TL = DI->getTypeLoc();
2817 TLB.reserve(TL.getFullDataSize());
2819 QualType Result = getDerived().TransformType(TLB, TL);
2820 if (Result.isNull())
2821 return 0;
2823 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
2826 template<typename Derived>
2827 QualType
2828 TreeTransform<Derived>::TransformType(TypeLocBuilder &TLB, TypeLoc T) {
2829 switch (T.getTypeLocClass()) {
2830 #define ABSTRACT_TYPELOC(CLASS, PARENT)
2831 #define TYPELOC(CLASS, PARENT) \
2832 case TypeLoc::CLASS: \
2833 return getDerived().Transform##CLASS##Type(TLB, cast<CLASS##TypeLoc>(T));
2834 #include "clang/AST/TypeLocNodes.def"
2837 llvm_unreachable("unhandled type loc!");
2838 return QualType();
2841 /// FIXME: By default, this routine adds type qualifiers only to types
2842 /// that can have qualifiers, and silently suppresses those qualifiers
2843 /// that are not permitted (e.g., qualifiers on reference or function
2844 /// types). This is the right thing for template instantiation, but
2845 /// probably not for other clients.
2846 template<typename Derived>
2847 QualType
2848 TreeTransform<Derived>::TransformQualifiedType(TypeLocBuilder &TLB,
2849 QualifiedTypeLoc T) {
2850 Qualifiers Quals = T.getType().getLocalQualifiers();
2852 QualType Result = getDerived().TransformType(TLB, T.getUnqualifiedLoc());
2853 if (Result.isNull())
2854 return QualType();
2856 // Silently suppress qualifiers if the result type can't be qualified.
2857 // FIXME: this is the right thing for template instantiation, but
2858 // probably not for other clients.
2859 if (Result->isFunctionType() || Result->isReferenceType())
2860 return Result;
2862 if (!Quals.empty()) {
2863 Result = SemaRef.BuildQualifiedType(Result, T.getBeginLoc(), Quals);
2864 TLB.push<QualifiedTypeLoc>(Result);
2865 // No location information to preserve.
2868 return Result;
2871 /// \brief Transforms a type that was written in a scope specifier,
2872 /// given an object type, the results of unqualified lookup, and
2873 /// an already-instantiated prefix.
2875 /// The object type is provided iff the scope specifier qualifies the
2876 /// member of a dependent member-access expression. The prefix is
2877 /// provided iff the the scope specifier in which this appears has a
2878 /// prefix.
2880 /// This is private to TreeTransform.
2881 template<typename Derived>
2882 QualType
2883 TreeTransform<Derived>::TransformTypeInObjectScope(QualType T,
2884 QualType ObjectType,
2885 NamedDecl *UnqualLookup,
2886 NestedNameSpecifier *Prefix) {
2887 if (getDerived().AlreadyTransformed(T))
2888 return T;
2890 TypeSourceInfo *TSI =
2891 SemaRef.Context.getTrivialTypeSourceInfo(T, getBaseLocation());
2893 TSI = getDerived().TransformTypeInObjectScope(TSI, ObjectType,
2894 UnqualLookup, Prefix);
2895 if (!TSI) return QualType();
2896 return TSI->getType();
2899 template<typename Derived>
2900 TypeSourceInfo *
2901 TreeTransform<Derived>::TransformTypeInObjectScope(TypeSourceInfo *TSI,
2902 QualType ObjectType,
2903 NamedDecl *UnqualLookup,
2904 NestedNameSpecifier *Prefix) {
2905 // TODO: in some cases, we might be some verification to do here.
2906 if (ObjectType.isNull())
2907 return getDerived().TransformType(TSI);
2909 QualType T = TSI->getType();
2910 if (getDerived().AlreadyTransformed(T))
2911 return TSI;
2913 TypeLocBuilder TLB;
2914 QualType Result;
2916 if (isa<TemplateSpecializationType>(T)) {
2917 TemplateSpecializationTypeLoc TL
2918 = cast<TemplateSpecializationTypeLoc>(TSI->getTypeLoc());
2920 TemplateName Template =
2921 getDerived().TransformTemplateName(TL.getTypePtr()->getTemplateName(),
2922 ObjectType, UnqualLookup);
2923 if (Template.isNull()) return 0;
2925 Result = getDerived()
2926 .TransformTemplateSpecializationType(TLB, TL, Template);
2927 } else if (isa<DependentTemplateSpecializationType>(T)) {
2928 DependentTemplateSpecializationTypeLoc TL
2929 = cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc());
2931 Result = getDerived()
2932 .TransformDependentTemplateSpecializationType(TLB, TL, Prefix);
2933 } else {
2934 // Nothing special needs to be done for these.
2935 Result = getDerived().TransformType(TLB, TSI->getTypeLoc());
2938 if (Result.isNull()) return 0;
2939 return TLB.getTypeSourceInfo(SemaRef.Context, Result);
2942 template <class TyLoc> static inline
2943 QualType TransformTypeSpecType(TypeLocBuilder &TLB, TyLoc T) {
2944 TyLoc NewT = TLB.push<TyLoc>(T.getType());
2945 NewT.setNameLoc(T.getNameLoc());
2946 return T.getType();
2949 template<typename Derived>
2950 QualType TreeTransform<Derived>::TransformBuiltinType(TypeLocBuilder &TLB,
2951 BuiltinTypeLoc T) {
2952 BuiltinTypeLoc NewT = TLB.push<BuiltinTypeLoc>(T.getType());
2953 NewT.setBuiltinLoc(T.getBuiltinLoc());
2954 if (T.needsExtraLocalData())
2955 NewT.getWrittenBuiltinSpecs() = T.getWrittenBuiltinSpecs();
2956 return T.getType();
2959 template<typename Derived>
2960 QualType TreeTransform<Derived>::TransformComplexType(TypeLocBuilder &TLB,
2961 ComplexTypeLoc T) {
2962 // FIXME: recurse?
2963 return TransformTypeSpecType(TLB, T);
2966 template<typename Derived>
2967 QualType TreeTransform<Derived>::TransformPointerType(TypeLocBuilder &TLB,
2968 PointerTypeLoc TL) {
2969 QualType PointeeType
2970 = getDerived().TransformType(TLB, TL.getPointeeLoc());
2971 if (PointeeType.isNull())
2972 return QualType();
2974 QualType Result = TL.getType();
2975 if (PointeeType->getAs<ObjCObjectType>()) {
2976 // A dependent pointer type 'T *' has is being transformed such
2977 // that an Objective-C class type is being replaced for 'T'. The
2978 // resulting pointer type is an ObjCObjectPointerType, not a
2979 // PointerType.
2980 Result = SemaRef.Context.getObjCObjectPointerType(PointeeType);
2982 ObjCObjectPointerTypeLoc NewT = TLB.push<ObjCObjectPointerTypeLoc>(Result);
2983 NewT.setStarLoc(TL.getStarLoc());
2984 return Result;
2987 if (getDerived().AlwaysRebuild() ||
2988 PointeeType != TL.getPointeeLoc().getType()) {
2989 Result = getDerived().RebuildPointerType(PointeeType, TL.getSigilLoc());
2990 if (Result.isNull())
2991 return QualType();
2994 PointerTypeLoc NewT = TLB.push<PointerTypeLoc>(Result);
2995 NewT.setSigilLoc(TL.getSigilLoc());
2996 return Result;
2999 template<typename Derived>
3000 QualType
3001 TreeTransform<Derived>::TransformBlockPointerType(TypeLocBuilder &TLB,
3002 BlockPointerTypeLoc TL) {
3003 QualType PointeeType
3004 = getDerived().TransformType(TLB, TL.getPointeeLoc());
3005 if (PointeeType.isNull())
3006 return QualType();
3008 QualType Result = TL.getType();
3009 if (getDerived().AlwaysRebuild() ||
3010 PointeeType != TL.getPointeeLoc().getType()) {
3011 Result = getDerived().RebuildBlockPointerType(PointeeType,
3012 TL.getSigilLoc());
3013 if (Result.isNull())
3014 return QualType();
3017 BlockPointerTypeLoc NewT = TLB.push<BlockPointerTypeLoc>(Result);
3018 NewT.setSigilLoc(TL.getSigilLoc());
3019 return Result;
3022 /// Transforms a reference type. Note that somewhat paradoxically we
3023 /// don't care whether the type itself is an l-value type or an r-value
3024 /// type; we only care if the type was *written* as an l-value type
3025 /// or an r-value type.
3026 template<typename Derived>
3027 QualType
3028 TreeTransform<Derived>::TransformReferenceType(TypeLocBuilder &TLB,
3029 ReferenceTypeLoc TL) {
3030 const ReferenceType *T = TL.getTypePtr();
3032 // Note that this works with the pointee-as-written.
3033 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
3034 if (PointeeType.isNull())
3035 return QualType();
3037 QualType Result = TL.getType();
3038 if (getDerived().AlwaysRebuild() ||
3039 PointeeType != T->getPointeeTypeAsWritten()) {
3040 Result = getDerived().RebuildReferenceType(PointeeType,
3041 T->isSpelledAsLValue(),
3042 TL.getSigilLoc());
3043 if (Result.isNull())
3044 return QualType();
3047 // r-value references can be rebuilt as l-value references.
3048 ReferenceTypeLoc NewTL;
3049 if (isa<LValueReferenceType>(Result))
3050 NewTL = TLB.push<LValueReferenceTypeLoc>(Result);
3051 else
3052 NewTL = TLB.push<RValueReferenceTypeLoc>(Result);
3053 NewTL.setSigilLoc(TL.getSigilLoc());
3055 return Result;
3058 template<typename Derived>
3059 QualType
3060 TreeTransform<Derived>::TransformLValueReferenceType(TypeLocBuilder &TLB,
3061 LValueReferenceTypeLoc TL) {
3062 return TransformReferenceType(TLB, TL);
3065 template<typename Derived>
3066 QualType
3067 TreeTransform<Derived>::TransformRValueReferenceType(TypeLocBuilder &TLB,
3068 RValueReferenceTypeLoc TL) {
3069 return TransformReferenceType(TLB, TL);
3072 template<typename Derived>
3073 QualType
3074 TreeTransform<Derived>::TransformMemberPointerType(TypeLocBuilder &TLB,
3075 MemberPointerTypeLoc TL) {
3076 MemberPointerType *T = TL.getTypePtr();
3078 QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
3079 if (PointeeType.isNull())
3080 return QualType();
3082 // TODO: preserve source information for this.
3083 QualType ClassType
3084 = getDerived().TransformType(QualType(T->getClass(), 0));
3085 if (ClassType.isNull())
3086 return QualType();
3088 QualType Result = TL.getType();
3089 if (getDerived().AlwaysRebuild() ||
3090 PointeeType != T->getPointeeType() ||
3091 ClassType != QualType(T->getClass(), 0)) {
3092 Result = getDerived().RebuildMemberPointerType(PointeeType, ClassType,
3093 TL.getStarLoc());
3094 if (Result.isNull())
3095 return QualType();
3098 MemberPointerTypeLoc NewTL = TLB.push<MemberPointerTypeLoc>(Result);
3099 NewTL.setSigilLoc(TL.getSigilLoc());
3101 return Result;
3104 template<typename Derived>
3105 QualType
3106 TreeTransform<Derived>::TransformConstantArrayType(TypeLocBuilder &TLB,
3107 ConstantArrayTypeLoc TL) {
3108 ConstantArrayType *T = TL.getTypePtr();
3109 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3110 if (ElementType.isNull())
3111 return QualType();
3113 QualType Result = TL.getType();
3114 if (getDerived().AlwaysRebuild() ||
3115 ElementType != T->getElementType()) {
3116 Result = getDerived().RebuildConstantArrayType(ElementType,
3117 T->getSizeModifier(),
3118 T->getSize(),
3119 T->getIndexTypeCVRQualifiers(),
3120 TL.getBracketsRange());
3121 if (Result.isNull())
3122 return QualType();
3125 ConstantArrayTypeLoc NewTL = TLB.push<ConstantArrayTypeLoc>(Result);
3126 NewTL.setLBracketLoc(TL.getLBracketLoc());
3127 NewTL.setRBracketLoc(TL.getRBracketLoc());
3129 Expr *Size = TL.getSizeExpr();
3130 if (Size) {
3131 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3132 Size = getDerived().TransformExpr(Size).template takeAs<Expr>();
3134 NewTL.setSizeExpr(Size);
3136 return Result;
3139 template<typename Derived>
3140 QualType TreeTransform<Derived>::TransformIncompleteArrayType(
3141 TypeLocBuilder &TLB,
3142 IncompleteArrayTypeLoc TL) {
3143 IncompleteArrayType *T = TL.getTypePtr();
3144 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3145 if (ElementType.isNull())
3146 return QualType();
3148 QualType Result = TL.getType();
3149 if (getDerived().AlwaysRebuild() ||
3150 ElementType != T->getElementType()) {
3151 Result = getDerived().RebuildIncompleteArrayType(ElementType,
3152 T->getSizeModifier(),
3153 T->getIndexTypeCVRQualifiers(),
3154 TL.getBracketsRange());
3155 if (Result.isNull())
3156 return QualType();
3159 IncompleteArrayTypeLoc NewTL = TLB.push<IncompleteArrayTypeLoc>(Result);
3160 NewTL.setLBracketLoc(TL.getLBracketLoc());
3161 NewTL.setRBracketLoc(TL.getRBracketLoc());
3162 NewTL.setSizeExpr(0);
3164 return Result;
3167 template<typename Derived>
3168 QualType
3169 TreeTransform<Derived>::TransformVariableArrayType(TypeLocBuilder &TLB,
3170 VariableArrayTypeLoc TL) {
3171 VariableArrayType *T = TL.getTypePtr();
3172 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3173 if (ElementType.isNull())
3174 return QualType();
3176 // Array bounds are not potentially evaluated contexts
3177 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3179 ExprResult SizeResult
3180 = getDerived().TransformExpr(T->getSizeExpr());
3181 if (SizeResult.isInvalid())
3182 return QualType();
3184 Expr *Size = SizeResult.take();
3186 QualType Result = TL.getType();
3187 if (getDerived().AlwaysRebuild() ||
3188 ElementType != T->getElementType() ||
3189 Size != T->getSizeExpr()) {
3190 Result = getDerived().RebuildVariableArrayType(ElementType,
3191 T->getSizeModifier(),
3192 Size,
3193 T->getIndexTypeCVRQualifiers(),
3194 TL.getBracketsRange());
3195 if (Result.isNull())
3196 return QualType();
3199 VariableArrayTypeLoc NewTL = TLB.push<VariableArrayTypeLoc>(Result);
3200 NewTL.setLBracketLoc(TL.getLBracketLoc());
3201 NewTL.setRBracketLoc(TL.getRBracketLoc());
3202 NewTL.setSizeExpr(Size);
3204 return Result;
3207 template<typename Derived>
3208 QualType
3209 TreeTransform<Derived>::TransformDependentSizedArrayType(TypeLocBuilder &TLB,
3210 DependentSizedArrayTypeLoc TL) {
3211 DependentSizedArrayType *T = TL.getTypePtr();
3212 QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
3213 if (ElementType.isNull())
3214 return QualType();
3216 // Array bounds are not potentially evaluated contexts
3217 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3219 ExprResult SizeResult
3220 = getDerived().TransformExpr(T->getSizeExpr());
3221 if (SizeResult.isInvalid())
3222 return QualType();
3224 Expr *Size = static_cast<Expr*>(SizeResult.get());
3226 QualType Result = TL.getType();
3227 if (getDerived().AlwaysRebuild() ||
3228 ElementType != T->getElementType() ||
3229 Size != T->getSizeExpr()) {
3230 Result = getDerived().RebuildDependentSizedArrayType(ElementType,
3231 T->getSizeModifier(),
3232 Size,
3233 T->getIndexTypeCVRQualifiers(),
3234 TL.getBracketsRange());
3235 if (Result.isNull())
3236 return QualType();
3238 else SizeResult.take();
3240 // We might have any sort of array type now, but fortunately they
3241 // all have the same location layout.
3242 ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
3243 NewTL.setLBracketLoc(TL.getLBracketLoc());
3244 NewTL.setRBracketLoc(TL.getRBracketLoc());
3245 NewTL.setSizeExpr(Size);
3247 return Result;
3250 template<typename Derived>
3251 QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType(
3252 TypeLocBuilder &TLB,
3253 DependentSizedExtVectorTypeLoc TL) {
3254 DependentSizedExtVectorType *T = TL.getTypePtr();
3256 // FIXME: ext vector locs should be nested
3257 QualType ElementType = getDerived().TransformType(T->getElementType());
3258 if (ElementType.isNull())
3259 return QualType();
3261 // Vector sizes are not potentially evaluated contexts
3262 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3264 ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
3265 if (Size.isInvalid())
3266 return QualType();
3268 QualType Result = TL.getType();
3269 if (getDerived().AlwaysRebuild() ||
3270 ElementType != T->getElementType() ||
3271 Size.get() != T->getSizeExpr()) {
3272 Result = getDerived().RebuildDependentSizedExtVectorType(ElementType,
3273 Size.take(),
3274 T->getAttributeLoc());
3275 if (Result.isNull())
3276 return QualType();
3279 // Result might be dependent or not.
3280 if (isa<DependentSizedExtVectorType>(Result)) {
3281 DependentSizedExtVectorTypeLoc NewTL
3282 = TLB.push<DependentSizedExtVectorTypeLoc>(Result);
3283 NewTL.setNameLoc(TL.getNameLoc());
3284 } else {
3285 ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
3286 NewTL.setNameLoc(TL.getNameLoc());
3289 return Result;
3292 template<typename Derived>
3293 QualType TreeTransform<Derived>::TransformVectorType(TypeLocBuilder &TLB,
3294 VectorTypeLoc TL) {
3295 VectorType *T = TL.getTypePtr();
3296 QualType ElementType = getDerived().TransformType(T->getElementType());
3297 if (ElementType.isNull())
3298 return QualType();
3300 QualType Result = TL.getType();
3301 if (getDerived().AlwaysRebuild() ||
3302 ElementType != T->getElementType()) {
3303 Result = getDerived().RebuildVectorType(ElementType, T->getNumElements(),
3304 T->getVectorKind());
3305 if (Result.isNull())
3306 return QualType();
3309 VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result);
3310 NewTL.setNameLoc(TL.getNameLoc());
3312 return Result;
3315 template<typename Derived>
3316 QualType TreeTransform<Derived>::TransformExtVectorType(TypeLocBuilder &TLB,
3317 ExtVectorTypeLoc TL) {
3318 VectorType *T = TL.getTypePtr();
3319 QualType ElementType = getDerived().TransformType(T->getElementType());
3320 if (ElementType.isNull())
3321 return QualType();
3323 QualType Result = TL.getType();
3324 if (getDerived().AlwaysRebuild() ||
3325 ElementType != T->getElementType()) {
3326 Result = getDerived().RebuildExtVectorType(ElementType,
3327 T->getNumElements(),
3328 /*FIXME*/ SourceLocation());
3329 if (Result.isNull())
3330 return QualType();
3333 ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
3334 NewTL.setNameLoc(TL.getNameLoc());
3336 return Result;
3339 template<typename Derived>
3340 ParmVarDecl *
3341 TreeTransform<Derived>::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
3342 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
3343 TypeSourceInfo *NewDI = getDerived().TransformType(OldDI);
3344 if (!NewDI)
3345 return 0;
3347 if (NewDI == OldDI)
3348 return OldParm;
3349 else
3350 return ParmVarDecl::Create(SemaRef.Context,
3351 OldParm->getDeclContext(),
3352 OldParm->getLocation(),
3353 OldParm->getIdentifier(),
3354 NewDI->getType(),
3355 NewDI,
3356 OldParm->getStorageClass(),
3357 OldParm->getStorageClassAsWritten(),
3358 /* DefArg */ NULL);
3361 template<typename Derived>
3362 bool TreeTransform<Derived>::
3363 TransformFunctionTypeParams(FunctionProtoTypeLoc TL,
3364 llvm::SmallVectorImpl<QualType> &PTypes,
3365 llvm::SmallVectorImpl<ParmVarDecl*> &PVars) {
3366 FunctionProtoType *T = TL.getTypePtr();
3368 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
3369 ParmVarDecl *OldParm = TL.getArg(i);
3371 QualType NewType;
3372 ParmVarDecl *NewParm;
3374 if (OldParm) {
3375 NewParm = getDerived().TransformFunctionTypeParam(OldParm);
3376 if (!NewParm)
3377 return true;
3378 NewType = NewParm->getType();
3380 // Deal with the possibility that we don't have a parameter
3381 // declaration for this parameter.
3382 } else {
3383 NewParm = 0;
3385 QualType OldType = T->getArgType(i);
3386 NewType = getDerived().TransformType(OldType);
3387 if (NewType.isNull())
3388 return true;
3391 PTypes.push_back(NewType);
3392 PVars.push_back(NewParm);
3395 return false;
3398 template<typename Derived>
3399 QualType
3400 TreeTransform<Derived>::TransformFunctionProtoType(TypeLocBuilder &TLB,
3401 FunctionProtoTypeLoc TL) {
3402 // Transform the parameters and return type.
3404 // We instantiate in source order, with the return type first followed by
3405 // the parameters, because users tend to expect this (even if they shouldn't
3406 // rely on it!).
3408 // When the function has a trailing return type, we instantiate the
3409 // parameters before the return type, since the return type can then refer
3410 // to the parameters themselves (via decltype, sizeof, etc.).
3412 llvm::SmallVector<QualType, 4> ParamTypes;
3413 llvm::SmallVector<ParmVarDecl*, 4> ParamDecls;
3414 FunctionProtoType *T = TL.getTypePtr();
3416 QualType ResultType;
3418 if (TL.getTrailingReturn()) {
3419 if (getDerived().TransformFunctionTypeParams(TL, ParamTypes, ParamDecls))
3420 return QualType();
3422 ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
3423 if (ResultType.isNull())
3424 return QualType();
3426 else {
3427 ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
3428 if (ResultType.isNull())
3429 return QualType();
3431 if (getDerived().TransformFunctionTypeParams(TL, ParamTypes, ParamDecls))
3432 return QualType();
3435 QualType Result = TL.getType();
3436 if (getDerived().AlwaysRebuild() ||
3437 ResultType != T->getResultType() ||
3438 !std::equal(T->arg_type_begin(), T->arg_type_end(), ParamTypes.begin())) {
3439 Result = getDerived().RebuildFunctionProtoType(ResultType,
3440 ParamTypes.data(),
3441 ParamTypes.size(),
3442 T->isVariadic(),
3443 T->getTypeQuals(),
3444 T->getExtInfo());
3445 if (Result.isNull())
3446 return QualType();
3449 FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result);
3450 NewTL.setLParenLoc(TL.getLParenLoc());
3451 NewTL.setRParenLoc(TL.getRParenLoc());
3452 NewTL.setTrailingReturn(TL.getTrailingReturn());
3453 for (unsigned i = 0, e = NewTL.getNumArgs(); i != e; ++i)
3454 NewTL.setArg(i, ParamDecls[i]);
3456 return Result;
3459 template<typename Derived>
3460 QualType TreeTransform<Derived>::TransformFunctionNoProtoType(
3461 TypeLocBuilder &TLB,
3462 FunctionNoProtoTypeLoc TL) {
3463 FunctionNoProtoType *T = TL.getTypePtr();
3464 QualType ResultType = getDerived().TransformType(TLB, TL.getResultLoc());
3465 if (ResultType.isNull())
3466 return QualType();
3468 QualType Result = TL.getType();
3469 if (getDerived().AlwaysRebuild() ||
3470 ResultType != T->getResultType())
3471 Result = getDerived().RebuildFunctionNoProtoType(ResultType);
3473 FunctionNoProtoTypeLoc NewTL = TLB.push<FunctionNoProtoTypeLoc>(Result);
3474 NewTL.setLParenLoc(TL.getLParenLoc());
3475 NewTL.setRParenLoc(TL.getRParenLoc());
3476 NewTL.setTrailingReturn(false);
3478 return Result;
3481 template<typename Derived> QualType
3482 TreeTransform<Derived>::TransformUnresolvedUsingType(TypeLocBuilder &TLB,
3483 UnresolvedUsingTypeLoc TL) {
3484 UnresolvedUsingType *T = TL.getTypePtr();
3485 Decl *D = getDerived().TransformDecl(TL.getNameLoc(), T->getDecl());
3486 if (!D)
3487 return QualType();
3489 QualType Result = TL.getType();
3490 if (getDerived().AlwaysRebuild() || D != T->getDecl()) {
3491 Result = getDerived().RebuildUnresolvedUsingType(D);
3492 if (Result.isNull())
3493 return QualType();
3496 // We might get an arbitrary type spec type back. We should at
3497 // least always get a type spec type, though.
3498 TypeSpecTypeLoc NewTL = TLB.pushTypeSpec(Result);
3499 NewTL.setNameLoc(TL.getNameLoc());
3501 return Result;
3504 template<typename Derived>
3505 QualType TreeTransform<Derived>::TransformTypedefType(TypeLocBuilder &TLB,
3506 TypedefTypeLoc TL) {
3507 TypedefType *T = TL.getTypePtr();
3508 TypedefDecl *Typedef
3509 = cast_or_null<TypedefDecl>(getDerived().TransformDecl(TL.getNameLoc(),
3510 T->getDecl()));
3511 if (!Typedef)
3512 return QualType();
3514 QualType Result = TL.getType();
3515 if (getDerived().AlwaysRebuild() ||
3516 Typedef != T->getDecl()) {
3517 Result = getDerived().RebuildTypedefType(Typedef);
3518 if (Result.isNull())
3519 return QualType();
3522 TypedefTypeLoc NewTL = TLB.push<TypedefTypeLoc>(Result);
3523 NewTL.setNameLoc(TL.getNameLoc());
3525 return Result;
3528 template<typename Derived>
3529 QualType TreeTransform<Derived>::TransformTypeOfExprType(TypeLocBuilder &TLB,
3530 TypeOfExprTypeLoc TL) {
3531 // typeof expressions are not potentially evaluated contexts
3532 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3534 ExprResult E = getDerived().TransformExpr(TL.getUnderlyingExpr());
3535 if (E.isInvalid())
3536 return QualType();
3538 QualType Result = TL.getType();
3539 if (getDerived().AlwaysRebuild() ||
3540 E.get() != TL.getUnderlyingExpr()) {
3541 Result = getDerived().RebuildTypeOfExprType(E.get(), TL.getTypeofLoc());
3542 if (Result.isNull())
3543 return QualType();
3545 else E.take();
3547 TypeOfExprTypeLoc NewTL = TLB.push<TypeOfExprTypeLoc>(Result);
3548 NewTL.setTypeofLoc(TL.getTypeofLoc());
3549 NewTL.setLParenLoc(TL.getLParenLoc());
3550 NewTL.setRParenLoc(TL.getRParenLoc());
3552 return Result;
3555 template<typename Derived>
3556 QualType TreeTransform<Derived>::TransformTypeOfType(TypeLocBuilder &TLB,
3557 TypeOfTypeLoc TL) {
3558 TypeSourceInfo* Old_Under_TI = TL.getUnderlyingTInfo();
3559 TypeSourceInfo* New_Under_TI = getDerived().TransformType(Old_Under_TI);
3560 if (!New_Under_TI)
3561 return QualType();
3563 QualType Result = TL.getType();
3564 if (getDerived().AlwaysRebuild() || New_Under_TI != Old_Under_TI) {
3565 Result = getDerived().RebuildTypeOfType(New_Under_TI->getType());
3566 if (Result.isNull())
3567 return QualType();
3570 TypeOfTypeLoc NewTL = TLB.push<TypeOfTypeLoc>(Result);
3571 NewTL.setTypeofLoc(TL.getTypeofLoc());
3572 NewTL.setLParenLoc(TL.getLParenLoc());
3573 NewTL.setRParenLoc(TL.getRParenLoc());
3574 NewTL.setUnderlyingTInfo(New_Under_TI);
3576 return Result;
3579 template<typename Derived>
3580 QualType TreeTransform<Derived>::TransformDecltypeType(TypeLocBuilder &TLB,
3581 DecltypeTypeLoc TL) {
3582 DecltypeType *T = TL.getTypePtr();
3584 // decltype expressions are not potentially evaluated contexts
3585 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3587 ExprResult E = getDerived().TransformExpr(T->getUnderlyingExpr());
3588 if (E.isInvalid())
3589 return QualType();
3591 QualType Result = TL.getType();
3592 if (getDerived().AlwaysRebuild() ||
3593 E.get() != T->getUnderlyingExpr()) {
3594 Result = getDerived().RebuildDecltypeType(E.get(), TL.getNameLoc());
3595 if (Result.isNull())
3596 return QualType();
3598 else E.take();
3600 DecltypeTypeLoc NewTL = TLB.push<DecltypeTypeLoc>(Result);
3601 NewTL.setNameLoc(TL.getNameLoc());
3603 return Result;
3606 template<typename Derived>
3607 QualType TreeTransform<Derived>::TransformRecordType(TypeLocBuilder &TLB,
3608 RecordTypeLoc TL) {
3609 RecordType *T = TL.getTypePtr();
3610 RecordDecl *Record
3611 = cast_or_null<RecordDecl>(getDerived().TransformDecl(TL.getNameLoc(),
3612 T->getDecl()));
3613 if (!Record)
3614 return QualType();
3616 QualType Result = TL.getType();
3617 if (getDerived().AlwaysRebuild() ||
3618 Record != T->getDecl()) {
3619 Result = getDerived().RebuildRecordType(Record);
3620 if (Result.isNull())
3621 return QualType();
3624 RecordTypeLoc NewTL = TLB.push<RecordTypeLoc>(Result);
3625 NewTL.setNameLoc(TL.getNameLoc());
3627 return Result;
3630 template<typename Derived>
3631 QualType TreeTransform<Derived>::TransformEnumType(TypeLocBuilder &TLB,
3632 EnumTypeLoc TL) {
3633 EnumType *T = TL.getTypePtr();
3634 EnumDecl *Enum
3635 = cast_or_null<EnumDecl>(getDerived().TransformDecl(TL.getNameLoc(),
3636 T->getDecl()));
3637 if (!Enum)
3638 return QualType();
3640 QualType Result = TL.getType();
3641 if (getDerived().AlwaysRebuild() ||
3642 Enum != T->getDecl()) {
3643 Result = getDerived().RebuildEnumType(Enum);
3644 if (Result.isNull())
3645 return QualType();
3648 EnumTypeLoc NewTL = TLB.push<EnumTypeLoc>(Result);
3649 NewTL.setNameLoc(TL.getNameLoc());
3651 return Result;
3654 template<typename Derived>
3655 QualType TreeTransform<Derived>::TransformInjectedClassNameType(
3656 TypeLocBuilder &TLB,
3657 InjectedClassNameTypeLoc TL) {
3658 Decl *D = getDerived().TransformDecl(TL.getNameLoc(),
3659 TL.getTypePtr()->getDecl());
3660 if (!D) return QualType();
3662 QualType T = SemaRef.Context.getTypeDeclType(cast<TypeDecl>(D));
3663 TLB.pushTypeSpec(T).setNameLoc(TL.getNameLoc());
3664 return T;
3668 template<typename Derived>
3669 QualType TreeTransform<Derived>::TransformTemplateTypeParmType(
3670 TypeLocBuilder &TLB,
3671 TemplateTypeParmTypeLoc TL) {
3672 return TransformTypeSpecType(TLB, TL);
3675 template<typename Derived>
3676 QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmType(
3677 TypeLocBuilder &TLB,
3678 SubstTemplateTypeParmTypeLoc TL) {
3679 return TransformTypeSpecType(TLB, TL);
3682 template<typename Derived>
3683 QualType TreeTransform<Derived>::TransformTemplateSpecializationType(
3684 TypeLocBuilder &TLB,
3685 TemplateSpecializationTypeLoc TL) {
3686 const TemplateSpecializationType *T = TL.getTypePtr();
3688 TemplateName Template
3689 = getDerived().TransformTemplateName(T->getTemplateName());
3690 if (Template.isNull())
3691 return QualType();
3693 return getDerived().TransformTemplateSpecializationType(TLB, TL, Template);
3696 namespace {
3697 /// \brief Simple iterator that traverses the template arguments in a
3698 /// container that provides a \c getArgLoc() member function.
3700 /// This iterator is intended to be used with the iterator form of
3701 /// \c TreeTransform<Derived>::TransformTemplateArguments().
3702 template<typename ArgLocContainer>
3703 class TemplateArgumentLocContainerIterator {
3704 ArgLocContainer *Container;
3705 unsigned Index;
3707 public:
3708 typedef TemplateArgumentLoc value_type;
3709 typedef TemplateArgumentLoc reference;
3710 typedef int difference_type;
3711 typedef std::input_iterator_tag iterator_category;
3713 class pointer {
3714 TemplateArgumentLoc Arg;
3716 public:
3717 explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { }
3719 const TemplateArgumentLoc *operator->() const {
3720 return &Arg;
3725 TemplateArgumentLocContainerIterator() {}
3727 TemplateArgumentLocContainerIterator(ArgLocContainer &Container,
3728 unsigned Index)
3729 : Container(&Container), Index(Index) { }
3731 TemplateArgumentLocContainerIterator &operator++() {
3732 ++Index;
3733 return *this;
3736 TemplateArgumentLocContainerIterator operator++(int) {
3737 TemplateArgumentLocContainerIterator Old(*this);
3738 ++(*this);
3739 return Old;
3742 TemplateArgumentLoc operator*() const {
3743 return Container->getArgLoc(Index);
3746 pointer operator->() const {
3747 return pointer(Container->getArgLoc(Index));
3750 friend bool operator==(const TemplateArgumentLocContainerIterator &X,
3751 const TemplateArgumentLocContainerIterator &Y) {
3752 return X.Container == Y.Container && X.Index == Y.Index;
3755 friend bool operator!=(const TemplateArgumentLocContainerIterator &X,
3756 const TemplateArgumentLocContainerIterator &Y) {
3757 return !(X == Y);
3763 template <typename Derived>
3764 QualType TreeTransform<Derived>::TransformTemplateSpecializationType(
3765 TypeLocBuilder &TLB,
3766 TemplateSpecializationTypeLoc TL,
3767 TemplateName Template) {
3768 TemplateArgumentListInfo NewTemplateArgs;
3769 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
3770 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
3771 typedef TemplateArgumentLocContainerIterator<TemplateSpecializationTypeLoc>
3772 ArgIterator;
3773 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
3774 ArgIterator(TL, TL.getNumArgs()),
3775 NewTemplateArgs))
3776 return QualType();
3778 // FIXME: maybe don't rebuild if all the template arguments are the same.
3780 QualType Result =
3781 getDerived().RebuildTemplateSpecializationType(Template,
3782 TL.getTemplateNameLoc(),
3783 NewTemplateArgs);
3785 if (!Result.isNull()) {
3786 TemplateSpecializationTypeLoc NewTL
3787 = TLB.push<TemplateSpecializationTypeLoc>(Result);
3788 NewTL.setTemplateNameLoc(TL.getTemplateNameLoc());
3789 NewTL.setLAngleLoc(TL.getLAngleLoc());
3790 NewTL.setRAngleLoc(TL.getRAngleLoc());
3791 for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i)
3792 NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo());
3795 return Result;
3798 template<typename Derived>
3799 QualType
3800 TreeTransform<Derived>::TransformElaboratedType(TypeLocBuilder &TLB,
3801 ElaboratedTypeLoc TL) {
3802 ElaboratedType *T = TL.getTypePtr();
3804 NestedNameSpecifier *NNS = 0;
3805 // NOTE: the qualifier in an ElaboratedType is optional.
3806 if (T->getQualifier() != 0) {
3807 NNS = getDerived().TransformNestedNameSpecifier(T->getQualifier(),
3808 TL.getQualifierRange());
3809 if (!NNS)
3810 return QualType();
3813 QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
3814 if (NamedT.isNull())
3815 return QualType();
3817 QualType Result = TL.getType();
3818 if (getDerived().AlwaysRebuild() ||
3819 NNS != T->getQualifier() ||
3820 NamedT != T->getNamedType()) {
3821 Result = getDerived().RebuildElaboratedType(TL.getKeywordLoc(),
3822 T->getKeyword(), NNS, NamedT);
3823 if (Result.isNull())
3824 return QualType();
3827 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
3828 NewTL.setKeywordLoc(TL.getKeywordLoc());
3829 NewTL.setQualifierRange(TL.getQualifierRange());
3831 return Result;
3834 template<typename Derived>
3835 QualType
3836 TreeTransform<Derived>::TransformParenType(TypeLocBuilder &TLB,
3837 ParenTypeLoc TL) {
3838 QualType Inner = getDerived().TransformType(TLB, TL.getInnerLoc());
3839 if (Inner.isNull())
3840 return QualType();
3842 QualType Result = TL.getType();
3843 if (getDerived().AlwaysRebuild() ||
3844 Inner != TL.getInnerLoc().getType()) {
3845 Result = getDerived().RebuildParenType(Inner);
3846 if (Result.isNull())
3847 return QualType();
3850 ParenTypeLoc NewTL = TLB.push<ParenTypeLoc>(Result);
3851 NewTL.setLParenLoc(TL.getLParenLoc());
3852 NewTL.setRParenLoc(TL.getRParenLoc());
3853 return Result;
3856 template<typename Derived>
3857 QualType TreeTransform<Derived>::TransformDependentNameType(TypeLocBuilder &TLB,
3858 DependentNameTypeLoc TL) {
3859 DependentNameType *T = TL.getTypePtr();
3861 NestedNameSpecifier *NNS
3862 = getDerived().TransformNestedNameSpecifier(T->getQualifier(),
3863 TL.getQualifierRange());
3864 if (!NNS)
3865 return QualType();
3867 QualType Result
3868 = getDerived().RebuildDependentNameType(T->getKeyword(), NNS,
3869 T->getIdentifier(),
3870 TL.getKeywordLoc(),
3871 TL.getQualifierRange(),
3872 TL.getNameLoc());
3873 if (Result.isNull())
3874 return QualType();
3876 if (const ElaboratedType* ElabT = Result->getAs<ElaboratedType>()) {
3877 QualType NamedT = ElabT->getNamedType();
3878 TLB.pushTypeSpec(NamedT).setNameLoc(TL.getNameLoc());
3880 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
3881 NewTL.setKeywordLoc(TL.getKeywordLoc());
3882 NewTL.setQualifierRange(TL.getQualifierRange());
3883 } else {
3884 DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result);
3885 NewTL.setKeywordLoc(TL.getKeywordLoc());
3886 NewTL.setQualifierRange(TL.getQualifierRange());
3887 NewTL.setNameLoc(TL.getNameLoc());
3889 return Result;
3892 template<typename Derived>
3893 QualType TreeTransform<Derived>::
3894 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
3895 DependentTemplateSpecializationTypeLoc TL) {
3896 DependentTemplateSpecializationType *T = TL.getTypePtr();
3898 NestedNameSpecifier *NNS
3899 = getDerived().TransformNestedNameSpecifier(T->getQualifier(),
3900 TL.getQualifierRange());
3901 if (!NNS)
3902 return QualType();
3904 return getDerived()
3905 .TransformDependentTemplateSpecializationType(TLB, TL, NNS);
3908 template<typename Derived>
3909 QualType TreeTransform<Derived>::
3910 TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB,
3911 DependentTemplateSpecializationTypeLoc TL,
3912 NestedNameSpecifier *NNS) {
3913 DependentTemplateSpecializationType *T = TL.getTypePtr();
3915 TemplateArgumentListInfo NewTemplateArgs;
3916 NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc());
3917 NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc());
3919 typedef TemplateArgumentLocContainerIterator<
3920 DependentTemplateSpecializationTypeLoc> ArgIterator;
3921 if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0),
3922 ArgIterator(TL, TL.getNumArgs()),
3923 NewTemplateArgs))
3924 return QualType();
3926 QualType Result
3927 = getDerived().RebuildDependentTemplateSpecializationType(T->getKeyword(),
3928 NNS,
3929 TL.getQualifierRange(),
3930 T->getIdentifier(),
3931 TL.getNameLoc(),
3932 NewTemplateArgs);
3933 if (Result.isNull())
3934 return QualType();
3936 if (const ElaboratedType *ElabT = dyn_cast<ElaboratedType>(Result)) {
3937 QualType NamedT = ElabT->getNamedType();
3939 // Copy information relevant to the template specialization.
3940 TemplateSpecializationTypeLoc NamedTL
3941 = TLB.push<TemplateSpecializationTypeLoc>(NamedT);
3942 NamedTL.setLAngleLoc(TL.getLAngleLoc());
3943 NamedTL.setRAngleLoc(TL.getRAngleLoc());
3944 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3945 NamedTL.setArgLocInfo(I, TL.getArgLocInfo(I));
3947 // Copy information relevant to the elaborated type.
3948 ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result);
3949 NewTL.setKeywordLoc(TL.getKeywordLoc());
3950 NewTL.setQualifierRange(TL.getQualifierRange());
3951 } else {
3952 TypeLoc NewTL(Result, TL.getOpaqueData());
3953 TLB.pushFullCopy(NewTL);
3955 return Result;
3958 template<typename Derived>
3959 QualType TreeTransform<Derived>::TransformPackExpansionType(TypeLocBuilder &TLB,
3960 PackExpansionTypeLoc TL) {
3961 // FIXME: Implement!
3962 getSema().Diag(TL.getEllipsisLoc(),
3963 diag::err_pack_expansion_instantiation_unsupported);
3964 return QualType();
3967 template<typename Derived>
3968 QualType
3969 TreeTransform<Derived>::TransformObjCInterfaceType(TypeLocBuilder &TLB,
3970 ObjCInterfaceTypeLoc TL) {
3971 // ObjCInterfaceType is never dependent.
3972 TLB.pushFullCopy(TL);
3973 return TL.getType();
3976 template<typename Derived>
3977 QualType
3978 TreeTransform<Derived>::TransformObjCObjectType(TypeLocBuilder &TLB,
3979 ObjCObjectTypeLoc TL) {
3980 // ObjCObjectType is never dependent.
3981 TLB.pushFullCopy(TL);
3982 return TL.getType();
3985 template<typename Derived>
3986 QualType
3987 TreeTransform<Derived>::TransformObjCObjectPointerType(TypeLocBuilder &TLB,
3988 ObjCObjectPointerTypeLoc TL) {
3989 // ObjCObjectPointerType is never dependent.
3990 TLB.pushFullCopy(TL);
3991 return TL.getType();
3994 //===----------------------------------------------------------------------===//
3995 // Statement transformation
3996 //===----------------------------------------------------------------------===//
3997 template<typename Derived>
3998 StmtResult
3999 TreeTransform<Derived>::TransformNullStmt(NullStmt *S) {
4000 return SemaRef.Owned(S);
4003 template<typename Derived>
4004 StmtResult
4005 TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S) {
4006 return getDerived().TransformCompoundStmt(S, false);
4009 template<typename Derived>
4010 StmtResult
4011 TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S,
4012 bool IsStmtExpr) {
4013 bool SubStmtInvalid = false;
4014 bool SubStmtChanged = false;
4015 ASTOwningVector<Stmt*> Statements(getSema());
4016 for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end();
4017 B != BEnd; ++B) {
4018 StmtResult Result = getDerived().TransformStmt(*B);
4019 if (Result.isInvalid()) {
4020 // Immediately fail if this was a DeclStmt, since it's very
4021 // likely that this will cause problems for future statements.
4022 if (isa<DeclStmt>(*B))
4023 return StmtError();
4025 // Otherwise, just keep processing substatements and fail later.
4026 SubStmtInvalid = true;
4027 continue;
4030 SubStmtChanged = SubStmtChanged || Result.get() != *B;
4031 Statements.push_back(Result.takeAs<Stmt>());
4034 if (SubStmtInvalid)
4035 return StmtError();
4037 if (!getDerived().AlwaysRebuild() &&
4038 !SubStmtChanged)
4039 return SemaRef.Owned(S);
4041 return getDerived().RebuildCompoundStmt(S->getLBracLoc(),
4042 move_arg(Statements),
4043 S->getRBracLoc(),
4044 IsStmtExpr);
4047 template<typename Derived>
4048 StmtResult
4049 TreeTransform<Derived>::TransformCaseStmt(CaseStmt *S) {
4050 ExprResult LHS, RHS;
4052 // The case value expressions are not potentially evaluated.
4053 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
4055 // Transform the left-hand case value.
4056 LHS = getDerived().TransformExpr(S->getLHS());
4057 if (LHS.isInvalid())
4058 return StmtError();
4060 // Transform the right-hand case value (for the GNU case-range extension).
4061 RHS = getDerived().TransformExpr(S->getRHS());
4062 if (RHS.isInvalid())
4063 return StmtError();
4066 // Build the case statement.
4067 // Case statements are always rebuilt so that they will attached to their
4068 // transformed switch statement.
4069 StmtResult Case = getDerived().RebuildCaseStmt(S->getCaseLoc(),
4070 LHS.get(),
4071 S->getEllipsisLoc(),
4072 RHS.get(),
4073 S->getColonLoc());
4074 if (Case.isInvalid())
4075 return StmtError();
4077 // Transform the statement following the case
4078 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
4079 if (SubStmt.isInvalid())
4080 return StmtError();
4082 // Attach the body to the case statement
4083 return getDerived().RebuildCaseStmtBody(Case.get(), SubStmt.get());
4086 template<typename Derived>
4087 StmtResult
4088 TreeTransform<Derived>::TransformDefaultStmt(DefaultStmt *S) {
4089 // Transform the statement following the default case
4090 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
4091 if (SubStmt.isInvalid())
4092 return StmtError();
4094 // Default statements are always rebuilt
4095 return getDerived().RebuildDefaultStmt(S->getDefaultLoc(), S->getColonLoc(),
4096 SubStmt.get());
4099 template<typename Derived>
4100 StmtResult
4101 TreeTransform<Derived>::TransformLabelStmt(LabelStmt *S) {
4102 StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt());
4103 if (SubStmt.isInvalid())
4104 return StmtError();
4106 // FIXME: Pass the real colon location in.
4107 SourceLocation ColonLoc = SemaRef.PP.getLocForEndOfToken(S->getIdentLoc());
4108 return getDerived().RebuildLabelStmt(S->getIdentLoc(), S->getID(), ColonLoc,
4109 SubStmt.get(), S->HasUnusedAttribute());
4112 template<typename Derived>
4113 StmtResult
4114 TreeTransform<Derived>::TransformIfStmt(IfStmt *S) {
4115 // Transform the condition
4116 ExprResult Cond;
4117 VarDecl *ConditionVar = 0;
4118 if (S->getConditionVariable()) {
4119 ConditionVar
4120 = cast_or_null<VarDecl>(
4121 getDerived().TransformDefinition(
4122 S->getConditionVariable()->getLocation(),
4123 S->getConditionVariable()));
4124 if (!ConditionVar)
4125 return StmtError();
4126 } else {
4127 Cond = getDerived().TransformExpr(S->getCond());
4129 if (Cond.isInvalid())
4130 return StmtError();
4132 // Convert the condition to a boolean value.
4133 if (S->getCond()) {
4134 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getIfLoc(),
4135 Cond.get());
4136 if (CondE.isInvalid())
4137 return StmtError();
4139 Cond = CondE.get();
4143 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
4144 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
4145 return StmtError();
4147 // Transform the "then" branch.
4148 StmtResult Then = getDerived().TransformStmt(S->getThen());
4149 if (Then.isInvalid())
4150 return StmtError();
4152 // Transform the "else" branch.
4153 StmtResult Else = getDerived().TransformStmt(S->getElse());
4154 if (Else.isInvalid())
4155 return StmtError();
4157 if (!getDerived().AlwaysRebuild() &&
4158 FullCond.get() == S->getCond() &&
4159 ConditionVar == S->getConditionVariable() &&
4160 Then.get() == S->getThen() &&
4161 Else.get() == S->getElse())
4162 return SemaRef.Owned(S);
4164 return getDerived().RebuildIfStmt(S->getIfLoc(), FullCond, ConditionVar,
4165 Then.get(),
4166 S->getElseLoc(), Else.get());
4169 template<typename Derived>
4170 StmtResult
4171 TreeTransform<Derived>::TransformSwitchStmt(SwitchStmt *S) {
4172 // Transform the condition.
4173 ExprResult Cond;
4174 VarDecl *ConditionVar = 0;
4175 if (S->getConditionVariable()) {
4176 ConditionVar
4177 = cast_or_null<VarDecl>(
4178 getDerived().TransformDefinition(
4179 S->getConditionVariable()->getLocation(),
4180 S->getConditionVariable()));
4181 if (!ConditionVar)
4182 return StmtError();
4183 } else {
4184 Cond = getDerived().TransformExpr(S->getCond());
4186 if (Cond.isInvalid())
4187 return StmtError();
4190 // Rebuild the switch statement.
4191 StmtResult Switch
4192 = getDerived().RebuildSwitchStmtStart(S->getSwitchLoc(), Cond.get(),
4193 ConditionVar);
4194 if (Switch.isInvalid())
4195 return StmtError();
4197 // Transform the body of the switch statement.
4198 StmtResult Body = getDerived().TransformStmt(S->getBody());
4199 if (Body.isInvalid())
4200 return StmtError();
4202 // Complete the switch statement.
4203 return getDerived().RebuildSwitchStmtBody(S->getSwitchLoc(), Switch.get(),
4204 Body.get());
4207 template<typename Derived>
4208 StmtResult
4209 TreeTransform<Derived>::TransformWhileStmt(WhileStmt *S) {
4210 // Transform the condition
4211 ExprResult Cond;
4212 VarDecl *ConditionVar = 0;
4213 if (S->getConditionVariable()) {
4214 ConditionVar
4215 = cast_or_null<VarDecl>(
4216 getDerived().TransformDefinition(
4217 S->getConditionVariable()->getLocation(),
4218 S->getConditionVariable()));
4219 if (!ConditionVar)
4220 return StmtError();
4221 } else {
4222 Cond = getDerived().TransformExpr(S->getCond());
4224 if (Cond.isInvalid())
4225 return StmtError();
4227 if (S->getCond()) {
4228 // Convert the condition to a boolean value.
4229 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getWhileLoc(),
4230 Cond.get());
4231 if (CondE.isInvalid())
4232 return StmtError();
4233 Cond = CondE;
4237 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
4238 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
4239 return StmtError();
4241 // Transform the body
4242 StmtResult Body = getDerived().TransformStmt(S->getBody());
4243 if (Body.isInvalid())
4244 return StmtError();
4246 if (!getDerived().AlwaysRebuild() &&
4247 FullCond.get() == S->getCond() &&
4248 ConditionVar == S->getConditionVariable() &&
4249 Body.get() == S->getBody())
4250 return Owned(S);
4252 return getDerived().RebuildWhileStmt(S->getWhileLoc(), FullCond,
4253 ConditionVar, Body.get());
4256 template<typename Derived>
4257 StmtResult
4258 TreeTransform<Derived>::TransformDoStmt(DoStmt *S) {
4259 // Transform the body
4260 StmtResult Body = getDerived().TransformStmt(S->getBody());
4261 if (Body.isInvalid())
4262 return StmtError();
4264 // Transform the condition
4265 ExprResult Cond = getDerived().TransformExpr(S->getCond());
4266 if (Cond.isInvalid())
4267 return StmtError();
4269 if (!getDerived().AlwaysRebuild() &&
4270 Cond.get() == S->getCond() &&
4271 Body.get() == S->getBody())
4272 return SemaRef.Owned(S);
4274 return getDerived().RebuildDoStmt(S->getDoLoc(), Body.get(), S->getWhileLoc(),
4275 /*FIXME:*/S->getWhileLoc(), Cond.get(),
4276 S->getRParenLoc());
4279 template<typename Derived>
4280 StmtResult
4281 TreeTransform<Derived>::TransformForStmt(ForStmt *S) {
4282 // Transform the initialization statement
4283 StmtResult Init = getDerived().TransformStmt(S->getInit());
4284 if (Init.isInvalid())
4285 return StmtError();
4287 // Transform the condition
4288 ExprResult Cond;
4289 VarDecl *ConditionVar = 0;
4290 if (S->getConditionVariable()) {
4291 ConditionVar
4292 = cast_or_null<VarDecl>(
4293 getDerived().TransformDefinition(
4294 S->getConditionVariable()->getLocation(),
4295 S->getConditionVariable()));
4296 if (!ConditionVar)
4297 return StmtError();
4298 } else {
4299 Cond = getDerived().TransformExpr(S->getCond());
4301 if (Cond.isInvalid())
4302 return StmtError();
4304 if (S->getCond()) {
4305 // Convert the condition to a boolean value.
4306 ExprResult CondE = getSema().ActOnBooleanCondition(0, S->getForLoc(),
4307 Cond.get());
4308 if (CondE.isInvalid())
4309 return StmtError();
4311 Cond = CondE.get();
4315 Sema::FullExprArg FullCond(getSema().MakeFullExpr(Cond.take()));
4316 if (!S->getConditionVariable() && S->getCond() && !FullCond.get())
4317 return StmtError();
4319 // Transform the increment
4320 ExprResult Inc = getDerived().TransformExpr(S->getInc());
4321 if (Inc.isInvalid())
4322 return StmtError();
4324 Sema::FullExprArg FullInc(getSema().MakeFullExpr(Inc.get()));
4325 if (S->getInc() && !FullInc.get())
4326 return StmtError();
4328 // Transform the body
4329 StmtResult Body = getDerived().TransformStmt(S->getBody());
4330 if (Body.isInvalid())
4331 return StmtError();
4333 if (!getDerived().AlwaysRebuild() &&
4334 Init.get() == S->getInit() &&
4335 FullCond.get() == S->getCond() &&
4336 Inc.get() == S->getInc() &&
4337 Body.get() == S->getBody())
4338 return SemaRef.Owned(S);
4340 return getDerived().RebuildForStmt(S->getForLoc(), S->getLParenLoc(),
4341 Init.get(), FullCond, ConditionVar,
4342 FullInc, S->getRParenLoc(), Body.get());
4345 template<typename Derived>
4346 StmtResult
4347 TreeTransform<Derived>::TransformGotoStmt(GotoStmt *S) {
4348 // Goto statements must always be rebuilt, to resolve the label.
4349 return getDerived().RebuildGotoStmt(S->getGotoLoc(), S->getLabelLoc(),
4350 S->getLabel());
4353 template<typename Derived>
4354 StmtResult
4355 TreeTransform<Derived>::TransformIndirectGotoStmt(IndirectGotoStmt *S) {
4356 ExprResult Target = getDerived().TransformExpr(S->getTarget());
4357 if (Target.isInvalid())
4358 return StmtError();
4360 if (!getDerived().AlwaysRebuild() &&
4361 Target.get() == S->getTarget())
4362 return SemaRef.Owned(S);
4364 return getDerived().RebuildIndirectGotoStmt(S->getGotoLoc(), S->getStarLoc(),
4365 Target.get());
4368 template<typename Derived>
4369 StmtResult
4370 TreeTransform<Derived>::TransformContinueStmt(ContinueStmt *S) {
4371 return SemaRef.Owned(S);
4374 template<typename Derived>
4375 StmtResult
4376 TreeTransform<Derived>::TransformBreakStmt(BreakStmt *S) {
4377 return SemaRef.Owned(S);
4380 template<typename Derived>
4381 StmtResult
4382 TreeTransform<Derived>::TransformReturnStmt(ReturnStmt *S) {
4383 ExprResult Result = getDerived().TransformExpr(S->getRetValue());
4384 if (Result.isInvalid())
4385 return StmtError();
4387 // FIXME: We always rebuild the return statement because there is no way
4388 // to tell whether the return type of the function has changed.
4389 return getDerived().RebuildReturnStmt(S->getReturnLoc(), Result.get());
4392 template<typename Derived>
4393 StmtResult
4394 TreeTransform<Derived>::TransformDeclStmt(DeclStmt *S) {
4395 bool DeclChanged = false;
4396 llvm::SmallVector<Decl *, 4> Decls;
4397 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
4398 D != DEnd; ++D) {
4399 Decl *Transformed = getDerived().TransformDefinition((*D)->getLocation(),
4400 *D);
4401 if (!Transformed)
4402 return StmtError();
4404 if (Transformed != *D)
4405 DeclChanged = true;
4407 Decls.push_back(Transformed);
4410 if (!getDerived().AlwaysRebuild() && !DeclChanged)
4411 return SemaRef.Owned(S);
4413 return getDerived().RebuildDeclStmt(Decls.data(), Decls.size(),
4414 S->getStartLoc(), S->getEndLoc());
4417 template<typename Derived>
4418 StmtResult
4419 TreeTransform<Derived>::TransformSwitchCase(SwitchCase *S) {
4420 assert(false && "SwitchCase is abstract and cannot be transformed");
4421 return SemaRef.Owned(S);
4424 template<typename Derived>
4425 StmtResult
4426 TreeTransform<Derived>::TransformAsmStmt(AsmStmt *S) {
4428 ASTOwningVector<Expr*> Constraints(getSema());
4429 ASTOwningVector<Expr*> Exprs(getSema());
4430 llvm::SmallVector<IdentifierInfo *, 4> Names;
4432 ExprResult AsmString;
4433 ASTOwningVector<Expr*> Clobbers(getSema());
4435 bool ExprsChanged = false;
4437 // Go through the outputs.
4438 for (unsigned I = 0, E = S->getNumOutputs(); I != E; ++I) {
4439 Names.push_back(S->getOutputIdentifier(I));
4441 // No need to transform the constraint literal.
4442 Constraints.push_back(S->getOutputConstraintLiteral(I));
4444 // Transform the output expr.
4445 Expr *OutputExpr = S->getOutputExpr(I);
4446 ExprResult Result = getDerived().TransformExpr(OutputExpr);
4447 if (Result.isInvalid())
4448 return StmtError();
4450 ExprsChanged |= Result.get() != OutputExpr;
4452 Exprs.push_back(Result.get());
4455 // Go through the inputs.
4456 for (unsigned I = 0, E = S->getNumInputs(); I != E; ++I) {
4457 Names.push_back(S->getInputIdentifier(I));
4459 // No need to transform the constraint literal.
4460 Constraints.push_back(S->getInputConstraintLiteral(I));
4462 // Transform the input expr.
4463 Expr *InputExpr = S->getInputExpr(I);
4464 ExprResult Result = getDerived().TransformExpr(InputExpr);
4465 if (Result.isInvalid())
4466 return StmtError();
4468 ExprsChanged |= Result.get() != InputExpr;
4470 Exprs.push_back(Result.get());
4473 if (!getDerived().AlwaysRebuild() && !ExprsChanged)
4474 return SemaRef.Owned(S);
4476 // Go through the clobbers.
4477 for (unsigned I = 0, E = S->getNumClobbers(); I != E; ++I)
4478 Clobbers.push_back(S->getClobber(I));
4480 // No need to transform the asm string literal.
4481 AsmString = SemaRef.Owned(S->getAsmString());
4483 return getDerived().RebuildAsmStmt(S->getAsmLoc(),
4484 S->isSimple(),
4485 S->isVolatile(),
4486 S->getNumOutputs(),
4487 S->getNumInputs(),
4488 Names.data(),
4489 move_arg(Constraints),
4490 move_arg(Exprs),
4491 AsmString.get(),
4492 move_arg(Clobbers),
4493 S->getRParenLoc(),
4494 S->isMSAsm());
4498 template<typename Derived>
4499 StmtResult
4500 TreeTransform<Derived>::TransformObjCAtTryStmt(ObjCAtTryStmt *S) {
4501 // Transform the body of the @try.
4502 StmtResult TryBody = getDerived().TransformStmt(S->getTryBody());
4503 if (TryBody.isInvalid())
4504 return StmtError();
4506 // Transform the @catch statements (if present).
4507 bool AnyCatchChanged = false;
4508 ASTOwningVector<Stmt*> CatchStmts(SemaRef);
4509 for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
4510 StmtResult Catch = getDerived().TransformStmt(S->getCatchStmt(I));
4511 if (Catch.isInvalid())
4512 return StmtError();
4513 if (Catch.get() != S->getCatchStmt(I))
4514 AnyCatchChanged = true;
4515 CatchStmts.push_back(Catch.release());
4518 // Transform the @finally statement (if present).
4519 StmtResult Finally;
4520 if (S->getFinallyStmt()) {
4521 Finally = getDerived().TransformStmt(S->getFinallyStmt());
4522 if (Finally.isInvalid())
4523 return StmtError();
4526 // If nothing changed, just retain this statement.
4527 if (!getDerived().AlwaysRebuild() &&
4528 TryBody.get() == S->getTryBody() &&
4529 !AnyCatchChanged &&
4530 Finally.get() == S->getFinallyStmt())
4531 return SemaRef.Owned(S);
4533 // Build a new statement.
4534 return getDerived().RebuildObjCAtTryStmt(S->getAtTryLoc(), TryBody.get(),
4535 move_arg(CatchStmts), Finally.get());
4538 template<typename Derived>
4539 StmtResult
4540 TreeTransform<Derived>::TransformObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4541 // Transform the @catch parameter, if there is one.
4542 VarDecl *Var = 0;
4543 if (VarDecl *FromVar = S->getCatchParamDecl()) {
4544 TypeSourceInfo *TSInfo = 0;
4545 if (FromVar->getTypeSourceInfo()) {
4546 TSInfo = getDerived().TransformType(FromVar->getTypeSourceInfo());
4547 if (!TSInfo)
4548 return StmtError();
4551 QualType T;
4552 if (TSInfo)
4553 T = TSInfo->getType();
4554 else {
4555 T = getDerived().TransformType(FromVar->getType());
4556 if (T.isNull())
4557 return StmtError();
4560 Var = getDerived().RebuildObjCExceptionDecl(FromVar, TSInfo, T);
4561 if (!Var)
4562 return StmtError();
4565 StmtResult Body = getDerived().TransformStmt(S->getCatchBody());
4566 if (Body.isInvalid())
4567 return StmtError();
4569 return getDerived().RebuildObjCAtCatchStmt(S->getAtCatchLoc(),
4570 S->getRParenLoc(),
4571 Var, Body.get());
4574 template<typename Derived>
4575 StmtResult
4576 TreeTransform<Derived>::TransformObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4577 // Transform the body.
4578 StmtResult Body = getDerived().TransformStmt(S->getFinallyBody());
4579 if (Body.isInvalid())
4580 return StmtError();
4582 // If nothing changed, just retain this statement.
4583 if (!getDerived().AlwaysRebuild() &&
4584 Body.get() == S->getFinallyBody())
4585 return SemaRef.Owned(S);
4587 // Build a new statement.
4588 return getDerived().RebuildObjCAtFinallyStmt(S->getAtFinallyLoc(),
4589 Body.get());
4592 template<typename Derived>
4593 StmtResult
4594 TreeTransform<Derived>::TransformObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4595 ExprResult Operand;
4596 if (S->getThrowExpr()) {
4597 Operand = getDerived().TransformExpr(S->getThrowExpr());
4598 if (Operand.isInvalid())
4599 return StmtError();
4602 if (!getDerived().AlwaysRebuild() &&
4603 Operand.get() == S->getThrowExpr())
4604 return getSema().Owned(S);
4606 return getDerived().RebuildObjCAtThrowStmt(S->getThrowLoc(), Operand.get());
4609 template<typename Derived>
4610 StmtResult
4611 TreeTransform<Derived>::TransformObjCAtSynchronizedStmt(
4612 ObjCAtSynchronizedStmt *S) {
4613 // Transform the object we are locking.
4614 ExprResult Object = getDerived().TransformExpr(S->getSynchExpr());
4615 if (Object.isInvalid())
4616 return StmtError();
4618 // Transform the body.
4619 StmtResult Body = getDerived().TransformStmt(S->getSynchBody());
4620 if (Body.isInvalid())
4621 return StmtError();
4623 // If nothing change, just retain the current statement.
4624 if (!getDerived().AlwaysRebuild() &&
4625 Object.get() == S->getSynchExpr() &&
4626 Body.get() == S->getSynchBody())
4627 return SemaRef.Owned(S);
4629 // Build a new statement.
4630 return getDerived().RebuildObjCAtSynchronizedStmt(S->getAtSynchronizedLoc(),
4631 Object.get(), Body.get());
4634 template<typename Derived>
4635 StmtResult
4636 TreeTransform<Derived>::TransformObjCForCollectionStmt(
4637 ObjCForCollectionStmt *S) {
4638 // Transform the element statement.
4639 StmtResult Element = getDerived().TransformStmt(S->getElement());
4640 if (Element.isInvalid())
4641 return StmtError();
4643 // Transform the collection expression.
4644 ExprResult Collection = getDerived().TransformExpr(S->getCollection());
4645 if (Collection.isInvalid())
4646 return StmtError();
4648 // Transform the body.
4649 StmtResult Body = getDerived().TransformStmt(S->getBody());
4650 if (Body.isInvalid())
4651 return StmtError();
4653 // If nothing changed, just retain this statement.
4654 if (!getDerived().AlwaysRebuild() &&
4655 Element.get() == S->getElement() &&
4656 Collection.get() == S->getCollection() &&
4657 Body.get() == S->getBody())
4658 return SemaRef.Owned(S);
4660 // Build a new statement.
4661 return getDerived().RebuildObjCForCollectionStmt(S->getForLoc(),
4662 /*FIXME:*/S->getForLoc(),
4663 Element.get(),
4664 Collection.get(),
4665 S->getRParenLoc(),
4666 Body.get());
4670 template<typename Derived>
4671 StmtResult
4672 TreeTransform<Derived>::TransformCXXCatchStmt(CXXCatchStmt *S) {
4673 // Transform the exception declaration, if any.
4674 VarDecl *Var = 0;
4675 if (S->getExceptionDecl()) {
4676 VarDecl *ExceptionDecl = S->getExceptionDecl();
4677 TypeSourceInfo *T = getDerived().TransformType(
4678 ExceptionDecl->getTypeSourceInfo());
4679 if (!T)
4680 return StmtError();
4682 Var = getDerived().RebuildExceptionDecl(ExceptionDecl, T,
4683 ExceptionDecl->getIdentifier(),
4684 ExceptionDecl->getLocation());
4685 if (!Var || Var->isInvalidDecl())
4686 return StmtError();
4689 // Transform the actual exception handler.
4690 StmtResult Handler = getDerived().TransformStmt(S->getHandlerBlock());
4691 if (Handler.isInvalid())
4692 return StmtError();
4694 if (!getDerived().AlwaysRebuild() &&
4695 !Var &&
4696 Handler.get() == S->getHandlerBlock())
4697 return SemaRef.Owned(S);
4699 return getDerived().RebuildCXXCatchStmt(S->getCatchLoc(),
4700 Var,
4701 Handler.get());
4704 template<typename Derived>
4705 StmtResult
4706 TreeTransform<Derived>::TransformCXXTryStmt(CXXTryStmt *S) {
4707 // Transform the try block itself.
4708 StmtResult TryBlock
4709 = getDerived().TransformCompoundStmt(S->getTryBlock());
4710 if (TryBlock.isInvalid())
4711 return StmtError();
4713 // Transform the handlers.
4714 bool HandlerChanged = false;
4715 ASTOwningVector<Stmt*> Handlers(SemaRef);
4716 for (unsigned I = 0, N = S->getNumHandlers(); I != N; ++I) {
4717 StmtResult Handler
4718 = getDerived().TransformCXXCatchStmt(S->getHandler(I));
4719 if (Handler.isInvalid())
4720 return StmtError();
4722 HandlerChanged = HandlerChanged || Handler.get() != S->getHandler(I);
4723 Handlers.push_back(Handler.takeAs<Stmt>());
4726 if (!getDerived().AlwaysRebuild() &&
4727 TryBlock.get() == S->getTryBlock() &&
4728 !HandlerChanged)
4729 return SemaRef.Owned(S);
4731 return getDerived().RebuildCXXTryStmt(S->getTryLoc(), TryBlock.get(),
4732 move_arg(Handlers));
4735 //===----------------------------------------------------------------------===//
4736 // Expression transformation
4737 //===----------------------------------------------------------------------===//
4738 template<typename Derived>
4739 ExprResult
4740 TreeTransform<Derived>::TransformPredefinedExpr(PredefinedExpr *E) {
4741 return SemaRef.Owned(E);
4744 template<typename Derived>
4745 ExprResult
4746 TreeTransform<Derived>::TransformDeclRefExpr(DeclRefExpr *E) {
4747 NestedNameSpecifier *Qualifier = 0;
4748 if (E->getQualifier()) {
4749 Qualifier = getDerived().TransformNestedNameSpecifier(E->getQualifier(),
4750 E->getQualifierRange());
4751 if (!Qualifier)
4752 return ExprError();
4755 ValueDecl *ND
4756 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getLocation(),
4757 E->getDecl()));
4758 if (!ND)
4759 return ExprError();
4761 DeclarationNameInfo NameInfo = E->getNameInfo();
4762 if (NameInfo.getName()) {
4763 NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo);
4764 if (!NameInfo.getName())
4765 return ExprError();
4768 if (!getDerived().AlwaysRebuild() &&
4769 Qualifier == E->getQualifier() &&
4770 ND == E->getDecl() &&
4771 NameInfo.getName() == E->getDecl()->getDeclName() &&
4772 !E->hasExplicitTemplateArgs()) {
4774 // Mark it referenced in the new context regardless.
4775 // FIXME: this is a bit instantiation-specific.
4776 SemaRef.MarkDeclarationReferenced(E->getLocation(), ND);
4778 return SemaRef.Owned(E);
4781 TemplateArgumentListInfo TransArgs, *TemplateArgs = 0;
4782 if (E->hasExplicitTemplateArgs()) {
4783 TemplateArgs = &TransArgs;
4784 TransArgs.setLAngleLoc(E->getLAngleLoc());
4785 TransArgs.setRAngleLoc(E->getRAngleLoc());
4786 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
4787 E->getNumTemplateArgs(),
4788 TransArgs))
4789 return ExprError();
4792 return getDerived().RebuildDeclRefExpr(Qualifier, E->getQualifierRange(),
4793 ND, NameInfo, TemplateArgs);
4796 template<typename Derived>
4797 ExprResult
4798 TreeTransform<Derived>::TransformIntegerLiteral(IntegerLiteral *E) {
4799 return SemaRef.Owned(E);
4802 template<typename Derived>
4803 ExprResult
4804 TreeTransform<Derived>::TransformFloatingLiteral(FloatingLiteral *E) {
4805 return SemaRef.Owned(E);
4808 template<typename Derived>
4809 ExprResult
4810 TreeTransform<Derived>::TransformImaginaryLiteral(ImaginaryLiteral *E) {
4811 return SemaRef.Owned(E);
4814 template<typename Derived>
4815 ExprResult
4816 TreeTransform<Derived>::TransformStringLiteral(StringLiteral *E) {
4817 return SemaRef.Owned(E);
4820 template<typename Derived>
4821 ExprResult
4822 TreeTransform<Derived>::TransformCharacterLiteral(CharacterLiteral *E) {
4823 return SemaRef.Owned(E);
4826 template<typename Derived>
4827 ExprResult
4828 TreeTransform<Derived>::TransformParenExpr(ParenExpr *E) {
4829 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
4830 if (SubExpr.isInvalid())
4831 return ExprError();
4833 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr())
4834 return SemaRef.Owned(E);
4836 return getDerived().RebuildParenExpr(SubExpr.get(), E->getLParen(),
4837 E->getRParen());
4840 template<typename Derived>
4841 ExprResult
4842 TreeTransform<Derived>::TransformUnaryOperator(UnaryOperator *E) {
4843 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
4844 if (SubExpr.isInvalid())
4845 return ExprError();
4847 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr())
4848 return SemaRef.Owned(E);
4850 return getDerived().RebuildUnaryOperator(E->getOperatorLoc(),
4851 E->getOpcode(),
4852 SubExpr.get());
4855 template<typename Derived>
4856 ExprResult
4857 TreeTransform<Derived>::TransformOffsetOfExpr(OffsetOfExpr *E) {
4858 // Transform the type.
4859 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeSourceInfo());
4860 if (!Type)
4861 return ExprError();
4863 // Transform all of the components into components similar to what the
4864 // parser uses.
4865 // FIXME: It would be slightly more efficient in the non-dependent case to
4866 // just map FieldDecls, rather than requiring the rebuilder to look for
4867 // the fields again. However, __builtin_offsetof is rare enough in
4868 // template code that we don't care.
4869 bool ExprChanged = false;
4870 typedef Sema::OffsetOfComponent Component;
4871 typedef OffsetOfExpr::OffsetOfNode Node;
4872 llvm::SmallVector<Component, 4> Components;
4873 for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
4874 const Node &ON = E->getComponent(I);
4875 Component Comp;
4876 Comp.isBrackets = true;
4877 Comp.LocStart = ON.getRange().getBegin();
4878 Comp.LocEnd = ON.getRange().getEnd();
4879 switch (ON.getKind()) {
4880 case Node::Array: {
4881 Expr *FromIndex = E->getIndexExpr(ON.getArrayExprIndex());
4882 ExprResult Index = getDerived().TransformExpr(FromIndex);
4883 if (Index.isInvalid())
4884 return ExprError();
4886 ExprChanged = ExprChanged || Index.get() != FromIndex;
4887 Comp.isBrackets = true;
4888 Comp.U.E = Index.get();
4889 break;
4892 case Node::Field:
4893 case Node::Identifier:
4894 Comp.isBrackets = false;
4895 Comp.U.IdentInfo = ON.getFieldName();
4896 if (!Comp.U.IdentInfo)
4897 continue;
4899 break;
4901 case Node::Base:
4902 // Will be recomputed during the rebuild.
4903 continue;
4906 Components.push_back(Comp);
4909 // If nothing changed, retain the existing expression.
4910 if (!getDerived().AlwaysRebuild() &&
4911 Type == E->getTypeSourceInfo() &&
4912 !ExprChanged)
4913 return SemaRef.Owned(E);
4915 // Build a new offsetof expression.
4916 return getDerived().RebuildOffsetOfExpr(E->getOperatorLoc(), Type,
4917 Components.data(), Components.size(),
4918 E->getRParenLoc());
4921 template<typename Derived>
4922 ExprResult
4923 TreeTransform<Derived>::TransformOpaqueValueExpr(OpaqueValueExpr *E) {
4924 assert(getDerived().AlreadyTransformed(E->getType()) &&
4925 "opaque value expression requires transformation");
4926 return SemaRef.Owned(E);
4929 template<typename Derived>
4930 ExprResult
4931 TreeTransform<Derived>::TransformSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
4932 if (E->isArgumentType()) {
4933 TypeSourceInfo *OldT = E->getArgumentTypeInfo();
4935 TypeSourceInfo *NewT = getDerived().TransformType(OldT);
4936 if (!NewT)
4937 return ExprError();
4939 if (!getDerived().AlwaysRebuild() && OldT == NewT)
4940 return SemaRef.Owned(E);
4942 return getDerived().RebuildSizeOfAlignOf(NewT, E->getOperatorLoc(),
4943 E->isSizeOf(),
4944 E->getSourceRange());
4947 ExprResult SubExpr;
4949 // C++0x [expr.sizeof]p1:
4950 // The operand is either an expression, which is an unevaluated operand
4951 // [...]
4952 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
4954 SubExpr = getDerived().TransformExpr(E->getArgumentExpr());
4955 if (SubExpr.isInvalid())
4956 return ExprError();
4958 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getArgumentExpr())
4959 return SemaRef.Owned(E);
4962 return getDerived().RebuildSizeOfAlignOf(SubExpr.get(), E->getOperatorLoc(),
4963 E->isSizeOf(),
4964 E->getSourceRange());
4967 template<typename Derived>
4968 ExprResult
4969 TreeTransform<Derived>::TransformArraySubscriptExpr(ArraySubscriptExpr *E) {
4970 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
4971 if (LHS.isInvalid())
4972 return ExprError();
4974 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
4975 if (RHS.isInvalid())
4976 return ExprError();
4979 if (!getDerived().AlwaysRebuild() &&
4980 LHS.get() == E->getLHS() &&
4981 RHS.get() == E->getRHS())
4982 return SemaRef.Owned(E);
4984 return getDerived().RebuildArraySubscriptExpr(LHS.get(),
4985 /*FIXME:*/E->getLHS()->getLocStart(),
4986 RHS.get(),
4987 E->getRBracketLoc());
4990 template<typename Derived>
4991 ExprResult
4992 TreeTransform<Derived>::TransformCallExpr(CallExpr *E) {
4993 // Transform the callee.
4994 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
4995 if (Callee.isInvalid())
4996 return ExprError();
4998 // Transform arguments.
4999 bool ArgChanged = false;
5000 ASTOwningVector<Expr*> Args(SemaRef);
5001 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
5002 &ArgChanged))
5003 return ExprError();
5005 if (!getDerived().AlwaysRebuild() &&
5006 Callee.get() == E->getCallee() &&
5007 !ArgChanged)
5008 return SemaRef.Owned(E);
5010 // FIXME: Wrong source location information for the '('.
5011 SourceLocation FakeLParenLoc
5012 = ((Expr *)Callee.get())->getSourceRange().getBegin();
5013 return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc,
5014 move_arg(Args),
5015 E->getRParenLoc());
5018 template<typename Derived>
5019 ExprResult
5020 TreeTransform<Derived>::TransformMemberExpr(MemberExpr *E) {
5021 ExprResult Base = getDerived().TransformExpr(E->getBase());
5022 if (Base.isInvalid())
5023 return ExprError();
5025 NestedNameSpecifier *Qualifier = 0;
5026 if (E->hasQualifier()) {
5027 Qualifier
5028 = getDerived().TransformNestedNameSpecifier(E->getQualifier(),
5029 E->getQualifierRange());
5030 if (Qualifier == 0)
5031 return ExprError();
5034 ValueDecl *Member
5035 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getMemberLoc(),
5036 E->getMemberDecl()));
5037 if (!Member)
5038 return ExprError();
5040 NamedDecl *FoundDecl = E->getFoundDecl();
5041 if (FoundDecl == E->getMemberDecl()) {
5042 FoundDecl = Member;
5043 } else {
5044 FoundDecl = cast_or_null<NamedDecl>(
5045 getDerived().TransformDecl(E->getMemberLoc(), FoundDecl));
5046 if (!FoundDecl)
5047 return ExprError();
5050 if (!getDerived().AlwaysRebuild() &&
5051 Base.get() == E->getBase() &&
5052 Qualifier == E->getQualifier() &&
5053 Member == E->getMemberDecl() &&
5054 FoundDecl == E->getFoundDecl() &&
5055 !E->hasExplicitTemplateArgs()) {
5057 // Mark it referenced in the new context regardless.
5058 // FIXME: this is a bit instantiation-specific.
5059 SemaRef.MarkDeclarationReferenced(E->getMemberLoc(), Member);
5060 return SemaRef.Owned(E);
5063 TemplateArgumentListInfo TransArgs;
5064 if (E->hasExplicitTemplateArgs()) {
5065 TransArgs.setLAngleLoc(E->getLAngleLoc());
5066 TransArgs.setRAngleLoc(E->getRAngleLoc());
5067 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
5068 E->getNumTemplateArgs(),
5069 TransArgs))
5070 return ExprError();
5073 // FIXME: Bogus source location for the operator
5074 SourceLocation FakeOperatorLoc
5075 = SemaRef.PP.getLocForEndOfToken(E->getBase()->getSourceRange().getEnd());
5077 // FIXME: to do this check properly, we will need to preserve the
5078 // first-qualifier-in-scope here, just in case we had a dependent
5079 // base (and therefore couldn't do the check) and a
5080 // nested-name-qualifier (and therefore could do the lookup).
5081 NamedDecl *FirstQualifierInScope = 0;
5083 return getDerived().RebuildMemberExpr(Base.get(), FakeOperatorLoc,
5084 E->isArrow(),
5085 Qualifier,
5086 E->getQualifierRange(),
5087 E->getMemberNameInfo(),
5088 Member,
5089 FoundDecl,
5090 (E->hasExplicitTemplateArgs()
5091 ? &TransArgs : 0),
5092 FirstQualifierInScope);
5095 template<typename Derived>
5096 ExprResult
5097 TreeTransform<Derived>::TransformBinaryOperator(BinaryOperator *E) {
5098 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
5099 if (LHS.isInvalid())
5100 return ExprError();
5102 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
5103 if (RHS.isInvalid())
5104 return ExprError();
5106 if (!getDerived().AlwaysRebuild() &&
5107 LHS.get() == E->getLHS() &&
5108 RHS.get() == E->getRHS())
5109 return SemaRef.Owned(E);
5111 return getDerived().RebuildBinaryOperator(E->getOperatorLoc(), E->getOpcode(),
5112 LHS.get(), RHS.get());
5115 template<typename Derived>
5116 ExprResult
5117 TreeTransform<Derived>::TransformCompoundAssignOperator(
5118 CompoundAssignOperator *E) {
5119 return getDerived().TransformBinaryOperator(E);
5122 template<typename Derived>
5123 ExprResult
5124 TreeTransform<Derived>::TransformConditionalOperator(ConditionalOperator *E) {
5125 ExprResult Cond = getDerived().TransformExpr(E->getCond());
5126 if (Cond.isInvalid())
5127 return ExprError();
5129 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
5130 if (LHS.isInvalid())
5131 return ExprError();
5133 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
5134 if (RHS.isInvalid())
5135 return ExprError();
5137 if (!getDerived().AlwaysRebuild() &&
5138 Cond.get() == E->getCond() &&
5139 LHS.get() == E->getLHS() &&
5140 RHS.get() == E->getRHS())
5141 return SemaRef.Owned(E);
5143 return getDerived().RebuildConditionalOperator(Cond.get(),
5144 E->getQuestionLoc(),
5145 LHS.get(),
5146 E->getColonLoc(),
5147 RHS.get());
5150 template<typename Derived>
5151 ExprResult
5152 TreeTransform<Derived>::TransformImplicitCastExpr(ImplicitCastExpr *E) {
5153 // Implicit casts are eliminated during transformation, since they
5154 // will be recomputed by semantic analysis after transformation.
5155 return getDerived().TransformExpr(E->getSubExprAsWritten());
5158 template<typename Derived>
5159 ExprResult
5160 TreeTransform<Derived>::TransformCStyleCastExpr(CStyleCastExpr *E) {
5161 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
5162 if (!Type)
5163 return ExprError();
5165 ExprResult SubExpr
5166 = getDerived().TransformExpr(E->getSubExprAsWritten());
5167 if (SubExpr.isInvalid())
5168 return ExprError();
5170 if (!getDerived().AlwaysRebuild() &&
5171 Type == E->getTypeInfoAsWritten() &&
5172 SubExpr.get() == E->getSubExpr())
5173 return SemaRef.Owned(E);
5175 return getDerived().RebuildCStyleCastExpr(E->getLParenLoc(),
5176 Type,
5177 E->getRParenLoc(),
5178 SubExpr.get());
5181 template<typename Derived>
5182 ExprResult
5183 TreeTransform<Derived>::TransformCompoundLiteralExpr(CompoundLiteralExpr *E) {
5184 TypeSourceInfo *OldT = E->getTypeSourceInfo();
5185 TypeSourceInfo *NewT = getDerived().TransformType(OldT);
5186 if (!NewT)
5187 return ExprError();
5189 ExprResult Init = getDerived().TransformExpr(E->getInitializer());
5190 if (Init.isInvalid())
5191 return ExprError();
5193 if (!getDerived().AlwaysRebuild() &&
5194 OldT == NewT &&
5195 Init.get() == E->getInitializer())
5196 return SemaRef.Owned(E);
5198 // Note: the expression type doesn't necessarily match the
5199 // type-as-written, but that's okay, because it should always be
5200 // derivable from the initializer.
5202 return getDerived().RebuildCompoundLiteralExpr(E->getLParenLoc(), NewT,
5203 /*FIXME:*/E->getInitializer()->getLocEnd(),
5204 Init.get());
5207 template<typename Derived>
5208 ExprResult
5209 TreeTransform<Derived>::TransformExtVectorElementExpr(ExtVectorElementExpr *E) {
5210 ExprResult Base = getDerived().TransformExpr(E->getBase());
5211 if (Base.isInvalid())
5212 return ExprError();
5214 if (!getDerived().AlwaysRebuild() &&
5215 Base.get() == E->getBase())
5216 return SemaRef.Owned(E);
5218 // FIXME: Bad source location
5219 SourceLocation FakeOperatorLoc
5220 = SemaRef.PP.getLocForEndOfToken(E->getBase()->getLocEnd());
5221 return getDerived().RebuildExtVectorElementExpr(Base.get(), FakeOperatorLoc,
5222 E->getAccessorLoc(),
5223 E->getAccessor());
5226 template<typename Derived>
5227 ExprResult
5228 TreeTransform<Derived>::TransformInitListExpr(InitListExpr *E) {
5229 bool InitChanged = false;
5231 ASTOwningVector<Expr*, 4> Inits(SemaRef);
5232 if (getDerived().TransformExprs(E->getInits(), E->getNumInits(), false,
5233 Inits, &InitChanged))
5234 return ExprError();
5236 if (!getDerived().AlwaysRebuild() && !InitChanged)
5237 return SemaRef.Owned(E);
5239 return getDerived().RebuildInitList(E->getLBraceLoc(), move_arg(Inits),
5240 E->getRBraceLoc(), E->getType());
5243 template<typename Derived>
5244 ExprResult
5245 TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
5246 Designation Desig;
5248 // transform the initializer value
5249 ExprResult Init = getDerived().TransformExpr(E->getInit());
5250 if (Init.isInvalid())
5251 return ExprError();
5253 // transform the designators.
5254 ASTOwningVector<Expr*, 4> ArrayExprs(SemaRef);
5255 bool ExprChanged = false;
5256 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
5257 DEnd = E->designators_end();
5258 D != DEnd; ++D) {
5259 if (D->isFieldDesignator()) {
5260 Desig.AddDesignator(Designator::getField(D->getFieldName(),
5261 D->getDotLoc(),
5262 D->getFieldLoc()));
5263 continue;
5266 if (D->isArrayDesignator()) {
5267 ExprResult Index = getDerived().TransformExpr(E->getArrayIndex(*D));
5268 if (Index.isInvalid())
5269 return ExprError();
5271 Desig.AddDesignator(Designator::getArray(Index.get(),
5272 D->getLBracketLoc()));
5274 ExprChanged = ExprChanged || Init.get() != E->getArrayIndex(*D);
5275 ArrayExprs.push_back(Index.release());
5276 continue;
5279 assert(D->isArrayRangeDesignator() && "New kind of designator?");
5280 ExprResult Start
5281 = getDerived().TransformExpr(E->getArrayRangeStart(*D));
5282 if (Start.isInvalid())
5283 return ExprError();
5285 ExprResult End = getDerived().TransformExpr(E->getArrayRangeEnd(*D));
5286 if (End.isInvalid())
5287 return ExprError();
5289 Desig.AddDesignator(Designator::getArrayRange(Start.get(),
5290 End.get(),
5291 D->getLBracketLoc(),
5292 D->getEllipsisLoc()));
5294 ExprChanged = ExprChanged || Start.get() != E->getArrayRangeStart(*D) ||
5295 End.get() != E->getArrayRangeEnd(*D);
5297 ArrayExprs.push_back(Start.release());
5298 ArrayExprs.push_back(End.release());
5301 if (!getDerived().AlwaysRebuild() &&
5302 Init.get() == E->getInit() &&
5303 !ExprChanged)
5304 return SemaRef.Owned(E);
5306 return getDerived().RebuildDesignatedInitExpr(Desig, move_arg(ArrayExprs),
5307 E->getEqualOrColonLoc(),
5308 E->usesGNUSyntax(), Init.get());
5311 template<typename Derived>
5312 ExprResult
5313 TreeTransform<Derived>::TransformImplicitValueInitExpr(
5314 ImplicitValueInitExpr *E) {
5315 TemporaryBase Rebase(*this, E->getLocStart(), DeclarationName());
5317 // FIXME: Will we ever have proper type location here? Will we actually
5318 // need to transform the type?
5319 QualType T = getDerived().TransformType(E->getType());
5320 if (T.isNull())
5321 return ExprError();
5323 if (!getDerived().AlwaysRebuild() &&
5324 T == E->getType())
5325 return SemaRef.Owned(E);
5327 return getDerived().RebuildImplicitValueInitExpr(T);
5330 template<typename Derived>
5331 ExprResult
5332 TreeTransform<Derived>::TransformVAArgExpr(VAArgExpr *E) {
5333 TypeSourceInfo *TInfo = getDerived().TransformType(E->getWrittenTypeInfo());
5334 if (!TInfo)
5335 return ExprError();
5337 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
5338 if (SubExpr.isInvalid())
5339 return ExprError();
5341 if (!getDerived().AlwaysRebuild() &&
5342 TInfo == E->getWrittenTypeInfo() &&
5343 SubExpr.get() == E->getSubExpr())
5344 return SemaRef.Owned(E);
5346 return getDerived().RebuildVAArgExpr(E->getBuiltinLoc(), SubExpr.get(),
5347 TInfo, E->getRParenLoc());
5350 template<typename Derived>
5351 ExprResult
5352 TreeTransform<Derived>::TransformParenListExpr(ParenListExpr *E) {
5353 bool ArgumentChanged = false;
5354 ASTOwningVector<Expr*, 4> Inits(SemaRef);
5355 if (TransformExprs(E->getExprs(), E->getNumExprs(), true, Inits,
5356 &ArgumentChanged))
5357 return ExprError();
5359 return getDerived().RebuildParenListExpr(E->getLParenLoc(),
5360 move_arg(Inits),
5361 E->getRParenLoc());
5364 /// \brief Transform an address-of-label expression.
5366 /// By default, the transformation of an address-of-label expression always
5367 /// rebuilds the expression, so that the label identifier can be resolved to
5368 /// the corresponding label statement by semantic analysis.
5369 template<typename Derived>
5370 ExprResult
5371 TreeTransform<Derived>::TransformAddrLabelExpr(AddrLabelExpr *E) {
5372 return getDerived().RebuildAddrLabelExpr(E->getAmpAmpLoc(), E->getLabelLoc(),
5373 E->getLabel());
5376 template<typename Derived>
5377 ExprResult
5378 TreeTransform<Derived>::TransformStmtExpr(StmtExpr *E) {
5379 StmtResult SubStmt
5380 = getDerived().TransformCompoundStmt(E->getSubStmt(), true);
5381 if (SubStmt.isInvalid())
5382 return ExprError();
5384 if (!getDerived().AlwaysRebuild() &&
5385 SubStmt.get() == E->getSubStmt())
5386 return SemaRef.Owned(E);
5388 return getDerived().RebuildStmtExpr(E->getLParenLoc(),
5389 SubStmt.get(),
5390 E->getRParenLoc());
5393 template<typename Derived>
5394 ExprResult
5395 TreeTransform<Derived>::TransformChooseExpr(ChooseExpr *E) {
5396 ExprResult Cond = getDerived().TransformExpr(E->getCond());
5397 if (Cond.isInvalid())
5398 return ExprError();
5400 ExprResult LHS = getDerived().TransformExpr(E->getLHS());
5401 if (LHS.isInvalid())
5402 return ExprError();
5404 ExprResult RHS = getDerived().TransformExpr(E->getRHS());
5405 if (RHS.isInvalid())
5406 return ExprError();
5408 if (!getDerived().AlwaysRebuild() &&
5409 Cond.get() == E->getCond() &&
5410 LHS.get() == E->getLHS() &&
5411 RHS.get() == E->getRHS())
5412 return SemaRef.Owned(E);
5414 return getDerived().RebuildChooseExpr(E->getBuiltinLoc(),
5415 Cond.get(), LHS.get(), RHS.get(),
5416 E->getRParenLoc());
5419 template<typename Derived>
5420 ExprResult
5421 TreeTransform<Derived>::TransformGNUNullExpr(GNUNullExpr *E) {
5422 return SemaRef.Owned(E);
5425 template<typename Derived>
5426 ExprResult
5427 TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
5428 switch (E->getOperator()) {
5429 case OO_New:
5430 case OO_Delete:
5431 case OO_Array_New:
5432 case OO_Array_Delete:
5433 llvm_unreachable("new and delete operators cannot use CXXOperatorCallExpr");
5434 return ExprError();
5436 case OO_Call: {
5437 // This is a call to an object's operator().
5438 assert(E->getNumArgs() >= 1 && "Object call is missing arguments");
5440 // Transform the object itself.
5441 ExprResult Object = getDerived().TransformExpr(E->getArg(0));
5442 if (Object.isInvalid())
5443 return ExprError();
5445 // FIXME: Poor location information
5446 SourceLocation FakeLParenLoc
5447 = SemaRef.PP.getLocForEndOfToken(
5448 static_cast<Expr *>(Object.get())->getLocEnd());
5450 // Transform the call arguments.
5451 ASTOwningVector<Expr*> Args(SemaRef);
5452 if (getDerived().TransformExprs(E->getArgs() + 1, E->getNumArgs() - 1, true,
5453 Args))
5454 return ExprError();
5456 return getDerived().RebuildCallExpr(Object.get(), FakeLParenLoc,
5457 move_arg(Args),
5458 E->getLocEnd());
5461 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
5462 case OO_##Name:
5463 #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
5464 #include "clang/Basic/OperatorKinds.def"
5465 case OO_Subscript:
5466 // Handled below.
5467 break;
5469 case OO_Conditional:
5470 llvm_unreachable("conditional operator is not actually overloadable");
5471 return ExprError();
5473 case OO_None:
5474 case NUM_OVERLOADED_OPERATORS:
5475 llvm_unreachable("not an overloaded operator?");
5476 return ExprError();
5479 ExprResult Callee = getDerived().TransformExpr(E->getCallee());
5480 if (Callee.isInvalid())
5481 return ExprError();
5483 ExprResult First = getDerived().TransformExpr(E->getArg(0));
5484 if (First.isInvalid())
5485 return ExprError();
5487 ExprResult Second;
5488 if (E->getNumArgs() == 2) {
5489 Second = getDerived().TransformExpr(E->getArg(1));
5490 if (Second.isInvalid())
5491 return ExprError();
5494 if (!getDerived().AlwaysRebuild() &&
5495 Callee.get() == E->getCallee() &&
5496 First.get() == E->getArg(0) &&
5497 (E->getNumArgs() != 2 || Second.get() == E->getArg(1)))
5498 return SemaRef.Owned(E);
5500 return getDerived().RebuildCXXOperatorCallExpr(E->getOperator(),
5501 E->getOperatorLoc(),
5502 Callee.get(),
5503 First.get(),
5504 Second.get());
5507 template<typename Derived>
5508 ExprResult
5509 TreeTransform<Derived>::TransformCXXMemberCallExpr(CXXMemberCallExpr *E) {
5510 return getDerived().TransformCallExpr(E);
5513 template<typename Derived>
5514 ExprResult
5515 TreeTransform<Derived>::TransformCXXNamedCastExpr(CXXNamedCastExpr *E) {
5516 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
5517 if (!Type)
5518 return ExprError();
5520 ExprResult SubExpr
5521 = getDerived().TransformExpr(E->getSubExprAsWritten());
5522 if (SubExpr.isInvalid())
5523 return ExprError();
5525 if (!getDerived().AlwaysRebuild() &&
5526 Type == E->getTypeInfoAsWritten() &&
5527 SubExpr.get() == E->getSubExpr())
5528 return SemaRef.Owned(E);
5530 // FIXME: Poor source location information here.
5531 SourceLocation FakeLAngleLoc
5532 = SemaRef.PP.getLocForEndOfToken(E->getOperatorLoc());
5533 SourceLocation FakeRAngleLoc = E->getSubExpr()->getSourceRange().getBegin();
5534 SourceLocation FakeRParenLoc
5535 = SemaRef.PP.getLocForEndOfToken(
5536 E->getSubExpr()->getSourceRange().getEnd());
5537 return getDerived().RebuildCXXNamedCastExpr(E->getOperatorLoc(),
5538 E->getStmtClass(),
5539 FakeLAngleLoc,
5540 Type,
5541 FakeRAngleLoc,
5542 FakeRAngleLoc,
5543 SubExpr.get(),
5544 FakeRParenLoc);
5547 template<typename Derived>
5548 ExprResult
5549 TreeTransform<Derived>::TransformCXXStaticCastExpr(CXXStaticCastExpr *E) {
5550 return getDerived().TransformCXXNamedCastExpr(E);
5553 template<typename Derived>
5554 ExprResult
5555 TreeTransform<Derived>::TransformCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
5556 return getDerived().TransformCXXNamedCastExpr(E);
5559 template<typename Derived>
5560 ExprResult
5561 TreeTransform<Derived>::TransformCXXReinterpretCastExpr(
5562 CXXReinterpretCastExpr *E) {
5563 return getDerived().TransformCXXNamedCastExpr(E);
5566 template<typename Derived>
5567 ExprResult
5568 TreeTransform<Derived>::TransformCXXConstCastExpr(CXXConstCastExpr *E) {
5569 return getDerived().TransformCXXNamedCastExpr(E);
5572 template<typename Derived>
5573 ExprResult
5574 TreeTransform<Derived>::TransformCXXFunctionalCastExpr(
5575 CXXFunctionalCastExpr *E) {
5576 TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
5577 if (!Type)
5578 return ExprError();
5580 ExprResult SubExpr
5581 = getDerived().TransformExpr(E->getSubExprAsWritten());
5582 if (SubExpr.isInvalid())
5583 return ExprError();
5585 if (!getDerived().AlwaysRebuild() &&
5586 Type == E->getTypeInfoAsWritten() &&
5587 SubExpr.get() == E->getSubExpr())
5588 return SemaRef.Owned(E);
5590 return getDerived().RebuildCXXFunctionalCastExpr(Type,
5591 /*FIXME:*/E->getSubExpr()->getLocStart(),
5592 SubExpr.get(),
5593 E->getRParenLoc());
5596 template<typename Derived>
5597 ExprResult
5598 TreeTransform<Derived>::TransformCXXTypeidExpr(CXXTypeidExpr *E) {
5599 if (E->isTypeOperand()) {
5600 TypeSourceInfo *TInfo
5601 = getDerived().TransformType(E->getTypeOperandSourceInfo());
5602 if (!TInfo)
5603 return ExprError();
5605 if (!getDerived().AlwaysRebuild() &&
5606 TInfo == E->getTypeOperandSourceInfo())
5607 return SemaRef.Owned(E);
5609 return getDerived().RebuildCXXTypeidExpr(E->getType(),
5610 E->getLocStart(),
5611 TInfo,
5612 E->getLocEnd());
5615 // We don't know whether the expression is potentially evaluated until
5616 // after we perform semantic analysis, so the expression is potentially
5617 // potentially evaluated.
5618 EnterExpressionEvaluationContext Unevaluated(SemaRef,
5619 Sema::PotentiallyPotentiallyEvaluated);
5621 ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
5622 if (SubExpr.isInvalid())
5623 return ExprError();
5625 if (!getDerived().AlwaysRebuild() &&
5626 SubExpr.get() == E->getExprOperand())
5627 return SemaRef.Owned(E);
5629 return getDerived().RebuildCXXTypeidExpr(E->getType(),
5630 E->getLocStart(),
5631 SubExpr.get(),
5632 E->getLocEnd());
5635 template<typename Derived>
5636 ExprResult
5637 TreeTransform<Derived>::TransformCXXUuidofExpr(CXXUuidofExpr *E) {
5638 if (E->isTypeOperand()) {
5639 TypeSourceInfo *TInfo
5640 = getDerived().TransformType(E->getTypeOperandSourceInfo());
5641 if (!TInfo)
5642 return ExprError();
5644 if (!getDerived().AlwaysRebuild() &&
5645 TInfo == E->getTypeOperandSourceInfo())
5646 return SemaRef.Owned(E);
5648 return getDerived().RebuildCXXTypeidExpr(E->getType(),
5649 E->getLocStart(),
5650 TInfo,
5651 E->getLocEnd());
5654 // We don't know whether the expression is potentially evaluated until
5655 // after we perform semantic analysis, so the expression is potentially
5656 // potentially evaluated.
5657 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
5659 ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand());
5660 if (SubExpr.isInvalid())
5661 return ExprError();
5663 if (!getDerived().AlwaysRebuild() &&
5664 SubExpr.get() == E->getExprOperand())
5665 return SemaRef.Owned(E);
5667 return getDerived().RebuildCXXUuidofExpr(E->getType(),
5668 E->getLocStart(),
5669 SubExpr.get(),
5670 E->getLocEnd());
5673 template<typename Derived>
5674 ExprResult
5675 TreeTransform<Derived>::TransformCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5676 return SemaRef.Owned(E);
5679 template<typename Derived>
5680 ExprResult
5681 TreeTransform<Derived>::TransformCXXNullPtrLiteralExpr(
5682 CXXNullPtrLiteralExpr *E) {
5683 return SemaRef.Owned(E);
5686 template<typename Derived>
5687 ExprResult
5688 TreeTransform<Derived>::TransformCXXThisExpr(CXXThisExpr *E) {
5689 DeclContext *DC = getSema().getFunctionLevelDeclContext();
5690 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC);
5691 QualType T = MD->getThisType(getSema().Context);
5693 if (!getDerived().AlwaysRebuild() && T == E->getType())
5694 return SemaRef.Owned(E);
5696 return getDerived().RebuildCXXThisExpr(E->getLocStart(), T, E->isImplicit());
5699 template<typename Derived>
5700 ExprResult
5701 TreeTransform<Derived>::TransformCXXThrowExpr(CXXThrowExpr *E) {
5702 ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr());
5703 if (SubExpr.isInvalid())
5704 return ExprError();
5706 if (!getDerived().AlwaysRebuild() &&
5707 SubExpr.get() == E->getSubExpr())
5708 return SemaRef.Owned(E);
5710 return getDerived().RebuildCXXThrowExpr(E->getThrowLoc(), SubExpr.get());
5713 template<typename Derived>
5714 ExprResult
5715 TreeTransform<Derived>::TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5716 ParmVarDecl *Param
5717 = cast_or_null<ParmVarDecl>(getDerived().TransformDecl(E->getLocStart(),
5718 E->getParam()));
5719 if (!Param)
5720 return ExprError();
5722 if (!getDerived().AlwaysRebuild() &&
5723 Param == E->getParam())
5724 return SemaRef.Owned(E);
5726 return getDerived().RebuildCXXDefaultArgExpr(E->getUsedLocation(), Param);
5729 template<typename Derived>
5730 ExprResult
5731 TreeTransform<Derived>::TransformCXXScalarValueInitExpr(
5732 CXXScalarValueInitExpr *E) {
5733 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
5734 if (!T)
5735 return ExprError();
5737 if (!getDerived().AlwaysRebuild() &&
5738 T == E->getTypeSourceInfo())
5739 return SemaRef.Owned(E);
5741 return getDerived().RebuildCXXScalarValueInitExpr(T,
5742 /*FIXME:*/T->getTypeLoc().getEndLoc(),
5743 E->getRParenLoc());
5746 template<typename Derived>
5747 ExprResult
5748 TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
5749 // Transform the type that we're allocating
5750 TypeSourceInfo *AllocTypeInfo
5751 = getDerived().TransformType(E->getAllocatedTypeSourceInfo());
5752 if (!AllocTypeInfo)
5753 return ExprError();
5755 // Transform the size of the array we're allocating (if any).
5756 ExprResult ArraySize = getDerived().TransformExpr(E->getArraySize());
5757 if (ArraySize.isInvalid())
5758 return ExprError();
5760 // Transform the placement arguments (if any).
5761 bool ArgumentChanged = false;
5762 ASTOwningVector<Expr*> PlacementArgs(SemaRef);
5763 if (getDerived().TransformExprs(E->getPlacementArgs(),
5764 E->getNumPlacementArgs(), true,
5765 PlacementArgs, &ArgumentChanged))
5766 return ExprError();
5768 // transform the constructor arguments (if any).
5769 ASTOwningVector<Expr*> ConstructorArgs(SemaRef);
5770 if (TransformExprs(E->getConstructorArgs(), E->getNumConstructorArgs(), true,
5771 ConstructorArgs, &ArgumentChanged))
5772 return ExprError();
5774 // Transform constructor, new operator, and delete operator.
5775 CXXConstructorDecl *Constructor = 0;
5776 if (E->getConstructor()) {
5777 Constructor = cast_or_null<CXXConstructorDecl>(
5778 getDerived().TransformDecl(E->getLocStart(),
5779 E->getConstructor()));
5780 if (!Constructor)
5781 return ExprError();
5784 FunctionDecl *OperatorNew = 0;
5785 if (E->getOperatorNew()) {
5786 OperatorNew = cast_or_null<FunctionDecl>(
5787 getDerived().TransformDecl(E->getLocStart(),
5788 E->getOperatorNew()));
5789 if (!OperatorNew)
5790 return ExprError();
5793 FunctionDecl *OperatorDelete = 0;
5794 if (E->getOperatorDelete()) {
5795 OperatorDelete = cast_or_null<FunctionDecl>(
5796 getDerived().TransformDecl(E->getLocStart(),
5797 E->getOperatorDelete()));
5798 if (!OperatorDelete)
5799 return ExprError();
5802 if (!getDerived().AlwaysRebuild() &&
5803 AllocTypeInfo == E->getAllocatedTypeSourceInfo() &&
5804 ArraySize.get() == E->getArraySize() &&
5805 Constructor == E->getConstructor() &&
5806 OperatorNew == E->getOperatorNew() &&
5807 OperatorDelete == E->getOperatorDelete() &&
5808 !ArgumentChanged) {
5809 // Mark any declarations we need as referenced.
5810 // FIXME: instantiation-specific.
5811 if (Constructor)
5812 SemaRef.MarkDeclarationReferenced(E->getLocStart(), Constructor);
5813 if (OperatorNew)
5814 SemaRef.MarkDeclarationReferenced(E->getLocStart(), OperatorNew);
5815 if (OperatorDelete)
5816 SemaRef.MarkDeclarationReferenced(E->getLocStart(), OperatorDelete);
5817 return SemaRef.Owned(E);
5820 QualType AllocType = AllocTypeInfo->getType();
5821 if (!ArraySize.get()) {
5822 // If no array size was specified, but the new expression was
5823 // instantiated with an array type (e.g., "new T" where T is
5824 // instantiated with "int[4]"), extract the outer bound from the
5825 // array type as our array size. We do this with constant and
5826 // dependently-sized array types.
5827 const ArrayType *ArrayT = SemaRef.Context.getAsArrayType(AllocType);
5828 if (!ArrayT) {
5829 // Do nothing
5830 } else if (const ConstantArrayType *ConsArrayT
5831 = dyn_cast<ConstantArrayType>(ArrayT)) {
5832 ArraySize
5833 = SemaRef.Owned(IntegerLiteral::Create(SemaRef.Context,
5834 ConsArrayT->getSize(),
5835 SemaRef.Context.getSizeType(),
5836 /*FIXME:*/E->getLocStart()));
5837 AllocType = ConsArrayT->getElementType();
5838 } else if (const DependentSizedArrayType *DepArrayT
5839 = dyn_cast<DependentSizedArrayType>(ArrayT)) {
5840 if (DepArrayT->getSizeExpr()) {
5841 ArraySize = SemaRef.Owned(DepArrayT->getSizeExpr());
5842 AllocType = DepArrayT->getElementType();
5847 return getDerived().RebuildCXXNewExpr(E->getLocStart(),
5848 E->isGlobalNew(),
5849 /*FIXME:*/E->getLocStart(),
5850 move_arg(PlacementArgs),
5851 /*FIXME:*/E->getLocStart(),
5852 E->getTypeIdParens(),
5853 AllocType,
5854 AllocTypeInfo,
5855 ArraySize.get(),
5856 /*FIXME:*/E->getLocStart(),
5857 move_arg(ConstructorArgs),
5858 E->getLocEnd());
5861 template<typename Derived>
5862 ExprResult
5863 TreeTransform<Derived>::TransformCXXDeleteExpr(CXXDeleteExpr *E) {
5864 ExprResult Operand = getDerived().TransformExpr(E->getArgument());
5865 if (Operand.isInvalid())
5866 return ExprError();
5868 // Transform the delete operator, if known.
5869 FunctionDecl *OperatorDelete = 0;
5870 if (E->getOperatorDelete()) {
5871 OperatorDelete = cast_or_null<FunctionDecl>(
5872 getDerived().TransformDecl(E->getLocStart(),
5873 E->getOperatorDelete()));
5874 if (!OperatorDelete)
5875 return ExprError();
5878 if (!getDerived().AlwaysRebuild() &&
5879 Operand.get() == E->getArgument() &&
5880 OperatorDelete == E->getOperatorDelete()) {
5881 // Mark any declarations we need as referenced.
5882 // FIXME: instantiation-specific.
5883 if (OperatorDelete)
5884 SemaRef.MarkDeclarationReferenced(E->getLocStart(), OperatorDelete);
5886 if (!E->getArgument()->isTypeDependent()) {
5887 QualType Destroyed = SemaRef.Context.getBaseElementType(
5888 E->getDestroyedType());
5889 if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) {
5890 CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl());
5891 SemaRef.MarkDeclarationReferenced(E->getLocStart(),
5892 SemaRef.LookupDestructor(Record));
5896 return SemaRef.Owned(E);
5899 return getDerived().RebuildCXXDeleteExpr(E->getLocStart(),
5900 E->isGlobalDelete(),
5901 E->isArrayForm(),
5902 Operand.get());
5905 template<typename Derived>
5906 ExprResult
5907 TreeTransform<Derived>::TransformCXXPseudoDestructorExpr(
5908 CXXPseudoDestructorExpr *E) {
5909 ExprResult Base = getDerived().TransformExpr(E->getBase());
5910 if (Base.isInvalid())
5911 return ExprError();
5913 ParsedType ObjectTypePtr;
5914 bool MayBePseudoDestructor = false;
5915 Base = SemaRef.ActOnStartCXXMemberReference(0, Base.get(),
5916 E->getOperatorLoc(),
5917 E->isArrow()? tok::arrow : tok::period,
5918 ObjectTypePtr,
5919 MayBePseudoDestructor);
5920 if (Base.isInvalid())
5921 return ExprError();
5923 QualType ObjectType = ObjectTypePtr.get();
5924 NestedNameSpecifier *Qualifier = E->getQualifier();
5925 if (Qualifier) {
5926 Qualifier
5927 = getDerived().TransformNestedNameSpecifier(E->getQualifier(),
5928 E->getQualifierRange(),
5929 ObjectType);
5930 if (!Qualifier)
5931 return ExprError();
5934 PseudoDestructorTypeStorage Destroyed;
5935 if (E->getDestroyedTypeInfo()) {
5936 TypeSourceInfo *DestroyedTypeInfo
5937 = getDerived().TransformTypeInObjectScope(E->getDestroyedTypeInfo(),
5938 ObjectType, 0, Qualifier);
5939 if (!DestroyedTypeInfo)
5940 return ExprError();
5941 Destroyed = DestroyedTypeInfo;
5942 } else if (ObjectType->isDependentType()) {
5943 // We aren't likely to be able to resolve the identifier down to a type
5944 // now anyway, so just retain the identifier.
5945 Destroyed = PseudoDestructorTypeStorage(E->getDestroyedTypeIdentifier(),
5946 E->getDestroyedTypeLoc());
5947 } else {
5948 // Look for a destructor known with the given name.
5949 CXXScopeSpec SS;
5950 if (Qualifier) {
5951 SS.setScopeRep(Qualifier);
5952 SS.setRange(E->getQualifierRange());
5955 ParsedType T = SemaRef.getDestructorName(E->getTildeLoc(),
5956 *E->getDestroyedTypeIdentifier(),
5957 E->getDestroyedTypeLoc(),
5958 /*Scope=*/0,
5959 SS, ObjectTypePtr,
5960 false);
5961 if (!T)
5962 return ExprError();
5964 Destroyed
5965 = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.GetTypeFromParser(T),
5966 E->getDestroyedTypeLoc());
5969 TypeSourceInfo *ScopeTypeInfo = 0;
5970 if (E->getScopeTypeInfo()) {
5971 ScopeTypeInfo = getDerived().TransformType(E->getScopeTypeInfo());
5972 if (!ScopeTypeInfo)
5973 return ExprError();
5976 return getDerived().RebuildCXXPseudoDestructorExpr(Base.get(),
5977 E->getOperatorLoc(),
5978 E->isArrow(),
5979 Qualifier,
5980 E->getQualifierRange(),
5981 ScopeTypeInfo,
5982 E->getColonColonLoc(),
5983 E->getTildeLoc(),
5984 Destroyed);
5987 template<typename Derived>
5988 ExprResult
5989 TreeTransform<Derived>::TransformUnresolvedLookupExpr(
5990 UnresolvedLookupExpr *Old) {
5991 TemporaryBase Rebase(*this, Old->getNameLoc(), DeclarationName());
5993 LookupResult R(SemaRef, Old->getName(), Old->getNameLoc(),
5994 Sema::LookupOrdinaryName);
5996 // Transform all the decls.
5997 for (UnresolvedLookupExpr::decls_iterator I = Old->decls_begin(),
5998 E = Old->decls_end(); I != E; ++I) {
5999 NamedDecl *InstD = static_cast<NamedDecl*>(
6000 getDerived().TransformDecl(Old->getNameLoc(),
6001 *I));
6002 if (!InstD) {
6003 // Silently ignore these if a UsingShadowDecl instantiated to nothing.
6004 // This can happen because of dependent hiding.
6005 if (isa<UsingShadowDecl>(*I))
6006 continue;
6007 else
6008 return ExprError();
6011 // Expand using declarations.
6012 if (isa<UsingDecl>(InstD)) {
6013 UsingDecl *UD = cast<UsingDecl>(InstD);
6014 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
6015 E = UD->shadow_end(); I != E; ++I)
6016 R.addDecl(*I);
6017 continue;
6020 R.addDecl(InstD);
6023 // Resolve a kind, but don't do any further analysis. If it's
6024 // ambiguous, the callee needs to deal with it.
6025 R.resolveKind();
6027 // Rebuild the nested-name qualifier, if present.
6028 CXXScopeSpec SS;
6029 NestedNameSpecifier *Qualifier = 0;
6030 if (Old->getQualifier()) {
6031 Qualifier = getDerived().TransformNestedNameSpecifier(Old->getQualifier(),
6032 Old->getQualifierRange());
6033 if (!Qualifier)
6034 return ExprError();
6036 SS.setScopeRep(Qualifier);
6037 SS.setRange(Old->getQualifierRange());
6040 if (Old->getNamingClass()) {
6041 CXXRecordDecl *NamingClass
6042 = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
6043 Old->getNameLoc(),
6044 Old->getNamingClass()));
6045 if (!NamingClass)
6046 return ExprError();
6048 R.setNamingClass(NamingClass);
6051 // If we have no template arguments, it's a normal declaration name.
6052 if (!Old->hasExplicitTemplateArgs())
6053 return getDerived().RebuildDeclarationNameExpr(SS, R, Old->requiresADL());
6055 // If we have template arguments, rebuild them, then rebuild the
6056 // templateid expression.
6057 TemplateArgumentListInfo TransArgs(Old->getLAngleLoc(), Old->getRAngleLoc());
6058 if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(),
6059 Old->getNumTemplateArgs(),
6060 TransArgs))
6061 return ExprError();
6063 return getDerived().RebuildTemplateIdExpr(SS, R, Old->requiresADL(),
6064 TransArgs);
6067 template<typename Derived>
6068 ExprResult
6069 TreeTransform<Derived>::TransformUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
6070 TypeSourceInfo *T = getDerived().TransformType(E->getQueriedTypeSourceInfo());
6071 if (!T)
6072 return ExprError();
6074 if (!getDerived().AlwaysRebuild() &&
6075 T == E->getQueriedTypeSourceInfo())
6076 return SemaRef.Owned(E);
6078 return getDerived().RebuildUnaryTypeTrait(E->getTrait(),
6079 E->getLocStart(),
6081 E->getLocEnd());
6084 template<typename Derived>
6085 ExprResult
6086 TreeTransform<Derived>::TransformBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
6087 TypeSourceInfo *LhsT = getDerived().TransformType(E->getLhsTypeSourceInfo());
6088 if (!LhsT)
6089 return ExprError();
6091 TypeSourceInfo *RhsT = getDerived().TransformType(E->getRhsTypeSourceInfo());
6092 if (!RhsT)
6093 return ExprError();
6095 if (!getDerived().AlwaysRebuild() &&
6096 LhsT == E->getLhsTypeSourceInfo() && RhsT == E->getRhsTypeSourceInfo())
6097 return SemaRef.Owned(E);
6099 return getDerived().RebuildBinaryTypeTrait(E->getTrait(),
6100 E->getLocStart(),
6101 LhsT, RhsT,
6102 E->getLocEnd());
6105 template<typename Derived>
6106 ExprResult
6107 TreeTransform<Derived>::TransformDependentScopeDeclRefExpr(
6108 DependentScopeDeclRefExpr *E) {
6109 NestedNameSpecifier *NNS
6110 = getDerived().TransformNestedNameSpecifier(E->getQualifier(),
6111 E->getQualifierRange());
6112 if (!NNS)
6113 return ExprError();
6115 // TODO: If this is a conversion-function-id, verify that the
6116 // destination type name (if present) resolves the same way after
6117 // instantiation as it did in the local scope.
6119 DeclarationNameInfo NameInfo
6120 = getDerived().TransformDeclarationNameInfo(E->getNameInfo());
6121 if (!NameInfo.getName())
6122 return ExprError();
6124 if (!E->hasExplicitTemplateArgs()) {
6125 if (!getDerived().AlwaysRebuild() &&
6126 NNS == E->getQualifier() &&
6127 // Note: it is sufficient to compare the Name component of NameInfo:
6128 // if name has not changed, DNLoc has not changed either.
6129 NameInfo.getName() == E->getDeclName())
6130 return SemaRef.Owned(E);
6132 return getDerived().RebuildDependentScopeDeclRefExpr(NNS,
6133 E->getQualifierRange(),
6134 NameInfo,
6135 /*TemplateArgs*/ 0);
6138 TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc());
6139 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
6140 E->getNumTemplateArgs(),
6141 TransArgs))
6142 return ExprError();
6144 return getDerived().RebuildDependentScopeDeclRefExpr(NNS,
6145 E->getQualifierRange(),
6146 NameInfo,
6147 &TransArgs);
6150 template<typename Derived>
6151 ExprResult
6152 TreeTransform<Derived>::TransformCXXConstructExpr(CXXConstructExpr *E) {
6153 // CXXConstructExprs are always implicit, so when we have a
6154 // 1-argument construction we just transform that argument.
6155 if (E->getNumArgs() == 1 ||
6156 (E->getNumArgs() > 1 && getDerived().DropCallArgument(E->getArg(1))))
6157 return getDerived().TransformExpr(E->getArg(0));
6159 TemporaryBase Rebase(*this, /*FIXME*/E->getLocStart(), DeclarationName());
6161 QualType T = getDerived().TransformType(E->getType());
6162 if (T.isNull())
6163 return ExprError();
6165 CXXConstructorDecl *Constructor
6166 = cast_or_null<CXXConstructorDecl>(
6167 getDerived().TransformDecl(E->getLocStart(),
6168 E->getConstructor()));
6169 if (!Constructor)
6170 return ExprError();
6172 bool ArgumentChanged = false;
6173 ASTOwningVector<Expr*> Args(SemaRef);
6174 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
6175 &ArgumentChanged))
6176 return ExprError();
6178 if (!getDerived().AlwaysRebuild() &&
6179 T == E->getType() &&
6180 Constructor == E->getConstructor() &&
6181 !ArgumentChanged) {
6182 // Mark the constructor as referenced.
6183 // FIXME: Instantiation-specific
6184 SemaRef.MarkDeclarationReferenced(E->getLocStart(), Constructor);
6185 return SemaRef.Owned(E);
6188 return getDerived().RebuildCXXConstructExpr(T, /*FIXME:*/E->getLocStart(),
6189 Constructor, E->isElidable(),
6190 move_arg(Args),
6191 E->requiresZeroInitialization(),
6192 E->getConstructionKind(),
6193 E->getParenRange());
6196 /// \brief Transform a C++ temporary-binding expression.
6198 /// Since CXXBindTemporaryExpr nodes are implicitly generated, we just
6199 /// transform the subexpression and return that.
6200 template<typename Derived>
6201 ExprResult
6202 TreeTransform<Derived>::TransformCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6203 return getDerived().TransformExpr(E->getSubExpr());
6206 /// \brief Transform a C++ expression that contains cleanups that should
6207 /// be run after the expression is evaluated.
6209 /// Since ExprWithCleanups nodes are implicitly generated, we
6210 /// just transform the subexpression and return that.
6211 template<typename Derived>
6212 ExprResult
6213 TreeTransform<Derived>::TransformExprWithCleanups(ExprWithCleanups *E) {
6214 return getDerived().TransformExpr(E->getSubExpr());
6217 template<typename Derived>
6218 ExprResult
6219 TreeTransform<Derived>::TransformCXXTemporaryObjectExpr(
6220 CXXTemporaryObjectExpr *E) {
6221 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
6222 if (!T)
6223 return ExprError();
6225 CXXConstructorDecl *Constructor
6226 = cast_or_null<CXXConstructorDecl>(
6227 getDerived().TransformDecl(E->getLocStart(),
6228 E->getConstructor()));
6229 if (!Constructor)
6230 return ExprError();
6232 bool ArgumentChanged = false;
6233 ASTOwningVector<Expr*> Args(SemaRef);
6234 Args.reserve(E->getNumArgs());
6235 if (TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
6236 &ArgumentChanged))
6237 return ExprError();
6239 if (!getDerived().AlwaysRebuild() &&
6240 T == E->getTypeSourceInfo() &&
6241 Constructor == E->getConstructor() &&
6242 !ArgumentChanged) {
6243 // FIXME: Instantiation-specific
6244 SemaRef.MarkDeclarationReferenced(E->getLocStart(), Constructor);
6245 return SemaRef.MaybeBindToTemporary(E);
6248 return getDerived().RebuildCXXTemporaryObjectExpr(T,
6249 /*FIXME:*/T->getTypeLoc().getEndLoc(),
6250 move_arg(Args),
6251 E->getLocEnd());
6254 template<typename Derived>
6255 ExprResult
6256 TreeTransform<Derived>::TransformCXXUnresolvedConstructExpr(
6257 CXXUnresolvedConstructExpr *E) {
6258 TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
6259 if (!T)
6260 return ExprError();
6262 bool ArgumentChanged = false;
6263 ASTOwningVector<Expr*> Args(SemaRef);
6264 Args.reserve(E->arg_size());
6265 if (getDerived().TransformExprs(E->arg_begin(), E->arg_size(), true, Args,
6266 &ArgumentChanged))
6267 return ExprError();
6269 if (!getDerived().AlwaysRebuild() &&
6270 T == E->getTypeSourceInfo() &&
6271 !ArgumentChanged)
6272 return SemaRef.Owned(E);
6274 // FIXME: we're faking the locations of the commas
6275 return getDerived().RebuildCXXUnresolvedConstructExpr(T,
6276 E->getLParenLoc(),
6277 move_arg(Args),
6278 E->getRParenLoc());
6281 template<typename Derived>
6282 ExprResult
6283 TreeTransform<Derived>::TransformCXXDependentScopeMemberExpr(
6284 CXXDependentScopeMemberExpr *E) {
6285 // Transform the base of the expression.
6286 ExprResult Base((Expr*) 0);
6287 Expr *OldBase;
6288 QualType BaseType;
6289 QualType ObjectType;
6290 if (!E->isImplicitAccess()) {
6291 OldBase = E->getBase();
6292 Base = getDerived().TransformExpr(OldBase);
6293 if (Base.isInvalid())
6294 return ExprError();
6296 // Start the member reference and compute the object's type.
6297 ParsedType ObjectTy;
6298 bool MayBePseudoDestructor = false;
6299 Base = SemaRef.ActOnStartCXXMemberReference(0, Base.get(),
6300 E->getOperatorLoc(),
6301 E->isArrow()? tok::arrow : tok::period,
6302 ObjectTy,
6303 MayBePseudoDestructor);
6304 if (Base.isInvalid())
6305 return ExprError();
6307 ObjectType = ObjectTy.get();
6308 BaseType = ((Expr*) Base.get())->getType();
6309 } else {
6310 OldBase = 0;
6311 BaseType = getDerived().TransformType(E->getBaseType());
6312 ObjectType = BaseType->getAs<PointerType>()->getPointeeType();
6315 // Transform the first part of the nested-name-specifier that qualifies
6316 // the member name.
6317 NamedDecl *FirstQualifierInScope
6318 = getDerived().TransformFirstQualifierInScope(
6319 E->getFirstQualifierFoundInScope(),
6320 E->getQualifierRange().getBegin());
6322 NestedNameSpecifier *Qualifier = 0;
6323 if (E->getQualifier()) {
6324 Qualifier = getDerived().TransformNestedNameSpecifier(E->getQualifier(),
6325 E->getQualifierRange(),
6326 ObjectType,
6327 FirstQualifierInScope);
6328 if (!Qualifier)
6329 return ExprError();
6332 // TODO: If this is a conversion-function-id, verify that the
6333 // destination type name (if present) resolves the same way after
6334 // instantiation as it did in the local scope.
6336 DeclarationNameInfo NameInfo
6337 = getDerived().TransformDeclarationNameInfo(E->getMemberNameInfo());
6338 if (!NameInfo.getName())
6339 return ExprError();
6341 if (!E->hasExplicitTemplateArgs()) {
6342 // This is a reference to a member without an explicitly-specified
6343 // template argument list. Optimize for this common case.
6344 if (!getDerived().AlwaysRebuild() &&
6345 Base.get() == OldBase &&
6346 BaseType == E->getBaseType() &&
6347 Qualifier == E->getQualifier() &&
6348 NameInfo.getName() == E->getMember() &&
6349 FirstQualifierInScope == E->getFirstQualifierFoundInScope())
6350 return SemaRef.Owned(E);
6352 return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(),
6353 BaseType,
6354 E->isArrow(),
6355 E->getOperatorLoc(),
6356 Qualifier,
6357 E->getQualifierRange(),
6358 FirstQualifierInScope,
6359 NameInfo,
6360 /*TemplateArgs*/ 0);
6363 TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc());
6364 if (getDerived().TransformTemplateArguments(E->getTemplateArgs(),
6365 E->getNumTemplateArgs(),
6366 TransArgs))
6367 return ExprError();
6369 return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(),
6370 BaseType,
6371 E->isArrow(),
6372 E->getOperatorLoc(),
6373 Qualifier,
6374 E->getQualifierRange(),
6375 FirstQualifierInScope,
6376 NameInfo,
6377 &TransArgs);
6380 template<typename Derived>
6381 ExprResult
6382 TreeTransform<Derived>::TransformUnresolvedMemberExpr(UnresolvedMemberExpr *Old) {
6383 // Transform the base of the expression.
6384 ExprResult Base((Expr*) 0);
6385 QualType BaseType;
6386 if (!Old->isImplicitAccess()) {
6387 Base = getDerived().TransformExpr(Old->getBase());
6388 if (Base.isInvalid())
6389 return ExprError();
6390 BaseType = ((Expr*) Base.get())->getType();
6391 } else {
6392 BaseType = getDerived().TransformType(Old->getBaseType());
6395 NestedNameSpecifier *Qualifier = 0;
6396 if (Old->getQualifier()) {
6397 Qualifier
6398 = getDerived().TransformNestedNameSpecifier(Old->getQualifier(),
6399 Old->getQualifierRange());
6400 if (Qualifier == 0)
6401 return ExprError();
6404 LookupResult R(SemaRef, Old->getMemberNameInfo(),
6405 Sema::LookupOrdinaryName);
6407 // Transform all the decls.
6408 for (UnresolvedMemberExpr::decls_iterator I = Old->decls_begin(),
6409 E = Old->decls_end(); I != E; ++I) {
6410 NamedDecl *InstD = static_cast<NamedDecl*>(
6411 getDerived().TransformDecl(Old->getMemberLoc(),
6412 *I));
6413 if (!InstD) {
6414 // Silently ignore these if a UsingShadowDecl instantiated to nothing.
6415 // This can happen because of dependent hiding.
6416 if (isa<UsingShadowDecl>(*I))
6417 continue;
6418 else
6419 return ExprError();
6422 // Expand using declarations.
6423 if (isa<UsingDecl>(InstD)) {
6424 UsingDecl *UD = cast<UsingDecl>(InstD);
6425 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
6426 E = UD->shadow_end(); I != E; ++I)
6427 R.addDecl(*I);
6428 continue;
6431 R.addDecl(InstD);
6434 R.resolveKind();
6436 // Determine the naming class.
6437 if (Old->getNamingClass()) {
6438 CXXRecordDecl *NamingClass
6439 = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl(
6440 Old->getMemberLoc(),
6441 Old->getNamingClass()));
6442 if (!NamingClass)
6443 return ExprError();
6445 R.setNamingClass(NamingClass);
6448 TemplateArgumentListInfo TransArgs;
6449 if (Old->hasExplicitTemplateArgs()) {
6450 TransArgs.setLAngleLoc(Old->getLAngleLoc());
6451 TransArgs.setRAngleLoc(Old->getRAngleLoc());
6452 if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(),
6453 Old->getNumTemplateArgs(),
6454 TransArgs))
6455 return ExprError();
6458 // FIXME: to do this check properly, we will need to preserve the
6459 // first-qualifier-in-scope here, just in case we had a dependent
6460 // base (and therefore couldn't do the check) and a
6461 // nested-name-qualifier (and therefore could do the lookup).
6462 NamedDecl *FirstQualifierInScope = 0;
6464 return getDerived().RebuildUnresolvedMemberExpr(Base.get(),
6465 BaseType,
6466 Old->getOperatorLoc(),
6467 Old->isArrow(),
6468 Qualifier,
6469 Old->getQualifierRange(),
6470 FirstQualifierInScope,
6472 (Old->hasExplicitTemplateArgs()
6473 ? &TransArgs : 0));
6476 template<typename Derived>
6477 ExprResult
6478 TreeTransform<Derived>::TransformCXXNoexceptExpr(CXXNoexceptExpr *E) {
6479 ExprResult SubExpr = getDerived().TransformExpr(E->getOperand());
6480 if (SubExpr.isInvalid())
6481 return ExprError();
6483 if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getOperand())
6484 return SemaRef.Owned(E);
6486 return getDerived().RebuildCXXNoexceptExpr(E->getSourceRange(),SubExpr.get());
6489 template<typename Derived>
6490 ExprResult
6491 TreeTransform<Derived>::TransformPackExpansionExpr(PackExpansionExpr *E) {
6492 llvm_unreachable("pack expansion expression in unhandled context");
6493 return ExprError();
6496 template<typename Derived>
6497 ExprResult
6498 TreeTransform<Derived>::TransformObjCStringLiteral(ObjCStringLiteral *E) {
6499 return SemaRef.Owned(E);
6502 template<typename Derived>
6503 ExprResult
6504 TreeTransform<Derived>::TransformObjCEncodeExpr(ObjCEncodeExpr *E) {
6505 TypeSourceInfo *EncodedTypeInfo
6506 = getDerived().TransformType(E->getEncodedTypeSourceInfo());
6507 if (!EncodedTypeInfo)
6508 return ExprError();
6510 if (!getDerived().AlwaysRebuild() &&
6511 EncodedTypeInfo == E->getEncodedTypeSourceInfo())
6512 return SemaRef.Owned(E);
6514 return getDerived().RebuildObjCEncodeExpr(E->getAtLoc(),
6515 EncodedTypeInfo,
6516 E->getRParenLoc());
6519 template<typename Derived>
6520 ExprResult
6521 TreeTransform<Derived>::TransformObjCMessageExpr(ObjCMessageExpr *E) {
6522 // Transform arguments.
6523 bool ArgChanged = false;
6524 ASTOwningVector<Expr*> Args(SemaRef);
6525 Args.reserve(E->getNumArgs());
6526 if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), false, Args,
6527 &ArgChanged))
6528 return ExprError();
6530 if (E->getReceiverKind() == ObjCMessageExpr::Class) {
6531 // Class message: transform the receiver type.
6532 TypeSourceInfo *ReceiverTypeInfo
6533 = getDerived().TransformType(E->getClassReceiverTypeInfo());
6534 if (!ReceiverTypeInfo)
6535 return ExprError();
6537 // If nothing changed, just retain the existing message send.
6538 if (!getDerived().AlwaysRebuild() &&
6539 ReceiverTypeInfo == E->getClassReceiverTypeInfo() && !ArgChanged)
6540 return SemaRef.Owned(E);
6542 // Build a new class message send.
6543 return getDerived().RebuildObjCMessageExpr(ReceiverTypeInfo,
6544 E->getSelector(),
6545 E->getSelectorLoc(),
6546 E->getMethodDecl(),
6547 E->getLeftLoc(),
6548 move_arg(Args),
6549 E->getRightLoc());
6552 // Instance message: transform the receiver
6553 assert(E->getReceiverKind() == ObjCMessageExpr::Instance &&
6554 "Only class and instance messages may be instantiated");
6555 ExprResult Receiver
6556 = getDerived().TransformExpr(E->getInstanceReceiver());
6557 if (Receiver.isInvalid())
6558 return ExprError();
6560 // If nothing changed, just retain the existing message send.
6561 if (!getDerived().AlwaysRebuild() &&
6562 Receiver.get() == E->getInstanceReceiver() && !ArgChanged)
6563 return SemaRef.Owned(E);
6565 // Build a new instance message send.
6566 return getDerived().RebuildObjCMessageExpr(Receiver.get(),
6567 E->getSelector(),
6568 E->getSelectorLoc(),
6569 E->getMethodDecl(),
6570 E->getLeftLoc(),
6571 move_arg(Args),
6572 E->getRightLoc());
6575 template<typename Derived>
6576 ExprResult
6577 TreeTransform<Derived>::TransformObjCSelectorExpr(ObjCSelectorExpr *E) {
6578 return SemaRef.Owned(E);
6581 template<typename Derived>
6582 ExprResult
6583 TreeTransform<Derived>::TransformObjCProtocolExpr(ObjCProtocolExpr *E) {
6584 return SemaRef.Owned(E);
6587 template<typename Derived>
6588 ExprResult
6589 TreeTransform<Derived>::TransformObjCIvarRefExpr(ObjCIvarRefExpr *E) {
6590 // Transform the base expression.
6591 ExprResult Base = getDerived().TransformExpr(E->getBase());
6592 if (Base.isInvalid())
6593 return ExprError();
6595 // We don't need to transform the ivar; it will never change.
6597 // If nothing changed, just retain the existing expression.
6598 if (!getDerived().AlwaysRebuild() &&
6599 Base.get() == E->getBase())
6600 return SemaRef.Owned(E);
6602 return getDerived().RebuildObjCIvarRefExpr(Base.get(), E->getDecl(),
6603 E->getLocation(),
6604 E->isArrow(), E->isFreeIvar());
6607 template<typename Derived>
6608 ExprResult
6609 TreeTransform<Derived>::TransformObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
6610 // 'super' and types never change. Property never changes. Just
6611 // retain the existing expression.
6612 if (!E->isObjectReceiver())
6613 return SemaRef.Owned(E);
6615 // Transform the base expression.
6616 ExprResult Base = getDerived().TransformExpr(E->getBase());
6617 if (Base.isInvalid())
6618 return ExprError();
6620 // We don't need to transform the property; it will never change.
6622 // If nothing changed, just retain the existing expression.
6623 if (!getDerived().AlwaysRebuild() &&
6624 Base.get() == E->getBase())
6625 return SemaRef.Owned(E);
6627 if (E->isExplicitProperty())
6628 return getDerived().RebuildObjCPropertyRefExpr(Base.get(),
6629 E->getExplicitProperty(),
6630 E->getLocation());
6632 return getDerived().RebuildObjCPropertyRefExpr(Base.get(),
6633 E->getType(),
6634 E->getImplicitPropertyGetter(),
6635 E->getImplicitPropertySetter(),
6636 E->getLocation());
6639 template<typename Derived>
6640 ExprResult
6641 TreeTransform<Derived>::TransformObjCIsaExpr(ObjCIsaExpr *E) {
6642 // Transform the base expression.
6643 ExprResult Base = getDerived().TransformExpr(E->getBase());
6644 if (Base.isInvalid())
6645 return ExprError();
6647 // If nothing changed, just retain the existing expression.
6648 if (!getDerived().AlwaysRebuild() &&
6649 Base.get() == E->getBase())
6650 return SemaRef.Owned(E);
6652 return getDerived().RebuildObjCIsaExpr(Base.get(), E->getIsaMemberLoc(),
6653 E->isArrow());
6656 template<typename Derived>
6657 ExprResult
6658 TreeTransform<Derived>::TransformShuffleVectorExpr(ShuffleVectorExpr *E) {
6659 bool ArgumentChanged = false;
6660 ASTOwningVector<Expr*> SubExprs(SemaRef);
6661 SubExprs.reserve(E->getNumSubExprs());
6662 if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false,
6663 SubExprs, &ArgumentChanged))
6664 return ExprError();
6666 if (!getDerived().AlwaysRebuild() &&
6667 !ArgumentChanged)
6668 return SemaRef.Owned(E);
6670 return getDerived().RebuildShuffleVectorExpr(E->getBuiltinLoc(),
6671 move_arg(SubExprs),
6672 E->getRParenLoc());
6675 template<typename Derived>
6676 ExprResult
6677 TreeTransform<Derived>::TransformBlockExpr(BlockExpr *E) {
6678 SourceLocation CaretLoc(E->getExprLoc());
6680 SemaRef.ActOnBlockStart(CaretLoc, /*Scope=*/0);
6681 BlockScopeInfo *CurBlock = SemaRef.getCurBlock();
6682 CurBlock->TheDecl->setIsVariadic(E->getBlockDecl()->isVariadic());
6683 llvm::SmallVector<ParmVarDecl*, 4> Params;
6684 llvm::SmallVector<QualType, 4> ParamTypes;
6686 // Parameter substitution.
6687 const BlockDecl *BD = E->getBlockDecl();
6688 for (BlockDecl::param_const_iterator P = BD->param_begin(),
6689 EN = BD->param_end(); P != EN; ++P) {
6690 ParmVarDecl *OldParm = (*P);
6691 ParmVarDecl *NewParm = getDerived().TransformFunctionTypeParam(OldParm);
6692 QualType NewType = NewParm->getType();
6693 Params.push_back(NewParm);
6694 ParamTypes.push_back(NewParm->getType());
6697 const FunctionType *BExprFunctionType = E->getFunctionType();
6698 QualType BExprResultType = BExprFunctionType->getResultType();
6699 if (!BExprResultType.isNull()) {
6700 if (!BExprResultType->isDependentType())
6701 CurBlock->ReturnType = BExprResultType;
6702 else if (BExprResultType != SemaRef.Context.DependentTy)
6703 CurBlock->ReturnType = getDerived().TransformType(BExprResultType);
6706 // Transform the body
6707 StmtResult Body = getDerived().TransformStmt(E->getBody());
6708 if (Body.isInvalid())
6709 return ExprError();
6710 // Set the parameters on the block decl.
6711 if (!Params.empty())
6712 CurBlock->TheDecl->setParams(Params.data(), Params.size());
6714 QualType FunctionType = getDerived().RebuildFunctionProtoType(
6715 CurBlock->ReturnType,
6716 ParamTypes.data(),
6717 ParamTypes.size(),
6718 BD->isVariadic(),
6720 BExprFunctionType->getExtInfo());
6722 CurBlock->FunctionType = FunctionType;
6723 return SemaRef.ActOnBlockStmtExpr(CaretLoc, Body.get(), /*Scope=*/0);
6726 template<typename Derived>
6727 ExprResult
6728 TreeTransform<Derived>::TransformBlockDeclRefExpr(BlockDeclRefExpr *E) {
6729 NestedNameSpecifier *Qualifier = 0;
6731 ValueDecl *ND
6732 = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getLocation(),
6733 E->getDecl()));
6734 if (!ND)
6735 return ExprError();
6737 if (!getDerived().AlwaysRebuild() &&
6738 ND == E->getDecl()) {
6739 // Mark it referenced in the new context regardless.
6740 // FIXME: this is a bit instantiation-specific.
6741 SemaRef.MarkDeclarationReferenced(E->getLocation(), ND);
6743 return SemaRef.Owned(E);
6746 DeclarationNameInfo NameInfo(E->getDecl()->getDeclName(), E->getLocation());
6747 return getDerived().RebuildDeclRefExpr(Qualifier, SourceLocation(),
6748 ND, NameInfo, 0);
6751 //===----------------------------------------------------------------------===//
6752 // Type reconstruction
6753 //===----------------------------------------------------------------------===//
6755 template<typename Derived>
6756 QualType TreeTransform<Derived>::RebuildPointerType(QualType PointeeType,
6757 SourceLocation Star) {
6758 return SemaRef.BuildPointerType(PointeeType, Star,
6759 getDerived().getBaseEntity());
6762 template<typename Derived>
6763 QualType TreeTransform<Derived>::RebuildBlockPointerType(QualType PointeeType,
6764 SourceLocation Star) {
6765 return SemaRef.BuildBlockPointerType(PointeeType, Star,
6766 getDerived().getBaseEntity());
6769 template<typename Derived>
6770 QualType
6771 TreeTransform<Derived>::RebuildReferenceType(QualType ReferentType,
6772 bool WrittenAsLValue,
6773 SourceLocation Sigil) {
6774 return SemaRef.BuildReferenceType(ReferentType, WrittenAsLValue,
6775 Sigil, getDerived().getBaseEntity());
6778 template<typename Derived>
6779 QualType
6780 TreeTransform<Derived>::RebuildMemberPointerType(QualType PointeeType,
6781 QualType ClassType,
6782 SourceLocation Sigil) {
6783 return SemaRef.BuildMemberPointerType(PointeeType, ClassType,
6784 Sigil, getDerived().getBaseEntity());
6787 template<typename Derived>
6788 QualType
6789 TreeTransform<Derived>::RebuildArrayType(QualType ElementType,
6790 ArrayType::ArraySizeModifier SizeMod,
6791 const llvm::APInt *Size,
6792 Expr *SizeExpr,
6793 unsigned IndexTypeQuals,
6794 SourceRange BracketsRange) {
6795 if (SizeExpr || !Size)
6796 return SemaRef.BuildArrayType(ElementType, SizeMod, SizeExpr,
6797 IndexTypeQuals, BracketsRange,
6798 getDerived().getBaseEntity());
6800 QualType Types[] = {
6801 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
6802 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
6803 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
6805 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
6806 QualType SizeType;
6807 for (unsigned I = 0; I != NumTypes; ++I)
6808 if (Size->getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
6809 SizeType = Types[I];
6810 break;
6813 IntegerLiteral ArraySize(SemaRef.Context, *Size, SizeType,
6814 /*FIXME*/BracketsRange.getBegin());
6815 return SemaRef.BuildArrayType(ElementType, SizeMod, &ArraySize,
6816 IndexTypeQuals, BracketsRange,
6817 getDerived().getBaseEntity());
6820 template<typename Derived>
6821 QualType
6822 TreeTransform<Derived>::RebuildConstantArrayType(QualType ElementType,
6823 ArrayType::ArraySizeModifier SizeMod,
6824 const llvm::APInt &Size,
6825 unsigned IndexTypeQuals,
6826 SourceRange BracketsRange) {
6827 return getDerived().RebuildArrayType(ElementType, SizeMod, &Size, 0,
6828 IndexTypeQuals, BracketsRange);
6831 template<typename Derived>
6832 QualType
6833 TreeTransform<Derived>::RebuildIncompleteArrayType(QualType ElementType,
6834 ArrayType::ArraySizeModifier SizeMod,
6835 unsigned IndexTypeQuals,
6836 SourceRange BracketsRange) {
6837 return getDerived().RebuildArrayType(ElementType, SizeMod, 0, 0,
6838 IndexTypeQuals, BracketsRange);
6841 template<typename Derived>
6842 QualType
6843 TreeTransform<Derived>::RebuildVariableArrayType(QualType ElementType,
6844 ArrayType::ArraySizeModifier SizeMod,
6845 Expr *SizeExpr,
6846 unsigned IndexTypeQuals,
6847 SourceRange BracketsRange) {
6848 return getDerived().RebuildArrayType(ElementType, SizeMod, 0,
6849 SizeExpr,
6850 IndexTypeQuals, BracketsRange);
6853 template<typename Derived>
6854 QualType
6855 TreeTransform<Derived>::RebuildDependentSizedArrayType(QualType ElementType,
6856 ArrayType::ArraySizeModifier SizeMod,
6857 Expr *SizeExpr,
6858 unsigned IndexTypeQuals,
6859 SourceRange BracketsRange) {
6860 return getDerived().RebuildArrayType(ElementType, SizeMod, 0,
6861 SizeExpr,
6862 IndexTypeQuals, BracketsRange);
6865 template<typename Derived>
6866 QualType TreeTransform<Derived>::RebuildVectorType(QualType ElementType,
6867 unsigned NumElements,
6868 VectorType::VectorKind VecKind) {
6869 // FIXME: semantic checking!
6870 return SemaRef.Context.getVectorType(ElementType, NumElements, VecKind);
6873 template<typename Derived>
6874 QualType TreeTransform<Derived>::RebuildExtVectorType(QualType ElementType,
6875 unsigned NumElements,
6876 SourceLocation AttributeLoc) {
6877 llvm::APInt numElements(SemaRef.Context.getIntWidth(SemaRef.Context.IntTy),
6878 NumElements, true);
6879 IntegerLiteral *VectorSize
6880 = IntegerLiteral::Create(SemaRef.Context, numElements, SemaRef.Context.IntTy,
6881 AttributeLoc);
6882 return SemaRef.BuildExtVectorType(ElementType, VectorSize, AttributeLoc);
6885 template<typename Derived>
6886 QualType
6887 TreeTransform<Derived>::RebuildDependentSizedExtVectorType(QualType ElementType,
6888 Expr *SizeExpr,
6889 SourceLocation AttributeLoc) {
6890 return SemaRef.BuildExtVectorType(ElementType, SizeExpr, AttributeLoc);
6893 template<typename Derived>
6894 QualType TreeTransform<Derived>::RebuildFunctionProtoType(QualType T,
6895 QualType *ParamTypes,
6896 unsigned NumParamTypes,
6897 bool Variadic,
6898 unsigned Quals,
6899 const FunctionType::ExtInfo &Info) {
6900 return SemaRef.BuildFunctionType(T, ParamTypes, NumParamTypes, Variadic,
6901 Quals,
6902 getDerived().getBaseLocation(),
6903 getDerived().getBaseEntity(),
6904 Info);
6907 template<typename Derived>
6908 QualType TreeTransform<Derived>::RebuildFunctionNoProtoType(QualType T) {
6909 return SemaRef.Context.getFunctionNoProtoType(T);
6912 template<typename Derived>
6913 QualType TreeTransform<Derived>::RebuildUnresolvedUsingType(Decl *D) {
6914 assert(D && "no decl found");
6915 if (D->isInvalidDecl()) return QualType();
6917 // FIXME: Doesn't account for ObjCInterfaceDecl!
6918 TypeDecl *Ty;
6919 if (isa<UsingDecl>(D)) {
6920 UsingDecl *Using = cast<UsingDecl>(D);
6921 assert(Using->isTypeName() &&
6922 "UnresolvedUsingTypenameDecl transformed to non-typename using");
6924 // A valid resolved using typename decl points to exactly one type decl.
6925 assert(++Using->shadow_begin() == Using->shadow_end());
6926 Ty = cast<TypeDecl>((*Using->shadow_begin())->getTargetDecl());
6928 } else {
6929 assert(isa<UnresolvedUsingTypenameDecl>(D) &&
6930 "UnresolvedUsingTypenameDecl transformed to non-using decl");
6931 Ty = cast<UnresolvedUsingTypenameDecl>(D);
6934 return SemaRef.Context.getTypeDeclType(Ty);
6937 template<typename Derived>
6938 QualType TreeTransform<Derived>::RebuildTypeOfExprType(Expr *E,
6939 SourceLocation Loc) {
6940 return SemaRef.BuildTypeofExprType(E, Loc);
6943 template<typename Derived>
6944 QualType TreeTransform<Derived>::RebuildTypeOfType(QualType Underlying) {
6945 return SemaRef.Context.getTypeOfType(Underlying);
6948 template<typename Derived>
6949 QualType TreeTransform<Derived>::RebuildDecltypeType(Expr *E,
6950 SourceLocation Loc) {
6951 return SemaRef.BuildDecltypeType(E, Loc);
6954 template<typename Derived>
6955 QualType TreeTransform<Derived>::RebuildTemplateSpecializationType(
6956 TemplateName Template,
6957 SourceLocation TemplateNameLoc,
6958 const TemplateArgumentListInfo &TemplateArgs) {
6959 return SemaRef.CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6962 template<typename Derived>
6963 NestedNameSpecifier *
6964 TreeTransform<Derived>::RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
6965 SourceRange Range,
6966 IdentifierInfo &II,
6967 QualType ObjectType,
6968 NamedDecl *FirstQualifierInScope) {
6969 CXXScopeSpec SS;
6970 // FIXME: The source location information is all wrong.
6971 SS.setRange(Range);
6972 SS.setScopeRep(Prefix);
6973 return static_cast<NestedNameSpecifier *>(
6974 SemaRef.BuildCXXNestedNameSpecifier(0, SS, Range.getEnd(),
6975 Range.getEnd(), II,
6976 ObjectType,
6977 FirstQualifierInScope,
6978 false, false));
6981 template<typename Derived>
6982 NestedNameSpecifier *
6983 TreeTransform<Derived>::RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
6984 SourceRange Range,
6985 NamespaceDecl *NS) {
6986 return NestedNameSpecifier::Create(SemaRef.Context, Prefix, NS);
6989 template<typename Derived>
6990 NestedNameSpecifier *
6991 TreeTransform<Derived>::RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
6992 SourceRange Range,
6993 bool TemplateKW,
6994 QualType T) {
6995 if (T->isDependentType() || T->isRecordType() ||
6996 (SemaRef.getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
6997 assert(!T.hasLocalQualifiers() && "Can't get cv-qualifiers here");
6998 return NestedNameSpecifier::Create(SemaRef.Context, Prefix, TemplateKW,
6999 T.getTypePtr());
7002 SemaRef.Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
7003 return 0;
7006 template<typename Derived>
7007 TemplateName
7008 TreeTransform<Derived>::RebuildTemplateName(NestedNameSpecifier *Qualifier,
7009 bool TemplateKW,
7010 TemplateDecl *Template) {
7011 return SemaRef.Context.getQualifiedTemplateName(Qualifier, TemplateKW,
7012 Template);
7015 template<typename Derived>
7016 TemplateName
7017 TreeTransform<Derived>::RebuildTemplateName(NestedNameSpecifier *Qualifier,
7018 SourceRange QualifierRange,
7019 const IdentifierInfo &II,
7020 QualType ObjectType,
7021 NamedDecl *FirstQualifierInScope) {
7022 CXXScopeSpec SS;
7023 SS.setRange(QualifierRange);
7024 SS.setScopeRep(Qualifier);
7025 UnqualifiedId Name;
7026 Name.setIdentifier(&II, /*FIXME:*/getDerived().getBaseLocation());
7027 Sema::TemplateTy Template;
7028 getSema().ActOnDependentTemplateName(/*Scope=*/0,
7029 /*FIXME:*/getDerived().getBaseLocation(),
7031 Name,
7032 ParsedType::make(ObjectType),
7033 /*EnteringContext=*/false,
7034 Template);
7035 return Template.get();
7038 template<typename Derived>
7039 TemplateName
7040 TreeTransform<Derived>::RebuildTemplateName(NestedNameSpecifier *Qualifier,
7041 OverloadedOperatorKind Operator,
7042 QualType ObjectType) {
7043 CXXScopeSpec SS;
7044 SS.setRange(SourceRange(getDerived().getBaseLocation()));
7045 SS.setScopeRep(Qualifier);
7046 UnqualifiedId Name;
7047 SourceLocation SymbolLocations[3]; // FIXME: Bogus location information.
7048 Name.setOperatorFunctionId(/*FIXME:*/getDerived().getBaseLocation(),
7049 Operator, SymbolLocations);
7050 Sema::TemplateTy Template;
7051 getSema().ActOnDependentTemplateName(/*Scope=*/0,
7052 /*FIXME:*/getDerived().getBaseLocation(),
7054 Name,
7055 ParsedType::make(ObjectType),
7056 /*EnteringContext=*/false,
7057 Template);
7058 return Template.template getAsVal<TemplateName>();
7061 template<typename Derived>
7062 ExprResult
7063 TreeTransform<Derived>::RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
7064 SourceLocation OpLoc,
7065 Expr *OrigCallee,
7066 Expr *First,
7067 Expr *Second) {
7068 Expr *Callee = OrigCallee->IgnoreParenCasts();
7069 bool isPostIncDec = Second && (Op == OO_PlusPlus || Op == OO_MinusMinus);
7071 // Determine whether this should be a builtin operation.
7072 if (Op == OO_Subscript) {
7073 if (!First->getType()->isOverloadableType() &&
7074 !Second->getType()->isOverloadableType())
7075 return getSema().CreateBuiltinArraySubscriptExpr(First,
7076 Callee->getLocStart(),
7077 Second, OpLoc);
7078 } else if (Op == OO_Arrow) {
7079 // -> is never a builtin operation.
7080 return SemaRef.BuildOverloadedArrowExpr(0, First, OpLoc);
7081 } else if (Second == 0 || isPostIncDec) {
7082 if (!First->getType()->isOverloadableType()) {
7083 // The argument is not of overloadable type, so try to create a
7084 // built-in unary operation.
7085 UnaryOperatorKind Opc
7086 = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec);
7088 return getSema().CreateBuiltinUnaryOp(OpLoc, Opc, First);
7090 } else {
7091 if (!First->getType()->isOverloadableType() &&
7092 !Second->getType()->isOverloadableType()) {
7093 // Neither of the arguments is an overloadable type, so try to
7094 // create a built-in binary operation.
7095 BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op);
7096 ExprResult Result
7097 = SemaRef.CreateBuiltinBinOp(OpLoc, Opc, First, Second);
7098 if (Result.isInvalid())
7099 return ExprError();
7101 return move(Result);
7105 // Compute the transformed set of functions (and function templates) to be
7106 // used during overload resolution.
7107 UnresolvedSet<16> Functions;
7109 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Callee)) {
7110 assert(ULE->requiresADL());
7112 // FIXME: Do we have to check
7113 // IsAcceptableNonMemberOperatorCandidate for each of these?
7114 Functions.append(ULE->decls_begin(), ULE->decls_end());
7115 } else {
7116 Functions.addDecl(cast<DeclRefExpr>(Callee)->getDecl());
7119 // Add any functions found via argument-dependent lookup.
7120 Expr *Args[2] = { First, Second };
7121 unsigned NumArgs = 1 + (Second != 0);
7123 // Create the overloaded operator invocation for unary operators.
7124 if (NumArgs == 1 || isPostIncDec) {
7125 UnaryOperatorKind Opc
7126 = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec);
7127 return SemaRef.CreateOverloadedUnaryOp(OpLoc, Opc, Functions, First);
7130 if (Op == OO_Subscript)
7131 return SemaRef.CreateOverloadedArraySubscriptExpr(Callee->getLocStart(),
7132 OpLoc,
7133 First,
7134 Second);
7136 // Create the overloaded operator invocation for binary operators.
7137 BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op);
7138 ExprResult Result
7139 = SemaRef.CreateOverloadedBinOp(OpLoc, Opc, Functions, Args[0], Args[1]);
7140 if (Result.isInvalid())
7141 return ExprError();
7143 return move(Result);
7146 template<typename Derived>
7147 ExprResult
7148 TreeTransform<Derived>::RebuildCXXPseudoDestructorExpr(Expr *Base,
7149 SourceLocation OperatorLoc,
7150 bool isArrow,
7151 NestedNameSpecifier *Qualifier,
7152 SourceRange QualifierRange,
7153 TypeSourceInfo *ScopeType,
7154 SourceLocation CCLoc,
7155 SourceLocation TildeLoc,
7156 PseudoDestructorTypeStorage Destroyed) {
7157 CXXScopeSpec SS;
7158 if (Qualifier) {
7159 SS.setRange(QualifierRange);
7160 SS.setScopeRep(Qualifier);
7163 QualType BaseType = Base->getType();
7164 if (Base->isTypeDependent() || Destroyed.getIdentifier() ||
7165 (!isArrow && !BaseType->getAs<RecordType>()) ||
7166 (isArrow && BaseType->getAs<PointerType>() &&
7167 !BaseType->getAs<PointerType>()->getPointeeType()
7168 ->template getAs<RecordType>())){
7169 // This pseudo-destructor expression is still a pseudo-destructor.
7170 return SemaRef.BuildPseudoDestructorExpr(Base, OperatorLoc,
7171 isArrow? tok::arrow : tok::period,
7172 SS, ScopeType, CCLoc, TildeLoc,
7173 Destroyed,
7174 /*FIXME?*/true);
7177 TypeSourceInfo *DestroyedType = Destroyed.getTypeSourceInfo();
7178 DeclarationName Name(SemaRef.Context.DeclarationNames.getCXXDestructorName(
7179 SemaRef.Context.getCanonicalType(DestroyedType->getType())));
7180 DeclarationNameInfo NameInfo(Name, Destroyed.getLocation());
7181 NameInfo.setNamedTypeInfo(DestroyedType);
7183 // FIXME: the ScopeType should be tacked onto SS.
7185 return getSema().BuildMemberReferenceExpr(Base, BaseType,
7186 OperatorLoc, isArrow,
7187 SS, /*FIXME: FirstQualifier*/ 0,
7188 NameInfo,
7189 /*TemplateArgs*/ 0);
7192 } // end namespace clang
7194 #endif // LLVM_CLANG_SEMA_TREETRANSFORM_H