Skip to content

[Compile Time Constant Extraction] Refactor to support recursion. #62461

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

Merged
merged 2 commits into from
Dec 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 43 additions & 49 deletions lib/ConstExtract/ConstExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,77 +120,71 @@ parseProtocolListFromFile(StringRef protocolListFilePath,
return true;
}

static std::string extractLiteralOutput(Expr *expr) {
std::string LiteralOutput;
llvm::raw_string_ostream OutputStream(LiteralOutput);
expr->printConstExprValue(&OutputStream, nullptr);

return LiteralOutput;
}

static std::shared_ptr<CompileTimeValue>
extractPropertyInitializationValue(VarDecl *propertyDecl) {
auto binding = propertyDecl->getParentPatternBinding();
if (binding) {
auto originalInit = binding->getOriginalInit(0);
if (originalInit) {
auto literalOutput = extractLiteralOutput(originalInit);
static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
if (expr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you seeing nullptrs passed into this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more of a precautionary construct, to avoid bad dereferences if nullptr were to get passed in, now or in the future.

switch (expr->getKind()) {
case ExprKind::Array:
case ExprKind::Dictionary:
case ExprKind::Tuple:

case ExprKind::BooleanLiteral:
case ExprKind::FloatLiteral:
case ExprKind::IntegerLiteral:
case ExprKind::NilLiteral:
case ExprKind::StringLiteral: {
std::string literalOutput;
llvm::raw_string_ostream OutputStream(literalOutput);
expr->printConstExprValue(&OutputStream, nullptr);
if (!literalOutput.empty()) {
return std::make_shared<RawLiteralValue>(literalOutput);
}
break;
}

if (auto callExpr = dyn_cast<CallExpr>(originalInit)) {
if (callExpr->getFn()->getKind() != ExprKind::ConstructorRefCall) {
return std::make_shared<RuntimeValue>();
}

case ExprKind::Call: {
auto callExpr = cast<CallExpr>(expr);
if (callExpr->getFn()->getKind() == ExprKind::ConstructorRefCall) {
std::vector<FunctionParameter> parameters;
const auto args = callExpr->getArgs();
for (auto arg : *args) {
auto label = arg.getLabel().str().str();
auto expr = arg.getExpr();

switch (expr->getKind()) {
case ExprKind::DefaultArgument: {
auto defaultArgument = cast<DefaultArgumentExpr>(expr);
auto argExpr = arg.getExpr();
const auto label = arg.getLabel().str().str();
const auto type = argExpr->getType();
if (auto defaultArgument = dyn_cast<DefaultArgumentExpr>(argExpr)) {
auto *decl = defaultArgument->getParamDecl();

if (decl->hasDefaultExpr()) {
literalOutput =
extractLiteralOutput(decl->getTypeCheckedDefaultExpr());
argExpr = decl->getTypeCheckedDefaultExpr();
}

break;
}
default:
literalOutput = extractLiteralOutput(expr);
break;
}

if (literalOutput.empty()) {
parameters.push_back(
{label, expr->getType(), std::make_shared<RuntimeValue>()});
} else {
parameters.push_back(
{label, expr->getType(),
std::make_shared<RawLiteralValue>(literalOutput)});
}
parameters.push_back({label, type, extractCompileTimeValue(argExpr)});
}

auto name = toFullyQualifiedTypeNameString(callExpr->getType());
return std::make_shared<InitCallValue>(name, parameters);
}
break;
}

default: {
break;
}
}
}
return std::make_shared<RuntimeValue>();
}

static std::shared_ptr<CompileTimeValue>
extractPropertyInitializationValue(VarDecl *propertyDecl) {
if (auto binding = propertyDecl->getParentPatternBinding()) {
if (auto originalInit = binding->getOriginalInit(0)) {
return extractCompileTimeValue(originalInit);
}
}

if (auto accessorDecl = propertyDecl->getAccessor(AccessorKind::Get)) {
auto node = accessorDecl->getTypecheckedBody()->getFirstElement();
if (node.is<Stmt *>()) {
if (auto returnStmt = dyn_cast<ReturnStmt>(node.get<Stmt *>())) {
auto expr = returnStmt->getResult();
std::string LiteralOutput = extractLiteralOutput(expr);
if (!LiteralOutput.empty())
return std::make_shared<RawLiteralValue>(LiteralOutput);
return extractCompileTimeValue(returnStmt->getResult());
}
}
}
Expand Down