Search in sources :

Example 6 with BStructType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BStructType 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 7 with BStructType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BStructType in project ballerina by ballerina-lang.

the class Types method checkEquivalencyOfTwoPrivateStructs.

private boolean checkEquivalencyOfTwoPrivateStructs(BStructType lhsType, BStructType rhsType) {
    for (int fieldCounter = 0; fieldCounter < lhsType.fields.size(); fieldCounter++) {
        BStructField lhsField = lhsType.fields.get(fieldCounter);
        BStructField rhsField = rhsType.fields.get(fieldCounter);
        if (lhsField.name.equals(rhsField.name) && isSameType(rhsField.type, lhsField.type)) {
            continue;
        }
        return false;
    }
    BStructSymbol lhsStructSymbol = (BStructSymbol) lhsType.tsymbol;
    List<BAttachedFunction> lhsFuncs = lhsStructSymbol.attachedFuncs;
    List<BAttachedFunction> rhsFuncs = ((BStructSymbol) rhsType.tsymbol).attachedFuncs;
    int lhsAttachedFuncCount = lhsStructSymbol.initializerFunc != null ? lhsFuncs.size() - 1 : lhsFuncs.size();
    if (lhsAttachedFuncCount > rhsFuncs.size()) {
        return false;
    }
    for (BAttachedFunction lhsFunc : lhsFuncs) {
        if (lhsFunc == lhsStructSymbol.initializerFunc || lhsFunc == lhsStructSymbol.defaultsValuesInitFunc) {
            continue;
        }
        BAttachedFunction rhsFunc = getMatchingInvokableType(rhsFuncs, lhsFunc);
        if (rhsFunc == null) {
            return false;
        }
    }
    return true;
}
Also used : BStructField(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 8 with BStructType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BStructType in project ballerina by ballerina-lang.

the class Types method checkEquivalencyOfPublicStructs.

private boolean checkEquivalencyOfPublicStructs(BStructType lhsType, BStructType rhsType) {
    int fieldCounter = 0;
    for (; fieldCounter < lhsType.fields.size(); fieldCounter++) {
        BStructField lhsField = lhsType.fields.get(fieldCounter);
        BStructField rhsField = rhsType.fields.get(fieldCounter);
        if (Symbols.isPrivate(lhsField.symbol) || Symbols.isPrivate(rhsField.symbol)) {
            return false;
        }
        if (lhsField.name.equals(rhsField.name) && isSameType(rhsField.type, lhsField.type)) {
            continue;
        }
        return false;
    }
    // Check the rest of the fields in RHS type
    for (; fieldCounter < rhsType.fields.size(); fieldCounter++) {
        if (Symbols.isPrivate(rhsType.fields.get(fieldCounter).symbol)) {
            return false;
        }
    }
    BStructSymbol lhsStructSymbol = (BStructSymbol) lhsType.tsymbol;
    List<BAttachedFunction> lhsFuncs = lhsStructSymbol.attachedFuncs;
    List<BAttachedFunction> rhsFuncs = ((BStructSymbol) rhsType.tsymbol).attachedFuncs;
    int lhsAttachedFuncCount = lhsStructSymbol.initializerFunc != null ? lhsFuncs.size() - 1 : lhsFuncs.size();
    if (lhsAttachedFuncCount > rhsFuncs.size()) {
        return false;
    }
    for (BAttachedFunction lhsFunc : lhsFuncs) {
        if (lhsFunc == lhsStructSymbol.initializerFunc || lhsFunc == lhsStructSymbol.defaultsValuesInitFunc) {
            continue;
        }
        if (Symbols.isPrivate(lhsFunc.symbol)) {
            return false;
        }
        BAttachedFunction rhsFunc = getMatchingInvokableType(rhsFuncs, lhsFunc);
        if (rhsFunc == null || Symbols.isPrivate(rhsFunc.symbol)) {
            return false;
        }
    }
    // Check for private attached function of the RHS type
    for (BAttachedFunction rhsFunc : rhsFuncs) {
        if (Symbols.isPrivate(rhsFunc.symbol)) {
            return false;
        }
    }
    return true;
}
Also used : BStructField(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 9 with BStructType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BStructType in project ballerina by ballerina-lang.

the class Desugar method getReturnType.

private BLangStructLiteral getReturnType(BLangTableQueryExpression tableQueryExpression) {
    // create a literal to represent the sql query.
    BTableType tableType = (BTableType) tableQueryExpression.type;
    BStructType structType = (BStructType) tableType.constraint;
    return new BLangStructLiteral(new ArrayList<>(), structType);
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Example 10 with BStructType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BStructType in project ballerina by ballerina-lang.

the class BLangMatchContextResolver method resolveItems.

@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    BLangNode symbolEnvNode = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
    List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
    if (!(symbolEnvNode instanceof BLangMatch)) {
        return completionItems;
    }
    BLangMatch bLangMatch = (BLangMatch) symbolEnvNode;
    if (bLangMatch.expr.type instanceof BUnionType) {
        Set<BType> memberTypes = ((BUnionType) ((BLangSimpleVarRef) bLangMatch.expr).type).getMemberTypes();
        memberTypes.forEach(bType -> {
            completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
        });
    } else if (bLangMatch.expr.type instanceof BJSONType) {
        ArrayList<Integer> typeTagsList = new ArrayList<>(Arrays.asList(TypeTags.INT, TypeTags.FLOAT, TypeTags.BOOLEAN, TypeTags.STRING, TypeTags.NULL, TypeTags.JSON));
        List<SymbolInfo> filteredBasicTypes = visibleSymbols.stream().filter(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            return bSymbol instanceof BTypeSymbol && typeTagsList.contains(bSymbol.getType().tag);
        }).collect(Collectors.toList());
        this.populateCompletionItemList(filteredBasicTypes, completionItems);
    } else {
        if (bLangMatch.expr.type instanceof BStructType) {
            List<SymbolInfo> structSymbols = visibleSymbols.stream().filter(symbolInfo -> {
                BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
                return bSymbol instanceof BStructSymbol && !bSymbol.getName().getValue().startsWith(UtilSymbolKeys.ANON_STRUCT_CHECKER);
            }).collect(Collectors.toList());
            this.populateCompletionItemList(structSymbols, completionItems);
        }
    }
    return completionItems;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) ArrayList(java.util.ArrayList) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionItem(org.eclipse.lsp4j.CompletionItem) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)13 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)12 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)8 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)7 BAttachedFunction (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction)6 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)6 List (java.util.List)4 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)3 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 Scope (org.wso2.ballerinalang.compiler.semantics.model.Scope)3 BStructField (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField)3 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)3 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)3 Name (org.wso2.ballerinalang.compiler.util.Name)3 EnumSet (java.util.EnumSet)2 XMLConstants (javax.xml.XMLConstants)2