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);
}
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);
}
}
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);
}
}
}
Aggregations