use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey 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.BLangRecordKey in project ballerina by ballerina-lang.
the class TypeChecker method checkStructLiteralKeyExpr.
private BType checkStructLiteralKeyExpr(BLangRecordKey key, BType recordType, RecordKind recKind) {
Name fieldName;
BLangExpression keyExpr = key.expr;
if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) keyExpr;
fieldName = names.fromIdNode(varRef.variableName);
} else {
// keys of the struct literal can only be a varRef (identifier)
dlog.error(keyExpr.pos, DiagnosticCode.INVALID_STRUCT_LITERAL_KEY);
return symTable.errType;
}
// Check weather the struct field exists
BSymbol fieldSymbol = symResolver.resolveStructField(keyExpr.pos, this.env, fieldName, recordType.tsymbol);
if (fieldSymbol == symTable.notFoundSymbol) {
dlog.error(keyExpr.pos, DiagnosticCode.UNDEFINED_STRUCT_FIELD, fieldName, recordType.tsymbol);
return symTable.errType;
}
// Setting the struct field symbol for future use in Desugar and code generator.
key.fieldSymbol = (BVarSymbol) fieldSymbol;
return fieldSymbol.type;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey 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