tvm
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
tvm::script::printer::DocTranslatorObj Class Reference

Eager translation context shared by registered hooks. Hooks construct Docs, use explicit lexical scopes, and attach origins when emitting. More...

#include <doc_translator.h>

Inheritance diagram for tvm::script::printer::DocTranslatorObj:
Collaboration diagram for tvm::script::printer::DocTranslatorObj:

Public Member Functions

ffi::Optional< ExprDoc > Translate (ffi::AnyView value, const ffi::Optional< Var > bind_var=std::nullopt)
 Translate a value with an optional pre-mapped binding variable.
 
void Emit (const Doc &doc, ffi::Optional< ffi::ObjectRef > origin=std::nullopt)
 Append a completed Doc and record its optional origin.
 
IdDoc VarGetOrAllocId (const Var &var, bool explicit_def)
 Reuse or allocate a variable's stable identifier.
 
IdDoc AllocId (const ffi::String &hint)
 Reserve a fresh identifier without mapping a variable.
 
ffi::Dict< Var, IdDoc > GetImplicitDefs ()
 Get the current variable scope's pending definitions.
 
template<typename T >
T GetExtraConfig (const ffi::String &name, T fallback) const
 Read a typed extension option.
 
template<typename T >
T GetOrCreateExtraState (const ffi::String &name)
 Get or initialize typed extension state.
 
void SetExtraState (const ffi::String &name, std::optional< ffi::Any > value)
 Replace or remove extension state.
 
void BeginVarScope ()
 Begin a lexical variable scope independent of Doc collection.
 
void EndVarScope ()
 End the latest naming region, retaining the root region. Explicit local bindings expire. Unresolved implicit references and their identifiers remain pending in the enclosing region, preserving captures shared by sibling functions.
 
void BeginDocScope ()
 Push an empty Doc collection.
 
ffi::List< Doc > EndDocScope ()
 Pop the latest Doc collection.
 
ffi::List< Doc > CurrentScopeDocs () const
 Get the current Doc collection without popping it.
 
template<typename Callback >
ffi::List< Doc > WithDocScope (Callback &&callback)
 Run a callback in a balanced Doc collection scope.
 
ffi::Dict< Doc, ffi::ObjectRef > GetDocOrigins () const
 Get the owned Doc-to-original-object associations.
 
void RecordOrigin (const Doc &doc, const ffi::ObjectRef &origin)
 Set a Doc's original IR object, replacing any previous association.
 
ffi::Optional< ExprDoc > DefaultTranslate (ffi::AnyView value, const ffi::Object *destination=nullptr)
 Dispatch directly to the registered native or packed hook.
 
 TVM_FFI_DECLARE_OBJECT_INFO ("script.printer.DocTranslator", DocTranslatorObj, ffi::Object)
 

Static Public Attributes

static constexpr bool _type_mutable = true
 

Protected Member Functions

 DocTranslatorObj (const DocTranslatorVTable *vtable, ffi::Map< ffi::String, ffi::Any > extra_config={})
 Initialize an engine's translation context.
 

Protected Attributes

ffi::List< ffi::List< Doc > > doc_scopes_
 Active Doc collections, independent of lexical variable scopes.
 

Detailed Description

Eager translation context shared by registered hooks. Hooks construct Docs, use explicit lexical scopes, and attach origins when emitting.

ffi::Optional<ExprDoc> Hook(DocTranslatorObj* d, ffi::AnyView value,
const ffi::Object* destination) {
ExprDoc rhs = LiteralDoc::Str("value", std::nullopt);
if (!destination) return rhs;
auto var = ffi::GetRef<Var>(static_cast<const VarNode*>(destination));
IdDoc lhs(d->VarGetOrAllocId(var, true)->name);
d->Emit(AssignDoc(lhs, rhs, std::nullopt), var);
return std::nullopt;
}
// Register with TypeAttrDef<Node>().attr(kDocTranslate, FDocTranslate::FromNative<&Hook>()).
A local variable in the IR.
Definition expr.h:694
Reference type of AssignDocNode.
Definition doc.h:842
Eager translation context shared by registered hooks. Hooks construct Docs, use explicit lexical scop...
Definition doc_translator.h:120
void Emit(const Doc &doc, ffi::Optional< ffi::ObjectRef > origin=std::nullopt)
Append a completed Doc and record its optional origin.
Definition doc_translator.h:148
IdDoc VarGetOrAllocId(const Var &var, bool explicit_def)
Reuse or allocate a variable's stable identifier.
Definition doc_translator.h:159
Reference type of ExprDocNode.
Definition doc.h:134
Reference type of IdDocNode.
Definition doc.h:386
static LiteralDoc Str(const ffi::String &v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent string.
Definition doc.h:303

Constructor & Destructor Documentation

◆ DocTranslatorObj()

tvm::script::printer::DocTranslatorObj::DocTranslatorObj ( const DocTranslatorVTable *  vtable,
ffi::Map< ffi::String, ffi::Any >  extra_config = {} 
)
inlineexplicitprotected

Initialize an engine's translation context.

Parameters
vtableThe engine callbacks, valid for this object's lifetime.
extra_configThe owned configuration snapshot.

Member Function Documentation

◆ AllocId()

IdDoc tvm::script::printer::DocTranslatorObj::AllocId ( const ffi::String &  hint)
inline

Reserve a fresh identifier without mapping a variable.

Parameters
hintThe preferred name.
Returns
The allocated identifier.

◆ BeginDocScope()

void tvm::script::printer::DocTranslatorObj::BeginDocScope ( )
inline

Push an empty Doc collection.

◆ BeginVarScope()

void tvm::script::printer::DocTranslatorObj::BeginVarScope ( )
inline

Begin a lexical variable scope independent of Doc collection.

◆ CurrentScopeDocs()

ffi::List< Doc > tvm::script::printer::DocTranslatorObj::CurrentScopeDocs ( ) const
inline

Get the current Doc collection without popping it.

Returns
A mutable handle sharing the active collection's storage.

◆ DefaultTranslate()

ffi::Optional< ExprDoc > tvm::script::printer::DocTranslatorObj::DefaultTranslate ( ffi::AnyView  value,
const ffi::Object *  destination = nullptr 
)
inline

Dispatch directly to the registered native or packed hook.

Parameters
valueThe input value.
destinationA borrowed Var, or nullptr when no binding is requested.
Returns
An expression for the caller, or None after completed emission.

◆ Emit()

void tvm::script::printer::DocTranslatorObj::Emit ( const Doc &  doc,
ffi::Optional< ffi::ObjectRef >  origin = std::nullopt 
)
inline

Append a completed Doc and record its optional origin.

Parameters
docThe emitted Doc.
originThe original IR object represented by this emission.

◆ EndDocScope()

ffi::List< Doc > tvm::script::printer::DocTranslatorObj::EndDocScope ( )
inline

Pop the latest Doc collection.

Returns
The collected Docs.

◆ EndVarScope()

void tvm::script::printer::DocTranslatorObj::EndVarScope ( )
inline

End the latest naming region, retaining the root region. Explicit local bindings expire. Unresolved implicit references and their identifiers remain pending in the enclosing region, preserving captures shared by sibling functions.

◆ GetDocOrigins()

ffi::Dict< Doc, ffi::ObjectRef > tvm::script::printer::DocTranslatorObj::GetDocOrigins ( ) const
inline

Get the owned Doc-to-original-object associations.

Returns
A mutable handle sharing the origin dictionary's storage.

◆ GetExtraConfig()

template<typename T >
T tvm::script::printer::DocTranslatorObj::GetExtraConfig ( const ffi::String &  name,
T  fallback 
) const
inline

Read a typed extension option.

Template Parameters
TThe option type.
Parameters
nameThe option name.
fallbackThe value to use when absent.
Returns
The typed option or fallback, rejecting an existing wrong type.

◆ GetImplicitDefs()

ffi::Dict< Var, IdDoc > tvm::script::printer::DocTranslatorObj::GetImplicitDefs ( )
inline

Get the current variable scope's pending definitions.

Returns
The live mutable dictionary of variables and their identifiers.

◆ GetOrCreateExtraState()

template<typename T >
T tvm::script::printer::DocTranslatorObj::GetOrCreateExtraState ( const ffi::String &  name)
inline

Get or initialize typed extension state.

Template Parameters
TThe state type, default constructed when absent.
Parameters
nameThe state name.
Returns
The stored value, rejecting an existing wrong type; mutable handles share storage.

◆ RecordOrigin()

void tvm::script::printer::DocTranslatorObj::RecordOrigin ( const Doc &  doc,
const ffi::ObjectRef &  origin 
)
inline

Set a Doc's original IR object, replacing any previous association.

Parameters
docThe constructed Doc.
originThe original IR object.

◆ SetExtraState()

void tvm::script::printer::DocTranslatorObj::SetExtraState ( const ffi::String &  name,
std::optional< ffi::Any >  value 
)
inline

Replace or remove extension state.

Parameters
nameThe state name.
valueThe replacement value, or nullopt to remove the entry.

◆ Translate()

ffi::Optional< ExprDoc > tvm::script::printer::DocTranslatorObj::Translate ( ffi::AnyView  value,
const ffi::Optional< Var >  bind_var = std::nullopt 
)
inline

Translate a value with an optional pre-mapped binding variable.

Parameters
valueThe input value.
bind_varThe binding variable, or None when no binding is requested.
Returns
An expression for the caller, or None when emission completed the request. Callers requiring an expression use value(); for a specific Doc subtype, use value().as_or_throw<T>().

◆ TVM_FFI_DECLARE_OBJECT_INFO()

tvm::script::printer::DocTranslatorObj::TVM_FFI_DECLARE_OBJECT_INFO ( "script.printer.DocTranslator"  ,
DocTranslatorObj  ,
ffi::Object   
)

◆ VarGetOrAllocId()

IdDoc tvm::script::printer::DocTranslatorObj::VarGetOrAllocId ( const Var &  var,
bool  explicit_def 
)
inline

Reuse or allocate a variable's stable identifier.

Parameters
varThe variable to name.
explicit_defWhether this occurrence defines the variable.
Returns
The binding's IdDoc, with explicit definitions removed from pending entries.

◆ WithDocScope()

template<typename Callback >
ffi::List< Doc > tvm::script::printer::DocTranslatorObj::WithDocScope ( Callback &&  callback)
inline

Run a callback in a balanced Doc collection scope.

Template Parameters
CallbackThe callable type.
Parameters
callbackThe callback that emits Docs.
Returns
The collected Docs.

Member Data Documentation

◆ _type_mutable

constexpr bool tvm::script::printer::DocTranslatorObj::_type_mutable = true
staticconstexpr

◆ doc_scopes_

ffi::List<ffi::List<Doc> > tvm::script::printer::DocTranslatorObj::doc_scopes_
protected

Active Doc collections, independent of lexical variable scopes.


The documentation for this class was generated from the following file: