protected MethodBodyReader GetMethodBodyReader(MethodInfo mi)
 {
     SDILReader.MethodBodyReader mr = null;
     try
     {
         mr = new MethodBodyReader(mi);
     }
     catch (System.IO.FileNotFoundException)
     {
         // We will already have been warned about missing files during the load.
     }
     return mr;
 }
 protected MethodBodyReader GetMethodBodyReader(MethodInfo mi)
 {
     SDILReader.MethodBodyReader mr = null;
     try
     {
         mr = new MethodBodyReader(mi);
     }
     catch (System.IO.FileNotFoundException)
     {
         // We will already have been warned about missing files during the load.
     }
     return(mr);
 }
        public void Generate(MethodInfo[] mi)
        {
            string @namespace = mi[0].DeclaringType.Namespace;
            string @class = mi[0].DeclaringType.Name;

            c = new StreamWriter(Path.Combine("src", @namespace + "_" + @class + ".cpp"), false);
            h = new StreamWriter(Path.Combine("src", @namespace + "_" + @class + ".h"), false);

            cs = new StringBuilder();
            hs = new StringBuilder();

            //Must include headers - translate framework types and functions
            hs.Append("#include \"..\\Framework\\types.h\"\n");

            hs.Append("namespace " + @namespace + " { \n class " + @class + "{ \n");
            RecurH.Push("}");
            RecurH.Push("};");

            cs.AppendLine("#include \"" + @namespace + "_" + @class + ".h\"");

            for (lineNum = 0; lineNum < mi.Length; lineNum++)
            {
                MethodInfo m = mi[lineNum];
                MethodBodyReader r = new MethodBodyReader(m);

                //Save the temporary MSIL
                string methodname = m.Name;
                lines = r.GetBodyCode().Split('\n');
                File.WriteAllText(Path.Combine("src/MSIL", @namespace + "_" + @class + "_" + methodname + ".MSIL"), r.GetBodyCode());

                //Parse and setup function parameters
                ParameterInfo[] @params = m.GetParameters();
                string par = "";
                if (@params.Length != 0)
                {
                    par = @params[0].ParameterType.Name + " v_0";
                    Vars["v_0"] = @params[0].ParameterType.Name;
                    for (int tmp = 1; tmp < @params.Length; tmp++)
                    {
                        par += ", " + @params[tmp].ParameterType.Name + " v_" + tmp.ToString();
                        Vars["v_" + tmp.ToString()] = @params[tmp].ParameterType.Name;
                    }
                }

                //Setup function declaration in header
                hs.AppendFormat("{0} {1}({2});", m.ReturnType.Namespace + "::" + m.ReturnType.Name, m.Name, par);

                //Setup function definition in source
                cs.AppendLine(m.ReturnType.Namespace + "::" + m.ReturnType.Name + " " + @namespace + "::" + @class + "::" + m.Name + "(" + par + "){" );
                RecurC.Push("}");

                //Define all variables
                foreach (LocalVariableInfo locals in m.GetMethodBody().LocalVariables)
                {
                    Vars.Add("V_" + locals.LocalIndex, locals.LocalType.Namespace + "::" + locals.LocalType.Name);
                    cs.AppendLine(locals.LocalType.Namespace + "::" + locals.LocalType.Name + " " + "V_" + locals.LocalIndex + ";");
                    Vars["V_" + locals.LocalIndex] = "";
                }

                bool exit = false;
                #region Generate code
                lineNum = 0;
                do
                {
                    for (; lineNum < lines.Length; lineNum++)
                    {
                        //skip empty lines
                        if (lines[lineNum].Trim() != string.Empty)
                        {
                            //Remove all whitespace from the string
                            string line = lines[lineNum].Trim();

                            //Get the offset
                            curOffset = long.Parse(line.Split(':')[0]);
                            line = line.Remove(line.Split(':')[0] + ":").Trim();

                            //Check if the line is of any interest to us
                            foreach (string key in Translators.Keys)
                            {
                                //if it is, call the appropriate handler and update the tokens
                                if (line.StartsWith(key))
                                {
                                    string[] f = Translators[key](line);
                                    if (!string.IsNullOrWhiteSpace(f[0])) cs.AppendLine(f[0]);
                                    if (!string.IsNullOrWhiteSpace(f[1])) hs.AppendLine(f[1]);
                                }
                            }
                        }
                    }

                    if (backupOffset.Count > 0)
                    {
                        lineNum = backupOffset.Pop();
                    }
                    else
                    {
                        exit = true;
                    }

                    if (@else.Count > 0)
                    {
                        if (@else.Pop())
                        {
                            cs.AppendLine(RecurC.Pop() + " else { ");
                        }
                    }
                } while (!exit);
                #endregion

                #region Header Recursion tree
                while (RecurH.Count > 0)
                {
                    hs.AppendLine(RecurH.Pop());
                }
                h.Write(hs);
                h.Flush();
                #endregion

                #region Code Recursion tree
                while (RecurC.Count > 0)
                {
                    cs.AppendLine(RecurC.Pop());
                }
                c.Write(cs);
                c.Flush();
                #endregion

                Process.Start("AStyle", "--style=allman --recursive  src/*.cpp  src/*.h");
            }
        }
