Exemplo n.º 1
0
 def result(element):
     lhs_type = element.env.canonicalize_name(type_string(element[0]))
     rhs_type = element.env.canonicalize_name(type_string(element[2]))
     if is_integral_primitive(lhs_type) and is_integral_primitive(rhs_type):
         element.attrib["type"] = "int"
         return
     error_if(lhs_type != rhs_type != expected_type_string,
           "binary_expression type mismatch")
     element.attrib["type"] = expected_type_string
Exemplo n.º 2
0
def relational_expression(element):
    lhs_type = element.env.canonicalize_name(element[0].attrib['type'])
    rhs_type = element.env.canonicalize_name(element[2].attrib['type'])
    if element[1].text in ["<", ">", "<=", ">="]:
        error_if(not (is_integral_primitive(lhs_type) and
                   is_integral_primitive(rhs_type)),
              "Relational expression passed non-integral types.")
    elif element[1].text == "instanceof":
        error_if(is_primitive(lhs_type) or is_primitive(rhs_type),
               "Cannot have primitives in instanceof")
    element.attrib["type"] = "boolean"
Exemplo n.º 3
0
 def is_primitive_assignable(self, lhs, rhs):
     if is_integral_primitive(lhs) and is_integral_primitive(rhs):
         if lhs == "byte" and rhs in ["char", "int", "short"]:
             return False
         if lhs == "short" and (rhs == "int" or rhs == "char"):
             return False
         if lhs == "char" and (rhs == "byte" or rhs == "int"):
             return False
         return True
     if lhs == rhs:
         return True
     return False
Exemplo n.º 4
0
    def are_identity_comparable(self, obj1, obj2):
        """Returns whether obj1 and obj2 can occur in an == expression."""
        if obj1 == "null" or obj2 in ["null", "void", "void"]:
            return True
        if is_integral_primitive(obj1) ^ is_integral_primitive(obj2):
            return False
        if is_integral_primitive(obj1) and is_integral_primitive(obj2):
            return True
        if is_primitive(obj1) and obj1 == obj2:
            return True
        if self.is_subtype(obj1, obj2) or self.is_subtype(obj2, obj1):
            return True

        return False
Exemplo n.º 5
0
def array_creation_expression(element):
    base_name = collect_token_text(element[1])
    if element.find(".//dim_expr") is not None:
        base_name += "[]"
    if element.find(".//dim_expr/expression") is not None:
        type_str = element.find(".//dim_expr/expression").attrib["type"]
        error_if(not is_integral_primitive(type_str),
              "dim_expr require integral type")
    element.attrib["type"] = element.env.canonicalize_name(base_name)
Exemplo n.º 6
0
def unary_expression(element):
    if len(element) == 2:
        # Unary minus.
        if element[0].tag == "tok_minus":
            error_if(not is_integral_primitive(element[1].attrib["type"]),
                     "must negate integral primitive")
            element.attrib["type"] = element[1].attrib["type"]
        if element[0].tag == "tok_complement":
            error_if(element[1].attrib["type"] != "boolean", "must !boolean")
            element.attrib["type"] = element[1].attrib["type"]
Exemplo n.º 7
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.")