public void BagSaveAndLoad1() { NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeBoolean); NativeTypeDef td = new NativeTypeDef("LPWSTR", new NativePointer(BuiltinType.NativeWChar)); p1.Signature.Parameters.Add(new NativeParameter("param1", new NativeNamedType("LPWSTR", td))); Assert.Equal("boolean p1(LPWSTR param1)", p1.DisplayName); Assert.Equal("p1(Sig(boolean)(Sal)(param1(LPWSTR(LPWSTR(*(wchar))))(Sal)))", SymbolPrinter.Convert(p1)); var ns = new BasicSymbolStorage(); ns.AddProcedure(p1); ns.AddTypeDef(td); NativeSymbolBag bag = new NativeSymbolBag(ns); NativeProcedure ret1 = null; Assert.True(bag.TryGetGlobalSymbol("p1", out ret1)); bag.AddProcedure(ret1); Assert.True(bag.TryResolveSymbolsAndValues()); Assert.Equal(SymbolPrinter.Convert(p1), SymbolPrinter.Convert(ret1)); }
private static BasicSymbolStorage Generate(TextWriter writer) { NativeCodeAnalyzer analyzer = NativeCodeAnalyzerFactory.Create(OsVersion.WindowsVista); analyzer.IncludePathList.AddRange(NativeCodeAnalyzerFactory.GetCommonSdkPaths()); // Run the preprocessor analyzer.Trace = true; string winPath = Path.Combine(PInvoke.Parser.NativeCodeAnalyzerFactory.GetPlatformSdkIncludePath(), "windows.h"); TextReaderBag tr = analyzer.RunPreProcessor(winPath); File.WriteAllText("d:\\temp\\windows.out.h", tr.TextReader.ReadToEnd()); analyzer.Trace = false; NativeCodeAnalyzerResult result = analyzer.Analyze(winPath); ErrorProvider ep = result.ErrorProvider; if (ep.Errors.Count > 0) { Debug.Fail("Encountered an error during the parse"); } NativeSymbolBag bag = NativeSymbolBag.CreateFrom(result, CreateInitialBasicSymbolStorage(), ep); // Resolve with the full dll list using (ProcedureFinder finder = new ProcedureFinder(FullDllList)) { bag.TryResolveSymbolsAndValues(finder, ep); } foreach (string msg in ep.AllMessages) { writer.WriteLine("' " + msg); } // GenerateCode(writer, bag) // Now write out the file var ns = new BasicSymbolStorage(); bag.SaveToNativeStorage(ns); VerifyGeneratedStorage(ns); // TODO: need to write to file again otherwise it's just in memory. // Copy the file to the various applications File.Copy("windows.xml", "..\\..\\..\\ConsoleTool\\bin\\Debug\\windows.xml", true); File.Copy("windows.xml", "..\\..\\Data\\windows.xml", true); string fullInstallTarget = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), Path.Combine(PInvoke.Constants.ProductName, "Data\\windows.xml")); if (File.Exists(fullInstallTarget)) { File.Copy("windows.xml", fullInstallTarget, true); } return(ns); }
public static void VerifyConstValue(LanguageType lang, NativeSymbolBag bag, string name, string val, string type) { Assert.True(bag.TryResolveSymbolsAndValues()); BasicConverter con = new BasicConverter(lang, StorageFactory.CreateStandard()); CodeTypeDeclarationCollection col = con.ConvertToCodeDom(bag, new ErrorProvider()); VerifyConstValue(col, lang, name, val, type); }
public void Value1() { NativeSymbolBag bag = new NativeSymbolBag(); bag.AddConstant(new NativeConstant("foo", "1")); bag.AddConstant(new NativeConstant("bar", "foo+2")); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
/// <summary> /// Core conversion routine. All code should just go through this /// </summary> /// <param name="bag"></param> /// <param name="ep"></param> /// <returns></returns> /// <remarks></remarks> private CodeTypeDeclarationCollection ConvertBagToCodeDom(NativeSymbolBag bag, ErrorProvider ep) { ThrowIfNull(bag); ThrowIfNull(ep); // Make sure than all of the referenced NativeDefinedType instances are in the correct // portion of the bag ChaseReferencedDefinedTypes(bag); // First step is to resolve the symbols bag.TryResolveSymbolsAndValues(ep); // Create the codedom transform CodeTransform transform = new CodeTransform(LanguageType, bag); MarshalTransform marshalUtil = new MarshalTransform(LanguageType, bag, TransformKindFlags); CodeTypeDeclarationCollection col = new CodeTypeDeclarationCollection(); // Only output the constants if there are actually any List <NativeConstant> list = new List <NativeConstant>(bag.FindResolvedConstants()); if (list.Count > 0) { CodeTypeDeclaration constCtd = transform.GenerateConstants(list); if (constCtd.Members.Count > 0) { col.Add(constCtd); } } foreach (NativeDefinedType definedNt in bag.FindResolvedDefinedTypes()) { CodeTypeDeclaration ctd = transform.GenerateDeclaration(definedNt); marshalUtil.Process(ctd); col.Add(ctd); } List <NativeProcedure> procList = new List <NativeProcedure>(bag.FindResolvedProcedures()); if (procList.Count > 0) { CodeTypeDeclaration procType = transform.GenerateProcedures(procList); marshalUtil.Process(procType); col.Add(procType); } // Add the helper types that we need AddHelperTypes(col); // Next step is to run the pretty lister on it CodeDomPrettyList prettyLister = new CodeDomPrettyList(bag); prettyLister.PerformRename(col); return(col); }
public void Opaque2() { NativeNamedType named = new NativeNamedType("foo"); NativePointer ptr = new NativePointer(named); NativeTypeDef td = new NativeTypeDef("FOOBAR", ptr); NativeSymbolBag bag = new NativeSymbolBag(); bag.AddTypeDef(td); Assert.Equal(1, bag.FindUnresolvedNativeSymbolRelationships().Count); Assert.False(bag.TryResolveSymbolsAndValues()); }
public void Value2() { NativeSymbolBag bag = new NativeSymbolBag(); NativeEnum ntEnum = new NativeEnum("Enum1"); ntEnum.AddValue("v1", "1"); ntEnum.AddValue("v2", "v1+1"); bag.AddDefinedType(ntEnum); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
internal SyntaxTree Generate(NativeSymbolBag bag, ErrorProvider ep) { // Make sure than all of the referenced NativeDefinedType instances are in the correct // portion of the bag ChaseReferencedDefinedTypes(bag); // First step is to resolve the symbols bag.TryResolveSymbolsAndValues(ep); // Create the codedom transform var transform = new CodeTransform(_langaugeType, bag); var marshalUtil = new MarshalTransform(_langaugeType, bag, TransformKindFlags.All); var col = new CodeTypeDeclarationCollection(); // Only output the constants if there are actually any var constList = bag.FindResolvedConstants().ToList(); if (constList.Count > 0) { var constCtd = transform.GenerateConstants(constList); if (constCtd.Members.Count > 0) { col.Add(constCtd); } } foreach (var definedNt in bag.FindResolvedDefinedTypes()) { var ctd = transform.GenerateDeclaration(definedNt); marshalUtil.Process(ctd); col.Add(ctd); } var procList = bag.FindResolvedProcedures().ToList(); if (procList.Count > 0) { var procType = transform.GenerateProcedures(procList); marshalUtil.Process(procType); col.Add(procType); } // Add the helper types that we need AddHelperTypes(col); // Next step is to run the pretty lister on it var prettyLister = new CodeDomPrettyList(bag); prettyLister.PerformRename(col); var code = BasicConverter.ConvertCodeDomToPInvokeCodeImpl(_langaugeType, col, ep); return(CSharpSyntaxTree.ParseText(code)); }
public void Proc1() { NativeSymbolBag bag = new NativeSymbolBag(); NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeDouble); bag.AddProcedure(p1); Assert.True(bag.TryResolveSymbolsAndValues()); }
public void ValueFromStorage1() { var ns = new BasicSymbolStorage(); ns.AddConstant(new NativeConstant("c1", "1")); var bag = new NativeSymbolBag(ns); bag.AddConstant(new NativeConstant("c2", "5+c1")); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
public void Proc4() { NativeSymbolBag bag = new NativeSymbolBag(); NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeNamedType("foo"); p1.Signature.Parameters.Add(new NativeParameter("param1", new NativeBuiltinType(BuiltinType.NativeDouble))); bag.AddProcedure(p1); Assert.False(bag.TryResolveSymbolsAndValues()); }
public void Value4() { NativeSymbolBag bag = new NativeSymbolBag(); NativeConstant ntConst1 = new NativeConstant("c1", "1"); bag.AddConstant(ntConst1); NativeConstant ntConst2 = new NativeConstant("c2", "5+c1"); bag.AddConstant(ntConst2); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
public void Proc6() { NativeSymbolBag bag = new NativeSymbolBag(); NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeInt32, true); p1.Signature.Parameters.Add(new NativeParameter("param1", new NativeNamedType("foo"))); bag.AddProcedure(p1); bag.AddTypeDef(new NativeTypeDef("foo", new NativeBuiltinType(BuiltinType.NativeFloat))); Assert.True(bag.TryResolveSymbolsAndValues()); }
public void Opaque1() { NativeNamedType named = new NativeNamedType("struct", "foo"); NativePointer ptr = new NativePointer(named); NativeTypeDef td = new NativeTypeDef("FOOBAR", ptr); NativeSymbolBag bag = new NativeSymbolBag(); bag.AddTypeDef(td); Assert.Equal(1, bag.FindUnresolvedNativeSymbolRelationships().Count); Assert.True(bag.TryResolveSymbolsAndValues()); Assert.NotNull(named.RealType); Assert.Equal(NativeSymbolKind.OpaqueType, named.RealType.Kind); }
public void ValueFromStorage3() { var ns = new BasicSymbolStorage(); NativeSymbolBag bag = new NativeSymbolBag(ns); ns.AddDefinedType(new NativeStruct("s1")); NativeConstant ntConst1 = new NativeConstant("c1", "(s1)1"); bag.AddConstant(ntConst1); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
public void Value3() { NativeSymbolBag bag = new NativeSymbolBag(); NativeStruct ntStruct1 = new NativeStruct("s1"); bag.AddDefinedType(ntStruct1); NativeConstant ntConst1 = new NativeConstant("c1", "(s1)1"); bag.AddConstant(ntConst1); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
public void ResolveLoad1() { var ns = new BasicSymbolStorage(); ns.AddDefinedType(new NativeStruct("s1")); NativeSymbolBag bag = new NativeSymbolBag(ns); bag.AddTypeDef(new NativeTypeDef("S1", "s1")); Assert.True(bag.TryResolveSymbolsAndValues()); NativeDefinedType td = null; Assert.True(bag.TryGetGlobalSymbol("s1", out td)); }
public void Resolve1() { NativeSymbolBag bag = new NativeSymbolBag(); NativeStruct s1 = new NativeStruct("s1"); bag.AddDefinedType(s1); NativeTypeDef td1 = new NativeTypeDef("td1"); NativeNamedType n1 = new NativeNamedType("s1"); td1.RealType = n1; bag.AddTypeDef(td1); Assert.True(bag.TryResolveSymbolsAndValues()); Assert.Same(s1, n1.RealType); }
/// <summary> /// Make sure that any NativeDefinedType referenced is in the bag. That way if we /// have structures which point to other NativeDefinedType instances, they are automagically /// put into the bag /// </summary> /// <param name="bag"></param> /// <remarks></remarks> private void ChaseReferencedDefinedTypes(NativeSymbolBag bag) { bag.TryResolveSymbolsAndValues(); foreach (NativeSymbol sym in bag.FindAllReachableNativeSymbols()) { if (NativeSymbolCategory.Defined == sym.Category) { NativeDefinedType defined = null; if (!bag.TryGetGlobalSymbol(sym.Name, out defined)) { bag.AddDefinedType((NativeDefinedType)sym); } } } }
public void ValueFromStorage2() { var ns = new BasicSymbolStorage(); var bag = new NativeSymbolBag(ns); NativeEnum ntEnum = new NativeEnum("e1"); ntEnum.AddValue("v1", "5"); bag.AddDefinedType(ntEnum); NativeConstant ntConst1 = new NativeConstant("c1", "5+v1"); bag.AddConstant(ntConst1); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
public void ResolveLoad2() { var ns = new BasicSymbolStorage(); ns.AddTypeDef(new NativeTypeDef("TEST_INT", BuiltinType.NativeInt32)); NativeStruct s1 = new NativeStruct("s1"); s1.Members.Add(new NativeMember("m1", new NativeNamedType("TEST_INT"))); NativeSymbolBag bag = new NativeSymbolBag(ns); bag.AddDefinedType(s1); Assert.True(bag.TryResolveSymbolsAndValues()); NativeTypeDef td = null; Assert.True(bag.TryGetGlobalSymbol("TEST_INT", out td)); }
public static void VerifyEnumValue(LanguageType lang, NativeSymbolBag bag, NativeEnum e, string name, string val) { Assert.True(bag.TryResolveSymbolsAndValues()); BasicConverter con = new BasicConverter(lang, StorageFactory.CreateStandard()); CodeTypeDeclarationCollection col = con.ConvertToCodeDom(bag, new ErrorProvider()); // Look for the constants class CodeTypeDeclaration ctd = null; foreach (CodeTypeDeclaration cur in col) { if (0 == string.CompareOrdinal(e.Name, cur.Name)) { ctd = cur; break; } } Assert.NotNull(ctd); // Find the value CodeTypeMember cMem = null; foreach (CodeTypeMember mem in ctd.Members) { if (0 == string.CompareOrdinal(name, mem.Name)) { cMem = mem; break; } } Assert.NotNull(cMem); // Make sure it's a constant value CodeMemberField field = cMem as CodeMemberField; Assert.NotNull(field); // Get the provider var provider = default(CodeDomProvider); switch (lang) { case LanguageType.CSharp: provider = new Microsoft.CSharp.CSharpCodeProvider(); break; case LanguageType.VisualBasic: provider = new Microsoft.VisualBasic.VBCodeProvider(); break; default: provider = null; break; } using (var writer = new StringWriter()) { provider.GenerateCodeFromExpression(field.InitExpression, writer, new CodeGeneratorOptions()); Assert.Equal(val, writer.ToString()); } }