示例#4
0
        private bool AddOutNodes(BaseItem initem)
        {
            string activeId = initem.GetShortID();
            curlevel++;
            if (curlevel > _localOptions.OuterScanLevel)
            {
                //MessageBox.Show("Max deep level reached");
                return false;
            }

            foreach (MethodItem methodItem in (initem as TypeItem).Methods)
            {
                SDILReader.MethodBodyReader mr = new MethodBodyReader(methodItem.MethodInfo);
                if (mr == null) continue;

                // Abstract..external, etc
                if (mr.instructions == null) continue;

                string id = string.Empty;
                string methodId = methodItem.GetShortID();

                foreach (ILInstruction instruction in mr.instructions)
                {
                    if (instruction.Code.OperandType != OperandType.InlineMethod) continue;

                    string name = "";
                    if (instruction.Operand is MethodInfo)
                    {
                        id = (instruction.Operand as MethodInfo).DeclaringType.ToString();
                        name = (instruction.Operand as MethodInfo).Name;
                    }
                    else if (instruction.Operand is ConstructorInfo)
                    {
                        id = (instruction.Operand as ConstructorInfo).DeclaringType.ToString();
                        name = (instruction.Operand as ConstructorInfo).Name;
                    }
                    else if (instruction.Operand == null)
                    {
                        _logView.LogStr("Class Butterfly failed to recognise call instruction due to generics.");
                    }
                    else
                    {
                        _logView.LogStr("Class Butterfly failed to recognise call instruction :" + instruction.Operand.GetType().ToString());
                    }

                    // Find the method baseItem we're calling.
                    // The declaring type of the method we're calling.
                    BaseItem item = _projectBrowser.Lookup(id);

                    // If the method is not present in our project, then forget about it.
                    if (item == null) continue;

                    // Only add methods once.
                    if (_addedNodes.ContainsKey(id)) continue;

                    //
                    /*
                    Color color = ChangeBrightnessColor(_sharedOptions.TypeColor, 1.2);
                    TypeItem ti = item as TypeItem;
                    if (ti != null)
                    {
                        color=Color.FromArgb(ti.GetParentAssemblyID().GetHashCode());
                    }
                     */
                    Color color = GetColorForNode(item,ChangeBrightnessColor(_sharedOptions.TypeColor, 1.2));
                    color = ChangeBrightnessColor(color, (1.0 + curlevel / 10.0));

                    AddNode(id, GetNameForItem(item), color, item);
                    AddEdge(activeId, id, EdgeStyle.NormalArrow);

                    AddOutNodes(item);
                }
            }
            curlevel--;
            return true;
        }
示例#5
0
        private void AddLinksFromMethods(TypeItem fromTypeItem, string toTypeId)
        {
            foreach (MethodItem methodItem in fromTypeItem.Methods)
            {
                string fromTypeId = fromTypeItem.GetShortID();

                MethodBodyReader mr = new MethodBodyReader(methodItem.MethodInfo);
                if (mr == null) continue;

                // Abstract..external, etc
                if (mr.instructions == null) continue;

                foreach (ILInstruction instruction in mr.instructions)
                {
                    string id = string.Empty;

                    if (instruction.Code.OperandType != OperandType.InlineMethod) continue;

                    // TODO dup code
                    if (instruction.Operand is MethodInfo)
                    {
                        id = (instruction.Operand as MethodInfo).DeclaringType.ToString();
                    }
                    else if (instruction.Operand is ConstructorInfo)
                    {
                        id = (instruction.Operand as ConstructorInfo).DeclaringType.ToString();
                    }
                    else if (instruction.Operand == null)
                    {
                        _logView.LogStr("Class Butterfly failed to recognise call instruction due to generics.(in)");
                    }
                    else
                    {
                        _logView.LogStr("Class Butterfly failed to recognise call instruction (in) :" + instruction.Operand.GetType().ToString());
                    }

                    // We're looking for hits on one specific method.
                    if (id != toTypeId) continue;

                    // Only add nodes once.
                    //if (_addedNodes.ContainsKey(fromTypeId)) continue;

                    //AddNode(fromTypeId, GetNameForItem(fromTypeItem), ChangeBrightnessColor(_sharedOptions.TypeColor,0.8), fromTypeItem);
                    Color color = GetColorForNode(fromTypeItem, ChangeBrightnessColor(_sharedOptions.TypeColor, 1));

                    AddNode(fromTypeId, GetNameForItem(fromTypeItem), color, fromTypeItem);
                    AddEdge(fromTypeId, toTypeId, EdgeStyle.NormalArrow);
                }
            }
        }
