Skip to content

atdcpp: faulty type names in generated code when using parametric types in atd file #427

@lambdaxdotx

Description

@lambdaxdotx

First of all, thanks to everyone who contributes to atdcpp and atd in general.

I have stumbled upon a weird bug in the code generated by atdcpp when using parametric types in an atd file in input.

Consider the following atd file:

<cpp namespace="mynamespace">

type ('t) some_type_t3 = {
  t2: 't some_type_t2;
}

type ('t) some_type_t1 = [
  | A of bool
  | B of 't some_type_t3
]

type ('t) some_type_t2 = {
  t1: 't some_type_t1;
}

type type_t1 = type_t1 some_type_t1 wrap <cpp templatize t="std::shared_ptr" wrap="_atd_wrap_shared_ptr" unwrap="_atd_unwrap_shared_ptr">
type type_t2 = type_t1 some_type_t2 wrap <cpp templatize t="std::shared_ptr" wrap="_atd_wrap_shared_ptr" unwrap="_atd_unwrap_shared_ptr">

The generated hpp file is correct and looks like this (I report here only the minimal/sufficient part to show the problem):

namespace atd::mynamespace {
// forward declarations
namespace TypeT1SomeTypeT1::Types {
    struct A;
    struct B;
}
struct TypeT1SomeTypeT2;
struct TypeT1SomeTypeT3;


namespace typedefs {
    typedef TypeT1SomeTypeT2 TypeT1SomeTypeT2;
    typedef TypeT1SomeTypeT3 TypeT1SomeTypeT3;

    typedef std::variant<TypeT1SomeTypeT1::Types::A, TypeT1SomeTypeT1::Types::B> TypeT1SomeTypeT1;

    typedef std::shared_ptr<typedefs::TypeT1SomeTypeT2> TypeT2;
    typedef std::shared_ptr<typedefs::TypeT1SomeTypeT1> TypeT1;
} // namespace typedefs
[...]
}

The problem I am referring to can be triggered by changing the name of (only) type_t1 into type_t_1 as follows:

[... same as before ...]
type type_t_1 = type_t_1 some_type_t1 wrap <cpp templatize t="std::shared_ptr" wrap="_atd_wrap_shared_ptr" unwrap="_atd_unwrap_shared_ptr">
type type_t2 = type_t_1 some_type_t2 wrap <cpp templatize t="std::shared_ptr" wrap="_atd_wrap_shared_ptr" unwrap="_atd_unwrap_shared_ptr">

The corresponding hpp file is now full of incorrect type names:

namespace atd::mynamespace {
// forward declarations
namespace T1Fb202dd::Types {
    struct A;
    struct B;
}
struct T22af8775;
struct T342d5ef9;


namespace typedefs {
    typedef T22af8775 T22af8775;
    typedef T342d5ef9 T342d5ef9;

    typedef std::variant<T1Fb202dd::Types::A, T1Fb202dd::Types::B> T1Fb202dd;

    typedef std::shared_ptr<typedefs::T1Fb202dd> TypeT1;
    typedef std::shared_ptr<typedefs::T22af8775> TypeT2;
} // namespace typedefs
[...]
}

This is a problem not related to using numbers in names. Indeed, the same happens when renaming type_t1 into type_t_t.

It seems to me that the bug is related to the handling of _ (underscore) character, especially when dealing with parametric types in the atd file. Indeed, when considering the classic version of the example without type parameters, nothing wrong happens even though the type_t_1 is still used:

<cpp namespace="mynamespace">

type type_t3 = {
  t2: type_t2;
}

type type_t_1 = [
  | A of bool
  | B of type_t3
]

type type_t2 = {
  t1: type_t_1;
}

The corresponding generated code is indeed fine:

namespace atd::mynamespace {
// forward declarations
struct TypeT2;
struct TypeT3;
namespace TypeT1::Types {
    struct A;
    struct B;
}


namespace typedefs {
    typedef TypeT2 TypeT2;
    typedef TypeT3 TypeT3;

    typedef std::variant<TypeT1::Types::A, TypeT1::Types::B> TypeT1;
} // namespace typedefs
[...]
}

(I let myself ping @elrandar on this issue as the maintainer of atdcpp as far as I understood.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions