2

For this template, is it possible to parameterize the uint32 type?

template test_t {
    saved uint32 data;
}

I tried using param mytype = uint32, but did not work. Is it possible another way?

  • What do you want to achieve with a type parameter to a template? What kind of coding are you thinking about? – jakobengblom2 Jan 10 '23 at 08:30
  • We have multiple AVST interfaces with different widths (8/16/32/64/etc) where each width has a unique data type and I'm seeing if there's any simplification that can be done to the templates that wrap these interfaces. – Anthony Moore Jan 11 '23 at 16:37
  • How is this called from the outside? In general, you could store data for any width as 64 bits internally, and just use different sizes in the interface to the outside. But I guess for data manipulation internally in the model you want to have a precise size? – jakobengblom2 Jan 12 '23 at 17:37

1 Answers1

3

DML doesn't have built-in support for generics, but you can emulate them by using param together with typeof. This, however, does have some notable shortcomings and is typically something you only reach for in desperation.

An example of how you'd emulate generics -- and to demonstrate the problems with doing so -- is the following:

template test_t {
    param data_type_carrier;
    #if (true) {
        saved typeof(data_type_carrier) data;
    }
}

group test is test_t {
    // safe as this expression is never evaluated
    param data_type_carrier = *cast(NULL, uint32 *);
}

That #if (true) probably sticks out to you: this is a hack to prevent DML from trying to make data part of the template type. Template types are evaluated and constructed at top-level, so object-local parameters are not in scope during their construction: without that #if (true) DML will reject the template, complaining that data_type_carrier is not in scope (this also applies to session.) Since data is made to not be part of the template type, this means you can't access it from a test_t value.

local test_t x = cast(test, test_t);
local uint32 y = x.data; // error

Similarly, you can't declare shared methods or typed parameters leveraging the parametrized type. Untyped parameters, non-shared methods, and subobjects are fair game, though, and as DML doesn't attempt to add such members to the template type you don't need the #if (true) hack for those.

Love Waern
  • 105
  • 2
  • I tried putting typeof(data_type_carrier) in a method and it said unknown identifier. Will this only work for variables? – Anthony Moore Feb 09 '23 at 05:01
  • @AnthonyMoore No, it will work for methods too -- as long as the method implementation [isn't shared](https://github.com/intel/device-modeling-language/wiki/3.-Device-Modeling-Language,-version-1.4#shared-methods). If you need the method to be a member of the template type, have the implementation be separate from the shared method declaration, as noted in the docs. – Love Waern Feb 22 '23 at 11:02