Search in sources :

Example 1 with ProgramFile

use of org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile in project ballerina by ballerina-lang.

the class BCompileUtil method compile.

/**
 * Compile and return the semantic errors.
 *
 * @param sourceRoot    root path of the source packages
 * @param packageName   name of the package to compile
 * @param compilerPhase Compiler phase
 * @return Semantic errors
 */
public static CompileResult compile(String sourceRoot, String packageName, CompilerPhase compilerPhase) {
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, sourceRoot);
    options.put(COMPILER_PHASE, compilerPhase.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    CompileResult comResult = new CompileResult();
    // catch errors
    DiagnosticListener listener = comResult::addDiagnostic;
    context.put(DiagnosticListener.class, listener);
    // compile
    Compiler compiler = Compiler.getInstance(context);
    BLangPackage packageNode = compiler.compile(packageName);
    comResult.setAST(packageNode);
    if (comResult.getErrorCount() > 0 || CompilerPhase.CODE_GEN.compareTo(compilerPhase) > 0) {
        return comResult;
    }
    CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(packageNode);
    if (programFile != null) {
        ProgramFile pFile = LauncherUtils.getExecutableProgram(programFile);
        comResult.setProgFile(pFile);
        if (pFile != null) {
            boolean distributedTxEnabled = CompilerUtils.isDistributedTransactionsEnabled();
            pFile.setDistributedTransactionEnabled(distributedTxEnabled);
        }
    }
    return comResult;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 2 with ProgramFile

use of org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile in project ballerina by ballerina-lang.

the class BCompileUtil method getDiagnostics.

/**
 * Used by IntelliJ IDEA plugin to provide semantic analyzing capability.
 *
 * @param classLoader a {@link ClassLoader} to be set as thread context class loader. This is used by {@link
 *                    java.util.ServiceLoader}. Otherwise semantic analyzing capability providing wont work since it
 *                    cant find core package.
 * @param sourceRoot  source root of a project
 * @param fileName    either the file name (if in project root) or the package name
 * @return list of diagnostics
 */
public static List<Diagnostic> getDiagnostics(ClassLoader classLoader, String sourceRoot, String fileName) {
    Thread.currentThread().setContextClassLoader(classLoader);
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, sourceRoot);
    options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    CompileResult comResult = new CompileResult();
    // catch errors
    DiagnosticListener listener = comResult::addDiagnostic;
    context.put(DiagnosticListener.class, listener);
    // compile
    Compiler compiler = Compiler.getInstance(context);
    BLangPackage entryPackageNode = compiler.compile(fileName);
    CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(entryPackageNode);
    if (programFile != null) {
        comResult.setProgFile(LauncherUtils.getExecutableProgram(programFile));
    }
    Diagnostic[] diagnostics = comResult.getDiagnostics();
    return Arrays.stream(diagnostics).collect(Collectors.toList());
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener)

Example 3 with ProgramFile

use of org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile in project ballerina by ballerina-lang.

the class CodeGenerator method generateBALO.

public PackageFile generateBALO(BLangPackage pkgNode) {
    this.buildCompiledPackage = true;
    this.packageFile = new PackageFile();
    genPackage(pkgNode.symbol);
    // Add global variable indexes to the ProgramFile
    prepareIndexes(pvIndexes);
    // Create Global variable attribute info
    addVarCountAttrInfo(this.packageFile, this.packageFile, pvIndexes);
    return this.packageFile;
}
Also used : PackageFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile.PackageFile)

Example 4 with ProgramFile

use of org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile in project ballerina by ballerina-lang.

the class CodeGenerator method generateBALX.

public ProgramFile generateBALX(BLangPackage pkgNode) {
    programFile = new ProgramFile();
    // TODO: Fix this. Added temporally for codegen. Load this from VM side.
    genPackage(this.symTable.builtInPackageSymbol);
    // Normal Flow.
    BPackageSymbol pkgSymbol = pkgNode.symbol;
    genPackage(pkgSymbol);
    programFile.entryPkgCPIndex = addPackageRefCPEntry(programFile, pkgSymbol.pkgID);
    setEntryPoints(programFile, pkgNode);
    // Add global variable indexes to the ProgramFile
    prepareIndexes(pvIndexes);
    // Create Global variable attribute info
    addVarCountAttrInfo(programFile, programFile, pvIndexes);
    return programFile;
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ProgramFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile)

Example 5 with ProgramFile

use of org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile in project ballerina by ballerina-lang.

the class BinaryFileWriter method writeExecutableBinary.

public void writeExecutableBinary(BLangPackage packageNode, String fileName) {
    if (fileName == null || fileName.isEmpty()) {
        throw new IllegalArgumentException("invalid target file name");
    }
    if (!fileName.endsWith(BLANG_EXEC_FILE_SUFFIX)) {
        fileName += BLANG_EXEC_FILE_SUFFIX;
    }
    // Generate code for the given executable
    ProgramFile programFile = this.codeGenerator.generateBALX(packageNode);
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    try {
        ProgramFileWriter.writeProgram(programFile, byteArrayOS);
    } catch (IOException e) {
        throw new BLangCompilerException("error writing program file '" + fileName + "'", e);
    }
    final Path execFilePath = this.sourceDirectory.saveCompiledProgram(new ByteArrayInputStream(byteArrayOS.toByteArray()), fileName);
    ServiceLoader<CompilerPlugin> processorServiceLoader = ServiceLoader.load(CompilerPlugin.class);
    processorServiceLoader.forEach(plugin -> {
        plugin.codeGenerated(execFilePath);
    });
}
Also used : Path(java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProgramFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile)

Aggregations

Compiler (org.wso2.ballerinalang.compiler.Compiler)4 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)4 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)4 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)4 CompiledBinaryFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile)4 DiagnosticListener (org.ballerinalang.util.diagnostic.DiagnosticListener)3 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)2 ProgramFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 BLangCompilerException (org.ballerinalang.compiler.BLangCompilerException)1 CompilerPlugin (org.ballerinalang.compiler.plugins.CompilerPlugin)1 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)1 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 PackageFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile.PackageFile)1 VarTypeCountAttributeInfo (org.wso2.ballerinalang.programfile.attributes.VarTypeCountAttributeInfo)1