@@ -2,34 +2,60 @@ package hello
2
2
3
3
import com.intellij.openapi.util.Disposer
4
4
import com.intellij.psi.search.GlobalSearchScope
5
+ import org.jetbrains.kotlin.analyzer.ModuleContent
6
+ import org.jetbrains.kotlin.analyzer.ModuleInfo
7
+ import org.jetbrains.kotlin.analyzer.PlatformAnalysisParameters
8
+ import org.jetbrains.kotlin.analyzer.common.DefaultAnalyzerFacade
5
9
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
6
- import org.jetbrains.kotlin.cli.common.messages.*
7
- import org.jetbrains.kotlin.cli.jvm.compiler.*
8
- import org.jetbrains.kotlin.cli.jvm.config.JVMConfigurationKeys
10
+ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
11
+ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
12
+ import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector
13
+ import org.jetbrains.kotlin.cli.common.messages.MessageCollector
14
+ import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
15
+ import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider
16
+ import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
9
17
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
10
- import org.jetbrains.kotlin.cli.jvm.config.getModuleName
11
18
import org.jetbrains.kotlin.config.CompilerConfiguration
12
19
import org.jetbrains.kotlin.config.addKotlinSourceRoot
13
- import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
14
- import org.jetbrains.kotlin.frontend.java.di.createContainerForTopDownAnalyzerForJvm
15
- import org.jetbrains.kotlin.incremental.components.LookupTracker
20
+ import org.jetbrains.kotlin.container.get
21
+ import org.jetbrains.kotlin.context.ProjectContext
22
+ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
16
23
import org.jetbrains.kotlin.load.java.JvmAbi
24
+ import org.jetbrains.kotlin.name.Name
17
25
import org.jetbrains.kotlin.psi.KtBlockExpression
18
- import org.jetbrains.kotlin.resolve.AnalyzerScriptParameter
26
+ import org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer
27
+ import org.jetbrains.kotlin.resolve.MultiTargetPlatform
19
28
import org.jetbrains.kotlin.resolve.TopDownAnalysisContext
20
29
import org.jetbrains.kotlin.resolve.TopDownAnalysisMode
21
- import org.jetbrains.kotlin.resolve.jvm.JvmAnalyzerFacade
22
- import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM
23
- import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
24
30
import org.jetbrains.kotlin.utils.PathUtil
25
31
import java.io.File
26
32
import java.util.*
27
33
import java.util.logging.Logger
28
34
29
35
class KotlinScriptParser {
36
+ private class SourceModuleInfo (
37
+ override val name : Name ,
38
+ override val capabilities : Map <ModuleDescriptor .Capability <* >, Any? >,
39
+ private val dependOnOldBuiltIns : Boolean
40
+ ) : ModuleInfo {
41
+ override fun dependencies () = listOf (this )
42
+
43
+ override fun dependencyOnBuiltIns (): ModuleInfo .DependencyOnBuiltIns =
44
+ if (dependOnOldBuiltIns) ModuleInfo .DependenciesOnBuiltIns .LAST else ModuleInfo .DependenciesOnBuiltIns .NONE
45
+ }
46
+
30
47
companion object {
31
- private val LOG = Logger .getLogger(KotlinScriptParser .javaClass .name)
48
+ private val LOG = Logger .getLogger(KotlinScriptParser :: class .java .name)
32
49
private val messageCollector = object : MessageCollector {
50
+ private var hasErrors = false
51
+ override fun clear () {
52
+ hasErrors = false
53
+ }
54
+
55
+ override fun hasErrors (): Boolean {
56
+ return hasErrors
57
+ }
58
+
33
59
override fun report (severity : CompilerMessageSeverity , message : String , location : CompilerMessageLocation ) {
34
60
val path = location.path
35
61
val position = if (path == null ) " " else " $path : (${location.line} , ${location.column} ) "
@@ -40,6 +66,7 @@ class KotlinScriptParser {
40
66
LOG .finest(text)
41
67
} else if (CompilerMessageSeverity .ERRORS .contains(severity)) {
42
68
LOG .severe(text)
69
+ hasErrors = true
43
70
} else if (severity == CompilerMessageSeverity .INFO ) {
44
71
LOG .info(text)
45
72
} else {
@@ -60,7 +87,7 @@ class KotlinScriptParser {
60
87
val configuration = CompilerConfiguration ()
61
88
62
89
val groupingCollector = GroupingMessageCollector (messageCollector)
63
- val severityCollector = MessageSeverityCollector (groupingCollector)
90
+ val severityCollector = GroupingMessageCollector (groupingCollector)
64
91
configuration.put(CLIConfigurationKeys .MESSAGE_COLLECTOR_KEY , severityCollector)
65
92
66
93
@@ -69,38 +96,33 @@ class KotlinScriptParser {
69
96
files.forEach { configuration.addKotlinSourceRoot(it) }
70
97
// Configuring Kotlin class path
71
98
configuration.addJvmClasspathRoots(classPath)
72
- configuration.put(JVMConfigurationKeys .MODULE_NAME , JvmAbi .DEFAULT_MODULE_NAME )
73
- configuration.put<List <AnalyzerScriptParameter >>(JVMConfigurationKeys .SCRIPT_PARAMETERS , CommandLineScriptUtils .scriptParameters())
74
99
75
100
val rootDisposable = Disposer .newDisposable()
76
101
try {
77
102
val environment = KotlinCoreEnvironment .createForProduction(rootDisposable, configuration, EnvironmentConfigFiles .JVM_CONFIG_FILES )
78
103
val ktFiles = environment.getSourceFiles()
79
- val sharedTrace = CliLightClassGenerationSupport .NoScopeRecordCliBindingTrace ()
80
- val moduleContext = TopDownAnalyzerFacadeForJVM .createContextWithSealedModule(environment.project,
81
- environment.getModuleName())
82
-
83
- val project = moduleContext.project
84
- val allFiles = JvmAnalyzerFacade .getAllFilesToAnalyze(project, null , ktFiles)
85
- val providerFactory = FileBasedDeclarationProviderFactory (moduleContext.storageManager, allFiles)
86
- val lookupTracker = LookupTracker .DO_NOTHING
87
- val packagePartProvider = JvmPackagePartProvider (environment)
88
- val container = createContainerForTopDownAnalyzerForJvm(
89
- moduleContext,
90
- sharedTrace,
91
- providerFactory,
92
- GlobalSearchScope .allScope(project),
93
- lookupTracker,
94
- packagePartProvider)
95
-
96
- val additionalProviders = ArrayList <PackageFragmentProvider >()
97
-
98
- additionalProviders.add(container.javaDescriptorResolver.packageFragmentProvider)
99
-
100
- return container.lazyTopDownAnalyzerForTopLevel.analyzeFiles(TopDownAnalysisMode .LocalDeclarations , allFiles, additionalProviders)
104
+
105
+ val capabilities: Map <ModuleDescriptor .Capability <* >, Any? > = mapOf (MultiTargetPlatform .CAPABILITY to MultiTargetPlatform .Common )
106
+
107
+ val moduleInfo = SourceModuleInfo (Name .special(" <${JvmAbi .DEFAULT_MODULE_NAME } " ), capabilities, false )
108
+ val project = ktFiles.firstOrNull()?.project ? : throw AssertionError (" No files to analyze" )
109
+ val resolver = DefaultAnalyzerFacade .setupResolverForProject(
110
+ " sources for metadata serializer" ,
111
+ ProjectContext (project), listOf (moduleInfo),
112
+ { ModuleContent (ktFiles, GlobalSearchScope .allScope(project)) },
113
+ object : PlatformAnalysisParameters {},
114
+ packagePartProviderFactory = {
115
+ _, content -> JvmPackagePartProvider (environment, content.moduleContentScope)
116
+ },
117
+ modulePlatforms = { MultiTargetPlatform .Common }
118
+ )
119
+
120
+ val container = resolver.resolverForModule(moduleInfo).componentProvider
121
+
122
+ return container.get<LazyTopDownAnalyzer >().analyzeDeclarations(TopDownAnalysisMode .TopLevelDeclarations , ktFiles)
101
123
} finally {
102
124
rootDisposable.dispose()
103
- if (severityCollector.anyReported( CompilerMessageSeverity . ERROR )) {
125
+ if (severityCollector.hasErrors( )) {
104
126
throw RuntimeException (" Compilation error" )
105
127
}
106
128
}
0 commit comments