Search in sources :

Example 1 with BEnumType

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

the class TypeChecker method visit.

public void visit(BLangFieldBasedAccess fieldAccessExpr) {
    // First analyze the variable reference expression.
    BType actualType = symTable.errType;
    BType varRefType = getTypeOfExprInFieldAccess(fieldAccessExpr.expr);
    if (fieldAccessExpr.fieldType == FieldType.ALL && varRefType.tag != TypeTags.XML) {
        dlog.error(fieldAccessExpr.pos, DiagnosticCode.CANNOT_GET_ALL_FIELDS, varRefType);
    }
    Name fieldName = names.fromIdNode(fieldAccessExpr.field);
    switch(varRefType.tag) {
        case TypeTags.STRUCT:
            actualType = checkStructFieldAccess(fieldAccessExpr, fieldName, varRefType);
            break;
        case TypeTags.MAP:
            actualType = ((BMapType) varRefType).getConstraint();
            break;
        case TypeTags.JSON:
            BType constraintType = ((BJSONType) varRefType).constraint;
            if (constraintType.tag == TypeTags.STRUCT) {
                BType fieldType = checkStructFieldAccess(fieldAccessExpr, fieldName, constraintType);
                // If the type of the field is struct, treat it as constraint JSON type.
                if (fieldType.tag == TypeTags.STRUCT) {
                    actualType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
                    break;
                }
            }
            actualType = symTable.jsonType;
            break;
        case TypeTags.ENUM:
            // Enumerator access expressions only allow enum type name as the first part e.g state.INSTALLED,
            BEnumType enumType = (BEnumType) varRefType;
            if (fieldAccessExpr.expr.getKind() != NodeKind.SIMPLE_VARIABLE_REF || !((BLangSimpleVarRef) fieldAccessExpr.expr).variableName.value.equals(enumType.tsymbol.name.value)) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.INVALID_ENUM_EXPR, enumType.tsymbol.name.value);
                break;
            }
            BSymbol symbol = symResolver.lookupMemberSymbol(fieldAccessExpr.pos, enumType.tsymbol.scope, this.env, fieldName, SymTag.VARIABLE);
            if (symbol == symTable.notFoundSymbol) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.UNDEFINED_SYMBOL, fieldName.value);
                break;
            }
            fieldAccessExpr.symbol = (BVarSymbol) symbol;
            actualType = fieldAccessExpr.expr.type;
            break;
        case TypeTags.XML:
            if (fieldAccessExpr.lhsVar) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
                break;
            }
            actualType = symTable.xmlType;
            break;
        case TypeTags.ERROR:
            // Do nothing
            break;
        default:
            dlog.error(fieldAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_FIELD_ACCESS, varRefType);
    }
    resultTypes = types.checkTypes(fieldAccessExpr, Lists.of(actualType), this.expTypes);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType)

Example 2 with BEnumType

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

the class CodeGenerator method createEnumInfoEntry.

private void createEnumInfoEntry(BLangEnum enumNode) {
    BTypeSymbol enumSymbol = (BTypeSymbol) enumNode.symbol;
    // Add Enum name as an UTFCPEntry to the constant pool
    int enumNameCPIndex = addUTF8CPEntry(currentPkgInfo, enumSymbol.name.value);
    EnumInfo enumInfo = new EnumInfo(currentPackageRefCPIndex, enumNameCPIndex, enumSymbol.flags);
    currentPkgInfo.addEnumInfo(enumSymbol.name.value, enumInfo);
    enumInfo.enumType = (BEnumType) enumSymbol.type;
    for (int i = 0; i < enumNode.enumerators.size(); i++) {
        BLangEnumerator enumeratorNode = enumNode.enumerators.get(i);
        enumeratorNode.symbol.varIndex = new RegIndex(i, enumSymbol.type.tag);
        enumeratorNode.symbol.varIndex.isVarIndex = true;
        int enumeratorNameCPIndex = addUTF8CPEntry(currentPkgInfo, enumeratorNode.symbol.name.toString());
        EnumeratorInfo enumeratorInfo = new EnumeratorInfo(enumeratorNameCPIndex, i, enumInfo.enumType);
        enumInfo.enumeratorInfoList.add(enumeratorInfo);
    }
}
Also used : EnumInfo(org.wso2.ballerinalang.programfile.EnumInfo) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BLangEnumerator(org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator) EnumeratorInfo(org.wso2.ballerinalang.programfile.EnumeratorInfo) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 3 with BEnumType

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

the class SymbolEnter method visit.

@Override
public void visit(BLangEnum enumNode) {
    BTypeSymbol enumSymbol = Symbols.createEnumSymbol(Flags.asMask(enumNode.flagSet), names.fromIdNode(enumNode.name), env.enclPkg.symbol.pkgID, null, env.scope.owner);
    enumNode.symbol = enumSymbol;
    defineSymbol(enumNode.pos, enumSymbol);
    BEnumType enumType = new BEnumType(enumSymbol, null);
    enumSymbol.type = enumType;
    SymbolEnv enumEnv = SymbolEnv.createPkgLevelSymbolEnv(enumNode, enumSymbol.scope, this.env);
    for (int i = 0; i < enumNode.enumerators.size(); i++) {
        BLangEnumerator enumerator = enumNode.enumerators.get(i);
        BVarSymbol enumeratorSymbol = new BVarSymbol(Flags.PUBLIC, names.fromIdNode(enumerator.name), enumSymbol.pkgID, enumType, enumSymbol);
        enumeratorSymbol.docTag = DocTag.FIELD;
        enumerator.symbol = enumeratorSymbol;
        if (symResolver.checkForUniqueSymbol(enumerator.pos, enumEnv, enumeratorSymbol, enumeratorSymbol.tag)) {
            enumEnv.scope.define(enumeratorSymbol.name, enumeratorSymbol);
        }
    }
}
Also used : BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangEnumerator(org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType)

Aggregations

BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)2 BEnumType (org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangEnumerator (org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator)2 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)1 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)1 Name (org.wso2.ballerinalang.compiler.util.Name)1 EnumInfo (org.wso2.ballerinalang.programfile.EnumInfo)1 EnumeratorInfo (org.wso2.ballerinalang.programfile.EnumeratorInfo)1 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)1