
The problem is, its most likely easier to push this to the end user programmers and provide them with a simple "extern(C):" type keyword and leverage the standard linkers and tools.

I've specifically used "C Linker" here, if you're going to write your own linker that can understand this meta data, then you might be able to get somewhere to automating some of this.
NAME MANGLER ALTERNATIVE CODE
For the others, you'd have to specify what concrete implementation you wanted to export - And if you wanted make a dynamic decision on which, you have to provide a stub function inside your languages code to do the dispatching. IMHO, I don't believe it makes sense to export Reference Types to C in this fashion, so id ignore subtype and probably templated/generic polymorphism. To get C to communicate to your language, my current opinion is you have to boil it down to one concrete function per exported function name. a C Linker will not be able to determine what to bind a name to. The function name also now implicitly states what stack frame size, and the layout of the bits in that stack frame - its expecting. If you're concerned with name mangling, we're already at the stage of linking. You've mentioned segments, do you mean executable segments? How is the C Linker going to understand this meta information? And what happens if there is more than one definition to a function? One of the key points of polymorphism is to have a single Symbol be bound to different definitions - this is a step towards composibility and generic programming. Ad hoc Polymorphism (Function Overloading, Operator Overloading) and Parametric Polymorphism - C does not support any of these. I think its important to note that there are several different types of polymorphism, e.g. On the bigger picture - You've already noted that polymorphism creates the need for name mangling or a name mangling like thing. I'm trying to make C and other system libraries/resources available to my language. (This skips the whole difference in ABI because that's a separate problem.)ĮDIT: I want to be clear that I'm not making my language available to C.

Now I can define C names in a module, and because they have only one implementation, the compiler can just link the C functions into the rest of the program. If a function has only one definition, it automatically starts at the main segment offset by default. Then the compiler adds an annotation segment or meta segment with a table containing the type signatures and offsets for each function.

Each definition for that function gets its own offset inside the segment. I think I finally figured out an alternative solution.įor each function name, the compiler adds a segment with that name. The problem is that functions in my language are also polymorphic which normally requires name mangling. I want my language to have automatic C compatibility so a module in my language can provide names of functions it links to in a C library.
