internal void Reference(LabelScopeInfo block)
 {
     _references.Add(block);
     if (HasDefinitions)
     {
         ValidateJump(block);
     }
 }
        private void ValidateJump(LabelScopeInfo reference)
        {
            // look for a simple jump out
            for (var j = reference; j != null; j = j.Parent)
            {
                if (DefinedIn(j))
                {
                    // found it, jump is valid!
                    return;
                }
                if (j.Kind == LabelScopeKind.Finally || j.Kind == LabelScopeKind.Filter)
                {
                    break;
                }
            }

            _acrossBlockJump = true;

            if (HasMultipleDefinitions)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Ambiguous jump {0}", _node.Name));
            }

            // We didn't find an outward jump. Look for a jump across blocks
            var def    = FirstDefinition();
            var common = CommonNode(def, reference, b => b.Parent);

            // Validate that we aren't jumping across a finally
            for (var j = reference; j != common; j = j.Parent)
            {
                if (j.Kind == LabelScopeKind.Finally)
                {
                    throw new InvalidOperationException("Control cannot leave finally");
                }
                if (j.Kind == LabelScopeKind.Filter)
                {
                    throw new InvalidOperationException("Control cannot leave filter test");
                }
            }

            // Valdiate that we aren't jumping into a catch or an expression
            for (var j = def; j != common; j = j.Parent)
            {
                if (!j.CanJumpInto)
                {
                    if (j.Kind == LabelScopeKind.Expression)
                    {
                        throw new InvalidOperationException("Control cannot enter an expression");
                    }
                    else
                    {
                        throw new InvalidOperationException("Control cannot enter try");
                    }
                }
            }
        }
        private bool DefinedIn(LabelScopeInfo scope)
        {
            if (_definitions == scope)
            {
                return(true);
            }

            var definitions = _definitions as HashSet <LabelScopeInfo>;

            if (definitions != null)
            {
                return(definitions.Contains(scope));
            }
            return(false);
        }
 private void AddDefinition(LabelScopeInfo scope)
 {
     if (_definitions == null)
     {
         _definitions = scope;
     }
     else
     {
         var set = _definitions as HashSet <LabelScopeInfo>;
         if (set == null)
         {
             _definitions = set = new HashSet <LabelScopeInfo> {
                 (LabelScopeInfo)_definitions
             };
         }
         set.Add(scope);
     }
 }
        internal void Define(LabelScopeInfo block)
        {
            // Prevent the label from being shadowed, which enforces cleaner
            // trees. Also we depend on this for simplicity (keeping only one
            // active IL Label per LabelInfo)
            for (var j = block; j != null; j = j.Parent)
            {
                if (j.ContainsTarget(_node))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Label target already defined: {0}", _node.Name));
                }
            }

            AddDefinition(block);
            block.AddLabelInfo(_node, this);

            // Once defined, validate all jumps
            if (HasDefinitions && !HasMultipleDefinitions)
            {
                foreach (var r in _references)
                {
                    ValidateJump(r);
                }
            }
            else
            {
                // Was just redefined, if we had any across block jumps, they're
                // now invalid
                if (_acrossBlockJump)
                {
                    throw new InvalidOperationException("Ambiguous jump");
                }
                // For local jumps, we need a new IL label
                // This is okay because:
                //   1. no across block jumps have been made or will be made
                //   2. we don't allow the label to be shadowed
                _label = null;
            }
        }
 internal LabelScopeInfo(LabelScopeInfo parent, LabelScopeKind kind)
 {
     Parent = parent;
     Kind   = kind;
 }