[cfe-commits] r50511 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/Expr.cpp lib/AST/StmtSerialization.cpp
Steve Naroff
snaroff at apple.com
Wed Apr 30 19:04:19 PDT 2008
Author: snaroff
Date: Wed Apr 30 21:04:18 2008
New Revision: 50511
URL: http://llvm.org/viewvc/llvm-project?rev=50511&view=rev
Log:
Extend InitListExpr API/IMPL to support arbitrary add/remove (in support of the initializer rewrite I am doing).
Modified:
cfe/trunk/include/clang/AST/Expr.h
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/lib/AST/StmtSerialization.cpp
Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=50511&r1=50510&r2=50511&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Wed Apr 30 21:04:18 2008
@@ -21,6 +21,7 @@
#include "clang/Basic/IdentifierTable.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/APFloat.h"
+#include <vector>
namespace clang {
class IdentifierInfo;
@@ -1280,32 +1281,42 @@
/// int x = { 1 }; int y[2] = { {1}, {2} };
///
class InitListExpr : public Expr {
- Expr **InitExprs;
- unsigned NumInits;
+ std::vector<Expr *> InitExprs;
SourceLocation LBraceLoc, RBraceLoc;
public:
InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
SourceLocation rbraceloc);
- ~InitListExpr() {
- delete [] InitExprs;
- }
- unsigned getNumInits() const { return NumInits; }
+ unsigned getNumInits() const { return InitExprs.size(); }
const Expr* getInit(unsigned Init) const {
- assert(Init < NumInits && "Initializer access out of range!");
+ assert(Init < getNumInits() && "Initializer access out of range!");
return InitExprs[Init];
}
Expr* getInit(unsigned Init) {
- assert(Init < NumInits && "Initializer access out of range!");
+ assert(Init < getNumInits() && "Initializer access out of range!");
return InitExprs[Init];
}
void setInit(unsigned Init, Expr *expr) {
- assert(Init < NumInits && "Initializer access out of range!");
+ assert(Init < getNumInits() && "Initializer access out of range!");
InitExprs[Init] = expr;
}
+
+ // Dynamic removal/addition (for constructing implicit InitExpr's).
+ void removeInit(unsigned Init) {
+ InitExprs.erase(InitExprs.begin()+Init);
+ }
+ void addInit(unsigned Init, Expr *expr) {
+ InitExprs.insert(InitExprs.begin()+Init, expr);
+ }
+
+ // Explicit InitListExpr's originate from source code (and have valid source
+ // locations). Implicit InitListExpr's are created by the semantic analyzer.
+ bool isExplicit() {
+ return LBraceLoc.isValid() && RBraceLoc.isValid();
+ }
virtual SourceRange getSourceRange() const {
return SourceRange(LBraceLoc, RBraceLoc);
@@ -1325,7 +1336,7 @@
private:
// Used by serializer.
InitListExpr() : Expr(InitListExprClass, QualType()),
- InitExprs(NULL), NumInits(0) {}
+ InitExprs(NULL) {}
};
/// ObjCStringLiteral, used for Objective-C string literals
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=50511&r1=50510&r2=50511&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Wed Apr 30 21:04:18 2008
@@ -254,14 +254,11 @@
InitListExpr::InitListExpr(SourceLocation lbraceloc,
Expr **initexprs, unsigned numinits,
SourceLocation rbraceloc)
- : Expr(InitListExprClass, QualType())
- , NumInits(numinits)
- , LBraceLoc(lbraceloc)
- , RBraceLoc(rbraceloc)
+ : Expr(InitListExprClass, QualType()),
+ LBraceLoc(lbraceloc), RBraceLoc(rbraceloc)
{
- InitExprs = new Expr*[numinits];
for (unsigned i = 0; i != numinits; i++)
- InitExprs[i] = initexprs[i];
+ InitExprs.push_back(initexprs[i]);
}
//===----------------------------------------------------------------------===//
@@ -1368,7 +1365,7 @@
return reinterpret_cast<Stmt**>(&InitExprs[0]);
}
Stmt::child_iterator InitListExpr::child_end() {
- return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
+ return reinterpret_cast<Stmt**>(&InitExprs[getNumInits()]);
}
// ObjCStringLiteral
Modified: cfe/trunk/lib/AST/StmtSerialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtSerialization.cpp?rev=50511&r1=50510&r2=50511&view=diff
==============================================================================
--- cfe/trunk/lib/AST/StmtSerialization.cpp (original)
+++ cfe/trunk/lib/AST/StmtSerialization.cpp Wed Apr 30 21:04:18 2008
@@ -647,18 +647,20 @@
void InitListExpr::EmitImpl(Serializer& S) const {
S.Emit(LBraceLoc);
S.Emit(RBraceLoc);
- S.EmitInt(NumInits);
- S.BatchEmitOwnedPtrs(NumInits,InitExprs);
+ S.EmitInt(InitExprs.size());
+ if (!InitExprs.empty()) S.BatchEmitOwnedPtrs(InitExprs.size(), &InitExprs[0]);
}
InitListExpr* InitListExpr::CreateImpl(Deserializer& D, ASTContext& C) {
InitListExpr* expr = new InitListExpr();
expr->LBraceLoc = SourceLocation::ReadVal(D);
expr->RBraceLoc = SourceLocation::ReadVal(D);
- expr->NumInits = D.ReadInt();
- assert(expr->NumInits);
- expr->InitExprs = new Expr*[expr->NumInits];
- D.BatchReadOwnedPtrs(expr->NumInits, expr->InitExprs, C);
+ unsigned size = D.ReadInt();
+ assert(size);
+ expr->InitExprs.reserve(size);
+ for (unsigned i = 0 ; i < size; ++i) expr->InitExprs.push_back(0);
+
+ D.BatchReadOwnedPtrs(size, &expr->InitExprs[0], C);
return expr;
}
More information about the cfe-commits
mailing list