当前位置: 首页>>代码示例>>C++>>正文


C++ FunctionDefinition::body方法代码示例

本文整理汇总了C++中FunctionDefinition::body方法的典型用法代码示例。如果您正苦于以下问题:C++ FunctionDefinition::body方法的具体用法?C++ FunctionDefinition::body怎么用?C++ FunctionDefinition::body使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在FunctionDefinition的用法示例。


在下文中一共展示了FunctionDefinition::body方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: toFormalType


//.........这里部分代码省略.........
	add(":");

	indent();
	indent();
	string retString = "(";
	for (auto const& retParam: _function.returnParameters())
	{
		string paramType;
		try
		{
			paramType = toFormalType(*retParam->annotation().type);
		}
		catch (NoFormalType &err)
		{
			string const* typeName = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
			error(*retParam, "Parameter type " + (typeName ? *typeName : "") + " not supported.");
		}
		if (retString.size() != 1)
			retString += ", ";
		retString += paramType;
	}
	add(retString + ")");
	unindent();

	addSourceFromDocStrings(_function.annotation());
	if (!m_currentContract.contract)
		error(_function, "Only functions inside contracts allowed.");
	addSourceFromDocStrings(m_currentContract.contract->annotation());

	if (_function.isDeclaredConst())
		addLine("ensures { (old this) = this }");
	else
		addLine("writes { this }");

	addLine("=");

	// store the prestate in the case we need to revert
	addLine("let prestate = {balance = this.balance; storage = " + copyOfStorage() + "} in ");

	// initialise local variables
	for (auto const& variable: _function.parameters())
		addLine("let _" + variable->name() + " = ref arg_" + variable->name() + " in");
	for (auto const& variable: _function.returnParameters())
	{
		if (variable->name().empty())
			error(*variable, "Unnamed return variables not yet supported.");
		string varType;
		try
		{
			varType = toFormalType(*variable->annotation().type);
		}
		catch (NoFormalType &err)
		{
			string const* typeNamePtr = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
			error(*variable, "Type " + (typeNamePtr ? *typeNamePtr : "") + "in return parameter not yet supported.");
		}
		addLine("let _" + variable->name() + ": ref " + varType + " = ref (of_int 0) in");
	}
	for (VariableDeclaration const* variable: _function.localVariables())
	{
		if (variable->name().empty())
			error(*variable, "Unnamed variables not yet supported.");
		string varType;
		try
		{
			varType = toFormalType(*variable->annotation().type);
		}
		catch (NoFormalType &err)
		{
			string const* typeNamePtr = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
			error(*variable, "Type " + (typeNamePtr ? *typeNamePtr : "") + "in variable declaration not yet supported.");
		}
		addLine("let _" + variable->name() + ": ref " + varType + " = ref (of_int 0) in");
	}
	addLine("try");

	_function.body().accept(*this);
	add(";");
	addLine("raise Return");

	string retVals;
	for (auto const& variable: _function.returnParameters())
	{
		if (!retVals.empty())
			retVals += ", ";
		retVals += "!_" + variable->name();
	}
	addLine("with Return -> (" + retVals + ") |");
	string reversion = "     Revert -> this.balance <- prestate.balance; ";
	for (auto const* variable: m_currentContract.stateVariables)
		reversion += "this.storage._" + variable->name() + " <- prestate.storage._" + variable->name() + "; ";
	//@TODO in case of reversion the return values are wrong - we need to change the
	// return type to include a bool to signify if an exception was thrown.
	reversion += "(" + retVals + ")";
	addLine(reversion);
	unindent();
	addLine("end");
	addLine("");
	return false;
}
开发者ID:M0rrisChang,项目名称:Smart-Contract,代码行数:101,代码来源:Why3Translator.cpp


注:本文中的FunctionDefinition::body方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。