Example #1
0
File: Scope.cs Project: taekun/lens
        /// <summary>
        /// Registers a name being referenced during closure detection.
        /// </summary>
        /// <returns>True if the local name has been found. Otherwise false.</returns>
        public bool ReferenceLocal(Context ctx, string name)
        {
            var scope      = this;
            var isClosured = false;

            while (scope != null)
            {
                Local local;
                if (scope.Locals.TryGetValue(name, out local))
                {
                    if (isClosured)
                    {
                        if (local.IsRefArgument)
                        {
                            Context.Error(CompilerMessages.ClosureRef, local.Name);
                        }

                        createClosureType(ctx, scope);
                        local.IsClosured = true;
                    }

                    return(true);
                }

                if (scope.Kind == ScopeKind.LambdaRoot)
                {
                    if (!isClosured)
                    {
                        isClosured = true;
                    }
                    else
                    {
                        scope.ClosureReferencesOuter = true;
                    }
                }

                scope = scope.OuterScope;
            }

            return(false);
        }