Skip to content

Commit

Permalink
Address review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ushirask committed Jul 19, 2023
1 parent b56a6a0 commit 7000cfb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5794,8 +5794,8 @@ public void visit(BLangForkJoin forkJoin) {

@Override
public void visit(BLangLiteral literalExpr) {
if (Types.getReferredType(literalExpr.getBType()).tag == TypeTags.ARRAY ||
Types.getReferredType(literalExpr.getBType()).tag == TypeTags.TUPLE) {
int tag = Types.getReferredType(literalExpr.getBType()).tag;
if (tag == TypeTags.ARRAY || tag == TypeTags.TUPLE) {
// this is blob literal as byte array
result = rewriteBlobLiteral(literalExpr);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,10 +944,8 @@ public BLangNode transform(ConstantDeclarationNode constantDeclarationNode) {

// Check whether the value is a literal or a unary expression and if it is not any one of the before mentioned
// kinds it is an invalid case, so we don't need to consider it.
if ((nodeKind == NodeKind.LITERAL || nodeKind == NodeKind.NUMERIC_LITERAL ||
nodeKind == NodeKind.UNARY_EXPR)
&& (constantNode.typeNode == null
|| constantNode.typeNode.getKind() != NodeKind.ARRAY_TYPE)) {
if ((nodeKind == NodeKind.LITERAL || nodeKind == NodeKind.NUMERIC_LITERAL || nodeKind == NodeKind.UNARY_EXPR)
&& (constantNode.typeNode == null || constantNode.typeNode.getKind() != NodeKind.ARRAY_TYPE)) {
// Note - If the RHS is a literal, we need to create an anonymous type definition which can later be used
// in type definitions.h
createAnonymousTypeDefForConstantDeclaration(constantNode, pos, identifierPos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,7 @@ public void visit(BLangLiteral literalExpr, AnalyzerData data) {
}

if (literalType.tag == TypeTags.BYTE_ARRAY) {
BLangListConstructorExpr listConstructorExpr = rewriteByteArrayLiteral(literalExpr);
BType expType = Types.getReferredType(data.expType);
if (expType.tag == TypeTags.ARRAY && ((BArrayType) expType).state == BArrayState.INFERRED) {
((BArrayType) expType).size = listConstructorExpr.exprs.size();
((BArrayType) expType).state = BArrayState.CLOSED;
}
if (types.typeIncompatible(literalExpr.pos, listConstructorExpr.getBType(), data.expType)) {
data.resultType = symTable.semanticError;
return;
}
data.resultType = checkListConstructorCompatibility(literalType, listConstructorExpr, data);
data.resultType = rewriteByteArrayLiteral(literalExpr, data);
return;
}

Expand All @@ -283,25 +273,27 @@ public void visit(BLangLiteral literalExpr, AnalyzerData data) {
data.resultType = finiteType;
}

private BLangListConstructorExpr rewriteByteArrayLiteral(BLangLiteral literalExpr) {
private BType rewriteByteArrayLiteral(BLangLiteral literalExpr, AnalyzerData data) {
byte[] values = types.convertToByteArray((String) literalExpr.value);
BLangListConstructorExpr.BLangArrayLiteral arrayLiteralNode
= (BLangListConstructorExpr.BLangArrayLiteral) TreeBuilder.createArrayLiteralExpressionNode();
BArrayType literalType = new BArrayType(symTable.byteType, null, values.length,
BArrayState.CLOSED);
arrayLiteralNode.setBType(literalType);
arrayLiteralNode.pos = literalExpr.pos;
arrayLiteralNode.exprs = new ArrayList<>();

List<BType> memberTypes = new ArrayList<>();
for (byte b : values) {
arrayLiteralNode.exprs.add(createByteLiteral(literalExpr.pos, b));
memberTypes.add(getFiniteType(Byte.toUnsignedInt(b), data.constantSymbol, literalExpr.pos,
symTable.byteType));
}

BType expType = Types.getReferredType(data.expType);
if (expType.tag == TypeTags.ARRAY && ((BArrayType) expType).state == BArrayState.INFERRED) {
((BArrayType) expType).size = memberTypes.size();
((BArrayType) expType).state = BArrayState.CLOSED;
}

BArrayType arrayType = new BArrayType(symTable.byteType, null, values.length, BArrayState.CLOSED);
if (types.typeIncompatible(literalExpr.pos, arrayType, expType)) {
return symTable.semanticError;
}
return arrayLiteralNode;
}

private BLangLiteral createByteLiteral(Location pos, Byte value) {
BLangLiteral byteLiteral = new BLangLiteral(Byte.toUnsignedInt(value), symTable.byteType);
byteLiteral.pos = pos;
return byteLiteral;
return createNewTupleType(literalExpr.pos, memberTypes, data);
}

@Override
Expand Down

0 comments on commit 7000cfb

Please sign in to comment.