Add compiler key to dump arguments
diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt
index 001ee71..34c1526 100644
--- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt
+++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt
@@ -274,6 +274,12 @@
     )
     var checkStickyPhaseConditions: Boolean by FreezableVar(false)
 
+    @Argument(
+        value = "-Xdump-model",
+        description = "Don't even try to use it"
+    )
+    var dumpArgumentsDir: String? by FreezableVar(null)
+
     open fun configureAnalysisFlags(collector: MessageCollector): MutableMap<AnalysisFlag<*>, Any> {
         return HashMap<AnalysisFlag<*>, Any>().apply {
             put(AnalysisFlags.skipMetadataVersionCheck, skipMetadataVersionCheck)
diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/arguments.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/arguments.kt
index 7621128..5c62d6c 100644
--- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/arguments.kt
+++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/arguments.kt
@@ -22,6 +22,7 @@
     createMetadataVersion: ((IntArray) -> BinaryVersion)? = null
 ) {
     put(CommonConfigurationKeys.DISABLE_INLINE, arguments.noInline)
+    putIfNotNull(CommonConfigurationKeys.DUMP_MODEL, arguments.dumpArgumentsDir)
     putIfNotNull(CLIConfigurationKeys.INTELLIJ_PLUGIN_ROOT, arguments.intellijPluginRoot)
     put(CommonConfigurationKeys.REPORT_OUTPUT_FILES, arguments.reportOutputFiles)
 
diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt
index d1979f3..bb9735f 100644
--- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt
+++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt
@@ -24,6 +24,12 @@
 import com.intellij.psi.PsiJavaModule
 import com.intellij.psi.search.DelegatingGlobalSearchScope
 import com.intellij.psi.search.GlobalSearchScope
+import com.intellij.psi.search.ProjectScope
+import org.jdom.Attribute
+import org.jdom.Document
+import org.jdom.Element
+import org.jdom.output.Format
+import org.jdom.output.XMLOutputter
 import org.jetbrains.kotlin.analyzer.AnalysisResult
 import org.jetbrains.kotlin.asJava.FilteredJvmDiagnostics
 import org.jetbrains.kotlin.backend.common.output.OutputFileCollection
@@ -98,6 +104,67 @@
         }
     }
 
+
+    private fun dumpModel(dir: String, chunk: List<Module>) {
+
+        val modules = Element("modules").apply {
+
+            for (module in chunk) {
+                addContent(Element("module").apply {
+
+                    attributes.add(
+                        Attribute("name", module.getModuleName())
+                    )
+                    attributes.add(
+                        Attribute("type", module.getModuleType())
+                    )
+                    attributes.add(
+                        Attribute("outputDir", module.getOutputDirectory())
+                    )
+
+                    for (friendDir in module.getFriendPaths()) {
+                        addContent(Element("friendDir").setAttribute("path", friendDir))
+                    }
+                    for (source in module.getSourceFiles()) {
+                        addContent(Element("sources").setAttribute("path", source))
+                    }
+                    for (javaSourceRoots in module.getJavaSourceRoots()) {
+                        addContent(Element("javaSourceRoots").setAttribute("path", javaSourceRoots.path))
+                    }
+                    for (classpath in module.getClasspathRoots()) {
+                        addContent(Element("classpath").setAttribute("path", classpath))
+                    }
+                    for (commonSources in module.getCommonSourceFiles()) {
+                        addContent(Element("commonSources").setAttribute("path", commonSources))
+                    }
+
+                })
+            }
+        }
+        val document = Document(modules)
+        val outputter = XMLOutputter(Format.getPrettyFormat())
+        val dirFile = File(dir)
+        if (!dirFile.exists()) {
+            dirFile.mkdirs()
+        }
+        val fileName = "model-${chunk.first().getModuleName()}"
+        var counter = 0
+        fun file(): File {
+            val postfix = if (counter != 0) ".$counter" else ""
+            return File(dirFile, "$fileName$postfix.xml")
+        }
+
+        var outputFile: File
+        do {
+            outputFile = file()
+            counter++
+        } while (outputFile.exists())
+        outputFile.bufferedWriter().use {
+            outputter.output(document, it)
+        }
+
+    }
+
     internal fun compileModules(environment: KotlinCoreEnvironment, buildFile: File?, chunk: List<Module>): Boolean {
         ProgressIndicatorAndCompilationCanceledStatus.checkCanceled()
 
@@ -113,6 +180,11 @@
             moduleVisibilityManager.addFriendPath(path)
         }
 
+        val dumpModelDir = projectConfiguration.get(CommonConfigurationKeys.DUMP_MODEL)
+        if (dumpModelDir != null) {
+            dumpModel(dumpModelDir, chunk)
+        }
+
         val targetDescription = "in targets [" + chunk.joinToString { input -> input.getModuleName() + "-" + input.getModuleType() } + "]"
 
         val result = repeatAnalysisIfNeeded(analyze(environment, targetDescription), environment, targetDescription)
diff --git a/compiler/frontend/src/org/jetbrains/kotlin/config/CommonConfigurationKeys.kt b/compiler/frontend/src/org/jetbrains/kotlin/config/CommonConfigurationKeys.kt
index d9f39f7..876359f 100644
--- a/compiler/frontend/src/org/jetbrains/kotlin/config/CommonConfigurationKeys.kt
+++ b/compiler/frontend/src/org/jetbrains/kotlin/config/CommonConfigurationKeys.kt
@@ -41,6 +41,9 @@
 
     @JvmField
     val METADATA_VERSION = CompilerConfigurationKey.create<BinaryVersion>("metadata version")
+
+    @JvmField
+    val DUMP_MODEL = CompilerConfigurationKey.create<String>("Dump compiler arguments")
 }
 
 var CompilerConfiguration.languageVersionSettings: LanguageVersionSettings