Text templates for code generation (?)

I didn’t make issues because I didn’t have time to work on them yet but here’s a sampling based on a quick run-through. It’s a 5k line file so at least we should have mercy on anyone else who wants to understand the code generator and break it up.

  1. the generic visitor stuff is currently mixed in the same file as the code that uses streams to generate very specific things like semicolons. The generic code could just get moved to another file to make the core of the code generation more readable.

  2. namespaces get generated separately from their end-brackets, the functions for adding namespaces should wrap a chunk of generated code in a namespace instead. Same with class declarations (and maybe more?)

  3. code for calculating valid sizes should be separate from code generation of statements to check for valid sizes.

  4. generate_validate_positive code-generates a call to a function called “validate_non_negative_index” so the name you call doesn’t match the name you generate (literally or conceptually). I think there’s more of this.

  5. few of the functions have any documentation even when they make non-trivial assumptions (which are not clear from the name).

  6. There are a bunch of comments like “// see member_var_decl_visgen cut & paste” and at least some of them refer to actual cut-and-paste that should just go into a function.

  7. code-generation for type inits could be its own file, ditto for validation functions.

  8. ‘generate_type’ actually generates array types (pretty sure…) so it’ generates code for array of array of … of type. A clearer name for this specific function would help but so would looking over the rest of them w.r.t. naming.

  9. generate_located_statement and generate located statement(s) are mostly cut-and-paste, one of those should call the other.

  10. In ‘generate_function_template_parameters’ there’s a bunch of switching on booleans that indicate function type but you have to read the code closely to figure out how many function types there are, this could just be a switch on a well-named enum. Similar in ‘generate_function_arguments’ and ‘generate_functor_arguments’.

  11. generate_cpp is actually generating the ‘class concept’, that could be more explicit, especially since you might want to code-generate other cpp (like, e.g., just the functions block or just the generated quantities block).

1 Like