Example #1
0
    def test_stringify_type(self):
        fake_span = self.fake_span
        m = self.m
        number_5 = ast.Number(m, fake_span, '5')
        number_2 = ast.Number(m, fake_span, '2')
        number_3 = ast.Number(m, fake_span, '3')
        bits_token = Token(value=Keyword.BITS, span=fake_span)

        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, bits_token, (number_5, ))
        self.assertEqual('bits[5]', str(type_))

        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, Token(value=Keyword.U32, span=fake_span),
            (number_5, ))
        self.assertEqual('u32[5]', str(type_))

        # "no-volume" bits array.
        # TODO(leary): 2020-08-24 delete bits in favor of uN
        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, bits_token, ())
        self.assertEqual('bits', str(type_))

        # TypeRef with dims.
        my_type_tok = Token(TokenKind.IDENTIFIER,
                            value='MyType',
                            span=fake_span)
        name_def = ast.NameDef(m, fake_span, 'MyType')
        type_def = ast.TypeDef(m, fake_span, name_def, type_, public=False)
        type_ref = ast.TypeRef(m, fake_span, my_type_tok.value, type_def)
        type_ = ast_helpers.make_type_ref_type_annotation(
            m, fake_span, type_ref, (number_2, number_3))
        self.assertEqual('MyType[2][3]', str(type_))
Example #2
0
    def test_type_annotation_properties(self):
        m = self.m
        fake_span = self.fake_span
        number_5 = ast.Number(m, fake_span, '5')
        number_2 = ast.Number(m, fake_span, '2')
        number_3 = ast.Number(m, fake_span, '3')
        bits_token = Token(value=Keyword.BITS, span=fake_span)
        un_token = Token(value=Keyword.UN, span=fake_span)
        u32_token = Token(value=Keyword.U32, span=fake_span)

        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, bits_token, (number_5, ))
        self.assertEqual('bits[5]', str(type_))

        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, bits_token, (number_5, number_2))
        self.assertIsInstance(type_, ast.ArrayTypeAnnotation)
        self.assertEqual('bits[5][2]', str(type_))

        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, u32_token, ())
        self.assertEqual('u32', str(type_))

        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, u32_token, (number_3, ))
        self.assertIsInstance(type_, ast.ArrayTypeAnnotation)
        self.assertEqual('u32[3]', str(type_))

        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, un_token, (number_2, ))
        self.assertEqual('uN[2]', str(type_))

        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, un_token, (number_2, number_3))
        self.assertIsInstance(type_, ast.ArrayTypeAnnotation)
        self.assertEqual('uN[2][3]', str(type_))

        # TODO(leary): 2020-08-24 delete bits in favor of uN
        # "no-volume" bits array.
        type_ = ast_helpers.make_builtin_type_annotation(
            m, fake_span, bits_token, ())
        self.assertEqual('bits', str(type_))

        # TypeRef with dims.
        name_def = ast.NameDef(m, fake_span, 'MyType')
        type_def = ast.TypeDef(m, fake_span, name_def, type_, public=False)
        type_ref = ast.TypeRef(m, fake_span, 'MyType', type_def)
        type_ = ast_helpers.make_type_ref_type_annotation(
            m, fake_span, type_ref, (number_2, number_3))
        self.assertIsInstance(type_, ast.ArrayTypeAnnotation)
        self.assertEqual('MyType[2][3]', str(type_))

        type_ = ast.TupleTypeAnnotation(
            m, fake_span,
            (ast_helpers.make_builtin_type_annotation(m, fake_span, bits_token,
                                                      (number_5, )),
             ast_helpers.make_builtin_type_annotation(m, fake_span, bits_token,
                                                      (number_2, ))))
        self.assertIsInstance(type_, ast.TupleTypeAnnotation)
        self.assertEqual('(bits[5], bits[2])', str(type_))
Example #3
0
    def _create_type_ref(self, type_: ast.TypeAnnotation) -> ast.TypeRef:
        """Creates and returns a type ref for the given annotation.

    As part of this process, an ast.TypeDef is created and added to the
    set of currently active set.

    Args:
      type_: The type for which to create refs.

    Returns:
      The generated TypeRef.
    """
        type_name = self.gensym()
        name_def = self._make_name_def(type_name)
        type_def = ast.TypeDef(self.m, self.fake_span, name_def, type_, False)
        type_ref = ast.TypeRef(self.m, self.fake_span, type_name, type_def)
        self._type_defs.append(type_def)
        self._type_bit_counts[str(type_ref)] = self._get_type_bit_count(type_)
        return type_ref