<commit_before>package scala.meta
package internal
package semantic

import org.scalameta.invariants._
import org.scalameta.unreachable
import scala.reflect.api.Universe
import scala.meta.internal.{ast => m}
import scala.meta.internal.{semantic => s}

// NOTE: This is the little brother of scalahost's Tree.withDenot, doing exactly the same,
// but bailing on local symbols and supporting only a subset of prefixes.
// In principle, I would be in favor of merging these two guys if that was possible,
// but that'll require moving a significant chunk of scalahost into scalameta.

trait Converters {
  val u: Universe

  // NOTE: If you want to uncomment this, think twice.
  // This trait is supposed to be usable with runtime reflection,
  // and at runtime some mirror-based stuff might behave really weirdly
  // (e.g you can have multiple root symbols).
  // val mirror: u.Mirror

  // NOTE: Need a blanket import for class tags to avoid patmat warnings.
  import u.{Type => _, Symbol => _, definitions => _, _}
  private implicit class XtensionConverterSymbol(sym: u.Symbol) {
    def isRootPackage: Boolean = {
      if (sym.isModuleClass) return sym.asClass.module.isRootPackage
      def isProperRoot = sym == u.rootMirror.RootPackage
      def isChildRoot = sym.isPackage && sym.name == termNames.ROOTPKG && sym.owner.isRootPackage
      isProperRoot || isChildRoot
    }
    def isEmptyPackage: Boolean = {
      if (sym.isModuleClass) return sym.asClass.module.isEmptyPackage
      sym.isPackage && sym.name == termNames.EMPTY_PACKAGE_NAME && sym.owner.isRootPackage
    }
  }

  def denot(sym: u.Symbol): s.Denotation = {
    denot(u.NoType, sym)
  }

