Skip to content

Commit

Permalink
preserve annotation arguments in the AST
Browse files Browse the repository at this point in the history
  • Loading branch information
larsrh authored and colder committed Aug 7, 2015
1 parent ae01e59 commit 3e4281d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/main/scala/leon/frontends/scalac/ASTExtractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ trait ASTExtractors {
rootMirror.getClassByName(newTermName(str))
}

def annotationsOf(s: Symbol): Set[String] = {
def annotationsOf(s: Symbol): Map[String, Seq[Option[Any]]] = {
val actualSymbol = s.accessedOrSelf

(for {
a <- actualSymbol.annotations ++ actualSymbol.owner.annotations
name = a.atp.safeToString.replaceAll("\\.package\\.", ".")
if (name startsWith "leon.annotation.")
} yield {
name.split("\\.", 3)(2)
}).toSet
val args = a.args.map {
case Literal(x) => Some(x.value)
case _ => None
}
(name.split("\\.", 3)(2), args)
}).toMap
}

protected lazy val tuple2Sym = classFromName("scala.Tuple2")
Expand Down Expand Up @@ -406,7 +410,7 @@ trait ASTExtractors {
case DefDef(_, name, tparams, vparamss, tpt, rhs) if name != nme.CONSTRUCTOR && !dd.symbol.isAccessor =>
if (dd.symbol.isSynthetic && dd.symbol.isImplicit && dd.symbol.isMethod) {
// Check that the class it was generated from is not ignored
if (annotationsOf(tpt.symbol)("ignore")) {
if (annotationsOf(tpt.symbol).isDefinedAt("ignore")) {
None
} else {
Some((dd.symbol, tparams.map(_.symbol), vparamss.flatten, tpt.tpe, rhs))
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/leon/frontends/scalac/CodeExtraction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ trait CodeExtraction extends ASTExtractors {

fd.setPos(sym.pos)

fd.addFlags(annotationsOf(sym).map(FunctionFlag.fromName))
fd.addFlags(annotationsOf(sym).map { case (name, args) => FunctionFlag.fromName(name, args) }.toSet)

if (sym.isImplicit) {
fd.addFlag(IsInlined)
Expand Down Expand Up @@ -667,7 +667,7 @@ trait CodeExtraction extends ASTExtractors {
fd.setPos(sym.pos)
fd.addFlag(IsField(isLazy))

fd.addFlags(annotationsOf(sym).map(FunctionFlag.fromName))
fd.addFlags(annotationsOf(sym).map { case (name, args) => FunctionFlag.fromName(name, args) }.toSet)

defsToDefs += sym -> fd

Expand Down Expand Up @@ -1082,7 +1082,7 @@ trait CodeExtraction extends ASTExtractors {

val tparamsMap = (tparams zip fd.tparams.map(_.tp)).toMap

fd.addFlags(annotationsOf(d.symbol).map(FunctionFlag.fromName))
fd.addFlags(annotationsOf(d.symbol).map { case (name, args) => FunctionFlag.fromName(name, args) }.toSet)

val newDctx = dctx.copy(tparams = dctx.tparams ++ tparamsMap)

Expand Down
9 changes: 5 additions & 4 deletions src/main/scala/leon/purescala/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,16 @@ object Definitions {
sealed abstract class FunctionFlag

object FunctionFlag {
def fromName(name: String): FunctionFlag = name match {
def fromName(name: String, args: Seq[Option[Any]]): FunctionFlag = name match {
case "inline" => IsInlined
case _ => Annotation(name)
case _ => Annotation(name, args)
}
}

// Whether this FunDef was originally a (lazy) field
case class IsField(isLazy: Boolean) extends FunctionFlag
// Compiler annotations given in the source code as @annot
case class Annotation(annot: String) extends FunctionFlag
case class Annotation(annot: String, args: Seq[Option[Any]]) extends FunctionFlag
// If this class was a method. owner is the original owner of the method
case class IsMethod(owner: ClassDef) extends FunctionFlag
// If this function represents a loop that was there before XLangElimination
Expand Down Expand Up @@ -401,7 +401,8 @@ object Definitions {

def flags = flags_

def annotations: Set[String] = flags_ collect { case Annotation(s) => s }
def annotations: Set[String] = extAnnotations.keySet
def extAnnotations: Map[String, Seq[Option[Any]]] = flags_.collect { case Annotation(s, args) => s -> args }.toMap
def canBeLazyField = flags.contains(IsField(true)) && params.isEmpty && tparams.isEmpty
def canBeStrictField = flags.contains(IsField(false)) && params.isEmpty && tparams.isEmpty
def canBeField = canBeLazyField || canBeStrictField
Expand Down

0 comments on commit 3e4281d

Please sign in to comment.