public static void AddPointerUnmarshalCall(this CodeMemberMethod method, RpcTypeDescriptor descriptor, string unmarshal_name, string var_name, params CodeExpression[] additional_args) { List <CodeExpression> args = new List <CodeExpression>(); args.AddRange(descriptor.AdditionalArgs.Select(r => r.Expression)); args.AddRange(additional_args); CodeAssignStatement assign = new CodeAssignStatement(GetVariable(var_name), new CodeMethodInvokeExpression(descriptor.GetUnmarshalMethod(GetVariable(unmarshal_name)), args.ToArray())); CodeAssignStatement assign_null = new CodeAssignStatement(GetVariable(var_name), new CodeDefaultValueExpression(descriptor.GetParameterType())); CodeConditionStatement if_statement = new CodeConditionStatement( new CodeBinaryOperatorExpression(new CodeMethodInvokeExpression(GetVariable(unmarshal_name), "ReadReferent"), CodeBinaryOperatorType.IdentityInequality, GetPrimitive(0)), new CodeStatement[] { assign }, new CodeStatement[] { assign_null }); method.Statements.Add(if_statement); }
private void GenerateClient(string name, CodeNamespace ns, int complex_type_count) { CodeTypeDeclaration type = ns.AddType(name); type.IsClass = true; type.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed; type.BaseTypes.Add(typeof(RpcAlpcClientBase)); CodeConstructor constructor = type.AddConstructor(MemberAttributes.Public | MemberAttributes.Final); constructor.BaseConstructorArgs.Add(CodeGenUtils.GetPrimitive(_server.InterfaceId.ToString())); constructor.BaseConstructorArgs.Add(CodeGenUtils.GetPrimitive(_server.InterfaceVersion.Major)); constructor.BaseConstructorArgs.Add(CodeGenUtils.GetPrimitive(_server.InterfaceVersion.Minor)); foreach (var proc in _server.Procedures) { string proc_name = proc.Name; if (!_proc_names.Add(proc_name)) { proc_name = $"{proc_name}_{proc.ProcNum}"; if (!_proc_names.Add(proc_name)) { throw new ArgumentException($"Duplicate name {proc.Name}"); } } var method = type.AddMethod(proc_name, MemberAttributes.Public | MemberAttributes.Final); RpcTypeDescriptor return_type = GetTypeDescriptor(proc.ReturnValue.Type); if (return_type == null) { method.ThrowNotImplemented("Return type unsupported."); continue; } var offset_to_name = proc.Params.Select(p => Tuple.Create(p.Offset, p.Name)).ToList(); method.ReturnType = return_type.CodeType; method.CreateMarshalObject(MARSHAL_NAME); foreach (var p in proc.Params) { if (p == proc.Handle) { continue; } RpcTypeDescriptor p_type = GetTypeDescriptor(p.Type); List <RpcMarshalArgument> extra_marshal_args = new List <RpcMarshalArgument>(); if (p_type.VarianceDescriptor.IsValid) { extra_marshal_args.Add(p_type.VarianceDescriptor.CalculateCorrelationArgument(p.Offset, offset_to_name)); } var p_obj = method.AddParam(p_type.GetParameterType(), p.Name); p_obj.Direction = p.GetDirection(); if (!p.IsIn) { continue; } if (p_type.Pointer) { if (p_type.PointerType == RpcPointerType.Reference) { method.AddNullCheck(MARSHAL_NAME, p.Name); } else { method.AddWriteReferent(MARSHAL_NAME, p.Name); } } else if (!p_type.ValueType) { method.AddNullCheck(MARSHAL_NAME, p.Name); } method.AddMarshalCall(p_type, MARSHAL_NAME, p.Name, extra_marshal_args.ToArray()); // If it's a constructed type then ensure any deferred writes are flushed. if (p_type.Constructed) { method.AddFlushDeferredWrites(MARSHAL_NAME); } } method.SendReceive(MARSHAL_NAME, UNMARSHAL_NAME, proc.ProcNum); foreach (var p in proc.Params.Where(x => x.IsOut)) { if (p == proc.Handle) { continue; } RpcTypeDescriptor p_type = GetTypeDescriptor(p.Type); if (p_type.Pointer) { method.AddPointerUnmarshalCall(p_type, UNMARSHAL_NAME, p.Name); } else { method.AddUnmarshalCall(p_type, UNMARSHAL_NAME, p.Name); } if (p_type.Constructed) { method.AddPopluateDeferredPointers(UNMARSHAL_NAME); } } method.AddUnmarshalReturn(return_type, UNMARSHAL_NAME); } if (complex_type_count > 0 && HasFlag(RpcClientBuilderFlags.GenerateConstructorProperties)) { var constructor_type = new CodeTypeReference(CodeGenUtils.MakeIdentifier(CONSTRUCTOR_STRUCT_NAME)); var prop = type.AddProperty("New", constructor_type, MemberAttributes.Public | MemberAttributes.Final, new CodeMethodReturnStatement(new CodeObjectCreateExpression(constructor_type))); constructor_type = new CodeTypeReference(CodeGenUtils.MakeIdentifier(ARRAY_CONSTRUCTOR_STRUCT_NAME)); type.AddProperty("NewArray", constructor_type, MemberAttributes.Public | MemberAttributes.Final, new CodeMethodReturnStatement(new CodeObjectCreateExpression(constructor_type))); } }