Ejemplo n.º 1
0
        private static void WriteFieldReference(IFieldReference fieldReference, IAssembly assembly, IFormattedCodeWriter writer)
        {
            WriteTypeReference(fieldReference.FieldType, assembly, writer);

            if (fieldReference.Resolve().DeclaringType != null)
            {
                writer.Write(' ');
                WriteTypeReference(fieldReference.Resolve().DeclaringType, assembly, writer);
            }
            writer.Write("::");
            writer.Write(fieldReference.Name);
        }
Ejemplo n.º 2
0
        public IEnumerable <FieldInfo> GetUsedFields(object method)
        {
            var analyzedMethod = method as IMethodDeclaration;

            if (analyzedMethod.Body == null)
            {
                yield break;
            }

            var body         = analyzedMethod.Body as IMethodBody;
            var instructions = body.Instructions.OfType <IInstruction>();

            foreach (IInstruction instr in instructions)
            {
                IFieldReference fr = instr.Value as IFieldReference;
                if (fr != null)
                {
                    IFieldDeclaration def = fr.Resolve();
                    if (def != null)
                    {
                        yield return(HAL.Converter.Field(def));
                    }
                }
            }
            yield break;
        }
Ejemplo n.º 3
0
        public static bool IsVisible(IFieldReference value, IVisibilityConfiguration visibility)
        {
            bool result;

            if (IsVisible(value.DeclaringType, visibility))
            {
                IFieldDeclaration fieldDeclaration = value.Resolve();
                if (fieldDeclaration == null)
                {
                    result = true;
                }
                else
                {
                    switch (fieldDeclaration.Visibility)
                    {
                    case FieldVisibility.PrivateScope:
                        result = visibility.Private;
                        break;

                    case FieldVisibility.Private:
                        result = visibility.Private;
                        break;

                    case FieldVisibility.FamilyAndAssembly:
                        result = visibility.FamilyAndAssembly;
                        break;

                    case FieldVisibility.Assembly:
                        result = visibility.Assembly;
                        break;

                    case FieldVisibility.Family:
                        result = visibility.Family;
                        break;

                    case FieldVisibility.FamilyOrAssembly:
                        result = visibility.FamilyOrAssembly;
                        break;

                    case FieldVisibility.Public:
                        result = visibility.Public;
                        break;

                    default:
                        throw new NotSupportedException();
                    }
                }
            }
            else
            {
                result = false;
            }
            return(result);
        }
Ejemplo n.º 4
0
        public static bool IsVisible(IFieldReference value, IVisibilityConfiguration visibility)
        {
            if (Helper.IsVisible(value.DeclaringType, visibility))
            {
                IFieldDeclaration fieldDeclaration = value.Resolve();
                if (fieldDeclaration == null)
                {
                    return(true);
                }

                switch (fieldDeclaration.Visibility)
                {
                case FieldVisibility.Public:
                    return(visibility.Public);

                case FieldVisibility.Assembly:
                    return(visibility.Assembly);

                case FieldVisibility.FamilyOrAssembly:
                    return(visibility.FamilyOrAssembly);

                case FieldVisibility.Family:
                    return(visibility.Family);

                case FieldVisibility.Private:
                    return(visibility.Private);

                case FieldVisibility.FamilyAndAssembly:
                    return(visibility.FamilyAndAssembly);

                case FieldVisibility.PrivateScope:
                    return(visibility.Private);
                }

                throw new NotSupportedException();
            }

            return(false);
        }
