Beispiel #1
0
        public FunctionAnalysisUnit(FunctionAnalysisUnit originalUnit, CallChain callChain, ArgumentSet callArgs)
            : base(originalUnit.Ast, null) {
            _originalUnit = originalUnit;
            _declUnit = originalUnit._declUnit;
            Function = originalUnit.Function;

            CallChain = callChain;

            var scope = new FunctionScope(
                Function,
                Ast,
                originalUnit.Scope.OuterScope,
                originalUnit.DeclaringModule.ProjectEntry
            );
            scope.UpdateParameters(this, callArgs, false, originalUnit.Scope as FunctionScope);
            _scope = scope;

            var walker = new OverviewWalker(_originalUnit.ProjectEntry, this, Tree);
            if (Ast.Body != null) {
                Ast.Body.Walk(walker);
            }

            AnalysisLog.NewUnit(this);
            Enqueue();
        }
Beispiel #2
0
        public CalledFunctionAnalysisUnit(IVersioned agg, FunctionAnalysisUnit originalUnit, CallChain callChain, ArgumentSet callArgs)
            : base(originalUnit.Function, originalUnit._declUnit)
        {
            _originalUnit = originalUnit;
            _agg          = agg;
            CallChain     = callChain;

            var scope = new FunctionScope(
                Function,
                Ast,
                originalUnit.Scope.OuterScope,
                originalUnit.DeclaringModule.ProjectEntry
                );

            scope.UpdateParameters(this, callArgs, false, originalUnit.Scope as FunctionScope);
            _scope = scope;

            var walker = new OverviewWalker(_originalUnit.ProjectEntry, this, Tree);

            if (Ast.Body != null)
            {
                Ast.Body.Walk(walker);
            }

            AnalysisLog.NewUnit(this);
            Enqueue();
        }
Beispiel #3
0
        internal void EnsureParameters(FunctionAnalysisUnit unit)
        {
            var astParams = Function.FunctionDefinition.Parameters;

            for (int i = 0; i < astParams.Count; ++i)
            {
                VariableDef param;
                if (!TryGetVariable(astParams[i].Name, out param))
                {
                    var n = (Node)astParams[i].NameExpression ?? astParams[i];
                    if (astParams[i].Kind == ParameterKind.List)
                    {
                        param = _seqParameters = _seqParameters ?? new ListParameterVariableDef(unit, n);
                    }
                    else if (astParams[i].Kind == ParameterKind.Dictionary)
                    {
                        param = _dictParameters = _dictParameters ?? new DictParameterVariableDef(unit, n);
                    }
                    else
                    {
                        param = new LocatedVariableDef(unit.ProjectEntry, n);
                    }
                    AddVariable(astParams[i].Name, param);
                }
            }
        }
Beispiel #4
0
        internal bool UpdateParameters(FunctionAnalysisUnit unit, ArgumentSet others, bool enqueue = true, FunctionScope scopeWithDefaultParameters = null)
        {
            EnsureParameters(unit);

            var  astParams = Function.FunctionDefinition.Parameters;
            bool added     = false;
            var  entry     = unit.DependencyProject;
            var  state     = unit.State;
            var  limits    = state.Limits;

            for (int i = 0; i < others.Args.Length && i < astParams.Count; ++i)
            {
                VariableDef param;
                if (!TryGetVariable(astParams[i].Name, out param))
                {
                    Debug.Assert(false, "Parameter " + astParams[i].Name + " has no variable in this scope");
                    param = AddVariable(astParams[i].Name);
                }
                param.MakeUnionStrongerIfMoreThan(limits.NormalArgumentTypes, others.Args[i]);
                added |= param.AddTypes(entry, others.Args[i], false, unit.ProjectEntry);
            }
            if (_seqParameters != null)
            {
                _seqParameters.List.MakeUnionStrongerIfMoreThan(limits.ListArgumentTypes, others.SequenceArgs);
                added |= _seqParameters.List.AddTypes(unit, new[] { others.SequenceArgs });
            }
            if (_dictParameters != null)
            {
                _dictParameters.Dict.MakeUnionStrongerIfMoreThan(limits.DictArgumentTypes, others.DictArgs);
                added |= _dictParameters.Dict.AddTypes(Function.FunctionDefinition, unit, state.GetConstant(""), others.DictArgs);
            }

            if (scopeWithDefaultParameters != null)
            {
                for (int i = 0; i < others.Args.Length && i < astParams.Count; ++i)
                {
                    VariableDef defParam, param;
                    if (TryGetVariable(astParams[i].Name, out param) &&
                        !param.HasTypes &&
                        scopeWithDefaultParameters.TryGetVariable(astParams[i].Name, out defParam))
                    {
                        param.MakeUnionStrongerIfMoreThan(
                            limits.NormalArgumentTypes,
                            defParam.GetTypesNoCopy(unit, AnalysisValue.DeclaringModule)
                            );
                        added |= param.AddTypes(entry, defParam.GetTypesNoCopy(unit, AnalysisValue.DeclaringModule), false, unit.ProjectEntry);
                    }
                }
            }

            if (enqueue && added)
            {
                unit.Enqueue();
            }
            return(added);
        }
Beispiel #5
0
        internal FunctionClosureAnalysisUnit(IVersioned agg, FunctionAnalysisUnit originalUnit, CallChain callChain) :
            base(originalUnit.Function, originalUnit._declUnit, originalUnit._declUnit.Scope, originalUnit.ProjectEntry, true)
        {
            _originalUnit = originalUnit;
            _agg          = agg;
            CallChain     = callChain;
            _originalUnit.Scope.AddLinkedScope(Scope);

            var node = originalUnit.Function.FunctionDefinition;

            node.Body.Walk(new OverviewWalker(originalUnit.ProjectEntry, this, originalUnit.Tree));

            AnalysisLog.NewUnit(this);
        }
Beispiel #6
0
        internal void EnsureParameters(FunctionAnalysisUnit unit, bool usePlaceholders)
        {
            var astParams = Function.FunctionDefinition.ParametersInternal;

            for (int i = 0; i < astParams.Length; ++i)
            {
                var p    = astParams[i];
                var name = p?.Name;
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }

                var node = (Node)astParams[i].NameExpression ?? astParams[i];

                if (astParams[i].Kind == ParameterKind.List)
                {
                    if (_seqParameters == null)
                    {
                        _seqParameters = new ListParameterVariableDef(unit, p, name);
                        AddParameter(unit, name, p, usePlaceholders ? null : _seqParameters);
                    }
                }
                else if (astParams[i].Kind == ParameterKind.Dictionary)
                {
                    if (_dictParameters == null)
                    {
                        _dictParameters = new DictParameterVariableDef(unit, p, name);
                        AddParameter(unit, name, p, usePlaceholders ? null : _dictParameters);
                    }
                }
                else if (!_parameters.ContainsKey(name))
                {
                    var v = _parameters[name] = new LocatedVariableDef(unit.ProjectEntry, new EncodedLocation(unit, p));
                    if (i == 0 &&
                        p.Kind == ParameterKind.Normal &&
                        !Function.IsStatic &&
                        Function.FunctionDefinition.Parent is ClassDefinition)
                    {
                        AddParameter(unit, name, p, v);
                    }
                    else
                    {
                        AddParameter(unit, name, p, usePlaceholders ? null : v);
                    }
                }
            }
        }
Beispiel #7
0
 internal void EnsureParameters(FunctionAnalysisUnit unit) {
     var astParams = Function.FunctionDefinition.Parameters;
     for (int i = 0; i < astParams.Count; ++i) {
         VariableDef param;
         if (!TryGetVariable(astParams[i].Name, out param)) {
             if (astParams[i].Kind == ParameterKind.List) {
                 param = _seqParameters = _seqParameters ?? new ListParameterVariableDef(unit, astParams[i]);
             } else if (astParams[i].Kind == ParameterKind.Dictionary) {
                 param = _dictParameters = _dictParameters ?? new DictParameterVariableDef(unit, astParams[i]);
             } else {
                 param = new LocatedVariableDef(unit.ProjectEntry, astParams[i]);
             }
             AddVariable(astParams[i].Name, param);
         }
     }
 }
Beispiel #8
0
        internal void EnsureParameterZero(FunctionAnalysisUnit unit)
        {
            var p = Function.FunctionDefinition.ParametersInternal.FirstOrDefault();

            if (!string.IsNullOrEmpty(p?.Name) &&
                p.Kind == ParameterKind.Normal &&
                !unit.Function.IsStatic &&
                unit.Scope.OuterScope is ClassScope cls &&
                _parameters.TryGetValue(p.Name, out var v)
                )
            {
                v.AddTypes(unit, unit.Function.IsClassMethod ?
                           cls.Class.SelfSet :
                           cls.Class.Instance.SelfSet,
                           enqueue: false);
            }
        }
Beispiel #9
0
        internal bool UpdateParameters(FunctionAnalysisUnit unit, ArgumentSet others, bool enqueue = true, FunctionScope scopeWithDefaultParameters = null) {
            EnsureParameters(unit);

            var astParams = Function.FunctionDefinition.Parameters;
            bool added = false;
            var entry = unit.ProjectEntry;
            var state = unit.ProjectState;
            var limits = state.Limits;

            for (int i = 0; i < others.Args.Length && i < astParams.Count; ++i) {
                VariableDef param;
                if (!TryGetVariable(astParams[i].Name, out param)) {
                    Debug.Assert(false, "Parameter " + astParams[i].Name + " has no variable in this scope");
                    param = AddVariable(astParams[i].Name);
                }
                param.MakeUnionStrongerIfMoreThan(limits.NormalArgumentTypes, others.Args[i]);
                added |= param.AddTypes(entry, others.Args[i], false);
            }
            if (_seqParameters != null) {
                _seqParameters.List.MakeUnionStrongerIfMoreThan(limits.ListArgumentTypes, others.SequenceArgs);
                added |= _seqParameters.List.AddTypes(unit, new[] { others.SequenceArgs });
            }
            if (_dictParameters != null) {
                _dictParameters.Dict.MakeUnionStrongerIfMoreThan(limits.DictArgumentTypes, others.DictArgs);
                added |= _dictParameters.Dict.AddTypes(Function.FunctionDefinition, unit, state.GetConstant(""), others.DictArgs);
            }

            if (scopeWithDefaultParameters != null) {
                for (int i = 0; i < others.Args.Length && i < astParams.Count; ++i) {
                    VariableDef defParam, param;
                    if (TryGetVariable(astParams[i].Name, out param) &&
                        !param.TypesNoCopy.Any() &&
                        scopeWithDefaultParameters.TryGetVariable(astParams[i].Name, out defParam)) {
                        param.MakeUnionStrongerIfMoreThan(limits.NormalArgumentTypes, defParam.TypesNoCopy);
                        added |= param.AddTypes(entry, defParam.TypesNoCopy, false);
                    }
                }
            }

            if (enqueue && added) {
                unit.Enqueue();
            }
            return added;
        }
Beispiel #10
0
        internal bool UpdateParameters(
            FunctionAnalysisUnit unit,
            ArgumentSet others,
            bool enqueue = true,
            FunctionScope scopeWithDefaultParameters = null,
            bool usePlaceholders = false
            )
        {
            EnsureParameters(unit, usePlaceholders);

            var astParams = Function.FunctionDefinition.ParametersInternal;
            var added     = false;
            var entry     = unit.DependencyProject;
            var state     = unit.State;
            var limits    = state.Limits;

            for (int i = 0; i < others.Args.Length && i < astParams.Length; ++i)
            {
                var         name = astParams[i].Name;
                VariableDef param;
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }
                if (name == _seqParameters?.Name)
                {
                    param = _seqParameters;
                }
                else if (name == _dictParameters?.Name)
                {
                    param = _dictParameters;
                }
                else if (!_parameters.TryGetValue(name, out param))
                {
                    Debug.Fail($"Parameter {name} has no variable in this function");
                    _parameters[name] = param = new LocatedVariableDef(Function.AnalysisUnit.ProjectEntry,
                                                                       new EncodedLocation(unit, (Node)astParams[i].NameExpression ?? astParams[i]));
                }
                var arg = others.Args[i].Resolve(unit);
                param.MakeUnionStrongerIfMoreThan(limits.NormalArgumentTypes, arg);
                added |= param.AddTypes(entry, arg, enqueue, unit.ProjectEntry);
            }
            if (_seqParameters != null)
            {
                var arg = others.SequenceArgs.Resolve(unit);
                _seqParameters.List.MakeUnionStrongerIfMoreThan(limits.ListArgumentTypes, arg);
                added |= _seqParameters.List.AddTypes(unit, new[] { arg });
            }
            if (_dictParameters != null)
            {
                var arg = others.DictArgs.Resolve(unit);
                _dictParameters.Dict.MakeUnionStrongerIfMoreThan(limits.DictArgumentTypes, arg);
                added |= _dictParameters.Dict.AddTypes(Function.FunctionDefinition, unit, state.GetConstant(""), arg);
            }

            if (scopeWithDefaultParameters != null)
            {
                for (int i = 0; i < others.Args.Length && i < astParams.Length; ++i)
                {
                    VariableDef defParam, param;
                    if (TryGetVariable(astParams[i].Name, out param) &&
                        !param.HasTypes &&
                        scopeWithDefaultParameters.TryGetVariable(astParams[i].Name, out defParam))
                    {
                        param.MakeUnionStrongerIfMoreThan(
                            limits.NormalArgumentTypes,
                            defParam.GetTypesNoCopy(unit, AnalysisValue.DeclaringModule)
                            );
                        added |= param.AddTypes(entry, defParam.GetTypesNoCopy(unit, AnalysisValue.DeclaringModule), enqueue, unit.ProjectEntry);
                    }
                }
            }

            if (enqueue && added)
            {
                unit.Enqueue();
            }
            return(added);
        }