clang API Documentation

EvaluatedExprVisitor.h
Go to the documentation of this file.
00001 //===--- EvaluatedExprVisitor.h - Evaluated expression visitor --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the EvaluatedExprVisitor class template, which visits
00011 //  the potentially-evaluated subexpressions of a potentially-evaluated
00012 //  expression.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 #ifndef LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
00016 #define LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
00017 
00018 #include "clang/AST/StmtVisitor.h"
00019 #include "clang/AST/DeclCXX.h"
00020 #include "clang/AST/Expr.h"
00021 #include "clang/AST/ExprCXX.h"
00022 
00023 namespace clang {
00024   
00025 class ASTContext;
00026   
00027 /// \begin Given a potentially-evaluated expression, this visitor visits all
00028 /// of its potentially-evaluated subexpressions, recursively.
00029 template<typename ImplClass>
00030 class EvaluatedExprVisitor : public StmtVisitor<ImplClass> {
00031   ASTContext &Context;
00032   
00033 public:
00034   explicit EvaluatedExprVisitor(ASTContext &Context) : Context(Context) { }
00035   
00036   // Expressions that have no potentially-evaluated subexpressions (but may have
00037   // other sub-expressions).
00038   void VisitDeclRefExpr(DeclRefExpr *E) { }
00039   void VisitOffsetOfExpr(OffsetOfExpr *E) { }
00040   void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { }
00041   void VisitExpressionTraitExpr(ExpressionTraitExpr *E) { }
00042   void VisitBlockExpr(BlockExpr *E) { }
00043   void VisitCXXUuidofExpr(CXXUuidofExpr *E) { }  
00044   void VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { }
00045   
00046   void VisitMemberExpr(MemberExpr *E) {
00047     // Only the base matters.
00048     return this->Visit(E->getBase());
00049   }
00050   
00051   void VisitChooseExpr(ChooseExpr *E) {
00052     // Only the selected subexpression matters; the other one is not evaluated.
00053     return this->Visit(E->getChosenSubExpr(Context));
00054   }
00055                  
00056   void VisitDesignatedInitExpr(DesignatedInitExpr *E) {
00057     // Only the actual initializer matters; the designators are all constant
00058     // expressions.
00059     return this->Visit(E->getInit());
00060   }
00061   
00062   void VisitCXXTypeidExpr(CXXTypeidExpr *E) {
00063     // typeid(expression) is potentially evaluated when the argument is
00064     // a glvalue of polymorphic type. (C++ 5.2.8p2-3)
00065     if (!E->isTypeOperand() && E->Classify(Context).isGLValue())
00066       if (const RecordType *Record 
00067                  = E->getExprOperand()->getType()->template getAs<RecordType>())
00068         if (cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic())
00069           return this->Visit(E->getExprOperand());
00070   }
00071   
00072   /// \brief The basis case walks all of the children of the statement or
00073   /// expression, assuming they are all potentially evaluated.
00074   void VisitStmt(Stmt *S) {
00075     for (Stmt::child_range C = S->children(); C; ++C)
00076       if (*C)
00077         this->Visit(*C);
00078   }
00079 };
00080 
00081 }
00082 
00083 #endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H