Пример #1
0
 public override void Execute(ParserStateValues state, string trimmedLine)
 {
     if (trimmedLine.StartsWith(".class", StringComparison.Ordinal))
     {
         state.State            = ParserState.ClassDeclaration;
         state.AddLine          = true;
         state.ClassDeclaration = trimmedLine;
     }
     else if (trimmedLine.StartsWith(".method", StringComparison.Ordinal))
     {
         ExportedClass exportedClass;
         if (state.ClassNames.Count == 0 || !this.Parser.Exports.ClassesByName.TryGetValue(state.ClassNames.Peek(), out exportedClass))
         {
             return;
         }
         state.Method.Reset();
         state.Method.Declaration = trimmedLine;
         state.AddLine            = false;
         state.State = ParserState.MethodDeclaration;
     }
     else
     {
         if (!trimmedLine.StartsWith("} // end of class", StringComparison.Ordinal))
         {
             return;
         }
         state.ClassNames.Pop();
         state.State = state.ClassNames.Count > 0 ? ParserState.Class : ParserState.Normal;
     }
 }
        private string ExtractPinvokeimpl(ParserStateValues state)
        {
            string data = state?.Method?.Result;

            if (String.IsNullOrWhiteSpace(data))
            {
                return(null);
            }

            const string sign = "pinvokeimpl";

            int op = data.IndexOf(sign, StringComparison.InvariantCulture);

            if (op == -1)
            {
                return(null);
            }

            int ed = data.IndexOf(')', op);

            if (ed == -1)
            {
                throw new MissingMethodException($"Failed {sign} signature.");
            }

            return(data.Substring(op, ed - op + 1));
        }
Пример #3
0
 public override void Execute(ParserStateValues state, string trimmedLine)
 {
     if (trimmedLine.StartsWith(".corflags", StringComparison.Ordinal))
     {
         state.Result.Add(string.Format((IFormatProvider)CultureInfo.InvariantCulture, ".corflags 0x{0}", new object[1]
         {
             (object)Utilities.GetCoreFlagsForPlatform(state.Cpu).ToString("X8", (IFormatProvider)CultureInfo.InvariantCulture)
         }));
         state.AddLine = false;
     }
     else if (trimmedLine.StartsWith(".class", StringComparison.Ordinal))
     {
         state.State            = ParserState.ClassDeclaration;
         state.AddLine          = true;
         state.ClassDeclaration = trimmedLine;
     }
     else
     {
         string assemblyName;
         string aliasName;
         if (!this.IsExternalAssemblyReference(trimmedLine, out assemblyName, out aliasName))
         {
             return;
         }
         state.RegisterMsCorelibAlias(assemblyName, aliasName);
     }
 }
Пример #4
0
        private bool TreatIL(ParserStateValues state, ref string raw)
        {
            if ((Parser.InputValues.Patches & PatchesType.InfToken) == PatchesType.InfToken)
            {
                // ldc.r8     inf
                // ldc.r8     -inf
                // ldc.r4     inf
                // ldc.r4     -inf

                Match m = Regex.Match
                          (
                    raw,
                    @"
                        ldc.r(?:(?'x64'8)|4)
                        \s*
                        (?'sign'-?)
                        inf
                    ",
                    RegexOptions.IgnorePatternWhitespace
                          );

                if (m.Success)
                {
                    raw = new string(' ', 4) + raw.Substring(0, m.Index) + GetFloatDef(m);
                    return(true);
                }
            }

            return(false);
        }
        private static string GetClassName(ParserStateValues state)
        {
            bool          hadClassName     = false;
            StringBuilder classNameBuilder = new StringBuilder(state.ClassDeclaration.Length);

            IlParsingUtils.ParseIlSnippet(state.ClassDeclaration, ParsingDirection.Forward, (Func <IlParsingUtils.IlSnippetLocation, bool>)(s =>
            {
                if (s.WithinString)
                {
                    hadClassName = true;
                    if (s.CurrentChar != '\'')
                    {
                        classNameBuilder.Append(s.CurrentChar);
                    }
                }
                else if (hadClassName)
                {
                    if (s.CurrentChar == '.' || s.CurrentChar == '/')
                    {
                        classNameBuilder.Append(s.CurrentChar);
                    }
                    else if (s.CurrentChar != '\'')
                    {
                        return(false);
                    }
                }
                return(true);
            }), (Action <IlParsingUtils.IlSnippetFinalizaton>)null);
            return(classNameBuilder.ToString());
        }
