Skip to content

Commit

Permalink
Fix printing of ID scopes.
Browse files Browse the repository at this point in the history
The intent has always been to normalize ID scopes so that they aren't
prefixed with the current module. However, that wasn't always working
because we lost the state of the current scope. Now moving that into a
global to retain it across different printing streams.
  • Loading branch information
rsmmr committed Dec 16, 2022
1 parent fa7e160 commit 17a5676
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 47 deletions.
5 changes: 0 additions & 5 deletions hilti/toolchain/include/compiler/printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ class Stream {

char newline() const { return _nl; }

const ID& currentScope() const { return _scopes.back(); }
void pushScope(ID id) { _scopes.push_back(std::move(id)); }
void popScope() { _scopes.pop_back(); }

bool isCompact() { return _compact; }
bool setCompact(bool new_compact) {
auto old = _compact;
Expand Down Expand Up @@ -120,7 +116,6 @@ class Stream {
bool _first_in_block = false;
bool _last_in_block = false;
bool _expand_subsequent_type = false;
std::vector<ID> _scopes = {""};
};

} // namespace hilti::printer
16 changes: 13 additions & 3 deletions hilti/toolchain/src/compiler/visitors/printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
using namespace hilti;
using util::fmt;

// Global state storing any scopes we are currently in during printing.
// Maintaining this globally isn't great, but because of various independent
// `printAST()` calls happening recursively through `operator<<` and `fmt()`,
// we can't easily pass this state around.
static std::vector<ID> _scopes = {""};

static const ID& _currentScope() { return _scopes.back(); }
static void _pushScope(ID id) { _scopes.push_back(std::move(id)); }
static void _popScope() { _scopes.pop_back(); }

static std::string renderOperator(operator_::Kind kind, const std::vector<std::string>& ops) {
switch ( kind ) {
case operator_::Kind::Add: return fmt("add %s[%s]", ops[0], ops[1]);
Expand Down Expand Up @@ -163,7 +173,7 @@ struct Visitor : visitor::PreOrder<void, Visitor> {
}

void operator()(const ID& n) {
if ( n.namespace_() == out.currentScope() )
if ( n.namespace_() == _currentScope() )
out << std::string(n.local());
else
out << std::string(n);
Expand All @@ -174,7 +184,7 @@ struct Visitor : visitor::PreOrder<void, Visitor> {
out << "module " << n.id() << " {" << out.newline();
out.endLine();

out.pushScope(n.id());
_pushScope(n.id());

auto printDecls = [&](const auto& decls) {
for ( const auto& d : decls )
Expand All @@ -200,7 +210,7 @@ struct Visitor : visitor::PreOrder<void, Visitor> {
if ( ! n.statements().statements().empty() )
out.emptyLine();

out.popScope();
_popScope();

out.beginLine();
out << "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type X = struct {

global optional<int<64>> i = global_unimplemented_int64();
global X x;
global optional<int<64>> j = Foo::x.unimplemented_int64();
global optional<int<64>> j = x.unimplemented_int64();

declare public function hook void global_unimplemented_void();
declare public function hook void global_implemented();
Expand All @@ -24,7 +24,7 @@ method hook void X::implemented() {

global_implemented();
global_unimplemented_void();
Foo::x.implemented();
Foo::x.unimplemented_void();
x.implemented();
x.unimplemented_void();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ method hook void X::implemented() {

global_implemented();
default<void>();
Foo::x.implemented();
x.implemented();
default<void>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ method extern method view<stream> foo::P0::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::P0::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/default-parser-functions.spicy:12:11"
local value_ref<P0> unit = value_ref(default<P0>())value_ref(default<P0>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::P0));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(P0));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -471,7 +471,7 @@ method extern method view<stream> foo::P1::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::P1::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/default-parser-functions.spicy:14:18"
local value_ref<P1> unit = value_ref(default<P1>())value_ref(default<P1>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::P1));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(P1));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -745,7 +745,7 @@ method extern method view<stream> foo::P2::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::P2::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/default-parser-functions.spicy:16:18-21:2"
local value_ref<P2> unit = value_ref(default<P2>())value_ref(default<P2>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::P2));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(P2));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ method extern method view<stream> foo::P1::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::P1::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/default-parser-functions.spicy:14:18"
local value_ref<P1> unit = value_ref(default<P1>())value_ref(default<P1>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::P1));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(P1));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -265,7 +265,7 @@ method extern method view<stream> foo::P2::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::P2::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/default-parser-functions.spicy:16:18-21:2"
local value_ref<P2> unit = value_ref(default<P2>())value_ref(default<P2>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::P2));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(P2));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down
14 changes: 7 additions & 7 deletions tests/Baseline/spicy.optimization.feature_requirements/noopt.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ method extern method view<stream> foo::X1::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X1::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:13:11-15:2"
local value_ref<X1> unit = value_ref(default<X1>())value_ref(default<X1>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X1));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X1));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -567,7 +567,7 @@ method extern method view<stream> foo::X2::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X2::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:18:11"
local value_ref<X2> unit = value_ref(default<X2>())value_ref(default<X2>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X2));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X2));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -785,7 +785,7 @@ method extern method view<stream> foo::X3::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X3::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:21:11-23:2"
local value_ref<X3> unit = value_ref(default<X3>())value_ref(default<X3>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X3));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X3));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -1003,7 +1003,7 @@ method extern method view<stream> foo::X4::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X4::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:27:11-29:2"
local value_ref<X4> unit = value_ref(default<X4>())value_ref(default<X4>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X4));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X4));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -1056,7 +1056,7 @@ init function void __register_foo_X4() {
}

