Skip to content
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

Scaffolding for gradual migration to AST::Expression #240

Draft
wants to merge 4 commits into
base: prism
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions ast/desugar/Desugar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,12 @@ ExpressionPtr node2TreeImpl(DesugarContext dctx, unique_ptr<parser::Node> what)
if (what.get() == nullptr) {
return MK::EmptyTree();
}

if (auto expr = what->getCachedDesugaredExpr()) {
Copy link
Author

Choose a reason for hiding this comment

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

The translations we implement in the Translator will fill this in, which we can use here to short-circuit the logic below.

Thinking goes that once we've implemented it for all nodes, none of the code below will be used anymore, and we can just delete and inline all this stuff.

// This is a NodeAndExpr node that's already been translated to `ExpressionPtr` in `Translator.cc`
return expr;
}

auto loc = what->loc;
auto locZeroLen = what->loc.copyWithZeroLength();
ENFORCE(loc.exists(), "parse-tree node has no location: {}", what->toString(dctx.ctx));
Expand Down
2 changes: 1 addition & 1 deletion main/pipeline/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ unique_ptr<parser::Node> runPrismParser(core::GlobalState &gs, core::FileRef fil
return std::unique_ptr<parser::Node>();
}

return Prism::Translator(parser, gs).translate(std::move(root));
return Prism::Translator(std::move(parser), gs).translate(std::move(root));
Copy link
Author

Choose a reason for hiding this comment

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

This just saves one reference count bump on the shared_ptr within the parser. Won't someone think of those poor poor clock cycles?!

}

ast::ExpressionPtr runDesugar(core::GlobalState &gs, core::FileRef file, unique_ptr<parser::Node> parseTree,
Expand Down
1 change: 1 addition & 0 deletions parser/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cc_library(
deps = [
"//core",
"//parser/parser",
"//ast",
"@com_google_absl//absl/algorithm:container",
],
)
Expand Down
12 changes: 11 additions & 1 deletion parser/Node.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SORBET_PARSER_NODE_H
#define SORBET_PARSER_NODE_H

#include "ast/Trees.h"
Copy link
Author

Choose a reason for hiding this comment

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

Needed for ast::ExpressionPtr

#include "common/common.h"
#include "core/core.h"
#include <memory>
Expand All @@ -22,6 +23,15 @@ class Node {
virtual std::string toJSONWithLocs(const core::GlobalState &gs, core::FileRef file, int tabs = 0) = 0;
virtual std::string toWhitequark(const core::GlobalState &gs, int tabs = 0) = 0;
virtual std::string nodeName() = 0;

virtual ast::ExpressionPtr getCachedDesugaredExpr() {
return nullptr;
}

virtual void cacheDesugaredExpr(ast::ExpressionPtr expr) {
// no-op, except for the override in NodeAndExpr
}

core::LocOffsets loc;

protected:
Expand All @@ -40,7 +50,7 @@ template <class To> To *cast_node(Node *what) {
static_assert(!std::is_pointer<To>::value, "To has to be a pointer");
static_assert(std::is_assignable<Node *&, To *>::value, "Ill Formed To, has to be a subclass of Expression");
#if __cplusplus >= 201402L
static_assert(std::is_final<To>::value, "To is not final");
// static_assert(std::is_final<To>::value, "To is not final");
Comment on lines -43 to +53
Copy link
Author

Choose a reason for hiding this comment

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

I had to temporarily disable this, since the classes we're checking against are no longer final :/

#elif __has_feature(is_final)
static_assert(__is_final(To), "To is not final");
#else
Expand Down
Loading