private void DefineField(CodeType typeDecl, CppField field) { CodeMethod caller = null; TypeDesc fieldType = GetTypeDesc(field.Type); var fld = new CodeField(ResolveCefType(fieldType.ToString()), field.Name.EscapeName()); if (fieldType.IsCallable) { fld.Attributes = CodeAttributes.Public; // | CodeAttributes.ReadOnly; fld.Comments.AddVSDocComment(fieldType.Name, "summary"); CppFunctionType fnType = fieldType.FunctionTypeRef; TypeDesc retType = GetTypeDesc(fnType.ReturnType); caller = new CodeMethod(field.Name.ToUpperCamel(fnType.Parameters.Count).EscapeName()); var rvtype = new CodeMethodParameter(null); rvtype.Type = ResolveCefType(retType.ToString()); if (retType.Name == "char16" || retType.Name == "wchar") { rvtype.CustomAttributes.Add(new CustomCodeAttribute("return: MarshalAs(UnmanagedType.U2)")); throw new NotImplementedException(); // TODO: check it } caller.RetVal = rvtype; caller.Attributes = CodeAttributes.Public | CodeAttributes.Unsafe; //caller.CustomAttributes.AddMethodImplForwardRefAttribute(); caller.CustomAttributes.Add(new CustomCodeAttribute("NativeName") { Parameters = { "\"" + field.Name + "\"" } }); caller.Comments.AddVSDocComment(field.Comment, "summary"); caller.Callee = fld; CppContainerList <CppParameter> @params = fnType.Parameters; for (int i = 0; i < @params.Count; i++) { CppParameter arg = @params[i]; if (i == 0 && arg.Name == "self") { string argTypeName = ResolveCefType(arg.Type.GetDisplayName()); if (argTypeName == typeDecl.Name + "*") { caller.HasThisArg = true; continue; } } var param = new CodeMethodParameter(arg.Name.EscapeName()); TypeDesc paramType = GetTypeDesc(arg.Type); string argType = paramType.ToString(); while (argType.StartsWith("const ")) { argType = argType.Substring(6); param.Direction = CodeMethodParameterDirection.In; } if (param.Direction == CodeMethodParameterDirection.In) { param.CustomAttributes.Add(new CustomCodeAttribute("Immutable")); } if (argType == "char16" || argType == "wchar") { param.CustomAttributes.Add(new CustomCodeAttribute("MarshalAs(UnmanagedType.U2)")); throw new NotImplementedException(); // TODO: check it } param.Type = ResolveCefType(argType); caller.Parameters.Add(param); } //fld.TypeName = GetNativeDelegate(caller, typeDecl); caller.Body = GetNativeCallMehtodBody(caller, fld, typeDecl); } else { fld.Comments.AddVSDocComment(field.Comment, "summary"); fld.Attributes = CodeAttributes.Public; if (fieldType.Name == "char16" || fieldType.Name == "wchar") { fld.CustomAttributes.Add(new CustomCodeAttribute("MarshalAs(UnmanagedType.U2)")); } } typeDecl.Members.Add(fld); if (caller != null) { typeDecl.Members.Add(caller); } }
private void DefineFunction(CodeType typeDecl, CppFunction func) { if (func.Name == "ArraySizeHelper") { return; } if (func.LinkageKind != CppLinkageKind.External) { throw new NotImplementedException(); } TypeDesc retType = GetTypeDesc(func.ReturnType); var fn = new CodeMethod(func.Name); fn.RetVal = new CodeMethodParameter(null) { Type = ResolveCefType(retType.ToString()) }; fn.Attributes = CodeAttributes.Public | CodeAttributes.External | CodeAttributes.Unsafe | CodeAttributes.Static; if (func.CallingConvention == CppCallingConvention.C) { fn.CustomAttributes.AddDllImportfAttribute(CallingConvention.Cdecl); } else if (func.CallingConvention == CppCallingConvention.X86StdCall) { fn.CustomAttributes.AddDllImportfAttribute(CallingConvention.StdCall); } else { throw new NotImplementedException(); } string filename = func.Span.Start.File; filename = Path.GetRelativePath(BaseDirectory, filename).Replace('\\', '/'); fn.Comments.AddVSDocComment(func.Comment, "summary"); fn.Comments.AddVSDocComment(string.Format("Defined in {0} as\n{1}", filename, func.ToString()), "remarks"); CppContainerList <CppParameter> @params = func.Parameters; for (int i = 0; i < @params.Count; i++) { CppParameter arg = @params[i]; var param = new CodeMethodParameter(arg.Name.EscapeName()); TypeDesc paramType = GetTypeDesc(arg.Type); string argType = paramType.ToString(); while (argType.StartsWith("const ")) { argType = argType.Substring(6); param.Direction = CodeMethodParameterDirection.In; } param.Type = ResolveCefType(argType); fn.Parameters.Add(param); } typeDecl.Members.Add(fn); }