Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Add indent parameter to Serializer.serialize() #2177

Merged
merged 3 commits into from
Apr 14, 2021

Conversation

jared-barocsi
Copy link
Contributor

@jared-barocsi jared-barocsi commented Apr 13, 2021

Type of Improvement

Code refactoring

API Impact

No impact

Backend Code Generation Impact

Should not impact code generation

Desired Merge Strategy

Squash

Release Notes

Add indentation parameter to Serializer.serialize() to indent FirrtlNode strings by a certain amount.

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels?
  • Did you mark the proper milestone (1.2.x, 1.3.0, 1.4.0) ?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you mark as Please Merge?

Using Utils.indent() gives deprecation warnings to use Serializer instead. However,
the Serializer class itself doesn't provide a means to manually indent a FirrtlNode
string a certain number of times.

The indent variable, previously hardcoded to 0, is now exposed as a second parameter
for the modified serialize function, and the old serialize function just calls the
modified serialize with indents = 0 for binary compatibility.
@CLAassistant
Copy link

CLAassistant commented Apr 13, 2021

CLA assistant check
All committers have signed the CLA.

}

/** Converts a `FirrtlNode` into its string representation. */
def serialize(node: FirrtlNode, indents: Int): String = {
Copy link
Contributor

@mwachs5 mwachs5 Apr 13, 2021

Choose a reason for hiding this comment

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

Suggested change
def serialize(node: FirrtlNode, indents: Int): String = {
def serialize(node: FirrtlNode, indents: Int = 0): String = {

Can't we do away with the above version by just adding a default param? Does that still match binary compatibility? (I don't actually know the answer this is a weak suggestion)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default parameter approach was my first attempt at solving this, but I'm not sure how it would be compiled into bytecode. My intuition tells me that the serialize with the default parameter will still be a two-parameter function (with some fancy magic to "insert" the 0 as an argument). So if we swap the firrtl binary then anything else that tries to run that will get a complaint that the one-parameter serialize doesn't exist.

Copy link
Contributor

Choose a reason for hiding this comment

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

looks like your intution is right: https://github.com/jatcwang/binary-compatibility-guide#dont-adding-parameters-with-default-values-to-methods

Not sure what the firrtl philosophy is on making this breaking change on master and then fixing in the backport version?

Copy link
Contributor

@jackkoenig jackkoenig Apr 13, 2021

Choose a reason for hiding this comment

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

I think just having the overloaded 2 methods is fine.

I'm going to be super bikesheddy here though and suggest naming the parameter just indent:

Suggested change
def serialize(node: FirrtlNode, indents: Int): String = {
def serialize(node: FirrtlNode, indent: Int): String = {

Doing a bit of search on other projects, singular tense is more common for this kind of parameter.

Comment on lines 13 to 14
/** Converts a `FirrtlNode` into its string representation
* with *zero* indents (old behavior for binary compatibility) */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** Converts a `FirrtlNode` into its string representation
* with *zero* indents (old behavior for binary compatibility) */
/** Converts a `FirrtlNode` into its string representation with default indentation
*/

A couple of things here:

  1. Use ScalaDoc style (stars align with the rightmost star from the start of the comment
  2. Slight tweak to phrasing, the behavior here didn't change so no reason to bring attention to it. There also is indentation if you pass something with things scoped inside it (like a Module or a When), so slightly more precise phrasing.

Put closing comment on new line

Co-authored-by: Megan Wachs <megan@sifive.com>
@jackkoenig jackkoenig merged commit 20890bb into chipsalliance:master Apr 14, 2021
mergify bot pushed a commit that referenced this pull request Apr 14, 2021
Using Utils.indent() gives deprecation warnings to use Serializer instead. However,
the Serializer class itself doesn't provide a means to manually indent a FirrtlNode
string a certain number of times.

The indent variable, previously hardcoded to 0, is now exposed as a second parameter
for the modified serialize function, and the old serialize function just calls the
modified serialize with indents = 0 for binary compatibility

Co-authored-by: Megan Wachs <megan@sifive.com>
(cherry picked from commit 20890bb)
@mergify mergify bot added the Backported This PR has been backported to marked stable branch label Apr 14, 2021
@ekiwi
Copy link
Contributor

ekiwi commented Apr 14, 2021

Congratulations on your first firrtl PR @jared-barocsi 🎉

azidar added a commit that referenced this pull request Apr 14, 2021
Add indent parameter to Serializer.serialize() (backport #2177)
@jared-barocsi jared-barocsi deleted the add-serializer-indents branch April 15, 2021 12:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Addition (no impact on existing code) Backported This PR has been backported to marked stable branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants