private void PreprocessDefinition() { Definition.ApplyTypeSubstitutions(Settings.TargetId); Definition.ApplyNamespaceSubsitutions(Settings.TargetId); foreach (var type in Definition.AllTypes) { if (type.Origin != TypeOrigin.Mapped) { if (type.IsObject && type.Origin != TypeOrigin.Managed && type.Assembly == Definition.Assembly) { AllGeneratedClasses.Add((AST.Object)type); } else if (type.IsStruct) { AllGeneratedStructs.Add((AST.Struct)type); } else if (type.IsEnum && type.Origin != TypeOrigin.Managed && type.Assembly == Definition.Assembly) { AllGeneratedEnums.Add((AST.Enum)type); } else if (type.IsDelegate && !((AST.Delegate)type).IsGeneric) { AllGeneratedDelegates.Add((AST.Delegate)type); } } } foreach (var type in Definition.AllTypes) { if (type.IsDelegate) { AllTranslatedDelegates.Add((AST.Delegate)type); } else if (type.IsStruct && CppRender.RequiresABITranslation(type.ToVariable())) { AllTranslatedStructs.Add((AST.Struct)type); } } Definition.StripAllGluonAttributes(); Definition.PutDefaultConstructorsFirst(); Definition.InsertDefaultConstructorsWhereNoneAreDefined(); var builder = new AST.Builder(Definition); _assemblyNS = builder.Resolve(Definition.Assembly.Name); _gluonNS = builder.Resolve("Gluon"); builder.Resolve(typeof(DllImportAttribute)); builder.Resolve(typeof(MarshalAsAttribute)); builder.Resolve(typeof(GluonObject)); builder.Resolve(typeof(DelegateBlob)); }
public void GenerateNativeDecl(CsTreeWriter file) { file.Namespace(_assemblyNS, true, () => { file.Line("public static class Native"); file.Block(() => { file.Line(@"static Native()"); file.Block(() => { foreach (var type in AllGeneratedClasses.Where(t => t.Origin != TypeOrigin.Managed && t.Origin != TypeOrigin.Native && !t.IsAbstract)) { file.Line("GluonObject.RegisterType<global::{0}>(new Guid(\"{1}\"), native => new global::{0}(new AbiPtr(native)));", type.FullName("."), type.PrivateId); } }); file.Line( @"public const string DllPath = ""{0}""; [DllImport(DllPath, EntryPoint = ""$_GetGluonExceptionDetails"")] private static extern int GetGluonExceptionDetails(out int outHR, out ExceptionType outType, out IntPtr outText); internal static void RegisterTypes() {{ }} public static void Throw(int hr) {{ if (hr == (int)HResult.S_OK) return; int checkhr; ExceptionType type; IntPtr msgPtr; string msg; var chk = GetGluonExceptionDetails(out checkhr, out type, out msgPtr); if (chk != 0 || hr != checkhr) throw new GluonExceptionAssertionFail(); msg = Marshal.PtrToStringAnsi(msgPtr); throw Translate.Exception(type, msg); }}", Settings.ImportedDll, Definition.Assembly.Name.Replace('.', '_')); }); }); }