xref: /aosp_15_r20/external/ksp/api/src/main/kotlin/com/google/devtools/ksp/symbol/KSFunctionDeclaration.kt (revision af87fb4bb8e3042070d2a054e912924f599b22b7)
1 /*
2  * Copyright 2020 Google LLC
3  * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.google.devtools.ksp.symbol
18 
19 import com.google.devtools.ksp.processing.Resolver
20 /**
21  * A function definition
22  *
23  * Dispatch receiver can be obtained through [parentDeclaration].
24  *
25  * To obtain the function signature where type arguments are resolved as member of a given [KSType],
26  * use [Resolver.asMemberOf].
27  *
28  * @see KSFunctionType
29  */
30 interface KSFunctionDeclaration : KSDeclaration, KSDeclarationContainer {
31     /**
32      * Kind of this function.
33      */
34     val functionKind: FunctionKind
35 
36     /**
37      * Whether this function is abstract.
38      */
39     val isAbstract: Boolean
40 
41     /**
42      * Extension receiver of this function
43      * @see [https://kotlinlang.org/docs/reference/extensions.html#extension-functions]
44      */
45     val extensionReceiver: KSTypeReference?
46 
47     /**
48      * Return type of this function.
49      * Can be null if an error occurred during resolution.
50      */
51     val returnType: KSTypeReference?
52 
53     /**
54      * [value parameters][KSValueParameter] of this function.
55      */
56     val parameters: List<KSValueParameter>
57 
58     /**
59      * Find the closest overridee of this function, if overriding.
60      *
61      * For the following input:
62      * ```
63      * abstract class A {
64      *   open fun x() {}
65      *   open fun y() {}
66      * }
67      * abstract class B : A() {
68      *   override open fun x() {}
69      * }
70      * abstract class C : B() {
71      *   override open fun x() {}
72      *   override open fun y() {}
73      * }
74      * ```
75      * Calling `findOverridee` on `C.x` will return `B.x`.
76      * Calling `findOverridee` on `C.y` will return `A.y`.
77      *
78      * When there are multiple super interfaces implementing the function, the closest declaration
79      * to the current containing declaration is selected. If they are in the same level, the
80      * function of the first specified interface (in source) will be returned.
81      *
82      * @return [KSDeclaration] for the original declaration, if overriding, otherwise null.
83      * Calling [findOverridee] is expensive and should be avoided if possible.
84      */
findOverrideenull85     fun findOverridee(): KSDeclaration?
86 
87     /**
88      * Returns the type of the [function] when it is viewed as member of the [containing] type.
89      *
90      * For instance, for the following input:
91      * ```
92      * interface Base<T> {
93      *   fun f(t:T?):T
94      * }
95      * val foo: Base<Int>
96      * val bar: Base<String>
97      * ```
98      * When `f()` is viewed as member of `foo`, this method will return a [KSFunction] where
99      * the [KSFunction.returnType] is `Int` and the parameter `t` is of type `Int?`.
100      * When `f()` is viewed as member of `bar`, this method will return a [KSFunction]
101      * where the [KSFunction.returnType] is `String` and the parameter `t` is of type `String?`.
102      *
103      * If the function has type parameters, they'll not be resolved and can be read from
104      * [KSFunction.typeParameters].
105      *
106      * If the substitution fails (e.g. if [containing] is an error type, a [KSFunction] with [KSFunction.isError] `true`
107      * is returned.
108      *
109      * @param containing The type that contains [function].
110      * @throws IllegalArgumentException Throws [IllegalArgumentException] when [containing] does not contain [function]
111      * or if the [function] is not declared in a class, object or interface.
112      */
113     fun asMemberOf(containing: KSType): KSFunction
114 }
115