Search in sources :

Example 6 with BLangInvocation

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation in project ballerina by ballerina-lang.

the class IterableAnalyzer method handlerIterableOperation.

public void handlerIterableOperation(BLangInvocation iExpr, BType expectedType, SymbolEnv env) {
    final IterableContext context;
    if (iExpr.expr.type.tag != TypeTags.INTERMEDIATE_COLLECTION) {
        // This is a new iteration chain.
        context = new IterableContext(iExpr.expr, env);
    } else {
        // Get context from previous invocation.
        context = ((BLangInvocation) iExpr.expr).iContext;
    }
    iExpr.iContext = context;
    final IterableKind iterableKind = IterableKind.getFromString(iExpr.name.value);
    final Operation iOperation = new Operation(iterableKind, iExpr, expectedType);
    iExpr.iContext.addOperation(iOperation);
    if (iterableKind.isLambdaRequired()) {
        handleLambdaBasedIterableOperation(context, iOperation);
    } else {
        handleSimpleTerminalOperations(iOperation);
    }
    validateIterableContext(context);
    if (iOperation.resultType != symTable.errType && context.foreachTypes.isEmpty()) {
        calculateForeachTypes(context);
    }
}
Also used : IterableContext(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)

Example 7 with BLangInvocation

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation in project ballerina by ballerina-lang.

the class TypeChecker method checkInvocationParam.

