Exemplo n.º 1
0
        /**
         * Iterates through contract set, filters functions and events from base contracts and adds them to AST_Handler instance
         */
        public void filterFunctionsAndEvents()
        {
            List <ContractDefinition> contractSet = new List <ContractDefinition>(classTranslatorContext.ContractDefinitionsMap);

            foreach (ContractDefinition indexContract in contractSet)
            {
                // create a localised list of integer values containi
                List <int> baseContractSet = new List <int>(indexContract.LinearBaseContracts);

                //for each base contract, return its respective function and event definitiosn and add them to the Handler context.
                foreach (int i in baseContractSet)
                {
                    //return the base contract at index i
                    ContractDefinition baseContract = classTranslatorContext.retrieveASTNodethroughID(i) as ContractDefinition;
                    if (baseContract != null)
                    {
                        //loop through function defs hashset and for each functionDefinition, compute
                        //the function signature and add the function to the AST_Handler instance.
                        foreach (FunctionDefinition function in classTranslatorContext.returnFunctionDefs(baseContract))
                        {
                            string functionSignature = Conversion_Utility_Tool.ComputeFunctionSignature(function);

                            if (classTranslatorContext != null)
                            {
                                classTranslatorContext.AddFunctionToDynamicType(
                                    Conversion_Utility_Tool.ComputeFunctionSignature(function), //Computed signature of function
                                    indexContract,                                              //contract for which function will be added
                                    function);                                                  //Function be added
                            }
                        }
                        foreach (EventDefinition singleEvent in classTranslatorContext.retrieveEventDefinitionsUsingContract(baseContract))
                        {
                            //if the translator context is not null then add the single event to the contract via addEventToContract
                            if (classTranslatorContext != null)
                            {
                                classTranslatorContext.InsertEventInContract(indexContract, singleEvent);
                            }
                        }

                        //Compute Visible functions in existing function declaration
                        foreach (string funcSig in classTranslatorContext.SignatureFunctionMap.Keys)
                        {
                            foreach (ContractDefinition tempContract in classTranslatorContext.SignatureFunctionMap[funcSig].Keys)
                            {
                                FunctionDefinition funcDef = classTranslatorContext.SignatureFunctionMap[funcSig][tempContract];
                                classTranslatorContext.AddVisibleFunctionToContract(funcDef, tempContract);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        /**
         * Function to filter through the current list of contract definitions stored in the shared translation context.
         * From that point, the for loop will attempt to retrieve the IDs of the base contracts.
         */
        public void checkContractForInheritance()
        {
            foreach (ContractDefinition typeDefinition in classTranslationContext.ContractDefinitionsMap)
            {
                foreach (int baseContractID in typeDefinition.LinearBaseContracts)
                {
                    ContractDefinition baseCurrentContract = classTranslationContext.retrieveASTNodethroughID(baseContractID) as ContractDefinition;

                    //add type to base current contract.
                    if (baseCurrentContract != null)
                    {
                        classTranslationContext.InsertTypeInContract(baseCurrentContract, typeDefinition);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static string InferFunctionSignature(AST_Handler context, FunctionCall node)
        {
            Debug.Assert(node.Arguments != null);

            if (node.Expression is MemberAccess memberAccess)
            {
                Debug.Assert(memberAccess.ReferencedDeclaration != null);
                FunctionDefinition function = context.retrieveASTNodethroughID(memberAccess.ReferencedDeclaration.Value) as FunctionDefinition;
                Debug.Assert(function != null, $"Could not find function {node.ToString()}");
                StringBuilder builder = new StringBuilder();
                builder.Append(function.Name).Append("(");
                if (function.Parameters.Parameters != null && function.Parameters.Parameters.Count > 0)
                {
                    foreach (VariableDeclaration varDecl in function.Parameters.Parameters)
                    {
                        builder.Append(varDecl.TypeDescriptions.TypeString).Append(", ");
                    }
                    builder.Length -= 2;
                }
                builder.Append(")");
                return(builder.ToString());
            }
            else
            {
                string        functionName = GetFuncNameFromFuncCall(node);
                StringBuilder builder      = new StringBuilder();
                builder.Append(functionName).Append("(");
                if (node.Arguments.Count > 0)
                {
                    foreach (Expression argument in node.Arguments)
                    {
                        string typeString = argument.TypeDescriptions.TypeString;
                        if (typeString.StartsWith("int_const"))
                        {
                            typeString = "int256";
                        }
                        if (typeString.StartsWith("uint_const"))
                        {
                            typeString = "int256";
                        }
                        if (typeString.StartsWith("string") || typeString.StartsWith("literal_string"))
                        {
                            typeString = "string";
                        }
                        if (typeString.StartsWith("bytes "))
                        {
                            typeString = "bytes";           //"bytes storage ref"
                        }
                        if (typeString.Contains(" memory")) //"struct Foo memory"
                        {
                            typeString = typeString.Substring(0, typeString.IndexOf(" memory"));
                        }
                        if (typeString.Contains(" storage"))
                        {
                            typeString = typeString.Substring(0, typeString.IndexOf(" storage"));
                        }
                        if (typeString.Contains(" payable"))
                        {
                            typeString = typeString.Substring(0, typeString.IndexOf(" payable")); //address payable
                        }
                        builder.Append(typeString).Append(", ");
                    }
                    builder.Length -= 2;
                }
                builder.Append(")");
                return(builder.ToString());
            }
        }