EvaluatedExprVisitor.h revision ba57183965f117279342903edec19766e478c9a8
1//===--- EvaluatedExprVisitor.h - Evaluated expression visitor --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the EvaluatedExprVisitor class template, which visits
11//  the potentially-evaluated subexpressions of a potentially-evaluated
12//  expression.
13//
14//===----------------------------------------------------------------------===//
15#ifndef LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
16#define LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
17
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/ExprCXX.h"
21#include "clang/AST/StmtVisitor.h"
22
23namespace clang {
24
25class ASTContext;
26
27/// \brief Given a potentially-evaluated expression, this visitor visits all
28/// of its potentially-evaluated subexpressions, recursively.
29template<typename ImplClass>
30class EvaluatedExprVisitor : public StmtVisitor<ImplClass> {
31  ASTContext &Context;
32
33public:
34  explicit EvaluatedExprVisitor(ASTContext &Context) : Context(Context) { }
35
36  // Expressions that have no potentially-evaluated subexpressions (but may have
37  // other sub-expressions).
38  void VisitDeclRefExpr(DeclRefExpr *E) { }
39  void VisitOffsetOfExpr(OffsetOfExpr *E) { }
40  void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { }
41  void VisitExpressionTraitExpr(ExpressionTraitExpr *E) { }
42  void VisitBlockExpr(BlockExpr *E) { }
43  void VisitCXXUuidofExpr(CXXUuidofExpr *E) { }
44  void VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { }
45
46  void VisitMemberExpr(MemberExpr *E) {
47    // Only the base matters.
48    return this->Visit(E->getBase());
49  }
50
51  void VisitChooseExpr(ChooseExpr *E) {
52    // Don't visit either child expression if the condition is dependent.
53    if (E->getCond()->isValueDependent())
54      return;
55    // Only the selected subexpression matters; the other one is not evaluated.
56    return this->Visit(E->getChosenSubExpr(Context));
57  }
58
59  void VisitDesignatedInitExpr(DesignatedInitExpr *E) {
60    // Only the actual initializer matters; the designators are all constant
61    // expressions.
62    return this->Visit(E->getInit());
63  }
64
65  void VisitCXXTypeidExpr(CXXTypeidExpr *E) {
66    if (E->isPotentiallyEvaluated())
67      return this->Visit(E->getExprOperand());
68  }
69
70  void VisitCallExpr(CallExpr *CE) {
71    if (!CE->isUnevaluatedBuiltinCall(Context))
72      return static_cast<ImplClass*>(this)->VisitExpr(CE);
73  }
74
75  /// \brief The basis case walks all of the children of the statement or
76  /// expression, assuming they are all potentially evaluated.
77  void VisitStmt(Stmt *S) {
78    for (Stmt::child_range C = S->children(); C; ++C)
79      if (*C)
80        this->Visit(*C);
81  }
82};
83
84}
85
86#endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
87