use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateDefaultIfEmpty.
/**
* Generates following.
*
* if(count == 0){
* return;
* }
*
* @param blockStmt target
* @param ctx current context
*/
private void generateDefaultIfEmpty(BLangBlockStmt blockStmt, IterableContext ctx) {
if (ctx.resultVar.symbol.type.tag > TypeTags.TYPEDESC) {
return;
}
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr equality = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
equality.pos = pos;
equality.type = symTable.booleanType;
equality.opKind = OperatorKind.EQUAL;
equality.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
equality.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
equality.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.EQUAL, symTable.intType, symTable.intType);
final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
ifNode.expr = equality;
ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
if (ctx.resultVar.symbol.type.tag <= TypeTags.FLOAT) {
final BLangAssignment assign = ASTBuilderUtil.createAssignmentStmt(pos, ifNode.body);
assign.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
switch(ctx.resultVar.symbol.type.tag) {
case TypeTags.INT:
assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
break;
case TypeTags.FLOAT:
assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, 0D);
break;
}
}
final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(pos, ifNode.body);
returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateFilter.
/**
* Generates statements for filter operation.
*
* if(!lambda(...)){
* next;
* }
*
* @param blockStmt target
* @param operation operation instance
*/
private void generateFilter(BLangBlockStmt blockStmt, Operation operation) {
final DiagnosticPos pos = operation.pos;
final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
final BLangUnaryExpr notExpr = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
notExpr.pos = pos;
notExpr.operator = OperatorKind.NOT;
notExpr.opSymbol = (BOperatorSymbol) symResolver.resolveUnaryOperator(pos, notExpr.operator, symTable.booleanType);
notExpr.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
notExpr.type = symTable.booleanType;
ifNode.expr = notExpr;
ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
ASTBuilderUtil.createNextStmt(pos, ifNode.body);
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCountAggregator.
/**
* Generates following.
*
* variable = variable + 1;
*
* @param blockStmt target
* @param variable variable to increment
*/
private void generateCountAggregator(BLangBlockStmt blockStmt, BLangVariable variable) {
final DiagnosticPos pos = blockStmt.pos;
// create count = count + 1;
final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
add.pos = pos;
add.type = symTable.intType;
add.opKind = OperatorKind.ADD;
add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, variable.symbol);
add.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 1L);
add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, symTable.intType, symTable.intType);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, variable.symbol));
countAdd.expr = add;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class SignatureTreeVisitor method populateSymbols.
/**
* Populate the symbols.
* @param symbolEntries symbol entries
*/
private void populateSymbols(Map<Name, Scope.ScopeEntry> symbolEntries) {
// TODO: Populate only the visible functions
this.terminateVisitor = true;
String identifierAgainst = documentServiceContext.get(SignatureKeys.IDENTIFIER_AGAINST);
List<SymbolInfo> visibleSymbols = new ArrayList<>();
/*
During the first iteration we filter out the functions and if there is, the variable reference against which
the function is called.
*/
symbolEntries.forEach((k, v) -> {
if (v.symbol instanceof BInvokableSymbol && !(v.symbol instanceof BOperatorSymbol) && !v.symbol.getName().getValue().contains("<init>")) {
SymbolInfo symbolInfo = new SymbolInfo(k.getValue(), v);
visibleSymbols.add(symbolInfo);
} else if (v.symbol instanceof BVarSymbol && k.getValue().equals(identifierAgainst)) {
documentServiceContext.put(SignatureKeys.IDENTIFIER_TYPE, v.symbol.type.toString());
} else if (v.symbol instanceof BPackageSymbol && k.getValue().equals(identifierAgainst)) {
documentServiceContext.put(SignatureKeys.IDENTIFIER_PKGID, v.symbol.pkgID.toString());
documentServiceContext.put(SignatureKeys.IDENTIFIER_TYPE, v.symbol.type.toString());
visibleSymbols.addAll(this.getInvokableSymbolsInPackage((BPackageSymbol) v.symbol));
}
});
/*
In this iteration we filter out the functions either having a receiver or otherwise.
If the identifier against value is a valid value, then check whether the receiver type equals to identifier
type. If there is no identifier, filter out functions without the receiver
*/
List<SymbolInfo> filteredSymbols = new ArrayList<>();
visibleSymbols.forEach(symbolInfo -> {
BVarSymbol receiver = ((BInvokableSymbol) symbolInfo.getScopeEntry().symbol).receiverSymbol;
String[] nameTokens = symbolInfo.getSymbolName().split("\\.");
String funcNameFromSymbol = nameTokens[nameTokens.length - 1];
String functionName = documentServiceContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
String identifierPkgName = documentServiceContext.get(SignatureKeys.IDENTIFIER_PKGID);
boolean onIdentifierTypePkg = "package".equals(documentServiceContext.get(SignatureKeys.IDENTIFIER_TYPE)) && symbolInfo.getScopeEntry().symbol.pkgID.toString().equals(identifierPkgName);
boolean onReceiverTypeMatchIdentifier = receiver != null && receiver.type.toString().equals(documentServiceContext.get(SignatureKeys.IDENTIFIER_TYPE));
boolean onIdentifierAgainstNull = (receiver == null && (identifierAgainst == null || identifierAgainst.equals("")));
if ((onIdentifierTypePkg || onReceiverTypeMatchIdentifier || onIdentifierAgainstNull) && funcNameFromSymbol.equals(functionName)) {
filteredSymbols.add(symbolInfo);
}
});
documentServiceContext.put(SignatureKeys.FILTERED_FUNCTIONS, filteredSymbols);
}
Aggregations