public override bool Visit(ContractDefinition node) { currentContract = node; context.UsingMap[currentContract] = new Dictionary <UserDefinedTypeName, List <TypeName> >(); return(true); }
public override bool Visit(ContractDefinition node) { currentContract = node; nodesToAdd = new List <ASTNode>(); return(true); }
public void AddContract(ContractDefinition contract) { ContractDefinitions.Add(contract); }
public HashSet <VariableDeclaration> GetVisibleStateVarsByContract(ContractDefinition contract) { return(ContractToVisibleStateVarsMap.ContainsKey(contract) ? ContractToVisibleStateVarsMap[contract] : new HashSet <VariableDeclaration>()); }
// generate a non-deterministic choice block to call every public visible functions except constructors private BoogieIfCmd GenerateChoiceBlock(ContractDefinition contract) { HashSet <FunctionDefinition> funcDefs = context.GetVisibleFunctionsByContract(contract); List <FunctionDefinition> publicFuncDefs = new List <FunctionDefinition>(); foreach (FunctionDefinition funcDef in funcDefs) { if (funcDef.IsConstructorForContract(contract.Name)) { continue; } if (funcDef.Visibility == EnumVisibility.PUBLIC || funcDef.Visibility == EnumVisibility.EXTERNAL) { publicFuncDefs.Add(funcDef); } } BoogieIfCmd ifCmd = null; for (int i = publicFuncDefs.Count - 1; i >= 0; --i) { BoogieExpr guard = new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.EQ, new BoogieIdentifierExpr("choice"), new BoogieLiteralExpr(i + 1)); BoogieStmtList thenBody = new BoogieStmtList(); FunctionDefinition funcDef = publicFuncDefs[i]; string callee = TransUtils.GetCanonicalFunctionName(funcDef, context); List <BoogieExpr> inputs = new List <BoogieExpr>() { new BoogieIdentifierExpr("this"), new BoogieIdentifierExpr("msgsender_MSG"), new BoogieIdentifierExpr("msgvalue_MSG"), }; foreach (VariableDeclaration param in funcDef.Parameters.Parameters) { string name = TransUtils.GetCanonicalLocalVariableName(param); inputs.Add(new BoogieIdentifierExpr(name)); if (param.TypeName is ArrayTypeName array) { thenBody.AddStatement(new BoogieCallCmd( "FreshRefGenerator", new List <BoogieExpr>(), new List <BoogieIdentifierExpr>() { new BoogieIdentifierExpr(name) })); } } List <BoogieIdentifierExpr> outputs = new List <BoogieIdentifierExpr>(); var retParamCount = 0; foreach (VariableDeclaration param in funcDef.ReturnParameters.Parameters) { //string name = "__ret" + funcDef.Name; string name = $"__ret_{retParamCount++}_" + funcDef.Name; if (!string.IsNullOrEmpty(param.Name)) { name = TransUtils.GetCanonicalLocalVariableName(param); } outputs.Add(new BoogieIdentifierExpr(name)); } BoogieCallCmd callCmd = new BoogieCallCmd(callee, inputs, outputs); thenBody.AddStatement(callCmd); BoogieStmtList elseBody = ifCmd == null ? null : BoogieStmtList.MakeSingletonStmtList(ifCmd); ifCmd = new BoogieIfCmd(guard, thenBody, elseBody); } return(ifCmd); }
public bool HasFuncSigInContract(ContractDefinition contract, string signature) { return(ContractToFuncSigsMap.ContainsKey(contract)); }
public HashSet <FunctionDefinition> GetVisibleFunctionsByContract(ContractDefinition contract) { return(ContractToVisibleFunctionsMap.ContainsKey(contract) ? ContractToVisibleFunctionsMap[contract] : new HashSet <FunctionDefinition>()); }
public static string GetCanonicalFunctionName(FunctionDefinition funcDef, TranslatorContext context) { ContractDefinition contract = context.GetContractByFunction(funcDef); return(funcDef.Name + "_" + contract.Name); }
public static string GetCanonicalConstructorName(ContractDefinition contract) { return(contract.Name + "_" + contract.Name); }
public virtual bool Visit(ContractDefinition node) { return(CommonVisit(node)); }
public void AddFallbackToContract(ContractDefinition contract, FunctionDefinition fallback) { Debug.Assert(fallback.IsFallback, $"{fallback.Name} is not a fallback function"); Debug.Assert(!ContractToFallbackMap.ContainsKey(contract), $"Multiple fallbacks are defined for {contract.Name}"); ContractToFallbackMap[contract] = fallback; }
public virtual void EndVisit(ContractDefinition node) { CommonEndVisit(node); }
static void Main(string[] args) { //StreamWriter y = null; try { var x = ContractDefinition.LoadFromFile(@"XMLFile1.xml"); string subfolder = "Contract\\Interfaces\\"; if (!Directory.Exists(subfolder)) { Directory.CreateDirectory(subfolder); } foreach (var item in x.ContractServiceInterfaces) { File.WriteAllText(subfolder + item.Name + ".cs", item.GenerateCode()); } foreach (var item in x.ContractServiceInterfaces) { File.WriteAllText(subfolder + item.Name + "Agent.cs", ((ContractAgentInterfaceDefinition)item).GenerateCode()); } subfolder = "Contract\\Agent\\"; if (!Directory.Exists(subfolder)) { Directory.CreateDirectory(subfolder); } foreach (var item in x.ContractServiceInterfaces) { File.WriteAllText(subfolder + item.Name.Substring(1) + "Agent.cs", ((ContractAgentClassDefinition)item).GenerateCode()); } File.WriteAllText(subfolder + x.Name + "ProviderFactory.cs", ContractAgentFactoryClassDefinition.GetAgentFactory(x.ContractServiceInterfaces, x.Name).GenerateCode()); subfolder = "Service\\"; if (!Directory.Exists(subfolder)) { Directory.CreateDirectory(subfolder); } foreach (var item in x.ContractServiceInterfaces) { File.WriteAllText(subfolder + item.Name.Substring(1) + ".cs", ((ContractServiceClassDefinition)item).GenerateCode()); } foreach (var item in x.ContractServiceInterfaces) { File.WriteAllText(subfolder + item.Name.Substring(1) + ".svc", ((ContractServiceHostDefinition)item).GenerateCode()); } subfolder = "Contract\\Domain\\"; if (!Directory.Exists(subfolder)) { Directory.CreateDirectory(subfolder); } foreach (var item in x.DomainObjectClasses) { File.WriteAllText(subfolder + item.Name + ".cs", ((DomainObjectClassDefinition)item).GenerateCode()); } } finally { //y.Close(); } }
public override void EndVisit(ContractDefinition node) { currentContract = null; }
public void AddConstructorToContract(ContractDefinition contract, FunctionDefinition ctor) { Debug.Assert(ctor.IsConstructor, $"{ctor.Name} is not a constructor"); Debug.Assert(!ContractToConstructorMap.ContainsKey(contract), $"Multiple constructors are defined"); ContractToConstructorMap[contract] = ctor; }
public static ContractDefinition GetUsedLibrary(TranslatorContext context, ContractDefinition curContract, MemberAccess memberAccess) { FunctionDefinition fnDef = context.GetASTNodeById(memberAccess.ReferencedDeclaration.Value) as FunctionDefinition; if (fnDef == null || !context.FunctionToContractMap.ContainsKey(fnDef)) { return(null); } ContractDefinition fnContract = context.GetContractByFunction(fnDef); Dictionary <ContractDefinition, UserDefinedTypeName> usingLibs = new Dictionary <ContractDefinition, UserDefinedTypeName>(); List <int> contractIds = new List <int>(); contractIds.Add(curContract.Id); contractIds.AddRange(curContract.LinearizedBaseContracts); foreach (int id in contractIds) { ContractDefinition baseContract = context.GetASTNodeById(id) as ContractDefinition; foreach (UserDefinedTypeName typeName in context.UsingMap[baseContract].Keys) { ContractDefinition libDef = context.GetASTNodeById(typeName.ReferencedDeclaration) as ContractDefinition; if (!usingLibs.ContainsKey(libDef)) { usingLibs[libDef] = typeName; } } } if (usingLibs.ContainsKey(fnContract)) { if (memberAccess.Expression.TypeDescriptions.IsContract() && !memberAccess.Expression.TypeDescriptions.IsArray()) { //search sub-types UserDefinedTypeName libType = usingLibs[fnContract]; String contractName = memberAccess.Expression.TypeDescriptions.TypeString.Split(" ")[1]; ContractDefinition contractDef = context.GetContractByName(contractName); HashSet <ContractDefinition> usedBy = context.UsingMap[curContract][libType].FindAll(t => t is UserDefinedTypeName u && context.GetASTNodeById(u.ReferencedDeclaration) is ContractDefinition).Select(c => context.GetASTNodeById(((UserDefinedTypeName)(c)) .ReferencedDeclaration) as ContractDefinition).ToHashSet(); bool usesLib = usedBy.Contains(contractDef); foreach (int id in contractDef.LinearizedBaseContracts) { ContractDefinition baseContract = context.GetASTNodeById(id) as ContractDefinition; if (usedBy.Contains(baseContract)) { usesLib = true; } } return(usesLib ? fnContract : null); } else { return(fnContract); } } return(null); }
public bool IsConstructorDefined(ContractDefinition contract) { return(ContractToConstructorMap.ContainsKey(contract)); }
public override bool Visit(ContractDefinition node) { context.AddContract(node); return(false); }
public FunctionDefinition GetFunctionByDynamicType(string funcSig, ContractDefinition dynamicType) { return(FuncSigResolutionMap[funcSig][dynamicType]); }
public ERC20SpecGenerator(TranslatorContext context, AST solidityAST, String entryPoint) { this.context = context; this.solidityAST = solidityAST; varDecls = new Dictionary <string, VariableDeclaration>(); fnContracts = new Dictionary <string, ContractDefinition>(); otherVars = new List <VariableDeclaration>(); declToContractInd = new Dictionary <VariableDeclaration, int>(); foreach (ContractDefinition def in context.ContractDefinitions) { if (def.Name.Equals(entryPoint)) { entryContract = def; } } int contractInd = 0; foreach (int id in entryContract.LinearizedBaseContracts) { contractInd++; ContractDefinition contract = context.GetASTNodeById(id) as ContractDefinition; if (context.ContractToStateVarsMap.ContainsKey(contract)) { otherVars.AddRange(context.ContractToStateVarsMap[contract]); foreach (VariableDeclaration decl in context.ContractToStateVarsMap[contract]) { declToContractInd[decl] = contractInd; } } if (!context.ContractToFunctionsMap.ContainsKey(contract)) { continue; } HashSet <FunctionDefinition> fnDefs = context.ContractToFunctionsMap[contract]; foreach (FunctionDefinition fnDef in fnDefs) { if (ERC20fns.Contains(fnDef.Name) && !fnContracts.ContainsKey(fnDef.Name)) { fnContracts[fnDef.Name] = contract; } if (ERC20Vars.Contains(fnDef.Name) && !varDecls.ContainsKey(fnDef.Name)) { ReturnDeclarationFinder declFinder = new ReturnDeclarationFinder(context); VariableDeclaration decl = declFinder.findDecl(contract, fnDef); if (decl != null) { varDecls[fnDef.Name] = decl; } } } } foreach (VariableDeclaration decl in varDecls.Values) { otherVars.Remove(decl); } otherVars.RemoveAll(v => v.Constant); }
public VariableDeclaration GetStateVarByDynamicType(string varName, ContractDefinition dynamicType) { return(StateVarNameResolutionMap[varName][dynamicType]); }
public override bool Visit(ContractDefinition node) { return(false); }
public bool IsMethodInIgnoredSet(FunctionDefinition func, ContractDefinition contract) { return(IgnoreMethods.Any(x => x.Item2.Equals(contract.Name) && (x.Item1.Equals("*") || x.Item1.Equals(func.Name)))); }
public abstract string GetFileName(ContractDefinition definition);
private string GetHoudiniVarName(int id, ContractDefinition contract) { return("HoudiniB" + id.ToString() + "_" + contract.Name); }
protected internal abstract void DoPrepare(DocumentGenerator generator, ContractDefinition definition);
public override void EndVisit(ContractDefinition node) { currentContract.Nodes.AddRange(nodesToAdd); currentContract = null; nodesToAdd = null; }
private ContractDefinition CoerceContractDefinition(ContractDefinition definition) { return(new ContractDefinition(definition.Root, definition.ExcludedContracts.Concat(GetExcludedTypes()).Distinct().ToArray())); }
public HashSet <ContractDefinition> GetSubTypesOfContract(ContractDefinition contract) { Debug.Assert(ContractToSubTypesMap.ContainsKey(contract), $"Cannot find {contract.Name} in the sub type map"); return(ContractToSubTypesMap[contract]); }
//returns state variable using lookup map with an index of dynamic type public VariableDeclaration retrieveStateVarDynamicType(string variableName, ContractDefinition dynamicType) { return(stateVarLookUpTable[variableName][dynamicType]); }