Пример #6
0
        /// <param name="state"></param>
        /// <param name="raw">raw definition of the .field</param>
        /// <returns>true if processed</returns>
        private bool TreatField(ParserStateValues state, ref string raw)
        {
            if ((Parser.InputValues.Patches & PatchesType.InfToken) == PatchesType.InfToken)
            {
                // .field public static literal float32 'Infinity' = float32(inf)
                // .field public static literal float32 'NegativeInfinity' = float32(-inf)
                // .field public static literal float64 'Infinity' = float64(inf)
                // .field public static literal float64 'NegativeInfinity' = float64(-inf)

                Match m = Regex.Match
                          (
                    raw,
                    @"=\s*
                        float(?:(?'x64'64)|32)
                        \(
                            (?'sign'-?)
                            inf
                        \)
                    ",
                    RegexOptions.IgnorePatternWhitespace
                          );

                if (m.Success)
                {
                    raw = new string(' ', 2) + raw.Substring(0, m.Index) + GetFloatDef(m);
                    return(true);
                }
            }

            return(false);
        }
Пример #7
0
 public override void Execute(ParserStateValues state, string trimmedLine)
 {
     if (!trimmedLine.StartsWith("} // end of method", StringComparison.Ordinal))
     {
         return;
     }
     state.State = ParserState.Class;
 }
        private bool GetIsExport(ParserStateValues state, out ExportedClass exportedClass)
        {
            if (!this.ExtractMethodParts(state))
            {
                exportedClass = (ExportedClass)null;
                return(false);
            }
            bool flag1 = this.Exports.ClassesByName.TryGetValue(state.ClassNames.Peek(), out exportedClass) && exportedClass != null;
            List <ExportedMethod> exportedMethodList1 = (List <ExportedMethod>)null;

            if (flag1 && exportedClass.HasGenericContext)
            {
                if (exportedClass.MethodsByName.TryGetValue(state.Method.Name, out exportedMethodList1))
                {
                    exportedMethodList1.ForEach((Action <ExportedMethod>)(method => this.Notify(2, DllExportLogginCodes.ExportInGenericType, Resources.The_type_1_cannot_export_the_method_2_as_0_because_it_is_generic_or_is_nested_within_a_generic_type, (object)method.ExportName, (object)method.ExportedClass.FullTypeName, (object)method.MemberName)));
                }
                return(false);
            }
            bool flag2 = flag1 && exportedClass.MethodsByName.TryGetValue(state.Method.Name, out exportedMethodList1);

            if (flag1 && !flag2)
            {
                ExportedMethod duplicateExport = this.Exports.GetDuplicateExport(exportedClass.FullTypeName, state.Method.Name);
                this.ValidateExportNameAndLogError(duplicateExport, state);
                if (duplicateExport != null)
                {
                    this.Notify(state, 1, DllExportLogginCodes.AmbigiguousExportName, Resources.Ambiguous_export_name_0_on_1_2_, (object)duplicateExport.ExportName, (object)duplicateExport.ExportedClass.FullTypeName, (object)duplicateExport.MemberName);
                }
            }
            else
            {
                List <ExportedMethod> exportedMethodList2;
                if ((exportedMethodList2 = exportedMethodList1 ?? exportedClass.NullSafeCall <ExportedClass, List <ExportedMethod> >((Func <ExportedClass, List <ExportedMethod> >)(i => i.Methods))) != null)
                {
                    foreach (ExportedMethod exportedMethod in exportedMethodList2)
                    {
                        if (!exportedMethod.IsStatic)
                        {
                            flag2 = false;
                            this.Notify(state, 2, DllExportLogginCodes.MethodIsNotStatic, Resources.The_method_1_2_is_not_static_export_name_0_, (object)exportedMethod.ExportName, (object)exportedMethod.ExportedClass.FullTypeName, (object)exportedMethod.MemberName);
                        }
                        if (exportedMethod.IsGeneric)
                        {
                            flag2 = false;
                            this.Notify(state, 2, DllExportLogginCodes.ExportOnGenericMethod, Resources.The_method_1_2_is_generic_export_name_0_Generic_methods_cannot_be_exported_, (object)exportedMethod.ExportName, (object)exportedMethod.ExportedClass.FullTypeName, (object)exportedMethod.MemberName);
                        }
                    }
                }
            }
            return(flag2);
        }
 public override void Execute(ParserStateValues state, string trimmedLine)
 {
     if (trimmedLine.StartsWith(".custom", StringComparison.Ordinal) || trimmedLine.StartsWith("// Code", StringComparison.Ordinal))
     {
         ExportedClass exportedClass;
         if (this.Exports.ClassesByName.TryGetValue(state.ClassNames.Peek(), out exportedClass))
         {
             ExportedMethod exportMethod  = exportedClass.MethodsByName[state.Method.Name][0];
             string         declaration   = state.Method.Declaration;
             StringBuilder  stringBuilder = new StringBuilder(250);
             stringBuilder.Append(".method ").Append(state.Method.Attributes.NullSafeTrim()).Append(" ");
             stringBuilder.Append(state.Method.Result.NullSafeTrim());
             stringBuilder.Append(" modopt(['mscorlib']'").Append(AssemblyExports.ConventionTypeNames[exportMethod.CallingConvention]).Append("') ");
             if (!string.IsNullOrEmpty(state.Method.ResultAttributes))
             {
                 stringBuilder.Append(" ").Append(state.Method.ResultAttributes);
             }
             stringBuilder.Append(" '").Append(state.Method.Name).Append("'").Append(state.Method.After.NullSafeTrim());
             bool flag = this.ValidateExportNameAndLogError(exportMethod, state);
             if (flag)
             {
                 state.Method.Declaration = stringBuilder.ToString();
             }
             if (state.MethodPos != 0)
             {
                 state.Result.Insert(state.MethodPos, state.Method.Declaration);
             }
             if (flag)
             {
                 this.Notifier.Notify(-2, DllExportLogginCodes.OldDeclaration, "\t" + Resources.OldDeclaration_0_, (object)declaration);
                 this.Notifier.Notify(-2, DllExportLogginCodes.NewDeclaration, "\t" + Resources.NewDeclaration_0_, (object)state.Method.Declaration);
                 state.Result.Add(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "    .export [{0}] as '{1}'", new object[2]
                 {
                     (object)exportMethod.VTableOffset,
                     (object)exportMethod.ExportName
                 }));
                 this.Notifier.Notify(-1, DllExportLogginCodes.AddingVtEntry, "\t" + Resources.AddingVtEntry_0_export_1_, (object)exportMethod.VTableOffset, (object)exportMethod.ExportName);
             }
         }
         else
         {
             state.AddLine = false;
         }
         state.State = ParserState.Method;
     }
     else
     {
         state.AddLine = false;
     }
 }
