Skip to content
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

Desugar type use abbreviation #16

Merged
merged 16 commits into from
Jul 20, 2023
Merged

Desugar type use abbreviation #16

merged 16 commits into from
Jul 20, 2023

Commits on Jul 20, 2023

  1. Return current field as type definition from #process_type_definition

    This is where type definitions actually originate from. I’m choosing to
    represent a type definition with its originating field verbatim (versus
    e.g. only the function type) so that we can use the same array to both
    look up existing type definitions and store any new ones which get
    generated as a result of desugaring the type use abbreviation.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    9a2c347 View commit details
    Browse the repository at this point in the history
  2. Return legitimately empty type definitions from all other fields

    We’ve filled in the type definitions returned by #process_type_definition,
    and we know that all other fields generate no immediate type
    definitions. (Notwithstanding the effect of desugaring the type use
    abbreviation, but we haven’t implemented that yet.)
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    65975bc View commit details
    Browse the repository at this point in the history
  3. Remove unused DUMMY_TYPE_DEFINITIONS constant

    The real type definition lists (sometimes legitimately empty!) have now
    been filled in everywhere, so we no longer need a dummy value.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    6dd6f86 View commit details
    Browse the repository at this point in the history
  4. Handle abbreviated type uses separately in #process_typeuse

    When no `(type …)` is present we’ll need to automatically insert one
    [0], which is going to be more complicated than simply passing through
    the existing one verbatim and desugaring its parameters & results, so it
    needs a dedicated code path.
    
    [0] https://webassembly.github.io/spec/core/text/modules.html#abbreviations
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    0a73248 View commit details
    Browse the repository at this point in the history
  5. Insert a type index when a matching type definition already exists

    This is the simplest case of the type use abbreviation [0]: if we can
    find an existing type definition whose type is exactly the same as the
    one being used, we insert a reference to that definition.
    
    We’re not yet accounting for (irrelevant) parameter identifier
    mismatches, nor for the case where no matching definition is found.
    
    This commit includes a lot of tests. Earlier commits have already
    plumbed the type definitions into every place where a type use can
    syntactically occur, but this is our first opportunity to check that
    it’s actually worked in all those cases.
    
    [0] https://webassembly.github.io/spec/core/text/modules.html#abbreviations
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    3aa1152 View commit details
    Browse the repository at this point in the history
  6. Extract #function_type helper for computing function types

    This allows us to extract the parameter and result declarations and
    compare them separately, rather than comparing the function type
    verbatim. This change is a prerequisite for ignoring parameter
    identifiers, which we’ll do next.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    246ed24 View commit details
    Browse the repository at this point in the history
  7. Extract parameter and result value types in #function_type

    This ignores identifiers in type definitions and uses, allowing them to
    match even when the identifiers don’t.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    3a724df View commit details
    Browse the repository at this point in the history
  8. Generate a new type definition if no matching one is found

    Per the spec [0], “if no such index exists, then a new type definition
    […] is inserted at the end of the module”. We’re doing this by mutating
    the incoming array of type definitions, which isn’t particularly elegant
    but gets the job done without having to worry about threading another
    result value (i.e. a separate array of generated type definitions)
    through the field-processing methods.
    
    We can’t add tests for this commit’s behaviour in isolation because the
    resulting program is syntactically invalid: we’re inserting the index of
    a type definition which hasn’t yet been added to the module. In the next
    commit we’ll also insert the generated definitions at the end of the
    module and add some tests to check that both halves of the solution are
    working together correctly.
    
    [0] https://webassembly.github.io/spec/core/text/modules.html#abbreviations
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    45c0ae5 View commit details
    Browse the repository at this point in the history
  9. Add generated type definitions to module fields in #process_module

    We need a deep copy of the generated type definitions because they
    contain object references to parameters and results elsewhere in the
    source S-expression; since the AST parser consumes the S-expression
    destructively, any structural sharing will cause serious problems.
    
    The approach of “count the type definitions before and after processing
    the fields, and the difference between these two counts must be the
    newly-generated definitions” is a bit hacky, but it gives us the
    behaviour we need. In future we should consider a more elegant approach
    to this whole problem of modifying different parts of the S-expression
    during preprocessing.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    a865637 View commit details
    Browse the repository at this point in the history
  10. Only try to parse a type use in #parse_blocktype when one is present

    Now that the preprocessor is desugaring all legitimate occurrences of
    the type use abbreviation, it’s safe for the parser to assume that a
    block type contains an optional value type [0] if the `type` keyword is
    missing.
    
    [0] https://webassembly.github.io/spec/core/text/instructions.html#text-blocktype
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    4928ab6 View commit details
    Browse the repository at this point in the history
  11. Remove support for type use abbreviation from AST parser

    This is desugared by the preprocessor now.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    8be010e View commit details
    Browse the repository at this point in the history
  12. Remove support for block type abbreviation from AST parser

    This is desugared by the preprocessor now.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    831badf View commit details
    Browse the repository at this point in the history
  13. Stop updating type definitions in AST parser

    The preprocessor has already generated all necessary type definitions
    while desugaring the type use abbreviation.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    2ba7b9f View commit details
    Browse the repository at this point in the history
  14. Add explicit context: argument to AST parser methods

    The initial context is no longer mutated during parsing because the
    preprocessor has already done the work to insert whatever type
    definitions are needed, so we don’t need to worry about allowing methods
    to update it. We’re therefore switching to an explicit argument since
    it’s clearer and easier to understand.
    
    For now we have to be careful to use the modified context (i.e.
    `self.context`) inside #with_context blocks instead of the `context`
    local variable. In the next commit we’ll be able to undo this when we
    remove #with_context calls entirely.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    d4af96d View commit details
    Browse the repository at this point in the history
  15. Remove uses of #with_context from AST parser

    The previous commit switched to an explicit `context:` argument, so we
    no longer need to update the current context.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    a4c98e7 View commit details
    Browse the repository at this point in the history
  16. Remove unused #with_context method and context attribute from AST p…

    …arser
    
    We now pass the context around explicitly.
    tomstuart committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    a998b64 View commit details
    Browse the repository at this point in the history