// // Subroutine.cs // // Authors: // Alexander Chebaturkin (chebaturkin@gmail.com) // // Copyright (C) 2011 Alexander Chebaturkin // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Collections.Generic; using System.IO; using Mono.CodeContracts.Static.Analysis; using Mono.CodeContracts.Static.DataStructures; namespace Mono.CodeContracts.Static.ControlFlow { abstract class Subroutine : IPropertyCollection, IEquatable { private static int _subroutineIdGenerator; private readonly PropertyCollection properties = new PropertyCollection (); private readonly int subroutine_id = _subroutineIdGenerator++; public virtual SubroutineKind Kind { get { return SubroutineKind.Unknown; } } public int Id { get { return this.subroutine_id; } } public abstract CFGBlock Entry { get; } public abstract CFGBlock EntryAfterRequires { get; } public abstract CFGBlock Exit { get; } public abstract CFGBlock ExceptionExit { get; } public abstract string Name { get; } public abstract int BlockCount { get; } public abstract IEnumerable Blocks { get; } public virtual bool IsRequires { get { return false; } } public virtual bool IsEnsures { get { return false; } } public virtual bool IsOldValue { get { return false; } } public virtual bool IsMethod { get { return false; } } public virtual bool IsConstructor { get { return false; } } public virtual bool IsInvariant { get { return false; } } public virtual bool IsContract { get { return false; } } public bool IsEnsuresOrOldValue { get { return IsEnsures || IsOldValue; } } public abstract EdgeMap SuccessorEdges { get; } public abstract EdgeMap PredecessorEdges { get; } public abstract DepthFirst.Visitor EdgeInfo { get; } public virtual bool IsFaultFinally { get { return false; } } public abstract bool HasReturnValue { get; } public abstract bool HasContextDependentStackDepth { get; } public abstract int StackDelta { get; } #region IPropertyCollection Members public bool TryGetValue (TypedKey key, out T value) { return this.properties.TryGetValue (key, out value); } #endregion #region Implementation of IPropertyCollection public bool Contains (TypedKey key) { return this.properties.Contains (key); } public void Add (TypedKey key, T value) { this.properties.Add (key, value); } #endregion public override string ToString () { return string.Format ("SR {0}: BlockCount:{1}, Kind:{2}", Id, BlockCount, Kind); } public abstract IEnumerable SuccessorBlocks (CFGBlock block); public IEnumerable> SuccessorEdgesFor (CFGBlock block) { return SuccessorEdges [block]; } public abstract IEnumerable PredecessorBlocks (CFGBlock block); public abstract bool IsJoinPoint (CFGBlock block); public abstract bool IsSplitPoint (CFGBlock block); public abstract bool HasSingleSuccessor (APC point, out APC ifFound); public abstract bool HasSinglePredecessor (APC point, out APC ifFound); public abstract void AddEdgeSubroutine (CFGBlock from, CFGBlock to, Subroutine subroutine, EdgeTag tag); public abstract IEnumerable Successors (APC pc); public abstract IEnumerable Predecessors (APC pc); public abstract bool IsSubroutineEnd (CFGBlock block); public abstract bool IsSubroutineStart (CFGBlock block); public abstract bool IsCatchFilterHeader (CFGBlock block); public abstract APC ComputeTargetFinallyContext (APC pc, CFGBlock succ); public abstract Sequence> EdgeSubroutinesOuterToInner (CFGBlock current, CFGBlock succ, out bool isExceptionHandlerEdge, Sequence> context); public abstract Sequence> GetOrdinaryEdgeSubroutines (CFGBlock current, CFGBlock succ, Sequence> context); public abstract void Initialize (); public abstract IEnumerable UsedSubroutines (HashSet alreadySeen); public IEnumerable UsedSubroutines () { return UsedSubroutines (new HashSet ()); } public abstract IEnumerable ExceptionHandlers (CFGBlock block, Subroutine innerSubroutine, Data data, IHandlerFilter handlerPredicate); public abstract void Print (TextWriter tw, ILPrinter printer, Func>>> contextLookup, Sequence> context, HashSet>>> set); #region Implementation of IEquatable public bool Equals (Subroutine other) { return Id == other.Id; } #endregion } }