Haxe Macro Type Decoder
A collection of helper functions and utilities for working with Haxe types and macros. This library simplifies decoding types, generating code, and handling macro-related tasks.
Features
- Decode Haxe types to structured data for macro processing.
- Generate unique names for identifiers in macros.
- Create constant-returning functions in macro-generated classes.
- Convert types to readable strings at compile time.
- Easily define new types and fields in macros.
- Save arbitrary values as macro expressions for code generation.
- Generate Haxe files from macros (see
generate/GenerateHaxeFile.hx). - Advanced type name and position helpers for macros.
Installation
haxelib install macro-type-decoder
Usage
1. Decoding Types in Macros
Decode a Haxe type to a structured representation for easier macro processing.
#if macro
using hx.macro.MacroTypeDecorderHelper;
var someType: Type = ...;
var decoded: hx.macro.MacroTypeDecorderHelper.DecodedType = someType.decode();
#end
2. Creating Unique Names in Macros
Generate unique identifier names to avoid collisions in macro-generated code.
using hx.macro.MacroTypeHelper;
var builder = MacroTypeHelper.createNameBuilder("prefix");
builder.next().get(); // "prefix_1"
builder.next().get(); // "prefix_2"
3. Defining Constant Value Functions in Macros
Easily define functions that return constant values in macro-generated types.
#if macro
using hx.macro.MacroDefineHelper;
Context.defineType({
pack: ["my", "package"],
name: "MyGeneratedClass",
pos: MacroDefineHelper.getPosition(),
params: [],
kind: TDClass(),
fields: [
// Public constructor
"new".toPublicFunction([], macro {}, macro: Void),
// Public function returning a constant array
"getSomeList".toPublicConstValueFunction([1, 2, 3], macro : Array<Int>),
// Public function returning a constant map
"getSomeMap".toPublicConstValueFunction(["a" => 1, "b" => 2], macro : Map<String, Int>),
// Private static function
"helper".toPrivateStaticFunction([], MacroDefineHelper.toExpr(EConst(CInt("42"))), macro:Int),
// Variable field
"myVar".toVariable(macro:Int, true, MacroDefineHelper.toExpr(EConst(CInt("7")))),
// Using toExprBlock to create a function body
"sum".toPublicFunction(
[],
[
EVar("a", macro:Int, EConst(CInt("3")).toExpr()).toExpr(),
EReturn(EConst(CInt("5")).toExpr()).toExpr()
].toExprBlock(),
macro:Int
)
],
});
#end
There are many more useful helpers in MacroDefineHelper. See the source for details.
4. Type Information at Runtime
Get string representations and decoded data for types at runtime.
var info = new hx.macro.TypeToStringPrinter<Map<Int, String>>();
info.getString(); // "Map<Int,String>"
info.getDecodedData(); // { nullable: false, decoded: TypeMap({decoded:TypeInt}, {decoded:TypeString}) }
Other Utility Functions
This library provides additional helpers for macro programming:
Save Arbitrary Values as Macro Expressions
Use MacroValueSaverExpr to generate Haxe expressions that reconstruct arbitrary values at compile time:
#if macro
import hx.macro.MacroValueSaverExpr;
import haxe.macro.Expr;
var exprs: Array<Expr> = MacroValueSaverExpr.saveToVariable([1, 2, 3], "myArr");
#end
Generate Haxe Files from Macros
Use generate.GenerateHaxeFile to write generated classes to disk:
#if macro
import hx.macro.generate.GenerateHaxeFile;
var generator = new GenerateHaxeFile("output", ["my", "package"]);
generator.writeClass("MyGeneratedClass.hx", () -> myTypeDefinition);
#end
Macro Position Helpers
MacroPositionHelper provides utilities to convert PosInfos to macro positions, useful for accurate error reporting and code generation.
Examples
See the test directory for comprehensive usage examples:
- CanCreateUniqueNames.hx: Shows unique name generation.
- CanDecodeATypeToString.hx: Converts types to readable strings.
- CanDecodeATypeToDecodedType.hx: Decodes types to structured data.
- CanReturnAnyValueFromBuildFunction.hx: Demonstrates macro-generated functions returning various values.
For more details, see the source code