internal override void PrintContents( TextWriter tw, int indent, XObjectGen xog) { tw.WriteLine("{"); for (int i = 0; i < size; i++) { if (i != 0) { tw.WriteLine(","); } Compiler.Indent(tw, indent + 1); XObject xo = xog.embeds[off + i]; if (xo is XObjectGen) { CCValues.PrintObjectGen(tw, indent + 1, (XObjectGen)xo); } else if (xo is XArrayGeneric) { CCValues.PrintArray(tw, indent + 1, (XArrayGeneric)xo); } else { throw new Exception(string.Format("unsupported embedded type: {0}", xo.GetType())); } } tw.WriteLine(); Compiler.Indent(tw, indent); tw.Write("}"); }
internal override XObject FindExt(string name) { XType xt = ObjectType; if (name == xt.Name) { return(this); } int[] p; if (!xt.extensionPath.TryGetValue(name, out p)) { throw new Exception(string.Format("type {0} does not extend type {1}", xt.Name, name)); } if (p.Length == 0) { throw new Exception(string.Format("ambiguous type extension {0} -> {1}", xt.Name, name)); } XObject xo = this; for (int i = 0; i < p.Length; i++) { XObjectGen xog = xo as XObjectGen; if (xog == null) { throw new Exception(string.Format("invalid extension path {0} -> {1}", xt.Name, name)); } xo = xog.embeds[p[i]]; } return(xo); }
internal override void Initialize(XObjectGen obj) { for (int i = 0; i < size; i++) { obj.fields[off + i].Clear(ft); } }
internal override void PrintValue(TextWriter tw) { tw.WriteLine(); string tname = Compiler.Encode(xo.ObjectType.Name); string name = string.Format("t1v_{0}_{1}", xo.Serial, tname); if (xo is XObjectGen) { XObjectGen xog = (XObjectGen)xo; tw.Write("static const struct t1s_{0} {1} = ", tname, name); PrintObjectGen(tw, 0, xog); } else if (xo is XArrayGeneric) { XArrayGeneric xag = (XArrayGeneric)xo; tw.WriteLine("static const t1x_array {0} = ", name); PrintArray(tw, 0, xag); } else { throw new Exception(string.Format("internal error: EmitInstanceTopLevel on {0}", xo.GetType())); } tw.WriteLine(";"); }
/* * Given a value, and a target type, find the relevant object * instance that the value extends. This is for accessors; an * exception is thrown on error. */ internal static XObject FindExtended(XObject xo, XType target) { XType xt = xo.ObjectType; if (xt == target) { return(xo); } int[] pp; if (!xt.extensionPath.TryGetValue(target.Name, out pp)) { throw new Exception(string.Format("accessor for an element of type {0} cannot be used on an instance of type {1}", target.Name, xt.Name)); } if (pp.Length == 0) { throw new Exception(string.Format("extension of type {0} by type {1} is ambiguous", target.Name, xt.Name)); } foreach (int p in pp) { XObjectGen xog = xo as XObjectGen; if (xog == null) { throw new Exception(string.Format("accessor applied to a non-generic object type {0}", xo.ObjectType.Name)); } xo = xog.embeds[p]; } return(xo); }
internal override void Initialize(XObjectGen obj) { for (int i = 0; i < size; i++) { obj.embeds[off + i] = et.NewInstance(); } }
internal void PrintContents(TextWriter tw, int indent, XObjectGen xog) { foreach (XTypeElt e in orderedContents) { tw.WriteLine(","); Compiler.Indent(tw, indent); e.PrintContents(tw, indent, xog); } }
/* * Given a value, and a target type, find the relevant object * instance that the value extends. This is for field accessors: * the final instance must be a generic object (XObjectGen). */ internal static XObjectGen FindExtendedGen(XObject xo, XType target) { xo = FindExtended(xo, target); XObjectGen xog = xo as XObjectGen; if (xog == null) { throw new Exception(string.Format("accessor applied to a non-generic object type {0}", xo.ObjectType.Name)); } return(xog); }
internal static void PrintObjectGen( TextWriter tw, int indent, XObjectGen xog) { XType xt = xog.ObjectType; tw.WriteLine("{"); Compiler.Indent(tw, indent + 1); tw.Write("(void *)&t1t_{0}", Compiler.Encode(xt.Name)); xt.PrintContents(tw, indent + 1, xog); tw.WriteLine(); Compiler.Indent(tw, indent); tw.Write("}"); }
internal override void PrintContents( TextWriter tw, int indent, XObjectGen xog) { tw.WriteLine("{"); for (int i = 0; i < size; i++) { if (i != 0) { tw.WriteLine(","); } Compiler.Indent(tw, indent + 1); CCValues.PrintRef(tw, xog.fields[off + i]); } tw.WriteLine(); Compiler.Indent(tw, indent); tw.Write("}"); }
internal override void PrintContents( TextWriter tw, int indent, XObjectGen xog) { XObject xo = xog.embeds[Offset]; if (xo is XObjectGen) { CCValues.PrintObjectGen(tw, indent, (XObjectGen)xo); } else if (xo is XArrayGeneric) { CCValues.PrintArray(tw, indent, (XArrayGeneric)xo); } else { throw new Exception(string.Format("unsupported embedded type: {0}", xo.GetType())); } }
XObject InstanceCreatorGeneric() { Close(); /* * We can create instances for types which are embeddable, * and vice versa. */ if (!IsEmbeddable) { throw new Exception(string.Format("type {0} does not allow creating generic instances", Name)); } XValue[] fields = new XValue[numFields]; XObject[] embeds = new XObject[numEmbeds]; XObjectGen xo = new XObjectGen(this, fields, embeds); foreach (XTypeElt elt in orderedContents) { elt.Initialize(xo); } return(xo); }
internal abstract void Initialize(XObjectGen obj);
static EmitInstance AddInstance(XObject xo, EmitInstance container, int index) { EmitInstance ei; if (toEmit.TryGetValue(xo, out ei)) { /* * We already have an EmitInstance for this object. * * If container is null, then we can simply keep * the existing instance, and we do not have to * propagate things any further: the object is * already handled. * * If container is not null, but the existing * instance is top-level, then we must convert it * to an EmitInstanceEmbedded; all its embedded * values automatically follow. * * Since embedding is a tree, there should be no * case where container is not null AND the * existing EmitInstance is also of embedded type. */ if (container != null) { if (!(ei is EmitInstanceTopLevel)) { throw new Exception("internal error: instance already embedded, or not embeddable"); } ei = new EmitInstanceEmbedded(xo, container, index); toEmit[xo] = ei; } return(ei); } /* * We have a new instance. We create the object, then * recursively walk over its fields and embedded values. */ if (xo is XObjectGen) { if (container != null) { ei = new EmitInstanceEmbedded(xo, container, index); } else { ei = new EmitInstanceTopLevel(xo); } XObjectGen xog = (XObjectGen)xo; int n; n = xog.fields.Length; for (int i = 0; i < n; i++) { AddValueInner(xog.fields[i]); } n = xog.embeds.Length; for (int i = 0; i < n; i++) { AddInstance(xog.embeds[i], ei, i); } } else if (xo is XArrayGeneric) { if (container != null) { ei = new EmitInstanceEmbedded(xo, container, index); } else { ei = new EmitInstanceTopLevel(xo); } XArrayGeneric xag = (XArrayGeneric)xo; AddBuffer(xag.dataBuf, xag.ObjectType.GetArrayElementType(), xag.ObjectType.arrayElementEmbedded, xag.dataOff, xag.dataLen); /* obsolete * } else if (xo is XString) { * throw new Exception("NYI"); */ } else if (xo is XType) { ei = new EmitXType((XType)xo); } else { throw new Exception(string.Format("unsupported object type for serialization: {0}", xo.ObjectType.Name)); } toEmit[xo] = ei; return(ei); }
internal abstract void PrintContents( TextWriter tw, int indent, XObjectGen xog);
internal override void Initialize(XObjectGen obj) { obj.embeds[Offset] = Embed.NewInstance(); }
internal override void PrintContents( TextWriter tw, int indent, XObjectGen xog) { CCValues.PrintRef(tw, xog.fields[off]); }
internal override void Initialize(XObjectGen obj) { obj.fields[off].Clear(ft); }