Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge the functionality of Descriptors into JavaSignatures. #385

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ private[reader] object ClassfileParser {
allRegisteredSymbols += sym
sym

def loadMembers(): IArray[(TermSymbol, AccessFlags, SigOrDesc)] =
val buf = IArray.newBuilder[(TermSymbol, AccessFlags, SigOrDesc)]
def loadMembers(): IArray[(TermSymbol, AccessFlags, MemberSig)] =
val buf = IArray.newBuilder[(TermSymbol, AccessFlags, MemberSig)]
structure.fields.use {
reader.readFields { (name, sigOrDesc, access) =>
buf += ((createMember(name, EmptyFlagSet, access), access, sigOrDesc))
Expand All @@ -242,7 +242,7 @@ private[reader] object ClassfileParser {
structure.supers.use {
val superClass = reader.readSuperClass().map(binaryName)
val interfaces = reader.readInterfaces().map(binaryName)
Descriptors.parseSupers(cls, superClass, interfaces)
JavaSignatures.parseSupers(cls, superClass, interfaces)
}
end parents
val parents1 =
Expand All @@ -262,10 +262,8 @@ private[reader] object ClassfileParser {
else if cls.isString then rctx.createStringMagicMethods(cls)
else if cls.isJavaEnum then rctx.createEnumMagicMethods(cls)

for (sym, javaFlags, sigOrDesc) <- loadMembers() do
val parsedType = sigOrDesc match
case SigOrDesc.Desc(desc) => Descriptors.parseDescriptor(sym, desc)
case SigOrDesc.Sig(sig) => JavaSignatures.parseSignature(sym, sig, allRegisteredSymbols)
for (sym, javaFlags, memberSig) <- loadMembers() do
val parsedType = JavaSignatures.parseSignature(sym, memberSig, allRegisteredSymbols)
val adaptedType =
if sym.isMethod && sym.name == nme.Constructor then cls.makePolyConstructorType(parsedType)
else if sym.isMethod && javaFlags.isVarargsIfMethod then patchForVarargs(sym, parsedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ private[classfiles] final class ClassfileReader private () {
reader
}

def readFields(op: (SimpleName, SigOrDesc, AccessFlags) => Unit)(using DataStream, ConstantPool): Unit =
def readFields(op: (SimpleName, MemberSig, AccessFlags) => Unit)(using DataStream, ConstantPool): Unit =
readMembers(isMethod = false, op)

def readMethods(op: (SimpleName, SigOrDesc, AccessFlags) => Unit)(using DataStream, ConstantPool): Unit =
def readMethods(op: (SimpleName, MemberSig, AccessFlags) => Unit)(using DataStream, ConstantPool): Unit =
readMembers(isMethod = true, op)

private def readMembers(
isMethod: Boolean,
op: (SimpleName, SigOrDesc, AccessFlags) => Unit
op: (SimpleName, MemberSig, AccessFlags) => Unit
)(using ds: DataStream, pool: ConstantPool): Unit = {
val count = data.readU2()
loop(count) {
Expand All @@ -214,9 +214,7 @@ private[classfiles] final class ClassfileReader private () {
case _ => false
}
val sig = sigOrNull
if !accessFlags.isSynthetic then
if sig == null then op(name, SigOrDesc.Desc(desc), accessFlags)
else op(name, SigOrDesc.Sig(sig), accessFlags)
if !accessFlags.isSynthetic then op(name, if sig == null then desc else sig, accessFlags)
}
}

Expand Down Expand Up @@ -399,9 +397,7 @@ private[classfiles] object ClassfileReader {
IArray.unsafeFromArray(arr)
}

enum SigOrDesc:
case Sig(str: String)
case Desc(str: String)
type MemberSig = String

enum SigOrSupers:
case Sig(str: String)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ import tastyquery.Symbols.*
import tastyquery.reader.ReaderContext
import tastyquery.reader.ReaderContext.rctx

import ClassfileParser.{InnerClasses, Resolver}
import ClassfileParser.{InnerClasses, Resolver, resolver}

private[classfiles] object JavaSignatures:

private object IgnoreTParamRefs

private type JavaSignature = Null | PolyType | Map[TypeName, ClassTypeParamSymbol] | IgnoreTParamRefs.type

private def classRef(binaryName: SimpleName)(using ReaderContext, InnerClasses, Resolver): TypeRef =
resolver.resolve(binaryName)

def parseSupers(cls: ClassSymbol, superClass: Option[SimpleName], interfaces: IArray[SimpleName])(
using ReaderContext,
InnerClasses,
Resolver
): List[Type] =
cls.withTypeParams(Nil)
if cls.isObject then rctx.AnyType :: rctx.MatchableType :: Nil
else
val superRef = superClass.map(classRef).getOrElse(rctx.ObjectType)
superRef :: interfaces.map(classRef).toList
end parseSupers

@throws[ClassfileFormatException]
def parseSignature(member: Symbol { val owner: Symbol }, signature: String, allRegisteredSymbols: Growable[Symbol])(
using ReaderContext,
Expand Down Expand Up @@ -163,7 +178,7 @@ private[classfiles] object JavaSignatures:
end classTypeSignatureRest

if consume('L') then // must have 'L', identifier, and ';'.
val pre = simpleClassTypeSignature(Descriptors.classRef(binaryName()))
val pre = simpleClassTypeSignature(classRef(binaryName()))
Some(classTypeSignatureRest(pre))
else None
end classTypeSignature
Expand Down Expand Up @@ -279,7 +294,7 @@ private[classfiles] object JavaSignatures:
classRest(null)

def fieldSignature: Type =
referenceType(null)
baseType.getOrElse(referenceType(null))

def cookFailure(tname: TypeName, reason: String): Nothing =
val path = if !isClass then s"${member.owner.displayFullName}#${member.name}" else member.displayFullName
Expand Down