Search in sources :

Example 16 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateDefaultIfEmpty.

/**
 * Generates following.
 *
 * if(count == 0){
 * return;
 * }
 *
 * @param blockStmt target
 * @param ctx       current context
 */
private void generateDefaultIfEmpty(BLangBlockStmt blockStmt, IterableContext ctx) {
    if (ctx.resultVar.symbol.type.tag > TypeTags.TYPEDESC) {
        return;
    }
    final DiagnosticPos pos = blockStmt.pos;
    final BLangBinaryExpr equality = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
    equality.pos = pos;
    equality.type = symTable.booleanType;
    equality.opKind = OperatorKind.EQUAL;
    equality.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
    equality.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
    equality.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.EQUAL, symTable.intType, symTable.intType);
    final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
    ifNode.expr = equality;
    ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
    if (ctx.resultVar.symbol.type.tag <= TypeTags.FLOAT) {
        final BLangAssignment assign = ASTBuilderUtil.createAssignmentStmt(pos, ifNode.body);
        assign.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
        switch(ctx.resultVar.symbol.type.tag) {
            case TypeTags.INT:
                assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
                break;
            case TypeTags.FLOAT:
                assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, 0D);
                break;
        }
    }
    final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(pos, ifNode.body);
    returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 17 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateFilter.

/**
 * Generates statements for filter operation.
 *
 * if(!lambda(...)){
 * next;
 * }
 *
 * @param blockStmt target
 * @param operation operation instance
 */
private void generateFilter(BLangBlockStmt blockStmt, Operation operation) {
    final DiagnosticPos pos = operation.pos;
    final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
    final BLangUnaryExpr notExpr = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
    notExpr.pos = pos;
    notExpr.operator = OperatorKind.NOT;
    notExpr.opSymbol = (BOperatorSymbol) symResolver.resolveUnaryOperator(pos, notExpr.operator, symTable.booleanType);
    notExpr.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
    notExpr.type = symTable.booleanType;
    ifNode.expr = notExpr;
    ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
    ASTBuilderUtil.createNextStmt(pos, ifNode.body);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf)

Example 18 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateCountAggregator.

/**
 * Generates following.
 *
 * variable = variable + 1;
 *
 * @param blockStmt target
 * @param variable  variable to increment
 */
private void generateCountAggregator(BLangBlockStmt blockStmt, BLangVariable variable) {
    final DiagnosticPos pos = blockStmt.pos;
    // create count = count + 1;
    final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
    add.pos = pos;
    add.type = symTable.intType;
    add.opKind = OperatorKind.ADD;
    add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, variable.symbol);
    add.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 1L);
    add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, symTable.intType, symTable.intType);
    final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, variable.symbol));
    countAdd.expr = add;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 19 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.

the class SignatureTreeVisitor method populateSymbols.

/**
 * Populate the symbols.
 * @param symbolEntries symbol entries
 */
private void populateSymbols(Map<Name, Scope.ScopeEntry> symbolEntries) {
    // TODO: Populate only the visible functions
    this.terminateVisitor = true;
    String identifierAgainst = documentServiceContext.get(SignatureKeys.IDENTIFIER_AGAINST);
    List<SymbolInfo> visibleSymbols = new ArrayList<>();
    /*
          During the first iteration we filter out the functions and if there is, the variable reference against which
          the function is called.
         */
    symbolEntries.forEach((k, v) -> {
        if (v.symbol instanceof BInvokableSymbol && !(v.symbol instanceof BOperatorSymbol) && !v.symbol.getName().getValue().contains("<init>")) {
            SymbolInfo symbolInfo = new SymbolInfo(k.getValue(), v);
            visibleSymbols.add(symbolInfo);
        } else if (v.symbol instanceof BVarSymbol && k.getValue().equals(identifierAgainst)) {
            documentServiceContext.put(SignatureKeys.IDENTIFIER_TYPE, v.symbol.type.toString());
        } else if (v.symbol instanceof BPackageSymbol && k.getValue().equals(identifierAgainst)) {
            documentServiceContext.put(SignatureKeys.IDENTIFIER_PKGID, v.symbol.pkgID.toString());
            documentServiceContext.put(SignatureKeys.IDENTIFIER_TYPE, v.symbol.type.toString());
            visibleSymbols.addAll(this.getInvokableSymbolsInPackage((BPackageSymbol) v.symbol));
        }
    });
    /*
          In this iteration we filter out the functions either having a receiver or otherwise.
          If the identifier against value is a valid value, then check whether the receiver type equals to identifier
          type. If there is no identifier, filter out functions without the receiver
         */
    List<SymbolInfo> filteredSymbols = new ArrayList<>();
    visibleSymbols.forEach(symbolInfo -> {
        BVarSymbol receiver = ((BInvokableSymbol) symbolInfo.getScopeEntry().symbol).receiverSymbol;
        String[] nameTokens = symbolInfo.getSymbolName().split("\\.");
        String funcNameFromSymbol = nameTokens[nameTokens.length - 1];
        String functionName = documentServiceContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
        String identifierPkgName = documentServiceContext.get(SignatureKeys.IDENTIFIER_PKGID);
        boolean onIdentifierTypePkg = "package".equals(documentServiceContext.get(SignatureKeys.IDENTIFIER_TYPE)) && symbolInfo.getScopeEntry().symbol.pkgID.toString().equals(identifierPkgName);
        boolean onReceiverTypeMatchIdentifier = receiver != null && receiver.type.toString().equals(documentServiceContext.get(SignatureKeys.IDENTIFIER_TYPE));
        boolean onIdentifierAgainstNull = (receiver == null && (identifierAgainst == null || identifierAgainst.equals("")));
        if ((onIdentifierTypePkg || onReceiverTypeMatchIdentifier || onIdentifierAgainstNull) && funcNameFromSymbol.equals(functionName)) {
            filteredSymbols.add(symbolInfo);
        }
    });
    documentServiceContext.put(SignatureKeys.FILTERED_FUNCTIONS, filteredSymbols);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ArrayList(java.util.ArrayList) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Aggregations

BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)6 BOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)5 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)5 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)3 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)3 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BLangUnaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr)2 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)2 ArrayList (java.util.ArrayList)1 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)1 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)1 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)1 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangIsAssignableExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIsAssignableExpr)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1