method hook void foo::X5::__on_0x25_init() {
spicy_rt::filter_connect(self, new foo::X4());
spicy_rt::filter_connect(self, new X4());
}

method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::RecoverableFailure>> foo::X5::__parse_stage1(inout value_ref<stream> __data, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) {
Expand Down Expand Up @@ -1217,7 +1217,7 @@ method extern method view<stream> foo::X5::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X5::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:31:18-35:2"
local value_ref<X5> unit = value_ref(default<X5>())value_ref(default<X5>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X5));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X5));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -1429,7 +1429,7 @@ method extern method view<stream> foo::X6::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X6::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:38:11-41:2"
local value_ref<X6> unit = value_ref(default<X6>())value_ref(default<X6>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X6));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X6));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ method extern method view<stream> foo::X4::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X4::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:27:11-29:2"
local value_ref<X4> unit = value_ref(default<X4>())value_ref(default<X4>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X4));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X4));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -208,7 +208,7 @@ init function void __register_foo_X4() {
}

method hook void foo::X5::__on_0x25_init() {
spicy_rt::filter_connect(self, new foo::X4());
spicy_rt::filter_connect(self, new X4());
}

method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::RecoverableFailure>> foo::X5::__parse_stage1(inout value_ref<stream> __data, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) {
Expand Down Expand Up @@ -292,7 +292,7 @@ method extern method view<stream> foo::X5::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X5::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:31:18-35:2"
local value_ref<X5> unit = value_ref(default<X5>())value_ref(default<X5>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X5));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X5));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -399,7 +399,7 @@ method extern method view<stream> foo::X6::parse1(inout value_ref<stream> data,
method extern method view<stream> foo::X6::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/feature_requirements.spicy:38:11-41:2"
local value_ref<X6> unit = value_ref(default<X6>())value_ref(default<X6>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::X6));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(X6));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down
10 changes: 5 additions & 5 deletions tests/Baseline/spicy.optimization.unused-functions/noopt.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ method extern method view<stream> foo::A::parse1(inout value_ref<stream> data, o
method extern method view<stream> foo::A::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/unused-functions.spicy:18:10"
local value_ref<A> unit = value_ref(default<A>())value_ref(default<A>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::A));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(A));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -537,7 +537,7 @@ method extern method view<stream> foo::B::parse1(inout value_ref<stream> data, o
method extern method view<stream> foo::B::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/unused-functions.spicy:21:17"
local value_ref<B> unit = value_ref(default<B>())value_ref(default<B>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::B));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(B));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -743,7 +743,7 @@ method extern method view<stream> foo::C::parse1(inout value_ref<stream> data, o
method extern method view<stream> foo::C::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/unused-functions.spicy:24:10"
local value_ref<C> unit = value_ref(default<C>())value_ref(default<C>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::C));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(C));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -964,7 +964,7 @@ method extern method view<stream> foo::D::parse1(inout value_ref<stream> data, o
method extern method view<stream> foo::D::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/unused-functions.spicy:25:17-27:2"
local value_ref<D> unit = value_ref(default<D>())value_ref(default<D>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::D));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(D));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down Expand Up @@ -1178,7 +1178,7 @@ method extern method view<stream> foo::F::parse1(inout value_ref<stream> data, o
method extern method view<stream> foo::F::parse3(inout value_ref<spicy_rt::ParsedUnit> gunit, inout value_ref<stream> data, optional<view<stream>> cur = Null, optional<spicy_rt::UnitContext> context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static {
# "<...>/unused-functions.spicy:30:10-32:2"
local value_ref<F> unit = value_ref(default<F>())value_ref(default<F>());
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(foo::F));
spicy_rt::initializeParsedUnit(gunit, unit, typeinfo(F));
local view<stream> ncur = cur ? (*cur) : cast<view<stream>>((*data));
local int<64> lahead = 0;
local iterator<stream> lahead_end;
Expand Down
Loading

0 comments on commit 17a5676

Please sign in to comment.