use of org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangUnaryExpr unaryExpr) {
RegIndex exprIndex = calcAndGetExprRegIndex(unaryExpr);
if (OperatorKind.ADD.equals(unaryExpr.operator) || OperatorKind.UNTAINT.equals(unaryExpr.operator)) {
unaryExpr.expr.regIndex = createLHSRegIndex(unaryExpr.regIndex);
genNode(unaryExpr.expr, this.env);
return;
}
int opcode;
genNode(unaryExpr.expr, this.env);
if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) {
opcode = unaryExpr.opSymbol.opcode;
if (opcode == InstructionCodes.TYPEOF) {
emit(opcode, unaryExpr.expr.regIndex, exprIndex);
} else {
Operand typeCPIndex = getTypeCPIndex(unaryExpr.expr.type);
emit(opcode, typeCPIndex, exprIndex);
}
} else if (OperatorKind.LENGTHOF.equals(unaryExpr.operator)) {
Operand typeCPIndex = getTypeCPIndex(unaryExpr.expr.type);
opcode = unaryExpr.opSymbol.opcode;
emit(opcode, unaryExpr.expr.regIndex, typeCPIndex, exprIndex);
} else {
opcode = unaryExpr.opSymbol.opcode;
emit(opcode, unaryExpr.expr.regIndex, exprIndex);
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr in project ballerina by ballerina-lang.
the class ASTBuilderUtil method createUnaryExpr.
static BLangUnaryExpr createUnaryExpr(DiagnosticPos pos, BLangExpression expr, BType type, OperatorKind kind, BOperatorSymbol symbol) {
final BLangUnaryExpr unaryExpr = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
unaryExpr.pos = pos;
unaryExpr.expr = expr;
unaryExpr.type = type;
unaryExpr.operator = kind;
unaryExpr.opSymbol = symbol;
return unaryExpr;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr in project ballerina by ballerina-lang.
the class BLangPackageBuilder method createUnaryExpr.
public void createUnaryExpr(DiagnosticPos pos, Set<Whitespace> ws, String operator) {
BLangUnaryExpr unaryExpressionNode = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
unaryExpressionNode.pos = pos;
unaryExpressionNode.addWS(ws);
unaryExpressionNode.expr = (BLangExpression) exprNodeStack.pop();
unaryExpressionNode.operator = OperatorKind.valueFrom(operator);
addExpressionNode(unaryExpressionNode);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangUnaryExpr unaryExpr) {
BType exprType = null;
BType actualType = symTable.errType;
if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) {
// Handle typeof operator separately
if (unaryExpr.expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) unaryExpr.expr;
Name varRefName = names.fromIdNode((varRef).variableName);
Name pkgAlias = names.fromIdNode((varRef).pkgAlias);
// Resolve symbol for BLangSimpleVarRef
BSymbol varRefSybmol = symResolver.lookupSymbolInPackage(unaryExpr.pos, env, pkgAlias, varRefName, SymTag.VARIABLE);
if (varRefSybmol == symTable.notFoundSymbol) {
// Resolve symbol for User Defined Type ( converted from BLangSimpleVarRef )
BLangTypeofExpr typeAccessExpr = getTypeAccessExpression(varRef);
unaryExpr.expr = typeAccessExpr;
actualType = typeAccessExpr.type;
resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
return;
} else {
// Check type if resolved as BLangSimpleVarRef
exprType = checkExpr(unaryExpr.expr, env).get(0);
}
} else {
// Check type if resolved as non BLangSimpleVarRef Expression
exprType = checkExpr(unaryExpr.expr, env).get(0);
}
if (exprType != symTable.errType) {
unaryExpr.opSymbol = Symbols.createTypeofOperatorSymbol(exprType, types, symTable, names);
actualType = unaryExpr.opSymbol.type.getReturnTypes().get(0);
}
} else {
exprType = checkExpr(unaryExpr.expr, env).get(0);
if (exprType != symTable.errType) {
BSymbol symbol = symResolver.resolveUnaryOperator(unaryExpr.pos, unaryExpr.operator, exprType);
if (symbol == symTable.notFoundSymbol) {
dlog.error(unaryExpr.pos, DiagnosticCode.UNARY_OP_INCOMPATIBLE_TYPES, unaryExpr.operator, exprType);
} else {
unaryExpr.opSymbol = (BOperatorSymbol) symbol;
actualType = symbol.type.getReturnTypes().get(0);
}
}
}
resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr in project ballerina by ballerina-lang.
the class EndpointDesugar method generateServiceRegistered.
private BLangBlockStmt generateServiceRegistered(BEndpointVarSymbol endpoint, BLangService service, SymbolEnv env, BSymbol encSymbol, BSymbol varEncSymbol) {
final DiagnosticPos pos = service.pos;
final String epName = endpoint.name.value;
BLangBlockStmt temp = new BLangBlockStmt();
final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.type);
final Name name = endpoint.name;
epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, name, SymTag.VARIABLE);
final BLangVariableDef serviceTypeDef = ASTBuilderUtil.createVariableDefStmt(pos, temp);
serviceTypeDef.var = ASTBuilderUtil.createVariable(pos, service.name + "type", symTable.typeDesc);
ASTBuilderUtil.defineVariable(serviceTypeDef.var, varEncSymbol, names);
final BLangUnaryExpr typeOfExpr = ASTBuilderUtil.createUnaryExpr(pos);
serviceTypeDef.var.expr = typeOfExpr;
typeOfExpr.operator = OperatorKind.TYPEOF;
typeOfExpr.expr = getTypeAccessExpression(pos, service.symbol.type);
typeOfExpr.type = symTable.typeDesc;
List<BLangVariable> args = Lists.of(serviceTypeDef.var);
if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() == 2) {
// Endpoint is already desugared. Fix this correctly.
args.add(0, epVariable);
}
final BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, temp);
final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(pos, endpoint.registerFunction, args, symResolver);
if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() != 2) {
iExpr.expr = ASTBuilderUtil.createVariableRef(epVariable.pos, epVariable.symbol);
}
expressionStmt.expr = iExpr;
return temp;
}
Aggregations