Hello, I want to implement compiler plugin, that c...
# compiler
t
Hello, I want to implement compiler plugin, that creates something like macro from annotated function. Something that would take something like:
Copy code
fun styled(block : RuleSet, @CallerPackage packageName)
and introduce synthetic macro-like function:
Copy code
fun styled(block : RuleSet) = styled(block, callerPackage())
and wherever
styled(block)
is used, plugin would replace calls with
styled(block, "actual.callers.package.name")
. Are there any good examples I could use? Maybe somewhere in compose?
s
It should be possible to implement this as an IR transform. You would need to find the symbol of
styled
(you can check how Compose finds
Composer
class, for example) and then run replace each
IrCall
which calls that function (using
IrElementTransformer
or similar). Package name can be retrieved from parent class or file (fqName property iirc) Btw, I recommend using
fun styled(block: RuleSet, packageName: String = throw IllegalArgumentException("Must be replaced at compile time"))
If it is called with default parameter, it will be easier to replace just one argument and not whole call (each function has different symbol and you'll have to replace it completely) I don't have any examples atm (typing this from the mobile), but maybe someone will link similar transforms.
r