<commit_before>/* NSC -- new scala compiler
 * Copyright 2004-2007 LAMP/EPFL
 */

// $Id$

package scala.tools.nsc.symtab.clr

import java.io.IOException

import ch.epfl.lamp.compiler.msil.{Type => MSILType, Attribute => MSILAttribute, _}

import scala.collection.mutable.{HashMap, HashSet}
import scala.tools.nsc.util.{Position, NoPosition}
import classfile.UnPickler

/**
 *  @author <NAME>
 */
abstract class TypeParser {

  val global: Global
  import global.loaders.clrTypes
  import clrTypes.global._

  //##########################################################################

  private var clazz: Symbol = _
  private var instanceDefs: Scope = _   // was members
  private var staticModule: Symbol = _  // was staticsClass
  private var staticDefs: Scope = _     // was statics

  protected def statics: Symbol = staticModule.moduleClass

  protected var busy: Boolean = false       // lock to detect recursive reads

  private object unpickler extends UnPickler {
    val global: TypeParser.this.global.type = TypeParser.this.global
  }

  def parse(typ: MSILType, root: Symbol) {

    def handleError(e: Exception) = {
      if (settings.debug.value) e.printStackTrace()  //debug
      throw new IOException("type '" + typ.FullName + "' is broken\n(" + e.getMessage() + ")")
    }
    assert(!busy)
    busy = true

    if (root.isModule) {
      this.clazz = root.linkedClassOfModule
      this.staticModule = root
    } else {
      this.clazz = root
      this.staticModule = root.linkedModuleOfClass
    }
    try {
      parseClass(typ)
    } catch {
      case e: FatalError => handleError(e)
      case e: RuntimeException => handleError(e)
    }
    busy = false
  }

  private def parseClass(typ: MSILType) {

    clrTypes.types(clazz) = typ
    clrTypes.sym2type(typ) = clazz

    if (typ.IsDefined(clrTypes.SCALA_SYMTAB_ATTR, false)) {
      val attrs = typ.GetCustomAttributes(clrTypes.SCALA_SYMTAB_ATTR, false);
      assert (attrs.length == 1, attrs.length);
      val a = attrs(0).asInstanceOf[MSILAttribute];
      assert (a.getConstructor() == clrTypes.SYMTAB_CONSTR);
      val symtab = a.getConstructorArguments()(0).asInstanceOf[Array[Byte]]
      unpickler.unpickle(symtab, 0, clazz.asInstanceOf, staticModule.asInstanceOf, typ.FullName);
      val mClass = clrTypes.getType(typ.FullName + "$");
      if (mClass != null) {
        clrTypes.types(statics) = mClass;
        val moduleInstance = mClass.GetField("MODULE$");
        assert (moduleInstance != null, mClass);
        clrTypes.fields(statics) = moduleInstance;
      }
      return
    }
    val flags = translateAttributes(typ)
    val ifaces: Array[MSILType] = typ.getInterfaces()
    val superType = if (typ.BaseType() != null) getCLRType(typ.BaseType())
                    else  if (typ.IsInterface()) definitions.ObjectClass.tpe
                    else definitions.AnyClass.tpe; // this is System.Object
    val parents = superType :: ifaces.map(getCLRType).toList
    instanceDefs = newClassScope
    staticDefs = newClassScope

    val classInfo = ClassInfoType(parents, instanceDefs, clazz)
    val staticInfo = ClassInfoType(List(), staticDefs, statics)

    clazz.setFlag(flags)
    clazz.setInfo(classInfo)
    statics.setFlag(Flags.JAVA)
    statics.setInfo(staticInfo)
    staticModule.setFlag(Flags.JAVA)
    staticModule.setInfo(statics.tpe)

    // import nested types<commit_msg>Fixing my asInstanceOf hack in TypeParser.scala...<commit_after>/* NSC -- new scala compiler
 * Copyright 2004-2007 LAMP/EPFL
 */

// $Id$

package scala.tools.nsc.symtab.clr

import java.io.IOException

import ch.epfl.lamp.compiler.msil.{Type => MSILType, Attribute => MSILAttribute, _}

import scala.collection.mutable.{HashMap, HashSet}
import scala.tools.nsc.util.{Position, NoPosition}
import classfile.UnPickler

/**
 *  @author <NAME>
 */
abstract class TypeParser {

  val global: Global
  import global.loaders.clrTypes
  import clrTypes.global._

  //##########################################################################

  private var clazz: Symbol = _
  private var instanceDefs: Scope = _   // was members
  private var staticModule: Symbol = _  // was staticsClass
  private var staticDefs: Scope = _     // was statics

  protected def statics: Symbol = staticModule.moduleClass

  protected var busy: Boolean = false       // lock to detect recursive reads

  private object unpickler extends UnPickler {
    val global: TypeParser.this.global.type = TypeParser.this.global
  }

  def parse(typ: MSILType, root: Symbol) {

    def handleError(e: Exception) = {
      if (settings.debug.value) e.printStackTrace()  //debug
      throw new IOException("type '" + typ.FullName + "' is broken\n(" + e.getMessage() + ")")
    }
    assert(!busy)
    busy = true

    if (root.isModule) {
      this.clazz = root.linkedClassOfModule
      this.staticModule = root
    } else {
      this.clazz = root
      this.staticModule = root.linkedModuleOfClass
    }
    try {
      parseClass(typ)
    } catch {
      case e: FatalError => handleError(e)
      case e: RuntimeException => handleError(e)
    }
    busy = false
  }

  private def parseClass(typ: MSILType) {

    clrTypes.types(clazz) = typ
    clrTypes.sym2type(typ) = clazz

    if (typ.IsDefined(clrTypes.SCALA_SYMTAB_ATTR, false)) {
      val attrs = typ.GetCustomAttributes(clrTypes.SCALA_SYMTAB_ATTR, false);
      assert (attrs.length == 1, attrs.length);
      val a = attrs(0).asInstanceOf[MSILAttribute];
      assert (a.getConstructor() == clrTypes.SYMTAB_CONSTR);
      val symtab = a.getConstructorArguments()(0).asInstanceOf[Array[Byte]]
      unpickler.unpickle(symtab, 0, clazz.asInstanceOf[unpickler.global.Symbol],
                                    staticModule.asInstanceOf[unpickler.global.Symbol], typ.FullName);
      val mClass = clrTypes.getType(typ.FullName + "$");
      if (mClass != null) {
        clrTypes.types(statics) = mClass;
        val moduleInstance = mClass.GetField("MODULE$");
        assert (moduleInstance != null, mClass);
        clrTypes.fields(statics) = moduleInstance;
      }
      return
    }
    val flags = translateAttributes(typ)
    val ifaces: Array[MSILType] = typ.getInterfaces()
    val superType = if (typ.BaseType() != null) getCLRType(typ.BaseType())
                    else  if (typ.IsInterface()) definitions.ObjectClass.tpe
                    else definitions.AnyClass.tpe; // this is System.Object
    val parents = superType :: ifaces.map(getCLRType).toList
    instanceDefs = newClassScope(clazz)
    staticDefs = newClassScope(staticModule)

    val classInfo = ClassInfoType(parents, instanceDefs, clazz)
    val staticInfo = ClassInfoType(List(), staticDefs, statics)

    clazz.setFlag(flags)
    clazz.setInfo(classInfo)
    statics.setFlag(Flags.JAVA)
    statics.setInfo(staticInfo)
    staticModule.setFlag(Flags.JAVA)
    staticModule.setInfo(statics.tpe)

    // import nested types
{'fraction_non_alphanumeric': 0.1306605134121719, 'fraction_numerical': 0.0037496394577444477, 'mean_word_length': 3.157673860911271, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1214306', 'n_tokens_mistral': 2216, 'n_tokens_neox': 2167, 'n_words': 464}