Skip to content

Commit c62e9eb

Browse files
committed
builtin-macros: Add more documentation for defining builtins
1 parent 6e64e66 commit c62e9eb

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

gcc/rust/expand/rust-macro-builtins.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@
2222
#include "rust-ast.h"
2323
#include "rust-location.h"
2424

25+
/**
26+
* This class provides a list of builtin macros implemented by the compiler.
27+
* The functions defined are called "builtin transcribers" in that they replace
28+
* the transcribing part of a macro definition.
29+
*
30+
* Like regular macro transcribers, they are responsible for building and
31+
* returning an AST fragment: basically a vector of AST nodes put together.
32+
*
33+
* Unlike regular declarative macros where each match arm has its own associated
34+
* transcriber, builtin transcribers are responsible for handling all match arms
35+
* of the macro. This means that you should take extra care when implementing a
36+
* builtin containing multiple match arms: You will probably need to do some
37+
* lookahead in order to determine which match arm the user intended to use.
38+
*
39+
* An example of this is the `assert!()` macro:
40+
*
41+
* ```
42+
* macro_rules! assert {
43+
* ($cond:expr $(,)?) => {{ ... }};
44+
* ($cond : expr, $ ($arg : tt) +) = > {{ ... }};
45+
* }
46+
* ```
47+
*
48+
* If more tokens exist beyond the optional comma, they need to be handled as
49+
* a token-tree for a custom panic message.
50+
*
51+
* These builtin macros with empty transcribers are defined in the standard
52+
* library. They are marked with a special attribute, `#[rustc_builtin_macro]`.
53+
* When this attribute is present on a macro definition, the compiler should
54+
* look for an associated transcriber in the mappings. Meaning that you must
55+
* remember to insert your transcriber in the `builtin_macros` map of the
56+
*`Mappings`.
57+
*
58+
* This map is built as a static variable in the `insert_macro_def()` method
59+
* of the `Mappings` class.
60+
*/
61+
2562
namespace Rust {
2663
class MacroBuiltin
2764
{

0 commit comments

Comments
 (0)