What information is required when emitting a decla...
# compiler
c
What information is required when emitting a declaration through
FirDeclarationGenerationExtension
? Can you just emit an unbound FIR symbol with a name only and fill out the rest in IR, or do you need to have a backing FIR function/class/property? If it's the latter, just how much information do you need in FIR? (e.g. do you need supertypes for classes, a fully-resolvable function signature, return type for properties, etc.) I'm trying to figure out the least amount of information I can get away with, even if it means I have to do more work during IR.
y
The rule usually is that you need to emit fully resolved (i.e.
BODY_RESOLVE
phase) FIR, but you can skip function bodies. The purpose of
FirDeclarationGenerationExtension
is to emit things that the same compilation (and further consumers of the compiled code) can see. So, if you were able to emit a function with just a name, that wouldn't be super useful, would it now? If you want to add private functions and such for your plugin's purposes, definitely do that in IR. Fun fact, you can emit things in IR that can be seen by other compilations by making them metadata-visible. More info: https://kotlinlang.slack.com/archives/C7L3JB43G/p1791977988326849?thread_ts=1791883135.742039&cid=C7L3JB43G
c
Thanks!
I'm creating a DSL for creating declarations at compile-time without going through a compiler plugin. I'm trying to figure out just how little info I can get away with them specifying, so I don't have to do quite so much inside a contract-style block.
y
Note that it's probably infeasible to read this DSL within the same compilation.
FirDeclarationGenerationExtension
is called very early on, earlier than the bodies of functions are ever resolved
c
Right, I forgot about that. I guess I gotta go the annotation route, then?
y
Sadly, yeah What you can try and do is have them specify things in the form of interfaces and such, then I think you may be able to read those
c
I think I get what you mean
But regardless, now I really want to know how little info I can get away with.
y
Well, you basically need the function signature fully written out. You don't really get the chance to fill things out anywhere else before IR. There are some intermediate
FirExtension
s you can try and use, especially some of the more internal/experimental ones. FirFunctionCallRefinementExtension in particular can do a lot of crazy things, especially if you throw caution to the wind and start modifying FIR (Not supported, obviously). Another favorite of mine is FirAssignExpressionAltererExtension. It seems really simple on the surface, but it's (to my knowledge) the only "non-internal" extension that lets you generate unresolved FIR (which is pretty close to source code, meaning you can trigger call resolution and smart casts and such). Combined with more hackery and FIR modification, you can do some stupid but fun things. I know less about these, but there are a few scripting-related `FirExtension`s, which give you a lot of freedom (it seems). That was a bit tangential, so to get back to the main point: try and narrow down what your use case really is (generating any declaration at compile time might be too much to approach at once), then you can start experimenting and (optionally) doing unsupported things. If this isn't just experimentation, then please ignore all the bad advice I've provided above 😅. If this is experimentation, I have some hacky code I can share, but it isn't well-commented.
c
Wow, this is really interesting! Yeah, I saw some insane stuff in the FuncCallRefinement docs that I'm really excited to try out here. Right now, I'm in the requirements phase for a really cool project. My master's program didn't end up using nearly as much of my time as I expected and I'm bored, so I'm eager to leverage my decade of metaprogramming-shenanigans experience on something really revolutionary. (or just providing feature-parity with another language, you know the drill)
👀 1
I'm doing my due-diligence ATM to make sure it's even possible before officially asking for help with it, lol
y
I haven't shared this around publicly before, and it's definitely misusing everything the compiler offers and more (even some reflection), but this project might offer some ideas It implements higher-kinded types and type functions as a compiler plugin. In other words, it lets you do (untyped) lambda calculus at the type level in other, other words, it makes the type system Turing complete (you can write infinite loops incredibly easily...) I haven't even scratched the surface of what these techniques can do. It plays around with the scopes that each class has, meaning you might be able to modify this to add functions at arbitrary points in code... Have I said this is definitely not supported yet?
👀 1
c
in other, other words, it makes the type system Turing complete
Hold the phone. Does it support variable-arity types?
Also, that's genuinely incredible, and is exactly the kind of thing I'm obsessed with
❤️ 1
thank you color 1
y
Kinda? It uses currying (at the type level, of course), meaning you could define a type-level
List
, for instance, using Church encoding
Here's what defining type-level numbers looks like: github.com/kyay10/highKT/blob/…/church.kt You can use type-level predicates on them to produce whatever types you want
c
No... no freaking way. You implemented C++'s stupid type-based computation system that I love???
😄 1
y
I'm not super familiar with C++ even (although I vaguely know of `concept`s and have thought about making a compiler plugin for them) but it probably does something similar with that or template metaprogramming or something
c
Actually I guess it is just "type-based computation", not specific to C++. I just happened to see it in C++ first.
It's such an interesting idea, and genuinely very useful
y
Also before I oversell this, there's definitely bugs. Most of my "testing" has been super specific examples and only done thru compilation testing, so I haven't checked IDE support or squashed all the bugs or anything I've done some crazy things with it though. If you're functionally inclined, you might enjoy the type functions example, or if you vaguely know some Category Theory, here's a "proof" that "a monad is a monoid in the category of endofunctors"
c
here's a "proof" that "a monad is a monoid in the category of endofunctors"
It's absolutely unreadable and completely nonsensical, but I can't help but get some overwhelming bliss at seeing it. It's just so stupid that it works, but it's so cool.
Not just as a "curiosity", but genuinely useful
it's just so silly seeing types used like that even though it's genuinely amazing
💯 1
I could see some really cool stuff from compile-time type validation to (oh god) SFINAE-based compile-time polymorphism being done with that
Not to mention simple things like validating that certain events have occurred in the lifetime of an object
y
I mean, Kotlin isn't a proof language, and it has TODOs and exceptions and infinite loops, so it doesn't actually function as a proof, but if you could somehow restrict all of that, it would be valid. Btw, the "proof" is a translated version of data-category from Haskell. I'm not clever enough to come up with this in a short enough time period, but I'm pretty darn good at translating code and solving type-puzzles! You can definitely do crazy things with it, but there's probably some practical limit on how much computation you can do before the compiler is just grinding to a halt (and where it'd obviously be better to build a specialized compiler plugin)
The general technique though of changing the compiler's perception of types, supertyping, and resolution scope, is definitely applicable to many more use cases
c
By the way, that SFINAE example is similar to what I was talking about with C++'s stupid type computation that I love
👀 1
Unsure, but it seems like your plugin can essentially mimic that static-polymorphism behavior with extension functions on those composite types:
SomeCalculationTrue.value = x
SomeCalculationFalse.value = y
y
You could do something like:
Copy code
fun <T: True> foo(x: String) {}
fun <F: False> foo(x: String) {}

foo<SomeTypeCalculation<True>>(x)
foo<SomeTypeCalculation<False>>(y)
and get different results