Ejemplo n.º 5
0
        protected override IExpression ConvertAssign(IAssignExpression iae)
        {
            assignmentIndex--;
            //Trace.WriteLine($"{analysis.methods[currentMethodIndex].Declaration.Name} {assignmentIndex} {iae}");
            bool assignmentIsDead = false;
            // do not convert the target, only its indices
            IVariableDeclaration ivd = Recognizer.GetVariableDeclaration(iae.Target);

            if (ivd != null)
            {
                if (!liveVariables.Contains(ivd) && !analysis.loopVariables.Contains(ivd))
                {
                    assignmentIsDead = true;
                }
            }
            IFieldReference   ifr = Recognizer.GetFieldReference(iae.Target);
            IFieldDeclaration ifd = ifr?.Resolve();

            if (ifd != null)
            {
                if (!liveFields.Contains(ifd))
                {
                    assignmentIsDead = true;
                }
            }
            if (iae.Target is IVariableReferenceExpression || iae.Target is IVariableDeclarationExpression)
            {
                liveVariables.Remove(ivd);
            }
            else if (iae.Target is IFieldReferenceExpression && ((IFieldReferenceExpression)iae.Target).Field.Equals(ifr))
            {
                liveFields.Remove(ifd);
            }
            if (!assignmentIsDead)
            {
                if (ivd != null)
                {
                    usedVariables.Add(ivd);
                }
                else if (ifd != null)
                {
                    usedFields.Add(ifd);
                }
                // analyze the indices of target
                foreach (var bracket in Recognizer.GetIndices(iae.Target))
                {
                    ConvertCollection(bracket);
                }
                // must convert the expression afterward, since it may use target.
                base.ConvertExpression(iae.Expression);
            }
            else if (LivenessThisMethod != null)
            {
                bool variableIsUsed = (ivd != null && usedVariables.Contains(ivd)) ||
                                      (ifd != null && usedFields.Contains(ifd));
                LivenessThisMethod[assignmentIndex] = variableIsUsed ? Liveness.Dead : Liveness.Unused;
                if (debug && false)
                {
                    Trace.WriteLine($"dead statement: {iae}");
                    if (!variableIsUsed)
                    {
                        Trace.WriteLine($"unused variable: {iae.Target}");
                    }
                }
            }
            return(iae);
        }
Ejemplo n.º 6
0
        public static bool IsVisible(IFieldReference value, IVisibilityConfiguration visibility)
        {
            if (IsVisible(value.DeclaringType, visibility))
            {
                IFieldDeclaration fieldDeclaration = value.Resolve();
                if (fieldDeclaration == null)
                {
                    return true;
                }

                switch (fieldDeclaration.Visibility)
                {
                    case FieldVisibility.Public:
                        return visibility.Public;

                    case FieldVisibility.Assembly:
                        return visibility.Assembly;

                    case FieldVisibility.FamilyOrAssembly:
                        return visibility.FamilyOrAssembly;

                    case FieldVisibility.Family:
                        return visibility.Family;

                    case FieldVisibility.Private:
                        return visibility.Private;

                    case FieldVisibility.FamilyAndAssembly:
                        return visibility.FamilyAndAssembly;

                    case FieldVisibility.PrivateScope:
                        return visibility.Private;
                }

                throw new NotSupportedException();
            }

            return false;
        }
Ejemplo n.º 7
0
        private TreeNode FindNode(IMemberReference memberReference)
        {
            if (memberReference is IAssemblyReference)
            {
                foreach (TreeNode node in _assembliesTree.Nodes)
                {
                    if (node.Tag == memberReference)
                    {
                        return(node);
                    }
                }

                return(null);
            }

            if (memberReference is IResourceReference)
            {
                IResourceReference resourceReference = (IResourceReference)memberReference;
                TreeNode           assemblyNode      = FindNode(resourceReference.Resolve().Assembly);
                TreeNode           resourcesNode     = null;
                foreach (TreeNode childNode in assemblyNode.Nodes)
                {
                    if (childNode.Text == "Resources")
                    {
                        resourcesNode = childNode;
                    }
                }

                return(FindNode(memberReference, resourcesNode));
            }

            if (memberReference is IModuleReference)
            {
                IModuleReference moduleReference = (IModuleReference)memberReference;
                return(FindNode(memberReference, FindNode(moduleReference.Resolve().Assembly)));
            }

            if (memberReference is ITypeReference)
            {
                ITypeReference typeReference = (ITypeReference)memberReference;


                if (typeReference.Resolve().DeclaringType != null)
                {
                    TreeNode declaringTypeNode = FindNode(typeReference.Resolve().DeclaringType);
                    return(FindNode(memberReference, declaringTypeNode));
                }
                else
                {
                    TreeNode moduleNode = FindNode(typeReference.Resolve().Module);

                    foreach (TreeNode namespaceNode in moduleNode.Nodes)
                    {
                        if (((namespaceNode.Text == "-") && (typeReference.Namespace == null)) ||
                            (namespaceNode.Text == typeReference.Namespace))
                        {
                            return(FindNode(memberReference, namespaceNode));
                        }
                    }
                }

                return(null);
            }

            if (memberReference is IMethodReference)
            {
                IMethodReference methodReference = (IMethodReference)memberReference;
                TreeNode         typeNode        = FindNode(methodReference.Resolve().DeclaringType);

                if (typeNode == null)
                {
                    return(null);
                }

                foreach (TreeNode node in typeNode.Nodes)
                {
                    if (node.Tag == memberReference)
                    {
                        return(node);
                    }

                    if ((node.Tag is IPropertyReference) || (node.Tag is IEventReference))
                    {
                        TreeNode nestedEventNode = FindNode(memberReference, node);
                        if (nestedEventNode != null)
                        {
                            return(nestedEventNode);
                        }
                    }
                }

                return(null);
            }

            if (memberReference is IFieldReference)
            {
                IFieldReference fieldReference = (IFieldReference)memberReference;
                return(FindNode(memberReference, FindNode(fieldReference.Resolve().DeclaringType)));
            }

            if (memberReference is IPropertyReference)
            {
                IPropertyReference propertyReference = (IPropertyReference)memberReference;
                return(FindNode(memberReference, FindNode(propertyReference.Resolve().DeclaringType)));
            }

            if (memberReference is IEventReference)
            {
                IEventReference eventReference = (IEventReference)memberReference;
                return(FindNode(memberReference, FindNode(eventReference.Resolve().DeclaringType)));
            }

            return(null);
        }
