nitc :: DerivingPhase :: defaultinit
nitc $ DerivingPhase :: SELF
Type of this instance, automatically specialized in every classnitc $ DerivingPhase :: process_annotated_node
Specific actions to execute on annotated nodesnitc :: Phase :: _in_hierarchy
The dependence relation of the phase with the other phasesnitc :: Phase :: _toolcontext
The toolcontext instance attached to the phasecore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Object :: defaultinit
nitc :: DerivingPhase :: defaultinit
nitc :: Phase :: defaultinit
nitc :: Phase :: in_hierarchy
The dependence relation of the phase with the other phasesnitc :: Phase :: in_hierarchy=
The dependence relation of the phase with the other phasescore :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: native_class_name
The class name of the object in CString format.core :: Object :: output_class_name
Display class name on stdout (debug only).nitc :: Phase :: process_annotated_node
Specific actions to execute on annotated nodesnitc :: Phase :: process_mainmodule
Specific action to execute on the whole program.nitc :: Phase :: process_nclassdef
Specific actions to execute on the tree of a class definitionnitc :: Phase :: process_nmodule
Specific actions to execute on the whole tree of a modulenitc :: Phase :: process_nmodule_after
Specific actions to execute on the whole tree of a modulenitc :: Phase :: process_npropdef
Specific actions to execute on the tree of a propertynitc :: Phase :: toolcontext
The toolcontext instance attached to the phasenitc :: Phase :: toolcontext=
The toolcontext instance attached to the phase
private class DerivingPhase
super Phase
redef fun process_annotated_node(nclassdef, nat)
do
if nat.name == "auto_inspect" then
if not nclassdef isa AStdClassdef then
toolcontext.error(nclassdef.location, "Syntax Error: only a concrete class can be `{nat.name}`.")
else
generate_inspect_method(nclassdef)
end
end
if nat.name == "auto_derive" then
if not nclassdef isa AStdClassdef then
toolcontext.error(nclassdef.location, "Syntax Error: only a concrete class can be `{nat.name}`.")
else
generate_derive_to_map_method(nclassdef, nat)
end
end
end
fun generate_inspect_method(nclassdef: AClassdef)
do
var npropdefs = nclassdef.n_propdefs
var code = new Array[String]
code.add "redef fun inspect"
code.add "do"
code.add " var res = super"
code.add " res = res.substring(0,res.length-1)"
for attribute in npropdefs do if attribute isa AAttrPropdef then
var name = attribute.n_id2.text
code.add """ res += " {{{name}}}: {self.{{{name}}}.inspect}""""
end
code.add " res += \">\""
code.add " return res"
code.add "end"
# Create method Node and add it to the AST
npropdefs.push(toolcontext.parse_propdef(code.join("\n")))
end
fun generate_derive_to_map_method(nclassdef: AClassdef, nat: AAnnotation)
do
var npropdefs = nclassdef.n_propdefs
var sc = toolcontext.parse_superclass("Derivable")
sc.location = nat.location
nclassdef.n_propdefs.add sc
var code = new Array[String]
code.add "redef fun derive_to_map"
code.add "do"
code.add " var res = super"
for attribute in npropdefs do if attribute isa AAttrPropdef then
var name = attribute.n_id2.text
code.add """ res["{{{name}}}"] = self.{{{name}}}"""
end
code.add " return res"
code.add "end"
# Create method Node and add it to the AST
npropdefs.push(toolcontext.parse_propdef(code.join("\n")))
end
end
src/frontend/deriving.nit:33,1--102,3