Search in sources :

Example 11 with Compiler

use of org.wso2.ballerinalang.compiler.Compiler 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 12 with Compiler

use of org.wso2.ballerinalang.compiler.Compiler in project ballerina by ballerina-lang.

the class TaintAnalyzer method analyzeInvocation.

// Private methods relevant to invocation analysis.
private void analyzeInvocation(BLangInvocation invocationExpr) {
    BInvokableSymbol invokableSymbol = (BInvokableSymbol) invocationExpr.symbol;
    Map<Integer, TaintRecord> taintTable = invokableSymbol.taintTable;
    List<Boolean> returnTaintedStatus = new ArrayList<>();
    TaintRecord allParamsUntaintedRecord = taintTable.get(ALL_UNTAINTED_TABLE_ENTRY_INDEX);
    if (allParamsUntaintedRecord.taintError != null && allParamsUntaintedRecord.taintError.size() > 0) {
        // This can occur when there is a error regardless of tainted status of parameters.
        // Example: Tainted value returned by function is passed to another functions's sensitive parameter.
        addTaintError(allParamsUntaintedRecord.taintError);
    } else {
        returnTaintedStatus = new ArrayList<>(taintTable.get(ALL_UNTAINTED_TABLE_ENTRY_INDEX).retParamTaintedStatus);
    }
    if (invocationExpr.argExprs != null) {
        for (int argIndex = 0; argIndex < invocationExpr.argExprs.size(); argIndex++) {
            BLangExpression argExpr = invocationExpr.argExprs.get(argIndex);
            argExpr.accept(this);
            // return-tainted-status when the given argument is in tainted state.
            if (getObservedTaintedStatus()) {
                TaintRecord taintRecord = taintTable.get(argIndex);
                if (taintRecord == null) {
                    // This is when current parameter is "sensitive". Therefore, providing a tainted
                    // value to a sensitive parameter is invalid and should return a compiler error.
                    int requiredParamCount = invokableSymbol.params.size();
                    int defaultableParamCount = invokableSymbol.defaultableParams.size();
                    int totalParamCount = requiredParamCount + defaultableParamCount + (invokableSymbol.restParam == null ? 0 : 1);
                    BVarSymbol paramSymbol = getParamSymbol(invokableSymbol, argIndex, requiredParamCount, defaultableParamCount);
                    addTaintError(argExpr.pos, paramSymbol.name.value, DiagnosticCode.TAINTED_VALUE_PASSED_TO_SENSITIVE_PARAMETER);
                } else if (taintRecord.taintError != null && taintRecord.taintError.size() > 0) {
                    // This is when current parameter is derived to be sensitive. The error already generated
                    // during taint-table generation will be used.
                    addTaintError(taintRecord.taintError);
                } else {
                    // status of all returns to get accumulated tainted status of all returns for the invocation.
                    for (int returnIndex = 0; returnIndex < returnTaintedStatus.size(); returnIndex++) {
                        if (taintRecord.retParamTaintedStatus.get(returnIndex)) {
                            returnTaintedStatus.set(returnIndex, true);
                        }
                    }
                }
                if (stopAnalysis) {
                    break;
                }
            }
        }
    }
    if (invocationExpr.expr != null) {
        // When an invocation like stringValue.trim() happens, if stringValue is tainted, the result will
        // also be tainted.
        // TODO: TaintedIf annotation, so that it's possible to define what can taint or untaint the return.
        invocationExpr.expr.accept(this);
        for (int i = 0; i < returnTaintedStatus.size(); i++) {
            if (getObservedTaintedStatus()) {
                returnTaintedStatus.set(i, getObservedTaintedStatus());
            }
        }
    }
    taintedStatusList = returnTaintedStatus;
}
Also used : ArrayList(java.util.ArrayList) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) TaintRecord(org.wso2.ballerinalang.compiler.semantics.model.symbols.TaintRecord) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 13 with Compiler

use of org.wso2.ballerinalang.compiler.Compiler in project ballerina by ballerina-lang.

the class CompilerPluginRunner method handleEndpointProcesses.

private void handleEndpointProcesses(CompilerPlugin plugin) {
    // Get the list of endpoint of that this particular compiler plugin is interested in.
    SupportEndpointTypes supportEndpointTypes = plugin.getClass().getAnnotation(SupportEndpointTypes.class);
    if (supportEndpointTypes == null) {
        return;
    }
    final SupportEndpointTypes.EndpointType[] endpointTypes = supportEndpointTypes.value();
    if (endpointTypes.length == 0) {
        return;
    }
    DefinitionID[] definitions = Arrays.stream(endpointTypes).map(endpointType -> new DefinitionID(endpointType.packageName(), endpointType.name())).toArray(DefinitionID[]::new);
    for (DefinitionID definitionID : definitions) {
        if (isValidEndpoints(definitionID, plugin)) {
            List<CompilerPlugin> processorList = endpointProcessorMap.computeIfAbsent(definitionID, k -> new ArrayList<>());
            processorList.add(plugin);
        }
    }
}
Also used : Arrays(java.util.Arrays) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) HashMap(java.util.HashMap) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) ArrayList(java.util.ArrayList) SupportEndpointTypes(org.ballerinalang.compiler.plugins.SupportEndpointTypes) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) PackageCache(org.wso2.ballerinalang.compiler.PackageCache) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) BAnnotationSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) Names(org.wso2.ballerinalang.compiler.util.Names) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangForever(org.wso2.ballerinalang.compiler.tree.statements.BLangForever) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) PackageID(org.ballerinalang.model.elements.PackageID) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ServiceLoader(java.util.ServiceLoader) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) Objects(java.util.Objects) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) BLangPackageDeclaration(org.wso2.ballerinalang.compiler.tree.BLangPackageDeclaration) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Collections(java.util.Collections) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) SupportEndpointTypes(org.ballerinalang.compiler.plugins.SupportEndpointTypes)

Example 14 with Compiler

use of org.wso2.ballerinalang.compiler.Compiler in project ballerina by ballerina-lang.

the class CompilerDriver method compile.

// Private methods
private BLangPackage compile(BLangPackage bLangPackage) {
    BLangPackage pkgNode = bLangPackage;
    // "ballerina/built-in" packages is only the pre-known package by the Ballerina compiler. So load it first.
    BLangPackage builtInPackage = loadBuiltInPackage();
    if (this.stopCompilation(pkgNode, CompilerPhase.DEFINE)) {
        return pkgNode;
    }
    pkgNode = define(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.TYPE_CHECK)) {
        return pkgNode;
    }
    pkgNode = typeCheck(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.CODE_ANALYZE)) {
        return pkgNode;
    }
    pkgNode = codeAnalyze(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.TAINT_ANALYZE)) {
        return pkgNode;
    }
    pkgNode = taintAnalyze(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.COMPILER_PLUGIN)) {
        return pkgNode;
    }
    pkgNode = annotationProcess(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.DESUGAR)) {
        return pkgNode;
    }
    // TODO : Improve this.
    desugar(builtInPackage);
    return desugar(pkgNode);
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage)

Example 15 with Compiler

use of org.wso2.ballerinalang.compiler.Compiler in project ballerina by ballerina-lang.

the class CommonUtil method prepareTempCompilerContext.

/**
 * Prepare a new compiler context.
 * @return {@link CompilerContext} Prepared compiler context
 */
public static CompilerContext prepareTempCompilerContext() {
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, "");
    options.put(COMPILER_PHASE, CompilerPhase.DESUGAR.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    context.put(SourceDirectory.class, new NullSourceDirectory());
    return context;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) NullSourceDirectory(org.ballerinalang.langserver.workspace.repository.NullSourceDirectory)

Aggregations

CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 Compiler (org.wso2.ballerinalang.compiler.Compiler)14 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)13 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)12 ArrayList (java.util.ArrayList)8 Path (java.nio.file.Path)7 DiagnosticListener (org.ballerinalang.util.diagnostic.DiagnosticListener)4 CompiledBinaryFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile)4 IOException (java.io.IOException)3 LSDocument (org.ballerinalang.langserver.common.LSDocument)3 WorkspacePackageRepository (org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)3 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)3 BDiagnostic (org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic)3 CompilerPhase (org.ballerinalang.compiler.CompilerPhase)2 CompilerPlugin (org.ballerinalang.compiler.plugins.CompilerPlugin)2 SupportedAnnotationPackages (org.ballerinalang.compiler.plugins.SupportedAnnotationPackages)2 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)2 PackageRepository (org.ballerinalang.repository.PackageRepository)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 File (java.io.File)1