Skip to content

Proposal: Unified Macro Definitions #58

@wangeguo

Description

@wangeguo

Problem Statement

The current Pliron Declarative Syntax Specification presents the following pain points:

  1. Inconsistent Syntax: Disparate syntax between operations/types/attributes (e.g. #[def_op] vs #[def_type]), appearing fragmented
  2. Verbose Trait Derivation: Overly cumbersome macros like #[derive_op_interface_impl(...)]
  3. Scattered Implementation: Field-level attributes mixed with implementation blocks (e.g. impl_verify_succ!)

Proposed Solution

This proposal addresses the above issues through:

  • Adoption of standardized #[derive(Operation/Type/Attribute)] syntax
  • Consolidation of all metadata within #[definition] attributes
  • Support for direct field-level attribute application

Note: Established macro patterns from popular crates like serde and clap can serve as references to improve consistency.

Current vs Proposed Implementation Comparison

1. Operation Definition

Current Implementation:

#[format_op("`(`$0`)` region($0)")]
#[def_op("test.if_op")]
#[derive_op_interface_impl(OneOpdInterface, ZeroResultInterface, OneRegionInterface)]
struct IfOp {}
impl_verify_succ!(IfOp);

Proposed Implementation:

#[derive(Operation)]
#[definition(
    name = "test.if_op",
    format = "`(`$0`)` region($0)",
    interfaces = [OneOpd, ZeroResult, OneRegion],
    verifier = "succ"
)]
struct IfOp {}

2. Type Definition

Current Implementation:

#[format_type("`type` `{` $flags `}`")]
#[def_type("test.flags_type")]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct FlagsType {
    flags: u32,
}
impl_verify_succ!(FlagsType);

Proposed Implementation:

#[derive(Type)]
#[definition(
    name = "test.flags_type",
    format = "`type` `{` $flags `}`",
    verifier = "succ"
)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct FlagsType {
    flags: u32,
}

3. Attribute Definition

Current Implementation:

#[format_attribute("`attr` `(` $value `)`")]
#[def_attribute("test.string_attr")]
#[derive(Debug, Clone, PartialEq, Eq)]
struct StringAttr {
    value: String,
}
impl_verify_succ!(StringAttr);

Proposed Implementation:

#[derive(Attribute)]
#[definition(
    name = "test.string_attr",
    format = "`attr` `(` $value `)`",
    verifier = "succ"
)]
#[derive(Debug, Clone, PartialEq, Eq)]
struct StringAttr {
    value: String,
}

Key Benefits

  1. Consistency: Unified #[definition] pattern across all IR entities
  2. Readability: Clear visibility of field roles within struct definitions
  3. Maintainability: Centralized management of all metadata
  4. Extensibility: Simplified addition of new attributes (e.g. docs = "...")

Implementation Notes

The above examples are illustrative only. The proposed solution may not fully correspond with actual macro definitions in the codebase. This proposal presents preliminary concepts requiring further design refinement for discussion purposes.

Regarding actual refactoring approaches, forward compatibility considerations suggest two options:

  1. Complete refactoring through incremental sub-Pull Requests
  2. Transitional implementation via a new pliron-macros module

The specific implementation approach remains open for discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions