Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:34

0001 //===--- EvaluatedExprVisitor.h - Evaluated expression visitor --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines the EvaluatedExprVisitor class template, which visits
0010 //  the potentially-evaluated subexpressions of a potentially-evaluated
0011 //  expression.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 #ifndef LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
0015 #define LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
0016 
0017 #include "clang/AST/DeclCXX.h"
0018 #include "clang/AST/Expr.h"
0019 #include "clang/AST/ExprCXX.h"
0020 #include "clang/AST/StmtVisitor.h"
0021 #include "llvm/ADT/STLExtras.h"
0022 
0023 namespace clang {
0024 
0025 class ASTContext;
0026 
0027 /// Given a potentially-evaluated expression, this visitor visits all
0028 /// of its potentially-evaluated subexpressions, recursively.
0029 template<template <typename> class Ptr, typename ImplClass>
0030 class EvaluatedExprVisitorBase : public StmtVisitorBase<Ptr, ImplClass, void> {
0031 protected:
0032   const ASTContext &Context;
0033 
0034 public:
0035   // Return whether this visitor should recurse into discarded statements for a
0036   // 'constexpr-if'.
0037   bool shouldVisitDiscardedStmt() const { return true; }
0038 #define PTR(CLASS) typename Ptr<CLASS>::type
0039 
0040   explicit EvaluatedExprVisitorBase(const ASTContext &Context) : Context(Context) { }
0041 
0042   // Expressions that have no potentially-evaluated subexpressions (but may have
0043   // other sub-expressions).
0044   void VisitDeclRefExpr(PTR(DeclRefExpr) E) { }
0045   void VisitOffsetOfExpr(PTR(OffsetOfExpr) E) { }
0046   void VisitUnaryExprOrTypeTraitExpr(PTR(UnaryExprOrTypeTraitExpr) E) { }
0047   void VisitExpressionTraitExpr(PTR(ExpressionTraitExpr) E) { }
0048   void VisitBlockExpr(PTR(BlockExpr) E) { }
0049   void VisitCXXUuidofExpr(PTR(CXXUuidofExpr) E) { }
0050   void VisitCXXNoexceptExpr(PTR(CXXNoexceptExpr) E) { }
0051 
0052   void VisitMemberExpr(PTR(MemberExpr) E) {
0053     // Only the base matters.
0054     return this->Visit(E->getBase());
0055   }
0056 
0057   void VisitChooseExpr(PTR(ChooseExpr) E) {
0058     // Don't visit either child expression if the condition is dependent.
0059     if (E->getCond()->isValueDependent())
0060       return;
0061     // Only the selected subexpression matters; the other one is not evaluated.
0062     return this->Visit(E->getChosenSubExpr());
0063   }
0064 
0065   void VisitGenericSelectionExpr(PTR(GenericSelectionExpr) E) {
0066     // The controlling expression of a generic selection is not evaluated.
0067 
0068     // Don't visit either child expression if the condition is type-dependent.
0069     if (E->isResultDependent())
0070       return;
0071     // Only the selected subexpression matters; the other subexpressions and the
0072     // controlling expression are not evaluated.
0073     return this->Visit(E->getResultExpr());
0074   }
0075 
0076   void VisitDesignatedInitExpr(PTR(DesignatedInitExpr) E) {
0077     // Only the actual initializer matters; the designators are all constant
0078     // expressions.
0079     return this->Visit(E->getInit());
0080   }
0081 
0082   void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E) {
0083     if (E->isPotentiallyEvaluated())
0084       return this->Visit(E->getExprOperand());
0085   }
0086 
0087   void VisitCallExpr(PTR(CallExpr) CE) {
0088     if (!CE->isUnevaluatedBuiltinCall(Context))
0089       return getDerived().VisitExpr(CE);
0090   }
0091 
0092   void VisitLambdaExpr(PTR(LambdaExpr) LE) {
0093     // Only visit the capture initializers, and not the body.
0094     for (LambdaExpr::const_capture_init_iterator I = LE->capture_init_begin(),
0095                                                  E = LE->capture_init_end();
0096          I != E; ++I)
0097       if (*I)
0098         this->Visit(*I);
0099   }
0100 
0101   /// The basis case walks all of the children of the statement or
0102   /// expression, assuming they are all potentially evaluated.
0103   void VisitStmt(PTR(Stmt) S) {
0104     for (auto *SubStmt : S->children())
0105       if (SubStmt)
0106         this->Visit(SubStmt);
0107   }
0108 
0109   void VisitIfStmt(PTR(IfStmt) If) {
0110     if (!getDerived().shouldVisitDiscardedStmt()) {
0111       if (auto SubStmt = If->getNondiscardedCase(Context)) {
0112         if (*SubStmt)
0113           this->Visit(*SubStmt);
0114         return;
0115       }
0116     }
0117 
0118     getDerived().VisitStmt(If);
0119   }
0120 
0121   ImplClass &getDerived() { return *static_cast<ImplClass *>(this); }
0122 
0123 #undef PTR
0124 };
0125 
0126 /// EvaluatedExprVisitor - This class visits 'Expr *'s
0127 template <typename ImplClass>
0128 class EvaluatedExprVisitor
0129     : public EvaluatedExprVisitorBase<std::add_pointer, ImplClass> {
0130 public:
0131   explicit EvaluatedExprVisitor(const ASTContext &Context)
0132       : EvaluatedExprVisitorBase<std::add_pointer, ImplClass>(Context) {}
0133 };
0134 
0135 /// ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
0136 template <typename ImplClass>
0137 class ConstEvaluatedExprVisitor
0138     : public EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass> {
0139 public:
0140   explicit ConstEvaluatedExprVisitor(const ASTContext &Context)
0141       : EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass>(Context) {}
0142 };
0143 }
0144 
0145 #endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H