Пример #1
0
        public static string GetCanonicalFunctionName(FunctionDefinition funcDef, TranslatorContext context)
        {
            ContractDefinition contract = context.GetContractByFunction(funcDef);
            string             paramStr = "";

            if (funcDef.Parameters != null)
            {
                foreach (VariableDeclaration paramDecl in funcDef.Parameters.Parameters)
                {
                    String typeStr = "";
                    if (paramDecl.TypeName is Mapping map)
                    {
                        typeStr = "map" + map.KeyType.ToString();
                    }
                    else if (paramDecl.TypeName is UserDefinedTypeName userDef)
                    {
                        typeStr = userDef.Name;
                    }
                    else if (paramDecl.TypeName is ArrayTypeName arr)
                    {
                        typeStr = "arr";
                    }
                    else
                    {
                        typeStr = paramDecl.TypeName.ToString().Replace(" ", "");
                    }

                    paramStr = $"{paramStr}~{typeStr}";
                }
            }

            return($"{funcDef.Name}{paramStr}_{contract.Name}");
            //return funcDef.Name + "_" + contract.Name;
        }
 private void PrintFunctionResolutionMap()
 {
     foreach (string signature in context.FuncSigResolutionMap.Keys)
     {
         Console.WriteLine("-- " + signature);
         foreach (ContractDefinition dynamicType in context.FuncSigResolutionMap[signature].Keys)
         {
             FunctionDefinition function = context.GetFunctionByDynamicType(signature, dynamicType);
             ContractDefinition contract = context.GetContractByFunction(function);
             Console.WriteLine(dynamicType.Name + " --> " + contract.Name + "." + function.Name);
         }
         Console.WriteLine();
     }
 }
Пример #3
0
        public static string GetCanonicalFunctionName(FunctionDefinition funcDef, TranslatorContext context)
        {
            ContractDefinition contract = context.GetContractByFunction(funcDef);

            return(funcDef.Name + "_" + contract.Name);
        }
Пример #4
0
        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);
        }