use of org.wso2.ballerinalang.programfile.StructInfo in project ballerina by ballerina-lang.
the class ErrorTableEntry method setPackageInfo.
public void setPackageInfo(PackageInfo packageInfo) {
this.packageInfo = packageInfo;
// Load Cache values.
if (errorStructCPIndex < 0) {
return;
}
StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) packageInfo.getCPEntry(errorStructCPIndex);
// this.error = (StructInfo) structureRefCPEntry.getStructureTypeInfo();
}
use of org.wso2.ballerinalang.programfile.StructInfo in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangTryCatchFinally tryNode) {
Operand gotoTryCatchEndAddr = getOperand(-1);
Instruction instructGotoTryCatchEnd = InstructionFactory.get(InstructionCodes.GOTO, gotoTryCatchEndAddr);
List<int[]> unhandledErrorRangeList = new ArrayList<>();
ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
// Handle try block.
int fromIP = nextIP();
genNode(tryNode.tryBody, env);
int toIP = nextIP() - 1;
// Append finally block instructions.
if (tryNode.finallyBody != null) {
genNode(tryNode.finallyBody, env);
}
emit(instructGotoTryCatchEnd);
unhandledErrorRangeList.add(new int[] { fromIP, toIP });
// Handle catch blocks.
int order = 0;
for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
addLineNumberInfo(bLangCatch.pos);
int targetIP = nextIP();
genNode(bLangCatch, env);
unhandledErrorRangeList.add(new int[] { targetIP, nextIP() - 1 });
// Append finally block instructions.
if (tryNode.finallyBody != null) {
genNode(tryNode.finallyBody, env);
}
emit(instructGotoTryCatchEnd);
// Create Error table entry for this catch block
BTypeSymbol structSymbol = bLangCatch.param.symbol.type.tsymbol;
BPackageSymbol packageSymbol = (BPackageSymbol) bLangCatch.param.symbol.type.tsymbol.owner;
int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, packageSymbol.pkgID);
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
int structCPEntryIndex = currentPkgInfo.addCPEntry(structureRefCPEntry);
StructInfo errorStructInfo = this.programFile.packageInfoMap.get(packageSymbol.pkgID.bvmAlias()).getStructInfo(structSymbol.name.value);
ErrorTableEntry errorTableEntry = new ErrorTableEntry(fromIP, toIP, targetIP, order++, structCPEntryIndex);
errorTableEntry.setError(errorStructInfo);
errorTable.addErrorTableEntry(errorTableEntry);
}
if (tryNode.finallyBody != null) {
// Create Error table entry for unhandled errors in try and catch(s) blocks
for (int[] range : unhandledErrorRangeList) {
ErrorTableEntry errorTableEntry = new ErrorTableEntry(range[0], range[1], nextIP(), order++, -1);
errorTable.addErrorTableEntry(errorTableEntry);
}
// Append finally block instruction.
genNode(tryNode.finallyBody, env);
emit(InstructionFactory.get(InstructionCodes.THROW, getOperand(-1)));
}
gotoTryCatchEndAddr.value = nextIP();
}
use of org.wso2.ballerinalang.programfile.StructInfo 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));
}
}
Aggregations