Search in sources :

Example 11 with BStructSymbol

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

the class TypeChecker method visit.

public void visit(BLangTypeInit cIExpr) {
    BType actualType;
    if (cIExpr.userDefinedType != null) {
        actualType = symResolver.resolveTypeNode(cIExpr.userDefinedType, env);
    } else {
        actualType = expTypes.get(0);
    }
    if (actualType == symTable.errType) {
        // TODO dlog error?
        resultTypes = Lists.of(symTable.errType);
        return;
    }
    if (actualType.tag != TypeTags.STRUCT) {
        // TODO dlog error?
        resultTypes = Lists.of(symTable.errType);
        return;
    }
    cIExpr.objectInitInvocation.symbol = ((BStructSymbol) actualType.tsymbol).initializerFunc.symbol;
    checkInvocationParam(cIExpr.objectInitInvocation);
    resultTypes = types.checkTypes(cIExpr, Lists.of(actualType), expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 12 with BStructSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol 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 13 with BStructSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol 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 14 with BStructSymbol

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

the class Desugar method getInitExpr.

private BLangExpression getInitExpr(BType type) {
    // Don't need to create an empty init expressions if the type allows null.
    if (type.isNullable()) {
        return null;
    }
    switch(type.tag) {
        case TypeTags.INT:
        case TypeTags.FLOAT:
        case TypeTags.BOOLEAN:
        case TypeTags.STRING:
        case TypeTags.BLOB:
            // Int, float, boolean, string, blob types will get default values from VM side.
            break;
        case TypeTags.JSON:
            return new BLangJSONLiteral(new ArrayList<>(), type);
        case TypeTags.XML:
            return new BLangXMLSequenceLiteral(type);
        case TypeTags.TABLE:
            return new BLangTableLiteral(type);
        case TypeTags.STREAM:
            return new BLangStreamLiteral(type, null);
        case TypeTags.MAP:
            return new BLangMapLiteral(new ArrayList<>(), type);
        case TypeTags.STRUCT:
            if (((BStructSymbol) type.tsymbol).isObject) {
                return new BLangTypeInit();
            }
            return new BLangStructLiteral(new ArrayList<>(), type);
        case TypeTags.ARRAY:
            BLangArrayLiteral array = new BLangArrayLiteral();
            array.exprs = new ArrayList<>();
            array.type = type;
            return rewriteExpr(array);
        default:
            break;
    }
    return null;
}
Also used : BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) BLangXMLSequenceLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLSequenceLiteral) BLangStreamLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStreamLiteral) BLangMapLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangJSONLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangTableLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangTableLiteral)

Example 15 with BStructSymbol

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

the class TypeChecker method validateStructInitalizer.

private void validateStructInitalizer(DiagnosticPos pos) {
    BStructType bStructType = (BStructType) expTypes.get(0);
    BStructSymbol bStructSymbol = (BStructSymbol) bStructType.tsymbol;
    if (bStructSymbol.initializerFunc == null) {
        return;
    }
    boolean samePkg = this.env.enclPkg.symbol.pkgID == bStructSymbol.pkgID;
    if (!samePkg && Symbols.isPrivate(bStructSymbol.initializerFunc.symbol)) {
        dlog.error(pos, DiagnosticCode.ATTEMPT_CREATE_NON_PUBLIC_INITIALIZER);
    }
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Aggregations

BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)17 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)7 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)6 BAttachedFunction (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction)5 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)5 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)5 ArrayList (java.util.ArrayList)3 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)3 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 List (java.util.List)2 BEndpointVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol)2 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)2 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)2 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)2 BStructField (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField)2 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)2 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)2 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)2 Arrays (java.util.Arrays)1 Set (java.util.Set)1