use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangTypeConversionExpr conversionExpr) {
// Set error type as the actual type.
// List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
List<BType> actualTypes;
BType targetType = symResolver.resolveTypeNode(conversionExpr.typeNode, env);
conversionExpr.targetType = targetType;
BType sourceType = checkExpr(conversionExpr.expr, env, Lists.of(symTable.noType)).get(0);
if (conversionExpr.transformerInvocation == null) {
// Lookup for built-in type conversion operator symbol
BSymbol symbol = symResolver.resolveConversionOperator(sourceType, targetType);
if (symbol == symTable.notFoundSymbol) {
// If not found, look for unnamed transformers for the given types
actualTypes = checkUnNamedTransformerInvocation(conversionExpr, sourceType, targetType);
} else {
BConversionOperatorSymbol conversionSym = (BConversionOperatorSymbol) symbol;
conversionExpr.conversionSymbol = conversionSym;
actualTypes = getActualTypesOfConversionExpr(conversionExpr, targetType, sourceType, conversionSym);
}
} else {
actualTypes = checkNamedTransformerInvocation(conversionExpr, sourceType, targetType);
}
resultTypes = types.checkTypes(conversionExpr, actualTypes, expTypes);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangTypeConversionExpr convExpr) {
int opcode = convExpr.conversionSymbol.opcode;
// Figure out the reg index of the result value
BType castExprType = convExpr.type;
RegIndex convExprRegIndex = calcAndGetExprRegIndex(convExpr.regIndex, castExprType.tag);
convExpr.regIndex = convExprRegIndex;
if (opcode == InstructionCodes.NOP) {
convExpr.expr.regIndex = createLHSRegIndex(convExprRegIndex);
genNode(convExpr.expr, this.env);
return;
}
genNode(convExpr.expr, this.env);
if (opcode == InstructionCodes.MAP2T || opcode == InstructionCodes.JSON2T || opcode == InstructionCodes.ANY2T || opcode == InstructionCodes.ANY2C || opcode == InstructionCodes.ANY2E || opcode == InstructionCodes.ANY2M || opcode == InstructionCodes.CHECKCAST) {
Operand typeCPIndex = getTypeCPIndex(convExpr.targetType);
emit(opcode, convExpr.expr.regIndex, typeCPIndex, convExprRegIndex);
} else {
emit(opcode, convExpr.expr.regIndex, convExprRegIndex);
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.
the class ASTBuilderUtil method generateConversionExpr.
static BLangExpression generateConversionExpr(BLangExpression varRef, BType target, SymbolResolver symResolver) {
if (varRef.type.tag == target.tag || varRef.type.tag > TypeTags.TYPEDESC) {
return varRef;
}
// Box value using cast expression.
final BLangTypeConversionExpr conversion = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
conversion.pos = varRef.pos;
conversion.expr = varRef;
conversion.type = target;
conversion.targetType = target;
conversion.conversionSymbol = (BConversionOperatorSymbol) symResolver.resolveConversionOperator(varRef.type, target);
return conversion;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangTypeConversionExpr conversionExpr) {
conversionExpr.expr = rewriteExpr(conversionExpr.expr);
// Built-in conversion
if (conversionExpr.conversionSymbol.tag != SymTag.TRANSFORMER) {
result = conversionExpr;
return;
}
// Named transformer invocation
BLangInvocation transformerInvoc = conversionExpr.transformerInvocation;
if (transformerInvoc != null) {
transformerInvoc = rewriteExpr(transformerInvoc);
// Add the rExpr as the first argument
conversionExpr.transformerInvocation.requiredArgs.add(0, conversionExpr.expr);
result = new BLangTransformerInvocation(conversionExpr.pos, transformerInvoc.requiredArgs, transformerInvoc.namedArgs, transformerInvoc.restArgs, transformerInvoc.symbol, conversionExpr.types);
conversionExpr.transformerInvocation = transformerInvoc;
return;
}
// Unnamed transformer invocation
BConversionOperatorSymbol transformerSym = conversionExpr.conversionSymbol;
transformerInvoc = new BLangTransformerInvocation(conversionExpr.pos, Lists.of(conversionExpr.expr), transformerSym, conversionExpr.types);
transformerInvoc.types = transformerSym.type.getReturnTypes();
result = transformerInvoc;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.
the class Desugar method createTypeConversionExpr.
private BLangExpression createTypeConversionExpr(BLangExpression expr, BType sourceType, BType targetType) {
BConversionOperatorSymbol symbol = (BConversionOperatorSymbol) symResolver.resolveConversionOperator(sourceType, targetType);
BLangTypeConversionExpr conversionExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
conversionExpr.pos = expr.pos;
conversionExpr.expr = expr;
conversionExpr.type = targetType;
conversionExpr.conversionSymbol = symbol;
return conversionExpr;
}
Aggregations