Uploaded image for project: 'Kogito'
  1. Kogito
  2. KOGITO-1767

Propose mechanism to extend codegen with addons

    XMLWordPrintable

Details

    • 5

    Description

      Addons should be able to inject codegen at predefined location. Describe and discuss how to obtain this

      (see design document)

      Codegen Extension

      How? Two approaches discussed in the following

      Placeholders

      • Addon is given an instantiated template (e.g. CompilationUnit)
      • Addon replaces arbitrary placeholders

      Example

      interface Addon {
          void replacePlaceHolders(CompilationUnit u);
      }
      class MyAddon implements Addon {
          void replacePlaceHolders(CompilationUnit u) {
              u.findFirst(MethodCallExpr.class,
                          m -> m.getNameAsString().equals("$someMethod$"))
                      .ifPresent(u.replace(...));
          }
      }
      

       

      PROS: 

      • Simplest approach
      • It is the same pattern used in “main” codegen
      • Potentially at some point we may align “main” codegen and treat everything as addons
      • Addons may do anything

      CONTRAS:

      • Addons may do anything
      • Harder to limit/predict what it is doing/where it is breaking

      Plug-In Mechanism

      • Addons may only hook into specific extension points; e.g. implement an interface
      • The system may replace known placeholders with addon-supplied AST nodes, by invoking the interface methods

      Example

      interface Addon {
          Expression augmentSomeMethod(MethodCallExpr m);
      }
      class MyAddon implements Addon {
          Expression augmentSomeMethod(MethodCallExpr m){
              // wrap m, and return the new expression
          }
      }
      
      

      PROS: 

      • Addons may only hook into specific extension points => easier to control/more predictable

       

      CONTRAS:

      • Addons are second-class citizens, as opposed to “main” codegen. Not necessarily a contra.

      The exercise may even help to find out that also “main” codegen only hooks into very specific points => define interfaces for all components__

      • It may be uglier to scale if the number of extension points grows; extension points may become ad-hoc, e.g. worst case, we may have many extension points, but each addon only replace 1 distinct placeholder (i.e. no overlaps)

      However we we may use “many interfaces” instead of 1 interface with many methods_

      e.g.:

      interface SomeMethodAugmenter {
          Expression augment(MethodCallExpr m);
      }
      interface SomeOtherMethodAugmenter {
          Expression augment(MethodCallExpr m);
      }
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              evacchi Edoardo Vacchi (Inactive)
              Marian Macik Marian Macik
              Marian Macik Marian Macik
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: