Пример #1
0
        private List <BoogieVariable> CollectLocalVars(ContractDefinition contract)
        {
            List <ContractDefinition> contracts = new List <ContractDefinition>()
            {
                contract
            };

            if (context.TranslateFlags.TxnsOnFields)
            {
                HashSet <VariableDeclaration> contractFields = context.GetStateVarsByContract(contract);
                foreach (VariableDeclaration contractField in contractFields)
                {
                    if (contractField.TypeDescriptions.IsContract() && contractField.TypeName is UserDefinedTypeName)
                    {
                        String             fieldContractName = contractField.TypeName.ToString();
                        ContractDefinition fieldDef          = context.GetContractByName(fieldContractName);
                        contracts.Add(fieldDef);
                    }
                }
            }

            return(TransUtils.CollectLocalVars(contracts, context));
        }
Пример #2
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);
        }