15.4 User-defined attributes and code generation
attr Audit { level: Level = Level.Info }
comptime fn Audit.apply(d: Decl, a: Audit) -> Decl | CompileError {
d.wrapBody(body => quote { log.at($a.level, "call {$d.name}"); $body })
}attr Name { fields }declares an attribute;comptime fn Name.apply(d: Decl, a: Name)is the handler called for every declaration with this attribute.std.comptimeprovides the AST (Decl,Expr,Type),quote { … }with$xsubstitutions and theCompileErrordiagnostic.@derive(I)is implemented the same way. The generated code is checked as ordinary code.
Implementation-defined. Code generation by attributes (D-8052; D-7530). In this implementation
attr Name { fields }declares a data type and@Name(…)works as aMiddleware(§10.2); the handlercomptime fn Name.apply(d: Decl, a: Name)that rewrites declarations is reserved and not implemented (an optional feature).
See also
Source: docs/spec/oxl-standard.en.md