use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class CodeAnalyzer method visit.
public void visit(BLangTransformer transformerNode) {
List<BVarSymbol> inputs = new ArrayList<>();
inputs.add(transformerNode.source.symbol);
transformerNode.requiredParams.forEach(param -> inputs.add(param.symbol));
List<BVarSymbol> outputs = new ArrayList<>();
transformerNode.retParams.forEach(param -> outputs.add(param.symbol));
for (BLangStatement stmt : transformerNode.body.stmts) {
switch(stmt.getKind()) {
case VARIABLE_DEF:
BLangVariableDef variableDefStmt = (BLangVariableDef) stmt;
variableDefStmt.var.expr.accept(new TransformerVarRefValidator(outputs, DiagnosticCode.TRANSFORMER_INVALID_OUTPUT_USAGE));
inputs.add(variableDefStmt.var.symbol);
break;
case ASSIGNMENT:
BLangAssignment assignStmt = (BLangAssignment) stmt;
assignStmt.varRefs.forEach(varRef -> {
varRef.accept(new TransformerVarRefValidator(inputs, DiagnosticCode.TRANSFORMER_INVALID_INPUT_UPDATE));
// If the stmt is declared using var, all the variable refs on lhs should be treated as inputs
if (assignStmt.declaredWithVar && varRef.getKind() == NodeKind.SIMPLE_VARIABLE_REF && !inputs.contains(((BLangSimpleVarRef) varRef).symbol)) {
inputs.add(((BLangSimpleVarRef) varRef).symbol);
}
});
assignStmt.expr.accept(new TransformerVarRefValidator(outputs, DiagnosticCode.TRANSFORMER_INVALID_OUTPUT_USAGE));
break;
case EXPRESSION_STATEMENT:
// Here we have assumed that the invocation expression is the only expression-statement available.
// TODO: support other types, once they are implemented.
dlog.error(stmt.pos, DiagnosticCode.INVALID_STATEMENT_IN_TRANSFORMER, "invocation");
break;
default:
dlog.error(stmt.pos, DiagnosticCode.INVALID_STATEMENT_IN_TRANSFORMER, stmt.getKind().name().toLowerCase().replace('_', ' '));
break;
}
}
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class ASTBuilderUtil method createAssignmentStmt.
static BLangAssignment createAssignmentStmt(DiagnosticPos pos, List<BLangExpression> varRefs, BLangExpression rhsExpr, boolean declaredWithVar) {
final BLangAssignment assignment = (BLangAssignment) TreeBuilder.createAssignmentNode();
assignment.pos = pos;
assignment.varRefs = varRefs;
assignment.expr = rhsExpr;
assignment.declaredWithVar = declaredWithVar;
return assignment;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class AnnotationDesugar method createAnnotationMapEntryVar.
private BLangVariable createAnnotationMapEntryVar(String key, BLangVariable annotationMapVar, BLangBlockStmt target, BSymbol parentSymbol) {
// create: map key = {};
final BLangRecordLiteral recordLiteralNode = ASTBuilderUtil.createEmptyRecordLiteral(target.pos, symTable.mapType);
BLangVariable entryVariable = ASTBuilderUtil.createVariable(target.pos, key, recordLiteralNode.type);
entryVariable.expr = recordLiteralNode;
ASTBuilderUtil.defineVariable(entryVariable, parentSymbol, names);
ASTBuilderUtil.createVariableDefStmt(target.pos, target).var = entryVariable;
// create: annotationMapVar["key"] = key;
BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, entryVariable.symbol);
BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
indexAccessNode.pos = target.pos;
indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, key);
indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapVar.symbol);
indexAccessNode.type = recordLiteralNode.type;
assignmentStmt.varRefs.add(indexAccessNode);
return entryVariable;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class AnnotationDesugar method createGlobalAnnotationMapVar.
private BLangVariable createGlobalAnnotationMapVar(BLangPackage pkgNode) {
DiagnosticPos pos = pkgNode.pos;
BLangVariable annotationMap = ASTBuilderUtil.createVariable(pkgNode.pos, ANNOTATION_DATA, symTable.mapType);
ASTBuilderUtil.defineVariable(annotationMap, pkgNode.symbol, names);
pkgNode.addGlobalVariable(annotationMap);
final BLangRecordLiteral recordLiteralNode = ASTBuilderUtil.createEmptyRecordLiteral(pos, symTable.mapType);
final BLangAssignment annMapAssignment = ASTBuilderUtil.createAssignmentStmt(pos, pkgNode.initFunction.body);
annMapAssignment.expr = recordLiteralNode;
annMapAssignment.addVariable(ASTBuilderUtil.createVariableRef(pos, annotationMap.symbol));
return annotationMap;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class AnnotationDesugar method initAnnotation.
private void initAnnotation(BLangAnnotationAttachment attachment, BLangVariable annotationMapEntryVar, BLangBlockStmt target, BSymbol parentSymbol, int index) {
BLangVariable annotationVar = null;
if (attachment.annotationSymbol.attachedType != null) {
// create: AttachedType annotationVar = { annotation-expression }
annotationVar = ASTBuilderUtil.createVariable(attachment.pos, attachment.annotationName.value, attachment.annotationSymbol.attachedType.type);
annotationVar.expr = attachment.expr;
ASTBuilderUtil.defineVariable(annotationVar, parentSymbol, names);
ASTBuilderUtil.createVariableDefStmt(attachment.pos, target).var = annotationVar;
}
// create: annotationMapEntryVar["name$index"] = annotationVar;
BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
if (annotationVar != null) {
assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationVar.symbol);
} else {
assignmentStmt.expr = ASTBuilderUtil.createLiteral(target.pos, symTable.nullType, null);
}
BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
indexAccessNode.pos = target.pos;
indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, attachment.annotationSymbol.bvmAlias() + "$" + index);
indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapEntryVar.symbol);
indexAccessNode.type = annotationMapEntryVar.symbol.type;
assignmentStmt.varRefs.add(indexAccessNode);
}
Aggregations