コード例 #1
0
        public CodeTypeDeclarationCollection ConvertToCodeDom(NativeConstant c, ErrorProvider ep)
        {
            NativeSymbolBag bag = new NativeSymbolBag(_storage);

            bag.AddConstant(c);
            return(ConvertBagToCodeDom(bag, ep));
        }
コード例 #2
0
        public void Gen14()
        {
            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddConstant(new NativeConstant("A", "L'\\10'"));
            VerifyConstValue(LanguageType.CSharp, bag, "A", "'\\n'", "System.Char");
        }
コード例 #3
0
        public void Invalid1()
        {
            NativeSymbolBag bag = new NativeSymbolBag(StorageFactory.CreateStandard());

            bag.AddConstant(new NativeConstant("c1", "(S1)5"));
            VerifyConstValue(bag, "c1", "\"(S1)5\"");
        }
コード例 #4
0
        public void Gen9()
        {
            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddConstant(new NativeConstant("A", "1.0"));
            VerifyConstValue(LanguageType.CSharp, bag, "A", "1F", "System.Single");
        }
コード例 #5
0
        public void Gen11()
        {
            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddConstant(new NativeConstant("A", "FALSE;"));
            VerifyConstValue(LanguageType.CSharp, bag, "A", "\"FALSE;\"", "System.String");
        }
コード例 #6
0
        public void Gen5()
        {
            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddConstant(new NativeConstant("A", "A"));
            VerifyConstValue(bag, "A", "Nothing", "System.Object");
        }
コード例 #7
0
        public void Gen8()
        {
            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddConstant(new NativeConstant("A", "0x5"));
            VerifyConstValue(LanguageType.CSharp, bag, "A", "5");
        }
コード例 #8
0
        public CodeTypeDeclarationCollection ConvertToCodeDom(NativeDefinedType definedNt, ErrorProvider ep)
        {
            NativeSymbolBag bag = new NativeSymbolBag(_storage);

            bag.AddDefinedType(definedNt);
            return(ConvertBagToCodeDom(bag, ep));
        }
コード例 #9
0
        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));
        }
コード例 #10
0
        /// <summary>
        /// Convert the block of native code into a CodeDom hierarchy
        /// </summary>
        /// <param name="code"></param>
        /// <param name="ep"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public CodeTypeDeclarationCollection ConvertNativeCodeToCodeDom(string code, ErrorProvider ep)
        {
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }

            if (ep == null)
            {
                throw new ArgumentNullException("ep");
            }

            var analyzer = NativeCodeAnalyzerFactory.CreateForMiniParse(OsVersion.WindowsVista, _storage.GetAllMacros());

            // TODO: probably should delete this
            analyzer.IncludePathList.Add("c:\\program files (x86)\\windows kits\\8.1\\include\\shared");
            NativeSymbolBag bag = default(NativeSymbolBag);

            using (System.IO.StringReader reader = new StringReader(code))
            {
                NativeCodeAnalyzerResult result = analyzer.Analyze(reader);

                ep.Append(result.ErrorProvider);
                bag = NativeSymbolBag.CreateFrom(result, Storage);
            }

            return(ConvertBagToCodeDom(bag, ep));
        }
コード例 #11
0
        public void Gen1()
        {
            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddConstant(new NativeConstant("C1", "1"));
            bag.AddConstant(new NativeConstant("C2", "1+C1"));
            VerifyConstValue(bag, "C2", string.Format("(1 + {0}.C1)", TransformConstants.NativeConstantsName));
        }
コード例 #12
0
        public void Storage1()
        {
            NativeSymbolBag bag = new NativeSymbolBag(StorageFactory.CreateStandard());

            NativeType nt = null;

            Assert.True(bag.TryGetType("RecursiveStruct", out nt));
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: vsliouniaev/pinvoke
    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);
    }
