Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

lib/meta.x

The compiler surface a meta function calls.

Primary API

FunctionSummary
x2c_block_makeReturns a block containing items in order.
x2c_decl_makeDeclares name with type and an optional initializer.
x2c_expr_callReturns the expression calling callee with arguments, a List of expressions.
x2c_expr_castReturns expression cast to type, which is a declared type rather than syntax.
x2c_expr_compositeReturns the comma-separated composite initializer holding items, a List of expressions.
x2c_expr_fieldReturns the expression receiver.name.
x2c_expr_identReturns an expression reading the identifier name, which is the syntax x2c_ident returned or a binding the compiler resolved.
x2c_expr_indexReturns the expression base[subscript].
x2c_function_bodyReturns the statements in the body of function.
x2c_literal_intReturns an int expression holding value.
x2c_literal_stringReturns a String expression holding value.
x2c_literal_symbolReturns a Symbol expression holding value.
x2c_param_makeReturns a parameter named name with type.
x2c_parameters_argumentsReturns the argument expressions that forward a parameter list, which is a params form or the parameters themselves.
x2c_stmnt_makeReturns an expression statement.
x2c_stmnt_returnReturns a return statement carrying expression.
x2c_type_membersReturns enum members as (name value) rows in declaration order.

Functions

x2c_block_make

meta List x2c_block_make(List items)

Returns a block containing items in order.

Source: lib/meta.x:110

x2c_decl_make

meta List x2c_decl_make(List type, Var name, List initializer)

Declares name with type and an optional initializer.

Source: lib/meta.x:113

x2c_expr_call

meta List x2c_expr_call(List callee, List arguments)

Returns the expression calling callee with arguments, a List of expressions.

Source: lib/meta.x:84

x2c_expr_cast

meta List x2c_expr_cast(List type, List expression)

Returns expression cast to type, which is a declared type rather than syntax. A generator needs it where the value it holds and the parameter it reaches differ in width or sign.

Source: lib/meta.x:95

x2c_expr_composite

meta List x2c_expr_composite(List items)

Returns the comma-separated composite initializer holding items, a List of expressions.

Source: lib/meta.x:89

x2c_expr_field

meta List x2c_expr_field(List receiver, String name)

Returns the expression receiver.name.

Source: lib/meta.x:77

x2c_expr_ident

meta List x2c_expr_ident(List name)

Returns an expression reading the identifier name, which is the syntax x2c_ident returned or a binding the compiler resolved.

Source: lib/meta.x:70

x2c_expr_index

meta List x2c_expr_index(List base, List subscript)

Returns the expression base[subscript].

Source: lib/meta.x:73

x2c_function_body

meta List x2c_function_body(List function)

Returns the statements in the body of function.

Source: lib/meta.x:165

x2c_literal_int

meta List x2c_literal_int(int value)

Returns an int expression holding value.

Source: lib/meta.x:55

x2c_literal_string

meta List x2c_literal_string(String value)

Returns a String expression holding value.

Source: lib/meta.x:50

x2c_literal_symbol

meta List x2c_literal_symbol(Symbol value)

Returns a Symbol expression holding value.

Source: lib/meta.x:59

x2c_param_make

meta List x2c_param_make(List type, Var name)

Returns a parameter named name with type.

Source: lib/meta.x:121

x2c_parameters_arguments

meta List x2c_parameters_arguments(List value)

Returns the argument expressions that forward a parameter list, which is a params form or the parameters themselves. A (void) parameter list answers nothing.

Source: lib/meta.x:173

x2c_stmnt_make

meta List x2c_stmnt_make(List expression)

Returns an expression statement.

Source: lib/meta.x:104

x2c_stmnt_return

meta List x2c_stmnt_return(List expression)

Returns a return statement carrying expression.

Source: lib/meta.x:107

x2c_type_members

meta List x2c_type_members(List type)

Returns enum members as (name value) rows in declaration order. An implicit value is nil; a literal value retains its spelling.

Source: lib/meta.x:231

Design notes

A meta function runs inside the compiler, so it can ask the compiler questions and build syntax for it to bind. Those operations were reachable only from compile-time Lisp, under names like x2c.ident and x2c.type.fields, which made Lisp the authoring language for any macro whose implementation needed them. The declarations below name the same operations from x2c, so a macro’s implementation is x2c. Each x2c name is its Lisp name with _ for ., and the lowering maps one to the other.

Syntax builders have meta bodies shared by compile time and runtime. A declaration with no body names a compiler operation. A meta function that reaches one, directly or through another meta function, is therefore compile-time only, the compiler derives that and emits no runtime form for it, and a run-time call to it is diagnosed where it is written.

This module is not part of the implicit prelude. Include it where the meta functions are parsed: a .xmacro borrows the consuming unit’s symbol table, so the unit that imports it includes this file.

The declarations below are the signatures. Each operation’s semantics are those of the compile-time Lisp operation of the same name, specified under “Compile-time Lisp and imports” in the language reference, which also gives the naming rule and the two answers whose shape differs. See plans/meta-functions.md.

Tests and examples

make verify (meta-comptime-only-call, meta-builder-compiler-only, and meta-literal-arguments compiler fixtures).