[cfe-commits] r56033 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/AST/ExprCXX.h include/clang/AST/StmtNodes.def lib/AST/ExprCXX.cpp lib/AST/StmtPrinter.cpp

Argiris Kirtzidis akyrtzi at gmail.com
Tue Sep 9 16:47:53 PDT 2008


Author: akirtzidis
Date: Tue Sep  9 18:47:53 2008
New Revision: 56033

URL: http://llvm.org/viewvc/llvm-project?rev=56033&view=rev
Log:
Add new 'CXXConditionDeclExpr' expression node used for a 'condition' declaration, e.g: "if (int x=0) {...}".
It is a subclass of DeclRefExpr and the main difference is that CXXConditionDeclExpr owns the declaration that it references.

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/AST/ExprCXX.h
    cfe/trunk/include/clang/AST/StmtNodes.def
    cfe/trunk/lib/AST/ExprCXX.cpp
    cfe/trunk/lib/AST/StmtPrinter.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=56033&r1=56032&r2=56033&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Tue Sep  9 18:47:53 2008
@@ -206,17 +206,24 @@
 class DeclRefExpr : public Expr {
   ValueDecl *D; 
   SourceLocation Loc;
+
+protected:
+  DeclRefExpr(StmtClass SC, ValueDecl *d, QualType t, SourceLocation l) :
+    Expr(SC, t), D(d), Loc(l) {}
+
 public:
   DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) : 
     Expr(DeclRefExprClass, t), D(d), Loc(l) {}
   
   ValueDecl *getDecl() { return D; }
   const ValueDecl *getDecl() const { return D; }
+  SourceLocation getLocation() const { return Loc; }
   virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
   
   
   static bool classof(const Stmt *T) { 
-    return T->getStmtClass() == DeclRefExprClass; 
+    return T->getStmtClass() == DeclRefExprClass ||
+           T->getStmtClass() == CXXConditionDeclExprClass; 
   }
   static bool classof(const DeclRefExpr *) { return true; }
   

Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=56033&r1=56032&r2=56033&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Tue Sep  9 18:47:53 2008
@@ -234,6 +234,43 @@
       CreateImpl(llvm::Deserializer& D, ASTContext& C);
 };
 
+/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for
+/// statement, e.g: "if (int x = f()) {...}".
+/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the
+/// decl that it references.
+///
+class CXXConditionDeclExpr : public DeclRefExpr {
+public:
+  CXXConditionDeclExpr(SourceLocation startLoc,
+                       SourceLocation eqLoc, VarDecl *var)
+    : DeclRefExpr(CXXConditionDeclExprClass, var, var->getType(), startLoc) {}
+
+  virtual void Destroy(ASTContext& Ctx);
+
+  SourceLocation getStartLoc() const { return getLocation(); }
+  
+  VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); }
+  const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); }
+
+  virtual SourceRange getSourceRange() const {
+    return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd());
+  }
+    
+  static bool classof(const Stmt *T) { 
+    return T->getStmtClass() == CXXConditionDeclExprClass;
+  }
+  static bool classof(const CXXConditionDeclExpr *) { return true; }
+      
+  // Iterators
+  virtual child_iterator child_begin();
+  virtual child_iterator child_end();
+
+  // FIXME: Implement these.
+  //virtual void EmitImpl(llvm::Serializer& S) const;
+  //static CXXConditionDeclExpr *
+  //    CreateImpl(llvm::Deserializer& D, ASTContext& C);
+};
+
 }  // end namespace clang
 
 #endif

Modified: cfe/trunk/include/clang/AST/StmtNodes.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtNodes.def?rev=56033&r1=56032&r2=56033&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/StmtNodes.def (original)
+++ cfe/trunk/include/clang/AST/StmtNodes.def Tue Sep  9 18:47:53 2008
@@ -95,6 +95,7 @@
 STMT(63, CXXDefaultArgExpr    , Expr)
 STMT(64, CXXFunctionalCastExpr, CastExpr)
 STMT(65, CXXZeroInitValueExpr , Expr)
+STMT(66, CXXConditionDeclExpr , DeclRefExpr)
 
 // Obj-C Expressions.
 STMT(70, ObjCStringLiteral    , Expr)

Modified: cfe/trunk/lib/AST/ExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprCXX.cpp?rev=56033&r1=56032&r2=56033&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprCXX.cpp (original)
+++ cfe/trunk/lib/AST/ExprCXX.cpp Tue Sep  9 18:47:53 2008
@@ -14,6 +14,12 @@
 #include "clang/AST/ExprCXX.h"
 using namespace clang;
 
+void CXXConditionDeclExpr::Destroy(ASTContext& C) {
+  getVarDecl()->Destroy(C);
+  delete this;
+}
+
+
 //===----------------------------------------------------------------------===//
 //  Child Iterators for iterating over subexpressions/substatements
 //===----------------------------------------------------------------------===//
@@ -53,3 +59,11 @@
 Stmt::child_iterator CXXZeroInitValueExpr::child_end() {
   return child_iterator();
 }
+
+// CXXConditionDeclExpr
+Stmt::child_iterator CXXConditionDeclExpr::child_begin() {
+  return getVarDecl();
+}
+Stmt::child_iterator CXXConditionDeclExpr::child_end() {
+  return child_iterator();
+}

Modified: cfe/trunk/lib/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtPrinter.cpp?rev=56033&r1=56032&r2=56033&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/lib/AST/StmtPrinter.cpp Tue Sep  9 18:47:53 2008
@@ -829,6 +829,11 @@
   OS << Node->getType().getAsString() << "()";
 }
 
+void
+StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
+  PrintRawDecl(E->getVarDecl());
+}
+
 // Obj-C 
 
 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {





More information about the cfe-commits mailing list