10.10. Type macro and template structure support
The TYPEMACRO_BOOST module provides infrastructure for defining type macros — custom compile-time type transformations. Type macros allow introducing new type syntax that expands into standard daslang types during compilation.
All functions and symbols are in “typemacro_boost” module, use require to get access to it.
require daslib/typemacro_boost
10.10.1. Structures
- TypeMacroTemplateArgument
Holds a type macro template argument with its name and inferred type.
- Fields:
name : string - Name of the template argument.
argument_type : TypeDeclPtr - Declared argument type from the template signature.
inferred_type : TypeDeclPtr - Inferred concrete type after template instantiation.
10.10.2. Function annotations
- typemacro_function
This macro converts function into a type macro.
- typemacro_template_function
This one converts function into a type macro that uses template arguments. For example [typemacro_template(TFlatHashTable)] def makeFlatHashTable ( macroArgument, passArgument : TypeDeclPtr; KeyType, ValueType : TypeDeclPtr; hashFunctionName : string) : TypeDeclPtr { … } We generate the body that handles template argument inference and instantiation.
10.10.3. Structure macros
- template_tuple
Like [template_structure], but produces a typemacro that returns a named tuple instead of cloning a structure. Result is structural — no module home, no per-consumer cloning. Use for sum-type / discriminated- pair shapes (Option, Result) where structural identity across transitive require ... public is the desired behavior (issue #2598).
- template_structure
This macro creates typemacro function and associates it with the structure.
- typemacro_documentation
Structure annotation that stores type macro documentation metadata.
- typemacro_template
Structure annotation that marks a struct as a type macro template instance.
- tsi_reified
Marker the reifier appends to a finished instance: a template CHAIN pipes one reifier copy per ancestor, and the surplus copies no-op on sight of this.
- template_struct_instance
Reifies a struct template / class template parent into the annotated instance at parse time: instance typedef``s bind the template's type parameters, ``override inits bind its @template_constant fields (erased from the result), @template_gate fields exist only in stamps where the named bool @template_constant is true (@template_gate = AXIS, or the string form "!AXIS" to invert; a gated-off field is erased as if never written, and the argument stays inert on kept fields — reference a gated field only from static_if arms on its axis: a dead static_if arm may name it, a dead ternary arm may not, since infer sees both ternary arms), @template_call fields rebind free-function callees by name (override sdot = @@ssdot — bare-spelled calls AND @@ addresses; erased too, while _::sdot / __::sdot keep their normal resolution rules — the pinned escape to the real function), template methods are cloned onto the instance (instance-authored methods win), and the template parent is cut — every later structure annotation sees a finished concrete struct or class.
A template may itself derive from a template: the instance reifies the whole CHAIN in one pass - constants, typedefs, gates and calls bind at any level from the instance’s materialized fields, methods stamp nearest-ancestor-first (an instance-authored method beats every level), and the parent cut lands past the last template ancestor.
Put [|> template_struct_instance] on the template itself so every instance inherits the reifier ahead of its own annotations; under a chain the instance inherits one copy per ancestor, and the surplus copies no-op against the tsi_reified marker the first application appends. Names the instance leaves unbound are left to the compiler: module types resolve on their own, a later macro (e.g. a [dirty_infer_macro]) may still supply a typedef, and a genuinely missing one surfaces as an undefined-type error in the stamped code.
10.10.4. Enum helpers
- int64_to_enum(_enu: auto(ET); value: int64 ): ET
Converts an int64 value to the specified enum type via reinterpret cast.
- Arguments:
_enu : auto(ET)
value : int64
10.10.5. Template structure instantiation
- is_typemacro_template_instance(passArgument: TypeDeclPtr; templateType: TypeDeclPtr; extra: array<tuple<string;string>> = array<tuple<string;string>>() ): bool
template instance is determined by having parent == template.parent
- Arguments:
passArgument : TypeDeclPtr
templateType : TypeDeclPtr
extra : array<tuple<string;string>>
- make_typemacro_template_instance(instance_type: Structure?; template_type: Structure?; ex: array<tuple<string;string>> = array<tuple<string;string>>() )
Annotates a structure as a typemacro template instance of the given template type.
- template_structure_name(base: Structure?; arguments: array<TypeMacroTemplateArgument>; extra: array<tuple<string;string>> = array<tuple<string;string>>() ): string
Builds a mangled template structure name from its base name and argument types.
- Arguments:
base : Structure?
arguments : array< TypeMacroTemplateArgument>
extra : array<tuple<string;string>>
10.10.6. Type inference helpers
- add_structure_aliases(structType: Structure?; args: array<TypeMacroTemplateArgument> )
Adds all template argument type aliases to a structure.
- Arguments:
structType : Structure?
args : array< TypeMacroTemplateArgument>
- infer_struct_aliases(structType: Structure?; args: array<TypeMacroTemplateArgument> ): bool
Infers structure alias types for all template arguments from a structure definition.
- Arguments:
structType : Structure?
args : array< TypeMacroTemplateArgument>
- infer_template_types(passArgument: TypeDeclPtr; args: array<TypeMacroTemplateArgument> ): TypeDeclPtr
Infers and validates template argument types against a pass argument, returning the resolved type.
- Arguments:
passArgument : TypeDeclPtr
args : array< TypeMacroTemplateArgument>
- verify_arguments(args: array<TypeMacroTemplateArgument> ): bool
Verifies that all template arguments have been fully inferred (no remaining auto or alias types).
- Arguments:
args : array< TypeMacroTemplateArgument>
10.10.7. String constant access
- get_string_const(expr: ExpressionPtr ): string
Extracts a string constant value or function address name from an expression.
- Arguments:
expr : ExpressionPtr
10.10.8. Work tracking
- is_custom_work_done(structType: Structure? ): bool
Returns true if custom work has already been performed on the template structure.
- Arguments:
structType : Structure?
- mark_custom_work_done(structType: Structure? )
Marks the template structure’s custom work as complete in its annotation.
- Arguments:
structType : Structure?
10.10.9. Type macro arguments
10.10.9.1. typemacro_argument
- typemacro_argument(dimExpr: auto; index: int; constType: ExprConstString; defaultValue: auto(ValueT) ): ValueT
Extracts a string constant or function address argument at the given index from a type macro’s dimension expressions.
- Arguments:
dimExpr : auto
index : int
constType : ExprConstString
defaultValue : auto(ValueT)
- typemacro_argument(dimExpr: auto; index: int; constType: auto(ExprConstType); defaultValue: auto(ValueT) ): ValueT