Skip to content

feat: Add support for fields, attribute accessors and funcs in method calls #21

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 3 commits into from
Jul 15, 2022
Merged
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
8 changes: 4 additions & 4 deletions cfg/builder/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class CFGContext {
BasicBlock *breakScope;
BasicBlock *rescueScope;
std::shared_ptr<core::SendAndBlockLink> link;
UnorderedMap<core::SymbolRef, LocalRef> &aliases;
UnorderedMap<core::NameRef, LocalRef> &discoveredUndeclaredFields;
UnorderedMap<core::SymbolRef, LocalOccurrence> &aliases;
UnorderedMap<core::NameRef, LocalOccurrence> &discoveredUndeclaredFields;

uint32_t &temporaryCounter;

Expand All @@ -64,8 +64,8 @@ class CFGContext {
private:
friend std::unique_ptr<CFG> CFGBuilder::buildFor(core::Context ctx, ast::MethodDef &md);
CFGContext(core::Context ctx, CFG &inWhat, LocalOccurrence target, int loops, BasicBlock *nextScope,
BasicBlock *breakScope, BasicBlock *rescueScope, UnorderedMap<core::SymbolRef, LocalRef> &aliases,
UnorderedMap<core::NameRef, LocalRef> &discoveredUndeclaredFields, uint32_t &temporaryCounter)
BasicBlock *breakScope, BasicBlock *rescueScope, UnorderedMap<core::SymbolRef, LocalOccurrence> &aliases,
UnorderedMap<core::NameRef, LocalOccurrence> &discoveredUndeclaredFields, uint32_t &temporaryCounter)
: ctx(ctx), inWhat(inWhat), target(target), loops(loops), isInsideRubyBlock(false), breakIsJump(false),
nextScope(nextScope), breakScope(breakScope), rescueScope(rescueScope), aliases(aliases),
discoveredUndeclaredFields(discoveredUndeclaredFields), temporaryCounter(temporaryCounter){};
Expand Down
15 changes: 7 additions & 8 deletions cfg/builder/builder_entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ unique_ptr<CFG> CFGBuilder::buildFor(core::Context ctx, ast::MethodDef &md) {
res->loc = md.loc;
res->symbol = md.symbol.data(ctx)->dealiasMethod(ctx);
uint32_t temporaryCounter = 1;
UnorderedMap<core::SymbolRef, LocalRef> aliases;
UnorderedMap<core::NameRef, LocalRef> discoveredUndeclaredFields;
UnorderedMap<core::SymbolRef, LocalOccurrence> aliases;
UnorderedMap<core::NameRef, LocalOccurrence> discoveredUndeclaredFields;
CFGContext cctx(ctx, *res.get(), LocalOccurrence::synthetic(LocalRef::noVariable()), 0, nullptr, nullptr, nullptr,
aliases, discoveredUndeclaredFields, temporaryCounter);

Expand Down Expand Up @@ -107,11 +107,10 @@ unique_ptr<CFG> CFGBuilder::buildFor(core::Context ctx, ast::MethodDef &md) {
vector<Binding> aliasesPrefix;
for (auto kv : aliases) {
core::SymbolRef global = kv.first;
LocalRef local = kv.second;
aliasesPrefix.emplace_back(LocalOccurrence::synthetic(local), core::LocOffsets::none(),
make_insn<Alias>(global));
LocalOccurrence local = kv.second;
aliasesPrefix.emplace_back(LocalOccurrence::synthetic(local.variable), local.loc, make_insn<Alias>(global));
if (global.isFieldOrStaticField()) {
res->minLoops[local.id()] = CFG::MIN_LOOP_FIELD;
res->minLoops[local.variable.id()] = CFG::MIN_LOOP_FIELD;
} else {
// We used to have special handling here for "MIN_LOOP_GLOBAL" but it was meaningless,
// because it only happened for type members, and we already prohibit re-assigning type
Expand All @@ -122,9 +121,9 @@ unique_ptr<CFG> CFGBuilder::buildFor(core::Context ctx, ast::MethodDef &md) {
}
}
for (auto kv : discoveredUndeclaredFields) {
aliasesPrefix.emplace_back(LocalOccurrence::synthetic(kv.second), core::LocOffsets::none(),
aliasesPrefix.emplace_back(LocalOccurrence::synthetic(kv.second.variable), kv.second.loc,
make_insn<Alias>(core::Symbols::Magic_undeclaredFieldStub(), kv.first));
res->minLoops[kv.second.id()] = CFG::MIN_LOOP_FIELD;
res->minLoops[kv.second.variable.id()] = CFG::MIN_LOOP_FIELD;
}
histogramInc("cfgbuilder.aliases", aliasesPrefix.size());
auto basicBlockCreated = res->basicBlocks.size();
Expand Down
19 changes: 10 additions & 9 deletions cfg/builder/builder_walk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,20 @@ void CFGBuilder::unconditionalJump(BasicBlock *from, BasicBlock *to, CFG &inWhat

namespace {

LocalRef global2Local(CFGContext cctx, core::SymbolRef what) {
LocalRef global2Local(CFGContext cctx, core::SymbolRef what, core::LocOffsets loc) {
if (what == core::Symbols::StubModule()) {
// We don't need all stub module assignments to alias to the same temporary.
// (The fact that there's a StubModule at all means an error was already reported elsewhere)
return cctx.newTemporary(what.name(cctx.ctx));
}

// Note: this will add an empty local to aliases if 'what' is not there
LocalRef &alias = cctx.aliases[what];
if (!alias.exists()) {
alias = cctx.newTemporary(what.name(cctx.ctx));
auto &alias = cctx.aliases[what];
if (!alias.variable.exists()) {
alias.loc = loc;
alias.variable = cctx.newTemporary(what.name(cctx.ctx));
}
return alias;
return alias.variable;
}

LocalRef unresolvedIdent2Local(CFGContext cctx, const ast::UnresolvedIdent &id) {
Expand Down Expand Up @@ -91,12 +92,12 @@ LocalRef unresolvedIdent2Local(CFGContext cctx, const ast::UnresolvedIdent &id)
}
}
auto ret = cctx.newTemporary(id.name);
cctx.discoveredUndeclaredFields[id.name] = ret;
cctx.discoveredUndeclaredFields[id.name] = {ret, id.loc};
return ret;
}
return fnd->second;
return fnd->second.variable;
} else {
return global2Local(cctx, sym);
return global2Local(cctx, sym, id.loc);
}
}

Expand Down Expand Up @@ -329,7 +330,7 @@ BasicBlock *CFGBuilder::walk(CFGContext cctx, ast::ExpressionPtr &what, BasicBlo
[&](ast::Assign &a) {
LocalRef lhs;
if (auto lhsIdent = ast::cast_tree<ast::ConstantLit>(a.lhs)) {
lhs = global2Local(cctx, lhsIdent->symbol);
lhs = global2Local(cctx, lhsIdent->symbol, a.loc);
} else if (auto lhsLocal = ast::cast_tree<ast::Local>(a.lhs)) {
lhs = cctx.inWhat.enterLocal(lhsLocal->localVariable);
} else if (auto ident = ast::cast_tree<ast::UnresolvedIdent>(a.lhs)) {
Expand Down
Loading