示例#6
0
        private string ProcessProperty(Type type, PropertyInfo property)
        {
            //Carrega o código da propriedade e busca uma referência ao componente
            string erros = "";
            var modelType = GetModel(property);
            var methodReader = new MethodBodyReader(property.GetSetMethod());
            if (methodReader.instructions != null)
            {
                foreach (ILInstruction instruction in methodReader.instructions)
                {
                    //Busca onde está usando o componente
                    if (instruction.Code.Name == "ldfld")
                    {
                        //achou o nome da variável do componente e o seu tipo
                        string fieldName = (instruction.Operand as FieldInfo).Name;
                        Type controlType = (instruction.Operand as FieldInfo).FieldType;

                        object instance = Activator.CreateInstance(type);
                        object componentInstance = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instance);

                        //processa o componente verificando os bindings
                        erros += ProcessComponent(modelType, type, fieldName, componentInstance);
                    }
                }
            }

            return erros;
        }
        private void AddOutNodes()
        {
            string activeId = _activeItem.GetShortID();

            foreach (NamespaceItem nsItem in (_activeItem as AssemblyItem).NameSpaces)
            {
                if (CheckWorker()) break;

                foreach (TypeItem typeItem in nsItem.Types)
                {
                    if (CheckWorker()) break;

                    foreach (MethodItem methodItem in typeItem.Methods)
                    {
                        SDILReader.MethodBodyReader mr = new MethodBodyReader(methodItem.MethodInfo);
                        if (mr == null) continue;

                        // Abstract..external, etc
                        if (mr.instructions == null) continue;

                        string id = string.Empty;
                        string methodId = methodItem.GetShortID();

                        foreach (ILInstruction instruction in mr.instructions)
                        {
                            if (CheckWorker()) break;
                            if (instruction.Code.OperandType != OperandType.InlineMethod) continue;

                            // common code :/ todo
                            string name = "";
                            if (instruction.Operand is MethodInfo)
                            {
                                id = (instruction.Operand as MethodInfo).DeclaringType.ToString();
                                name = (instruction.Operand as MethodInfo).Name;
                            }
                            else if (instruction.Operand is ConstructorInfo)
                            {
                                id = (instruction.Operand as ConstructorInfo).DeclaringType.ToString();
                                name = (instruction.Operand as ConstructorInfo).Name;
                            }
                            else if (instruction.Operand == null)
                            {
                                _logView.LogStr("Assembly Butterfly failed to recognise call instruction due to generics.");
                            }
                            else
                            {
                                _logView.LogStr("Assembly Butterfly failed to recognise call instruction :" + instruction.Operand.GetType().ToString());
                            }

                            // Find the method baseItem we're calling.
                            // The declaring type of the method we're calling.
                            BaseItem item = _projectBrowser.Lookup(id);

                            // If the method is not present in our project, then forget about it.
                            if (item == null) continue;

                            // Find the assembly the method is defined in.
                            BaseItem asmFrom = item;
                            while (!(asmFrom is AssemblyItem))
                            {
                                asmFrom = asmFrom.Parent;
                                if (asmFrom == null) break;
                            }

                            string asmFromId = asmFrom.GetShortID();

                            // Only add methods once.
                            if (this.AddedNodes.ContainsKey(asmFromId)) continue; //todo, needed now?
                            //

                            AddNode(asmFromId, asmFrom.Name, _sharedOptions.AssemblyColor, asmFrom);
                            AddEdge(activeId, asmFromId, EdgeStyle.NormalArrow);
                        }
                    }
                }
            }
        }
