示例#1
0
文件: Util.cs 项目: wdstest/SharpJS
        public static JSTemporaryVariable ForFunction(
            JSFunctionExpression function, TypeReference type,
            IFunctionSource functionSource
            )
        {
            var index = function.TemporaryVariableTypes.Count;

            function.TemporaryVariableTypes.Add(type);

            MethodReference methodRef = null;

            if (function.Method != null)
            {
                methodRef = function.Method.Reference;
            }

            var id     = string.Format("$temp{0:X2}", index);
            var result = new JSTemporaryVariable(id, type, methodRef);

            function.AllVariables.Add(id, result);

            // HACK: If the static analysis data for the function is stale, this temporary
            //  variable might get eliminated later despite being in use.
            // We should really just fix all the transforms that aren't invalidating static
            //  analysis data when they should, but this is good enough for now.
            if (function.Method != null)
            {
                functionSource.InvalidateFirstPass(function.Method.QualifiedIdentifier);
            }

            return(result);
        }
示例#2
0
 public EmulateStructAssignment(TypeSystem typeSystem, IFunctionSource functionSource, CLRSpecialIdentifiers clr, bool optimizeCopies)
 {
     TypeSystem     = typeSystem;
     FunctionSource = functionSource;
     CLR            = clr;
     OptimizeCopies = optimizeCopies;
 }
示例#3
0
 public EmulateStructAssignment(TypeSystem typeSystem, IFunctionSource functionSource, CLRSpecialIdentifiers clr, bool optimizeCopies)
 {
     TypeSystem = typeSystem;
     FunctionSource = functionSource;
     CLR = clr;
     OptimizeCopies = optimizeCopies;
 }
示例#4
0
        public IntroduceEnumCasts(
            TypeSystem typeSystem, JSSpecialIdentifiers js,
            TypeInfoProvider typeInfo, MethodTypeFactory methodTypes,
            IFunctionSource functionSource
            )
        {
            TypeSystem     = typeSystem;
            TypeInfo       = typeInfo;
            MethodTypes    = methodTypes;
            JS             = js;
            FunctionSource = functionSource;

            LogicalOperators = new HashSet <JSOperator>()
            {
                JSOperator.LogicalAnd,
                JSOperator.LogicalOr,
                JSOperator.LogicalNot
            };

            BitwiseOperators = new HashSet <JSOperator>()
            {
                JSOperator.BitwiseAnd,
                JSOperator.BitwiseOr,
                JSOperator.BitwiseXor
            };
        }
 public SynthesizePropertySetterReturnValues (
     TypeSystem typeSystem, ITypeInfoSource typeInfo,
     IFunctionSource functionSource
 ) {
     TypeSystem = typeSystem;
     TypeInfo = typeInfo;
     FunctionSource = functionSource;
 }
 public EmulateStructAssignment(QualifiedMemberIdentifier member, IFunctionSource functionSource, TypeSystem typeSystem, TypeInfoProvider typeInfo, CLRSpecialIdentifiers clr, bool optimizeCopies)
     : base(member, functionSource)
 {
     TypeSystem = typeSystem;
     TypeInfo = typeInfo;
     CLR = clr;
     OptimizeCopies = optimizeCopies;
 }
 public OptimizePropertyMutationAssignments (
     TypeSystem typeSystem, ITypeInfoSource typeInfo,
     IFunctionSource functionSource
 ) {
     TypeSystem = typeSystem;
     TypeInfo = typeInfo;
     FunctionSource = functionSource;
 }
