use of org.wso2.ballerinalang.compiler.semantics.model.types.BMapType in project ballerina by ballerina-lang.
the class Types method checkForeachTypes.
List<BType> checkForeachTypes(BLangNode collection, int variableSize) {
BType collectionType = collection.type;
List<BType> errorTypes;
int maxSupportedTypes;
switch(collectionType.tag) {
case TypeTags.ARRAY:
BArrayType bArrayType = (BArrayType) collectionType;
if (variableSize == 1) {
return Lists.of(bArrayType.eType);
} else if (variableSize == 2) {
return Lists.of(symTable.intType, bArrayType.eType);
} else {
maxSupportedTypes = 2;
errorTypes = Lists.of(symTable.intType, bArrayType.eType);
}
break;
case TypeTags.MAP:
BMapType bMapType = (BMapType) collectionType;
if (variableSize == 1) {
return Lists.of(bMapType.constraint);
} else if (variableSize == 2) {
return Lists.of(symTable.stringType, bMapType.constraint);
} else {
maxSupportedTypes = 2;
errorTypes = Lists.of(symTable.stringType, bMapType.constraint);
}
break;
case TypeTags.JSON:
if (variableSize == 1) {
return Lists.of(symTable.jsonType);
} else {
maxSupportedTypes = 1;
errorTypes = Lists.of(symTable.jsonType);
}
break;
case TypeTags.XML:
if (variableSize == 1) {
return Lists.of(symTable.xmlType);
} else if (variableSize == 2) {
return Lists.of(symTable.intType, symTable.xmlType);
} else {
maxSupportedTypes = 2;
errorTypes = Lists.of(symTable.intType, symTable.xmlType);
}
break;
case TypeTags.TABLE:
BTableType tableType = (BTableType) collectionType;
if (variableSize == 1) {
return Lists.of(tableType.constraint);
} else {
maxSupportedTypes = 1;
errorTypes = Lists.of(tableType.constraint);
}
break;
case TypeTags.ERROR:
return Collections.nCopies(variableSize, symTable.errType);
default:
dlog.error(collection.pos, DiagnosticCode.ITERABLE_NOT_SUPPORTED_COLLECTION, collectionType);
return Collections.nCopies(variableSize, symTable.errType);
}
dlog.error(collection.pos, DiagnosticCode.ITERABLE_TOO_MANY_VARIABLES, collectionType);
errorTypes.addAll(Collections.nCopies(variableSize - maxSupportedTypes, symTable.errType));
return errorTypes;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BMapType in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangMapLiteral mapLiteral) {
Operand mapVarRegIndex = calcAndGetExprRegIndex(mapLiteral);
Operand typeCPIndex = getTypeCPIndex(mapLiteral.type);
emit(InstructionCodes.NEWMAP, mapVarRegIndex, typeCPIndex);
// Handle Map init stuff
for (BLangRecordKeyValue keyValue : mapLiteral.keyValuePairs) {
BLangExpression keyExpr = keyValue.key.expr;
genNode(keyExpr, this.env);
BLangExpression valueExpr = keyValue.valueExpr;
genNode(valueExpr, this.env);
BMapType mapType = (BMapType) mapLiteral.type;
int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
if (opcode == InstructionCodes.NOP) {
emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, valueExpr.regIndex);
} else {
RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
emit(opcode, valueExpr.regIndex, refRegMapValue);
emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, refRegMapValue);
}
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BMapType in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangMapAccessExpr mapKeyAccessExpr) {
boolean variableStore = this.varAssignment;
this.varAssignment = false;
genNode(mapKeyAccessExpr.expr, this.env);
Operand varRefRegIndex = mapKeyAccessExpr.expr.regIndex;
genNode(mapKeyAccessExpr.indexExpr, this.env);
Operand keyRegIndex = mapKeyAccessExpr.indexExpr.regIndex;
BMapType mapType = (BMapType) mapKeyAccessExpr.expr.type;
if (variableStore) {
int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
if (opcode == InstructionCodes.NOP) {
emit(InstructionCodes.MAPSTORE, varRefRegIndex, keyRegIndex, mapKeyAccessExpr.regIndex);
} else {
RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
emit(opcode, mapKeyAccessExpr.regIndex, refRegMapValue);
emit(InstructionCodes.MAPSTORE, varRefRegIndex, keyRegIndex, refRegMapValue);
}
} else {
int opcode = getRefToValueTypeCastOpcode(mapType.constraint.tag);
if (opcode == InstructionCodes.NOP) {
emit(InstructionCodes.MAPLOAD, varRefRegIndex, keyRegIndex, calcAndGetExprRegIndex(mapKeyAccessExpr));
} else {
RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
emit(InstructionCodes.MAPLOAD, varRefRegIndex, keyRegIndex, refRegMapValue);
emit(opcode, refRegMapValue, calcAndGetExprRegIndex(mapKeyAccessExpr));
}
}
this.varAssignment = variableStore;
}
Aggregations