1be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor//===--- EvaluatedExprVisitor.h - Evaluated expression visitor --*- C++ -*-===// 2be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// 3be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// The LLVM Compiler Infrastructure 4be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// 5be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// This file is distributed under the University of Illinois Open Source 6be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// License. See LICENSE.TXT for details. 7be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// 8be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor//===----------------------------------------------------------------------===// 9be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// 10be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// This file defines the EvaluatedExprVisitor class template, which visits 11be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// the potentially-evaluated subexpressions of a potentially-evaluated 12be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// expression. 13be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor// 14be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor//===----------------------------------------------------------------------===// 15be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor#ifndef LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H 16be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor#define LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H 17be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 18be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor#include "clang/AST/DeclCXX.h" 19be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor#include "clang/AST/Expr.h" 20be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor#include "clang/AST/ExprCXX.h" 2130a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/AST/StmtVisitor.h" 22be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 23be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregornamespace clang { 24be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 25be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregorclass ASTContext; 26be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 270982205bade2fb4fc984c27b2ab401e683963b10James Dennett/// \brief Given a potentially-evaluated expression, this visitor visits all 28be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor/// of its potentially-evaluated subexpressions, recursively. 29be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregortemplate<typename ImplClass> 30be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregorclass EvaluatedExprVisitor : public StmtVisitor<ImplClass> { 31be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor ASTContext &Context; 32be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 33be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregorpublic: 34be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor explicit EvaluatedExprVisitor(ASTContext &Context) : Context(Context) { } 35be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 36be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor // Expressions that have no potentially-evaluated subexpressions (but may have 37be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor // other sub-expressions). 38be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitDeclRefExpr(DeclRefExpr *E) { } 39be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitOffsetOfExpr(OffsetOfExpr *E) { } 40f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { } 41552622067dc45013d240f73952fece703f5e63bdJohn Wiegley void VisitExpressionTraitExpr(ExpressionTraitExpr *E) { } 42be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitBlockExpr(BlockExpr *E) { } 43be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitCXXUuidofExpr(CXXUuidofExpr *E) { } 44be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { } 45be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 46be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitMemberExpr(MemberExpr *E) { 47be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor // Only the base matters. 48be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor return this->Visit(E->getBase()); 49be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor } 50be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 51be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitChooseExpr(ChooseExpr *E) { 526c3af3d0e3e65bcbca57bfd458d684941f6d0531Richard Smith // Don't visit either child expression if the condition is dependent. 536c3af3d0e3e65bcbca57bfd458d684941f6d0531Richard Smith if (E->getCond()->isValueDependent()) 546c3af3d0e3e65bcbca57bfd458d684941f6d0531Richard Smith return; 55be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor // Only the selected subexpression matters; the other one is not evaluated. 56a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman return this->Visit(E->getChosenSubExpr()); 57be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor } 580f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith 590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines void VisitGenericSelectionExpr(GenericSelectionExpr *E) { 600e2c34f92f00628d48968dfea096d36381f494cbStephen Hines // The controlling expression of a generic selection is not evaluated. 610e2c34f92f00628d48968dfea096d36381f494cbStephen Hines 620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines // Don't visit either child expression if the condition is type-dependent. 630e2c34f92f00628d48968dfea096d36381f494cbStephen Hines if (E->isResultDependent()) 640e2c34f92f00628d48968dfea096d36381f494cbStephen Hines return; 650e2c34f92f00628d48968dfea096d36381f494cbStephen Hines // Only the selected subexpression matters; the other subexpressions and the 660e2c34f92f00628d48968dfea096d36381f494cbStephen Hines // controlling expression are not evaluated. 670e2c34f92f00628d48968dfea096d36381f494cbStephen Hines return this->Visit(E->getResultExpr()); 680e2c34f92f00628d48968dfea096d36381f494cbStephen Hines } 690e2c34f92f00628d48968dfea096d36381f494cbStephen Hines 70be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitDesignatedInitExpr(DesignatedInitExpr *E) { 71be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor // Only the actual initializer matters; the designators are all constant 72be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor // expressions. 73be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor return this->Visit(E->getInit()); 74be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor } 75ba57183965f117279342903edec19766e478c9a8Richard Smith 76be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor void VisitCXXTypeidExpr(CXXTypeidExpr *E) { 77f42755ea9f0d8792e55dba28f1e013bc8223a676Richard Smith if (E->isPotentiallyEvaluated()) 78f42755ea9f0d8792e55dba28f1e013bc8223a676Richard Smith return this->Visit(E->getExprOperand()); 79be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor } 80ba57183965f117279342903edec19766e478c9a8Richard Smith 81ba57183965f117279342903edec19766e478c9a8Richard Smith void VisitCallExpr(CallExpr *CE) { 82ba57183965f117279342903edec19766e478c9a8Richard Smith if (!CE->isUnevaluatedBuiltinCall(Context)) 83ba57183965f117279342903edec19766e478c9a8Richard Smith return static_cast<ImplClass*>(this)->VisitExpr(CE); 84ba57183965f117279342903edec19766e478c9a8Richard Smith } 85ba57183965f117279342903edec19766e478c9a8Richard Smith 860f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith void VisitLambdaExpr(LambdaExpr *LE) { 870f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith // Only visit the capture initializers, and not the body. 880f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith for (LambdaExpr::capture_init_iterator I = LE->capture_init_begin(), 890f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith E = LE->capture_init_end(); 900f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith I != E; ++I) 910f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith if (*I) 920f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith this->Visit(*I); 930f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith } 940f2fc5ff49cb9abd6c6972ffd6db066295672867Richard Smith 954fcf5b2f816347ba7a3f16557d5e2b293634d4d6Douglas Gregor /// \brief The basis case walks all of the children of the statement or 964fcf5b2f816347ba7a3f16557d5e2b293634d4d6Douglas Gregor /// expression, assuming they are all potentially evaluated. 974fcf5b2f816347ba7a3f16557d5e2b293634d4d6Douglas Gregor void VisitStmt(Stmt *S) { 987502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall for (Stmt::child_range C = S->children(); C; ++C) 990656e5b9aa52f2a90fb38517e504b4eebbe53381Chandler Carruth if (*C) 1000656e5b9aa52f2a90fb38517e504b4eebbe53381Chandler Carruth this->Visit(*C); 1014fcf5b2f816347ba7a3f16557d5e2b293634d4d6Douglas Gregor } 102be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor}; 103be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 104be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor} 105be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor 106be0f7bd61c7b2879d02ae75aad7a91d92f819d94Douglas Gregor#endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H 107