Beispiel #1
0
    def fix_function_overloads(self, stmts: List[Node]) -> List[Node]:
        ret = []  # type: List[Node]
        current_overload = []
        current_overload_name = None
        # mypy doesn't actually check that the decorator is literally @overload
        for stmt in stmts:
            if isinstance(stmt, Decorator) and stmt.name() == current_overload_name:
                current_overload.append(stmt)
            else:
                if len(current_overload) == 1:
                    ret.append(current_overload[0])
                elif len(current_overload) > 1:
                    ret.append(OverloadedFuncDef(current_overload))

                if isinstance(stmt, Decorator):
                    current_overload = [stmt]
                    current_overload_name = stmt.name()
                else:
                    current_overload = []
                    current_overload_name = None
                    ret.append(stmt)

        if len(current_overload) == 1:
            ret.append(current_overload[0])
        elif len(current_overload) > 1:
            ret.append(OverloadedFuncDef(current_overload))
        return ret
Beispiel #2
0
    def visit_overloaded_func_def(self, func: OverloadedFuncDef) -> None:
        if self.sem.type is not None:
            # Don't process methods during pass 1.
            return
        kind = self.kind_by_scope()
        if kind == GDEF:
            self.sem.check_no_global(func.name(), func, True)
        func._fullname = self.sem.qualified_name(func.name())
        if kind == GDEF:
            self.sem.globals[func.name()] = SymbolTableNode(kind, func)
        if func.impl:
            impl = func.impl
            # Also analyze the function body (in case there are conditional imports).
            sem = self.sem

            if isinstance(impl, FuncDef):
                sem.function_stack.append(impl)
                sem.scope.enter_function(func)
                sem.enter()
                impl.body.accept(self)
            elif isinstance(impl, Decorator):
                sem.function_stack.append(impl.func)
                sem.scope.enter_function(func)
                sem.enter()
                impl.func.body.accept(self)
            else:
                assert False, "Implementation of an overload needs to be FuncDef or Decorator"
            sem.leave()
            sem.scope.leave()
            sem.function_stack.pop()
Beispiel #3
0
    def fix_function_overloads(self,
                               stmts: List[Statement]) -> List[Statement]:
        ret: List[Statement] = []
        current_overload: List[OverloadPart] = []
        current_overload_name: Optional[str] = None
        for stmt in stmts:
            if (current_overload_name is not None
                    and isinstance(stmt, (Decorator, FuncDef))
                    and stmt.name == current_overload_name):
                current_overload.append(stmt)
            else:
                if len(current_overload) == 1:
                    ret.append(current_overload[0])
                elif len(current_overload) > 1:
                    ret.append(OverloadedFuncDef(current_overload))

                if isinstance(stmt,
                              Decorator) and not unnamed_function(stmt.name):
                    current_overload = [stmt]
                    current_overload_name = stmt.name
                else:
                    current_overload = []
                    current_overload_name = None
                    ret.append(stmt)

        if len(current_overload) == 1:
            ret.append(current_overload[0])
        elif len(current_overload) > 1:
            ret.append(OverloadedFuncDef(current_overload))
        return ret
Beispiel #4
0
    def visit_overloaded_func_def(self, func: OverloadedFuncDef) -> None:
        if self.sem.type is not None:
            # Don't process methods during pass 1.
            return
        kind = self.kind_by_scope()
        if kind == GDEF:
            self.sem.check_no_global(func.name(), func, True)
        func._fullname = self.sem.qualified_name(func.name())
        if kind == GDEF:
            self.sem.globals[func.name()] = SymbolTableNode(kind, func)
        if func.impl:
            impl = func.impl
            # Also analyze the function body (in case there are conditional imports).
            sem = self.sem

            if isinstance(impl, FuncDef):
                sem.function_stack.append(impl)
                sem.scope.enter_function(func)
                sem.enter()
                impl.body.accept(self)
            elif isinstance(impl, Decorator):
                sem.function_stack.append(impl.func)
                sem.scope.enter_function(func)
                sem.enter()
                impl.func.body.accept(self)
            else:
                assert False, "Implementation of an overload needs to be FuncDef or Decorator"
            sem.leave()
            sem.scope.leave()
            sem.function_stack.pop()
Beispiel #5
0
 def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> None:
     if not self.recurse_into_functions:
         return
     # Revert change made during semantic analysis pass 2.
     node.items = node.unanalyzed_items.copy()
     node.is_final = False
     super().visit_overloaded_func_def(node)
