Beispiel #1
0
    def is_assignable(self, lhs, rhs):
        if lhs == "void":
            return False
        if lhs == rhs:
            return True
        can_assign_arrays_to = ["java.lang.Object", "java.io.Serializable",
                                "java.lang.Cloneable"]
        if lhs in can_assign_arrays_to and isarray(rhs):
            return True
        if isarray(lhs) and isarray(rhs):
            lhs = lhs[:-2]
            rhs = rhs[:-2]
            if lhs == rhs:
                return True
            if is_primitive(lhs) or is_primitive(rhs):
                return False
            return self.is_assignable(lhs, rhs)
        if isarray(rhs):
            return False
        if rhs == "null" and not is_primitive(lhs):
            return True

        if is_primitive(lhs) or is_primitive(rhs):
            return self.is_primitive_assignable(lhs, rhs)
        else:
            if rhs == "null":
                return True
            return self.is_supertype(lhs, rhs)
Beispiel #2
0
def type_tag_of_subtree(subtree, if_array_then_contents=False):
    if isarray(collect_token_text(subtree)):
        if if_array_then_contents:
            subtree = subtree[0]
    if subtree.tag == "primitive_type":
        return {"short": -1, "int": -2, "char": -3, "byte": -4, "boolean": -5}[collect_token_text(subtree)]
    return subtree.env.findclass(name_to_str(subtree)).type_tag
Beispiel #3
0
 def get_type_for_declaration_site(self, element):
     decl_type_string = type_string(element)
     if (is_primitive(decl_type_string) or
         isarray(decl_type_string) or
         decl_type_string == "void"):
         return decl_type_string
     return self.findclass(decl_type_string).get("canonical_name")
Beispiel #4
0
    def is_subtype(self, obj1, obj2):
        """Returns whether obj1 is a subclass of obj2."""

        assert not is_primitive(obj1)
        assert not is_primitive(obj2)

        if isarray(obj1) and isarray(obj2):
            obj1 = obj1[:-2]
            obj2 = obj2[:-2]
        elif isarray(obj1):
            if obj2 == "java.lang.Object":
                return True
            else:
                return False

        obj1 = self.findclass(obj1)
        obj2 = self.findclass(obj2)
        return self.is_classes_subtype(obj1, obj2)
Beispiel #5
0
def array_access(element):
    if element[0].tag == "name":
        name = name_to_str(element[0])
        decl_site = element.env.get_declaration_site_for_variable_name(name,
                                                                       element[0])
        type_name = element.env.get_type_for_declaration_site(decl_site)
        element.declaration = decl_site
    else:
        type_name = element[0].attrib["type"]
    error_if(not isarray(type_name), "Array access on non-array type.")
    element.attrib["type"] = type_name[:-2]

    index_expression_type = element[-2].attrib["type"]
    error_if(not is_integral_primitive(index_expression_type),
          "Cannot index into array with non-integral expression.")
Beispiel #6
0
def _mlp(nonlinearities, params, inputs):
    ravel, unravel = _make_ravelers(inputs.shape)
    eval_mlp = compose(layer(nonlin, W, b)
                       for nonlin, (W, b) in zip(nonlinearities, params))
    out = eval_mlp(ravel(inputs))
    return unravel(out) if isarray(out) else map(unravel, out)
Beispiel #7
0
def _mlp(nonlinearities, params, inputs):
    ravel, unravel = _make_ravelers(inputs.shape)
    eval_mlp = compose(
        layer(nonlin, W, b) for nonlin, (W, b) in zip(nonlinearities, params))
    out = eval_mlp(ravel(inputs))
    return unravel(out) if isarray(out) else map(unravel, out)
Beispiel #8
0
        def get_declaration_site_for_name_internal(env,
                                                   name,
                                                   type,
                                                   arglist,
                                                   canBeClass=True):
            qualified_parts = name.split(".")

            if len(qualified_parts) == 1:

                path = []
                if type == "Variable":
                    path += [env.find_nonlocal(name)]
                if type == "Method":
                    path += [env.find_method_by_name(name, arglist)]

                path = [x for x in path if x][:1]
                error_if(not path,
                      "Could not find %s %s" % (type, name))
            else:
                path = []
                rhs = None  # Initialize
                for x in range(1, len(qualified_parts) + 1):

                    lhs = ".".join(qualified_parts[:x])
                    rhs = ".".join(qualified_parts[x:])

                    if env.find_nonlocal(lhs) is not None:
                        path = [env.find_nonlocal(lhs)]
                        break

                    elif env.find_method_by_name(lhs, arglist) is not None and rhs == "":
                        path = [env.find_method_by_name(lhs, arglist)]
                        break

                    # resolving LHS is a Class
                    elif canBeClass and env.findclass(lhs) is not None:
                        path = [env.findclass(lhs)]
                        break

                error_if(len(path) == 0,
                      "Cannot find declaration for %s %s" % (type, name))
                if isvariable(path[-1]) or (path[-1].tag == "method" and rhs):
                    if isarray(type_string(path[-1])):
                        new_env = env.findclass("java.lang.$Array").env
                    else:
                        if isarray(type_string(path[-1])):
                            new_env = env.findclass("java.lang.$Array").env
                        else:
                            error_if(is_primitive(type_string(path[-1])),
                                  "Method called on primitive.")
                            new_env = path[-1].env.findclass(
                                type_string(path[-1])).env
                else:
                    new_env = path[-1].env
                path += get_declaration_site_for_name_internal(new_env,
                                                               rhs,
                                                               type,
                                                               arglist,
                                                               False)

            return path
Beispiel #9
0
 def get_declaration_site_for_class_name(self, name):
     if isarray(name):
         return self.findclass("java.lang.$Array")
     error_if(self.findclass(name) is None,
              "No class %s found" % name)
     return self.findclass(name)