Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support constant byte array literal #40587

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
db20fb1
Introduce TypeResolver
SandaruJayawardana Jul 5, 2022
4d8ec1d
Merge branch 'master' of https://github.com/ballerina-platform/baller…
SandaruJayawardana Jul 5, 2022
24ca97e
Introduce TypeResolver
SandaruJayawardana Jul 6, 2022
0894ddb
Update type resolving logic
SandaruJayawardana Jul 8, 2022
2b258fa
Update SymbolEnter
SandaruJayawardana Jul 8, 2022
ad175fc
Add DummyTest for testing
SandaruJayawardana Jul 8, 2022
4510d38
Add DummyTest for testing
SandaruJayawardana Jul 14, 2022
fa0fc3f
Update logic
SandaruJayawardana Jul 25, 2022
8ab9398
Update logic
SandaruJayawardana Aug 3, 2022
8a1f256
Address review suggestions
SandaruJayawardana Aug 5, 2022
174e699
Remove an unused import
SandaruJayawardana Aug 8, 2022
0a1f8a4
Refactor code
SandaruJayawardana Aug 9, 2022
d28ec04
Refactor code
SandaruJayawardana Aug 15, 2022
68d30a0
Update code
SandaruJayawardana Aug 17, 2022
cac46b1
Revert changes
SandaruJayawardana Aug 18, 2022
20fa36f
Merge branch 'master' of https://github.com/ballerina-platform/baller…
SandaruJayawardana Aug 25, 2022
7b96cc6
Implement constant type checking and resolving logic
SandaruJayawardana Sep 16, 2022
ea30100
Update SymbolEnter
SandaruJayawardana Sep 16, 2022
f3980ec
Fix an exception
SandaruJayawardana Sep 16, 2022
10ca232
Clean the code
SandaruJayawardana Sep 16, 2022
3c765fc
Add support for unary-expr
SandaruJayawardana Sep 16, 2022
279222c
Implement constant value resolving logic
SandaruJayawardana Sep 16, 2022
68bb427
Update types of expressions
SandaruJayawardana Oct 3, 2022
86d5a7d
Fix fill member logic
SandaruJayawardana Oct 3, 2022
85656e6
Clean the code
SandaruJayawardana Oct 6, 2022
617f9b5
Fix object and float langlib related issues
SandaruJayawardana Oct 21, 2022
05277a8
Update dummy.bal
SandaruJayawardana Oct 21, 2022
d8c3cb1
Merge branch 'symbolEnter-revamp' of https://github.com/ballerina-pla…
SandaruJayawardana Oct 21, 2022
a732958
Fix failing langlibs
SandaruJayawardana Nov 4, 2022
580a3e2
Update code
SandaruJayawardana Nov 21, 2022
98277e1
Update code
SandaruJayawardana Nov 22, 2022
d10933c
Update code
SandaruJayawardana Jan 3, 2023
0c058ba
Revert toString() changes for recursive type
prakanth97 Jan 26, 2023
dc9bd4f
Fix cyclic references in record and object type
prakanth97 Feb 7, 2023
0238b5d
Fix issues in the type resolving logic
prakanth97 Feb 7, 2023
6dd5e92
Fix cloneableType flag propagates to original type
prakanth97 Feb 10, 2023
b2ff1ac
Set cyclic flag properly
prakanth97 Feb 14, 2023
c1cc1f9
Update return type of binary const operation
prakanth97 Feb 17, 2023
03e7304
Fix bugs in type resolver logic
prakanth97 Feb 17, 2023
cfa621d
Update failing unit tests
prakanth97 Feb 17, 2023
afcc0d9
Update readonly and nullable flag of union type
prakanth97 Feb 22, 2023
f9db8b0
Resolve fields of object const in type checker
prakanth97 Feb 22, 2023
9a521c6
Resolve type definition of error intersection
prakanth97 Feb 22, 2023
668e7e7
Log error for assign non const value to const decl
prakanth97 Feb 24, 2023
201e7fb
Sync with master
prakanth97 Feb 24, 2023
16173ff
handle decimal and int overflow for constant
prakanth97 Feb 28, 2023
faff1db
Fix constant type checking by using expected type
prakanth97 Mar 8, 2023
babf8f0
Handle assigning const to subtype of int variable
prakanth97 Mar 8, 2023
d96e127
Add mistakenly removed code
prakanth97 Mar 9, 2023
bc646df
Resolve compound expr containing singleton operand
prakanth97 Mar 10, 2023
90c0d4d
Fix logic mistake
prakanth97 Mar 10, 2023
7180cab
Refactor code to reduce duplicates
prakanth97 Mar 13, 2023
abab458
Preserve reference type in cyclic type definition
prakanth97 Mar 15, 2023
14c283c
Log proper errors for negative enum tests
prakanth97 Mar 15, 2023
101f091
Resolve type properly when type def not available
prakanth97 Mar 22, 2023
60520c1
Update failing unit tests
prakanth97 Mar 22, 2023
66f6026
Log errors properly for negative constant cases
prakanth97 Mar 22, 2023
155e3c4
Fix failing unit tests
prakanth97 Mar 29, 2023
5d8f2ea
Fix type inclusion for unresolved structural types
prakanth97 Mar 31, 2023
589888a
Resolve merge conflicts
prakanth97 Mar 31, 2023
3674042
Fix failing unit tests
prakanth97 Apr 12, 2023
1c4d86b
Fix failing unit and lang lib tests
prakanth97 Apr 21, 2023
f3937dd
Update the immutable type of cyclic intersection
prakanth97 Apr 25, 2023
0836224
Clean code
prakanth97 Apr 25, 2023
1f152d9
Improve isSelectivelyImmutableType logic
prakanth97 Apr 25, 2023
2ec7c43
Enable ubuntu build
prakanth97 Apr 25, 2023
a65a2fa
Fix failing compiler api tests
prakanth97 Apr 26, 2023
66a2309
Fix style check failures
prakanth97 Apr 26, 2023
0b86986
Fix CloneableType symbol name
prakanth97 Apr 26, 2023
84c2c52
Fix failing completion tests
prakanth97 Apr 26, 2023
7674ed9
Fix language server core test failures
prakanth97 Apr 27, 2023
83f8c65
Disallow list constructor expr in const decl
prakanth97 Apr 27, 2023
d9750d5
Add constant definition related tests
prakanth97 May 3, 2023
4b78e28
Add type definition related tests
prakanth97 May 3, 2023
426bc09
Enable full build
prakanth97 May 3, 2023
051497c
Fix mistakes in the code
prakanth97 May 4, 2023
a346d4d
Restrict record as constant declaration exp type
prakanth97 May 5, 2023
45ce35b
Clean up allocated memory for variables
prakanth97 May 5, 2023
810fa3a
Resolve merge conflict
prakanth97 May 5, 2023
2a5ece4
Set typeId of distinct error type properly
prakanth97 May 8, 2023
7d7a37a
Merge branch 'symbolEnter-revamp' of https://github.com/ballerina-pla…
prakanth97 May 8, 2023
9c58def
Handle out of memory error
prakanth97 May 8, 2023
df00809
Handle computed name fields in const map properly
prakanth97 May 10, 2023
cfebb71
Fix nballerina build failures
prakanth97 May 11, 2023
6634967
Fix NPE for recursive inclusion in type definition
prakanth97 May 12, 2023
deeb076
Restrict invalid inclusion in class definition
prakanth97 May 12, 2023
d018378
Fix mistakes in resolving cyclic type definition
prakanth97 May 13, 2023
1e2f0b5
Correct mistakes in the code
prakanth97 May 16, 2023
4fa1f4d
Add tests for cyclic type definition
prakanth97 May 21, 2023
aa1d3de
Refactor code
prakanth97 May 23, 2023
a767540
Merge branch 'symbolEnter-revamp' of https://github.com/ballerina-pla…
prakanth97 May 23, 2023
f8fe77c
Set type of CloneableType typenode properly
prakanth97 May 26, 2023
3a4e635
Fix failing cyclic type definitions
prakanth97 May 26, 2023
e140443
Merge branch 'symbolEnter-revamp' of https://github.com/ballerina-pla…
prakanth97 May 29, 2023
c64fd4e
Correct invalid error msgs for const and type def
prakanth97 May 30, 2023
2310102
Update completion test result
prakanth97 May 30, 2023
a1798aa
Support constant byte array literal
ushirask Jun 5, 2023
dd0d3f6
Fix imports
ushirask Jun 5, 2023
a2819e2
Fix test cases
ushirask Jun 6, 2023
bf42460
Fix formatting
ushirask Jun 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pull_request_full_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
branches:
- master
- symbolEnter-revamp

jobs:
build-lang:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pull_request_windows_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
- 2201.[0-9]+.[0-9]+-stage
- native-build
- revert-client-decl-master
- symbolEnter-revamp

jobs:
windows_build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ private BLangConstantValue readConstLiteralValue(BType valueType, DataInputStrea
}
return new BLangConstantValue(keyValuePairs, valueType);
case TypeTags.TUPLE:
case TypeTags.BYTE_ARRAY:
int tupleSize = dataInStream.readInt();
List<BLangConstantValue> members = new ArrayList<>(tupleSize);
for (int i = 0; i < tupleSize; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ public static String getFieldTypeSignature(BType bType) {
case TypeTags.TABLE:
return GET_TABLE_VALUE_IMPL;
case TypeTags.ARRAY:
case TypeTags.BYTE_ARRAY:
case TypeTags.TUPLE:
return GET_ARRAY_VALUE;
case TypeTags.ERROR:
Expand Down Expand Up @@ -744,7 +745,6 @@ public static BType getReferredType(BType type) {

public static void loadConstantValue(BType bType, Object constVal, MethodVisitor mv,
JvmConstantsGen jvmConstantsGen) {

int typeTag = getReferredType(bType).tag;
if (TypeTags.isIntegerTypeTag(typeTag)) {
long intValue = constVal instanceof Long ? (long) constVal : Long.parseLong(String.valueOf(constVal));
Expand Down Expand Up @@ -780,6 +780,10 @@ public static void loadConstantValue(BType bType, Object constVal, MethodVisitor
mv.visitLdcInsn(removeDecimalDiscriminator(String.valueOf(constVal)));
mv.visitMethodInsn(INVOKESPECIAL, DECIMAL_VALUE, JVM_INIT_METHOD, INIT_WITH_STRING, false);
break;
case TypeTags.BYTE_ARRAY:
String byteArrayLiteral = String.valueOf(constVal);
mv.visitLdcInsn(byteArrayLiteral);
break;
case TypeTags.NIL:
case TypeTags.NEVER:
mv.visitInsn(ACONST_NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ private void writeValue(Object value, BType typeOfValue) {
byteBuf.writeBoolean((Boolean) value);
break;
case TypeTags.NIL:
case TypeTags.BYTE_ARRAY:
break;
default:
throw new UnsupportedOperationException("finite type value is not supported for type: " + typeOfValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BType;
import org.wso2.ballerinalang.compiler.tree.BLangConstantValue;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral;
import org.wso2.ballerinalang.compiler.util.Name;
import org.wso2.ballerinalang.compiler.util.TypeTags;

Expand Down Expand Up @@ -126,6 +129,7 @@ public static void writeConstValue(ConstantPool cp, ByteBuf buf, Object value, B
});
break;
case TypeTags.TUPLE:
case TypeTags.BYTE_ARRAY:
BIRNode.ConstValue[] tupleConstVal = (BIRNode.ConstValue[]) value;
buf.writeInt(tupleConstVal.length);
for (BIRNode.ConstValue memValue : tupleConstVal) {
Expand Down Expand Up @@ -242,6 +246,18 @@ public static BIRNode.ConstValue getBIRConstantVal(BLangConstantValue constValue
return new BIRNode.ConstValue(tupleConstVal, ((BTupleType) constValue.type).getIntersectionType().get());
}

if (tag == TypeTags.BYTE_ARRAY) {
BLangListConstructorExpr.BLangArrayLiteral arrayLiteral =
(BLangListConstructorExpr.BLangArrayLiteral) constValue.value;
List<BLangExpression> exprList = arrayLiteral.exprs;
BIRNode.ConstValue[] byteArrConstVal = new BIRNode.ConstValue[exprList.size()];
for (int exprIndex = 0; exprIndex < exprList.size(); exprIndex++) {
BLangLiteral expr = (BLangLiteral) exprList.get(exprIndex);
byteArrConstVal[exprIndex] = new BIRNode.ConstValue(expr.value, expr.getBType());
}
return new BIRNode.ConstValue(byteArrConstVal, constValue.type);
}

return new BIRNode.ConstValue(constValue.value, constValue.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,13 @@ public void visit(BLangSimpleVarRef varRefExpr) {
// If the var ref is a const-ref of value type, then replace the ref
// from a simple literal
BType literalType = Types.getReferredType(constSymbol.literalType);
BType varRefType = Types.getReferredType(varRefExpr.getBType());

// Handle the assignment of int to subtypes of int (byte, int:Signed16, ...).
if (literalType.tag == TypeTags.INT && types.isContainSubtypeOfInt(varRefType)) {
literalType = varRefType;
}

if (literalType.tag <= TypeTags.BOOLEAN || literalType.tag == TypeTags.NIL) {
BLangConstRef constRef = ASTBuilderUtil.createBLangConstRef(varRefExpr.pos, literalType,
constSymbol.value.value);
Expand All @@ -1097,7 +1104,7 @@ public void visit(BLangSimpleVarRef varRefExpr) {
implConversionExpr.targetType = varRefExpr.impConversionExpr.targetType;
constRef.impConversionExpr = implConversionExpr;
} else {
types.setImplicitCastExpr(constRef, constRef.getBType(), varRefExpr.getBType());
types.setImplicitCastExpr(constRef, constRef.getBType(), varRefType);
}
result = constRef;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
*/
package org.wso2.ballerinalang.compiler.semantics.analyzer;

import io.ballerina.tools.diagnostics.Location;
import org.ballerinalang.model.tree.NodeKind;
import org.ballerinalang.model.tree.expressions.RecordLiteralNode;
import org.ballerinalang.util.diagnostic.DiagnosticErrorCode;
import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLog;
import org.wso2.ballerinalang.compiler.parser.BLangMissingNodesHelper;
import org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv;
import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag;
import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor;
Expand All @@ -35,6 +39,7 @@
import org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr;
import org.wso2.ballerinalang.compiler.util.CompilerContext;
import org.wso2.ballerinalang.compiler.util.Names;

import java.util.Stack;

Expand All @@ -47,12 +52,21 @@ public class ConstantAnalyzer extends BLangNodeVisitor {

private static final CompilerContext.Key<ConstantAnalyzer> CONSTANT_ANALYZER_KEY =
new CompilerContext.Key<>();

private final BLangMissingNodesHelper missingNodesHelper;
private final Names names;
private final SymbolTable symTable;
private final SymbolResolver symResolver;
private BLangDiagnosticLog dlog;
private Stack<BLangExpression> expressions = new Stack<>();

private ConstantAnalyzer(CompilerContext context) {

context.put(CONSTANT_ANALYZER_KEY, this);
this.missingNodesHelper = BLangMissingNodesHelper.getInstance(context);
this.names = Names.getInstance(context);
this.symTable = SymbolTable.getInstance(context);
this.symResolver = SymbolResolver.getInstance(context);
this.dlog = BLangDiagnosticLog.getInstance(context);
}

Expand Down Expand Up @@ -87,6 +101,16 @@ public void visit(BLangNumericLiteral literal) {
@Override
public void visit(BLangSimpleVarRef varRef) {
BSymbol symbol = varRef.symbol;
if (varRef.pkgSymbol != symTable.notFoundSymbol && symbol == symTable.notFoundSymbol) {
SymbolEnv pkgEnv = symTable.pkgEnvMap.get(varRef.pkgSymbol);
symbol = pkgEnv == null ? symbol : symResolver.lookupMainSpaceSymbolInPackage(varRef.pos, pkgEnv,
names.fromIdNode(varRef.pkgAlias), names.fromIdNode(varRef.variableName));
}

if (symbol == symTable.notFoundSymbol) {
logUndefinedSymbolError(varRef.pos, varRef.variableName.value);
}

// Symbol can be null in some invalid scenarios. Eg - const string m = { name: "Ballerina" };
if (symbol != null && (symbol.tag & SymTag.CONSTANT) != SymTag.CONSTANT) {
dlog.error(varRef.pos, DiagnosticErrorCode.EXPRESSION_IS_NOT_A_CONSTANT_EXPRESSION);
Expand All @@ -111,15 +135,6 @@ public void visit(BLangRecordLiteral recordLiteral) {

@Override
public void visit(BLangBinaryExpr binaryExpr) {
for (int i = expressions.size() - 1; i >= 0; i--) {
NodeKind kind = expressions.get(i).getKind();
if (kind == NodeKind.GROUP_EXPR || kind == NodeKind.UNARY_EXPR) {
continue;
}
if (kind != NodeKind.BINARY_EXPR) {
dlog.error(binaryExpr.pos, DiagnosticErrorCode.CONSTANT_EXPRESSION_NOT_SUPPORTED);
}
}
analyzeExpr(binaryExpr.lhsExpr);
analyzeExpr(binaryExpr.rhsExpr);
}
Expand Down Expand Up @@ -156,4 +171,10 @@ void analyzeExpr(BLangExpression expr) {
}
dlog.error(expr.pos, DiagnosticErrorCode.EXPRESSION_IS_NOT_A_CONSTANT_EXPRESSION);
}

private void logUndefinedSymbolError(Location pos, String name) {
if (!missingNodesHelper.isMissingNode(name)) {
dlog.error(pos, DiagnosticErrorCode.UNDEFINED_SYMBOL, name);
}
}
}
Loading