static void GenClass(string sName, object[] sItems, string suffix) { Dictionary <string, int> sizeParams = new Dictionary <string, int> (); //bool basicStruct = false; bool basicStruct = basic; if (sName == "GContext") { basicStruct = true; } if (sName == "Drawable") { basicStruct = true; } if (sName == "Fontable") { basicStruct = true; } //if (sName == "ClientMessageData") // basicStruct = true; bool isRequest = sName.EndsWith("Request"); bool isEvent = sName.EndsWith("Event"); if (!basicStruct) { int structSize = StructSize(sItems); cwt.WriteLine("[StructLayout (LayoutKind.Explicit, Pack=1, CharSet=CharSet.Ansi, Size=" + structSize + ")]"); cwt.WriteLine("public struct @" + sName + "Data"); cwt.WriteLine("{"); if (isRequest) //TODO: generate one or the other { cwt.WriteLine("[FieldOffset (0)]"); cwt.WriteLine("public Request Header;"); cwt.WriteLine("[FieldOffset (0)]"); cwt.WriteLine("public ExtensionRequest ExtHeader;"); } GenClassData(sName + "Data", sItems, "", true); cwt.WriteLine("}"); cwt.WriteLine(); } if (basicStruct) { int structSize = StructSize(sItems); cwt.WriteLine("[StructLayout (LayoutKind.Explicit, Pack=1, CharSet=CharSet.Ansi, Size=" + structSize + ")]"); cwt.WriteLine("public struct @" + sName + suffix); } else { //TODO: clean up hack if (suffix == "") { suffix += " : "; } else { suffix += ", "; } suffix += "IMessagePart"; cwt.WriteLine("public class @" + sName + suffix); } cwt.WriteLine("{"); if (!basicStruct) { cwt.WriteLine("public " + sName + "Data" + " MessageData;"); } int offset = GenClassData(sName, sItems, "", basicStruct); if (!basicStruct) { //cwt.WriteLine ("public byte[] dat;"); /* * cwt.WriteLine ("public int Read (IntPtr ptr)"); * cwt.WriteLine ("{"); * //cwt.WriteLine ("MessageData = (" + sName + "Data" + ")Marshal.PtrToStructure (ptr, typeof(" + sName + "Data" + "));"); * cwt.WriteLine ("unsafe {"); * cwt.WriteLine ("MessageData = *(" + sName + "Data" + "*)ptr;"); * cwt.WriteLine ("}"); * cwt.WriteLine ("return 0;"); * cwt.WriteLine ("}"); * cwt.WriteLine (); */ cwt.WriteLine("public int Read (IntPtr ptr)"); cwt.WriteLine("{"); cwt.WriteLine("int offset = 0;"); cwt.WriteLine("unsafe {"); cwt.WriteLine("MessageData = *(" + sName + "Data" + "*)ptr;"); cwt.WriteLine("offset += sizeof (" + sName + "Data" + ");"); cwt.WriteLine("}"); if (sItems != null) { foreach (object ob in sItems) { if (ob is list) { list l = ob as list; string lName = ToCs(l.name); string lType = TypeToCs(l.type); if (lName == sName) { Console.Error.WriteLine("Warning: list field renamed: " + lName); lName = "Values"; } if (l.type == "CHAR2B" || lType == "sbyte") { cwt.WriteLine("//if (@" + lName + " != null)"); cwt.WriteLine("//yield return XMarshal.Do (@" + lName + ");"); //cwt.WriteLine (lName + " = Marshal.PtrToStringAnsi (new IntPtr ((int)ptr + offset), MessageData.@" + ToCs (l.fieldref) + ");"); cwt.WriteLine("//" + lName + " = Marshal.PtrToStringAnsi (new IntPtr ((int)ptr + offset), MessageData.@" + (lName + "Len") + ");"); cwt.WriteLine("//offset += " + (lName + "Len") + ";"); } } else if (ob is valueparam) { valueparam v = ob as valueparam; string vName = "Values"; if (v.valuelistname != null) { vName = ToCs(v.valuelistname); } string vType = TypeToCs(v.valuemasktype); if (vType == "uint") { cwt.WriteLine("//if (@" + vName + " != null)"); cwt.WriteLine("//yield return XMarshal.Do (ref @" + vName + ");"); } } } } cwt.WriteLine("return offset;"); cwt.WriteLine("}"); cwt.WriteLine(); cwt.WriteLine("IEnumerator IEnumerable.GetEnumerator () { return GetEnumerator (); }"); cwt.WriteLine(); cwt.WriteLine("public IEnumerator<IOVector> GetEnumerator ()"); cwt.WriteLine("{"); cwt.WriteLine("yield return XMarshal.Do (ref MessageData);"); if (sItems != null) { foreach (object ob in sItems) { if (ob is list) { list l = ob as list; string lName = ToCs(l.name); string lType = TypeToCs(l.type); if (lName == sName) { Console.Error.WriteLine("Warning: list field renamed: " + lName); lName = "Values"; } if (l.type == "CHAR2B" || lType == "sbyte" || lType == "byte") { cwt.WriteLine("if (@" + lName + " != null)"); cwt.WriteLine("yield return XMarshal.Do (ref @" + lName + ");"); } } else if (ob is valueparam) { valueparam v = ob as valueparam; string vName = "Values"; if (v.valuelistname != null) { vName = ToCs(v.valuelistname); } string vType = TypeToCs(v.valuemasktype); if (vType == "uint") { cwt.WriteLine("if (@" + vName + " != null)"); cwt.WriteLine("yield return XMarshal.Do (ref @" + vName + ");"); } } } } cwt.WriteLine("}"); cwt.WriteLine(); } if (sItems != null) { foreach (object ob in sItems) { if (ob is list) { list l = ob as list; string lName = ToCs(l.name); if (lName == sName) { Console.Error.WriteLine("Warning: list field renamed: " + lName); lName = "Values"; } string lType = TypeToCs(l.type); //cwt.WriteLine ("//" + l.type); if (!sizeParams.ContainsKey(l.name)) { Console.Error.WriteLine("Warning: No length given for " + lName); cwt.WriteLine("//FIXME: No length given"); } else { if (l.type == "CHAR2B" || lType == "sbyte") { cwt.WriteLine("//[MarshalAs (UnmanagedType.LPStr, SizeParamIndex=" + sizeParams[l.name] + ")]"); } else { cwt.WriteLine("[MarshalAs (UnmanagedType.LPArray, SizeParamIndex=" + sizeParams[l.name] + ")]"); } } ////cwt.WriteLine ("//public " + lType + "[]" + " @" + lName + ";"); //cwt.WriteLine ("[FieldOffset (" + offset + ")]"); ////cwt.WriteLine ("//public ValueList<" + lType + ">" + " @" + lName + ";"); if (l.type == "CHAR2B" || lType == "sbyte") { cwt.WriteLine("public " + "string" + " @" + lName + ";"); } else { cwt.WriteLine("public " + lType + "[]" + " @" + lName + ";"); } offset += 4; } else if (ob is valueparam) { valueparam v = ob as valueparam; string vName = "Values"; if (v.valuelistname != null) { vName = ToCs(v.valuelistname); } string vType = TypeToCs(v.valuemasktype); //cwt.WriteLine ("[FieldOffset (" + offset + ")]"); //cwt.WriteLine ("public ValueList<" + TypeToCs (v.valuemasktype) + "> @" + "Values" + ";"); cwt.WriteLine("//public ValueList<" + vType + "> @" + vName + ";"); cwt.WriteLine("public " + vType + "[] @" + vName + ";"); offset += 4; } } } cwt.WriteLine("}"); cwt.WriteLine(); }
MemberDeclarationSyntax GenFunction(@request r) { // TODO: we should be able to share a lot of this with InterfaceGenerator if (r.name == null) { throw new Exception("Can't have null name"); // FIXME: handle this } //TODO: share code with struct List <ParameterSyntax> methodParameters = new List <ParameterSyntax>(); List <StatementSyntax> methodBody = new List <StatementSyntax>(); //cw.WriteLine(GeneratorUtil.ToCs(r.name) + "Request req = new " + GeneratorUtil.ToCs(r.name) + "Request ();"); var requestType = IdentifierName(GeneratorUtil.ToCs(r.name) + "Request"); methodBody.Add( LocalDeclarationStatement( VariableDeclaration( requestType, SingletonSeparatedList( VariableDeclarator(Identifier("req"), null, EqualsValueClause( ObjectCreationExpression( requestType). WithArgumentList(ArgumentList()))))))); var messageDataAccess = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("req"), IdentifierName("MessageData")); if (isExtension) { var extHeaderAccess = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, messageDataAccess, IdentifierName("ExtHeader")); methodBody.Add(ExpressionStatement( AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, extHeaderAccess, IdentifierName("MajorOpcode")), IdentifierName("GlobalId")))); methodBody.Add(ExpressionStatement( AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, extHeaderAccess, IdentifierName("MajorOpcode")), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(int.Parse(r.opcode)))))); } else { var headerAccess = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, messageDataAccess, IdentifierName("Header")); methodBody.Add(ExpressionStatement( AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, headerAccess, IdentifierName("Opcode")), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(int.Parse(r.opcode)))))); } if (r.Items != null) { foreach (object ob in r.Items) { if (ob is field) { field f = ob as field; if (f.name == null) { continue; } string paramName = "@" + GeneratorUtil.ToParm(GeneratorUtil.ToCs(f.name)); methodParameters.Add(Parameter(Identifier(paramName)). WithType(IdentifierName(typeMap.TypeToCs(f.type)))); methodBody.Add(ExpressionStatement( AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, messageDataAccess, IdentifierName("@" + GeneratorUtil.ToCs(f.name))), IdentifierName(paramName)))); } else if (ob is list) { list l = ob as list; if (l.name == null) { continue; } string paramName = "@" + GeneratorUtil.ToParm(GeneratorUtil.ToCs(l.name)); TypeSyntax paramType; if (l.type == "char") { paramType = PredefinedType(Token(SyntaxKind.StringKeyword)); } else if (l.type == "CARD32") { paramType = ArrayType(PredefinedType(Token(SyntaxKind.UIntKeyword))). WithRankSpecifiers(SingletonList( ArrayRankSpecifier( SingletonSeparatedList <ExpressionSyntax>( OmittedArraySizeExpression())))); } else { // FIXME: handle these continue; } methodParameters.Add(Parameter(Identifier(paramName)). WithType(paramType)); methodBody.Add(ExpressionStatement( AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("req"), IdentifierName("@" + GeneratorUtil.ToCs(l.name))), IdentifierName(paramName)))); } else if (ob is valueparam) { valueparam v = ob as valueparam; string vName = (v.valuelistname == null) ? "Values" : GeneratorUtil.ToCs(v.valuelistname); string vType = typeMap.TypeToCs(v.valuemasktype); string paramName = "@" + GeneratorUtil.ToParm(vName); if (vType == "uint") { methodParameters.Add(Parameter(Identifier(paramName)). WithType(ArrayType(PredefinedType(Token(SyntaxKind.UIntKeyword))). WithRankSpecifiers(SingletonList( ArrayRankSpecifier( SingletonSeparatedList <ExpressionSyntax>( OmittedArraySizeExpression())))))); methodBody.Add(ExpressionStatement( AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("req"), IdentifierName("@" + vName)), IdentifierName(paramName)))); } } } } //cw.WriteLine("c.xw.Send (req);"); methodBody.Add(ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("c"), IdentifierName("xw")), IdentifierName("Send"))). WithArgumentList(ArgumentList( SingletonSeparatedList(Argument(IdentifierName("req"))))))); if (r.reply != null) { //cw.WriteLine("return c.xrr.GenerateCookie<" + GeneratorUtil.ToCs(r.name) + "Reply> ();"); var xrrAccess = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("c"), IdentifierName("xrr")); methodBody.Add( ReturnStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, xrrAccess, GenericName("GenerateCookie"). WithTypeArgumentList( TypeArgumentList( SingletonSeparatedList <TypeSyntax>( IdentifierName( GeneratorUtil.ToCs(r.name) + "Reply")))))))); } if (r.reply != null) { //cw.WriteLine("public Cookie<" + GeneratorUtil.ToCs(r.name) + "Reply> " + GeneratorUtil.ToCs(r.name) + // " (" + parms + ");"); return(MethodDeclaration( GenericName("Cookie"). WithTypeArgumentList( TypeArgumentList( SingletonSeparatedList <TypeSyntax>( IdentifierName(GeneratorUtil.ToCs(r.name) + "Reply")))), GeneratorUtil.ToCs(r.name)). WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword))). WithParameterList(ParameterList(SeparatedList(methodParameters))). WithBody(Block(methodBody))); } else { //cw.WriteLine("public void " + GeneratorUtil.ToCs(r.name) + " (" + parms + ");"); return(MethodDeclaration( PredefinedType(Token(SyntaxKind.VoidKeyword)), GeneratorUtil.ToCs(r.name)). WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword))). WithParameterList(ParameterList(SeparatedList(methodParameters))). WithBody(Block(methodBody))); } }
static void GenFunction(@request r, string name) { if (r.name == null) { return; } //TODO: share code with struct string parms = ""; List <string> parmList1 = new List <string> (); List <string> parmList2 = new List <string> (); if (r.Items != null) { foreach (object ob in r.Items) { if (ob is field) { field f = ob as field; if (f.name == null) { continue; } //if (f.name.EndsWith ("_len")) // continue; parms += ", " + TypeToCs(f.type) + " @" + ToParm(ToCs(f.name)); parmList1.Add(ToCs(f.name)); } else if (ob is list) { list l = ob as list; if (l.name == null) { continue; } if (l.type == "char") { parms += ", " + "string" + " @" + ToParm(ToCs(l.name)); parmList2.Add(ToCs(l.name)); } else if (l.type == "CARD32") { parms += ", " + "uint[]" + " @" + ToParm(ToCs(l.name)); parmList2.Add(ToCs(l.name)); } } else if (ob is valueparam) { valueparam v = ob as valueparam; string vName = "Values"; if (v.valuelistname != null) { vName = ToCs(v.valuelistname); } string vType = TypeToCs(v.valuemasktype); if (vType == "uint") { parms += ", " + "uint[]" + " @" + ToParm(vName); parmList2.Add(vName); } } } parms = parms.Trim(',', ' '); } /* * if (r.reply != null) * cw.WriteLine ("[Reply (typeof (" + ToCs (r.name) + "Reply" + "))]"); * cw.WriteLine ("public void " + ToCs (r.name) + " (" + parms + ")"); */ if (r.reply != null) { cw.WriteLine("public " + "Cookie<" + ToCs(r.name) + "Reply" + ">" + " " + ToCs(r.name) + " (" + parms + ")", cwi, ";"); } else { cw.WriteLine("public " + "void" + " " + ToCs(r.name) + " (" + parms + ")", cwi, ";"); } cw.WriteLine("{"); //cw.WriteLine ("ProtocolRequest req = new ProtocolRequest ();"); /* * cw.WriteLine ("req.Count = " + 2 + ";"); * cw.WriteLine ("req.Extension = " + 0 + ";"); * cw.WriteLine ("req.Opcode = " + r.opcode + ";"); * cw.WriteLine ("req.IsVoid = " + (r.reply == null ? "1" : "0") + ";"); */ /* * cw.WriteLine ("req.Opcode = " + r.opcode + ";"); * cw.WriteLine ("req.Data = " + 0 + ";"); * cw.WriteLine ("req.Length = " + 4 + ";"); * cw.WriteLine (); */ cw.WriteLine("" + ToCs(r.name) + "Request req = new " + ToCs(r.name) + "Request ();"); if (isExtension) { cw.WriteLine("req.MessageData.ExtHeader.MajorOpcode = GlobalId;"); cw.WriteLine("req.MessageData.ExtHeader.MinorOpcode = " + r.opcode + ";"); } else { cw.WriteLine("req.MessageData.Header.Opcode = " + r.opcode + ";"); } cw.WriteLine(); /* * if (r.Items != null) * foreach (object ob in r.Items) { * if (ob is field) { * field f = ob as field; * if (f.name != null) { * if (f.name == "roots") * Console.Error.WriteLine (f.type); * cw.WriteLine ("req.MessageData.@" + ToCs (f.name) + " = @" + ToParm (ToCs (f.name)) + ";"); * } * } * } */ foreach (string par in parmList1) { cw.WriteLine("req.MessageData.@" + par + " = @" + ToParm(par) + ";"); } foreach (string par in parmList2) { cw.WriteLine("req.@" + par + " = @" + ToParm(par) + ";"); } /* * cw.WriteLine ("unsafe {"); * //cw.WriteLine (ToCs (r.name) + "RequestData* dp;"); * cw.WriteLine ("fixed (" + ToCs (r.name) + "RequestData* dp = &req.MessageData) {"); * cw.WriteLine ("c.Send ((IntPtr)dp, sizeof (" + ToCs (r.name) + "RequestData" + "));"); * cw.WriteLine ("}"); * cw.WriteLine ("}"); * cw.WriteLine ("IntPtr ptr;"); */ if (r.Items != null) { foreach (object ob in r.Items) { if (ob is list) { list l = ob as list; if (l.name == null) { continue; } if (l.type != "char") { continue; } /* * cw.WriteLine (); * cw.WriteLine ("ptr = UnixMarshal.StringToHeap (@" + ToParm (ToCs (l.name)) + ");"); * cw.WriteLine ("c.Send (ptr, @" + ToParm (ToCs (l.name)) + ".Length);"); */ cw.WriteLine("req.@" + ToCs(l.name) + " = @" + ToParm(ToCs(l.name)) + ";"); } } } cw.WriteLine(); cw.WriteLine("c.xw.Send (req);"); cw.WriteLine(); if (r.reply != null) { cw.WriteLine(); cw.WriteLine("return c.xrr.GenerateCookie" + "<" + ToCs(r.name) + "Reply" + ">" + " ();"); } cw.WriteLine("}"); cw.WriteLine(); }
private MethodDeclarationSyntax GenFunction(@request r, string name) { if (r.name == null) { return(null); } //TODO: share code with struct List <ParameterSyntax> parameters = new List <ParameterSyntax>(); if (r.Items != null) { foreach (object ob in r.Items) { if (ob is field) { field f = ob as field; if (f.name == null) { continue; } parameters.Add(Parameter(Identifier("@" + f.name)).WithType(IdentifierName(f.type))); } else if (ob is list) { list l = ob as list; if (l.name == null) { continue; } string listName = "@" + GeneratorUtil.ToParm(GeneratorUtil.ToCs(l.name)); if (l.type == "char") { parameters.Add(Parameter(Identifier(listName)). WithType(PredefinedType(Token(SyntaxKind.StringKeyword)))); } else if (l.type == "CARD32") { parameters.Add(Parameter(Identifier(listName)). WithType(ArrayType(PredefinedType(Token(SyntaxKind.UIntKeyword))). WithRankSpecifiers(SingletonList( ArrayRankSpecifier( SingletonSeparatedList <ExpressionSyntax>( OmittedArraySizeExpression())))))); } } else if (ob is valueparam) { valueparam v = ob as valueparam; string vName = (v.valuelistname == null) ? "Values" : "@" + GeneratorUtil.ToParm(GeneratorUtil.ToCs(v.valuelistname)); string vType = typeMap.TypeToCs(v.valuemasktype); if (vType == "uint") { parameters.Add(Parameter(Identifier(vName)). WithType(ArrayType(PredefinedType(Token(SyntaxKind.UIntKeyword))). WithRankSpecifiers(SingletonList( ArrayRankSpecifier( SingletonSeparatedList <ExpressionSyntax>( OmittedArraySizeExpression())))))); } } } } if (r.reply != null) { TypeSyntax returnType = GenericName(Identifier("Cookie"), TypeArgumentList(SingletonSeparatedList <TypeSyntax>( IdentifierName(GeneratorUtil.ToCs(r.name))))); return(MethodDeclaration(returnType, Identifier(GeneratorUtil.ToCs(r.name))). WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword))). WithParameterList(ParameterList(SeparatedList(parameters))). WithSemicolonToken(Token(SyntaxKind.SemicolonToken))); } else { return(MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(GeneratorUtil.ToCs(r.name))). WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword))). WithParameterList(ParameterList(SeparatedList(parameters))). WithSemicolonToken(Token(SyntaxKind.SemicolonToken))); } }