|
| 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 | +} |
0 commit comments