Skip to content

Commit e59a950

Browse files
Add documentation to MainProxies
1 parent 1d9e8cb commit e59a950

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

compiler/src/dotty/tools/dotc/ast/MainProxies.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ object MainProxies {
5050
def mainProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
5151
import tpd._
5252

53+
/**
54+
* Compute the default values of the function. Since they cannot be infered anymore at this point
55+
* of the compilation, they must be explicitely passed by [[mainProxy]].
56+
*/
5357
def defaultValues(scope: Tree, funSymbol: Symbol): Map[Int, Tree] =
5458
scope match {
5559
case TypeDef(_, template: Template) =>
@@ -63,6 +67,7 @@ object MainProxies {
6367
case _ => Map[Int, Tree]()
6468
}
6569

70+
/** Computes the list of main methods present in the code. */
6671
def mainMethods(scope: Tree, stats: List[Tree]): List[(Symbol, Vector[Option[Annotation]], Map[Int, Tree], Option[Comment])] = stats.flatMap {
6772
case stat: DefDef =>
6873
val sym = stat.symbol
@@ -105,6 +110,12 @@ object MainProxies {
105110

106111
inline def some(value: Tree): Tree = Apply(ref(defn.SomeClass.companionModule.termRef), value)
107112

113+
/**
114+
* Creates a list of references and definitions of arguments, the first referencing the second.
115+
* The goal is to create the
116+
* `val arg0: () => S = ...`
117+
* part of the code. The first element of the tuple is a ref to `arg0`, the second is the whole definition.
118+
*/
108119
def createArgs(mt: MethodType, cmdName: TermName): List[(Tree, ValDef)] =
109120
mt.paramInfos.zip(mt.paramNames).zipWithIndex.map {
110121
case ((formal, paramName), n) =>
@@ -125,15 +136,24 @@ object MainProxies {
125136
else
126137
defn.MainAnnotCommand_argGetter
127138

139+
// The ParameterInfos to be passed to the arg getter
128140
val parameterInfos = {
129141
val param = paramName.toString
130142
val paramInfosName = argName ++ "paramInfos"
131143
val paramInfosIdent = Ident(paramInfosName)
132144
val paramInfosTree = New(
133145
AppliedTypeTree(TypeTree(defn.MainAnnotParameterInfos.typeRef), List(TypeTree(formalType))),
146+
// Arguments to be passed to ParameterInfos' constructor
134147
List(List(lit(param), lit(formalType.show)))
135148
)
136149

150+
/*
151+
* Assignations to be made after the creation of the ParameterInfos.
152+
* For example:
153+
* args0paramInfos.documentation = Some("my param x")
154+
* is represented by the pair
155+
* ("documentation", some(lit("my param x")))
156+
*/
137157
var assignations: List[(String, Tree)] = Nil
138158
for (dv <- defaultValue)
139159
assignations = ("defaultValue" -> some(dv)) :: assignations
@@ -163,6 +183,7 @@ object MainProxies {
163183
}
164184
end createArgs
165185

186+
/** Turns an annotation (e.g. `@main(40)`) into an instance of the class (e.g. `new scala.main(40)`). */
166187
def instanciateAnnotation(annot: Annotation): Tree =
167188
val argss = {
168189
def recurse(t: Tree, acc: List[List[Tree]]): List[List[Tree]] = t match {
@@ -245,12 +266,13 @@ object MainProxies {
245266
result
246267
}
247268

269+
/** A class responsible for extracting the docstrings of a method. */
248270
private class Documentation(docComment: Option[Comment]):
249271
import util.CommentParsing._
250272

251273
/** The main part of the documentation. */
252274
lazy val mainDoc: String = _mainDoc
253-
/** The parameters identified by @param. Maps from param name to documentation. */
275+
/** The parameters identified by @param. Maps from parameter name to its documentation. */
254276
lazy val argDocs: Map[String, String] = _argDocs
255277

256278
private var _mainDoc: String = ""

library/src/scala/main.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ package scala
1111
import collection.mutable
1212
import annotation._
1313

14-
/** An annotation that designates a main function
15-
*/
14+
/**
15+
* An annotation that designates a main function.
16+
* @param maxLineLength the maximum number of characters to print on a single line when
17+
* displaying the help
18+
*/
1619
final class main(maxLineLength: Int) extends MainAnnotation:
1720
self =>
1821
import main._
1922
import MainAnnotation._
2023

24+
/** An annotation that designates a main function. */
2125
def this() = this(120)
2226

2327
override type ArgumentParser[T] = util.CommandLineParser.FromString[T]
@@ -145,15 +149,12 @@ final class main(maxLineLength: Int) extends MainAnnotation:
145149
}
146150

147151
private def indicesOfArg(argName: String, shortArgName: Option[Char]): Seq[Int] =
148-
def allIndicesOf(s: String): Seq[Int] =
149-
def recurse(s: String, from: Int): Seq[Int] =
150-
val i = args.indexOf(s, from)
151-
if i < 0 then Seq() else i +: recurse(s, i + 1)
152+
def allIndicesOf(s: String, from: Int): Seq[Int] =
153+
val i = args.indexOf(s, from)
154+
if i < 0 then Seq() else i +: allIndicesOf(s, i + 1)
152155

153-
recurse(s, 0)
154-
155-
val indices = allIndicesOf(s"--$argName")
156-
val indicesShort = shortArgName.map(shortName => allIndicesOf(s"-$shortName")).getOrElse(Seq())
156+
val indices = allIndicesOf(s"--$argName", 0)
157+
val indicesShort = shortArgName.map(shortName => allIndicesOf(s"-$shortName", 0)).getOrElse(Seq())
157158
(indices ++: indicesShort).filter(_ >= 0)
158159

159160
private def getArgGetter[T](paramInfos: ParameterInfos[_], getDefaultGetter: () => () => T)(using p: ArgumentParser[T]): () => T =
@@ -189,7 +190,7 @@ final class main(maxLineLength: Int) extends MainAnnotation:
189190
argKinds += argKind
190191

191192
val shortName = getShortName(paramInfos)
192-
if shortName.exists(c => !shortNameIsValid(c)) then throw IllegalArgumentException(s"Invalid short name: -${shortName.get}")
193+
shortName.foreach(c => if !shortNameIsValid(c) then throw IllegalArgumentException(s"Invalid short name: -$c"))
193194
argShortNames += shortName
194195

195196
override def argGetter[T](paramInfos: ParameterInfos[T])(using p: ArgumentParser[T]): () => T =

0 commit comments

Comments
 (0)