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:

Static methods

staticaddResource(name:String, data:Bytes):Void

Available on neko, macro

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.

Compilation server : when using the compilation server, the resource is bound to the Haxe module which calls the macro, so it will be included again if that module is reused. If this resource concerns several modules, prefix its name with a $ sign, this will bind it to the macro module instead.

staticcontainsDisplayPosition(pos:Position):Bool

Available on neko, macro

Check if current display position is within pos.

staticcurrentPos():Position

Available on neko, macro

Returns the position at which the macro was called.

staticdefineModule(modulePath:String, types:Array<TypeDefinition>, ?imports:Array<ImportExpr>, ?usings:Array<TypePath>):Void

Available on neko, macro

Defines a new module as modulePath with several TypeDefinition types. This is analogous to defining a .hx file.

The individual types can reference each other and any identifier respects the imports and usings as usual, expect that imports are not allowed to have .* wildcards or as s shorthands.

staticdefineType(t:TypeDefinition, ?moduleDependency:String):Void

Available on neko, macro

Defines a new type from TypeDefinition t.

If moduleDependency is given and is not null, it should contain a module path that will be used as a dependency for the newly defined module instead of the current module.

staticdefined(s:String):Bool

Available on neko, macro

Tells if the conditional compilation flag s has been set.

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

See also:

staticdefinedValue(key:String):String

Available on neko, macro

Returns the value defined for the conditional compilation flag key.

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

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

The default value is "1".

See also:

staticerror(msg:String, pos:Position):Dynamic

Available on neko, macro

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

staticfatalError(msg:String, pos:Position):Dynamic

Available on neko, macro

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

staticfilterMessages(predicate:Message ‑> Bool):Void

Available on neko, macro

Filters all current info/warning messages. Filtered out messages will not be displayed by the compiler.

staticfollow(t:Type, ?once:Bool):Type

Available on neko, macro

Follows a type.

See haxe.macro.TypeTools.follow for details.

@:value({ once : false })staticfollowWithAbstracts(t:Type, once:Bool = false):Type

Available on neko, macro

Follows a type, including abstracts' underlying implementation

See haxe.macro.TypeTools.followWithAbstracts for details.

staticgetBuildFields():Array<Field>

Available on neko, macro

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

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

staticgetCallArguments():Null<Array<Expr>>

Available on neko, macro

Returns the call arguments that lead to the invocation of the current @:genericBuild macro, if available.

Returns null if the current macro is not a @:genericBuild macro.

staticgetClassPath():Array<String>

Available on neko, macro

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.

staticgetDefines():Map<String, String>

Available on neko, macro

Returns a map of all conditional compilation flags that have been set.

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

Modifying the returned map has no effect on the compiler.

See also:

staticgetExpectedType():Null<Type>

Available on neko, macro

Returns the type which is expected at the place the macro is called.

This affects usages such as var x:Int = macroCall(), where the expected type will be reported as Int.

Might return null if no specific type is expected or if the calling macro is not an expression-macro.

staticgetLocalClass():Null<Ref<ClassType>>

Available on neko, macro

Returns the current class in which the macro was called.

If no such class exists, null is returned.

staticgetLocalImports():Array<ImportExpr>

Available on neko, macro

Returns an Array of all imports in the context the macro was called.

Modifying the returned array has no effect on the compiler.

staticgetLocalMethod():Null<String>

Available on neko, macro

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

If no such method exists, null is returned.

staticgetLocalModule():String

Available on neko, macro

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

staticgetLocalTVars():Map<String, TVar>

Available on neko, macro

Similar to getLocalVars, but returns elements of type TVar instead of Type.

staticgetLocalType():Null<Type>

Available on neko, macro

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

If no such type exists, null is returned.

staticgetLocalUsing():Array<Ref<ClassType>>

Available on neko, macro

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.

staticgetLocalVars():Map<String, Type>

Deprecated: "Use Context.getLocalTVars() instead"

Available on neko, macro

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.

staticgetMessages():Array<Message>

Available on neko, macro

Gets a list of all current compilation info/warning messages.

staticgetModule(name:String):Array<Type>

Available on neko, macro

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, an exception of type String is thrown.

staticgetPosInfos(p:Position):{min:Int, max:Int, file:String}

Available on neko, macro

Returns the information stored in Position p.

staticgetResources():Map<String, Bytes>

Available on neko, macro

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.

staticgetType(name:String):Type

Available on neko, macro

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, an exception of type String is thrown.

staticgetTypedExpr(t:TypedExpr):Expr