Beispiel #6
0
 def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> None:
     if not self.recurse_into_functions:
         return
     # Revert change made during semantic analysis pass 2.
     node.items = node.unanalyzed_items.copy()
     node.is_final = False
     super().visit_overloaded_func_def(node)
Beispiel #7
0
    def fix_function_overloads(self,
                               stmts: List[Statement]) -> List[Statement]:
        ret = []  # type: List[Statement]
        current_overload = []  # type: List[OverloadPart]
        current_overload_name = None
        for stmt in stmts:
            if (isinstance(stmt, Decorator)
                    and stmt.name() == current_overload_name):
                current_overload.append(stmt)
            elif (isinstance(stmt, FuncDef)
                  and stmt.name() == current_overload_name
                  and stmt.name() is not None):
                ret.append(OverloadedFuncDef(current_overload + [stmt]))
                current_overload = []
                current_overload_name = None
            else:
                if len(current_overload) == 1:
                    ret.append(current_overload[0])
                elif len(current_overload) > 1:
                    ret.append(OverloadedFuncDef(current_overload))

                if isinstance(stmt, Decorator):
                    current_overload = [stmt]
                    current_overload_name = stmt.name()
                else:
                    current_overload = []
                    current_overload_name = None
                    ret.append(stmt)

        if len(current_overload) == 1:
            ret.append(current_overload[0])
        elif len(current_overload) > 1:
            ret.append(OverloadedFuncDef(current_overload))
        return ret
Beispiel #8
0
 def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> Node:
     items = [self.visit_decorator(decorator) for decorator in node.items]
     for newitem, olditem in zip(items, node.items):
         newitem.line = olditem.line
     new = OverloadedFuncDef(items)
     new._fullname = node._fullname
     new.type = self.type(node.type)
     new.info = node.info
     return new
Beispiel #9
0
 def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> Node:
     items = [self.visit_decorator(decorator) for decorator in node.items]
     for newitem, olditem in zip(items, node.items):
         newitem.line = olditem.line
     new = OverloadedFuncDef(items)
     new._fullname = node._fullname
     new.type = self.type(node.type)
     new.info = node.info
     return new
Beispiel #10
0
 def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> OverloadedFuncDef:
     items = [cast(OverloadPart, item.accept(self)) for item in node.items]
     for newitem, olditem in zip(items, node.items):
         newitem.line = olditem.line
     new = OverloadedFuncDef(items)
     new._fullname = node._fullname
     new.type = self.optional_type(node.type)
     new.info = node.info
     new.is_static = node.is_static
     new.is_class = node.is_class
     new.is_property = node.is_property
     new.is_final = node.is_final
     if node.impl:
         new.impl = cast(OverloadPart, node.impl.accept(self))
     return new
Beispiel #11
0
 def visit_overloaded_func_def(self, o: OverloadedFuncDef) -> None:
     if self.current_info is not None:
         o.info = self.current_info
     if o.type:
         o.type.accept(self.type_fixer)
     for item in o.items:
         item.accept(self)
Beispiel #12
0
 def visit_overloaded_func_def(self, o: OverloadedFuncDef) -> None:
     if self.current_info is not None:
         o.info = self.current_info
     if o.type:
         o.type.accept(self.type_fixer)
     for item in o.items:
         item.accept(self)
Beispiel #13
0
 def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> OverloadedFuncDef:
     items = [cast(OverloadPart, item.accept(self)) for item in node.items]
     for newitem, olditem in zip(items, node.items):
         newitem.line = olditem.line
     new = OverloadedFuncDef(items)
     new._fullname = node._fullname
     new.type = self.optional_type(node.type)
     new.info = node.info
     new.is_static = node.is_static
     new.is_class = node.is_class
     new.is_property = node.is_property
     new.is_final = node.is_final
     if node.impl:
         new.impl = cast(OverloadPart, node.impl.accept(self))
     return new
Beispiel #14
0
 def visit_overloaded_func_def(
         self, node: OverloadedFuncDef) -> OverloadedFuncDef:
     items = [cast(OverloadPart, item.accept(self)) for item in node.items]
     for newitem, olditem in zip(items, node.items):
         newitem.line = olditem.line
     new = OverloadedFuncDef(items)
     new._fullname = node._fullname
     new.type = self.type(node.type)
     new.info = node.info
     if node.impl:
         new.impl = cast(OverloadPart, node.impl.accept(self))
     return new
Beispiel #15
0
 def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> None:
     if node.info:
         node.info = self.fixup(node.info)
     super().visit_overloaded_func_def(node)