static void GenRequest(@request r, string name) { if (r.name == null) { return; } string inherits; if (isExtension) { inherits = "ExtensionRequest"; } else { inherits = "Request"; } cwt.WriteLine("[Request (" + r.opcode + ")]"); GenClass(NewTypeToCs(ToCs(r.name) + "Request"), r.Items); if (r.reply != null) { cwt.WriteLine("[Reply (" + r.opcode + ")]"); //GenClass (NewTypeToCs (ToCs (r.name) + "Reply"), r.reply.Items, " : " + "Reply"); GenClass(NewTypeToCs(ToCs(r.name) + "Reply"), r.reply.Items); } }
MemberDeclarationSyntax GenRequest(@request r) { if (r.name == null) { return; } string inherits = isExtension ? "ExtensionRequest" : "Request"; cwt.WriteLine("[Request (" + r.opcode + ")]"); GenClass(cwt, typeMap.NewTypeToCs(GeneratorUtil.ToCs(r.name) + "Request"), r.Items); if (r.reply != null) { cwt.WriteLine("[Reply (" + r.opcode + ")]"); GenClass(cwt, typeMap.NewTypeToCs(GeneratorUtil.ToCs(r.name) + "Reply"), r.reply.Items); } }
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(); }
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 (); }
static void GenRequest (@request r, string name) { if (r.name == null) return; string inherits; if (isExtension) inherits = "ExtensionRequest"; else inherits = "Request"; cwt.WriteLine ("[Request (" + r.opcode + ")]"); GenClass (NewTypeToCs (ToCs (r.name) + "Request"), r.Items); if (r.reply != null) { cwt.WriteLine ("[Reply (" + r.opcode + ")]"); //GenClass (NewTypeToCs (ToCs (r.name) + "Reply"), r.reply.Items, " : " + "Reply"); GenClass (NewTypeToCs (ToCs (r.name) + "Reply"), r.reply.Items); } }
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))); } }
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))); } }