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

Add new table impl #1

Merged
merged 7 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ private void addData(ArrayValue data) {
}
}


@Override
public IteratorValue getIterator() {
return new TableIterator<K, V>(entries.values().iterator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@ public interface IndexBasedAccessNode extends VariableReferenceNode {
ExpressionNode getExpression();

ExpressionNode getIndex();

TableMultiKeyExpressionNode getMultiKeyExpr();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.ballerinalang.model.tree.TableKeyTypeConstraintNode;

/**
* table&lt;Person&gt; key(...);.
* {@code TableTypeNode} represents a table type node in Ballerina.
*
* @since 1.2.0
* @since 1.3.0
*/
public interface TableTypeNode extends TypeNode {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public enum DiagnosticCode {
MISSING_REQUIRED_METHOD_NEXT("missing.required.method.next"),

// Table related codes
TABLE_CONSTRAINT_INVALID_SUBTYPE("invalid.stream.constraint.subtype"),
TABLE_CONSTRAINT_INVALID_SUBTYPE("invalid.table.constraint.subtype"),
TABLE_KEY_SPECIFIER_MISMATCH("table.key.specifier.mismatch"),
KEY_SPECIFIER_SIZE_MISMATCH_WITH_KEY_CONSTRAINT("key.specifier.size.mismatch.with.key.constraint"),
KEY_SPECIFIER_MISMATCH_WITH_KEY_CONSTRAINT("key.specifier.mismatch.with.key.constraint"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.wso2.ballerinalang.compiler.tree.expressions.BLangStatementExpression;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTableConstructorExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTableMultiKeyExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTrapExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTupleVarRef;
Expand Down Expand Up @@ -747,6 +748,14 @@ public void visit(BLangInvocation iExpr) {
result = iExpr;
}

@Override
public void visit(BLangTableMultiKeyExpr tableMultiKeyExpr) {
List<BLangExpression> exprList = new ArrayList<>();
tableMultiKeyExpr.multiKeyIndexExprs.forEach(expression -> exprList.add(rewriteExpr(expression)));
tableMultiKeyExpr.multiKeyIndexExprs = exprList;
result = tableMultiKeyExpr;
}

public void visit(BLangTypeInit typeInitExpr) {
typeInitExpr.initInvocation = rewriteExpr(typeInitExpr.initInvocation);
result = typeInitExpr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ public void visit(BLangFieldBasedAccess fieldAccessExpr) {
public void visit(BLangIndexBasedAccess indexAccessExpr) {
indexAccessExpr.expr = rewrite(indexAccessExpr.expr);
indexAccessExpr.indexExpr = rewrite(indexAccessExpr.indexExpr);
indexAccessExpr.multiKeyExpr = rewrite(indexAccessExpr.multiKeyExpr);
result = indexAccessExpr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
import org.wso2.ballerinalang.compiler.tree.expressions.BLangStatementExpression;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTableConstructorExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTableMultiKeyExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTrapExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTupleVarRef;
Expand Down Expand Up @@ -3566,12 +3567,12 @@ public void visit(BLangIndexBasedAccess indexAccessExpr) {
targetVarRef = new BLangXMLAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr,
indexAccessExpr.indexExpr);
} else if (varRefType.tag == TypeTags.TABLE) {
if (targetVarRef.multiKeyExpr != null) {
BLangTupleLiteral listConstructorExpr =
new BLangTupleLiteral();
listConstructorExpr.exprs = indexAccessExpr.multiKeyExpr.multiKeyIndexExprs;
if (targetVarRef.indexExpr.getKind() == NodeKind.TABLE_MULTI_KEY) {
BLangTupleLiteral listConstructorExpr = new BLangTupleLiteral();
listConstructorExpr.exprs = ((BLangTableMultiKeyExpr) indexAccessExpr.indexExpr).multiKeyIndexExprs;
List<BType> memberTypes = new ArrayList<>();
indexAccessExpr.multiKeyExpr.multiKeyIndexExprs.forEach(expression -> memberTypes.add(expression.type));
((BLangTableMultiKeyExpr) indexAccessExpr.indexExpr).multiKeyIndexExprs.
forEach(expression -> memberTypes.add(expression.type));
listConstructorExpr.type = new BTupleType(memberTypes);
indexAccessExpr.indexExpr = listConstructorExpr;
}
Expand All @@ -3584,6 +3585,12 @@ public void visit(BLangIndexBasedAccess indexAccessExpr) {
result = targetVarRef;
}

@Override
public void visit(BLangTableMultiKeyExpr tableMultiKeyExpr) {
rewriteExprs(tableMultiKeyExpr.multiKeyIndexExprs);
result = tableMultiKeyExpr;
}

@Override
public void visit(BLangInvocation iExpr) {
BLangInvocation genIExpr = iExpr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ void createIndexBasedAccessNode(DiagnosticPos pos, Set<Whitespace> ws) {
indexBasedAccess.pos = pos;
indexBasedAccess.addWS(ws);
if (tableMultiKeyExpressionNodeStack.size() == 1) {
indexBasedAccess.multiKeyExpr = (BLangTableMultiKeyExpr) tableMultiKeyExpressionNodeStack.pop();
indexBasedAccess.indexExpr = (BLangTableMultiKeyExpr) tableMultiKeyExpressionNodeStack.pop();
} else {
indexBasedAccess.indexExpr = (BLangExpression) exprNodeStack.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ private void cloneBLangAccessExpression(BLangAccessExpression source, BLangAcces
private void cloneBLangIndexBasedAccess(BLangIndexBasedAccess source, BLangIndexBasedAccess clone) {

clone.indexExpr = clone(source.indexExpr);
clone.multiKeyExpr = clone(source.multiKeyExpr);
cloneBLangAccessExpression(source, clone);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,6 @@ public void visit(BLangFieldBasedAccess fieldAccessExpr) {
public void visit(BLangIndexBasedAccess indexAccessExpr) {
analyzeExpr(indexAccessExpr.indexExpr);
analyzeExpr(indexAccessExpr.expr);
analyzeExpr(indexAccessExpr.multiKeyExpr);
}

public void visit(BLangTableMultiKeyExpr tableMultiKeyExpr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,6 @@ public void visit(BLangFieldBasedAccess fieldAccessExpr) {
public void visit(BLangIndexBasedAccess indexAccessExpr) {
analyzeNode(indexAccessExpr.expr, env);
analyzeNode(indexAccessExpr.indexExpr, env);
analyzeNode(indexAccessExpr.multiKeyExpr, env);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import org.wso2.ballerinalang.compiler.tree.expressions.BLangStatementExpression;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTableConstructorExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTableMultiKeyExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTrapExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr;
Expand Down Expand Up @@ -1032,6 +1033,11 @@ public void visit(BLangIndexBasedAccess indexAccessExpr) {
indexAccessExpr.expr.accept(this);
}

@Override
public void visit(BLangTableMultiKeyExpr tableMultiKeyExpr) {
analyzeExprList(tableMultiKeyExpr.multiKeyIndexExprs);
}

@Override
public void visit(BLangInvocation invocationExpr) {
// handle error constructor invocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTableConstructorExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTableMultiKeyExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTrapExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangTupleVarRef;
Expand Down Expand Up @@ -1983,7 +1984,8 @@ public void visit(BLangIndexBasedAccess indexBasedAccessExpr) {
indexBasedAccessExpr.compoundAssignmentLhsVar;
checkExpr(indexBasedAccessExpr.expr, this.env, symTable.noType);

if (indexBasedAccessExpr.multiKeyExpr != null && indexBasedAccessExpr.expr.type.tag != TypeTags.TABLE) {
if (indexBasedAccessExpr.indexExpr.getKind() == NodeKind.TABLE_MULTI_KEY &&
indexBasedAccessExpr.expr.type.tag != TypeTags.TABLE) {
dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.MULTI_KEY_MEMBER_ACCESS_NOT_SUPPORTED,
indexBasedAccessExpr.expr.type);
resultType = symTable.semanticError;
Expand Down Expand Up @@ -5276,15 +5278,16 @@ private BType checkIndexAccessExpr(BLangIndexBasedAccess indexBasedAccessExpr) {
}
}

if (indexExpr != null) {
if (indexExpr.getKind() != NodeKind.TABLE_MULTI_KEY) {
checkExpr(indexExpr, this.env, keyTypeConstraint);
if (indexExpr.type == symTable.semanticError) {
dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.INVALID_KEY_CONSTRAINT_PROVIDED_FOR_ACCESS,
keyTypeConstraint);
return symTable.semanticError;
}
} else {
List<BLangExpression> multiKeyExpressionList = indexBasedAccessExpr.multiKeyExpr.multiKeyIndexExprs;
List<BLangExpression> multiKeyExpressionList = ((BLangTableMultiKeyExpr)
indexBasedAccessExpr.indexExpr).multiKeyIndexExprs;
List<BType> keyConstraintTypes = ((BTupleType) keyTypeConstraint).tupleTypes;
if (keyConstraintTypes.size() != multiKeyExpressionList.size()) {
dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.INVALID_KEY_CONSTRAINT_PROVIDED_FOR_ACCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@ private boolean isAssignable(BType source, BType target, Set<TypePair> unresolve
unresolvedTypes);
}

//TODO Need to check the key specifier
if (targetTag == TypeTags.TABLE && sourceTag == TypeTags.TABLE) {
return isAssignableTableType((BTableType) source, (BTableType) target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
public class BLangIndexBasedAccess extends BLangAccessExpression implements IndexBasedAccessNode {

public BLangExpression indexExpr;
public BLangTableMultiKeyExpr multiKeyExpr;

// Only used at Desugar and after.
public boolean isStoreOnCreation = false;
Expand All @@ -49,16 +48,7 @@ public BLangExpression getIndex() {

@Override
public String toString() {
if (indexExpr != null) {
return String.valueOf(expr) + "[" + String.valueOf(indexExpr) + "]";
} else {
return String.valueOf(expr) + "[" + multiKeyExpr.toString();
}
}

@Override
public BLangTableMultiKeyExpr getMultiKeyExpr() {
return this.multiKeyExpr;
return String.valueOf(expr) + "[" + String.valueOf(indexExpr) + "]";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
import java.util.List;

/**
* BLangTableConstructorExpr class.
* Implementation of Table constructor expression.
*
* @see BLangTableConstructorExpr
* @since 1.3.0
*/
public class BLangTableConstructorExpr extends BLangExpression implements TableConstructorExprNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;

/**
* Represents an expression which can hold expressions related to table member access.
*
* @since 1.3.0
*/
public class BLangTableMultiKeyExpr extends BLangAccessExpression implements TableMultiKeyExpressionNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.wso2.ballerinalang.compiler.tree.BLangTableKeyTypeConstraint;

/**
* {@code BLangTableTypeNode} represents a table type node in Ballerina.
*
* @since 1.3.0
*/
public class BLangTableTypeNode extends BLangType implements TableTypeNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ error.error.type.expected = \
error.missing.required.method.next = \
''{0}'' must implement ''public function next() returns {1}''.

error.invalid.stream.constraint.subtype = \
error.invalid.table.constraint.subtype = \
invalid constraint type. expected subtype of ''map<any|error>'' but ''{0}''

error.table.key.specifier.mismatch = \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
/**
* Native implementation of lang.table:filter(table&lt;Type&gt;, function).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "filter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
/**
* Native implementation of lang.table:forEach(table&lt;Type&gt;, function).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "forEach",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* Native implementation of lang.table:get(table&lt;Type&gt;, KeyType).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "get",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/**
* Native implementation of lang.table:iterator(table&lt;Type&gt;).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "iterator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
/**
* Extern function to get key arrays from the table.
* ballerina.model.table:keys()
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
/**
* Extern function to check existence of key.
* ballerina.model.table:hasKey(KeyType)
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "hasKey",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* Native implementation of lang.table:length(map).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "length",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
/**
* Native implementation of lang.table:map(table&lt;Type&gt;, function).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "map",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/**
* Native implementation of lang.table.TableIterator:next().
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "next",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
/**
* Extern function to get next key from the table.
* ballerina.model.table:nextKey()
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/**
* Native implementation of lang.table:reduce(table&lt;Type&gt;, function).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "reduce",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* Native implementation of lang.table:remove(table&lt;Type&gt;, KeyType).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "remove",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* Native implementation of lang.table:removeAll(table&lt;Type&gt;).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "removeAll",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* Native implementation of lang.table:removeIfHasKey(table&lt;Type&gt;, KeyType).
*
* @since 1.3.0
*/
@BallerinaFunction(
orgName = "ballerina", packageName = "lang.table", functionName = "removeIfHasKey",
Expand Down
Loading