Search in sources :

Example 51 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangSimpleVarRef varRefExpr) {
    BLangSimpleVarRef genVarRefExpr = varRefExpr;
    // XML qualified name reference. e.g: ns0:foo
    if (varRefExpr.pkgSymbol != null && varRefExpr.pkgSymbol.tag == SymTag.XMLNS) {
        BLangXMLQName qnameExpr = new BLangXMLQName(varRefExpr.variableName);
        qnameExpr.nsSymbol = (BXMLNSSymbol) varRefExpr.pkgSymbol;
        qnameExpr.localname = varRefExpr.variableName;
        qnameExpr.prefix = varRefExpr.pkgAlias;
        qnameExpr.namespaceURI = qnameExpr.nsSymbol.namespaceURI;
        qnameExpr.isUsedInXML = false;
        qnameExpr.pos = varRefExpr.pos;
        qnameExpr.type = symTable.stringType;
        result = qnameExpr;
        return;
    }
    BSymbol ownerSymbol = varRefExpr.symbol.owner;
    if ((varRefExpr.symbol.tag & SymTag.FUNCTION) == SymTag.FUNCTION && varRefExpr.symbol.type.tag == TypeTags.INVOKABLE) {
        genVarRefExpr = new BLangFunctionVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        // Local variable in a function/resource/action/worker
        genVarRefExpr = new BLangLocalVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
        // Field variable in a receiver
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.STRUCT) == SymTag.STRUCT) {
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymbol.tag & SymTag.SERVICE) == SymTag.SERVICE) {
        // Package variable | service variable
        // We consider both of them as package level variables
        genVarRefExpr = new BLangPackageVarRef(varRefExpr.symbol);
        // Only locking service level and package level variables
        if (!enclLocks.isEmpty()) {
            enclLocks.peek().addLockVariable(varRefExpr.symbol);
        }
    }
    genVarRefExpr.type = varRefExpr.type;
    result = genVarRefExpr;
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangPackageVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangPackageVarRef) BLangFunctionVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFunctionVarRef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) BLangFieldVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFieldVarRef)

Example 52 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.

the class Desugar method visit.

// visitors
@Override
public void visit(BLangPackage pkgNode) {
    if (pkgNode.completedPhases.contains(CompilerPhase.DESUGAR)) {
        result = pkgNode;
        return;
    }
    SymbolEnv env = this.symTable.pkgEnvMap.get(pkgNode.symbol);
    // Adding object functions to package level.
    pkgNode.objects.forEach(o -> {
        o.functions.forEach(f -> {
            pkgNode.functions.add(f);
            pkgNode.topLevelNodes.add(f);
        });
    });
    // Rewriting Object to struct
    pkgNode.objects.forEach(o -> pkgNode.structs.add(rewriteObjectToStruct(o, env)));
    pkgNode.structs = rewrite(pkgNode.structs, env);
    // Adding struct init functions to package level.
    pkgNode.structs.forEach(struct -> {
        pkgNode.functions.add(struct.initFunction);
        pkgNode.topLevelNodes.add(struct.initFunction);
    });
    pkgNode.imports = rewrite(pkgNode.imports, env);
    pkgNode.xmlnsList = rewrite(pkgNode.xmlnsList, env);
    pkgNode.globalVars = rewrite(pkgNode.globalVars, env);
    pkgNode.globalEndpoints = rewrite(pkgNode.globalEndpoints, env);
    pkgNode.globalEndpoints.forEach(endpoint -> endpointDesugar.defineGlobalEndpoint(endpoint, env));
    annotationDesugar.rewritePackageAnnotations(pkgNode);
    endpointDesugar.rewriteAllEndpointsInPkg(pkgNode, env);
    endpointDesugar.rewriteServiceBoundToEndpointInPkg(pkgNode, env);
    pkgNode.transformers = rewrite(pkgNode.transformers, env);
    pkgNode.functions = rewrite(pkgNode.functions, env);
    pkgNode.connectors = rewrite(pkgNode.connectors, env);
    pkgNode.services = rewrite(pkgNode.services, env);
    pkgNode.initFunction = rewrite(pkgNode.initFunction, env);
    pkgNode.startFunction = rewrite(pkgNode.startFunction, env);
    pkgNode.stopFunction = rewrite(pkgNode.stopFunction, env);
    pkgNode.completedPhases.add(CompilerPhase.DESUGAR);
    result = pkgNode;
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 53 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addAnonStructType.

public void addAnonStructType(DiagnosticPos pos, Set<Whitespace> ws) {
    // Generate a name for the anonymous struct
    String genName = anonymousModelHelper.getNextAnonymousStructKey(pos.src.pkgID);
    IdentifierNode anonStructGenName = createIdentifier(genName);
    // Create an anonymous struct and add it to the list of structs in the current package.
    BLangStruct structNode = populateStructNode(pos, ws, anonStructGenName, true);
    this.compUnit.addTopLevelNode(structNode);
    addType(createUserDefinedType(pos, ws, (BLangIdentifier) TreeBuilder.createIdentifierNode(), structNode.name));
}
Also used : IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier)

Example 54 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.

the class BinaryFileWriter method writeLibraryPackage.

public void writeLibraryPackage(BLangPackage packageNode) {
    String fileName = getOutputFileName(packageNode, BLANG_COMPILED_PACKAGE_FILE_SUFFIX);
    CompiledBinaryFile.PackageFile packageFile = this.codeGenerator.generateBALO(packageNode);
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    try {
        PackageFileWriter.writePackage(packageFile, byteArrayOS);
    } catch (IOException e) {
        throw new BLangCompilerException("error writing package file '" + fileName + "'", e);
    }
    this.sourceDirectory.saveCompiledPackage(new ByteArrayInputStream(byteArrayOS.toByteArray()), fileName);
}
Also used : CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) ByteArrayInputStream(java.io.ByteArrayInputStream) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 55 with PACKAGE

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE 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)

Aggregations

BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)49 ArrayList (java.util.ArrayList)34 Test (org.testng.annotations.Test)29 Page (org.ballerinalang.docgen.model.Page)18 File (java.io.File)16 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)16 IOException (java.io.IOException)15 Path (java.nio.file.Path)13 List (java.util.List)13 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)11 PackageID (org.ballerinalang.model.elements.PackageID)10 Compiler (org.wso2.ballerinalang.compiler.Compiler)10 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)10 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)10 Collectors (java.util.stream.Collectors)9 Arrays (java.util.Arrays)8 CompilerPhase (org.ballerinalang.compiler.CompilerPhase)7 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)6 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)6 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)6