示例#8
0
        private void lbAvailableMethodsList_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                MethodInfo mi = methods[lbAvailableMethodsList.SelectedIndex];
                SDILReader.MethodBodyReader mr = new MethodBodyReader(mi);
                rchMethodBodyCode.Clear();
                rchMethodBodyCode.Text = mr.GetBodyCode();
            }
            catch
            {

            }
        }
        private void AddLinksFromMethods(TypeItem fromTypeItem, string toMethodId)
        {
            string fromMethodId, id;

            foreach (MethodItem methodItem in fromTypeItem.Methods)
            {
                if (CheckWorker()) break;
                fromMethodId = methodItem.GetShortID();

                SDILReader.MethodBodyReader mr = new MethodBodyReader(methodItem.MethodInfo);
                if (mr == null) continue;

                // Abstract..external, etc
                if (mr.instructions == null) continue;

                foreach (ILInstruction instruction in mr.instructions)
                {
                    if (CheckWorker()) break;
                    id = string.Empty;

                    if (instruction.Code.OperandType != OperandType.InlineMethod) continue;

                    if (instruction.Operand is MethodInfo)
                    {
                        id = string.Format("{0} {1}",
                            (instruction.Operand as MethodInfo).DeclaringType.ToString(),
                            RefHelp.GetNameWithParameterList(instruction.Operand as MethodInfo));
                    }
                    else if (instruction.Operand is ConstructorInfo)
                    {
                        id = string.Format("{0} {1}",
                            (instruction.Operand as ConstructorInfo).DeclaringType.ToString(),
                            RefHelp.GetNameWithParameterList(instruction.Operand as ConstructorInfo));
                    }
                    else if (instruction.Operand == null)
                    {
                        _logView.LogStr("Butterfly failed with null instruction.operand :" + instruction.ToString());
                    }
                    else
                    {
                        _logView.LogStr("Butterfly failed to recognise call instruction (in) :" + instruction.Operand.GetType().ToString());
                    }

                    // We're looking for hits on one specific method.
                    if (id != toMethodId) continue;

                    // Only add nodes once.
                    //if (_addedNodes.ContainsKey(fromMethodId)) continue;

                    string caption = fromTypeItem + "\n" + methodItem.Name;

                    AddNode(fromMethodId, caption, _sharedOptions.MethodColor, methodItem);
                    AddEdge(fromMethodId, toMethodId, EdgeStyle.NormalArrow);
                }
            }
        }
示例#10
0
        private void AddOutNodes()
        {
            SDILReader.MethodBodyReader mr = new MethodBodyReader((_activeItem as MethodItem).MethodInfo);
            if (mr == null) return;

            // Abstract..external, etc
            if (mr.instructions == null)
            {
                AddNullBodyOutNodes();
                return;
            }

            string id = string.Empty;
            string methodId = _activeItem.GetShortID();

            foreach (ILInstruction instruction in mr.instructions)
            {
                if (CheckWorker()) break;

                if (instruction.Code.OperandType != OperandType.InlineMethod) continue;

                Type type = null;
                if (instruction.Operand is MethodInfo)
                {
                    id = string.Format("{0} {1}",
                        (instruction.Operand as MethodInfo).DeclaringType.ToString(),
                        RefHelp.GetNameWithParameterList(instruction.Operand as MethodInfo));

                    type = (instruction.Operand as MethodInfo).DeclaringType;
                }
                else if (instruction.Operand is ConstructorInfo)
                {
                    id = string.Format("{0} {1}",
                        (instruction.Operand as ConstructorInfo).DeclaringType.ToString(),
                        RefHelp.GetNameWithParameterList(instruction.Operand as ConstructorInfo));

                    type = (instruction.Operand as ConstructorInfo).DeclaringType;
                }
                else
                {
                    _logView.LogStr("Butterfly failed to recognise call instruction :" + instruction.Operand.GetType().ToString());
                }

                // Find the method baseItem we're calling.
                BaseItem item = _projectBrowser.Lookup(id);

                // If the method is not present in our project, then forget about it.
                if (item == null) continue;

                string caption = item.Name;
                if (_methodType != type)
                {
                    caption = type.Name + "\n" + caption;
                }
                AddNode(id, caption, _sharedOptions.MethodColor, item);
                AddEdge(methodId, id, EdgeStyle.NormalArrow);
            }
        }