@@ -10,12 +10,6 @@ ModelingToolkit IR. Example:
10
10
```
11
11
12
12
registers `f` as a possible two-argument function.
13
-
14
- NOTE: If registering outside of ModelingToolkit (i.e. in your own package),
15
- this should be done at runtime (e.g. in a package `__init__()`, or inside a
16
- method that is called at runtime and not during precompile) to ensure that
17
- any generated functions will use your registered method. See
18
- `inject_registered_module_functions()`.
19
13
"""
20
14
macro register (sig)
21
15
splitsig = splitdef (:($ sig = nothing ))
@@ -94,24 +88,29 @@ Base.:^(x::Expression,y::T) where T <: Rational = Operation(Base.:^, Expression[
94
88
Base. getindex (x:: Operation ,i:: Int64 ) = Operation (getindex,[x,i])
95
89
Base. one (:: Operation ) = 1
96
90
91
+ # Ensure that Operations that get @registered from outside the ModelingToolkit
92
+ # module can work without having to bring in the associated function into the
93
+ # ModelingToolkit namespace. We basically store information about functions
94
+ # registered at runtime in a ModelingToolkit variable,
95
+ # `registered_external_functions`. It's not pretty, but we are limited by the
96
+ # way GeneralizedGenerated builds a function (adding "ModelingToolkit" to every
97
+ # function call).
97
98
# ---
98
- # Ensure that Operations that get @registered from outside the ModelingToolkit module can work without
99
- # having to bring in the associated function into the ModelingToolkit namespace.
100
- # We basically store information about functions registered at runtime in a ModelingToolkit variable,
101
- # `registered_external_functions`. It's not pretty, but we are limited by the way GeneralizedGenerated
102
- # builds a function (adding "ModelingToolkit" to every function call).
103
- # ---
104
- import JuliaVariables
105
99
const registered_external_functions = Dict {Symbol,Module} ()
106
100
function inject_registered_module_functions (expr)
107
101
MacroTools. postwalk (expr) do x
108
- MacroTools . @capture (x, f_ (xs__)) # We need to find all function calls in the expression.
109
- # If the function call has been converted to a JuliaVariables.Var and matches
110
- # one of the functions we've registered...
102
+ # We need to find all function calls in the expression.. .
103
+ MacroTools . @capture (x, f_ (xs__))
104
+
111
105
if ! isnothing (f) && f isa Expr && f. head == :. && f. args[2 ] isa QuoteNode
112
- f_name = f. args[2 ]. value
106
+ # If the function call matches any of the functions we've
107
+ # registered, set the calling module (which is probably
108
+ # "ModelingToolkit") to the module it is registered to.
109
+ f_name = f. args[2 ]. value # function name
113
110
f. args[1 ] = get (registered_external_functions, f_name, f. args[1 ])
114
111
end
115
- return x # Make sure we rebuild the expression as is.
112
+
113
+ # Make sure we rebuild the expression as is.
114
+ return x
116
115
end
117
116
end
0 commit comments