Пример #10
0
        public override void Execute(ParserStateValues state, string trimmedLine)
        {
            if (state.Method.Pinvokeimpl != null && trimmedLine == "}")
            {
                state.State = ParserState.Class;
                return;
            }

            if (trimmedLine.StartsWith("} // end of method", StringComparison.Ordinal))
            {
                state.State = ParserState.Class;
                return;
            }
        }
 public override void Execute(ParserStateValues state, string trimmedLine)
 {
     if (trimmedLine.StartsWith("{"))
     {
         state.State = ParserState.Class;
         string str = ClassDeclarationParserAction.GetClassName(state);
         if (state.ClassNames.Count > 0)
         {
             str = state.ClassNames.Peek() + "/" + str;
         }
         state.ClassNames.Push(str);
     }
     else
     {
         state.ClassDeclaration = state.ClassDeclaration + " " + trimmedLine;
         state.AddLine          = true;
     }
 }
        private bool ExtractMethodParts(ParserStateValues state)
        {
            string methodName;
            string afterMethodName;
            string foundResult;
            string foundResultModifier;
            string foundMethodAttributes;

            if (!this.GetPartBeforeParameters(state.Method.Declaration, out methodName, out afterMethodName, out foundResult, out foundResultModifier, out foundMethodAttributes))
            {
                return(false);
            }
            state.Method.After            = afterMethodName;
            state.Method.Name             = methodName;
            state.Method.Attributes       = foundMethodAttributes;
            state.Method.Result           = foundResult;
            state.Method.ResultAttributes = foundResultModifier;
            return(true);
        }
 public override void Execute(ParserStateValues state, string trimmedLine)
 {
     if (trimmedLine.StartsWith(".custom instance void ", StringComparison.Ordinal) && trimmedLine.Contains(this.Parser.DllExportAttributeIlAsmFullName))
     {
         state.AddLine = false;
         state.State   = ParserState.DeleteExportAttribute;
         this.Notifier.Notify(-2, DllExportLogginCodes.RemovingDllExportAttribute, Resources.Removing_0_from_1_, (object)Utilities.DllExportAttributeFullName, (object)(state.ClassNames.Peek() + "." + state.Method.Name));
     }
     else
     {
         if (!trimmedLine.StartsWith("// Code", StringComparison.Ordinal))
         {
             return;
         }
         state.State = ParserState.Method;
         if (state.MethodPos == 0)
         {
             return;
         }
         state.Result.Insert(state.MethodPos, state.Method.Declaration);
     }
 }
