New/better macro pragmas, mark some as experimental (#19406)

* New/better macro pragmas, make some experimental

fix #15920, close #18212, close #14781, close #6696,
close https://github.com/nim-lang/RFCs/issues/220

Variable macro pragmas have been changed to
only take a unary section node.
They can now also be applied in sections with multiple variables,
as well as `const` sections. They also accept arguments.

Templates now support macro pragmas, mirroring other routine types.

Type and variable macro pragmas have been made experimental.
Symbols without parentheses instatiating nullary macros or templates
has also been documented in the experimental manual.

A check for a redefinition error based on the left hand side of variable
definitions when using variable macro pragmas was disabled.
This nerfs `byaddr` specifically, however this has been documented as
a consequence of the experimental features `byaddr` uses.

Given how simple these changes are I'm worried if I'm missing something.

* accomodate compiler boot

* allow weird pragmas

* add test for #10994

* remove some control flow, try remove some logic
This commit is contained in:
metagn 2022-01-20 22:57:50 +03:00 • committed by GitHub
commit 2bd1aa186e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 435 additions and 205 deletions

View file

@ -7781,9 +7781,10 @@ More examples with custom pragmas:
Macro pragmas
-------------
All macros and templates can also be used as pragmas. They can be attached
to routines (procs, iterators, etc), type names, or type expressions. The
compiler will perform the following simple syntactic transformations:
Macros and templates can sometimes be called with the pragma syntax. Cases
where this is possible include when attached to routine (procs, iterators, etc)
declarations or routine type expressions. The compiler will perform the
following simple syntactic transformations:
.. code-block:: nim
template command(name: string, def: untyped) = discard
@ -7810,20 +7811,15 @@ This is translated to:
------
.. code-block:: nim
type
MyObject {.schema: "schema.protobuf".} = object
This is translated to a call to the `schema` macro with a `nnkTypeDef`
AST node capturing both the left-hand side and right-hand side of the
definition. The macro can return a potentially modified `nnkTypeDef` tree
or multiple `nnkTypeDef` trees contained in a `nnkTypeSection` node
which will replace the original row in the type section.
When multiple macro pragmas are applied to the same definition, the
compiler will apply them consequently from left to right. Each macro
will receive as input the output of the previous one.
When multiple macro pragmas are applied to the same definition, the first one
from left to right will be evaluated. This macro can then choose to keep
the remaining macro pragmas in its output, and those will be evaluated in
the same way.
There are a few more applications of macro pragmas, such as in type,
variable and constant declarations, but this behavior is considered to be
experimental and is documented in the `experimental manual
<manual_experimental.html#extended-macro-pragmas>` instead.
Foreign function interface