Ejemplo n.º 8
0
        internal static int GetImageIndex(IMemberReference memberReference)
        {
            IFieldReference fieldReference = memberReference as IFieldReference;

            if (fieldReference != null)
            {
                IFieldDeclaration fieldDeclaration = fieldReference.Resolve();
                if (fieldDeclaration == null)
                {
                    return(BrowserResource.Error);
                }

                int icon = BrowserResource.Field;

                if (IsEnumerationElement(fieldReference))
                {
                    icon = BrowserResource.EnumerationElement;
                }
                else
                {
                    if (fieldDeclaration.Static)
                    {
                        icon += 6;
                    }
                }

                switch (fieldDeclaration.Visibility)
                {
                case FieldVisibility.Public:
                    return(icon);

                case FieldVisibility.Assembly:
                    return(icon + 1);

                case FieldVisibility.FamilyOrAssembly:
                    return(icon + 2);

                case FieldVisibility.Family:
                    return(icon + 3);

                case FieldVisibility.Private:
                case FieldVisibility.PrivateScope:
                    return(icon + 4);

                case FieldVisibility.FamilyAndAssembly:
                    return(icon + 5);
                }
            }

            IMethodReference methodReference = memberReference as IMethodReference;

            if (methodReference != null)
            {
                IArrayType arrayType = methodReference.DeclaringType as IArrayType;
                if (arrayType != null)
                {
                    return(BrowserResource.Method);
                }

                IMethodDeclaration methodDeclaration = methodReference.Resolve();
                if (methodDeclaration == null)
                {
                    return(BrowserResource.Error);
                }

                int icon = BrowserResource.Method;

                string methodName = methodReference.Name;
                if ((methodName == ".ctor") || (methodName == ".cctor"))
                {
                    icon = BrowserResource.Constructor;
                }
                else if ((methodDeclaration.Virtual) && (!methodDeclaration.Abstract))
                {
                    icon += 12;
                }

                if (methodDeclaration.Static)
                {
                    icon += 6;
                }

                switch (methodDeclaration.Visibility)
                {
                case MethodVisibility.Public:
                    return(icon);

                case MethodVisibility.Assembly:
                    return(icon + 1);

                case MethodVisibility.FamilyOrAssembly:
                    return(icon + 2);

                case MethodVisibility.Family:
                    return(icon + 3);

                case MethodVisibility.Private:
                case MethodVisibility.PrivateScope:
                    return(icon + 4);

                case MethodVisibility.FamilyAndAssembly:
                    return(icon + 5);
                }
            }

            IPropertyReference propertyReference = memberReference as IPropertyReference;

            if (propertyReference != null)
            {
                IPropertyDeclaration propertyDeclaration = propertyReference.Resolve();
                if (propertyDeclaration != null)
                {
                    IMethodReference   getMethodReference = propertyDeclaration.GetMethod;
                    IMethodDeclaration getMethod          = (getMethodReference == null) ? null : getMethodReference.Resolve();

                    IMethodReference   setMethodReference = propertyDeclaration.SetMethod;
                    IMethodDeclaration setMethod          = (setMethodReference == null) ? null : setMethodReference.Resolve();

                    int index = BrowserResource.Property;

                    if ((setMethod != null) && (getMethod != null))
                    {
                        index = BrowserResource.Property;
                    }
                    else if (setMethod != null)
                    {
                        index = BrowserResource.PropertyWrite;
                    }
                    else if (getMethod != null)
                    {
                        index = BrowserResource.PropertyRead;
                    }

                    if (Helper.IsStatic(propertyReference))
                    {
                        index += 6;
                    }

                    switch (Helper.GetVisibility(propertyReference))
                    {
                    case MethodVisibility.Public:
                        return(index + 0);

                    case MethodVisibility.Assembly:
                        return(index + 1);

                    case MethodVisibility.FamilyOrAssembly:
                        return(index + 2);

                    case MethodVisibility.Family:
                        return(index + 3);

                    case MethodVisibility.Private:
                    case MethodVisibility.PrivateScope:
                        return(index + 4);

                    case MethodVisibility.FamilyAndAssembly:
                        return(index + 5);
                    }
                }
            }

            IEventReference eventReference = memberReference as IEventReference;

            if (eventReference != null)
            {
                int index = BrowserResource.Event;

                if (Helper.IsStatic(eventReference))
                {
                    index += 6;
                }

                switch (Helper.GetVisibility(eventReference))
                {
                case MethodVisibility.Public:
                    return(index + 0);

                case MethodVisibility.Assembly:
                    return(index + 1);

                case MethodVisibility.FamilyOrAssembly:
                    return(index + 2);

                case MethodVisibility.Family:
                    return(index + 3);

                case MethodVisibility.Private:
                case MethodVisibility.PrivateScope:
                    return(index + 4);

                case MethodVisibility.FamilyAndAssembly:
                    return(index + 5);
                }
            }

            throw new NotSupportedException();
        }
