Search in sources :

Example 96 with PACKAGE

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

the class SymbolEnter method visit.

@Override
public void visit(BLangImportPackage importPkgNode) {
    Name pkgAlias = names.fromIdNode(importPkgNode.alias);
    if (symResolver.lookupSymbol(env, pkgAlias, SymTag.IMPORT) != symTable.notFoundSymbol) {
        dlog.error(importPkgNode.pos, DiagnosticCode.REDECLARED_SYMBOL, pkgAlias);
        return;
    }
    // TODO Clean this code up. Can we move the this to BLangPackageBuilder class
    // Create import package symbol
    Name orgName;
    if (importPkgNode.orgName.value == null || importPkgNode.orgName.value.isEmpty()) {
        // means it's in 'import <pkg-name>' style
        orgName = Names.ANON_ORG;
    } else {
        // means it's in 'import <org-name>/<pkg-name>' style
        orgName = names.fromIdNode(importPkgNode.orgName);
    }
    List<Name> nameComps = importPkgNode.pkgNameComps.stream().map(identifier -> names.fromIdNode(identifier)).collect(Collectors.toList());
    String version = names.fromIdNode(importPkgNode.version).getValue().replaceAll("[^\\d.]", "");
    PackageID pkgId = new PackageID(orgName, nameComps, new Name(version));
    if (pkgId.name.getValue().startsWith(Names.BUILTIN_PACKAGE.value)) {
        dlog.error(importPkgNode.pos, DiagnosticCode.PACKAGE_NOT_FOUND, importPkgNode.getQualifiedPackageName());
        return;
    }
    // Attempt to load the imported package.
    BLangPackage pkgNode = pkgLoader.loadPackage(pkgId, env.enclPkg.packageRepository);
    if (pkgNode == null) {
        dlog.error(importPkgNode.pos, DiagnosticCode.PACKAGE_NOT_FOUND, importPkgNode.getQualifiedPackageName());
        return;
    }
    // This import package is not defined.
    if (pkgNode.symbol == null) {
        // Define import package now.
        definePackage(pkgNode);
        populateInitFunctionInvocation(importPkgNode, pkgNode.symbol);
    }
    // define the import package symbol in the current package scope
    importPkgNode.symbol = pkgNode.symbol;
    this.env.scope.define(pkgAlias, pkgNode.symbol);
}
Also used : BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BConnectorType(org.wso2.ballerinalang.compiler.semantics.model.types.BConnectorType) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Flag(org.ballerinalang.model.elements.Flag) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) BAnnotationSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) BServiceSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BServiceSymbol) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType) EnumSet(java.util.EnumSet) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BConnectorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConnectorSymbol) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) BXMLAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLAttributeSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) List(java.util.List) IMPORT(org.ballerinalang.model.tree.NodeKind.IMPORT) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) NodeKind(org.ballerinalang.model.tree.NodeKind) BAnnotationAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationAttributeSymbol) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangXMLNSStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangXMLNSStatement) BLangInvokableNode(org.wso2.ballerinalang.compiler.tree.BLangInvokableNode) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BStructField(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BAnnotationType(org.wso2.ballerinalang.compiler.semantics.model.types.BAnnotationType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) PackageLoader(org.wso2.ballerinalang.compiler.PackageLoader) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) BServiceType(org.wso2.ballerinalang.compiler.semantics.model.types.BServiceType) Flags(org.wso2.ballerinalang.util.Flags) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) DocTag(org.ballerinalang.model.elements.DocTag) TreeBuilder(org.ballerinalang.model.TreeBuilder) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) XMLConstants(javax.xml.XMLConstants) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Name(org.wso2.ballerinalang.compiler.util.Name) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangEnumerator(org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) PackageID(org.ballerinalang.model.elements.PackageID) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 97 with PACKAGE

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

the class SemanticAnalyzer method visit.

