Using damlc as library?

I would like to gather information on Daml source code, specifically data type definitions:

data Foo = Bar | Baz Int

An example for information I am interested in is data constructor names and parameters.

I am thinking about two approaches:

  1. given a Daml source file, parse contents using damlc as library
  2. given a .dar file, interpret .hi file using damlc as library

Does damlc support any of the above? If yes, I am really curious how.

Alternatively, I would rely on ghc to interpret .hi files. A .hi generated by daml can be fed into ghc just fine, it seems:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.8.1
$ ghc --show-iface Account.hi
Magic: Wanted 33214052,
       got    33214052
Version: Wanted [8, 0, 8, 1],
         got    [8, 0, 8, 1]
Way: Wanted [],
     got    [u]
interface Account 8081
  interface hash: 314bbad17875fb85b8ac18419f6815da
  ABI hash: 51bdfa62586e157c5e48478363757a07
  [...]
exports:
  _choice_AccountArchive
  _choice_AccountUpdateAccount
  [...]
[...]

I’d recommend against either of those options. The Daml compiler is definitely not usable as a reasonable API and we have no plans to change that. And neither are the .hi files. They are already not present in all circumstances, they are not stored on the ledger and we give no guarantees that you can read them with regular GHC.

Instead, you are much better off doing analysis at the Daml-LF level instead. There are a few different options here:

  1. Work with the generated Java protobuf library Maven Central Repository Search. This gives you pretty much a direct mapping from the daml-lf protobuf.
  2. Work with the Scala interface library Maven Central Repository Search. This gives you only access to serializable types but in return it provides you with a more high-level API that abstracts over versions.
  3. Use the scala archive reader Maven Central Repository Search. This is similar to 1 but gives you a slightly nicer representation.

Note that the Java libraries are only as stable as LF itself which changes a fair bit cross versions and the Scala libraries are not supported and can break APIs at any point.

So if I’m not mistaken, you suggest querying against a ledger via gRPC. Could you point me to which gRPC API endpoint could help? GetPackageRequest seems like a candidate to retrieve a package. I suppose a package contains modules, which contain data types.

you have to get the Daml-LF source code from somewhere. There are a few different options for that:

  1. Either you already have the DAR locally.
  2. Or you use daml ledger fetch-dar to fetch a DAR from the ledger. Internally that uses the API you pointed to.
  3. Or you call that API yourself. I’d probably still separate fetching the packages from doing whatever analysis you want to do on them though.