diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..5b4bbf65181 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +.DS_Store +.idea/shelf +/dependencies +/dist +out +tmp +workspace.xml +*.versionsBackup + +.gradle +/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache + diff --git a/translator/.gradle/2.9/taskArtifacts/cache.properties b/translator/.gradle/2.9/taskArtifacts/cache.properties new file mode 100644 index 00000000000..8ffd4eec9e1 --- /dev/null +++ b/translator/.gradle/2.9/taskArtifacts/cache.properties @@ -0,0 +1 @@ +#Thu Jul 07 16:26:11 MSK 2016 diff --git a/translator/.gradle/2.9/taskArtifacts/cache.properties.lock b/translator/.gradle/2.9/taskArtifacts/cache.properties.lock new file mode 100644 index 00000000000..5d12538d200 Binary files /dev/null and b/translator/.gradle/2.9/taskArtifacts/cache.properties.lock differ diff --git a/translator/.gradle/2.9/taskArtifacts/fileHashes.bin b/translator/.gradle/2.9/taskArtifacts/fileHashes.bin new file mode 100644 index 00000000000..2097cddd1b2 Binary files /dev/null and b/translator/.gradle/2.9/taskArtifacts/fileHashes.bin differ diff --git a/translator/.gradle/2.9/taskArtifacts/fileSnapshots.bin b/translator/.gradle/2.9/taskArtifacts/fileSnapshots.bin new file mode 100644 index 00000000000..15cddfed94e Binary files /dev/null and b/translator/.gradle/2.9/taskArtifacts/fileSnapshots.bin differ diff --git a/translator/.gradle/2.9/taskArtifacts/outputFileStates.bin b/translator/.gradle/2.9/taskArtifacts/outputFileStates.bin new file mode 100644 index 00000000000..6e981f93221 Binary files /dev/null and b/translator/.gradle/2.9/taskArtifacts/outputFileStates.bin differ diff --git a/translator/.gradle/2.9/taskArtifacts/taskArtifacts.bin b/translator/.gradle/2.9/taskArtifacts/taskArtifacts.bin new file mode 100644 index 00000000000..1eb8c2e00c5 Binary files /dev/null and b/translator/.gradle/2.9/taskArtifacts/taskArtifacts.bin differ diff --git a/translator/.idea/compiler.xml b/translator/.idea/compiler.xml new file mode 100644 index 00000000000..10357ec3d62 --- /dev/null +++ b/translator/.idea/compiler.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/copyright/profiles_settings.xml b/translator/.idea/copyright/profiles_settings.xml new file mode 100644 index 00000000000..e7bedf3377d --- /dev/null +++ b/translator/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/translator/.idea/gradle.xml b/translator/.idea/gradle.xml new file mode 100644 index 00000000000..ab6e46d7aef --- /dev/null +++ b/translator/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/translator/.idea/libraries/Gradle__junit_junit_4_11.xml b/translator/.idea/libraries/Gradle__junit_junit_4_11.xml new file mode 100644 index 00000000000..dc26b345fb7 --- /dev/null +++ b/translator/.idea/libraries/Gradle__junit_junit_4_11.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml b/translator/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 00000000000..8262f729c28 --- /dev/null +++ b/translator/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_compiler_1_0_3.xml b/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_compiler_1_0_3.xml new file mode 100644 index 00000000000..340062081ce --- /dev/null +++ b/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_compiler_1_0_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_1.xml b/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_1.xml new file mode 100644 index 00000000000..75d28ad69b0 --- /dev/null +++ b/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_1.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_1.xml b/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_1.xml new file mode 100644 index 00000000000..9c961954a78 --- /dev/null +++ b/translator/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_1.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/misc.xml b/translator/.idea/misc.xml new file mode 100644 index 00000000000..ee2a58cb493 --- /dev/null +++ b/translator/.idea/misc.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/modules.xml b/translator/.idea/modules.xml new file mode 100644 index 00000000000..be261610699 --- /dev/null +++ b/translator/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/modules/ast-kotlin.iml b/translator/.idea/modules/ast-kotlin.iml new file mode 100644 index 00000000000..8d0ac41cd95 --- /dev/null +++ b/translator/.idea/modules/ast-kotlin.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/modules/ast-kotlin_main.iml b/translator/.idea/modules/ast-kotlin_main.iml new file mode 100644 index 00000000000..81e1de0dcdd --- /dev/null +++ b/translator/.idea/modules/ast-kotlin_main.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/translator/.idea/modules/ast-kotlin_test.iml b/translator/.idea/modules/ast-kotlin_test.iml new file mode 100644 index 00000000000..04f30958348 --- /dev/null +++ b/translator/.idea/modules/ast-kotlin_test.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/translator/build.gradle b/translator/build.gradle new file mode 100644 index 00000000000..658e32d3566 --- /dev/null +++ b/translator/build.gradle @@ -0,0 +1,27 @@ +group 'ast-kotlin' +version '1.0-SNAPSHOT' + +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.1" + } +} + +apply plugin: 'java' +apply plugin: 'kotlin' + +sourceCompatibility = 1.5 + +repositories { + mavenCentral() +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.1" + // https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-compiler + compile group: 'org.jetbrains.kotlin', name: 'kotlin-compiler', version: '1.0.3' + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/translator/build/classes/main/META-INF/ast-kotlin_main.kotlin_module b/translator/build/classes/main/META-INF/ast-kotlin_main.kotlin_module new file mode 100644 index 00000000000..057d2d3830c Binary files /dev/null and b/translator/build/classes/main/META-INF/ast-kotlin_main.kotlin_module differ diff --git a/translator/build/classes/main/hello/KotlinScriptParser$Companion$classPath$2.class b/translator/build/classes/main/hello/KotlinScriptParser$Companion$classPath$2.class new file mode 100644 index 00000000000..79b25720589 Binary files /dev/null and b/translator/build/classes/main/hello/KotlinScriptParser$Companion$classPath$2.class differ diff --git a/translator/build/classes/main/hello/KotlinScriptParser$Companion$messageCollector$1.class b/translator/build/classes/main/hello/KotlinScriptParser$Companion$messageCollector$1.class new file mode 100644 index 00000000000..5b14cb22de2 Binary files /dev/null and b/translator/build/classes/main/hello/KotlinScriptParser$Companion$messageCollector$1.class differ diff --git a/translator/build/classes/main/hello/KotlinScriptParser$Companion.class b/translator/build/classes/main/hello/KotlinScriptParser$Companion.class new file mode 100644 index 00000000000..d3489af3d52 Binary files /dev/null and b/translator/build/classes/main/hello/KotlinScriptParser$Companion.class differ diff --git a/translator/build/classes/main/hello/KotlinScriptParser.class b/translator/build/classes/main/hello/KotlinScriptParser.class new file mode 100644 index 00000000000..cd9e6a004a1 Binary files /dev/null and b/translator/build/classes/main/hello/KotlinScriptParser.class differ diff --git a/translator/build/classes/main/hello/MainKt.class b/translator/build/classes/main/hello/MainKt.class new file mode 100644 index 00000000000..2d8372d5b04 Binary files /dev/null and b/translator/build/classes/main/hello/MainKt.class differ diff --git a/translator/build/classes/main/hello/llvmVariable.class b/translator/build/classes/main/hello/llvmVariable.class new file mode 100644 index 00000000000..e282413b68a Binary files /dev/null and b/translator/build/classes/main/hello/llvmVariable.class differ diff --git a/translator/build/classes/main/llvmBuilder$llvmBuilder.class b/translator/build/classes/main/llvmBuilder$llvmBuilder.class new file mode 100644 index 00000000000..8eaeb8d1204 Binary files /dev/null and b/translator/build/classes/main/llvmBuilder$llvmBuilder.class differ diff --git a/translator/build/classes/main/llvmBuilder.class b/translator/build/classes/main/llvmBuilder.class new file mode 100644 index 00000000000..bb65853b6ca Binary files /dev/null and b/translator/build/classes/main/llvmBuilder.class differ diff --git a/translator/build/classes/main/org/kotlin/native/translator/debug/DebugKt.class b/translator/build/classes/main/org/kotlin/native/translator/debug/DebugKt.class new file mode 100644 index 00000000000..ce174ba60c2 Binary files /dev/null and b/translator/build/classes/main/org/kotlin/native/translator/debug/DebugKt.class differ diff --git a/translator/gradle/wrapper/gradle-wrapper.jar b/translator/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000000..941144813d2 Binary files /dev/null and b/translator/gradle/wrapper/gradle-wrapper.jar differ diff --git a/translator/gradle/wrapper/gradle-wrapper.properties b/translator/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000000..87174db222c --- /dev/null +++ b/translator/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Thu Jul 07 16:27:28 MSK 2016 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip diff --git a/translator/gradlew b/translator/gradlew new file mode 100755 index 00000000000..9d82f789151 --- /dev/null +++ b/translator/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/translator/gradlew.bat b/translator/gradlew.bat new file mode 100644 index 00000000000..aec99730b4e --- /dev/null +++ b/translator/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/translator/settings.gradle b/translator/settings.gradle new file mode 100644 index 00000000000..ffc54b24c49 --- /dev/null +++ b/translator/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'ast-kotlin' + diff --git a/translator/src/main/kotlin/KotlinScriptParser.kt b/translator/src/main/kotlin/KotlinScriptParser.kt new file mode 100644 index 00000000000..4183fdb40fe --- /dev/null +++ b/translator/src/main/kotlin/KotlinScriptParser.kt @@ -0,0 +1,121 @@ +package hello + +import com.intellij.openapi.util.Disposer +import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity +import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector +import org.jetbrains.kotlin.cli.common.messages.MessageCollector +import org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport +import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles +import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots +import org.jetbrains.kotlin.cli.jvm.config.getModuleName +import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.config.JVMConfigurationKeys +import org.jetbrains.kotlin.config.LanguageVersion +import org.jetbrains.kotlin.config.addKotlinSourceRoot +import org.jetbrains.kotlin.descriptors.PackageFragmentProvider +import org.jetbrains.kotlin.frontend.java.di.createContainerForTopDownAnalyzerForJvm +import org.jetbrains.kotlin.incremental.components.LookupTracker +import org.jetbrains.kotlin.load.java.JvmAbi +import org.jetbrains.kotlin.resolve.TopDownAnalysisContext +import org.jetbrains.kotlin.resolve.TopDownAnalysisMode +import org.jetbrains.kotlin.resolve.jvm.JvmAnalyzerFacade +import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM +import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory +import org.jetbrains.kotlin.utils.PathUtil +import java.io.File +import java.util.* +import java.util.logging.Logger +import org.kotlin.native.translator.debug.* + +/** + * Created by user on 7/8/16. + */ +class KotlinScriptParser { + companion object { + private val LOG = Logger.getLogger(KotlinScriptParser.javaClass.name) + private val messageCollector = object : MessageCollector { + override fun hasErrors(): Boolean { + throw UnsupportedOperationException() + } + + override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) { + val path = location.path + val position = if (path == null) "" else "$path: (${location.line}, ${location.column}) " + + val text = position + message + + if (CompilerMessageSeverity.VERBOSE.contains(severity)) { + LOG.finest(text) + } else if (CompilerMessageSeverity.ERRORS.contains(severity)) { + LOG.severe(text) + } else if (severity == CompilerMessageSeverity.INFO) { + LOG.info(text) + } else { + LOG.warning(text) + } + } + } + + private val classPath: ArrayList by lazy { + val classpath = arrayListOf() + classpath += PathUtil.getResourcePathForClass(AnnotationTarget.CLASS.javaClass) + classpath + } + } + + fun parse(vararg files: String): TopDownAnalysisContext { + // The Kotlin compiler configuration + val configuration = CompilerConfiguration() + + val groupingCollector = GroupingMessageCollector(messageCollector) + //val severityCollector = MessageSeverityCollector(groupingCollector) + //configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, severityCollector) + + + configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots()) + // The path to .kt files sources + files.forEach { configuration.addKotlinSourceRoot(it) } + // Configuring Kotlin class path + configuration.addJvmClasspathRoots(classPath) + configuration.put(JVMConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME) + //configuration.put>(JVMConfigurationKeys.SCRIPT_PARAMETERS, CommandLineScriptUtils.scriptParameters()) + + val rootDisposable = Disposer.newDisposable() + try { + val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) + val ktFiles = environment.getSourceFiles() + //printFile(ktFiles.first()) + val sharedTrace = CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace() + val moduleContext = TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(environment.project, + environment.getModuleName()) + + val project = moduleContext.project + val allFiles = JvmAnalyzerFacade.getAllFilesToAnalyze(project, null, ktFiles) + val providerFactory = FileBasedDeclarationProviderFactory(moduleContext.storageManager, allFiles) + val lookupTracker = LookupTracker.DO_NOTHING + val packagePartProvider = JvmPackagePartProvider(environment) + val container = createContainerForTopDownAnalyzerForJvm( + moduleContext, + sharedTrace, + providerFactory, + GlobalSearchScope.allScope(project), + lookupTracker, + packagePartProvider, LanguageVersion.LATEST) + + val additionalProviders = ArrayList() + + additionalProviders.add(container.javaDescriptorResolver.packageFragmentProvider) + + return container.lazyTopDownAnalyzerForTopLevel.analyzeFiles(TopDownAnalysisMode.LocalDeclarations, allFiles, additionalProviders) + } finally { + rootDisposable.dispose() + //if (severityCollector.anyReported(CompilerMessageSeverity.ERROR)) { + // throw RuntimeException("Compilation error") + //} + } + } +} \ No newline at end of file diff --git a/translator/src/main/kotlin/debug.kt b/translator/src/main/kotlin/debug.kt new file mode 100644 index 00000000000..5b4e584cf6d --- /dev/null +++ b/translator/src/main/kotlin/debug.kt @@ -0,0 +1,56 @@ +/** + * Created by user on 7/8/16. + */ +package org.kotlin.native.translator.debug + +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiWhiteSpace +import com.intellij.psi.impl.source.tree.CompositeElement +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtNamedFunction + +fun debugPrintNode(node: Any?, indent: Int = 0): Unit { + if (node == null) { + return + } + + for (i in 0..indent) { + print(" ") + } + + println(node.toString()) + + when (node) { + is CompositeElement -> { + debugPrintNode(node.firstChildNode, indent + 2) + debugPrintNode(node.treeNext, indent) + } + is PsiElement -> { + debugPrintNode(node.firstChild, indent + 2) + debugPrintNode(node.nextSibling, indent) + } + is PsiWhiteSpace -> { + debugPrintNode(node.firstChild, indent + 2) + debugPrintNode(node.nextSibling, indent) + } + else -> { + print(" not supported: " + node.javaClass.canonicalName) + } + } + +} + +fun printFunction(function: KtNamedFunction) { + debugPrintNode(function.node) +} + +fun printFile(file: KtFile) { + + for (declaration in file.declarations) { + + when (declaration) { + is KtNamedFunction -> printFunction(declaration) + else -> println(declaration.toString()) + } + } +} \ No newline at end of file diff --git a/translator/src/main/kotlin/llvmBuilder.kt b/translator/src/main/kotlin/llvmBuilder.kt new file mode 100644 index 00000000000..a06fbeb9348 --- /dev/null +++ b/translator/src/main/kotlin/llvmBuilder.kt @@ -0,0 +1,14 @@ +package hello + +class llvmBuilder{ + private val llvmCode : StringBuilder = StringBuilder() + class llvmBuilder constructor() {} + + fun addLlvmCode(code : String){ + llvmCode.appendln(code) + } + + override fun toString(): String { + return llvmCode.toString() + } +} \ No newline at end of file diff --git a/translator/src/main/kotlin/llvmVariable.kt b/translator/src/main/kotlin/llvmVariable.kt new file mode 100644 index 00000000000..7c0f255d22c --- /dev/null +++ b/translator/src/main/kotlin/llvmVariable.kt @@ -0,0 +1,3 @@ +package hello + +data class llvmVariable(val label : String) \ No newline at end of file diff --git a/translator/src/main/kotlin/main.kt b/translator/src/main/kotlin/main.kt new file mode 100644 index 00000000000..7c6ef167d3c --- /dev/null +++ b/translator/src/main/kotlin/main.kt @@ -0,0 +1,152 @@ +package hello + +import com.intellij.openapi.util.Disposer +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiWhiteSpace +import com.intellij.psi.impl.source.tree.LeafPsiElement +import com.intellij.psi.impl.source.tree.TreeElement +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.tree.IElementType +import org.jetbrains.kotlin.KtNodeType +import org.jetbrains.kotlin.KtNodeTypes +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys +import org.jetbrains.kotlin.cli.common.messages.* +import org.jetbrains.kotlin.cli.jvm.compiler.* +import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots +import org.jetbrains.kotlin.cli.jvm.config.getModuleName +import org.jetbrains.kotlin.descriptors.PackageFragmentProvider +import org.jetbrains.kotlin.frontend.java.di.createContainerForTopDownAnalyzerForJvm +import org.jetbrains.kotlin.incremental.components.LookupTracker +import org.jetbrains.kotlin.load.java.JvmAbi +import org.jetbrains.kotlin.psi.KtBlockExpression +import org.jetbrains.kotlin.resolve. AnalyzingUtils +import org.jetbrains.kotlin.resolve.TopDownAnalysisContext +import org.jetbrains.kotlin.resolve.TopDownAnalysisMode +import org.jetbrains.kotlin.resolve.jvm.JvmAnalyzerFacade +import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM +import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory +import org.jetbrains.kotlin.utils.PathUtil +import org.jetbrains.kotlin.cli.jvm.compiler.* +import org.jetbrains.kotlin.config.* +import org.jetbrains.kotlin.lexer.KtKeywordToken +import org.jetbrains.kotlin.lexer.KtSingleValueToken +import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.psi.KtBinaryExpression +import org.jetbrains.kotlin.psi.KtConstantExpression +import org.jetbrains.kotlin.psi.KtReturnExpression +import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext +import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments +import org.kotlin.native.translator.debug.debugPrintNode +import org.jetbrains.kotlin.lexer.KtToken +import java.lang.StringBuilder +import java.io.File +import java.util.* +import java.util.logging.Logger + + +var llvmCode = llvmBuilder() + +fun expressionWalker(expr : Any?) { + when(expr){ + is KtBlockExpression -> evaluateBlockExpression(expr) + is PsiElement -> evaluatePsiExpression(expr) + null -> Unit + else -> UnsupportedOperationException() + } +} + + +fun evaluateBlockExpression(expr : KtBlockExpression){ + var element = expr + expressionWalker(element.firstChild) + + expressionWalker(element.getNextSiblingIgnoringWhitespaceAndComments()) +} + +var variableCount = 0 +fun evaluatePsiExpression(expr : PsiElement){ + var element = expr + evaluateExpression(element.firstChild) + + evaluatePsiExpression(element.getNextSiblingIgnoringWhitespaceAndComments() ?: return) +} + +fun evaluateExpression(expr: Any?) : llvmVariable?{ + return when(expr){ + is KtBinaryExpression -> evaluateBinaryExpression(expr) + is PsiWhiteSpace -> null + is PsiElement -> evaluatePsiElement(expr) + is KtConstantExpression -> evaluateConstantExpression(expr) + null -> null + else -> throw UnsupportedOperationException() + } +} + +fun evaluateBinaryExpression(expr : KtBinaryExpression) : llvmVariable{ + val left = evaluateExpression(expr.firstChild) + val right = evaluateExpression(expr.lastChild) + val operator = expr.operationToken + + + + val llvmOperator = when (operator) { + KtTokens.PLUS -> "add nsw i32" + KtTokens.MINUS -> "sub nsw i32" + KtTokens.MUL -> "mul nsw i32" + else -> throw UnsupportedOperationException("Unkbown binary operator") + } + + variableCount++ + llvmCode.addLlvmCode("%var$variableCount = $llvmOperator ${left?.label}, ${right?.label}") + return llvmVariable("%var$variableCount") +} + +fun evaluateConstantExpression(expr : KtConstantExpression) : llvmVariable { + val node = expr.node + return llvmVariable(node.firstChildNode.text) +} + +fun evaluatePsiElement(element : PsiElement) : llvmVariable? { + return when(element){ + is LeafPsiElement -> evaluateLeafPsiElement(element) + is KtConstantExpression -> evaluateConstantExpression(element) + KtTokens.INTEGER_LITERAL -> null + else -> null + } +} + +fun evaluateLeafPsiElement(element : LeafPsiElement) : llvmVariable? { + return when(element.elementType){ + KtTokens.RETURN_KEYWORD -> evaluateReturnInstruction(element) + else -> llvmVariable("") + } + +} + +fun evaluateReturnInstruction(element : LeafPsiElement) : llvmVariable?{ + var next = element.getNextSiblingIgnoringWhitespaceAndComments(); + val retVar = evaluateExpression(next) + + llvmCode.addLlvmCode("ret i32 ${retVar?.label}") + return null +} + +fun main(args: Array) { + val scriptFile = "/home/user/Kotlin/manual/sum.kt" + + val parser = KotlinScriptParser() + + val analyzeContext = parser.parse(scriptFile) + + val function = analyzeContext.functions.keys.first() + var body = function.bodyExpression as KtBlockExpression + + llvmCode.addLlvmCode("define i32 @${function.fqName}() #0{") + + expressionWalker(body); + + llvmCode.addLlvmCode("}") + println(llvmCode) + + +} \ No newline at end of file