Search code examples
javadebuggingintellij-ideaclojurecompiler-construction

How do I debug the application of arguments to my function in the Clojure Compiler?


My purpose is to debug the Clojure Compiler to understand more about it. (My project code is an aid to this purpose.).

I've got both the Clojure Compiler source and my project test set up in my IntelliJ IDE. enter image description here

I've got the the clojure project as the primary project, and the test project is on the classpath. enter image description here

I'm triggering the Clojure REPL in the debugger by running the clojure.main class in the clojure project. enter image description here

My test project has the following source in the test.core core.clj file:

(ns test.core
  (:gen-class))

(deftype SquarePeg [width length])

(deftype RoundHole [radius])

(def square-peg (SquarePeg. 5 50))

(defn insert-peg [^test.core.RoundHole peg]
    (println "insert-peg inserted with: " peg))

(defn -main
  "Insert a square peg in a round hole"
  [& args]
    (insert-peg square-peg)
  (println "Done."))

I've set the following breakpoints:

enter image description here

Now what I'm doing is pasting in the following function invocation into the REPL after I've switched the breakpoints on:

(insert-peg square-peg)

enter image description here

Now what I see is that the println (as pr) in the code above runs through the AFn.java single arg invoke, but the call to insert-peg does not. (This is what I want to debug).

enter image description here

(First breakpoint tripped after breakpoints switched on).

enter image description here

I do see the declarations of insert-peg and square-peg running through the emit code in the Compiler.java - but what I'm looking for is the function application. I want to see square-peg be applied to insert-peg running in the compiler code in the debugger. (Perhaps I'm missing a breakpoint, or looking in the wrong place).

Assumptions:

  • I don't switch the breakpoints on, until after the REPL has loaded, and I've pasted the code into the REPL running in the IDE console.

My question is: How do I debug the application of arguments to my function in the Clojure Compiler?


Solution

  • The trick was the function InvokeExpr(...) in Compiler.java.

    In particular the VarExpr section.