From: Anders Carlsson Date: Fri, 31 Aug 2007 04:56:16 +0000 (+0000) Subject: Add InitListExpr class. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=66b5a8a39088598c01a9fa6f032dc908612dc8ec;p=clang Add InitListExpr class. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41636 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/Expr.cpp b/AST/Expr.cpp index 3aed116c76..d664c46081 100644 --- a/AST/Expr.cpp +++ b/AST/Expr.cpp @@ -194,6 +194,18 @@ const char *BinaryOperator::getOpcodeStr(Opcode Op) { } } +InitListExpr::InitListExpr(SourceLocation lbraceloc, + Expr **initexprs, unsigned numinits, + SourceLocation rbraceloc) + : Expr(InitListExprClass, QualType()) + , NumInits(numinits) + , LBraceLoc(lbraceloc) + , RBraceLoc(rbraceloc) +{ + InitExprs = new Expr*[numinits]; + for (unsigned i = 0; i != numinits; i++) + InitExprs[i] = initexprs[i]; +} //===----------------------------------------------------------------------===// // Generic Expression Routines @@ -871,6 +883,14 @@ Stmt::child_iterator ChooseExpr::child_end() { return reinterpret_cast(&SubExprs)+END_EXPR; } +// InitListExpr +Stmt::child_iterator InitListExpr::child_begin() { + return reinterpret_cast(&InitExprs[0]); +} +Stmt::child_iterator InitListExpr::child_end() { + return reinterpret_cast(&InitExprs[NumInits]); +} + // ObjCStringLiteral Stmt::child_iterator ObjCStringLiteral::child_begin() { return NULL; } Stmt::child_iterator ObjCStringLiteral::child_end() { return NULL; } diff --git a/AST/StmtPrinter.cpp b/AST/StmtPrinter.cpp index a771b4ce1a..6903080422 100644 --- a/AST/StmtPrinter.cpp +++ b/AST/StmtPrinter.cpp @@ -550,6 +550,15 @@ void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { OS << ")"; } +void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { + OS << "{ "; + for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { + if (i) OS << ", "; + PrintExpr(Node->getInit(i)); + } + OS << " }"; +} + // C++ void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) { diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp index a32e607aa4..3b65ab2bc1 100644 --- a/Sema/SemaExpr.cpp +++ b/Sema/SemaExpr.cpp @@ -640,12 +640,16 @@ ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, } Action::ExprResult Sema:: -ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit, - SourceLocation RParenLoc) { +ParseInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit, + SourceLocation RBraceLoc) { +// Expr **InitList = reinterpret_cast(initlist); + // FIXME: add semantic analysis (C99 6.7.8). This involves // knowledge of the object being intialized. As a result, the code for // doing the semantic analysis will likely be located elsewhere (i.e. in // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral). + + //return new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc); return false; // FIXME instantiate an InitListExpr. } diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 375252c6c9..44b97a942f 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -967,6 +967,43 @@ public: virtual child_iterator child_end(); }; +/// InitListExpr, used for struct and array initializers. +class InitListExpr : public Expr { + Expr **InitExprs; + unsigned NumInits; + SourceLocation LBraceLoc, RBraceLoc; +public: + InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, + SourceLocation rbraceloc); + ~InitListExpr() { + delete [] InitExprs; + } + + unsigned getNumInits() const { return NumInits; } + + const Expr* getInit(unsigned Init) const { + assert(Init < NumInits && "Initializer access out of range!"); + return InitExprs[Init]; + } + + Expr* getInit(unsigned Init) { + assert(Init < NumInits && "Initializer access out of range!"); + return InitExprs[Init]; + } + + virtual SourceRange getSourceRange() const { + return SourceRange(LBraceLoc, RBraceLoc); + } + static bool classof(const Stmt *T) { + return T->getStmtClass() == InitListExprClass; + } + static bool classof(const InitListExpr *) { return true; } + + // Iterators + virtual child_iterator child_begin(); + virtual child_iterator child_end(); +}; + /// ObjCStringLiteral, used for Objective-C string literals /// i.e. @"foo". class ObjCStringLiteral : public Expr { diff --git a/include/clang/AST/StmtNodes.def b/include/clang/AST/StmtNodes.def index bc022775a0..54be80e040 100644 --- a/include/clang/AST/StmtNodes.def +++ b/include/clang/AST/StmtNodes.def @@ -65,6 +65,7 @@ STMT(48, ConditionalOperator , Expr) STMT(49, ImplicitCastExpr , Expr) STMT(50, CompoundLiteralExpr , Expr) STMT(51, OCUVectorElementExpr , Expr) +STMT(52, InitListExpr , Expr) // GNU Extensions. STMT(55, AddrLabelExpr , Expr)