macro
Macros are Coil functions that run at compile time and return code. A derive
receives a TypeDecl, an attribute macro an FnDecl or TypeDecl, and a
function-style macro (name!(…)) its arguments as Expr. Each returns
Code, usually built with a quote block. See Macros
and the macros tutorial.
Written in Coil and shipped inside the compiler.
Import what you use (use path::* is not allowed):
use macro::{Code, Ident};
Types#
| Name | Description |
|---|---|
Code | Generated coil source: declarations, an expression, statements or a type. |
Ident | A name as written in source. |
Expr | A function-style macro argument as written: f(1), a + b, "text". |
TypeRef | A type annotation as written (not resolved): Vec<int>, json::Value. |
AttrArg | One key = value (or positional value, with key == "") attribute argument. |
Attr | #[name(args)] on a declaration, field or variant. |
Field | A class field, or a field of a record enum variant. |
Variant | One enum variant. |
TypeDecl | A class or enum declaration handed to a derive or attribute macro. |
Param | One function parameter. |
FnDecl | A function or method handed to an attribute macro. |
Reader | Reads a macro call’s input: length-prefixed fields (<len>:<bytes>, lists as a count then their items) in constructor order, written by the compiler (macros::encode). |
Functions#
| Name | Description |
|---|---|
join | Join fragments with sep (the lowering of $(xs) sep *). |
lit | A string literal: lit("a\"b") splices as "a\"b". |
lit_int | An integer literal. |
raw | Raw source, spliced as is. |
concat | Combine several generated item lists. |
ident | A name, for splicing generated identifiers: ${ident("to_" + n)}. |
decimal | A decimal integer the compiler wrote (- then digits, in range). |
attr_index | Index of the attribute called name, or -1. |
attr_value | #[attr(key = "value")] from an attribute list, or fallback. |