Uploaded image for project: 'Swift'
  1. Swift
  2. SR-12022

Refactor LiteralExpr subclasses to combine common initializer code paths



    • Type: Task
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:


      Every *LiteralExpr AST node has an Initializer member variable or equivalent, and a large subset of them have a second BuiltinInitializer member variable. However, each subclass declares its own copies of these. This leads to a lot of unnecessary duplication; for instance, SILGenFunction::emitLiteral() has about a dozen lines that merely call getInitializer() or getBuiltinInitializer() on various subclasses.

      You could improve this situation by refactoring the LiteralExpr subclasses:

      1. Rename InterpolatedStringLiteralExpr::get/setResultInit() to get/setInitializer() and update use sites so that it matches the convention of the other literal expressions
      2. Hoist the Initializer member variable and its accessor methods into LiteralExpr
      3. Introduce a new subclass with the BuiltinInitializer member variable and its accessor methods
      4. Make all the literals with {{BuiltinInitializer}}s subclasses of that new class
      5. Audit the call sites of getInitializer(), setInitializer(), getBuiltinInitializer(), and setBuiltinInitializer() to see how much code you can now consolidate

      Since you're changing the hierarchy of Expr subclasses, you'll need to update include/swift/AST/ExprNodes.def. The comments at the top of the file should help you do that.

      This task should not cause any changes in functionality—it's just a straight-up refactoring and simplification—so if you've done this correctly, all existing tests should pass and you won't need to write any new ones.




            xcadaverx Daniel Williams
            brentdax Brent Royal-Gordon
            0 Vote for this issue
            1 Start watching this issue