コード例 #14
0
        public void Invalid2()
        {
            NativeSymbolBag bag = new NativeSymbolBag(StorageFactory.CreateStandard());
            NativeEnum      e1  = new NativeEnum("e1");

            e1.AddValue("v1", "(S1)5");
            bag.AddDefinedType(e1);
            VerifyEnumValue(bag, e1, "v1", "\"(S1)5\"");
        }
コード例 #15
0
        public void Gen6()
        {
            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddConstant(new NativeConstant("A", "B"));
            bag.AddConstant(new NativeConstant("B", "A"));
            VerifyConstValue(bag, "A", string.Format("{0}.B", TransformConstants.NativeConstantsName));
            VerifyConstValue(bag, "B", string.Format("{0}.A", TransformConstants.NativeConstantsName));
        }
コード例 #16
0
ファイル: Utilities.cs プロジェクト: vsliouniaev/pinvoke
        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);
        }
コード例 #17
0
 private void m_okBtn_Click(object sender, EventArgs e)
 {
     DialogResult = DialogResult.OK;
     if (_searchGrid != null)
     {
         _bag = _searchGrid.SelectedSymbolBag;
     }
     Close();
 }
コード例 #18
0
        public void AddTypeDef3()
        {
            NativeSymbolBag bag = new NativeSymbolBag();
            NativeTypeDef   td1 = new NativeTypeDef("td1");
            NativeTypeDef   td2 = new NativeTypeDef("td1");

            bag.AddTypeDef(td1);
            Assert.Throws <ArgumentException>(() => bag.AddTypeDef(td2));
        }
コード例 #19
0
        public void AddDefinedTypeTest3()
        {
            NativeSymbolBag bag        = new NativeSymbolBag();
            NativeStruct    definedNt1 = new NativeStruct("s1");
            NativeStruct    definedNt2 = new NativeStruct("s1");

            bag.AddDefinedType(definedNt1);
            Assert.Throws <ArgumentException>(() => bag.AddDefinedType(definedNt2));
        }
コード例 #20
0
        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());
        }
コード例 #21
0
        /// <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);
        }
コード例 #22
0
        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());
        }
コード例 #23
0
        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());
        }
コード例 #24
0
ファイル: CodeConverter.cs プロジェクト: vsliouniaev/pinvoke
        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));
        }
コード例 #25
0
        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());
        }
コード例 #26
0
        public void Gen4()
        {
            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddConstant(new NativeConstant("C1", "42"));
            NativeEnum e1 = new NativeEnum("e1");

            e1.AddValue("v1", "C1+2");
            bag.AddDefinedType(e1);
            VerifyEnumValue(bag, e1, "v1", string.Format("({0}.C1 + 2)", TransformConstants.NativeConstantsName));
        }
コード例 #27
0
        public void Gen2()
        {
            NativeSymbolBag bag = new NativeSymbolBag();
            NativeEnum      e1  = new NativeEnum("e1");

            e1.AddValue("v1", "2");
            e1.AddValue("v2", "v1+1");
            bag.AddEnumAndValues(e1);
            VerifyEnumValue(bag, e1, "v1", "2");
            VerifyEnumValue(bag, e1, "v2", "(e1.v1 + 1)");
        }
コード例 #28
0
        public void AddTypeDef1()
        {
            NativeSymbolBag bag = new NativeSymbolBag();
            NativeTypeDef   td1 = new NativeTypeDef("td1");

            bag.AddTypeDef(td1);

            NativeType ret1 = null;

            Assert.True(bag.TryGetType(td1.DisplayName, out ret1));
            Assert.Same(ret1, td1);
        }
コード例 #29
0
        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());
        }
コード例 #30
0
        public void AnonymousType1()
        {
            NativeStruct nt = new NativeStruct();

            nt.IsAnonymous = true;
            Assert.False(string.IsNullOrEmpty(nt.Name));

            NativeSymbolBag bag = new NativeSymbolBag();

            bag.AddDefinedType(nt);
            Assert.True(NativeSymbolBag.IsAnonymousName(nt.Name));
        }