Пример #14
0
 public override void Execute(ParserStateValues state, string trimmedLine)
 {
     if (trimmedLine.StartsWith(".class", StringComparison.Ordinal))
     {
         state.State            = ParserState.ClassDeclaration;
         state.AddLine          = true;
         state.ClassDeclaration = trimmedLine;
     }
     else if (trimmedLine.StartsWith(".method", StringComparison.Ordinal))
     {
         if (state.ClassNames.Count == 0 || !Parser.Exports.ClassesByName.TryGetValue(state.ClassNames.Peek(), out _))
         {
             state.State = ParserState.Method; // https://github.com/3F/DllExport/issues/174
             return;
         }
         state.Method.Reset();
         state.Method.Declaration = trimmedLine;
         state.AddLine            = false;
         state.State = ParserState.MethodDeclaration;
     }
     else if (trimmedLine.StartsWith(".field", StringComparison.Ordinal))
     {
         if (TreatField(state, ref trimmedLine))
         {
             state.AddLine = false;
             state.Result.Add(trimmedLine);
         }
         return;
     }
     else
     {
         if (!trimmedLine.StartsWith("} // end of class", StringComparison.Ordinal))
         {
             return;
         }
         state.ClassNames.Pop();
         state.State = state.ClassNames.Count > 0 ? ParserState.Class : ParserState.Normal;
     }
 }
        private bool ExtractMethodParts(ParserStateValues state)
        {
            if (!GetPartBeforeParameters
                (
                    state.Method.Declaration,
                    out string name,
                    out string afterName,
                    out string result,
                    out string resultAttrs,
                    out string attributes
                ))
            {
                return(false);
            }

            state.Method.Name             = name;
            state.Method.After            = afterName;
            state.Method.Attributes       = attributes;
            state.Method.Result           = result;
            state.Method.ResultAttributes = resultAttrs;
            state.Method.Pinvokeimpl      = ExtractPinvokeimpl(state);
            return(true);
        }
Пример #16
0
        public override void Execute(ParserStateValues state, string trimmedLine)
        {
            if (state.Method.Pinvokeimpl != null && trimmedLine == "}")
            {
                state.State = ParserState.Class;
                return;
            }

            if (trimmedLine.StartsWith("} // end of method", StringComparison.Ordinal))
            {
                state.State = ParserState.Class;
                return;
            }

            if (trimmedLine.StartsWith("IL_", StringComparison.Ordinal))
            {
                if (TreatIL(state, ref trimmedLine))
                {
                    state.AddLine = false;
                    state.Result.Add(trimmedLine);
                }
                return;
            }
        }
 public override void Execute(ParserStateValues state, string trimmedLine)
 {
     if (trimmedLine.StartsWith("{", StringComparison.Ordinal))
     {
         ExportedClass exportedClass;
         if (this.GetIsExport(state, out exportedClass))
         {
             this.Notify(-1, DllExportLogginCodes.MethodFound, string.Format(Resources.Found_method_0_1_, (object)exportedClass.FullTypeName, (object)state.Method.Declaration));
             state.MethodPos = state.Result.Count;
             state.State     = ParserState.MethodProperties;
         }
         else
         {
             state.Result.Add(state.Method.Declaration);
             state.State     = ParserState.Method;
             state.MethodPos = 0;
         }
     }
     else
     {
         state.Method.Declaration = state.Method.Declaration + " " + trimmedLine;
         state.AddLine            = false;
     }
 }
 private ExportedMethod GetExportedMethod(ParserStateValues state, ExportedClass exportedClass)
 {
     //TODO: see details in NextExportedMethod()
     return(exportedClass.NextExportedMethod(state.Method.Name));
 }
        public override void Execute(ParserStateValues state, string trimmedLine)
        {
            if (!trimmedLine.StartsWith(".custom", StringComparison.InvariantCulture) && // .custom instance void ['DllExport']'...'.'DllExportAttribute'::.ctor(string) = ( 01 00 06 50 72 69 6E 74 31 00 00 ) // ...Print1..
                !trimmedLine.StartsWith(".maxstack", StringComparison.InvariantCulture))
            {
                state.AddLine = false;
                return;
            }
            state.State = ParserState.Method;

            ExportedClass exportedClass;

            if (!Exports.ClassesByName.TryGetValue(state.ClassNames.Peek(), out exportedClass))
            {
                state.AddLine = false;
                return;
            }

            ExportedMethod exportMethod  = GetExportedMethod(state, exportedClass);
            string         declaration   = state.Method.Declaration;
            StringBuilder  stringBuilder = new StringBuilder(250);

            stringBuilder.Append(".method ").Append(state.Method.Attributes.NullSafeTrim()).Append(" ");
            stringBuilder.Append(state.Method.Result.NullSafeTrim());
            stringBuilder.Append(" modopt(['mscorlib']'").Append(AssemblyExports.ConventionTypeNames[exportMethod.CallingConvention]).Append("') ");

            if (!String.IsNullOrEmpty(state.Method.ResultAttributes))
            {
                stringBuilder.Append(" ").Append(state.Method.ResultAttributes);
            }

            stringBuilder.Append(" '").Append(state.Method.Name).Append("'").Append(state.Method.After.NullSafeTrim());
            bool flag = ValidateExportNameAndLogError(exportMethod, state);

            if (flag)
            {
                state.Method.Declaration = stringBuilder.ToString();
            }

            if (state.MethodPos != 0)
            {
                state.Result.Insert(state.MethodPos, state.Method.Declaration);
            }

            if (flag)
            {
                Notifier.Notify(-2, DllExportLogginCodes.OldDeclaration, "\t" + Resources.OldDeclaration_0_, declaration);
                Notifier.Notify(-2, DllExportLogginCodes.NewDeclaration, "\t" + Resources.NewDeclaration_0_, state.Method.Declaration);

                state.Result.Add(
                    String.Format(
                        CultureInfo.InvariantCulture,
                        "    .export [{0}] as '{1}'",
                        exportMethod.VTableOffset,
                        exportMethod.ExportName
                        )
                    );

                Notifier.Notify(-1, DllExportLogginCodes.AddingVtEntry, "\t" + Resources.AddingVtEntry_0_export_1_, exportMethod.VTableOffset, exportMethod.ExportName);
            }
        }