Beispiel #1
0
 def _validate_return(self, node: ast.AST):
     if (self._current_method.return_type is not Type.none
             and hasattr(node, 'body')
             and not self._has_return(node)):
         lst = list(self.symbols.items())
         keys, values = [key_value[0] for key_value in lst], [key_value[-1] for key_value in lst]
         self._log_error(
             CompilerError.MissingReturnStatement(
                 line=node.lineno, col=node.col_offset,
                 symbol_id=keys[values.index(self._current_method)]
             )
         )
Beispiel #2
0
    def _read_metadata_object(self, function: ast.FunctionDef):
        """
        Gets the metadata object defined in this function

        :param function:
        """
        if self._metadata is not None:
            # metadata function has been defined already
            self._log_warning(
                CompilerWarning.RedeclaredSymbol(
                    line=function.lineno,
                    col=function.col_offset,
                    symbol_id=Builtin.Metadata.identifier))
        # this function must have a return and no arguments
        elif len(function.args.args) != 0:
            self._log_error(
                CompilerError.UnexpectedArgument(line=function.lineno,
                                                 col=function.col_offset))
        elif not any(isinstance(stmt, ast.Return) for stmt in function.body):
            self._log_error(
                CompilerError.MissingReturnStatement(line=function.lineno,
                                                     col=function.col_offset,
                                                     symbol_id=function.name))
        else:
            function.returns = None
            function.decorator_list = []
            module: ast.Module = ast.parse('')
            module.body = [
                node for node in self._tree.body
                if isinstance(node, (ast.ImportFrom, ast.Import))
            ]
            module.body.append(function)
            ast.copy_location(module, function)

            # executes the function
            code = compile(module, filename='<boa3>', mode='exec')
            namespace = {}
            exec(code, namespace)
            obj: Any = namespace[function.name]()

            node: ast.AST = function.body[-1] if len(
                function.body) > 0 else function
            # return must be a NeoMetadata object
            if not isinstance(obj, NeoMetadata):
                obj_type = self.get_type(obj).identifier if self.get_type(
                    obj) is not Type.any else type(obj).__name__
                self._log_error(
                    CompilerError.MismatchedTypes(
                        line=node.lineno,
                        col=node.col_offset,
                        expected_type_id=NeoMetadata.__name__,
                        actual_type_id=obj_type))
                return

            # validates the metadata attributes types
            attributes: Dict[str, Any] = {
                attr: value
                for attr, value in dict(obj.__dict__).items()
                if attr in Builtin.metadata_fields
            }
            if any(not isinstance(value, Builtin.metadata_fields[attr])
                   for attr, value in attributes.items()):
                for expected, actual in [
                    (Builtin.metadata_fields[attr], type(v_type))
                        for attr, v_type in attributes.items() if
                        not isinstance(v_type, Builtin.metadata_fields[attr])
                ]:
                    if isinstance(expected, Iterable):
                        expected_id = 'Union[{0}]'.format(', '.join(
                            [tpe.__name__ for tpe in expected]))
                    elif hasattr(expected, '__name__'):
                        expected_id = expected.__name__
                    else:
                        expected_id = str(expected)

                    self._log_error(
                        CompilerError.MismatchedTypes(
                            line=node.lineno,
                            col=node.col_offset,
                            expected_type_id=expected_id,
                            actual_type_id=actual.__name__))
            else:
                # if the function was defined correctly, sets the metadata object of the smart contract
                self._metadata = obj
                self._metadata_node = function  # for error messages only