private List<BType> checkInvocationParam(BLangInvocation iExpr) {
    List<BType> paramTypes = ((BInvokableType) iExpr.symbol.type).getParameterTypes();
    int requiredParamsCount;
    if (iExpr.symbol.tag == SymTag.VARIABLE) {
        // Here we assume function pointers can have only required params.
        // And assume that named params and rest params are not supported.
        requiredParamsCount = paramTypes.size();
    } else {
        requiredParamsCount = ((BInvokableSymbol) iExpr.symbol).params.size();
    }
    // Split the different argument types: required args, named args and rest args
    int i = 0;
    BLangExpression vararg = null;
    for (BLangExpression expr : iExpr.argExprs) {
        switch(expr.getKind()) {
            case NAMED_ARGS_EXPR:
                iExpr.namedArgs.add((BLangNamedArgsExpression) expr);
                break;
            case REST_ARGS_EXPR:
                vararg = expr;
                break;
            default:
                if (i < requiredParamsCount) {
                    iExpr.requiredArgs.add(expr);
                } else {
                    iExpr.restArgs.add(expr);
                }
                i++;
                break;
        }
    }
    return checkInvocationArgs(iExpr, paramTypes, requiredParamsCount, vararg);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 8 with BLangInvocation

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation in project ballerina by ballerina-lang.

the class TypeChecker method checkInvocationArgs.

private List<BType> checkInvocationArgs(BLangInvocation iExpr, List<BType> paramTypes, int requiredParamsCount, BLangExpression vararg) {
    List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    BInvokableSymbol invocableSymbol = (BInvokableSymbol) iExpr.symbol;
    // Check whether the expected param count and the actual args counts are matching.
    if (requiredParamsCount > iExpr.requiredArgs.size()) {
        dlog.error(iExpr.pos, DiagnosticCode.NOT_ENOUGH_ARGS_FUNC_CALL, iExpr.name.value);
        return actualTypes;
    } else if (invocableSymbol.restParam == null && (vararg != null || !iExpr.restArgs.isEmpty())) {
        dlog.error(iExpr.pos, DiagnosticCode.TOO_MANY_ARGS_FUNC_CALL, iExpr.name.value);
        return actualTypes;
    }
    // formal parameters of the outer function.
    if (iExpr.argExprs.size() == 1 && iExpr.argExprs.get(0).getKind() == NodeKind.INVOCATION) {
        checkExpr(iExpr.requiredArgs.get(0), this.env, ((BInvokableType) invocableSymbol.type).paramTypes);
    } else {
        checkRequiredArgs(iExpr.requiredArgs, paramTypes);
    }
    checkNamedArgs(iExpr.namedArgs, invocableSymbol.defaultableParams);
    checkRestArgs(iExpr.restArgs, vararg, invocableSymbol.restParam);
    if (iExpr.async) {
        return Arrays.asList(this.generateFutureType(invocableSymbol));
    } else {
        return invocableSymbol.type.getReturnTypes();
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)

Example 9 with BLangInvocation

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation in project ballerina by ballerina-lang.

the class TypeChecker method checkFunctionInvocationExpr.

private void checkFunctionInvocationExpr(BLangInvocation iExpr, BStructType structType) {
    String funcName = iExpr.name.value;
    Name uniqueFuncName = names.fromString(Symbols.getAttachedFuncSymbolName(structType.tsymbol.name.value, funcName));
    BPackageSymbol packageSymbol = (BPackageSymbol) structType.tsymbol.owner;
    BSymbol funcSymbol = symResolver.lookupMemberSymbol(iExpr.pos, packageSymbol.scope, this.env, uniqueFuncName, SymTag.FUNCTION);
    if (funcSymbol == symTable.notFoundSymbol) {
        // Check functions defined within the struct.
        Name functionName = names.fromString(Symbols.getAttachedFuncSymbolName(iExpr.expr.symbol.type.tsymbol.name.value, iExpr.name.value));
        funcSymbol = symResolver.resolveStructField(iExpr.pos, env, functionName, iExpr.expr.symbol.type.tsymbol);
        if (funcSymbol == symTable.notFoundSymbol) {
            // Check, any function pointer in struct field with given name.
            funcSymbol = symResolver.resolveStructField(iExpr.pos, env, names.fromIdNode(iExpr.name), iExpr.expr.symbol.type.tsymbol);
            if (funcSymbol == symTable.notFoundSymbol || funcSymbol.type.tag != TypeTags.INVOKABLE) {
                dlog.error(iExpr.pos, DiagnosticCode.UNDEFINED_FUNCTION_IN_STRUCT, funcName, structType);
                resultTypes = getListWithErrorTypes(expTypes.size());
                return;
            }
            if ((funcSymbol.flags & Flags.ATTACHED) != Flags.ATTACHED) {
                iExpr.functionPointerInvocation = true;
            }
        }
    } else {
        // Attached function found
        // Check for the explicit initializer function invocation
        BStructSymbol.BAttachedFunction initializerFunc = ((BStructSymbol) structType.tsymbol).initializerFunc;
        if (initializerFunc != null && initializerFunc.funcName.value.equals(funcName)) {
            dlog.error(iExpr.pos, DiagnosticCode.STRUCT_INITIALIZER_INVOKED, structType.tsymbol.toString());
        }
    }
    iExpr.symbol = funcSymbol;
    checkInvocationParamAndReturnType(iExpr);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 10 with BLangInvocation

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation in project ballerina by ballerina-lang.

the class TypeChecker method checkActionInvocationExpr.

private void checkActionInvocationExpr(BLangInvocation iExpr, BType conType) {
    List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    if (conType == symTable.errType || conType.tag != TypeTags.STRUCT || iExpr.expr.symbol.tag != SymTag.ENDPOINT) {
        dlog.error(iExpr.pos, DiagnosticCode.INVALID_ACTION_INVOCATION);
        resultTypes = actualTypes;
        return;
    }
    final BEndpointVarSymbol epSymbol = (BEndpointVarSymbol) iExpr.expr.symbol;
    if (!epSymbol.interactable) {
        dlog.error(iExpr.pos, DiagnosticCode.ENDPOINT_NOT_SUPPORT_INTERACTIONS, epSymbol.name);
        resultTypes = actualTypes;
        return;
    }
    BSymbol conSymbol = epSymbol.clientSymbol;
    if (conSymbol == null || conSymbol == symTable.notFoundSymbol || conSymbol == symTable.errSymbol || conSymbol.tag != SymTag.STRUCT) {
        dlog.error(iExpr.pos, DiagnosticCode.INVALID_ACTION_INVOCATION);
        resultTypes = actualTypes;
        return;
    }
    Name actionName = names.fromIdNode(iExpr.name);
    Name uniqueFuncName = names.fromString(Symbols.getAttachedFuncSymbolName(conSymbol.name.value, actionName.value));
    BPackageSymbol packageSymbol = (BPackageSymbol) conSymbol.owner;
    BSymbol actionSym = symResolver.lookupMemberSymbol(iExpr.pos, packageSymbol.scope, this.env, uniqueFuncName, SymTag.FUNCTION);
    if (actionSym == symTable.errSymbol || actionSym == symTable.notFoundSymbol) {
        dlog.error(iExpr.pos, DiagnosticCode.UNDEFINED_ACTION, actionName, epSymbol.name, conSymbol.type);
        resultTypes = actualTypes;
        return;
    }
    iExpr.symbol = actionSym;
    checkInvocationParamAndReturnType(iExpr);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)22 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)12 ArrayList (java.util.ArrayList)11 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)9 Name (org.wso2.ballerinalang.compiler.util.Name)8 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)7 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)6 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)5 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)5 List (java.util.List)4 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)4 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)4 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)4 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)4 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)4 Paths (java.nio.file.Paths)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)3 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)3