tree: 5df293a2f180dc74a885e594c9e5be741273e0f6 [path history] [tgz]
  1. driver/
  2. codegen.go
  3. file_common_types_header.tmpl
  4. file_common_types_source.tmpl
  5. file_markers.tmpl
  6. file_natural_messaging_header.tmpl
  7. file_natural_messaging_source.tmpl
  8. file_natural_types_header.tmpl
  9. file_natural_types_source.tmpl
  10. file_type_conversions_header.tmpl
  11. file_type_conversions_source.tmpl
  12. file_unified_header.tmpl
  13. file_wire_header.tmpl
  14. file_wire_messaging_header.tmpl
  15. file_wire_messaging_source.tmpl
  16. file_wire_test_base.tmpl
  17. file_wire_types_header.tmpl
  18. file_wire_types_source.tmpl
  19. fragment_bits.tmpl
  20. fragment_const.tmpl
  21. fragment_enum.tmpl
  22. fragment_method_client_impl_async.tmpl
  23. fragment_method_client_impl_async_buffer.tmpl
  24. fragment_method_client_impl_oneway.tmpl
  25. fragment_method_client_impl_oneway_buffer.tmpl
  26. fragment_method_client_impl_sync.tmpl
  27. fragment_method_completer_base.tmpl
  28. fragment_method_event.tmpl
  29. fragment_method_request.tmpl
  30. fragment_method_response.tmpl
  31. fragment_method_result.tmpl
  32. fragment_method_types.tmpl
  33. fragment_method_unownedresult.tmpl
  34. fragment_protocol.tmpl
  35. fragment_protocol_caller.tmpl
  36. fragment_protocol_client_impl.tmpl
  37. fragment_protocol_details.tmpl
  38. fragment_protocol_dispatcher.tmpl
  39. fragment_protocol_event_handler.tmpl
  40. fragment_protocol_event_sender.tmpl
  41. fragment_protocol_interface.tmpl
  42. fragment_service.tmpl
  43. fragment_struct.tmpl
  44. fragment_table.tmpl
  45. fragment_union.tmpl
  46. natural_struct.tmpl
  47. natural_table.tmpl
  48. natural_union.tmpl
  49. README.md
tools/fidl/fidlgen_llcpp/codegen/README.md

Template Organization And Naming

To keep things kind of maintainable here's how templates are named and organized.

Template source files

All template files are named .tmpl.go so that they're recognized by editors and other tools as Go source files but can be differentiated from Go source files that implement their logic in Go rather than the template language.

Each .tmpl.go file should only contain a single string variable named similarly to the file. A file named foo_bar_baz.tmpl.go will contain a single variable:

const fooBarBaz = `
// actual template code here
`

The file should include a comment above the string describing the templates defined in the variable.

Template names

Templates are defined with {{- define "TemplateName" -}} ... {{- end -}} where the template name string with two or three parts separated by colons.

The first part of the template is always the type of data it operates on, the last part of the template is where it can be used and the middle is for disambiguation.

File templates

Each file generated by the bindings generator is rooted in a template file named file_something.tmpl.go containing a string variable named fileSomething with a template named File:Something. For example the header template is in the file file_header.tmpl.go and contains:

const fileHeaderTmpl = `
{{- define "File:Header" -}}
// The header file template
{{ -end }}

Fragment templates

We refer to templates that generate a part of a file as fragment templates. Fragment templates are in files named fragment_*.tmpl.go. Fragment templates are named by the type of object that they operate on. Each fragment template file contains only fragment templates that operate on a specific object type.

For example all templates that operate on struct objects are in fragment_struct.tmpl.go in a variable called fragmentStruct and their names all begin with Struct:.

Templates for some more complex types may be split across multiple files (and hence variables). They should be logically grouped and still must begin with the name of the type being operated on. For example fragment_protocol_client_impl.tmpl.go contains the templates for generating a protocol's ClientImpl. There are templates in this file named Protocol:ClientImpl:Header and Protocol:ClientImpl:Source

Fragment templates must include the name of the file they should be generated into as the last part of their name. So the template for generating struct information into a the header (called from the File:Header file template) is Struct:Header and the template for generating the source file (called from the File:Source file template) is Struct:Source.

Helper templates

Some templates generate code that belongs in multiple places. These are helper templates and instead of having the name of a file as their last part they end with :Helper, for example Method:ClientAllocationComment:Helper.

Integration and build

All template files are listed as sources for gopkg in //tools/fidl/fidlgen_llcpp/BUILD.gn. All template variables are listed alphabetically in the templates variable in the NewGenerator function in //tools/fidl/fidlgen_llcpp/codegen/codegen.go