aboutsummaryrefslogtreecommitdiff
path: root/src/Analysis/CompilerAPI.kt
diff options
context:
space:
mode:
authorIlya Ryzhenkov <orangy@jetbrains.com>2014-07-11 23:47:29 +0400
committerIlya Ryzhenkov <orangy@jetbrains.com>2014-07-11 23:47:29 +0400
commit1fc5f504c77e98f80c7f6921e26ca7c9463c8bf3 (patch)
tree3b55cb9949db0e432051401ba76526494ef95c1a /src/Analysis/CompilerAPI.kt
parentcec0fc9f3eaa5df8fc2654ce90d87b9be48b16c0 (diff)
downloaddokka-1fc5f504c77e98f80c7f6921e26ca7c9463c8bf3.tar.gz
dokka-1fc5f504c77e98f80c7f6921e26ca7c9463c8bf3.tar.bz2
dokka-1fc5f504c77e98f80c7f6921e26ca7c9463c8bf3.zip
Create resolution scopes for all entities to resolve names inside doc-comments.
Diffstat (limited to 'src/Analysis/CompilerAPI.kt')
-rw-r--r--src/Analysis/CompilerAPI.kt57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Analysis/CompilerAPI.kt b/src/Analysis/CompilerAPI.kt
index bf77a7d4..039ad799 100644
--- a/src/Analysis/CompilerAPI.kt
+++ b/src/Analysis/CompilerAPI.kt
@@ -13,6 +13,11 @@ import org.jetbrains.jet.lang.resolve.*
import org.jetbrains.jet.lang.psi.*
import org.jetbrains.jet.analyzer.*
import org.jetbrains.jet.lang.descriptors.*
+import org.jetbrains.jet.lang.resolve.scopes.JetScope
+import com.intellij.psi.impl.source.tree.LeafPsiElement
+import org.jetbrains.jet.lang.resolve.scopes.WritableScope
+import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl
+import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler
private fun getAnnotationsPath(paths: KotlinPaths, arguments: K2JVMCompilerArguments): MutableList<File> {
val annotationsPath = arrayListOf<File>()
@@ -60,3 +65,55 @@ fun DeclarationDescriptor.isUserCode() =
is CallableMemberDescriptor -> getKind() == CallableMemberDescriptor.Kind.DECLARATION
else -> true
}
+
+public fun getFunctionInnerScope(outerScope: JetScope, descriptor: FunctionDescriptor): JetScope {
+ val redeclarationHandler = object : RedeclarationHandler {
+ override fun handleRedeclaration(first: DeclarationDescriptor, second: DeclarationDescriptor) {
+ // TODO: check if we can ignore redeclarations
+ }
+ }
+
+ val parameterScope = WritableScopeImpl(outerScope, descriptor, redeclarationHandler, "Function KDoc scope")
+ val receiver = descriptor.getReceiverParameter()
+ if (receiver != null) {
+ parameterScope.setImplicitReceiver(receiver)
+ }
+ for (typeParameter in descriptor.getTypeParameters()) {
+ parameterScope.addTypeParameterDescriptor(typeParameter)
+ }
+ for (valueParameterDescriptor in descriptor.getValueParameters()) {
+ parameterScope.addVariableDescriptor(valueParameterDescriptor)
+ }
+ parameterScope.addLabeledDeclaration(descriptor)
+ parameterScope.changeLockLevel(WritableScope.LockLevel.READING)
+ return parameterScope
+}
+
+fun BindingContext.getResolutionScope(descriptor: DeclarationDescriptor): JetScope {
+ when (descriptor) {
+ is PackageFragmentDescriptor -> return descriptor.getMemberScope()
+ is PackageViewDescriptor -> return descriptor.getMemberScope()
+
+ is ClassDescriptor -> return descriptor.getUnsubstitutedInnerClassesScope()
+ is PropertyAccessorDescriptor -> return getResolutionScope(descriptor.getCorrespondingProperty())
+ is FunctionDescriptor -> {
+ val container = getFunctionInnerScope(getResolutionScope(descriptor.getContainingDeclaration()), descriptor)
+ if (container is JetFunction)
+ return getFunctionBodyScope(container)
+ }
+ }
+
+ if (descriptor is DeclarationDescriptorNonRoot)
+ return getResolutionScope(descriptor.getContainingDeclaration())
+
+ throw IllegalArgumentException("Cannot find resolution scope for root $descriptor")
+}
+
+fun BindingContext.getFunctionBodyScope(element: JetFunction): JetScope {
+ val body = element.getBodyExpression()
+ val scope = get(BindingContext.RESOLUTION_SCOPE, body)
+ if (scope != null)
+ return scope
+ throw IllegalArgumentException("Cannot find resolution scope for function $element")
+}
+