#[format_op]Expand description
Derive Printable and Parsable for Ops
Note: It is suggested to use the pliron_op macro instead of using this macro directly.
The documention here is useful though, because pliron_op’s format field
expands to this macro.
This derive only supports a syntax in which results appear before the opid:
res1, ... = opid ...
The format string specifies what comes after the opid.
-
A named variable
$namespecifies a named attribute of the operation. This cannot be combined with the attr_dict directive. -
An unnamed variable
$ispecifiesoperands[i], except when inside some directives. This cannot be combined with the “operands” directive. -
The “type” directive specifies that a type must be parsed. It takes one argument, which is an unnamed variable
$iwithispecifyingresult[i]. This cannot be combined with the “types” directive. -
The “region” directive specifies that a region must be parsed. It takes one argument, which is an unnamed variable
$iwithispecifyingregion[i]. This cannot be combined with the “regions” directive. -
The “attr” directive can be used to specify attribute on an
Opwhen the attribute’s rust type is fixed at compile time. It takes two mandatory and two optional arguments.- The first operand is a named variable
$namewhich is used as a key into the operation’s attribute dictionary - The second is the concrete rust type of the attribute. This second argument can be a
named variable
$name(withnamebeing in scope) or a literal string denoting the path to a rust type (e.g.`::pliron::builtin::attributes::IntegerAttr`). - Two additional optional arguments can be specified:
- The “label” directive, with one argument, a named variable
$label, which specifies the label to be used while printing / parsing the attribute. - The “delimiters” directive, which takes two literal arguments, specifying the opening and closing delimiters to be used while printing / parsing.
- The “label” directive, with one argument, a named variable
The advantage over specifying an attribute using the attr directive (as against just using a named variable) is that the attribute-id is not a part of the syntax here (because the type is statically known, allowing us to be able to parse it), thus allowing it to be more succinct. This cannot be combined with the attr_dict directive.
- The first operand is a named variable
-
The “succ” directive specifies an operation’s successor. It takes one argument, which is an unnamed variable
$iwithispecifyingsuccessor[i]. -
The “operands” directive specifies all the operands of an operation. It takes one argument which is a directive specifying the separator between operands. The following directives are supported:
NewLine: takes no argument, and specifies a newline to be used as list separator.CharNewline(`c`): takes a single character argument that will be followed by a newline.Char(`c`): takes a single character argument that will be used as separator.CharSpace(`c`): takes a single character argument that will be followed by a space.
-
The “successors” directive specifies all the successors of an operation. It takes one argument which is a directive specifying the separator between successors. The separator directive is same as that for “operands” above. This cannot be combined with the “succ” directive.
-
The “regions” directive specifies all the regions of an operation. It takes one argument which is a directive specifying the separator between regions. The separator directive is same as that for “operands” above. This cannot be combined with the “region” directive.
-
The “attr_dict” directive specifies an AttributeDict. It cannot be combined with any of attr, opt_attr directives or a named variable (
$name). -
The “types” directive specifies all the result types of an operation. It takes one argument which is a directive specifying the separator between result types. The separator directive is same as that for “operands” above. This cannot be combined with the “type” directive.
-
The “opt_attr” directive specifies an optional attribute on an
Op. It takes two or more arguments, which are same as those of the attr directive. This cannot be combined with the attr_dict directive.
Examples:
- Derive for a struct, with no format string (default format):
use pliron::derive::{def_op, format_op, verify_succ};
#[verify_succ]
#[format_op]
#[def_op("test.myop")]
struct MyOp;- An example with a custom format string:
use pliron::derive::{def_op, derive_op_interface_impl, format_op, verify_succ};
use pliron::{op::Op, builtin::op_interfaces::
{OneOpdInterface, OneResultInterface, NOpdsInterface, NResultsInterface}};
#[verify_succ]
#[format_op("$0 `<` $attr `>` `:` type($0)")]
#[def_op("test.one_result_one_operand")]
#[derive_op_interface_impl
(OneOpdInterface, OneResultInterface, NOpdsInterface<1>, NResultsInterface<1>)]
struct OneResultOneOperandOp;More examples can be seen in the tests for this macro in pliron-derive/tests/format_op.rs.