Example #1
0
 internal void Reference(LabelScopeInfo block)
 {
     _references.Add(block);
     if (HasDefinitions)
     {
         ValidateJump(block);
     }
 }
Example #2
0
        private LabelScopeInfo FirstDefinition()
        {
            LabelScopeInfo scope = _definitions as LabelScopeInfo;

            if (scope != null)
            {
                return(scope);
            }
            return(((HashSet <LabelScopeInfo>)_definitions).First());
        }
Example #3
0
        private void ValidateJump(LabelScopeInfo reference)
        {
            // look for a simple jump out
            for (LabelScopeInfo j = reference; j != null; j = j.Parent)
            {
                if (DefinedIn(j))
                {
                    // found it, jump is valid!
                    return;
                }
                if (j.Kind == LabelScopeKind.Filter)
                {
                    break;
                }
            }

            _acrossBlockJump = true;

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

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

            // Validate that we aren't jumping across a finally
            for (LabelScopeInfo j = reference; j != common; j = j.Parent)
            {
                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 (LabelScopeInfo 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");
                    }
                }
            }
        }
Example #4
0
        private bool DefinedIn(LabelScopeInfo scope)
        {
            if (_definitions == scope)
            {
                return(true);
            }

            if (_definitions is HashSet <LabelScopeInfo> definitions)
            {
                return(definitions.Contains(scope));
            }
            return(false);
        }
Example #5
0
 private void AddDefinition(LabelScopeInfo scope)
 {
     if (_definitions == null)
     {
         _definitions = scope;
     }
     else
     {
         if (!(_definitions is HashSet <LabelScopeInfo> set))
         {
             _definitions = set = new HashSet <LabelScopeInfo>()
             {
                 (LabelScopeInfo)_definitions
             };
         }
         set.Add(scope);
     }
 }
Example #6
0
        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 (LabelScopeInfo j = block; j != null; j = j.Parent)
            {
                if (j.ContainsTarget(_node))
                {
                    throw new InvalidOperationException(String.Format("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;
            }
        }
Example #7
0
 internal LabelScopeInfo(LabelScopeInfo parent, LabelScopeKind kind)
 {
     Parent = parent;
     Kind   = kind;
 }