Exemplo n.º 1
0
		public static List<Statement> ExtractContractBlocks (List<Statement> blocks, int firstBlockIndex, int firstStmtIndex, int lastBlockIndex, int lastStmtIndex)
		{
			var result = new List<Statement> ();
			var firstBlock = (Block) blocks [firstBlockIndex];
			var block = new Block (new List<Statement> ());
			if (firstBlock != null) {
				int cnt = firstBlockIndex == lastBlockIndex ? lastStmtIndex + 1 : firstBlock.Statements.Count;
				for (int i = firstStmtIndex; i < cnt; i++) {
					Statement stmt = firstBlock.Statements [i];
					block.Statements.Add (stmt);
					if (stmt != null)
						firstBlock.Statements [i] = null;
				}
			}
			result.Add (block);
			int nextIndex = firstBlockIndex + 1;
			if (nextIndex > lastBlockIndex)
				return result;
			Block newLastBlock = null;
			int lastFullBlockIndex = lastBlockIndex - 1;
			var lastBlock = (Block) blocks [lastBlockIndex];
			if (lastBlock != null && lastStmtIndex == lastBlock.Statements.Count - 1)
				lastFullBlockIndex = lastBlockIndex;
			else {
				newLastBlock = new Block (new List<Statement> ());
				if (block.Statements != null && block.Statements.Count > 0) {
					var branch = block.Statements [block.Statements.Count - 1] as Branch;
					if (branch != null && branch.Target != null && branch.Target == lastBlock)
						branch.Target = newLastBlock;
				}
			}

			for (; nextIndex < lastFullBlockIndex; ++nextIndex) {
				var curBlock = (Block) blocks [nextIndex];
				result.Add (curBlock);
				if (curBlock != null) {
					blocks [nextIndex] = null;
					if (newLastBlock != null && curBlock.Statements != null && curBlock.Statements.Count > 0) {
						var branch = curBlock.Statements [curBlock.Statements.Count - 1] as Branch;
						if (branch != null && branch.Target != null && branch.Target == lastBlock)
							branch.Target = newLastBlock;
					}
				}
			}

			if (newLastBlock != null) {
				for (int i = 0; i < lastStmtIndex + 1; i++) {
					newLastBlock.Statements.Add (lastBlock.Statements [i]);
					lastBlock.Statements [i] = null;
				}

				result.Add (newLastBlock);
			}
			return result;
		}
Exemplo n.º 2
0
		private bool ExtractPrePostConditionsFromContractSection (List<Statement> contractSection,
		                                                          List<Requires> preconditions, List<Ensures> postconditions)
		{
			List<Statement> blocks = contractSection;
			int firstBlockIndex = 0;
			int blocksCount = blocks.Count;
			int firstStmtIndex = HelperMethods.FindNextRealStatement (((Block) blocks [firstBlockIndex]).Statements, 0);

			bool wasEndContractBlock = false;

			for (int lastBlockIndex = firstBlockIndex; lastBlockIndex < blocksCount; ++lastBlockIndex) {
				var block = (Block) blocks [lastBlockIndex];
				if (block == null)
					continue;

				int cnt = block.Statements == null ? 0 : block.Statements.Count;
				for (int lastStmtIndex = 0; lastStmtIndex < cnt; ++lastStmtIndex) {
					Statement s = block.Statements [lastStmtIndex];
					if (s == null)
						continue;

					Method calledMethod = HelperMethods.IsMethodCall (s);
					if (!this.contract_nodes.IsContractMethod (calledMethod))
						continue;

					if (wasEndContractBlock) {
						if (DebugOptions.Debug)
							Console.WriteLine ("Contract call after prior ContractBlock");
						break;
					}
					if (this.contract_nodes.IsEndContractBlock (calledMethod)) {
						wasEndContractBlock = true;
						continue;
					}

					//here we definitely know that s is ExpressionStatement of (MethodCall). see HelperMethods.IsMethodCall(s)
					var methodCall = ((ExpressionStatement) s).Expression as MethodCall;
					Expression assertionExpression = methodCall.Arguments [0];
					Expression expression;
					if (firstBlockIndex == lastBlockIndex && firstStmtIndex == lastStmtIndex)
						expression = assertionExpression;
					else {
						block.Statements [lastStmtIndex] = new ExpressionStatement (assertionExpression);
						List<Statement> contractBlocks = HelperMethods.ExtractContractBlocks (blocks, firstBlockIndex, firstStmtIndex,
						                                                                      lastBlockIndex, lastStmtIndex);
						var b = new Block (contractBlocks);

						expression = new BlockExpression (b);
					}

					MethodContractElement methodContractElement = null;
					if (this.contract_nodes.IsPlainPrecondition (calledMethod)) {
						var requires = new Requires (expression);

						methodContractElement = requires;
					} else if (this.contract_nodes.IsPostCondition (calledMethod))
						methodContractElement = new Ensures (expression);

					if (methodContractElement == null)
						throw new InvalidOperationException ("Unrecognized contract method");

					if (methodCall.Arguments.Count > 1) {
						Expression userMessage = methodCall.Arguments [1];
						methodContractElement.UserMessage = userMessage;
					}

					switch (methodContractElement.NodeType) {
					case NodeType.Requires:
						var requires = (Requires) methodContractElement;
						preconditions.Add (requires);
						break;
					case NodeType.Ensures:
						var ensures = (Ensures) methodContractElement;
						postconditions.Add (ensures);
						break;
					}
				}
			}

			return true;
		}
Exemplo n.º 3
0
		public static Local ExtractPreamble (Method method, ContractNodes contractNodes, Block contractInitializer, out Block postPreamble)
		{
			postPreamble = null;
			return null;
		}
Exemplo n.º 4
0
		private Block GetOrCreateBlock (int address)
		{
			Block block;
			if (!this.block_map.TryGetValue (address, out block)) {
				this.block_map [address] = (block = new Block (new List<Statement> ()));
				block.ILOffset = address;
			}
			return block;
		}