use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangTypeInit cIExpr) {
BType actualType;
if (cIExpr.userDefinedType != null) {
actualType = symResolver.resolveTypeNode(cIExpr.userDefinedType, env);
} else {
actualType = expTypes.get(0);
}
if (actualType == symTable.errType) {
// TODO dlog error?
resultTypes = Lists.of(symTable.errType);
return;
}
if (actualType.tag != TypeTags.STRUCT) {
// TODO dlog error?
resultTypes = Lists.of(symTable.errType);
return;
}
cIExpr.objectInitInvocation.symbol = ((BStructSymbol) actualType.tsymbol).initializerFunc.symbol;
checkInvocationParam(cIExpr.objectInitInvocation);
resultTypes = types.checkTypes(cIExpr, Lists.of(actualType), expTypes);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit in project ballerina by ballerina-lang.
the class Desugar method getInitExpr.
private BLangExpression getInitExpr(BType type) {
// Don't need to create an empty init expressions if the type allows null.
if (type.isNullable()) {
return null;
}
switch(type.tag) {
case TypeTags.INT:
case TypeTags.FLOAT:
case TypeTags.BOOLEAN:
case TypeTags.STRING:
case TypeTags.BLOB:
// Int, float, boolean, string, blob types will get default values from VM side.
break;
case TypeTags.JSON:
return new BLangJSONLiteral(new ArrayList<>(), type);
case TypeTags.XML:
return new BLangXMLSequenceLiteral(type);
case TypeTags.TABLE:
return new BLangTableLiteral(type);
case TypeTags.STREAM:
return new BLangStreamLiteral(type, null);
case TypeTags.MAP:
return new BLangMapLiteral(new ArrayList<>(), type);
case TypeTags.STRUCT:
if (((BStructSymbol) type.tsymbol).isObject) {
return new BLangTypeInit();
}
return new BLangStructLiteral(new ArrayList<>(), type);
case TypeTags.ARRAY:
BLangArrayLiteral array = new BLangArrayLiteral();
array.exprs = new ArrayList<>();
array.type = type;
return rewriteExpr(array);
default:
break;
}
return null;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangTypeInit cIExpr) {
BSymbol structSymbol = cIExpr.type.tsymbol;
int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, structSymbol.pkgID);
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
Operand structCPIndex = getOperand(currentPkgInfo.addCPEntry(structureRefCPEntry));
// Emit an instruction to create a new struct.
RegIndex structRegIndex = calcAndGetExprRegIndex(cIExpr);
emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
// Invoke the struct initializer here.
Operand[] operands = getFuncOperands(cIExpr.objectInitInvocation);
Operand[] callOperands = new Operand[operands.length + 1];
callOperands[0] = operands[0];
callOperands[1] = operands[1];
callOperands[2] = getOperand(operands[2].value + 1);
callOperands[3] = structRegIndex;
System.arraycopy(operands, 3, callOperands, 4, operands.length - 3);
emit(InstructionCodes.CALL, callOperands);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addTypeInitExpression.
public void addTypeInitExpression(DiagnosticPos pos, Set<Whitespace> ws, String initName, boolean typeAvailable, boolean exprAvailable) {
BLangTypeInit objectInitNode = (BLangTypeInit) TreeBuilder.createObjectInitNode();
objectInitNode.pos = pos;
objectInitNode.addWS(ws);
if (typeAvailable) {
objectInitNode.userDefinedType = (BLangUserDefinedType) typeNodeStack.pop();
}
BLangInvocation invocationNode = (BLangInvocation) TreeBuilder.createInvocationNode();
invocationNode.pos = pos;
invocationNode.addWS(ws);
if (exprAvailable) {
List<ExpressionNode> exprNodes = exprNodeListStack.pop();
Set<Whitespace> cws = commaWsStack.pop();
exprNodes.forEach(exprNode -> {
invocationNode.argExprs.add((BLangExpression) exprNode);
objectInitNode.argsExpr.add((BLangExpression) exprNode);
});
invocationNode.addWS(cws);
objectInitNode.addWS(cws);
}
// TODO check whether pkgName can be be empty
IdentifierNode pkgNameNode = TreeBuilder.createIdentifierNode();
IdentifierNode nameNode = createIdentifier(initName);
BLangNameReference nameReference = new BLangNameReference(pos, ws, pkgNameNode, nameNode);
invocationNode.name = (BLangIdentifier) nameReference.name;
invocationNode.addWS(nameReference.ws);
invocationNode.pkgAlias = (BLangIdentifier) nameReference.pkgAlias;
objectInitNode.objectInitInvocation = invocationNode;
this.addExpressionNode(objectInitNode);
}
Aggregations