@@ -6,6 +6,7 @@ using ..NodeModule: AbstractExpressionNode, Node
66using .. OperatorEnumModule: AbstractOperatorEnum, OperatorEnum
77using .. UtilsModule: Undefined
88
9+ import .. NodeModule: copy_node, set_node!, count_nodes, tree_mapreduce, constructorof
910import .. NodeUtilsModule:
1011 preserve_sharing,
1112 count_constants,
8586end
8687
8788node_type (:: Union{E,Type{E}} ) where {N,E<: AbstractExpression{<:Any,N} } = N
88- @unstable default_node_type (:: Type{<:AbstractExpression} ) = Node
89+ @unstable default_node_type (_ ) = Node
8990default_node_type (:: Type{<:AbstractExpression{T}} ) where {T} = Node{T}
9091
9192# #######################################################
@@ -128,28 +129,52 @@ end
128129function Base. copy (ex:: AbstractExpression ; break_sharing:: Val = Val (false ))
129130 return error (" `copy` function must be implemented for $(typeof (ex)) types." )
130131end
131- function Base. hash (ex:: AbstractExpression , h:: UInt )
132- return error (" `hash` function must be implemented for $(typeof (ex)) types." )
133- end
134- function Base.:(== )(x:: AbstractExpression , y:: AbstractExpression )
135- return error (" `==` function must be implemented for $(typeof (x)) types." )
136- end
137132function get_constants (ex:: AbstractExpression )
138133 return error (" `get_constants` function must be implemented for $(typeof (ex)) types." )
139134end
140135function set_constants! (ex:: AbstractExpression{T} , constants, refs) where {T}
141136 return error (" `set_constants!` function must be implemented for $(typeof (ex)) types." )
142137end
138+ function get_contents (ex:: AbstractExpression )
139+ return error (" `get_contents` function must be implemented for $(typeof (ex)) types." )
140+ end
141+ function get_metadata (ex:: AbstractExpression )
142+ return error (" `get_metadata` function must be implemented for $(typeof (ex)) types." )
143+ end
143144# #######################################################
144145
145146"""
146- with_tree(ex::AbstractExpression, tree::AbstractExpressionNode)
147+ with_contents(ex::AbstractExpression, tree::AbstractExpressionNode)
148+ with_contents(ex::AbstractExpression, tree::AbstractExpression)
147149
148150Create a new expression based on `ex` but with a different `tree`
149151"""
150- function with_tree (ex:: AbstractExpression , tree)
151- return constructorof (typeof (ex))(tree, ex. metadata)
152+ function with_contents (ex:: AbstractExpression , tree:: AbstractExpression )
153+ return with_contents (ex, get_contents (tree))
154+ end
155+ function with_contents (ex:: AbstractExpression , tree)
156+ return constructorof (typeof (ex))(tree, get_metadata (ex))
157+ end
158+ function get_contents (ex:: Expression )
159+ return ex. tree
160+ end
161+
162+ """
163+ with_metadata(ex::AbstractExpression, metadata)
164+ with_metadata(ex::AbstractExpression; metadata...)
165+
166+ Create a new expression based on `ex` but with a different `metadata`.
167+ """
168+ function with_metadata (ex:: AbstractExpression ; metadata... )
169+ return with_metadata (ex, Metadata ((; metadata... )))
170+ end
171+ function with_metadata (ex:: AbstractExpression , metadata:: Metadata )
172+ return constructorof (typeof (ex))(get_contents (ex), metadata)
173+ end
174+ function get_metadata (ex:: Expression )
175+ return ex. metadata
152176end
177+
153178function preserve_sharing (:: Union{E,Type{E}} ) where {T,N,E<: AbstractExpression{T,N} }
154179 return preserve_sharing (N)
155180end
@@ -169,25 +194,17 @@ end
169194function Base. copy (ex:: Expression ; break_sharing:: Val = Val (false ))
170195 return Expression (copy (ex. tree; break_sharing), copy (ex. metadata))
171196end
172- function Base. hash (ex:: Expression , h:: UInt )
173- return hash (ex . tree , hash (ex . metadata , h))
197+ function Base. hash (ex:: AbstractExpression , h:: UInt )
198+ return hash (get_contents (ex) , hash (get_metadata (ex) , h))
174199end
175-
176- """
177- Base.:(==)(x::Expression, y::Expression)
178-
179- Check equality of two expressions `x` and `y` by comparing their trees and metadata.
180- """
181- function Base.:(== )(x:: Expression , y:: Expression )
182- return x. tree == y. tree && x. metadata == y. metadata
200+ function Base.:(== )(x:: AbstractExpression , y:: AbstractExpression )
201+ return get_contents (x) == get_contents (y) && get_metadata (x) == get_metadata (y)
183202end
184203
185204# Overload all methods on AbstractExpressionNode that return an aggregation, or can
186205# return an entire tree. Methods that only return the nodes are *not* overloaded, so
187206# that the user must use the low-level interface.
188207
189- import .. NodeModule: copy_node, set_node!, count_nodes, tree_mapreduce, constructorof
190-
191208# ! format: off
192209@unstable constructorof (:: Type{E} ) where {E<: AbstractExpression } = Base. typename (E). wrapper
193210@unstable constructorof (:: Type{<:Expression} ) = Expression
0 commit comments