  def denot(pre: u.Type, sym: u.Symbol): s.Denotation = {
    def isGlobal(sym: u.Symbol): Boolean = {
      def definitelyLocal = sym == u.NoSymbol || sym.name.toString.startsWith("<local ") || (sym.owner.isMethod && !sym.isParameter)
      def isParentGlobal = sym.isPackage || sym.isPackageClass || isGlobal(sym.owner)
      !definitelyLocal && isParentGlobal
    }
    def signature(sym: u.Symbol): s.Signature = {
      if (sym.isMethod && !sym.asMethod.isGetter) {
        val jvmSignature = {
          // NOTE: unfortunately, this simple-looking facility generates side effects that corrupt the state of the compiler
          // in particular, mixin composition stops working correctly, at least for `object Main extends App`
          // val g = c.universe.asInstanceOf[scala.tools.nsc.Global]
          // g.exitingDelambdafy(new g.genASM.JPlainBuilder(null, false).descriptor(gsym))
          def jvmSignature(tpe: u.Type): String = {
            val u.TypeRef(_, sym, args) = tpe
            require(args.nonEmpty ==> (sym == u.definitions.ArrayClass))
            if (sym == u.definitions.UnitClass) "V"
            else if (sym == u.definitions.BooleanClass) "Z"
            else if (sym == u.definitions.CharClass) "C"
            else if (sym == u.definitions.ByteClass) "B"
            else if (sym == u.definitions.ShortClass) "S"
            else if (sym == u.definitions.IntClass) "I"
            else if (sym == u.definitions.FloatClass) "F"
            else if (sym == u.definitions.LongClass) "J"
            else if (sym == u.definitions.DoubleClass) "D"
            else if (sym == u.definitions.ArrayClass) "[" + jvmSignature(args.head)
            else "L" + sym.fullName.replace(".", "/") + ";"
          }
          val u.MethodType(params, ret) = sym.info.erasure
          val jvmRet = if (!sym.isConstructor) ret else u.definitions.UnitClass.toType
          s"(" + params.map(param => jvmSignature(param.info)).mkString("") + ")" + jvmSignature(jvmRet)
        }
        s.Signature.Method(jvmSignature)
      }
      else if (sym.isTerm) s.Signature.Term
      else if (sym.isType) s.Signature.Type
      else unreachable(debug(sym, sym.asInstanceOf[scala.reflect.internal.Symbols#Symbol].flags, sym.getClass, sym.owner))
    }
    def convertPrefix(pre: u.Type): s.Prefix = {
      def singletonType(pre: u.Type, sym: u.Symbol): m.Type = {
        val name = {
          if (sym.isRootPackage) "_root_"
          else if (sym.isEmptyPackage) "_empty_"
          else sym.name.toString
        }
        m.Type.Singleton(m.Term.Name(name).withDenot(denot(pre, sym)))
      }<commit_msg>properly attributes trees emitted by Converters.scala<commit_after>package scala.meta
package internal
package semantic

import org.scalameta.invariants._
import org.scalameta.unreachable
import scala.reflect.api.Universe
import scala.meta.internal.{ast => m}
import scala.meta.internal.{semantic => s}
import scala.meta.internal.flags._

// NOTE: This is the little brother of scalahost's Tree.withDenot, doing exactly the same,
// but bailing on local symbols and supporting only a subset of prefixes.
// In principle, I would be in favor of merging these two guys if that was possible,
// but that'll require moving a significant chunk of scalahost into scalameta.

trait Converters {
  val u: Universe

  // NOTE: If you want to uncomment this, think twice.
  // This trait is supposed to be usable with runtime reflection,
  // and at runtime some mirror-based stuff might behave really weirdly
  // (e.g you can have multiple root symbols).
  // val mirror: u.Mirror

  // NOTE: Need a blanket import for class tags to avoid patmat warnings.
  import u.{Type => _, Symbol => _, definitions => _, _}
  private implicit class XtensionConverterSymbol(sym: u.Symbol) {
    def isRootPackage: Boolean = {
      if (sym.isModuleClass) return sym.asClass.module.isRootPackage
      def isProperRoot = sym == u.rootMirror.RootPackage
      def isChildRoot = sym.isPackage && sym.name == termNames.ROOTPKG && sym.owner.isRootPackage
      isProperRoot || isChildRoot
    }
    def isEmptyPackage: Boolean = {
      if (sym.isModuleClass) return sym.asClass.module.isEmptyPackage
      sym.isPackage && sym.name == termNames.EMPTY_PACKAGE_NAME && sym.owner.isRootPackage
    }
  }

  def denot(sym: u.Symbol): s.Denotation = {
    denot(u.NoType, sym)
  }

  def denot(pre: u.Type, sym: u.Symbol): s.Denotation = {
    def isGlobal(sym: u.Symbol): Boolean = {
      def definitelyLocal = sym == u.NoSymbol || sym.name.toString.startsWith("<local ") || (sym.owner.isMethod && !sym.isParameter)
      def isParentGlobal = sym.isPackage || sym.isPackageClass || isGlobal(sym.owner)
      !definitelyLocal && isParentGlobal
    }
    def signature(sym: u.Symbol): s.Signature = {
      if (sym.isMethod && !sym.asMethod.isGetter) {
        val jvmSignature = {
          // NOTE: unfortunately, this simple-looking facility generates side effects that corrupt the state of the compiler
          // in particular, mixin composition stops working correctly, at least for `object Main extends App`
          // val g = c.universe.asInstanceOf[scala.tools.nsc.Global]
          // g.exitingDelambdafy(new g.genASM.JPlainBuilder(null, false).descriptor(gsym))
          def jvmSignature(tpe: u.Type): String = {
            val u.TypeRef(_, sym, args) = tpe
            require(args.nonEmpty ==> (sym == u.definitions.ArrayClass))
            if (sym == u.definitions.UnitClass) "V"
            else if (sym == u.definitions.BooleanClass) "Z"
            else if (sym == u.definitions.CharClass) "C"
            else if (sym == u.definitions.ByteClass) "B"
            else if (sym == u.definitions.ShortClass) "S"
            else if (sym == u.definitions.IntClass) "I"
            else if (sym == u.definitions.FloatClass) "F"
            else if (sym == u.definitions.LongClass) "J"
            else if (sym == u.definitions.DoubleClass) "D"
            else if (sym == u.definitions.ArrayClass) "[" + jvmSignature(args.head)
            else "L" + sym.fullName.replace(".", "/") + ";"
          }
          val u.MethodType(params, ret) = sym.info.erasure
          val jvmRet = if (!sym.isConstructor) ret else u.definitions.UnitClass.toType
          s"(" + params.map(param => jvmSignature(param.info)).mkString("") + ")" + jvmSignature(jvmRet)
        }
        s.Signature.Method(jvmSignature)
      }
      else if (sym.isTerm) s.Signature.Term
      else if (sym.isType) s.Signature.Type
      else unreachable(debug(sym, sym.asInstanceOf[scala.reflect.internal.Symbols#Symbol].flags, sym.getClass, sym.owner))
    }
    def convertPrefix(pre: u.Type): s.Prefix = {
      def singletonType(pre: u.Type, sym: u.Symbol): m.Type = {
        val name = {
          if (sym.isRootPackage) "_root_"
          else if (sym.isEmptyPackage) "_empty_"
          else sym.name.toString
        }
        val denotedName = m.Term.Name(name).withDenot(denot(pre, sym))
        val attributedName = denotedName.withRecursiveTyping.withIdentityExpansion
        m.Type.Singleton(attributedName).setTypechecked
      }
{'fraction_non_alphanumeric': 0.11766044606281292, 'fraction_numerical': 0.0, 'mean_word_length': 2.8246301131418625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2144730', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2657, 'n_words': 723}