use of org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef 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.BLangSimpleVarRef 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.expressions.BLangSimpleVarRef in project ballerina by ballerina-lang.
the class SemanticAnalyzer method handleForeachVariables.
// Private methods
private void handleForeachVariables(BLangForeach foreachStmt, List<BType> varTypes, SymbolEnv env) {
for (int i = 0; i < foreachStmt.varRefs.size(); i++) {
BLangExpression varRef = foreachStmt.varRefs.get(i);
// foreach variables supports only simpleVarRef expressions only.
if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
dlog.error(varRef.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, varRef);
continue;
}
BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
simpleVarRef.lhsVar = true;
Name varName = names.fromIdNode(simpleVarRef.variableName);
if (varName == Names.IGNORE) {
simpleVarRef.type = this.symTable.noType;
typeChecker.checkExpr(simpleVarRef, env);
continue;
}
// Check variable symbol for existence.
BSymbol symbol = symResolver.lookupSymbol(env, varName, SymTag.VARIABLE);
if (symbol == symTable.notFoundSymbol) {
symbolEnter.defineVarSymbol(simpleVarRef.pos, Collections.emptySet(), varTypes.get(i), varName, env);
typeChecker.checkExpr(simpleVarRef, env);
} else {
dlog.error(simpleVarRef.pos, DiagnosticCode.REDECLARED_SYMBOL, varName);
}
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef in project ballerina by ballerina-lang.
the class TypeChecker method getTypeAccessExpression.
private BLangTypeofExpr getTypeAccessExpression(BLangSimpleVarRef varRef) {
BLangUserDefinedType userDefinedType = new BLangUserDefinedType();
userDefinedType.pkgAlias = varRef.pkgAlias;
userDefinedType.typeName = varRef.variableName;
userDefinedType.pos = varRef.pos;
BLangTypeofExpr typeAccessExpr = (BLangTypeofExpr) TreeBuilder.createTypeAccessNode();
typeAccessExpr.typeNode = userDefinedType;
typeAccessExpr.resolvedType = symResolver.resolveTypeNode(userDefinedType, env);
typeAccessExpr.pos = varRef.pos;
typeAccessExpr.type = symTable.typeDesc;
return typeAccessExpr;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef in project ballerina by ballerina-lang.
the class TypeChecker method getTypeOfExprInFieldAccess.
private BType getTypeOfExprInFieldAccess(BLangExpression expr) {
// First check whether variable expression is of type enum.
if (expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) expr;
BSymbol symbol = symResolver.lookupSymbolInPackage(varRef.pos, env, names.fromIdNode(varRef.pkgAlias), names.fromIdNode(varRef.variableName), SymTag.ENUM);
if (symbol != symTable.notFoundSymbol) {
expr.type = symbol.type;
return symbol.type;
}
}
checkExpr(expr, this.env, Lists.of(symTable.noType));
return expr.type;
}
Aggregations