Skip to content

Commit

Permalink
Fix namespacing of hilti IDs in Spicy-side diagnostic output.
Browse files Browse the repository at this point in the history
We now show them with a `spicy` prefix, which makes more sense for
users.

Closes #1803.
  • Loading branch information
rsmmr committed Oct 18, 2024
1 parent a866f35 commit 4ebb7c9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 4 deletions.
6 changes: 3 additions & 3 deletions doc/autogen/types/bytes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Returns an iterator representing the offset *i* inside the bytes
value.

.. spicy:method:: bytes::decode bytes decode False string ([ charset: spicy::Charset = hilti::Charset::UTF8 ], [ errors: spicy::DecodeErrorStrategy = hilti::DecodeErrorStrategy::REPLACE ])
.. spicy:method:: bytes::decode bytes decode False string ([ charset: spicy::Charset = spicy::Charset::UTF8 ], [ errors: spicy::DecodeErrorStrategy = spicy::DecodeErrorStrategy::REPLACE ])
Interprets the ``bytes`` as representing an binary string encoded with
the given character set, and converts it into a UTF8 string. If data
Expand All @@ -29,7 +29,7 @@
as printable strings. The portions will be separated by the bytes
value to which this method is invoked as a member.

.. spicy:method:: bytes::lower bytes lower False bytes ([ charset: spicy::Charset = hilti::Charset::UTF8 ], [ errors: spicy::DecodeErrorStrategy = hilti::DecodeErrorStrategy::REPLACE ])
.. spicy:method:: bytes::lower bytes lower False bytes ([ charset: spicy::Charset = spicy::Charset::UTF8 ], [ errors: spicy::DecodeErrorStrategy = spicy::DecodeErrorStrategy::REPLACE ])
Returns a lower-case version of the bytes value, assuming it is
encoded in character set *charset*. If data is encountered that
Expand Down Expand Up @@ -139,7 +139,7 @@
conversion fails, throws a `RuntimeError` exception, this can happen
when ``bytes`` is empty or its size is larger than 8 bytes.

.. spicy:method:: bytes::upper bytes upper False bytes ([ charset: spicy::Charset = hilti::Charset::UTF8 ], [ errors: spicy::DecodeErrorStrategy = hilti::DecodeErrorStrategy::REPLACE ])
.. spicy:method:: bytes::upper bytes upper False bytes ([ charset: spicy::Charset = spicy::Charset::UTF8 ], [ errors: spicy::DecodeErrorStrategy = spicy::DecodeErrorStrategy::REPLACE ])
Returns an upper-case version of the bytes value, assuming it is
encoded in character set *charset*. If data is encountered that
Expand Down
2 changes: 1 addition & 1 deletion doc/autogen/types/string.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.. rubric:: Methods

.. spicy:method:: string::encode string encode False bytes ([ charset: spicy::Charset = hilti::Charset::UTF8 ])
.. spicy:method:: string::encode string encode False bytes ([ charset: spicy::Charset = spicy::Charset::UTF8 ])
Converts the string into a binary representation encoded with the
given character set.
Expand Down
7 changes: 7 additions & 0 deletions spicy/toolchain/include/compiler/detail/printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ namespace spicy::detail::printer {
*/
bool print(hilti::printer::Stream& stream, Node* root);

/**
* Prints out an AST ID. This only prints the ID if the Spicy-side output needs
* to be different from the default HILTI output. Returns true in that case,
* otherwise false.
*/
bool printID(hilti::printer::Stream& out, const ID& id);

} // namespace spicy::detail::printer
2 changes: 2 additions & 0 deletions spicy/toolchain/src/compiler/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ hilti::Plugin spicy::detail::createSpicyPlugin() {

.ast_print = [](Node* node, hilti::printer::Stream& out) { return printer::print(out, node); },

.ast_print_id = [](const ID& id, hilti::printer::Stream& out) { return printer::printID(out, id); },

.ast_transform = [](hilti::Builder* builder, hilti::ASTRoot* m) -> bool {
auto spicy_builder = static_cast<spicy::Builder*>(builder);
return CodeGen(spicy_builder).compileAST(m);
Expand Down
20 changes: 20 additions & 0 deletions spicy/toolchain/src/compiler/printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@

using namespace spicy;

bool spicy::detail::printer::printID(hilti::printer::Stream& out, const ID& id) {
// In user-visible output, replace any `hilti` prefix with `spicy`. This is
// a bit of a hammer: it's assuming that any HILTI types showing up there
// have a corresponding Spicy type. The alternative would be to explicitly
// identify valid mappings somehow (e.g., through a shared `&cxxname`).
// However, that's neither easy nor is it clear that that's worth it. For
// one, we currently do indeed maintain only such 1:1 mappings (i.e., we
// don't rename IDs existing at both layers other than the namespace). And
// second, when displaying Spicy code to users, there should really never
// be any HILTI identifier showing up anyways; so if we still end up with
// any, printing them with a `spicy` prefix is probably still a better
// solution than just printing them as-is.
if ( out.state().user_visible && id.namespace_() && id.sub(0) == ID("hilti") ) {
out << ID("spicy", id.sub(1, -1));
return true;
}

return false;
}

namespace {

struct VisitorPrinter : visitor::PreOrder {
Expand Down
10 changes: 10 additions & 0 deletions tests/Baseline/spicy.tools.spicyc-hilti-free-output-2/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
module Test {

import spicy;
import hilti;
import spicy_rt;

hilti::print(b"xxx".decode(spicy::Charset::UTF8, hilti::DecodeErrorStrategy::REPLACE), True);

}
5 changes: 5 additions & 0 deletions tests/Baseline/spicy.tools.spicyc-hilti-free-output/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[error] <...>/spicyc-hilti-free-output.spicy:8:7-8:28: call does not match any method: <const bytes>.decode(<string>)
[error] <...>/spicyc-hilti-free-output.spicy:8:7-8:28: candidates:
[error] <...>/spicyc-hilti-free-output.spicy:8:7-8:28: - <bytes>.decode([charset: spicy::Charset = spicy::Charset::UTF8], [errors: spicy::DecodeErrorStrategy = spicy::DecodeErrorStrategy::REPLACE])
[error] spicyc: aborting after errors
16 changes: 16 additions & 0 deletions tests/spicy/tools/spicyc-hilti-free-output.spicy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# @TEST-EXEC: spicyc -p -d %INPUT >output 2>&1; true
# @TEST-EXEC: btest-diff output
#
# @TEST-DOC: Checks that HILTI-level identifiers are not showing up in compiler errors, but do show up when printing generated HILTI code; regression test for #1803

module Test;

print b"xxx".decode("utf-8");

# @TEST-START-NEXT

module Test;

import spicy;

print b"xxx".decode(spicy::Charset::UTF8);

0 comments on commit 4ebb7c9

Please sign in to comment.