Implement core_serialize_to on nclassdef

Are attributes serialized on demand per_attribute with serialize? Otherwise they are serialized by default, and we check instead for noserialize.

Property definitions

nitc $ SerializationPhasePreModel :: generate_serialization_method
	# Implement `core_serialize_to` on `nclassdef`
	#
	# Are attributes serialized on demand `per_attribute` with `serialize`?
	# Otherwise they are serialized by default, and we check instead for `noserialize`.
	fun generate_serialization_method(nclassdef: AClassdef, per_attribute: Bool)
	do
		var npropdefs = nclassdef.n_propdefs

		# Do not insert a `core_serialize_to` if it already exists
		for npropdef in npropdefs do
			if npropdef isa AMethPropdef then
				var methid = npropdef.n_methid
				if methid != null and methid.collect_text == "core_serialize_to" then
					return
				end
			end
		end

		var code = new Array[String]
		code.add "redef fun core_serialize_to(v)"
		code.add "do"
		code.add "	super"

		for attribute in npropdefs do if attribute isa AAttrPropdef then

			# Is `attribute` to be skipped?
			if (per_attribute and not attribute.is_serialize) or
				attribute.is_noserialize then continue

			code.add "	v.serialize_attribute(\"{attribute.serialize_name}\", {attribute.name})"
		end

		code.add "end"

		# Create method Node and add it to the AST
		npropdefs.push(toolcontext.parse_propdef(code.join("\n")))
	end
src/frontend/serialization_model_phase.nit:172,2--208,4