use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.
the class CodeGenerator method createStructInfoEntry.
private void createStructInfoEntry(BLangStruct structNode) {
BStructSymbol structSymbol = (BStructSymbol) structNode.symbol;
// Add Struct name as an UTFCPEntry to the constant pool
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructInfo structInfo = new StructInfo(currentPackageRefCPIndex, structNameCPIndex, structSymbol.flags);
currentPkgInfo.addStructInfo(structSymbol.name.value, structInfo);
structInfo.structType = (BStructType) structSymbol.type;
List<BLangVariable> structFields = structNode.fields;
for (BLangVariable structField : structFields) {
// Create StructFieldInfo Entry
int fieldNameCPIndex = addUTF8CPEntry(currentPkgInfo, structField.name.value);
int sigCPIndex = addUTF8CPEntry(currentPkgInfo, structField.type.getDesc());
StructFieldInfo structFieldInfo = new StructFieldInfo(fieldNameCPIndex, sigCPIndex, structField.symbol.flags);
structFieldInfo.fieldType = structField.type;
// Populate default values
if (structField.expr != null && structField.expr.getKind() == NodeKind.LITERAL) {
DefaultValueAttributeInfo defaultVal = getDefaultValueAttributeInfo((BLangLiteral) structField.expr);
structFieldInfo.addAttributeInfo(AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE, defaultVal);
}
structInfo.fieldInfoEntries.add(structFieldInfo);
structField.symbol.varIndex = getFieldIndex(structField.symbol.type.tag);
}
// Create variable count attribute info
prepareIndexes(fieldIndexes);
int[] fieldCount = new int[] { fieldIndexes.tInt, fieldIndexes.tFloat, fieldIndexes.tString, fieldIndexes.tBoolean, fieldIndexes.tBlob, fieldIndexes.tRef };
addVariableCountAttributeInfo(currentPkgInfo, structInfo, fieldCount);
fieldIndexes = new VariableIndex(FIELD);
// Create attached function info entries
for (BAttachedFunction attachedFunc : structSymbol.attachedFuncs) {
int funcNameCPIndex = addUTF8CPEntry(currentPkgInfo, attachedFunc.funcName.value);
// Remove the first type. The first type is always the type to which the function is attached to
BType[] paramTypes = attachedFunc.type.paramTypes.toArray(new BType[0]);
if (paramTypes.length == 1) {
paramTypes = new BType[0];
} else {
paramTypes = attachedFunc.type.paramTypes.toArray(new BType[0]);
paramTypes = Arrays.copyOfRange(paramTypes, 1, paramTypes.length);
}
int sigCPIndex = addUTF8CPEntry(currentPkgInfo, generateFunctionSig(paramTypes, attachedFunc.type.retTypes.toArray(new BType[0])));
int flags = attachedFunc.symbol.flags;
structInfo.attachedFuncInfoEntries.add(new AttachedFunctionInfo(funcNameCPIndex, sigCPIndex, flags));
}
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.
the class CodeGenerator method createConnectorInfoEntry.
private void createConnectorInfoEntry(BLangConnector connectorNode) {
BConnectorType connectorType = (BConnectorType) connectorNode.symbol.type;
// Add connector name as an UTFCPEntry to the constant pool
int connectorNameCPIndex = addUTF8CPEntry(currentPkgInfo, connectorNode.name.value);
// Create connector info
ConnectorInfo connectorInfo = new ConnectorInfo(currentPackageRefCPIndex, connectorNameCPIndex, connectorNode.symbol.flags);
connectorInfo.paramTypes = connectorType.paramTypes.toArray(new BType[0]);
connectorInfo.signatureCPIndex = addUTF8CPEntry(this.currentPkgInfo, generateConnectorSig(connectorInfo));
// Add connector level variables
int localVarAttNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE.value());
LocalVariableAttributeInfo localVarAttributeInfo = new LocalVariableAttributeInfo(localVarAttNameIndex);
connectorNode.params.forEach(var -> visitVarSymbol(var.symbol, fieldIndexes, localVarAttributeInfo));
connectorNode.varDefs.forEach(var -> visitVarSymbol(var.var.symbol, fieldIndexes, localVarAttributeInfo));
connectorInfo.addAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE, localVarAttributeInfo);
// Create variable count attribute info
prepareIndexes(fieldIndexes);
int[] fieldCount = new int[] { fieldIndexes.tInt, fieldIndexes.tFloat, fieldIndexes.tString, fieldIndexes.tBoolean, fieldIndexes.tBlob, fieldIndexes.tRef };
addVariableCountAttributeInfo(currentPkgInfo, connectorInfo, fieldCount);
// Create the init function info
BLangFunction connectorInitFunction = (BLangFunction) connectorNode.getInitFunction();
createFunctionInfoEntry(connectorInitFunction);
this.currentPkgInfo.connectorInfoMap.put(connectorNode.name.value, connectorInfo);
// Create action info entries for all actions
connectorNode.actions.forEach(res -> createActionInfoEntry(res, connectorInfo));
createActionInfoEntry(connectorNode.initAction, connectorInfo);
fieldIndexes = new VariableIndex(FIELD);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangFieldVarRef fieldVarRef) {
RegIndex fieldIndex = fieldVarRef.symbol.varIndex;
// This is a connector field.
// the connector reference must be stored in the current reference register index.
Operand varRegIndex = getOperand(0);
if (varAssignment) {
int opcode = getOpcode(fieldVarRef.type.tag, InstructionCodes.IFIELDSTORE);
emit(opcode, varRegIndex, fieldIndex, fieldVarRef.regIndex);
return;
}
int opcode = getOpcode(fieldVarRef.type.tag, InstructionCodes.IFIELDLOAD);
RegIndex exprRegIndex = calcAndGetExprRegIndex(fieldVarRef);
emit(opcode, varRegIndex, fieldIndex, exprRegIndex);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.
the class AnnotationDesugar method rewritePackageAnnotations.
protected void rewritePackageAnnotations(BLangPackage pkgNode) {
BLangFunction initFunction = pkgNode.initFunction;
// Remove last return statement. we will add it later. TODO : Fix this properly.
initFunction.body.stmts.remove(initFunction.body.stmts.size() - 1);
// This is the variable which store all package level annotations.
BLangVariable annotationMap = createGlobalAnnotationMapVar(pkgNode);
// handle Service Annotations.
for (BLangService service : pkgNode.services) {
generateAnnotations(service, service.name.value, initFunction, annotationMap);
for (BLangResource resource : service.resources) {
String key = service.name.value + DOT + resource.name.value;
generateAnnotations(resource, key, initFunction, annotationMap);
}
}
// Handle Function Annotations.
for (BLangFunction function : pkgNode.functions) {
generateAnnotations(function, function.symbol.name.value, initFunction, annotationMap);
}
// Handle Connector Annotations.
for (BLangConnector connector : pkgNode.connectors) {
generateAnnotations(connector, connector.name.value, initFunction, annotationMap);
for (BLangAction action : connector.actions) {
String key = connector.name.value + DOT + action.name.value;
generateAnnotations(connector, key, initFunction, annotationMap);
}
}
// Handle Struct Annotations.
for (BLangStruct struct : pkgNode.structs) {
generateAnnotations(struct, struct.name.value, initFunction, annotationMap);
for (BLangVariable field : struct.fields) {
String key = struct.name.value + DOT + field.name.value;
generateAnnotations(field, key, initFunction, annotationMap);
}
}
for (BLangEndpoint globalEndpoint : pkgNode.globalEndpoints) {
generateAnnotations(globalEndpoint, globalEndpoint.name.value, initFunction, annotationMap);
}
ASTBuilderUtil.createReturnStmt(pkgNode.pos, initFunction.body);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangStruct structNode) {
// Add struct level variables to the init function.
structNode.fields.stream().map(field -> {
// then get the default value literal for that particular struct.
if (field.expr == null) {
field.expr = getInitExpr(field.type);
}
return field;
}).filter(field -> field.expr != null).forEachOrdered(field -> {
if (!structNode.initFunction.initFunctionStmts.containsKey(field.symbol)) {
structNode.initFunction.initFunctionStmts.put(field.symbol, (BLangStatement) createAssignmentStmt(field));
}
});
// Adding init statements to the init function.
BLangStatement[] initStmts = structNode.initFunction.initFunctionStmts.values().toArray(new BLangStatement[0]);
for (int i = 0; i < structNode.initFunction.initFunctionStmts.size(); i++) {
structNode.initFunction.body.stmts.add(i, initStmts[i]);
}
result = structNode;
}
Aggregations