|
| 1 | +//===--------------- InterpBuiltinConstantP.cpp -----------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +#include "InterpBuiltinConstantP.h" |
| 9 | +#include "Compiler.h" |
| 10 | +#include "EvalEmitter.h" |
| 11 | +#include "clang/AST/Expr.h" |
| 12 | +#include "clang/AST/ExprCXX.h" |
| 13 | + |
| 14 | +using namespace clang; |
| 15 | +using namespace interp; |
| 16 | + |
| 17 | +bool BCPVisitor::VisitStmt(Stmt *S) { |
| 18 | + switch (S->getStmtClass()) { |
| 19 | + case Stmt::DeclRefExprClass: |
| 20 | + return VisitDeclRefExpr(cast<DeclRefExpr>(S)); |
| 21 | + case Stmt::ImplicitCastExprClass: |
| 22 | + case Stmt::CStyleCastExprClass: |
| 23 | + return VisitCastExpr(cast<CastExpr>(S)); |
| 24 | + case Stmt::CallExprClass: |
| 25 | + return VisitCallExpr(cast<CallExpr>(S)); |
| 26 | + case Stmt::UnaryOperatorClass: |
| 27 | + return VisitUnaryOperator(cast<UnaryOperator>(S)); |
| 28 | + case Stmt::BinaryOperatorClass: |
| 29 | + return VisitBinaryOperator(cast<BinaryOperator>(S)); |
| 30 | + case Stmt::ParenExprClass: |
| 31 | + return VisitStmt(cast<ParenExpr>(S)->getSubExpr()); |
| 32 | + case Stmt::ConditionalOperatorClass: |
| 33 | + return VisitConditionalOperator(cast<ConditionalOperator>(S)); |
| 34 | + case Stmt::MemberExprClass: |
| 35 | + return VisitMemberExpr(cast<MemberExpr>(S)); |
| 36 | + |
| 37 | + case Stmt::IntegerLiteralClass: |
| 38 | + case Stmt::FloatingLiteralClass: |
| 39 | + case Stmt::CXXNullPtrLiteralExprClass: |
| 40 | + return true; |
| 41 | + default: |
| 42 | + return Fail(); |
| 43 | + } |
| 44 | + |
| 45 | + llvm_unreachable("All handled above"); |
| 46 | +} |
| 47 | + |
| 48 | +bool BCPVisitor::VisitDeclRefExpr(DeclRefExpr *E) { |
| 49 | + const ValueDecl *D = E->getDecl(); |
| 50 | + |
| 51 | + // Local variable? |
| 52 | + if (auto LocalOffset = findLocal(D)) { |
| 53 | + Result = pointerChainIsLive(Frame->getLocalPointer(*LocalOffset)); |
| 54 | + } else if (auto G = S.P.getGlobal(D)) { |
| 55 | + // Fine. |
| 56 | + } else if (auto P = findParam(D)) { |
| 57 | + Result = pointerChainIsLive(Frame->getParamPointer(*P)); |
| 58 | + } else { |
| 59 | + Result = false; |
| 60 | + } |
| 61 | + |
| 62 | + return Result; |
| 63 | +} |
| 64 | + |
| 65 | +bool BCPVisitor::VisitUnaryOperator(UnaryOperator *E) { |
| 66 | + switch (E->getOpcode()) { |
| 67 | + case UO_AddrOf: |
| 68 | + Result = isa<CXXTypeidExpr>(E->getSubExpr()); |
| 69 | + break; |
| 70 | + case UO_PostInc: |
| 71 | + case UO_PreInc: |
| 72 | + case UO_PostDec: |
| 73 | + case UO_PreDec: |
| 74 | + return Fail(); |
| 75 | + default:; |
| 76 | + } |
| 77 | + return Result; |
| 78 | +} |
| 79 | + |
| 80 | +bool BCPVisitor::VisitBinaryOperator(BinaryOperator *E) { |
| 81 | + if (E->isCommaOp()) |
| 82 | + return VisitStmt(E->getRHS()); |
| 83 | + |
| 84 | + return VisitStmt(E->getLHS()) && VisitStmt(E->getRHS()); |
| 85 | +} |
| 86 | + |
| 87 | +bool BCPVisitor::VisitCastExpr(CastExpr *E) { |
| 88 | + if (E->getCastKind() == CK_ToVoid) |
| 89 | + return Fail(); |
| 90 | + return VisitStmt(E->getSubExpr()); |
| 91 | +} |
| 92 | + |
| 93 | +bool BCPVisitor::VisitCallExpr(CallExpr *E) { |
| 94 | + // FIXME: We're not passing any arguments to the function call. |
| 95 | + Compiler<EvalEmitter> C(S.getContext(), S.P, S, S.Stk); |
| 96 | + |
| 97 | + auto OldDiag = S.getEvalStatus().Diag; |
| 98 | + S.getEvalStatus().Diag = nullptr; |
| 99 | + auto Res = C.interpretExpr(E, /*ConvertResultToRValue=*/E->isGLValue()); |
| 100 | + |
| 101 | + S.getEvalStatus().Diag = OldDiag; |
| 102 | + Result = !Res.isInvalid(); |
| 103 | + return Result; |
| 104 | +} |
| 105 | + |
| 106 | +bool BCPVisitor::VisitConditionalOperator(ConditionalOperator *E) { |
| 107 | + return VisitStmt(E->getCond()) && VisitStmt(E->getTrueExpr()) && |
| 108 | + VisitStmt(E->getFalseExpr()); |
| 109 | +} |
| 110 | + |
| 111 | +bool BCPVisitor::VisitMemberExpr(MemberExpr *E) { |
| 112 | + if (!isa<DeclRefExpr>(E->getBase())) |
| 113 | + return Fail(); |
| 114 | + |
| 115 | + const auto *BaseDecl = cast<DeclRefExpr>(E->getBase())->getDecl(); |
| 116 | + const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 117 | + if (!FD) |
| 118 | + return Fail(); |
| 119 | + |
| 120 | + if (!VisitStmt(E->getBase())) |
| 121 | + return Fail(); |
| 122 | + |
| 123 | + Pointer BasePtr = getPointer(BaseDecl); |
| 124 | + const Record *R = BasePtr.getRecord(); |
| 125 | + assert(R); |
| 126 | + Pointer FieldPtr = BasePtr.atField(R->getField(FD)->Offset); |
| 127 | + if (!pointerChainIsLive(FieldPtr)) |
| 128 | + return Fail(); |
| 129 | + return true; |
| 130 | +} |
| 131 | + |
| 132 | +std::optional<unsigned> BCPVisitor::findLocal(const ValueDecl *D) const { |
| 133 | + const auto *Func = Frame->getFunction(); |
| 134 | + if (!Func) |
| 135 | + return std::nullopt; |
| 136 | + for (auto &Scope : Func->scopes()) { |
| 137 | + for (auto &Local : Scope.locals()) { |
| 138 | + if (Local.Desc->asValueDecl() == D) { |
| 139 | + return Local.Offset; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + return std::nullopt; |
| 144 | +} |
| 145 | + |
| 146 | +std::optional<unsigned> BCPVisitor::findParam(const ValueDecl *D) const { |
| 147 | + const auto *Func = Frame->getFunction(); |
| 148 | + if (!Func || !Frame->Caller) |
| 149 | + return std::nullopt; |
| 150 | + |
| 151 | + return Func->findParam(D); |
| 152 | +} |
| 153 | + |
| 154 | +bool BCPVisitor::pointerChainIsLive(const Pointer &P) const { |
| 155 | + Pointer Ptr = P; |
| 156 | + for (;;) { |
| 157 | + if (!Ptr.isLive() || !Ptr.isInitialized() || Ptr.isExtern() || |
| 158 | + Ptr.isDummy()) |
| 159 | + return false; |
| 160 | + |
| 161 | + if (Ptr.isZero()) |
| 162 | + return true; |
| 163 | + |
| 164 | + const Descriptor *Desc = Ptr.getFieldDesc(); |
| 165 | + if (!Desc->isPrimitive() || Desc->getPrimType() != PT_Ptr) |
| 166 | + return true; |
| 167 | + |
| 168 | + Ptr = Ptr.deref<Pointer>(); |
| 169 | + } |
| 170 | + |
| 171 | + return true; |
| 172 | +} |
| 173 | + |
| 174 | +Pointer BCPVisitor::getPointer(const ValueDecl *D) const { |
| 175 | + if (auto LocalOffset = findLocal(D)) |
| 176 | + return Frame->getLocalPointer(*LocalOffset); |
| 177 | + if (auto G = S.P.getGlobal(D)) |
| 178 | + return S.P.getPtrGlobal(*G); |
| 179 | + if (auto P = findParam(D)) |
| 180 | + return Frame->getParamPointer(*P); |
| 181 | + |
| 182 | + llvm_unreachable("One of the ifs before should've worked."); |
| 183 | +} |
0 commit comments