class haxe.macro.Context

Available on neko

Context provides an API for macro programming.

It contains common functions that interact with the macro interpreter to
query or set information. Other API functions are available in the tools
classes:

- `haxe.macro.ComplexTypeTools`
- `haxe.macro.ExprTools`
- `haxe.macro.TypeTools`

Class Fields

static function addResource(name:String, data:Bytes):Void

Makes resource data available as name.

The resource is then available using the haxe.macro.Resource API.

If a previous resource was bound to name, it is overwritten.

static function currentPos():Position

Returns the position at which the macro was called.

static function defineModule(modulePath:String, types:Array<TypeDefinition>):Void

Defines a new module with several TypeDefinition types.

static function defineType(t:TypeDefinition):Void

Defines a new type from TypeDefinition t.

static function defined(s:String):Bool

Tells if compiler directive s has been set.

Compiler directives are set using the -D command line parameter, or by calling haxe.macro.Compiler.define.

static function definedValue(key:String):String

Returns the value defined for compiler directive key.

If no value is defined for key, null is returned.

Compiler directive values are set using the -D key=value command line parameter, or by calling haxe.macro.Compiler.define.

The default value is "1".

static function error(msg:String, pos:Position):Dynamic

Displays a compilation error msg at the given Position pos and aborts the current macro call.

static function fatalError(msg:String, pos:Position):Dynamic

Displays a compilation error msg at the given Position pos and aborts the compilation.

static function follow(t:Type, ?once:Bool):Type

Follows a type.

See haxe.macro.TypeTools.follow for details.

static function getBuildFields():Array<Field>

Returns an Array of fields of the class which is to be built.

This is only defined for @:build/@:autoBuild macros.

static function getClassPath():Array<String>

Returns an Array of current class paths in the order of their declaration.

Modifying the returned array has no effect on the compiler. Class paths can be added using haxe.macro.Compiler.addClassPath.

static function getLocalClass():Null<Ref<ClassType>>

Returns the current class in which the macro was called.

If no such class exists, null is returned.

static function getLocalMethod():Null<String>

Returns the name of the method from which the macro was called.

If no such method exists, null is returned.

static function getLocalModule():String

Returns the current module path in/on which the macro was called.

static function getLocalType():Null<Type>

Returns the current type in/on which the macro was called.

If no such type exists, null is returned.

static function getLocalUsing():Array<Ref<ClassType>>

Returns an Array of classes which are available for using usage in the context the macro was called.

Modifying the returned array has no effect on the compiler.

static function getLocalVars():StringMap<Type>

Returns a map of local variables accessible in the context the macro was called.

The keys of the returned map are the variable names, the values are their types.

Modifying the returned map has no effect on the compiler.

static function getModule(name:String):Array<Type>

Resolves a module identified by name and returns an Array of all its contained types.

The resolution follows the usual class path rules where the last declared class path has priority.

If no module can be found, null is returned.

static function getPosInfos(p:Position):{min:Int, max:Int, file:String}

Returns the information stored in Position p.

static function getResources():StringMap<Bytes>

Returns a map of all registered resources for this compilation unit.

Modifying the returned map has no effect on the compilation, use haxe.macro.Context.addResource to add new resources to the compilation unit.

static function getType(name:String):Type

Resolves a type identified by name.

The resolution follows the usual class path rules where the last declared class path has priority.

If no type can be found, null is returned.

static function getTypedExpr(t:TypedExpr):Expr

Returns a syntax-level expression corresponding to typed expression t.

This process may lose some information.

static function makeExpr(v:Dynamic, pos:Position):Expr

Builds an expression from v.

This method generates AST nodes depending on the macro-runtime value of v. As such, only basic types and enums are supported and the behavior for other types is undefined.

The provided Position pos is used for all generated inner AST nodes.

static function makePosition(inf:{min:Int, max:Int, file:String}):Position

Builds a Position from inf.

static function onAfterGenerate(callback:Void):Void

Adds a callback function callback which is invoked after the compiler generation phase.

Compilation has completed at this point and cannot be influenced anymore. However, contextual information is still available.

static function onGenerate(callback:Array<Type> ->Void):Void

Adds a callback function callback which is invoked after the compiler's typing phase, just before its generation phase.

The callback receives an Array containing all types which are about to be generated. Modifications are limited to metadata, it is mainly intended to obtain information.

static function onMacroContextReused(callb:Bool):Void

Register a callback function that will be called everytime the macro context cached is reused with a new compilation. This enable to reset some static vars since the code might have been changed. If the callback returns false, the macro context is discarded and another one is created.

static function onTypeNotFound(callback:String ->TypeDefinition):Void

Adds a callback function callback which is invoked when a type name cannot be resolved.

The callback may return a type definition, which is then used for the expected type. If it returns null, the type is considered to still not exist.

static function parse(expr:String, pos:Position):Expr

Parses expr as haxe code, returning the corresponding AST.

The provided Position pos is used for all generated inner AST nodes.

static function parseInlineString(expr:String, pos:Position):Expr

Similar to parse, but error positions are reported within the provided String expr.

static function registerModuleDependency(modulePath:String, externFile:String):Void

Manually adds a dependency between module modulePath and an external file externFile.

This affects the compilation cache, causing the module to be typed if externFile has changed.

Has no effect if the compilation cache is not used.

static function registerModuleReuseCall(modulePath:String, macroCall:String):Void

Add a macro call to perform in case the module is reused by the compilation cache.

static function resolvePath(file:String):String

Resolves a file name file based on the current class paths.

The resolution follows the usual class path rules where the last declared class path has priority.

If a class path was declared relative, this method returns the relative file path. Otherwise it returns the absolute file path.

static function signature(v:Dynamic):String

Returns a hashed MD5 signature of value v.

static function toComplexType(t:Type):Null<ComplexType>

Returns the ComplexType corresponding to the given Type t.

See haxe.macro.TypeTools.toComplexType for details.

static function typeExpr(e:Expr):TypedExpr

Types expression e and returns the corresponding TypedExpr.

Typing the expression may result in an compiler error which can be caught using try ... catch.

static function typeof(e:Expr):Type

Types expression e and returns its type.

Typing the expression may result in an compiler error which can be caught using try ... catch.

static function unify(t1:Type, t2:Type):Bool

Returns true if t1 and t2 unify, false otherwise.

static function warning(msg:String, pos:Position):Void

Displays a compilation warning msg at the given Position pos.