示例#8
0
        public FunctionAnalysis2ndPass(IFunctionSource functionSource, MethodInfo method)
        {
            FunctionSource = functionSource;
            Data           = null;
            _IsPure        = method.Metadata.HasAttribute("JSIL.Meta.JSIsPure");

            var parms = method.Metadata.GetAttributeParameters("JSIL.Meta.JSMutatedArguments");

            if (parms != null)
            {
                ModifiedVariables = new HashSet <string>();
                foreach (var p in parms)
                {
                    var s = p.Value as string;
                    if (s != null)
                    {
                        ModifiedVariables.Add(s);
                    }
                }
            }
            else if (!_IsPure)
            {
                ModifiedVariables = new HashSet <string>(from p in method.Parameters select p.Name);
            }
            else
            {
                ModifiedVariables = new HashSet <string>();
            }

            parms = method.Metadata.GetAttributeParameters("JSIL.Meta.JSEscapingArguments");
            if (parms != null)
            {
                EscapingVariables = new HashSet <string>();
                foreach (var p in parms)
                {
                    var s = p.Value as string;
                    if (s != null)
                    {
                        EscapingVariables.Add(s);
                    }
                }
            }
            else if (!_IsPure)
            {
                EscapingVariables = new HashSet <string>(from p in method.Parameters select p.Name);
            }
            else
            {
                EscapingVariables = new HashSet <string>();
            }

            VariableAliases = new Dictionary <string, HashSet <string> >();

            ResultVariable = null;
            ResultIsNew    = method.Metadata.HasAttribute("JSIL.Meta.JSResultIsNew");

            Trace(method.Member.FullName);
        }
 public DecomposeMutationOperators (
     TypeSystem typeSystem, TypeInfoProvider typeInfo, 
     IFunctionSource functionSource, bool decomposeAllMutations
 ) {
     TypeSystem = typeSystem;
     TypeInfo = typeInfo;
     FunctionSource = functionSource;
     DecomposeAllMutations = decomposeAllMutations;
 }
 public EliminateSingleUseTemporaries (
     QualifiedMemberIdentifier member, IFunctionSource functionSource, 
     TypeSystem typeSystem, Dictionary<string, JSVariable> variables,
     ITypeInfoSource typeInfo
 ) : base (member, functionSource) {
     TypeSystem = typeSystem;
     Variables = variables;
     TypeInfo = typeInfo;
 }
 public SynthesizePropertySetterReturnValues(
     TypeSystem typeSystem, ITypeInfoSource typeInfo,
     IFunctionSource functionSource
     )
 {
     TypeSystem     = typeSystem;
     TypeInfo       = typeInfo;
     FunctionSource = functionSource;
 }
        protected StaticAnalysisJSAstVisitor (QualifiedMemberIdentifier member, IFunctionSource functionSource) {
            if (functionSource == null)
                throw new ArgumentNullException("functionSource");

            Member = member;
            FunctionSource = functionSource;

            // Console.WriteLine("Static analysis visitor used in function {0}", new System.Diagnostics.StackFrame(2).GetMethod().Name);
        }
示例#13
0
 public EliminateSingleUseTemporaries(
     QualifiedMemberIdentifier member, IFunctionSource functionSource,
     TypeSystem typeSystem, Dictionary <string, JSVariable> variables,
     ITypeInfoSource typeInfo
     ) : base(member, functionSource)
 {
     TypeSystem = typeSystem;
     Variables  = variables;
     TypeInfo   = typeInfo;
 }
示例#14
0
 public HoistAllocations (
     QualifiedMemberIdentifier member, 
     IFunctionSource functionSource, 
     TypeSystem typeSystem,
     MethodTypeFactory methodTypes
 ) 
     : base (member, functionSource) {
     TypeSystem = typeSystem;
     MethodTypes = methodTypes;
 }
 public DecomposeMutationOperators(
     TypeSystem typeSystem, TypeInfoProvider typeInfo,
     IFunctionSource functionSource, bool decomposeAllMutations
     )
 {
     TypeSystem            = typeSystem;
     TypeInfo              = typeInfo;
     FunctionSource        = functionSource;
     DecomposeAllMutations = decomposeAllMutations;
 }
示例#16
0
 public UnsafeCodeTransforms (
     Configuration configuration,
     TypeSystem typeSystem, 
     MethodTypeFactory methodTypes,
     IFunctionSource functionSource
 ) {
     Configuration = configuration;
     TypeSystem = typeSystem;
     MethodTypes = methodTypes;
     FunctionSource = functionSource;
 }
 public HoistAllocations(
     QualifiedMemberIdentifier member,
     IFunctionSource functionSource,
     TypeSystem typeSystem,
     MethodTypeFactory methodTypes
     )
     : base(member, functionSource)
 {
     TypeSystem  = typeSystem;
     MethodTypes = methodTypes;
 }
示例#18
0
 public UnsafeCodeTransforms(
     Configuration configuration,
     TypeSystem typeSystem,
     MethodTypeFactory methodTypes,
     IFunctionSource functionSource
     )
 {
     Configuration  = configuration;
     TypeSystem     = typeSystem;
     MethodTypes    = methodTypes;
     FunctionSource = functionSource;
 }
示例#19
0
        protected StaticAnalysisJSAstVisitor(QualifiedMemberIdentifier member, IFunctionSource functionSource)
        {
            if (functionSource == null)
            {
                throw new ArgumentNullException("functionSource");
            }

            Member         = member;
            FunctionSource = functionSource;

            // Console.WriteLine("Static analysis visitor used in function {0}", new System.Diagnostics.StackFrame(2).GetMethod().Name);
        }
        public EliminatePointlessRetargeting (
            QualifiedMemberIdentifier member, 
            IFunctionSource functionSource, 
            TypeSystem typeSystem,
            MethodTypeFactory methodTypes
        ) 
            : base (member, functionSource) {
            TypeSystem = typeSystem;
            MethodTypes = methodTypes;

            SeenRetargetsInScope.Push(new Dictionary<RetargetKey, int>());
            ScopeNodeIndices.Push(-1);
        }
        public EliminatePointlessRetargeting(
            QualifiedMemberIdentifier member,
            IFunctionSource functionSource,
            TypeSystem typeSystem,
            MethodTypeFactory methodTypes
            )
            : base(member, functionSource)
        {
            TypeSystem  = typeSystem;
            MethodTypes = methodTypes;

            SeenRetargetsInScope.Push(new Dictionary <RetargetKey, int>());
            ScopeNodeIndices.Push(-1);
        }
示例#22
0
 public EmulateStructAssignment(
     QualifiedMemberIdentifier member,
     IFunctionSource functionSource,
     TypeSystem typeSystem,
     TypeInfoProvider typeInfo,
     CLRSpecialIdentifiers clr,
     MethodTypeFactory methodTypes,
     bool optimizeCopies
     )
     : base(member, functionSource)
 {
     TypeSystem     = typeSystem;
     TypeInfo       = typeInfo;
     CLR            = clr;
     MethodTypes    = methodTypes;
     OptimizeCopies = optimizeCopies;
 }
示例#23
0
        public IntroduceEnumCasts (
            TypeSystem typeSystem, JSSpecialIdentifiers js, 
            TypeInfoProvider typeInfo, MethodTypeFactory methodTypes,
            IFunctionSource functionSource
        ) {
            TypeSystem = typeSystem;
            TypeInfo = typeInfo;
            MethodTypes = methodTypes;
            JS = js;
            FunctionSource = functionSource;

            LogicalOperators = new HashSet<JSOperator>() {
                JSOperator.LogicalAnd,
                JSOperator.LogicalOr,
                JSOperator.LogicalNot
            };

            BitwiseOperators = new HashSet<JSOperator>() {
                JSOperator.BitwiseAnd,
                JSOperator.BitwiseOr,
                JSOperator.BitwiseXor
            };
        }
 public IntroduceVariableReferences (JSILIdentifier jsil, Dictionary<string, JSVariable> variables, IFunctionSource functionSource) {
     JSIL = jsil;
     Variables = variables;
     FunctionSource = functionSource;
 }
示例#25
0
 public DecomposeMutationOperators (TypeSystem typeSystem, TypeInfoProvider typeInfo, IFunctionSource functionSource) {
     TypeSystem = typeSystem;
     TypeInfo = typeInfo;
     FunctionSource = functionSource;
 }
 public OptimizeArrayEnumerators(QualifiedMemberIdentifier member, IFunctionSource functionSource, TypeSystem typeSystem)
     : base(member, functionSource)
 {
     TypeSystem = typeSystem;
 }
 public EliminatePointlessFinallyBlocks(TypeSystem typeSystem, ITypeInfoSource typeInfo, IFunctionSource functionSource)
 {
     TypeSystem = typeSystem;
     TypeInfo = typeInfo;
     FunctionSource = functionSource;
 }
示例#28
0
 public EliminatePointlessFinallyBlocks(QualifiedMemberIdentifier member, IFunctionSource functionSource, TypeSystem typeSystem, ITypeInfoSource typeInfo)
     : base(member, functionSource)
 {
     TypeSystem = typeSystem;
     TypeInfo   = typeInfo;
 }
示例#29
0
 public IntroduceVariableReferences(JSILIdentifier jsil, Dictionary <string, JSVariable> variables, IFunctionSource functionSource)
 {
     JSIL           = jsil;
     Variables      = variables;
     FunctionSource = functionSource;
 }
 public OptimizeArrayEnumerators(TypeSystem typeSystem, IFunctionSource functionSource)
 {
     TypeSystem = typeSystem;
     FunctionSource = functionSource;
 }
示例#31
0
 public OptimizeArrayEnumerators(TypeSystem typeSystem, IFunctionSource functionSource)
 {
     TypeSystem     = typeSystem;
     FunctionSource = functionSource;
 }
示例#32
0
 public VariableReferenceAccessTransformer(JSILIdentifier jsil, JSVariable variable, IFunctionSource functionSource)
 {
     JSIL           = jsil;
     Variable       = variable;
     FunctionSource = functionSource;
 }
 public EliminatePointlessFinallyBlocks (QualifiedMemberIdentifier member, IFunctionSource functionSource, TypeSystem typeSystem, ITypeInfoSource typeInfo)
     : base (member, functionSource) {
     TypeSystem = typeSystem;
     TypeInfo = typeInfo;
 }
 public EliminatePointlessFinallyBlocks(TypeSystem typeSystem, ITypeInfoSource typeInfo, IFunctionSource functionSource)
 {
     TypeSystem     = typeSystem;
     TypeInfo       = typeInfo;
     FunctionSource = functionSource;
 }
示例#35
0
 public StaticAnalyzer(TypeSystem typeSystem, IFunctionSource functionSource)
 {
     TypeSystem     = typeSystem;
     FunctionSource = functionSource;
 }
示例#36
0
 public EliminateSingleUseTemporaries(TypeSystem typeSystem, Dictionary <string, JSVariable> variables, IFunctionSource functionSource)
 {
     TypeSystem     = typeSystem;
     FunctionSource = functionSource;
     Variables      = variables;
 }
 public EliminateSingleUseTemporaries(TypeSystem typeSystem, Dictionary<string, JSVariable> variables, IFunctionSource functionSource)
 {
     TypeSystem = typeSystem;
     FunctionSource = functionSource;
     Variables = variables;
 }
示例#38
0
 public OptimizeArrayEnumerators(QualifiedMemberIdentifier member, IFunctionSource functionSource, TypeSystem typeSystem)
     : base(member, functionSource)
 {
     TypeSystem = typeSystem;
 }
 public VariableReferenceAccessTransformer (JSILIdentifier jsil, JSVariable variable, IFunctionSource functionSource) {
     JSIL = jsil;
     Variable = variable;
     FunctionSource = functionSource;
 }
示例#40
0
        public FunctionAnalysis2ndPass(IFunctionSource functionSource, FunctionAnalysis1stPass data)
        {
            FunctionSource = functionSource;
            Data           = data;
            _IsPure        = (data.StaticReferences.Count == 0) &&
                             (data.SideEffects.Count == 0);

            VariableAliases = new Dictionary <string, HashSet <string> >();
            foreach (var assignment in data.Assignments)
            {
                if (assignment.SourceVariable != null)
                {
                    HashSet <string> aliases;
                    if (!VariableAliases.TryGetValue(assignment.SourceVariable.Identifier, out aliases))
                    {
                        VariableAliases[assignment.SourceVariable.Identifier] = aliases = new HashSet <string>();
                    }

                    aliases.Add(assignment.Target.Identifier);
                }
            }

            var parameterNames = new HashSet <string>(
                from p in data.Function.Parameters select p.Name
                );

            ModifiedVariables = new HashSet <string>(
                data.ModificationCount.Where((kvp) => {
                var isParameter = parameterNames.Contains(kvp.Key);
                return(kvp.Value >= (isParameter ? 1 : 2));
            }).Select((kvp) => kvp.Key)
                );

            foreach (var v in Data.VariablesPassedByRef)
            {
                ModifiedVariables.Add(v);
            }

            EscapingVariables = Data.EscapingVariables;
            ResultVariable    = Data.ResultVariable;
            ResultIsNew       = Data.ResultIsNew;

            var seenMethods = new HashSet <string>();
            var rm          = Data.ResultMethod;

            while (rm != null)
            {
                var currentMethod = rm.QualifiedIdentifier.ToString();
                if (seenMethods.Contains(currentMethod))
                {
                    break;
                }

                seenMethods.Add(currentMethod);

                var rmfp = functionSource.GetFirstPass(rm.QualifiedIdentifier);
                if (rmfp == null)
                {
                    ResultIsNew = rm.Method.Metadata.HasAttribute("JSIL.Meta.JSResultIsNew");
                    break;
                }

                rm          = rmfp.ResultMethod;
                ResultIsNew = rmfp.ResultIsNew;
            }

            Trace(data.Function.Method.Reference.FullName);
        }
示例#41
0
        public FunctionAnalysis2ndPass(IFunctionSource functionSource, FunctionAnalysis1stPass data)
        {
            FunctionSource = functionSource;
            Data           = data;

            if (data.Function.Method.Method.Metadata.HasAttribute("JSIsPure"))
            {
                _IsPure = true;
            }
            else
            {
                _IsPure = (data.StaticReferences.Count == 0) &&
                          (data.SideEffects.Count == 0);
            }

            VariableAliases = new Dictionary <string, HashSet <string> >();
            foreach (var assignment in data.Assignments)
            {
                if (assignment.SourceVariable != null)
                {
                    HashSet <string> aliases;
                    if (!VariableAliases.TryGetValue(assignment.SourceVariable.Identifier, out aliases))
                    {
                        VariableAliases[assignment.SourceVariable.Identifier] = aliases = new HashSet <string>();
                    }

                    aliases.Add(assignment.Target.Identifier);
                }
            }

            var parameterNames = new HashSet <string>(
                from p in data.Function.Parameters select p.Name
                );

            var parms = data.Function.Method.Method.Metadata.GetAttributeParameters("JSIL.Meta.JSMutatedArguments");

            if (parms != null)
            {
                ModifiedVariables = new HashSet <string>();
                foreach (var p in parms)
                {
                    var s = p.Value as string;
                    if (s != null)
                    {
                        ModifiedVariables.Add(s);
                    }
                }
            }
            else
            {
                ModifiedVariables = new HashSet <string>(
                    data.ModificationCount.Where((kvp) => {
                    var isParameter = parameterNames.Contains(kvp.Key);
                    return(kvp.Value >= (isParameter ? 1 : 2));
                }).Select((kvp) => kvp.Key)
                    );

                foreach (var v in Data.VariablesPassedByRef)
                {
                    ModifiedVariables.Add(v);
                }
            }

            parms = data.Function.Method.Method.Metadata.GetAttributeParameters("JSIL.Meta.JSEscapingArguments");
            if (parms != null)
            {
                EscapingVariables = new HashSet <string>();
                foreach (var p in parms)
                {
                    var s = p.Value as string;
                    if (s != null)
                    {
                        EscapingVariables.Add(s);
                    }
                }
            }
            else
            {
                EscapingVariables = Data.EscapingVariables;

                // Scan over all the invocations performed by this function and see if any of them cause
                //  a variable to escape
                FunctionAnalysis2ndPass invocationSecondPass;
                foreach (var invocation in Data.Invocations)
                {
                    if (invocation.Method != null)
                    {
                        invocationSecondPass = functionSource.GetSecondPass(invocation.Method);
                    }
                    else
                    {
                        invocationSecondPass = null;
                    }

                    foreach (var invocationKvp in invocation.Variables)
                    {
                        bool escapes;

                        if (invocationSecondPass != null)
                        {
                            escapes = invocationSecondPass.EscapingVariables.Contains(invocationKvp.Key);
                        }
                        else
                        {
                            escapes = true;
                        }

                        if (escapes)
                        {
                            foreach (var variableName in invocationKvp.Value)
                            {
                                Data.EscapingVariables.Add(variableName);
                            }
                        }
                    }
                }
            }

            ResultVariable = Data.ResultVariable;
            ResultIsNew    = Data.ResultIsNew;

            var seenMethods = new HashSet <string>();
            var rm          = Data.ResultMethod;

            while (rm != null)
            {
                var currentMethod = rm.QualifiedIdentifier.ToString();
                if (seenMethods.Contains(currentMethod))
                {
                    break;
                }

                seenMethods.Add(currentMethod);

                var rmfp = functionSource.GetFirstPass(rm.QualifiedIdentifier);
                if (rmfp == null)
                {
                    ResultIsNew = rm.Method.Metadata.HasAttribute("JSIL.Meta.JSResultIsNew");
                    break;
                }

                rm          = rmfp.ResultMethod;
                ResultIsNew = rmfp.ResultIsNew;
            }

            Trace(data.Function.Method.Reference.FullName);
        }