[cfe-commits] r122761 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/AST/ExprCXX.h include/clang/AST/ExprObjC.h lib/AST/Expr.cpp lib/Sema/TreeTransform.h
Douglas Gregor
dgregor at apple.com
Mon Jan 3 11:04:46 PST 2011
Author: dgregor
Date: Mon Jan 3 13:04:46 2011
New Revision: 122761
URL: http://llvm.org/viewvc/llvm-project?rev=122761&view=rev
Log:
Refactor the tree transform's many loops over sets of expressions
(transforming each in turn) into calls into one central routine
(TransformExprs) that transforms a list of expressions. This
refactoring is preparatory work for pack expansions whose in an
expression-list.
No functionality change.
Modified:
cfe/trunk/include/clang/AST/Expr.h
cfe/trunk/include/clang/AST/ExprCXX.h
cfe/trunk/include/clang/AST/ExprObjC.h
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/lib/Sema/TreeTransform.h
Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=122761&r1=122760&r2=122761&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Jan 3 13:04:46 2011
@@ -1694,6 +1694,9 @@
///
unsigned getNumArgs() const { return NumArgs; }
+ /// \brief Retrieve the call arguments.
+ Expr **getArgs() { return reinterpret_cast<Expr **>(SubExprs + ARGS_START); }
+
/// getArg - Return the specified argument.
Expr *getArg(unsigned Arg) {
assert(Arg < NumArgs && "Arg access out of range!");
@@ -2737,6 +2740,9 @@
/// pointers.
unsigned getNumSubExprs() const { return NumExprs; }
+ /// \brief Retrieve the array of expressions.
+ Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); }
+
/// getExpr - Return the Expr at the specified index.
Expr *getExpr(unsigned Index) {
assert((Index < NumExprs) && "Arg access out of range!");
@@ -2971,6 +2977,9 @@
unsigned getNumInits() const { return InitExprs.size(); }
+ /// \brief Retrieve the set of initializers.
+ Expr **getInits() { return reinterpret_cast<Expr **>(InitExprs.data()); }
+
const Expr *getInit(unsigned Init) const {
assert(Init < getNumInits() && "Initializer access out of range!");
return cast_or_null<Expr>(InitExprs[Init]);
Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=122761&r1=122760&r2=122761&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Mon Jan 3 13:04:46 2011
@@ -21,11 +21,11 @@
namespace clang {
- class CXXConstructorDecl;
- class CXXDestructorDecl;
- class CXXMethodDecl;
- class CXXTemporary;
- class TemplateArgumentListInfo;
+class CXXConstructorDecl;
+class CXXDestructorDecl;
+class CXXMethodDecl;
+class CXXTemporary;
+class TemplateArgumentListInfo;
//===--------------------------------------------------------------------===//
// C++ Expressions.
@@ -1052,6 +1052,10 @@
}
unsigned getNumPlacementArgs() const { return NumPlacementArgs; }
+ Expr **getPlacementArgs() {
+ return reinterpret_cast<Expr **>(SubExprs + Array);
+ }
+
Expr *getPlacementArg(unsigned i) {
assert(i < NumPlacementArgs && "Index out of range");
return cast<Expr>(SubExprs[Array + i]);
@@ -1070,6 +1074,11 @@
void setHasInitializer(bool V) { Initializer = V; }
unsigned getNumConstructorArgs() const { return NumConstructorArgs; }
+
+ Expr **getConstructorArgs() {
+ return reinterpret_cast<Expr **>(SubExprs + Array + NumPlacementArgs);
+ }
+
Expr *getConstructorArg(unsigned i) {
assert(i < NumConstructorArgs && "Index out of range");
return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
Modified: cfe/trunk/include/clang/AST/ExprObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprObjC.h?rev=122761&r1=122760&r2=122761&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExprObjC.h (original)
+++ cfe/trunk/include/clang/AST/ExprObjC.h Mon Jan 3 13:04:46 2011
@@ -746,11 +746,11 @@
/// \brief Retrieve the arguments to this message, not including the
/// receiver.
- Stmt **getArgs() {
- return reinterpret_cast<Stmt **>(this + 1) + 1;
+ Expr **getArgs() {
+ return reinterpret_cast<Expr **>(this + 1) + 1;
}
- const Stmt * const *getArgs() const {
- return reinterpret_cast<const Stmt * const *>(this + 1) + 1;
+ const Expr * const *getArgs() const {
+ return reinterpret_cast<const Expr * const *>(this + 1) + 1;
}
/// getArg - Return the specified argument.
@@ -792,10 +792,16 @@
typedef ExprIterator arg_iterator;
typedef ConstExprIterator const_arg_iterator;
- arg_iterator arg_begin() { return getArgs(); }
- arg_iterator arg_end() { return getArgs() + NumArgs; }
- const_arg_iterator arg_begin() const { return getArgs(); }
- const_arg_iterator arg_end() const { return getArgs() + NumArgs; }
+ arg_iterator arg_begin() { return reinterpret_cast<Stmt **>(getArgs()); }
+ arg_iterator arg_end() {
+ return reinterpret_cast<Stmt **>(getArgs() + NumArgs);
+ }
+ const_arg_iterator arg_begin() const {
+ return reinterpret_cast<Stmt const * const*>(getArgs());
+ }
+ const_arg_iterator arg_end() const {
+ return reinterpret_cast<Stmt const * const*>(getArgs() + NumArgs);
+ }
friend class ASTStmtReader;
friend class ASTStmtWriter;
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=122761&r1=122760&r2=122761&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Jan 3 13:04:46 2011
@@ -2300,7 +2300,7 @@
SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
{
setReceiverPointer(Receiver);
- Stmt **MyArgs = getArgs();
+ Expr **MyArgs = getArgs();
for (unsigned I = 0; I != NumArgs; ++I) {
if (Args[I]->isTypeDependent())
ExprBits.TypeDependent = true;
@@ -2331,7 +2331,7 @@
SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
{
setReceiverPointer(Receiver);
- Stmt **MyArgs = getArgs();
+ Expr **MyArgs = getArgs();
for (unsigned I = 0; I != NumArgs; ++I) {
if (Args[I]->isTypeDependent())
ExprBits.TypeDependent = true;
@@ -2928,10 +2928,10 @@
Stmt::child_iterator ObjCMessageExpr::child_begin() {
if (getReceiverKind() == Instance)
return reinterpret_cast<Stmt **>(this + 1);
- return getArgs();
+ return reinterpret_cast<Stmt **>(getArgs());
}
Stmt::child_iterator ObjCMessageExpr::child_end() {
- return getArgs() + getNumArgs();
+ return reinterpret_cast<Stmt **>(getArgs() + getNumArgs());
}
// Blocks
Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=122761&r1=122760&r2=122761&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Mon Jan 3 13:04:46 2011
@@ -270,6 +270,33 @@
/// \returns the transformed expression.
ExprResult TransformExpr(Expr *E);
+ /// \brief Transform the given list of expressions.
+ ///
+ /// This routine transforms a list of expressions by invoking
+ /// \c TransformExpr() for each subexpression. However, it also provides
+ /// support for variadic templates by expanding any pack expansions (if the
+ /// derived class permits such expansion) along the way. When pack expansions
+ /// are present, the number of outputs may not equal the number of inputs.
+ ///
+ /// \param Inputs The set of expressions to be transformed.
+ ///
+ /// \param NumInputs The number of expressions in \c Inputs.
+ ///
+ /// \param IsCall If \c true, then this transform is being performed on
+ /// function-call arguments, and any arguments that should be dropped, will
+ /// be.
+ ///
+ /// \param Outputs The transformed input expressions will be added to this
+ /// vector.
+ ///
+ /// \param ArgChanged If non-NULL, will be set \c true if any argument changed
+ /// due to transformation.
+ ///
+ /// \returns true if an error occurred, false otherwise.
+ bool TransformExprs(Expr **Inputs, unsigned NumInputs, bool IsCall,
+ llvm::SmallVectorImpl<Expr *> &Outputs,
+ bool *ArgChanged = 0);
+
/// \brief Transform the given declaration, which is referenced from a type
/// or expression.
///
@@ -2159,6 +2186,34 @@
}
template<typename Derived>
+bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
+ unsigned NumInputs,
+ bool IsCall,
+ llvm::SmallVectorImpl<Expr *> &Outputs,
+ bool *ArgChanged) {
+ for (unsigned I = 0; I != NumInputs; ++I) {
+ // If requested, drop call arguments that need to be dropped.
+ if (IsCall && getDerived().DropCallArgument(Inputs[I])) {
+ if (ArgChanged)
+ *ArgChanged = true;
+
+ break;
+ }
+
+ ExprResult Result = getDerived().TransformExpr(Inputs[I]);
+ if (Result.isInvalid())
+ return true;
+
+ if (Result.get() != Inputs[I] && ArgChanged)
+ *ArgChanged = true;
+
+ Outputs.push_back(Result.get());
+ }
+
+ return false;
+}
+
+template<typename Derived>
NestedNameSpecifier *
TreeTransform<Derived>::TransformNestedNameSpecifier(NestedNameSpecifier *NNS,
SourceRange Range,
@@ -4878,15 +4933,10 @@
// Transform arguments.
bool ArgChanged = false;
ASTOwningVector<Expr*> Args(SemaRef);
- for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
- ExprResult Arg = getDerived().TransformExpr(E->getArg(I));
- if (Arg.isInvalid())
- return ExprError();
-
- ArgChanged = ArgChanged || Arg.get() != E->getArg(I);
- Args.push_back(Arg.get());
- }
-
+ if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
+ &ArgChanged))
+ return ExprError();
+
if (!getDerived().AlwaysRebuild() &&
Callee.get() == E->getCallee() &&
!ArgChanged)
@@ -5114,15 +5164,10 @@
bool InitChanged = false;
ASTOwningVector<Expr*, 4> Inits(SemaRef);
- for (unsigned I = 0, N = E->getNumInits(); I != N; ++I) {
- ExprResult Init = getDerived().TransformExpr(E->getInit(I));
- if (Init.isInvalid())
- return ExprError();
-
- InitChanged = InitChanged || Init.get() != E->getInit(I);
- Inits.push_back(Init.get());
- }
-
+ if (getDerived().TransformExprs(E->getInits(), E->getNumInits(), false,
+ Inits, &InitChanged))
+ return ExprError();
+
if (!getDerived().AlwaysRebuild() && !InitChanged)
return SemaRef.Owned(E);
@@ -5242,15 +5287,10 @@
TreeTransform<Derived>::TransformParenListExpr(ParenListExpr *E) {
bool ArgumentChanged = false;
ASTOwningVector<Expr*, 4> Inits(SemaRef);
- for (unsigned I = 0, N = E->getNumExprs(); I != N; ++I) {
- ExprResult Init = getDerived().TransformExpr(E->getExpr(I));
- if (Init.isInvalid())
- return ExprError();
-
- ArgumentChanged = ArgumentChanged || Init.get() != E->getExpr(I);
- Inits.push_back(Init.get());
- }
-
+ if (TransformExprs(E->getExprs(), E->getNumExprs(), true, Inits,
+ &ArgumentChanged))
+ return ExprError();
+
return getDerived().RebuildParenListExpr(E->getLParenLoc(),
move_arg(Inits),
E->getRParenLoc());
@@ -5344,16 +5384,9 @@
// Transform the call arguments.
ASTOwningVector<Expr*> Args(SemaRef);
- for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) {
- if (getDerived().DropCallArgument(E->getArg(I)))
- break;
-
- ExprResult Arg = getDerived().TransformExpr(E->getArg(I));
- if (Arg.isInvalid())
- return ExprError();
-
- Args.push_back(Arg.release());
- }
+ if (getDerived().TransformExprs(E->getArgs() + 1, E->getNumArgs() - 1, true,
+ Args))
+ return ExprError();
return getDerived().RebuildCallExpr(Object.get(), FakeLParenLoc,
move_arg(Args),
@@ -5662,35 +5695,16 @@
// Transform the placement arguments (if any).
bool ArgumentChanged = false;
ASTOwningVector<Expr*> PlacementArgs(SemaRef);
- for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) {
- if (getDerived().DropCallArgument(E->getPlacementArg(I))) {
- ArgumentChanged = true;
- break;
- }
-
- ExprResult Arg = getDerived().TransformExpr(E->getPlacementArg(I));
- if (Arg.isInvalid())
- return ExprError();
-
- ArgumentChanged = ArgumentChanged || Arg.get() != E->getPlacementArg(I);
- PlacementArgs.push_back(Arg.take());
- }
+ if (getDerived().TransformExprs(E->getPlacementArgs(),
+ E->getNumPlacementArgs(), true,
+ PlacementArgs, &ArgumentChanged))
+ return ExprError();
// transform the constructor arguments (if any).
ASTOwningVector<Expr*> ConstructorArgs(SemaRef);
- for (unsigned I = 0, N = E->getNumConstructorArgs(); I != N; ++I) {
- if (getDerived().DropCallArgument(E->getConstructorArg(I))) {
- ArgumentChanged = true;
- break;
- }
-
- ExprResult Arg = getDerived().TransformExpr(E->getConstructorArg(I));
- if (Arg.isInvalid())
- return ExprError();
-
- ArgumentChanged = ArgumentChanged || Arg.get() != E->getConstructorArg(I);
- ConstructorArgs.push_back(Arg.take());
- }
+ if (TransformExprs(E->getConstructorArgs(), E->getNumConstructorArgs(), true,
+ ConstructorArgs, &ArgumentChanged))
+ return ExprError();
// Transform constructor, new operator, and delete operator.
CXXConstructorDecl *Constructor = 0;
@@ -6092,22 +6106,10 @@
bool ArgumentChanged = false;
ASTOwningVector<Expr*> Args(SemaRef);
- for (CXXConstructExpr::arg_iterator Arg = E->arg_begin(),
- ArgEnd = E->arg_end();
- Arg != ArgEnd; ++Arg) {
- if (getDerived().DropCallArgument(*Arg)) {
- ArgumentChanged = true;
- break;
- }
-
- ExprResult TransArg = getDerived().TransformExpr(*Arg);
- if (TransArg.isInvalid())
- return ExprError();
-
- ArgumentChanged = ArgumentChanged || TransArg.get() != *Arg;
- Args.push_back(TransArg.get());
- }
-
+ if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
+ &ArgumentChanged))
+ return ExprError();
+
if (!getDerived().AlwaysRebuild() &&
T == E->getType() &&
Constructor == E->getConstructor() &&
@@ -6165,21 +6167,9 @@
bool ArgumentChanged = false;
ASTOwningVector<Expr*> Args(SemaRef);
Args.reserve(E->getNumArgs());
- for (CXXTemporaryObjectExpr::arg_iterator Arg = E->arg_begin(),
- ArgEnd = E->arg_end();
- Arg != ArgEnd; ++Arg) {
- if (getDerived().DropCallArgument(*Arg)) {
- ArgumentChanged = true;
- break;
- }
-
- ExprResult TransArg = getDerived().TransformExpr(*Arg);
- if (TransArg.isInvalid())
- return ExprError();
-
- ArgumentChanged = ArgumentChanged || TransArg.get() != *Arg;
- Args.push_back((Expr *)TransArg.release());
- }
+ if (TransformExprs(E->getArgs(), E->getNumArgs(), true, Args,
+ &ArgumentChanged))
+ return ExprError();
if (!getDerived().AlwaysRebuild() &&
T == E->getTypeSourceInfo() &&
@@ -6206,17 +6196,11 @@
bool ArgumentChanged = false;
ASTOwningVector<Expr*> Args(SemaRef);
- for (CXXUnresolvedConstructExpr::arg_iterator Arg = E->arg_begin(),
- ArgEnd = E->arg_end();
- Arg != ArgEnd; ++Arg) {
- ExprResult TransArg = getDerived().TransformExpr(*Arg);
- if (TransArg.isInvalid())
- return ExprError();
-
- ArgumentChanged = ArgumentChanged || TransArg.get() != *Arg;
- Args.push_back(TransArg.get());
- }
-
+ Args.reserve(E->arg_size());
+ if (getDerived().TransformExprs(E->arg_begin(), E->arg_size(), true, Args,
+ &ArgumentChanged))
+ return ExprError();
+
if (!getDerived().AlwaysRebuild() &&
T == E->getTypeSourceInfo() &&
!ArgumentChanged)
@@ -6473,15 +6457,11 @@
// Transform arguments.
bool ArgChanged = false;
ASTOwningVector<Expr*> Args(SemaRef);
- for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
- ExprResult Arg = getDerived().TransformExpr(E->getArg(I));
- if (Arg.isInvalid())
- return ExprError();
-
- ArgChanged = ArgChanged || Arg.get() != E->getArg(I);
- Args.push_back(Arg.get());
- }
-
+ Args.reserve(E->getNumArgs());
+ if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), false, Args,
+ &ArgChanged))
+ return ExprError();
+
if (E->getReceiverKind() == ObjCMessageExpr::Class) {
// Class message: transform the receiver type.
TypeSourceInfo *ReceiverTypeInfo
@@ -6613,14 +6593,10 @@
TreeTransform<Derived>::TransformShuffleVectorExpr(ShuffleVectorExpr *E) {
bool ArgumentChanged = false;
ASTOwningVector<Expr*> SubExprs(SemaRef);
- for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I) {
- ExprResult SubExpr = getDerived().TransformExpr(E->getExpr(I));
- if (SubExpr.isInvalid())
- return ExprError();
-
- ArgumentChanged = ArgumentChanged || SubExpr.get() != E->getExpr(I);
- SubExprs.push_back(SubExpr.get());
- }
+ SubExprs.reserve(E->getNumSubExprs());
+ if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false,
+ SubExprs, &ArgumentChanged))
+ return ExprError();
if (!getDerived().AlwaysRebuild() &&
!ArgumentChanged)
More information about the cfe-commits
mailing list