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

feat: implement procedure annotation syntax #1510

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

bitwalker
Copy link
Contributor

@bitwalker bitwalker commented Sep 23, 2024

This commit introduces the concept of attributes/annotations to Miden Assembly procedures. These can be used to represent interesting/useful bits of metadata associated with specific procedures in three different forms:

  • Marker attributes, e.g. #[inline], a name with no associated data
  • List attributes, e.g. #[inline(always)], i.e. a parameterized marker; multiple values can be provided as comma-delimited values.
  • Key-value attributes, e.g. #[key = <value>], where <value> can be a "meta expression" of three possible types: identifier, string, or integer (in either decimal or hexadecimal format).

Attributes will provide the foundation for upcoming changes that will rely on being able to attach metadata to procedures. For now, attributes may only be attached to procedure definitions, not re-exports or any other syntactic construct.

NOTE: This does not yet act on any attributes, nor store them anywhere when assembling to MAST. For now, they are simply parsed, made available in the AST, and ignored. Future PRs will introduce these as needed.

Closes #1434

@bitwalker bitwalker added enhancement New feature or request assembly Related to Miden assembly labels Sep 23, 2024
@bitwalker bitwalker self-assigned this Sep 23, 2024
This commit introduces the concept of attributes/annotations to Miden
Assembly procedures. These can be used to represent interesting/useful
bits of metadata associated with specific procedures in three different
forms:

* Marker attributes, e.g. `#[inline]`, a name with no associated data
* List attributes, e.g. `#[inline(always)]`, i.e. a parameterized
  marker; multiple values can be provided as comma-delimited values.
* Key-value attributes, e.g. `#[key = <value>]`, where `<value>` can be
  a "meta expression" of three possible types: identifier, string, or
  integer (in either decimal or hexadecimal format).

Attributes will provide the foundation for upcoming changes that will
rely on being able to attach metadata to procedures. For now, attributes
may _only_ be attached to procedure definitions, not re-exports or any
other syntactic construct.

NOTE: This does not yet act on any attributes, nor store them anywhere
when assembling to MAST. For now, they are simply parsed, made available
in the AST, and ignored. Future PRs will introduce these as needed.

Closes #1434
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good! I left one question inline.

Also, I'm trying to think through the syntax choice #[...]. On the one hand, we use # for comments and so this syntax overloads comments a bit. But on the other hand, maybe that's not a bad thing.

An alternative could have been something like @[...]. It would make the attributes stand out a bit more syntactically - so, curious why you decided to go with #[...]. But to be honest, either way is fine.

For now, attributes may only be attached to procedure definitions, not re-exports or any other syntactic construct.

This is fine for the initial PR - but we'll probably need to lift this restriction fairly soon. The reason is that when defining account code, we'd want to attach attributes to re-exports. For example, we may define a basic wallet as something like this:

export.::miden::contracts::wallets::basic::receive_asset
export.::miden::contracts::wallets::basic::create_note
export.::miden::contracts::wallets::basic::move_asset_to_note

#[storage(offset = 0, size = 1)
export.::miden::contracts::auth::basic::auth_tx_rpo_falcon512

While, for example, a basic faucet could be defined as:

export.::miden::contracts::faucets::basic_fungible::distribute
export.::miden::contracts::faucets::basic_fungible::burn

#[storage(offset = 1, size = 1)
export.::miden::contracts::auth::basic::auth_tx_rpo_falcon512

So, in both cases we re-export miden::contracts::auth::basic::auth_tx_rpo_falcon512 procedure, but we attach different attributes to it based on the context.

Comment on lines +647 to +652
# Key value attributes of various kinds
#[decimal = 1]
#[hex = 0xdeadbeef]
#[id = foo]
#[string = "not a valid quoted identifier"]
proc.baz.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also test something like:

#[storage(offset = 1, size = 2)]

Comment on lines +134 to +141
pub enum Attribute {
/// A named behavior, trait or action; e.g. `#[inline]`
Marker(Ident),
/// A parameterized behavior, trait or action; e.g. `#[derive(Foo)]`
List(MetaList),
/// A named property; e.g. `#[key = "value"]`, `#[key = 1]`, `#[key = 0x1]`, or `#[key = foo]`
KeyValue(MetaKeyValue),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems something like #[storage(offset = 1, size = 2)] would not be supported by this structure, right? We can of course emulate it as:

#[storage_offset = 1]
#[storage_size = 2]

But I wonder if we should try to support something like the original in the MetaList variant.

Copy link
Contributor

@plafer plafer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@Fumuran Fumuran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assembly Related to Miden assembly enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants