use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangJSONLiteral jsonLiteral) {
jsonLiteral.regIndex = calcAndGetExprRegIndex(jsonLiteral);
Operand typeCPIndex = getTypeCPIndex(jsonLiteral.type);
emit(InstructionCodes.NEWJSON, jsonLiteral.regIndex, typeCPIndex);
for (BLangRecordKeyValue keyValue : jsonLiteral.keyValuePairs) {
BLangExpression keyExpr = keyValue.key.expr;
genNode(keyExpr, this.env);
BLangExpression valueExpr = keyValue.valueExpr;
genNode(valueExpr, this.env);
emit(InstructionCodes.JSONSTORE, jsonLiteral.regIndex, keyExpr.regIndex, valueExpr.regIndex);
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addKeyValueRecord.
public void addKeyValueRecord(Set<Whitespace> ws) {
BLangRecordKeyValue keyValue = (BLangRecordKeyValue) TreeBuilder.createRecordKeyValue();
keyValue.addWS(ws);
keyValue.valueExpr = (BLangExpression) exprNodeStack.pop();
keyValue.key = new BLangRecordKey((BLangExpression) exprNodeStack.pop());
recordLiteralNodes.peek().keyValuePairs.add(keyValue);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.
the class TypeChecker method checkRecLiteralKeyValue.
private void checkRecLiteralKeyValue(BLangRecordKeyValue keyValuePair, BType recType) {
BType fieldType = symTable.errType;
BLangExpression valueExpr = keyValuePair.valueExpr;
switch(recType.tag) {
case TypeTags.STRUCT:
fieldType = checkStructLiteralKeyExpr(keyValuePair.key, recType, RecordKind.STRUCT);
break;
case TypeTags.MAP:
fieldType = checkMapLiteralKeyExpr(keyValuePair.key.expr, recType, RecordKind.MAP);
break;
case TypeTags.JSON:
fieldType = checkJSONLiteralKeyExpr(keyValuePair.key, recType, RecordKind.JSON);
// If the field is again a struct, treat that literal expression as another constraint JSON.
if (fieldType.tag == TypeTags.STRUCT) {
fieldType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
}
// First visit the expression having field type, as the expected type.
checkExpr(valueExpr, this.env, Lists.of(fieldType));
// Again check the type compatibility with JSON
if (valueExpr.impConversionExpr == null) {
types.checkTypes(valueExpr, Lists.of(valueExpr.type), Lists.of(symTable.jsonType));
} else {
BType valueType = valueExpr.type;
types.checkTypes(valueExpr, valueExpr.impConversionExpr.types, Lists.of(symTable.jsonType));
valueExpr.type = valueType;
}
resultTypes = Lists.of(valueExpr.type);
return;
}
checkExpr(valueExpr, this.env, Lists.of(fieldType));
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue 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.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangStructLiteral structLiteral) {
BStructSymbol structSymbol = (BStructSymbol) structLiteral.type.tsymbol;
int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, structSymbol.pkgID);
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
Operand structCPIndex = getOperand(currentPkgInfo.addCPEntry(structureRefCPEntry));
// Emit an instruction to create a new struct.
RegIndex structRegIndex = calcAndGetExprRegIndex(structLiteral);
emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
// Invoke the struct default values init function here.
if (structSymbol.defaultsValuesInitFunc != null) {
int funcRefCPIndex = getFuncRefCPIndex(structSymbol.defaultsValuesInitFunc.symbol);
// call funcRefCPIndex 1 structRegIndex 0
Operand[] operands = new Operand[5];
operands[0] = getOperand(funcRefCPIndex);
operands[1] = getOperand(false);
operands[2] = getOperand(1);
operands[3] = structRegIndex;
operands[4] = getOperand(0);
emit(InstructionCodes.CALL, operands);
}
// Invoke the struct initializer here.
if (structLiteral.initializer != null) {
int funcRefCPIndex = getFuncRefCPIndex(structLiteral.initializer.symbol);
// call funcRefCPIndex 1 structRegIndex 0
Operand[] operands = new Operand[5];
operands[0] = getOperand(funcRefCPIndex);
operands[1] = getOperand(false);
operands[2] = getOperand(1);
operands[3] = structRegIndex;
operands[4] = getOperand(0);
emit(InstructionCodes.CALL, operands);
}
// Generate code the struct literal.
for (BLangRecordKeyValue keyValue : structLiteral.keyValuePairs) {
BLangRecordKey key = keyValue.key;
Operand fieldIndex = key.fieldSymbol.varIndex;
genNode(keyValue.valueExpr, this.env);
int opcode = getOpcode(key.fieldSymbol.type.tag, InstructionCodes.IFIELDSTORE);
emit(opcode, structRegIndex, fieldIndex, keyValue.valueExpr.regIndex);
}
}
Aggregations