Beispiel #1
0
    /// <summary>
    /// Verification of the generated code
    /// </summary>
    /// <param name="ns"></param>
    /// <remarks></remarks>

    private static void VerifyGeneratedStorage(BasicSymbolStorage ns)
    {
        NativeProcedure proc = null;

        VerifyTrue(ns.TryGetGlobalSymbol("SendMessageA", out proc));
        VerifyTrue(ns.TryGetGlobalSymbol("SendMessageW", out proc));
        VerifyTrue(ns.TryGetGlobalSymbol("GetForegroundWindow", out proc));
        VerifyTrue(ns.TryGetGlobalSymbol("CreateWellKnownSid", out proc));

        NativeTypeDef typedef = null;

        VerifyTrue(ns.TryGetGlobalSymbol("LPCSTR", out typedef));
        VerifyTrue(ns.TryGetGlobalSymbol("LPWSTR", out typedef));

        NativeType defined = null;

        VerifyTrue(ns.TryGetType("WNDPROC", out defined));
        VerifyTrue(ns.TryGetType("HOOKPROC", out defined));
        VerifyTrue(ns.TryGetType("tagPOINT", out defined));
        VerifyTrue(ns.TryGetType("_SYSTEM_INFO", out defined));

        NativeConstant c = null;

        VerifyTrue(ns.TryGetGlobalSymbol("WM_PAINT", out c));
        VerifyTrue(ns.TryGetGlobalSymbol("WM_LBUTTONDOWN", out c));
    }
Beispiel #2
0
        public CodeTypeDeclarationCollection ConvertToCodeDom(NativeConstant c, ErrorProvider ep)
        {
            NativeSymbolBag bag = new NativeSymbolBag(_storage);

            bag.AddConstant(c);
            return(ConvertBagToCodeDom(bag, ep));
        }
Beispiel #3
0
 private void WriteConstant(NativeConstant c)
 {
     _writer.WriteNameKind(NativeNameKind.Constant);
     _writer.WriteString(c.Name);
     _writer.WriteString(c.Value.Expression);
     _writer.WriteInt32((int)c.ConstantKind);
 }
Beispiel #4
0
        /// <summary>
        /// Calculate the type of the specified constant.  It's possible for a C++ constant to refer
        /// to itself which is strange but legal.  Imagine the following.
        ///
        /// #define A A
        ///
        /// In this case the type is indermenistic so we choose Object.  To detect this we record the
        /// objects we are currently evaluating for types and whenever we recursively hit one, return
        /// Object.  This prevents a mutually exclusive scenario
        /// </summary>
        /// <param name="nConst"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private CodeTypeReference CalculateConstantType(NativeConstant nConst)
        {
            if (nConst.Value == null)
            {
                return(new CodeTypeReference(typeof(int)));
            }
            else if (_typeMap.ContainsKey(nConst.Name))
            {
                return(new CodeTypeReference(typeof(object)));
            }

            _typeMap.Add(nConst.Name, nConst);
            try
            {
                CodeExpression    codeExpr = null;
                CodeTypeReference codeType = null;
                Exception         ex       = null;
                if (!TryGenerateValueExpression(nConst.Value, out codeExpr, out codeType, out ex))
                {
                    codeType = new CodeTypeReference(typeof(int));
                }

                return(codeType);
            }
            finally
            {
                _typeMap.Remove(nConst.Name);
            }
        }
Beispiel #5
0
        public void Empty()
        {
            NativeConstant c1 = new NativeConstant("c1");

            Assert.Equal(ConstantKind.Macro, c1.ConstantKind);
            Assert.Equal("c1", c1.Name);
        }
Beispiel #6
0
        public string ConvertToPInvokeCode(NativeConstant c)
        {
            ErrorProvider ep = new ErrorProvider();
            CodeTypeDeclarationCollection col = ConvertToCodeDom(c, ep);

            return(ConvertCodeDomToPInvokeCodeImpl(col, ep));
        }
Beispiel #7
0
        public void Value1()
        {
            NativeConstant c1 = new NativeConstant("p", "1+2");

            Assert.Equal(ConstantKind.Macro, c1.ConstantKind);
            Assert.Equal("1+2", c1.Value.Expression);
            Assert.Equal("p", c1.Name);
        }
Beispiel #8
0
        private NativeGlobalSymbol ImportConstant()
        {
            var name     = _reader.ReadString();
            var value    = _reader.ReadString();
            var kind     = (ConstantKind)_reader.ReadInt32();
            var constant = new NativeConstant(name, value, kind);

            return(new NativeGlobalSymbol(constant));
        }
Beispiel #9
0
        public void Method1()
        {
            string         sig = "(x) x+1";
            NativeConstant c1  = new NativeConstant("c1", sig, ConstantKind.MacroMethod);

            Assert.Equal(ConstantKind.MacroMethod, c1.ConstantKind);
            Assert.Equal("\"" + sig + "\"", c1.Value.Expression);
            Assert.Equal("c1", c1.Name);
        }
Beispiel #10
0
        internal FieldDeclarationSyntax GenerateConstantValue(NativeConstant constant)
        {
            var declarator = SyntaxFactory.VariableDeclarator(
                SyntaxFactory.Identifier(constant.Name),
                SyntaxFactory.BracketedArgumentList(),
                SyntaxFactory.EqualsValueClause(GenerateExpression(constant.Value)));

            var declaration = SyntaxFactory.VariableDeclaration(
                SyntaxFactory.ParseTypeName("int"),
                SyntaxFactory.SeparatedList(new[] { declarator }));

            return(SyntaxFactory.FieldDeclaration(declaration));
        }
        public void FindOrLoad3()
        {
            var ns = new BasicSymbolStorage();

            ns.AddConstant(new NativeConstant("c1", "value"));
            NativeSymbolBag bag = new NativeSymbolBag(ns);

            NativeConstant c = null;

            Assert.False(bag.Storage.TryGetGlobalSymbol("c1", out c));
            Assert.True(bag.TryGetGlobalSymbol("c1", out c));
            Assert.True(bag.TryGetGlobalSymbol("c1", out c));
        }
        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());
        }
Beispiel #13
0
        internal FieldDeclarationSyntax GenerateConstant(NativeConstant constant)
        {
            switch (constant.ConstantKind)
            {
            case ConstantKind.Macro:
                return(GenerateConstantValue(constant));

            case ConstantKind.MacroMethod:
                throw new NotImplementedException();

            default:
                throw Contract.InvalidEnum(constant.ConstantKind);
            }
        }
        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());
        }
Beispiel #16
0
            public static bool IsValidConstant(NativeConstant constant)
            {
                if (constant.ConstantKind == ConstantKind.MacroMethod)
                {
                    return(false);
                }

                Parser.ExpressionParser p    = new Parser.ExpressionParser();
                Parser.ExpressionNode   node = null;
                if (!p.TryParse(constant.Value.Expression, out node))
                {
                    return(false);
                }

                return(!ContainsInvalidKind(node));
            }
        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());
        }
Beispiel #18
0
        public void SaveAndLoad5()
        {
            NativeConstant c1 = new NativeConstant("c1", "v1");
            NativeConstant c2 = new NativeConstant("c2", "v2", ConstantKind.MacroMethod);
            var            ns = new BasicSymbolStorage();

            ns.AddConstant(c1);
            ns.AddConstant(c2);

            NativeConstant ret = null;

            Assert.True(ns.TryGetGlobalSymbol("c1", out ret));
            Assert.Equal("c1", ret.Name);
            Assert.Equal("v1", ret.Value.Expression);
            Assert.Equal(ConstantKind.Macro, ret.ConstantKind);

            Assert.True(ns.TryGetGlobalSymbol("c2", out ret));
            Assert.Equal("c2", ret.Name);
            Assert.Equal("\"v2\"", ret.Value.Expression);
            Assert.Equal(ConstantKind.MacroMethod, ret.ConstantKind);
        }
Beispiel #19
0
 public static void AddConstant(this INativeSymbolStorage storage, NativeConstant constant) => storage.Add(new NativeGlobalSymbol(constant));
 public NativeGlobalSymbol(NativeConstant constant) : this(constant.NativeName, constant)
 {
 }