Skip to content

Commit 98a7e90

Browse files
committed
Inkuire MVP
1 parent 782f57e commit 98a7e90

File tree

7 files changed

+529
-1
lines changed

7 files changed

+529
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tests.inkuire
2+
3+
trait InType1
4+
trait InType2 extends InType1
5+
6+
trait OutType1
7+
trait OutType2 extends OutType1
8+
9+
class JustAClass {
10+
def mathod(l: InType1): OutType1 = ???
11+
}
12+
13+
class JustAnotherClass extends JustAClass {
14+
def method(i: InType2): OutType2 = ???
15+
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package dotty.tools.scaladoc
2+
3+
import dotty.tools.scaladoc.util._
4+
5+
object Inkuire {
6+
7+
var db = InkuireDb(Seq.empty, Map.empty)
8+
9+
case class InkuireDb(
10+
functions: Seq[ExternalSignature],
11+
types: Map[ITID, (Type, Seq[Type])]
12+
)
13+
14+
case class ITID(uuid: String, isParsed: Boolean)
15+
16+
case class Signature(
17+
receiver: Option[Contravariance],
18+
arguments: Seq[Contravariance],
19+
result: Covariance,
20+
context: SignatureContext
21+
) {
22+
def typesWithVariances: Seq[Variance] = receiver.toSeq ++ arguments ++ Seq(result)
23+
}
24+
25+
object Signature {
26+
def apply(receiver: Option[Type], arguments: Seq[Type], result: Type, context: SignatureContext): Signature =
27+
Signature(receiver.map(Contravariance(_)), arguments.map(Contravariance(_)), Covariance(result), context)
28+
}
29+
30+
case class ExternalSignature(
31+
signature: Signature,
32+
name: String,
33+
packageName: String,
34+
uri: String
35+
)
36+
37+
case class Type(
38+
name: TypeName,
39+
params: Seq[Variance] = Seq.empty,
40+
nullable: Boolean = false,
41+
itid: Option[ITID] = None,
42+
isVariable: Boolean = false,
43+
isStarProjection: Boolean = false,
44+
isUnresolved: Boolean = false
45+
)
46+
47+
case class TypeName(name: String) {
48+
override def hashCode(): Int = name.toLowerCase.hashCode
49+
50+
override def equals(obj: Any): Boolean = {
51+
obj match {
52+
case o: TypeName => this.name.toLowerCase == o.name.toLowerCase
53+
case _ => false
54+
}
55+
}
56+
57+
override def toString: String = name
58+
}
59+
60+
case class SignatureContext(
61+
vars: Set[String],
62+
constraints: Map[String, Seq[Type]]
63+
) {
64+
override def equals(obj: Any): Boolean =
65+
obj match {
66+
case other: SignatureContext if this.vars.size == other.vars.size => true
67+
case _ => false
68+
}
69+
}
70+
71+
object SignatureContext {
72+
def empty: SignatureContext = SignatureContext(Set.empty, Map.empty)
73+
}
74+
75+
sealed abstract class Variance {
76+
val typ: Type
77+
}
78+
79+
case class Covariance(typ: Type) extends Variance
80+
81+
case class Contravariance(typ: Type) extends Variance
82+
83+
case class Invariance(typ: Type) extends Variance
84+
85+
case class UnresolvedVariance(typ: Type) extends Variance
86+
87+
object EngineModelSerializers {
88+
def serialize(db: InkuireDb): JSON = {
89+
jsonObject(
90+
("types", serialize(db.types)),
91+
("functions", jsonList(db.functions.map(serialize)))
92+
)
93+
}
94+
95+
private def serialize(types: Map[ITID, (Type, Seq[Type])]): JSON = {
96+
jsonObject((
97+
types.toList.map {
98+
case (itid, v) =>
99+
(serializeAsKey(itid), serialize(v))
100+
}
101+
)*)
102+
}
103+
104+
private def serializeAsKey(itid: ITID): String = {
105+
s"""${itid.isParsed}=${itid.uuid}"""
106+
}
107+
108+
private def serialize(v: (Type, Seq[Type])): JSON = {
109+
jsonList(
110+
Seq(
111+
serialize(v._1),
112+
jsonList(v._2.map(serialize))
113+
)
114+
)
115+
}
116+
117+
private def serialize(t: Type): JSON = {
118+
jsonObject(
119+
("name", serialize(t.name)),
120+
("params", jsonList(t.params.map(serialize))),
121+
("nullable", serialize(t.nullable)),
122+
("itid", serialize(t.itid.get)),
123+
("isVariable", serialize(t.isVariable)),
124+
("isStarProjection", serialize(t.isStarProjection)),
125+
("isUnresolved", serialize(t.isUnresolved))
126+
)
127+
}
128+
129+
private def serialize(b: Boolean): JSON = {
130+
if b then rawJSON("true") else rawJSON("false")
131+
}
132+
133+
private def serialize(itid: ITID): JSON = {
134+
jsonObject(
135+
("uuid", serialize(itid.uuid)),
136+
("isParsed", serialize(itid.isParsed))
137+
)
138+
}
139+
140+
private def serialize(s: TypeName): JSON = {
141+
jsonObject(
142+
("name", serialize(s.name))
143+
)
144+
}
145+
146+
private def serialize(v: Variance): JSON = v match {
147+
case _: Invariance =>
148+
jsonObject(
149+
("typ", serialize(v.typ)),
150+
("variancekind", serialize("invariance"))
151+
)
152+
case _: Covariance =>
153+
jsonObject(
154+
("typ", serialize(v.typ)),
155+
("variancekind", serialize("covariance"))
156+
)
157+
case _: Contravariance =>
158+
jsonObject(
159+
("typ", serialize(v.typ)),
160+
("variancekind", serialize("contravariance"))
161+
)
162+
case _: UnresolvedVariance => serialize("KAWABANGA")
163+
}
164+
165+
private def serialize(e: ExternalSignature): JSON = {
166+
jsonObject(
167+
("signature", serialize(e.signature)),
168+
("name", serialize(e.name)),
169+
("packageName", serialize(e.packageName)),
170+
("uri", serialize(e.uri))
171+
)
172+
}
173+
174+
private def serialize(s: String): JSON = {
175+
jsonString(s)
176+
}
177+
178+
private def serialize(s: Signature): JSON = {
179+
jsonObject(
180+
("receiver", serialize(s.receiver)),
181+
("arguments", jsonList(s.arguments.map(serialize))),
182+
("result", serialize(s.result)),
183+
("context", serialize(s.context))
184+
)
185+
}
186+
187+
private def serialize(o: Option[Contravariance]): JSON = {
188+
o.fold(rawJSON("null")) { v =>
189+
serialize(v)
190+
}
191+
}
192+
193+
private def serialize(c: SignatureContext): JSON = {
194+
jsonObject(
195+
("vars", jsonList(c.vars.toSeq.map(serialize))),
196+
("constraints", serializeConstraints(c.constraints))
197+
)
198+
}
199+
200+
private def serializeConstraints(constraints: Map[String, Seq[Type]]): JSON = {
201+
jsonObject((
202+
constraints.toList.map {
203+
case (name, vs) =>
204+
(name, jsonList(vs.map(serialize)))
205+
}
206+
)*)
207+
}
208+
}
209+
210+
}

scaladoc/src/dotty/tools/scaladoc/Scaladoc.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty.tools.scaladoc
22

33
import java.util.ServiceLoader
44
import java.io.File
5+
import java.io.FileWriter
56
import java.util.jar._
67
import collection.JavaConverters._
78
import collection.immutable.ArraySeq
@@ -13,6 +14,9 @@ import dotty.tools.dotc.config.CommonScalaSettings
1314
import dotty.tools.dotc.reporting.Reporter
1415
import dotty.tools.dotc.core.Contexts._
1516

17+
import dotty.tools.scaladoc.Inkuire
18+
import dotty.tools.scaladoc.Inkuire._
19+
1620
object Scaladoc:
1721
enum CommentSyntax:
1822
case Wiki
@@ -74,8 +78,20 @@ object Scaladoc:
7478
else report.error("Failure")
7579

7680
}
81+
82+
println(s"Types: ${Inkuire.db.types.size}")
83+
println(s"Functions: ${Inkuire.db.functions.size}")
84+
dumpDB(Inkuire.db)
85+
7786
ctx.reporter
7887

88+
def dumpDB(db: InkuireDb) = {
89+
val file = new File("/home/kkorban/Inkuire/data/db.json")
90+
file.createNewFile()
91+
val myWriter = new FileWriter("/home/kkorban/Inkuire/data/db.json", false)
92+
myWriter.write(s"${EngineModelSerializers.serialize(db)}")
93+
myWriter.close()
94+
}
7995

8096
def extract(args: Array[String], rootCtx: CompilerContext): (Option[Scaladoc.Args], CompilerContext) =
8197
val newContext = rootCtx.fresh

0 commit comments

Comments
 (0)