Ejemplo n.º 9
0
        internal static int GetColor(IMemberReference memberReference)
        {
            IFieldReference fieldReference = memberReference as IFieldReference;

            if (fieldReference != null)
            {
                IFieldDeclaration fieldDeclaration = fieldReference.Resolve();
                if (fieldDeclaration == null)
                {
                    return(ColorInformation.Error);
                }

                switch (fieldDeclaration.Visibility)
                {
                case FieldVisibility.Private:
                case FieldVisibility.PrivateScope:
                case FieldVisibility.Assembly:
                case FieldVisibility.FamilyAndAssembly:
                    return(ColorInformation.Hidden);

                case FieldVisibility.Public:
                case FieldVisibility.Family:
                case FieldVisibility.FamilyOrAssembly:
                    return(ColorInformation.Normal);
                }
            }

            IMethodReference methodReference = memberReference as IMethodReference;

            if (methodReference != null)
            {
                IArrayType arrayType = methodReference.DeclaringType as IArrayType;
                if (arrayType != null)
                {
                    return(IconHelper.GetColor(arrayType.ElementType as ITypeReference));
                }

                IMethodDeclaration methodDeclaration = methodReference.Resolve();
                if (methodDeclaration == null)
                {
                    return(ColorInformation.Error);
                }

                switch (methodDeclaration.Visibility)
                {
                case MethodVisibility.Private:
                case MethodVisibility.PrivateScope:
                case MethodVisibility.Assembly:
                case MethodVisibility.FamilyAndAssembly:
                    return(ColorInformation.Hidden);

                case MethodVisibility.Public:
                case MethodVisibility.Family:
                case MethodVisibility.FamilyOrAssembly:
                    return(ColorInformation.Normal);
                }
            }

            IPropertyReference propertyReference = memberReference as IPropertyReference;

            if (propertyReference != null)
            {
                switch (Helper.GetVisibility(propertyReference))
                {
                case MethodVisibility.Private:
                case MethodVisibility.PrivateScope:
                case MethodVisibility.Assembly:
                case MethodVisibility.FamilyAndAssembly:
                    return(ColorInformation.Hidden);

                case MethodVisibility.Public:
                case MethodVisibility.Family:
                case MethodVisibility.FamilyOrAssembly:
                    return(ColorInformation.Normal);
                }
            }

            IEventReference eventReference = memberReference as IEventReference;

            if (eventReference != null)
            {
                switch (Helper.GetVisibility(eventReference))
                {
                case MethodVisibility.Private:
                case MethodVisibility.PrivateScope:
                case MethodVisibility.Assembly:
                case MethodVisibility.FamilyAndAssembly:
                    return(ColorInformation.Hidden);

                case MethodVisibility.Public:
                case MethodVisibility.Family:
                case MethodVisibility.FamilyOrAssembly:
                    return(ColorInformation.Normal);
                }
            }

            throw new NotSupportedException();
        }
Ejemplo n.º 10
0
 void processExpression(IExpression expr, bool lvalue)
 {
     if (expr is IMethodInvokeExpression)
     {
         IMethodInvokeExpression call       = (IMethodInvokeExpression)expr;
         IExpression             methodExpr = call.Method;
         if (methodExpr is IMethodReferenceExpression)
         {
             IMethodReference method = ((IMethodReferenceExpression)methodExpr).Method;
             if (method.Name == "Modify" || method.Name == "Store")
             {
                 IExpression target = ((IMethodReferenceExpression)methodExpr).Target;
                 if (target is IBaseReferenceExpression && thisExpr != null)
                 {
                     target = thisExpr;
                 }
                 mutators.Remove(target);
             }
         }
         processExpression(methodExpr);
         foreach (IExpression arg in call.Arguments)
         {
             processExpression(arg);
         }
     }
     else if (expr is IAssignExpression)
     {
         processExpression(((IAssignExpression)expr).Target, true);
         processExpression(((IAssignExpression)expr).Expression);
     }
     else if (expr is IBinaryExpression)
     {
         processExpression(((IBinaryExpression)expr).Left);
         processExpression(((IBinaryExpression)expr).Right);
     }
     else if (expr is IUnaryExpression)
     {
         processExpression(((IUnaryExpression)expr).Expression);
     }
     else if (expr is ICastExpression)
     {
         processExpression(((ICastExpression)expr).Expression);
     }
     else if (expr is IArrayIndexerExpression)
     {
         processExpression(((IArrayIndexerExpression)expr).Target, lvalue);
         foreach (IExpression index in ((IArrayIndexerExpression)expr).Indices)
         {
             processExpression(index);
         }
     }
     else if (expr is IArrayCreateExpression)
     {
         processExpression(((IArrayCreateExpression)expr).Initializer);
         foreach (IExpression dim in ((IArrayCreateExpression)expr).Dimensions)
         {
             processExpression(dim);
         }
     }
     else if (expr is ICanCastExpression)
     {
         processExpression(((ICanCastExpression)expr).Expression);
     }
     else if (expr is IConditionExpression)
     {
         processExpression(((IConditionExpression)expr).Condition);
         processExpression(((IConditionExpression)expr).Then);
         processExpression(((IConditionExpression)expr).Else);
     }
     else if (expr is IDelegateInvokeExpression)
     {
         processExpression(((IDelegateInvokeExpression)expr).Target);
         foreach (IExpression arg in ((IDelegateInvokeExpression)expr).Arguments)
         {
             processExpression(arg);
         }
     }
     else if (expr is IEventReferenceExpression)
     {
         processExpression(((IEventReferenceExpression)expr).Target);
     }
     else if (expr is IFieldReferenceExpression)
     {
         IExpression target = ((IFieldReferenceExpression)expr).Target;
         if (lvalue)
         {
             IFieldReference   fieldRef = ((IFieldReferenceExpression)expr).Field;
             IFieldDeclaration field    = fieldRef.Resolve();
             if (field != null &&
                 IsPersistent(field.DeclaringType as ITypeReference) &&
                 !field.Static &&
                 !IsNonSerialized(field))
             {
                 List <IExpression> updateList;
                 if (target is IBaseReferenceExpression && thisExpr != null)
                 {
                     target = thisExpr;
                 }
                 if (!mutators.TryGetValue(target, out updateList))
                 {
                     mutators[target] = updateList = new List <IExpression>();
                 }
                 updateList.Add(expr);
             }
         }
         processExpression(target);
     }
     else if (expr is IObjectCreateExpression)
     {
         processExpression(((IObjectCreateExpression)expr).Initializer);
         foreach (IExpression arg in ((IObjectCreateExpression)expr).Arguments)
         {
             processExpression(arg);
         }
     }
     else if (expr is IBlockExpression)
     {
         foreach (IExpression elem in ((IBlockExpression)expr).Expressions)
         {
             processExpression(elem);
         }
     }
     else if (expr is IPropertyIndexerExpression)
     {
         processExpression(((IPropertyIndexerExpression)expr).Target);
         foreach (IExpression index in ((IPropertyIndexerExpression)expr).Indices)
         {
             processExpression(index);
         }
     }
     else if (expr is IPropertyReferenceExpression)
     {
         processExpression(((IPropertyReferenceExpression)expr).Target);
     }
     else if (expr is IThisReferenceExpression)
     {
         thisExpr = expr;
     }
 }