use of org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo in project ballerina by ballerina-lang.
the class CPU method convertMapToStruct.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void convertMapToStruct(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
int i = operands[0];
int cpIndex = operands[1];
int j = operands[2];
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
BMap<String, BValue> bMap = (BMap<String, BValue>) sf.refRegs[i];
if (bMap == null) {
handleNullRefError(ctx);
return;
}
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int blobRegIndex = -1;
int refRegIndex = -1;
BStructType structType = (BStructType) typeRefCPEntry.getType();
BStruct bStruct = new BStruct(structType);
StructInfo structInfo = ctx.callableUnitInfo.getPackageInfo().getStructInfo(structType.getName());
Set<String> keys = bMap.keySet();
for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) {
String key = fieldInfo.getName();
BType fieldType = fieldInfo.getFieldType();
BValue mapVal = null;
try {
boolean containsField = keys.contains(key);
DefaultValueAttributeInfo defaultValAttrInfo = null;
if (containsField) {
mapVal = bMap.get(key);
if (mapVal == null && BTypes.isValueType(fieldType)) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, null);
}
if (mapVal != null && !checkCast(mapVal, fieldType)) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, mapVal.getType());
}
} else {
defaultValAttrInfo = (DefaultValueAttributeInfo) getAttributeInfo(fieldInfo, AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE);
}
switch(fieldType.getTag()) {
case TypeTags.INT_TAG:
longRegIndex++;
if (containsField) {
bStruct.setIntField(longRegIndex, ((BInteger) mapVal).intValue());
} else if (defaultValAttrInfo != null) {
bStruct.setIntField(longRegIndex, defaultValAttrInfo.getDefaultValue().getIntValue());
}
break;
case TypeTags.FLOAT_TAG:
doubleRegIndex++;
if (containsField) {
bStruct.setFloatField(doubleRegIndex, ((BFloat) mapVal).floatValue());
} else if (defaultValAttrInfo != null) {
bStruct.setFloatField(doubleRegIndex, defaultValAttrInfo.getDefaultValue().getFloatValue());
}
break;
case TypeTags.STRING_TAG:
stringRegIndex++;
if (containsField) {
bStruct.setStringField(stringRegIndex, ((BString) mapVal).stringValue());
} else if (defaultValAttrInfo != null) {
bStruct.setStringField(stringRegIndex, defaultValAttrInfo.getDefaultValue().getStringValue());
}
break;
case TypeTags.BOOLEAN_TAG:
booleanRegIndex++;
if (containsField) {
bStruct.setBooleanField(booleanRegIndex, ((BBoolean) mapVal).booleanValue() ? 1 : 0);
} else if (defaultValAttrInfo != null) {
bStruct.setBooleanField(booleanRegIndex, defaultValAttrInfo.getDefaultValue().getBooleanValue() ? 1 : 0);
}
break;
case TypeTags.BLOB_TAG:
blobRegIndex++;
if (containsField && mapVal != null) {
bStruct.setBlobField(blobRegIndex, ((BBlob) mapVal).blobValue());
}
break;
default:
bStruct.setRefField(++refRegIndex, (BRefType) mapVal);
}
} catch (BallerinaException e) {
sf.refRegs[j] = null;
String errorMsg = "cannot convert '" + bMap.getType() + "' to type '" + structType + ": " + e.getMessage();
handleTypeConversionError(ctx, sf, j, errorMsg);
return;
}
}
sf.refRegs[j] = bStruct;
}
use of org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo in project ballerina by ballerina-lang.
the class ProgramFileReader method getAttributeInfo.
private AttributeInfo getAttributeInfo(DataInputStream dataInStream, ConstantPool constantPool) throws IOException {
int attribNameCPIndex = dataInStream.readInt();
UTF8CPEntry attribNameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(attribNameCPIndex);
AttributeInfo.Kind attribKind = AttributeInfo.Kind.fromString(attribNameCPEntry.getValue());
if (attribKind == null) {
throw new ProgramFileFormatException("unknown attribute kind " + attribNameCPEntry.getValue());
}
switch(attribKind) {
case CODE_ATTRIBUTE:
CodeAttributeInfo codeAttributeInfo = new CodeAttributeInfo();
codeAttributeInfo.setAttributeNameIndex(attribNameCPIndex);
codeAttributeInfo.setCodeAddrs(dataInStream.readInt());
codeAttributeInfo.setMaxLongLocalVars(dataInStream.readUnsignedShort());
codeAttributeInfo.setMaxDoubleLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxStringLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxIntLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxByteLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxRefLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxLongRegs(dataInStream.readShort());
codeAttributeInfo.setMaxDoubleRegs(dataInStream.readShort());
codeAttributeInfo.setMaxStringRegs(dataInStream.readShort());
codeAttributeInfo.setMaxIntRegs(dataInStream.readShort());
codeAttributeInfo.setMaxByteRegs(dataInStream.readShort());
codeAttributeInfo.setMaxRefRegs(dataInStream.readShort());
return codeAttributeInfo;
case VARIABLE_TYPE_COUNT_ATTRIBUTE:
VarTypeCountAttributeInfo varCountAttributeInfo = new VarTypeCountAttributeInfo(attribNameCPIndex);
varCountAttributeInfo.setMaxLongVars(dataInStream.readShort());
varCountAttributeInfo.setMaxDoubleVars(dataInStream.readShort());
varCountAttributeInfo.setMaxStringVars(dataInStream.readShort());
varCountAttributeInfo.setMaxIntVars(dataInStream.readShort());
varCountAttributeInfo.setMaxByteVars(dataInStream.readShort());
varCountAttributeInfo.setMaxRefVars(dataInStream.readShort());
return varCountAttributeInfo;
case ERROR_TABLE:
ErrorTableAttributeInfo tableAttributeInfo = new ErrorTableAttributeInfo(attribNameCPIndex);
int tableEntryCount = dataInStream.readShort();
for (int i = 0; i < tableEntryCount; i++) {
int ipFrom = dataInStream.readInt();
int ipTo = dataInStream.readInt();
int ipTarget = dataInStream.readInt();
int priority = dataInStream.readInt();
int errorStructCPIndex = dataInStream.readInt();
ErrorTableEntry tableEntry = new ErrorTableEntry(ipFrom, ipTo, ipTarget, priority, errorStructCPIndex);
if (errorStructCPIndex != -1) {
StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) constantPool.getCPEntry(errorStructCPIndex);
tableEntry.setError((StructInfo) structureRefCPEntry.getStructureTypeInfo());
}
tableAttributeInfo.addErrorTableEntry(tableEntry);
}
return tableAttributeInfo;
case LOCAL_VARIABLES_ATTRIBUTE:
LocalVariableAttributeInfo localVarAttrInfo = new LocalVariableAttributeInfo(attribNameCPIndex);
int localVarInfoCount = dataInStream.readShort();
for (int i = 0; i < localVarInfoCount; i++) {
LocalVariableInfo localVariableInfo = getLocalVariableInfo(dataInStream, constantPool);
localVarAttrInfo.addLocalVarInfo(localVariableInfo);
}
return localVarAttrInfo;
case LINE_NUMBER_TABLE_ATTRIBUTE:
LineNumberTableAttributeInfo lnNoTblAttrInfo = new LineNumberTableAttributeInfo(attribNameCPIndex);
int lineNoInfoCount = dataInStream.readShort();
for (int i = 0; i < lineNoInfoCount; i++) {
LineNumberInfo lineNumberInfo = getLineNumberInfo(dataInStream, constantPool);
lnNoTblAttrInfo.addLineNumberInfo(lineNumberInfo);
}
return lnNoTblAttrInfo;
case DEFAULT_VALUE_ATTRIBUTE:
DefaultValue defaultValue = getDefaultValue(dataInStream, constantPool);
DefaultValueAttributeInfo defaultValAttrInfo = new DefaultValueAttributeInfo(attribNameCPIndex, defaultValue);
return defaultValAttrInfo;
case PARAMETER_DEFAULTS_ATTRIBUTE:
ParamDefaultValueAttributeInfo paramDefaultValAttrInfo = new ParamDefaultValueAttributeInfo(attribNameCPIndex);
int paramDefaultsInfoCount = dataInStream.readShort();
for (int i = 0; i < paramDefaultsInfoCount; i++) {
DefaultValue paramDefaultValue = getDefaultValue(dataInStream, constantPool);
paramDefaultValAttrInfo.addParamDefaultValueInfo(paramDefaultValue);
}
return paramDefaultValAttrInfo;
default:
throw new ProgramFileFormatException("unsupported attribute kind " + attribNameCPEntry.getValue());
}
}
Aggregations