public void visit(BLangVariable varNode) {
    int ownerSymTag = env.scope.owner.tag;
    if ((ownerSymTag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        // If the variable is parameter then the variable symbol is already defined
        if (varNode.symbol == null) {
            symbolEnter.defineNode(varNode, env);
        }
    }
    BType lhsType = varNode.symbol.type;
    varNode.type = lhsType;
    // Here we validate annotation attachments for package level variables.
    varNode.annAttachments.forEach(a -> {
        a.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.CONST);
        a.accept(this);
    });
    // Here we validate document attachments for package level variables.
    varNode.docAttachments.forEach(doc -> {
        doc.accept(this);
    });
    // Analyze the init expression
    BLangExpression rhsExpr = varNode.expr;
    if (rhsExpr == null) {
        return;
    }
    // Here we create a new symbol environment to catch self references by keep the current
    // variable symbol in the symbol environment
    // e.g. int a = x + a;
    SymbolEnv varInitEnv = SymbolEnv.createVarInitEnv(varNode, env, varNode.symbol);
    // It will we done during the init-function of the respective construct is visited.
    if ((ownerSymTag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymTag & SymTag.SERVICE) == SymTag.SERVICE || (ownerSymTag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
        return;
    }
    // Return if this not a safe assignment
    if (!varNode.safeAssignment) {
        typeChecker.checkExpr(rhsExpr, varInitEnv, Lists.of(lhsType));
        return;
    }
    handleSafeAssignment(varNode.pos, lhsType, rhsExpr, varInitEnv);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint)

Example 98 with PACKAGE

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

the class Types method checkStructEquivalency.

public boolean checkStructEquivalency(BType rhsType, BType lhsType) {
    if (rhsType.tag != TypeTags.STRUCT || lhsType.tag != TypeTags.STRUCT) {
        return false;
    }
    // The public bit is on means, one is public, and the other one is private.
    if (Symbols.isFlagOn(lhsType.tsymbol.flags ^ rhsType.tsymbol.flags, Flags.PUBLIC)) {
        return false;
    }
    // If both structs are private, they should be in the same package.
    if (Symbols.isPrivate(lhsType.tsymbol) && rhsType.tsymbol.pkgID != lhsType.tsymbol.pkgID) {
        return false;
    }
    // RHS type should have at least all the fields as well attached functions of LHS type.
    BStructType lhsStructType = (BStructType) lhsType;
    BStructType rhsStructType = (BStructType) rhsType;
    if (lhsStructType.fields.size() > rhsStructType.fields.size()) {
        return false;
    }
    return Symbols.isPrivate(lhsType.tsymbol) && rhsType.tsymbol.pkgID == lhsType.tsymbol.pkgID ? checkEquivalencyOfTwoPrivateStructs(lhsStructType, rhsStructType) : checkEquivalencyOfPublicStructs(lhsStructType, rhsStructType);
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)

Example 99 with PACKAGE

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

the class CodeGenerator method getRegIndexInternal.

private RegIndex getRegIndexInternal(int typeTag, VariableIndex.Kind varIndexKind) {
    int index;
    switch(varIndexKind) {
        case REG:
            return new RegIndex(getNextIndex(typeTag, regIndexes), typeTag);
        case PACKAGE:
            index = getNextIndex(typeTag, pvIndexes);
            break;
        case FIELD:
            index = getNextIndex(typeTag, fieldIndexes);
            break;
        default:
            index = getNextIndex(typeTag, lvIndexes);
            break;
    }
    RegIndex regIndex = new RegIndex(index, typeTag);
    regIndex.isVarIndex = true;
    return regIndex;
}
Also used : BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 100 with PACKAGE

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

the class AnnotationDesugar method rewritePackageAnnotations.

protected void rewritePackageAnnotations(BLangPackage pkgNode) {
    BLangFunction initFunction = pkgNode.initFunction;
    // Remove last return statement. we will add it later. TODO : Fix this properly.
    initFunction.body.stmts.remove(initFunction.body.stmts.size() - 1);
    // This is the variable which store all package level annotations.
    BLangVariable annotationMap = createGlobalAnnotationMapVar(pkgNode);
    // handle Service Annotations.
    for (BLangService service : pkgNode.services) {
        generateAnnotations(service, service.name.value, initFunction, annotationMap);
        for (BLangResource resource : service.resources) {
            String key = service.name.value + DOT + resource.name.value;
            generateAnnotations(resource, key, initFunction, annotationMap);
        }
    }
    // Handle Function Annotations.
    for (BLangFunction function : pkgNode.functions) {
        generateAnnotations(function, function.symbol.name.value, initFunction, annotationMap);
    }
    // Handle Connector Annotations.
    for (BLangConnector connector : pkgNode.connectors) {
        generateAnnotations(connector, connector.name.value, initFunction, annotationMap);
        for (BLangAction action : connector.actions) {
            String key = connector.name.value + DOT + action.name.value;
            generateAnnotations(connector, key, initFunction, annotationMap);
        }
    }
    // Handle Struct Annotations.
    for (BLangStruct struct : pkgNode.structs) {
        generateAnnotations(struct, struct.name.value, initFunction, annotationMap);
        for (BLangVariable field : struct.fields) {
            String key = struct.name.value + DOT + field.name.value;
            generateAnnotations(field, key, initFunction, annotationMap);
        }
    }
    for (BLangEndpoint globalEndpoint : pkgNode.globalEndpoints) {
        generateAnnotations(globalEndpoint, globalEndpoint.name.value, initFunction, annotationMap);
    }
    ASTBuilderUtil.createReturnStmt(pkgNode.pos, initFunction.body);
}
Also used : BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction)

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