asn1: Circular types and Topo. sort declarations

Many external ASN.1 modules that we have imported over time define types
like this:

  Foo ::= SEQUENCE { bar Bar }
  Bar ::= SEQUENCE { aMember INTEGER }

and before this change one had to re-order the definitions so that the
one for `Bar` came first.  No more.

We can now have out of order definitions in ASN.1 modules and the
compiler will topologically sort output C type declarations so that one
no longer has to manually sort types in ASN.1 modules when importing
them.

Besides that, it is now possible to create circular data types using
OPTIONAL since we generate such fields as pointers (which can then be
pointers to incomplete struct declarations):

  Circular ::= SEQUENCE {
          name UTF8String,
          next Circular OPTIONAL
  }

Circular types aren't necessarily useful, but they have been used in the
past.  E.g., the rpc.mountd protocol uses a circular type as a linked
list -- it should just have used an array, of course, as that's
semantically equivalent but more space efficient in its encoding, but
the point is that such types exist out there.
This commit is contained in:
Nicolas Williams
2020-12-24 03:18:28 -06:00
parent 5b978c7437
commit 83d4c6ddb5
8 changed files with 367 additions and 30 deletions

View File

@@ -103,13 +103,16 @@ typedef struct asn1_module {
/* CLI options and flags needed everywhere: */
getarg_strings preserve;
getarg_strings seq;
const char *enum_prefix;
unsigned int one_code_file:1;
unsigned int support_ber:1;
unsigned int parse_units_flag:1;
unsigned int prefix_enum:1; /* Should be a getarg_strings of bitrsting types to do this for */
unsigned int rfc1510_bitstring:1; /* Should be a getarg_strings of bitrsting types to do this for */
} *asn1_module;
void generate_type (const Symbol *);
void generate_type_header_forwards(const Symbol *);
void generate_constant (const Symbol *);
void generate_type_encode (const Symbol *);
void generate_type_decode (const Symbol *);
@@ -143,6 +146,7 @@ void close_codefile(void);
int is_template_compat (const Symbol *);
void generate_template(const Symbol *);
void generate_template_type_forward(const char *);
void gen_template_import(const Symbol *);
@@ -152,6 +156,7 @@ extern int support_ber;
extern int template_flag;
extern int rfc1510_bitstring;
extern int one_code_file;
extern int original_order;
extern int parse_units_flag;
extern char *type_file_string;