title: How do I get a scala macro to replace a method call
body: <p>How do I get a scala macro to replace a method call?</p>
  
  <p>My goal is to create a trait called <code>ToStringAdder</code>. Supposing I have an object <code>x</code> with this trait, then when I call <code>x.add(any)</code> I want the macro to actually call <code>x.add(any, string)</code> where the string is a string representation of the AST. (This is so that I can have nice <code>tostring</code> when 'any' is a function). </p>
  
  <ul>
  <li>I've written the macros given in the documentation
  <a href="http://docs.scala-lang.org/overviews/macros/overview.html">http://docs.scala-lang.org/overviews/macros/overview.html</a>,  </li>
  <li>Read <a href="http://docs.scala-lang.org/sips/pending/self-cleaning-macros.html">http://docs.scala-lang.org/sips/pending/self-cleaning-macros.html</a></li>
  <li>Read the pdfs <a href="http://scalamacros.org/paperstalks/2013-04-22-LetOurPowersCombine.pdf">http://scalamacros.org/paperstalks/2013-04-22-LetOurPowersCombine.pdf</a>.</li>
  <li>I've looked at the code in <a href="https://github.com/retronym/macrocosm/blob/master/src/main/scala/com/github/retronym/macrocosm/Macrocosm.scala">https://github.com/retronym/macrocosm/blob/master/src/main/scala/com/github/retronym/macrocosm/Macrocosm.scala</a></li>
  <li>And the code in    <a href="https://github.com/pniederw/expecty/tree/master/src/main/scala/org/expecty">https://github.com/pniederw/expecty/tree/master/src/main/scala/org/expecty</a>,
  but I'm sad to say I didn't Grok it all </li>
  <li>I have had a look at the scaladoc, but...woo..that's complex... I suspect that I would need some significant time to get up to speed with how the compiler works.</li>
  </ul>
  
  <p>With the exception of Expecty all of these examples I have seen are effectively using static method calls: the object that the macro is called on is not used.
  Expecty has the following method, which gives me a clue about how to detect the 'implicit this', but I couldn't find a way to reference it in a reify call.</p>
  
  <pre><code> private[this] def recordAllValues(expr: Tree): Tree = expr match {
      case New(_) =&gt; expr // only record after ctor call
      case Literal(_) =&gt; expr // don't record
      // don't record value of implicit "this" added by compiler; couldn't find a better way to detect implicit "this" than via point
      case Select(x@This(_), y) if getPosition(expr).point == getPosition(x).point =&gt; expr
      case _ =&gt; recordValue(recordSubValues(expr), expr)
    }
  </code></pre>
  
  <p>So how do I go about replacing the call to the object that the macro has been called on. The code I have at the moment is below, and it is the code in the reify call that needs to be sorted</p>
  
  <pre><code>trait ToStringAdder {
    def add(param: Any): Any = macro ToStringAdder.toStringAndValueImpl
    def add(param: Any, toStringBasedOnAST: String): Any ; //This is the actual method I want the above method call to be replaced by
  }
  
  object ToStringAdder {
    def toStringAndValueImpl(c: Context)(param: c.Expr[Any]): c.Expr[Unit] = {
      import c.universe._
      val paramRep = show(param.tree)
      val paramRepTree = Literal(Constant(paramRep))
      val paramRepExpr = c.Expr[String](paramRepTree)
      //need to put something here 
       reify { c.someMethodCall("something to represent the method any", param.splice, paramRepExpr.splice ) }
    }
  }
  </code></pre>