-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang][Interp] Add an EvaluationResult class #71315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,18 +30,8 @@ Context::~Context() {} | |
bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) { | ||
assert(Stk.empty()); | ||
Function *Func = P->getFunction(FD); | ||
if (!Func || !Func->hasBody()) { | ||
if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) { | ||
Func = *R; | ||
} else { | ||
handleAllErrors(R.takeError(), [&Parent](ByteCodeGenError &Err) { | ||
Parent.FFDiag(Err.getRange().getBegin(), | ||
diag::err_experimental_clang_interp_failed) | ||
<< Err.getRange(); | ||
}); | ||
return false; | ||
} | ||
} | ||
if (!Func || !Func->hasBody()) | ||
Func = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD); | ||
|
||
APValue DummyResult; | ||
if (!Run(Parent, Func, DummyResult)) { | ||
|
@@ -54,36 +44,90 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) { | |
bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) { | ||
assert(Stk.empty()); | ||
ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result); | ||
if (Check(Parent, C.interpretExpr(E))) { | ||
assert(Stk.empty()); | ||
#ifndef NDEBUG | ||
// Make sure we don't rely on some value being still alive in | ||
// InterpStack memory. | ||
|
||
auto Res = C.interpretExpr(E); | ||
|
||
if (Res.isInvalid()) { | ||
Stk.clear(); | ||
return false; | ||
} | ||
|
||
assert(Stk.empty()); | ||
#ifndef NDEBUG | ||
// Make sure we don't rely on some value being still alive in | ||
// InterpStack memory. | ||
Stk.clear(); | ||
#endif | ||
return true; | ||
|
||
// Implicit lvalue-to-rvalue conversion. | ||
if (E->isGLValue()) { | ||
std::optional<APValue> RValueResult = Res.toRValue(); | ||
if (!RValueResult) { | ||
return false; | ||
} | ||
Result = *RValueResult; | ||
} else { | ||
Result = Res.toAPValue(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool Context::evaluate(State &Parent, const Expr *E, APValue &Result) { | ||
assert(Stk.empty()); | ||
ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result); | ||
|
||
auto Res = C.interpretExpr(E); | ||
if (Res.isInvalid()) { | ||
Stk.clear(); | ||
return false; | ||
} | ||
|
||
assert(Stk.empty()); | ||
#ifndef NDEBUG | ||
// Make sure we don't rely on some value being still alive in | ||
// InterpStack memory. | ||
Stk.clear(); | ||
return false; | ||
#endif | ||
Result = Res.toAPValue(); | ||
return true; | ||
} | ||
|
||
bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD, | ||
APValue &Result) { | ||
assert(Stk.empty()); | ||
ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result); | ||
if (Check(Parent, C.interpretDecl(VD))) { | ||
assert(Stk.empty()); | ||
#ifndef NDEBUG | ||
// Make sure we don't rely on some value being still alive in | ||
// InterpStack memory. | ||
|
||
auto Res = C.interpretDecl(VD); | ||
if (Res.isInvalid()) { | ||
Stk.clear(); | ||
#endif | ||
return true; | ||
return false; | ||
} | ||
|
||
assert(Stk.empty()); | ||
#ifndef NDEBUG | ||
// Make sure we don't rely on some value being still alive in | ||
// InterpStack memory. | ||
Stk.clear(); | ||
return false; | ||
#endif | ||
|
||
// Ensure global variables are fully initialized. | ||
if (shouldBeGloballyIndexed(VD) && !Res.isInvalid() && | ||
(VD->getType()->isRecordType() || VD->getType()->isArrayType())) { | ||
assert(Res.isLValue()); | ||
|
||
if (!Res.checkFullyInitialized(C.getState())) | ||
return false; | ||
|
||
// lvalue-to-rvalue conversion. | ||
std::optional<APValue> RValueResult = Res.toRValue(); | ||
if (!RValueResult) | ||
return false; | ||
Result = *RValueResult; | ||
Comment on lines
+122
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably turn this into a function rather than reimplement it in different places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two versions are slightly different: in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, that seems fine to do in a follow-up. |
||
|
||
} else | ||
Result = Res.toAPValue(); | ||
return true; | ||
} | ||
|
||
const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); } | ||
|
@@ -234,12 +278,8 @@ const Function *Context::getOrCreateFunction(const FunctionDecl *FD) { | |
return Func; | ||
|
||
if (!Func || WasNotDefined) { | ||
if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) | ||
Func = *R; | ||
else { | ||
llvm::consumeError(R.takeError()); | ||
return nullptr; | ||
} | ||
if (auto F = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) | ||
Func = F; | ||
} | ||
|
||
return Func; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be asserting that we have an rvalue as opposed to an lvalue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have either here and just return what we have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, okay!