Inline-only in generated code

This commit is contained in:
Ilya Gorbunov
2016-01-27 21:27:54 +03:00
parent 8c0008aa2e
commit c7bd70732c
16 changed files with 350 additions and 175 deletions
@@ -95,6 +95,7 @@ fun aggregates(): List<GenericFunction> {
return count
"""
}
inline(CharSequences, Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) { Inline.Only }
doc(CharSequences) { "Returns the length of this char sequence."}
body(CharSequences) {
"return length"
@@ -315,6 +316,7 @@ fun aggregates(): List<GenericFunction> {
return maxElem
"""
}
inline(Maps) { Inline.Only }
body(Maps) { "return entries.maxBy(selector)" }
}
@@ -346,6 +348,7 @@ fun aggregates(): List<GenericFunction> {
return max
"""
}
inline(Maps) { Inline.Only }
body(Maps) { "return entries.maxWith(comparator)" }
}
@@ -6,6 +6,7 @@ fun arrays(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("isEmpty()") {
inline(Inline.Only)
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns `true` if the array is empty." }
returns("Boolean")
@@ -15,6 +16,7 @@ fun arrays(): List<GenericFunction> {
}
templates add f("isNotEmpty()") {
inline(Inline.Only)
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns `true` if the array is not empty." }
returns("Boolean")
@@ -204,6 +204,7 @@ fun elements(): List<GenericFunction> {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") }
"""
}
inline(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { Inline.Only }
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
"""
return get(index)
@@ -244,7 +245,7 @@ fun elements(): List<GenericFunction> {
return defaultValue(index)
"""
}
inline(true, CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
inline(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { Inline.Only }
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
"""
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
@@ -255,7 +256,7 @@ fun elements(): List<GenericFunction> {
templates add f("getOrElse(index: Int, defaultValue: (Int) -> T)") {
doc { f -> "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." }
returns("T")
inline(true)
inline(Inline.Only)
only(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
body {
"""
@@ -298,9 +299,10 @@ fun elements(): List<GenericFunction> {
return null
"""
}
inline(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { Inline.Only }
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
"""
return if (index >= 0 && index <= lastIndex) get(index) else null
return this.getOrNull(index)
"""
}
}
@@ -422,7 +424,7 @@ fun elements(): List<GenericFunction> {
}
templates add f("find(predicate: (T) -> Boolean)") {
inline(true)
inline(Inline.Only)
include(CharSequences)
doc { f -> "Returns the first ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
returns("T?")
@@ -590,7 +592,7 @@ fun elements(): List<GenericFunction> {
}
templates add f("findLast(predicate: (T) -> Boolean)") {
inline(true)
inline(Inline.Only)
include(Lists, CharSequences)
doc { f -> "Returns the last ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
returns("T?")
@@ -740,8 +742,7 @@ fun elements(): List<GenericFunction> {
templates addAll (1..5).map { n ->
f("component$n()") {
operator(true)
inline(true)
annotations("""@Suppress("NOTHING_TO_INLINE")""")
inline(Inline.Only)
fun getOrdinal(n: Int) = n.toString() + when (n) {
1 -> "st"
2 -> "nd"
@@ -542,7 +542,7 @@ fun filtering(): List<GenericFunction> {
doc(CharSequences, Strings) { f -> "Returns a ${f.collection} containing ${f.element.pluralize()} of the original ${f.collection} at specified [indices]." }
returns(CharSequences, Strings) { "SELF" }
body(CharSequences, Strings) { f ->
body(CharSequences) {
"""
val size = indices.collectionSizeOrDefault(10)
if (size == 0) return ""
@@ -550,9 +550,13 @@ fun filtering(): List<GenericFunction> {
for (i in indices) {
result.append(get(i))
}
return result${toResult(f)}
return result
"""
}
inline(Strings) { Inline.Only }
body(Strings) {
"return (this as CharSequence).slice(indices).toString()"
}
}
templates add f("slice(indices: IntRange)") {
@@ -6,6 +6,8 @@ fun generators(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("plusElement(element: T)") {
inline(Inline.Only)
only(Iterables, Collections, Sets, Sequences)
doc { "Returns a list containing all elements of the original collection and then the given [element]." }
doc(Sets) { "Returns a set containing all elements of the original set and then the given [element]." }
@@ -218,6 +220,8 @@ fun generators(): List<GenericFunction> {
}
templates add f("minusElement(element: T)") {
inline(Inline.Only)
only(Iterables, Sets, Sequences)
doc { "Returns a list containing all elements of the original collection without the first occurrence of the given [element]." }
doc(Sets) { "Returns a set containing all elements of the original set except the given [element]." }
@@ -49,11 +49,15 @@ fun ordering(): List<GenericFunction> {
doc(CharSequences, Strings) { f -> "Returns a ${f.collection} with characters in reversed order." }
returns(CharSequences, Strings) { "SELF" }
body(CharSequences, Strings) { f ->
body(CharSequences) { f ->
"""
return StringBuilder(this).reverse()${ if (f == Strings) ".toString()" else "" }
return StringBuilder(this).reverse()
"""
}
inline(Strings) { Inline.Only }
body(Strings) {
"return (this as CharSequence).reversed().toString()"
}
exclude(Sequences)
}
@@ -21,6 +21,9 @@ fun sequences(): List<GenericFunction> {
}
"""
}
inline(Iterables, Maps) { Inline.Only }
doc(Iterables) { "Returns this collection as an [Iterable]." }
body(Iterables) { "return this" }
body(Maps) { "return entries" }
@@ -59,6 +62,7 @@ fun sequences(): List<GenericFunction> {
}
doc(Sequences) { "Returns this sequence as a [Sequence]."}
inline(Sequences) { Inline.Only }
body(Sequences) { "return this" }
}
@@ -6,6 +6,8 @@ fun specialJVM(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("plusElement(element: T)") {
inline(Inline.Only)
only(InvariantArraysOfObjects)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then the given [element]." }
@@ -64,6 +66,8 @@ fun specialJVM(): List<GenericFunction> {
templates add f("copyOfRange(fromIndex: Int, toIndex: Int)") {
inline(Inline.Only)
only(InvariantArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of range of original array." }
returns("SELF")
@@ -73,6 +77,8 @@ fun specialJVM(): List<GenericFunction> {
}
templates add f("copyOf()") {
inline(Inline.Only)
only(InvariantArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of the original array." }
returns("SELF")
@@ -83,6 +89,8 @@ fun specialJVM(): List<GenericFunction> {
// This overload can cause nulls if array size is expanding, hence different return overload
templates add f("copyOf(newSize: Int)") {
inline(Inline.Only)
only(InvariantArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of the original array." }
returns("SELF")