Skip to content

cmd/compile: refactor access to Val values to use node methods #27212

Closed

Description

@martisch

For accessing e.g the string value of a node n that is a string literal the cmd/compile code base (walk.go, typecheck.go...) often uses n.Val().U.(string). This exposes a lot of details about how constants and literals are implemented in the compiler to code that is mostly operating on nodes.

Some types such as CTBOOL and CTINT already have node methods ((n *Node) Bool() and (n *Node) Int64()) to extract the value the node represents.

We can abstract away how values are represented with node methods and use these outside const.go instead of relying on the implementation details of how values are stored in nodes.
All implementation details of how values are operated on inside nodes can be confined to const.go and the specific files for helper structures such as MpInt and MpFlt.

This should make the compiler code more readable, maintainable and help efforts to easier replace how the compiler stores and handles constants e.g. #4617.

Alternative

Instead of methods on the level of node they could be methods on the level of Val.
So instead of n.StringVal() for node n. It could be n.Val().StringValue().
This would keep the set of methods for operating on the subset of nodes with values separated instead of merging them into the pool of general node methods.
The downsides for the alternative would be:

  • more verbose
  • extra function call if we later decide to make a new constant implementation just a direct interface field of a node with no overlap with other node info.
  • likely larger binary size due to more Val() function calls all around the code base

cc @josharian @griesemer

Activity

  1. added this to the Unplanned milestone on Aug 25, 2018
  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Aug 30, 2018
  3. griesemer commented on Sep 5, 2018

    @griesemer
    Contributor

    @martisch I think you should experiment with this and send a CL if successful. I am going to assign this to you.

  4. martisch commented on Dec 3, 2020

    @martisch
    ContributorAuthor

    /cc @mdempsky

    I think with the CLs to make the compiler use go/constant package this will be resolved.

  5. removed their assignment
    on Dec 3, 2020
  6. mdempsky commented on Dec 3, 2020

    @mdempsky
    Contributor

    Yeah, I think this is basically done now on the dev.regabi branch. We can probably continue improving the API, but values are now opaquely represented using constant.Value.

  7. locked and limited conversation to collaborators on Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions