Beispiel #1
0
        internal FunctionAnalysisUnit AddFunction(FunctionObject node, AnalysisUnit outerUnit, bool isExpression = false)
        {
            EnvironmentRecord scope;

            if (!_scope.GlobalEnvironment.TryGetNodeEnvironment(node, out scope))
            {
                if (node.Body == null)
                {
                    return(null);
                }

                IAnalysisSet      functionObj;
                UserFunctionValue func = null;
                if (!_scope.GlobalEnvironment.TryGetNodeValue(NodeEnvironmentKind.UserFunctionValue, node, out functionObj))
                {
                    func = CreateUserFunction(node, outerUnit);
                }
                else
                {
                    func = (UserFunctionValue)functionObj;
                }

                var funcScope = GetFunctionEnvironment(func);
                scope = funcScope;

                VariableDef[] parameters = new VariableDef[node.ParameterDeclarations != null ? node.ParameterDeclarations.Length : 0];
                for (int i = 0; i < parameters.Length; i++)
                {
                    parameters[i] = funcScope.AddLocatedVariable(
                        node.ParameterDeclarations[i].Name,
                        node.ParameterDeclarations[i],
                        _curUnit.ProjectEntry
                        );
                }

                _scope.Children.Add(scope);
                _scope.GlobalEnvironment.AddNodeEnvironment(node, scope);

                if (!isExpression && node.Name != null)
                {
                    // lambdas don't have their names published
                    var funcVar = _scope.AddLocatedVariable(node.Name, node, funcScope.AnalysisUnit);
                    funcVar.AddTypes(funcScope.AnalysisUnit, func.SelfSet);
                }

                funcScope.AnalysisUnit.Enqueue();
            }

            return(((FunctionEnvironmentRecord)scope).AnalysisUnit);
        }