Skip to content

Merging of Zng definitons, Type Templates, and Conditional Definitions #41

Description

@Ryex

Overview

There are three features I feel would be really nice to have

  • The ability to split definitions across multiple files for organizational purposes
  • The ability to define a Template definition for generic types
  • The ability to specify definitions and members there of conditionally

I group these into one because depending on their implementation they can have significant overlap.

I had at least a preliminary plan for the implementation of each

  • Merging

    • Change the zngur_def::ZngurFile::types member form a Vec to an IndexMap that maps a RustType to its ZngurType
    • change add_to_zngur_file to check if it's type is present already and if it is merge
      • Confirm matching layout
      • Append non overlapping fields and Well Known Traits
      • Append new member functions
      • Error on conflict
    • Allow specifying multiple input files to parse, and are considered to be concatenated together
  • Template Types

    • syntax similar to
      type<T> ::std::option::Option<T> {
          #layout(size = {T.size}, align = {T.align})
      }
      
      type<T,E> ::std::result::Result<T, E> {
          #layout(size = {max(T.size, E.size)}, align = {max(T.align, E.align)})
      }
      
      type<T> crate::SomeWrapper<T> {
          #layout(size = {T.size + 12}, align = 8)
      }
    • Templates are expanded last, after aliases, ONLY if a field or member function references a type that is not already defined
    • Alternatively, Templates are expanded for every use but are merged with existing definitions deferring to the explicit definitions on conflict
  • Conditional definition

    • Use familiar #[cfg(target = "x86_64")] syntax
    • Expanded using Cargo ENV similar to cxx
    • evaluate after parse to remove non matching definitions

Questions

I'd like some input on if these features fit within Zugur and if so what they should look like.

  • Syntax
    • I could also see a template syntax like:
      template <T> ::std::option::Option<T> {
         #layout(size = {T.size}, align = {T.align})
      }
    • Conditionals could also use a more Preprocessor like #ifcfg /* ...*/ #endif (not sure I'd prefer that though
  • Operations allowed when evaluating template size (min, max +, - ? * ? / ?)
  • CLI support for conditional expansion

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions