Skip to content

Commit

Permalink
Merge pull request #16068 from SupunS/varref-tests
Browse files Browse the repository at this point in the history
 Enable literal tests and varref tests
  • Loading branch information
manuranga authored Jun 27, 2019
2 parents 9580039 + 91d6b51 commit a5babc4
Show file tree
Hide file tree
Showing 21 changed files with 323 additions and 315 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -742,20 +742,23 @@ public BType readType(int cpI) throws IOException {
recordSymbol.scope.define(varSymbol.name, varSymbol);
}

// read record init function
String recordInitFuncName = getStringCPEntryValue(inputStream);
int recordInitFuncFlags = inputStream.readInt();
BInvokableType recordInitFuncType = (BInvokableType) readTypeFromCp();
Name initFuncName = names.fromString(recordInitFuncName);
boolean isNative = Symbols.isFlagOn(recordInitFuncFlags, Flags.NATIVE);
BInvokableSymbol recordInitFuncSymbol =
Symbols.createFunctionSymbol(recordInitFuncFlags,
initFuncName, env.pkgSymbol.pkgID, recordInitFuncType,
env.pkgSymbol, isNative);
recordInitFuncSymbol.retType = recordInitFuncType.retType;
recordSymbol.initializerFunc = new BAttachedFunction(initFuncName, recordInitFuncSymbol,
recordInitFuncType);
recordSymbol.scope.define(initFuncName, recordInitFuncSymbol);
boolean isInitAvailable = inputStream.readByte() == 1;
if (isInitAvailable) {
// read record init function
String recordInitFuncName = getStringCPEntryValue(inputStream);
int recordInitFuncFlags = inputStream.readInt();
BInvokableType recordInitFuncType = (BInvokableType) readTypeFromCp();
Name initFuncName = names.fromString(recordInitFuncName);
boolean isNative = Symbols.isFlagOn(recordInitFuncFlags, Flags.NATIVE);
BInvokableSymbol recordInitFuncSymbol =
Symbols.createFunctionSymbol(recordInitFuncFlags,
initFuncName, env.pkgSymbol.pkgID, recordInitFuncType,
env.pkgSymbol, isNative);
recordInitFuncSymbol.retType = recordInitFuncType.retType;
recordSymbol.initializerFunc = new BAttachedFunction(initFuncName, recordInitFuncSymbol,
recordInitFuncType);
recordSymbol.scope.define(initFuncName, recordInitFuncSymbol);
}

// setDocumentation(varSymbol, attrData); // TODO fix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public void visit(BLangFunction astFunc) {

// TODO: Return variable with NIL type should be written to BIR
// Special %0 location for storing return values
birFunc.returnVariable = new BIRVariableDcl(astFunc.pos, astFunc.symbol.retType,
birFunc.returnVariable = new BIRVariableDcl(astFunc.pos, astFunc.symbol.type.getReturnType(),
this.env.nextLocalVarId(names), VarScope.FUNCTION, VarKind.RETURN);

//add closure vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.wso2.ballerinalang.compiler.bir.writer.CPEntry.IntegerCPEntry;
import org.wso2.ballerinalang.compiler.bir.writer.CPEntry.PackageCPEntry;
import org.wso2.ballerinalang.compiler.bir.writer.CPEntry.StringCPEntry;
import org.wso2.ballerinalang.compiler.semantics.model.Scope;
import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BAttachedFunction;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BObjectTypeSymbol;
Expand Down Expand Up @@ -60,15 +59,13 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral;
import org.wso2.ballerinalang.compiler.util.Name;
import org.wso2.ballerinalang.compiler.util.Names;
import org.wso2.ballerinalang.compiler.util.TypeTags;
import org.wso2.ballerinalang.util.Flags;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -252,20 +249,21 @@ public void visit(BRecordType bRecordType) {
buff.writeBoolean(bRecordType.sealed);
writeTypeCpIndex(bRecordType.restFieldType);

buff.writeInt(bRecordType.fields.size());
for (BField field : bRecordType.fields) {
BSymbol symbol = field.symbol;
buff.writeInt(addStringCPEntry(symbol.name.value));
buff.writeInt(symbol.flags);
writeTypeCpIndex(field.type);
}

BAttachedFunction initializerFunc = tsymbol.initializerFunc;
Set<Map.Entry<Name, Scope.ScopeEntry>> recordSymbols = tsymbol.scope.entries.entrySet();

buff.writeInt(recordSymbols.size() - 1); // recordSymbols = 1 initializer + n fields
for (Map.Entry<Name, Scope.ScopeEntry> entry : recordSymbols) {
BSymbol symbol = entry.getValue().symbol;
String fieldName = entry.getKey().value;
if (symbol != initializerFunc.symbol) {
buff.writeInt(addStringCPEntry(fieldName));
buff.writeInt(symbol.flags);
writeTypeCpIndex(symbol.type);
}
if (initializerFunc == null) {
buff.writeByte(0);
return;
}

buff.writeByte(1);
buff.writeInt(addStringCPEntry(initializerFunc.funcName.value));
buff.writeInt(initializerFunc.symbol.flags);
writeTypeCpIndex(initializerFunc.type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,8 @@ private BLangInvocation generateErrorDetailBuiltinFunction(DiagnosticPos pos, BT
}
detailInvocation.symbol = bSymbol;
}

detailInvocation.type = detailInvocation.symbol.type.getReturnType();
return detailInvocation;
}

Expand All @@ -1014,6 +1016,8 @@ private BLangInvocation generateErrorReasonBuiltinFunction(DiagnosticPos pos, BT
reasonInvocation.symbol = symResolver.resolveBuiltinOperator(
names.fromString(ERROR_REASON_FUNCTION_NAME), errorVarSymbol.type);
}

reasonInvocation.type = reasonInvocation.symbol.type.getReturnType();
return reasonInvocation;
}

Expand Down Expand Up @@ -1312,7 +1316,7 @@ private void createVarRefAssignmentStmts(BLangTupleVarRef parentTupleVariable, B
BLangLiteral indexExpr = ASTBuilderUtil.createLiteral(errorVarRef.pos, symTable.intType,
(long) index);
BLangIndexBasedAccess arrayAccessExpr = ASTBuilderUtil.createIndexBasesAccessExpr(
parentTupleVariable.pos, symTable.mapType, tupleVarSymbol, indexExpr);
parentTupleVariable.pos, symTable.errorType, tupleVarSymbol, indexExpr);
if (parentIndexAccessExpr != null) {
arrayAccessExpr.expr = parentIndexAccessExpr;
}
Expand Down Expand Up @@ -1435,7 +1439,7 @@ private void createVarRefAssignmentStmts(BLangRecordVarRef parentRecordVarRef, B
if (NodeKind.TUPLE_VARIABLE_REF == variableReference.getKind()) {
BLangTupleVarRef tupleVariable = (BLangTupleVarRef) variableReference;
BLangIndexBasedAccess arrayAccessExpr = ASTBuilderUtil.createIndexBasesAccessExpr(tupleVariable.pos,
new BArrayType(symTable.anyType), recordVarSymbol, indexExpr);
symTable.tupleType, recordVarSymbol, indexExpr);
if (parentIndexAccessExpr != null) {
arrayAccessExpr.expr = parentIndexAccessExpr;
}
Expand All @@ -1445,12 +1449,12 @@ private void createVarRefAssignmentStmts(BLangRecordVarRef parentRecordVarRef, B

if (NodeKind.ERROR_VARIABLE_REF == variableReference.getKind()) {
BLangIndexBasedAccess arrayAccessExpr = ASTBuilderUtil.createIndexBasesAccessExpr(variableReference.pos,
new BArrayType(symTable.anyType), recordVarSymbol, indexExpr);
symTable.errorType, recordVarSymbol, indexExpr);
if (parentIndexAccessExpr != null) {
arrayAccessExpr.expr = parentIndexAccessExpr;
}
createVarRefAssignmentStmts(
(BLangErrorVarRef) variableReference, parentBlockStmt, recordVarSymbol, arrayAccessExpr);
createVarRefAssignmentStmts((BLangErrorVarRef) variableReference, parentBlockStmt, recordVarSymbol,
arrayAccessExpr);
}
}

Expand Down
5 changes: 4 additions & 1 deletion stdlib/bir/src/main/ballerina/bir/type_parser.bal
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ public type TypeParser object {
self.cp.types[self.cpI] = obj;
obj.restFieldType = self.parseTypeCpRef();
obj.fields = self.parseRecordFields();
obj.initFunction = self.parseRecordInitFunction();
boolean isInitFuncAvailable = self.readInt8() == 1;
if (isInitFuncAvailable) {
obj.initFunction = self.parseRecordInitFunction();
}
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class IdentifierLiteralPackageTest {
public void testAccessingVarsInOtherPackage() {
CompileResult result = BCompileUtil.compile(this, "test-src/expressions/literals/identifierliteral",
"pkg.main");
BValue[] returns = BRunUtil.invoke(result, "pkg.main:0.0.0", "getVarsInOtherPkg");
BValue[] returns = BRunUtil.invoke(result, "getVarsInOtherPkg");
Assert.assertEquals(returns.length, 4);
Assert.assertSame(returns[0].getClass(), BInteger.class);
Assert.assertSame(returns[1].getClass(), BString.class);
Expand All @@ -53,7 +53,7 @@ public void testAccessingVarsInOtherPackage() {
public void testAccessStructGlobalVarWithIdentifierLiteralInOtherPackage() {
CompileResult result = BCompileUtil.compile(this, "test-src/expressions/literals/identifierliteral",
"pkg.main");
BValue[] returns = BRunUtil.invoke(result, "pkg.main:0.0.0", "accessStructWithIL");
BValue[] returns = BRunUtil.invoke(result, "accessStructWithIL");

Assert.assertEquals(returns.length, 2);
Assert.assertSame(returns[0].getClass(), BString.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import org.ballerinalang.test.services.testutils.HTTPTestRequest;
import org.ballerinalang.test.services.testutils.MessageUtils;
import org.ballerinalang.test.services.testutils.Services;
import org.ballerinalang.test.util.BServiceUtil;
import org.ballerinalang.test.util.CompileResult;
import org.ballerinalang.test.util.BCompileUtil;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand All @@ -36,22 +35,20 @@
/**
* identifier literals in service and resource names.
*/
@Test(groups = { "brokenOnJBallerina" })
public class IdentifierLiteralServiceTest {

private CompileResult application;
private static final String MOCK_ENDPOINT_NAME = "testEP";
private static final int MOCK_ENDPOINT_PORT = 9090;

@BeforeClass
public void setup() {
application = BServiceUtil
.setupProgramFile(this,
"test-src/expressions/literals/identifierliteral/identifier-literal-service.bal");
BCompileUtil.compile("test-src/expressions/literals/identifierliteral/identifier-literal-service.bal");
}

@Test(description = "Test using identifier literals in service and resource names")
public void testUsingIdentifierLiteralsInServiceAndResourceNames() {
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/identifierLiteral/resource", "GET");
HttpCarbonMessage response = Services.invokeNew(application, MOCK_ENDPOINT_NAME, cMsg);
HttpCarbonMessage response = Services.invoke(MOCK_ENDPOINT_PORT, cMsg);
Assert.assertNotNull(response);
BValue bJson = JsonParser.parse(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertTrue(bJson instanceof BMap);
Expand All @@ -62,10 +59,9 @@ public void testUsingIdentifierLiteralsInServiceAndResourceNames() {
@Test(description = "Test identifier literals payload")
public void testIdentifierLiteralsInPayload() {
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/identifierLiteral/resource2", "GET");
HttpCarbonMessage response = Services.invokeNew(application, MOCK_ENDPOINT_NAME, cMsg);
HttpCarbonMessage response = Services.invoke(MOCK_ENDPOINT_PORT, cMsg);
Assert.assertNotNull(response);
String payload = StringUtils
.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
String payload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertEquals(payload, "hello");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public void testErrorInTupleWithDestructure() {
Assert.assertTrue(((BBoolean) returns[4]).booleanValue());
}

@Test(description = "Test simple error var def inside tuple with destructuring error")
@Test(description = "Test simple error var def inside tuple with destructuring error",
groups = { "brokenOnJBallerina" })
public void testErrorInTupleWithDestructure2() {
BValue[] returns = BRunUtil.invoke(result, "testErrorInTupleWithDestructure2");
Assert.assertEquals(returns.length, 5);
Expand Down Expand Up @@ -233,7 +234,7 @@ public void testNegativeRecordVariables() {
BAssertUtil.validateError(resultNegative, ++i,
incompatibleTypes + "expected 'boolean', found 'string'", 64, 20);
BAssertUtil.validateError(resultNegative, ++i, incompatibleTypes +
"expected '(any,string,map,(error,any))', found '(int,string,error,(error,Foo))'", 78, 58);
"expected '[any,string,map,[error,any]]', found '[int,string,error,[error,Foo]]'", 78, 58);
BAssertUtil.validateError(resultNegative, ++i,
incompatibleTypes + "expected 'boolean', found 'string'", 91, 40);
BAssertUtil.validateError(resultNegative, ++i, incompatibleTypes + "expected 'Bar', found 'map<anydata|error>'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testTupleVarRefBasic1() {
validateVarRefBasicTestResults(returns);
}

@Test(description = "Test tuple var reference 2")
@Test(description = "Test tuple var reference 2", groups = { "brokenOnJBallerina" })
public void testTupleVarRefBasic2() {
BValue[] returns = BRunUtil.invoke(result, "testTupleVarRefBasic2");
validateVarRefBasicTestResults(returns);
Expand All @@ -71,7 +71,7 @@ public void testTupleVarRefBasic4() {
validateVarRefBasicTestResults(returns);
}

@Test(description = "Test tuple var reference 5")
@Test(description = "Test tuple var reference 5", groups = { "brokenOnJBallerina" })
public void testTupleVarRefBasic5() {
BValue[] returns = BRunUtil.invoke(result, "testTupleVarRefBasic5");
validateVarRefBasicTestResults(returns);
Expand Down Expand Up @@ -203,7 +203,7 @@ public void testTupleVarRefWithArray3() {
validateVarRefArrayResults(returns);
}

@Test(description = "Test tuple var reference with array 4")
@Test(description = "Test tuple var reference with array 4", groups = { "brokenOnJBallerina" })
public void testTupleVarRefWithArray4() {
BValue[] returns = BRunUtil.invoke(result, "testTupleVarRefWithArray4");
validateVarRefArrayResults(returns);
Expand Down Expand Up @@ -301,7 +301,7 @@ public void testFieldAndIndexBasedVarRefs() {
Assert.assertEquals(returns[1].stringValue(), "S1");
}

@Test
@Test(groups = { "brokenOnJBallerina" })
public void testNegativeTupleVariablesReferences() {
Assert.assertEquals(resultNegative.getErrorCount(), 20);
int i = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public void testErrorInTupleWithDestructure() {
Assert.assertTrue(((BBoolean) returns[4]).booleanValue());
}

@Test(description = "Test simple error var def inside tuple with destructuring error")
@Test(description = "Test simple error var def inside tuple with destructuring error",
groups = { "brokenOnJBallerina" })
public void testErrorInTupleWithDestructure2() {
BValue[] returns = BRunUtil.invoke(result, "testErrorInTupleWithDestructure2");
Assert.assertEquals(returns.length, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ function getConstWithIL() returns (float) {
return ^"const IL";
}

function defineAndGetIL() returns (string, float, int) {
function defineAndGetIL() returns [string, float, int] {
string ^"local string var" = ^"global var";
float ^"local float var" = ^"const IL";
int ^"local int var" = 99934;
return (^"local string var", ^"local float var", ^"local int var");
return [^"local string var", ^"local float var", ^"local int var"];
}

function useILWithinStruct() returns (string, string, int) {
function useILWithinStruct() returns [string, string, int] {
Person person = {^"first name": "Tom", ^"last name":"hank", ^"current age": 50};
return (person.^"first name", person.^"last name", person.^"current age");
return [person.^"first name", person.^"last name", person.^"current age"];
}

type Person record {
Expand All @@ -31,9 +31,9 @@ type Person record {
int ^"current age";
};

function useILInStructVar() returns (string, string, int) {
function useILInStructVar() returns [string, string, int] {
Person ^"person 1" = {^"first name": "Harry", ^"last name":"potter", ^"current age": 25};
return (^"person 1".^"first name", ^"person 1".^"last name", ^"person 1".^"current age");
return [^"person 1".^"first name", ^"person 1".^"last name", ^"person 1".^"current age"];
}

function useILAsrefType()returns (json) {
Expand All @@ -47,16 +47,16 @@ function useILAsArrayIndex() returns (float) {
return ^"float array"[^"array index"];
}

function passILValuesToFunction() returns (string, int) {
function passILValuesToFunction() returns [string, int] {
string ^"first name" = "Bill";
string ^"last name" = "Kary";
int age = 40;
return passILValuesAsParams(^"first name", ^"last name", age);
}

function passILValuesAsParams(string ^"first name", string ^"last name", int ^"current age") returns (string, int) {
function passILValuesAsParams(string ^"first name", string ^"last name", int ^"current age") returns [string, int] {
string ^"full name" = ^"first name" + " " + ^"last name";
return (^"full name", ^"current age");
return [^"full name", ^"current age"];
}

function testCharInIL() returns (string) {
Expand Down Expand Up @@ -107,9 +107,9 @@ function ^"test function for identifier"(string val) returns (string) {
// return value;
//}

function useILInStructName() returns (string, string, int, string?) {
function useILInStructName() returns [string, string, int, string?] {
^"family person" ^"person one" = {^"first name": "Tom", ^"last name":"hank", ^"current age": 50};
return (^"person one".^"first name", ^"person one".^"last name", ^"person one".^"current age", ^"person one"["first name"]);
return [^"person one".^"first name", ^"person one".^"last name", ^"person one".^"current age", ^"person one"["first name"]];
}

type ^"family person" record {
Expand All @@ -123,9 +123,9 @@ function testUnicodeInIL() returns (string) {
return ^"සිංහල වචනය";
}

function testAcessILWithoutPipe() returns (string, string) {
function testAcessILWithoutPipe() returns [string, string] {
string ^"x" = "hello";
return (^"x", x);
return [^"x", x];
}

function testAcessJSONFielAsIL() returns (json) {
Expand Down
Loading

0 comments on commit a5babc4

Please sign in to comment.