Available on neko, macro

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

This process may lose some information.

staticinfo(msg:String, pos:Position):Void

Available on neko, macro

Displays a compilation info msg at the given Position pos.

staticmakeExpr(v:Dynamic, pos:Position):Expr

Available on neko, macro

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.

staticmakePosition(inf:{min:Int, max:Int, file:String}):Position

Available on neko, macro

Builds a Position from inf.

staticonAfterGenerate(callback:() ‑> Void):Void

Available on neko, macro

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.

Note: the callback is still invoked when generation is disabled with --no-output.

staticonAfterTyping(callback:Array<ModuleType> ‑> Void):Void

Available on neko, macro

Adds a callback function callback which is invoked after the compiler is done typing, but before optimization. The callback receives the types which have been typed.

It is possible to define new types in the callback, in which case it will be called again with the new types as argument.

@:value({ persistent : true })staticonGenerate(callback:Array<Type> ‑> Void, persistent:Bool = true):Void

Available on neko, macro

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.

By default, the callback is made before types are stored in the compilation server, if active. This means that any effect persists for the next compilation. If persistent is set to false, changes to types made by the callback only affect the current compilation. If no compilation server is used, this flag has no effect.

Note: the callback is still invoked when generation is disabled with --no-output.

staticonMacroContextReused(callb:() ‑> Bool):Void

Deprecated:

Available on neko, macro

staticonTypeNotFound(callback:String ‑> TypeDefinition):Void

Available on neko, macro

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.

staticparse(expr:String, pos:Position):Expr

Available on neko, macro

Parses expr as Haxe code, returning the corresponding AST.

String interpolation of single quote strings within expr is not supported.

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

staticparseInlineString(expr:String, pos:Position):Expr

Available on neko, macro

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

staticregisterModuleDependency(modulePath:String, externFile:String):Void

Available on neko, macro

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.

staticregisterModuleReuseCall(modulePath:String, macroCall:String):Void

Deprecated:

Available on neko, macro

staticreportError(msg:String, pos:Position):Void

Available on neko, macro

Displays a compilation error msg at the given Position pos without aborting the current macro call.

staticresolvePath(file:String):String

Available on neko, macro

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.

staticresolveType(t:ComplexType, p:Position):Type

Available on neko, macro

Resolve type t and returns the corresponding Type.

Resolving the type may result in a compiler error which can be caught using try ... catch. Resolution is performed based on the current context in which the macro is called.

staticsignature(v:Dynamic):String

Available on neko, macro

Returns a hashed MD5 signature of value v.

staticstoreExpr(e:Expr):Expr

Available on neko, macro

Types expression e, stores the resulting typed expression internally and returns a syntax-level expression that can be returned from a macro and will be replaced by the stored typed expression.

If e is null or invalid, an exception is thrown.

A call to storeExpr(e) is equivalent to storeTypedExpr(typeExpr(e)) without the overhead of encoding and decoding between regular and macro runtime.

NOTE: the returned value references an internally stored typed expression that is reset between compilations, so care should be taken when storing the expression returned by this method in a static variable and using the compilation server.

staticstoreTypedExpr(t:TypedExpr):Expr

Available on neko, macro

Store typed expression t internally and give a syntax-level expression that can be returned from a macro and will be replaced by the stored typed expression.

If t is null or invalid, an exception is thrown.

NOTE: the returned value references an internally stored typed expression that is reset between compilations, so care should be taken when storing the expression returned by this method in a static variable and using the compilation server.

statictimer(id:String):() ‑> Void

Available on neko, macro

Creates a timer which will be printed in the compilation report if --times compilation argument is set.

Note that a timer may be omitted from the report if the amount of time measured is too small.

This method immediately starts a timer and returns a function to stop it:

var stopTimer = haxe.macro.Context.timer("my heavy task");
runTask();
stopTimer();

statictoComplexType(t:Type):Null<ComplexType>

Available on neko, macro

Returns the ComplexType corresponding to the given Type t.

See haxe.macro.TypeTools.toComplexType for details.

statictypeExpr(e:Expr):TypedExpr

Available on neko, macro

Types expression e and returns the corresponding TypedExpr.

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

statictypeof(e:Expr):Type

Available on neko, macro

Types expression e and returns its type.

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

staticunify(t1:Type, t2:Type):Bool

Available on neko, macro

Tries to unify t1 and t2 and returns true if successful.

staticwarning(msg:String, pos:Position):Void

Available on neko, macro

Displays a compilation warning msg at the given Position pos.