Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/irdl/test_attr_declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,25 @@ def test_optional_print_correctness(attr: Attribute, expected: str):
assert print_attr(attr) == expected


# ============================================================================
# Integration tests — qualified directive
# ============================================================================


def test_qualified_roundtrip():
"""qualified($x) should parse and print identically to $x in xdsl."""

@irdl_attr_definition
class QualifiedType(ParametrizedAttribute, TypeAttribute):
name = "test_af.qualified"
value: IntegerType = param_def()
assembly_format = "qualified($value)"

ctx = Context(allow_unregistered=True)
ctx.load_attr_or_type(QualifiedType)
check_roundtrip(QualifiedType, "i32", ctx)


# ============================================================================
# Integration tests — printing correctness
# ============================================================================
Expand Down
9 changes: 9 additions & 0 deletions xdsl/irdl/declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,15 @@ def set_empty(self, state: AttrParsingState) -> None:
state.parameters[self.index] = NoneAttr()


@dataclass(frozen=True)
class QualifiedParameterVariable(ParameterVariable):
"""Wraps a ParameterVariable to print with full dialect qualification.

In xdsl, print_attribute() always qualifies, so this is currently
identical to ParameterVariable. Exists for MLIR format compatibility.
"""


@dataclass(frozen=True)
class AttrOptionalGroupDirective(AttrFormatDirective):
"""An optional group in attribute assembly format."""
Expand Down
8 changes: 8 additions & 0 deletions xdsl/irdl/declarative_assembly_format_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
OptionalUnitAttrVariable,
ParameterVariable,
PunctuationDirective,
QualifiedParameterVariable,
RegionDirective,
RegionVariable,
ResultsDirective,
Expand Down Expand Up @@ -945,6 +946,8 @@ def parse_format_directive(self) -> AttrFormatDirective:
return self.parse_optional_group()
if self._current_token.text == "$":
return self.parse_variable()
if self.parse_optional_keyword("qualified"):
return self.parse_qualified_directive()
self.raise_error(f"unexpected token '{self._current_token.text}'")

def parse_variable(self, inside_ref: bool = False) -> ParameterVariable:
Expand Down Expand Up @@ -1053,3 +1056,8 @@ def parse_optional_group(self) -> AttrFormatDirective:
then_elements[first_non_whitespace_index + 1 :],
else_elements,
)

def parse_qualified_directive(self) -> QualifiedParameterVariable:
with self.in_parens():
pv = self.parse_variable()
return QualifiedParameterVariable(pv.name, pv.index, pv.is_optional)
Loading