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

Address review Suggestions #8

Closed
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 @@ -91,10 +91,6 @@ public Object instantiate(Strand s) {
return instantiate(s, new BInitialValueEntry[0]);
}

public MapValue getAnnotations() {
return annotations;
}

@Override
public Object instantiate(Strand s, BInitialValueEntry[] initialValues) {
Type referredType = getReferredType(this.describingType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ public void visit(BLangTypeConversionExpr astTypeConversionExpr) {
public void visit(BLangStructLiteral astStructLiteralExpr) {
List<BIROperand> varDcls = mapToVarDcls(astStructLiteralExpr.enclMapSymbols);
BType type = astStructLiteralExpr.getBType();
visitTypedesc(astStructLiteralExpr.pos, type, varDcls, getAnnotations(type.tsymbol, this.env));
visitTypedesc(astStructLiteralExpr.pos, type, varDcls);

BIRVariableDcl tempVarDcl = new BIRVariableDcl(astStructLiteralExpr.getBType(),
this.env.nextLocalVarId(names), VarScope.FUNCTION, VarKind.TEMP);
Expand Down Expand Up @@ -1672,33 +1672,18 @@ public void visit(BLangSimpleVarRef.BLangFieldVarRef fieldVarRef) {
public void visit(BLangArrayLiteral astArrayLiteralExpr) {
BType bType = astArrayLiteralExpr.getBType();
if (bType.tag == TypeTags.TUPLE) {
visitTypedesc(astArrayLiteralExpr.pos, bType, Collections.emptyList(),
getAnnotations(bType.tsymbol, this.env));
visitTypedesc(astArrayLiteralExpr.pos, bType, Collections.emptyList());
}
generateListConstructorExpr(astArrayLiteralExpr);
}

@Override
public void visit(BLangTupleLiteral tupleLiteral) {
BType type = tupleLiteral.getBType();
visitTypedesc(tupleLiteral.pos, type, Collections.emptyList(), getAnnotations(type.tsymbol, this.env));
visitTypedesc(tupleLiteral.pos, type, Collections.emptyList());
generateListConstructorExpr(tupleLiteral);
}

private BIROperand getAnnotations(BTypeSymbol typeSymbol, BIRGenEnv env) {
if (typeSymbol == null || typeSymbol.annotations == null) {
return null;
}
return new BIROperand(getAnnotations(typeSymbol.annotations, env));
}

private BIRVariableDcl getAnnotations(BVarSymbol annotations, BIRGenEnv env) {
if (env.symbolVarMap.containsKey(annotations)) {
return env.symbolVarMap.get(annotations);
}
return globalVarMap.get(annotations);
}

@Override
public void visit(BLangGroupExpr groupExpr) {
groupExpr.expression.accept(this);
Expand All @@ -1708,8 +1693,7 @@ public void visit(BLangGroupExpr groupExpr) {
public void visit(BLangJSONArrayLiteral jsonArrayLiteralExpr) {
BType bType = jsonArrayLiteralExpr.getBType();
if (bType.tag == TypeTags.TUPLE) {
visitTypedesc(jsonArrayLiteralExpr.pos, bType, Collections.emptyList(),
getAnnotations(bType.tsymbol, this.env));
visitTypedesc(jsonArrayLiteralExpr.pos, bType, Collections.emptyList());
}
generateListConstructorExpr(jsonArrayLiteralExpr);
}
Expand Down Expand Up @@ -2267,21 +2251,19 @@ public void visit(BLangSimpleVarRef.BLangTypeLoad typeLoad) {
}

private void visitTypedesc(Location pos, BType type, List<BIROperand> varDcls) {
visitTypedesc(pos, type, varDcls, null);
}

private void visitTypedesc(Location pos, BType type, List<BIROperand> varDcls, BIROperand annotations) {
BIRVariableDcl tempVarDcl = new BIRVariableDcl(symTable.typeDesc, this.env.nextLocalVarId(names),
VarScope.FUNCTION, VarKind.TEMP);
this.env.enclFunc.localVars.add(tempVarDcl);
BIRGenEnv env = this.env;
env.enclFunc.localVars.add(tempVarDcl);
BIROperand toVarRef = new BIROperand(tempVarDcl);
BIROperand annotations = getAnnotations(type.tsymbol, env);
if (annotations != null) {
setScopeAndEmit(new BIRNonTerminator.NewTypeDesc(pos, toVarRef, type, varDcls, annotations));
this.env.targetOperand = toVarRef;
env.targetOperand = toVarRef;
return;
}
setScopeAndEmit(new BIRNonTerminator.NewTypeDesc(pos, toVarRef, type, varDcls));
this.env.targetOperand = toVarRef;
env.targetOperand = toVarRef;
}

@Override
Expand Down Expand Up @@ -3052,4 +3034,17 @@ private List<BIRAnnotationAttachment> getBIRAnnotAttachmentsForASTAnnotAttachmen
return annotationAttachments;
}

private BIROperand getAnnotations(BTypeSymbol typeSymbol, BIRGenEnv env) {
if (typeSymbol == null || typeSymbol.annotations == null) {
return null;
}
return new BIROperand(getAnnotations(typeSymbol.annotations, env));
}

private BIRVariableDcl getAnnotations(BVarSymbol annotations, BIRGenEnv env) {
if (env.symbolVarMap.containsKey(annotations)) {
return env.symbolVarMap.get(annotations);
}
return globalVarMap.get(annotations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ private void createTypeDescConstructor(ClassWriter cw) {
}

private void createTypeDescConstructorWithAnnotations(ClassWriter cw, String name) {

String descriptor = TYPE_DESC_CONSTRUCTOR_WITH_ANNOTATIONS;
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, JVM_INIT_METHOD, descriptor, null, null);
mv.visitCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ private BLangLambdaFunction addReturnAndDefineLambda(BLangFunction function, BLa
return lambdaFunction;
}

public void addAnnotsToLiteral(List<BLangAnnotationAttachment> nodeAttachments, BLangRecordLiteral mapLiteral,
private void addAnnotsToLiteral(List<BLangAnnotationAttachment> nodeAttachments, BLangRecordLiteral mapLiteral,
Location pos, SymbolEnv env, boolean isLocalObjectCtor) {
Map<BAnnotationSymbol, List<BLangAnnotationAttachment>> attachments =
collectAnnotationAttachments(nodeAttachments, env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,17 @@ public void visit(BLangTupleTypeNode tupleTypeNode) {

private void desugarFieldAnnotations(BSymbol owner, BTypeSymbol typeSymbol, List<BLangSimpleVariable> fields,
Location pos) {
if (owner.getKind() != SymbolKind.PACKAGE) {
owner = getOwner(env);
BLangLambdaFunction lambdaFunction = annotationDesugar.defineFieldAnnotations(fields, pos, env.enclPkg, env,
typeSymbol.pkgID, owner);
if (lambdaFunction != null) {
boolean isPackageLevel = owner.getKind() == SymbolKind.PACKAGE;
BInvokableSymbol invokableSymbol = createSimpleVariable(lambdaFunction.function, lambdaFunction,
isPackageLevel);
typeSymbol.annotations = createSimpleVariable(invokableSymbol, isPackageLevel);
}
if (owner.getKind() == SymbolKind.PACKAGE) {
return;
}
owner = getOwner(env);
BLangLambdaFunction lambdaFunction = annotationDesugar.defineFieldAnnotations(fields, pos, env.enclPkg, env,
typeSymbol.pkgID, owner);
if (lambdaFunction != null) {
boolean isPackageLevelAnnotationClosure = owner.getKind() == SymbolKind.PACKAGE;
BInvokableSymbol invokableSymbol = createSimpleVariable(lambdaFunction.function, lambdaFunction,
isPackageLevelAnnotationClosure);
typeSymbol.annotations = createSimpleVariable(invokableSymbol, isPackageLevelAnnotationClosure);
}
}

Expand Down Expand Up @@ -588,42 +589,45 @@ public BInvokableSymbol createSimpleVariable(BLangFunction function, BLangLambda
boolean isAnnotationClosure) {
BInvokableSymbol invokableSymbol = function.symbol;
BType type = function.getBType();
BInvokableSymbol varSymbol = new BInvokableSymbol(SymTag.VARIABLE, 0, invokableSymbol.name,
invokableSymbol.pkgID, type,
invokableSymbol.owner, function.pos, VIRTUAL);
Location pos = function.pos;
Name name = invokableSymbol.name;
BInvokableSymbol varSymbol = new BInvokableSymbol(SymTag.VARIABLE, 0, name, invokableSymbol.pkgID, type,
invokableSymbol.owner, pos, VIRTUAL);
varSymbol.params = invokableSymbol.params;
varSymbol.restParam = invokableSymbol.restParam;
varSymbol.retType = invokableSymbol.retType;
BLangSimpleVariable simpleVariable = ASTBuilderUtil.createVariable(function.pos, function.name.value, type,
lambdaFunction, varSymbol);
BLangSimpleVariableDef variableDef = ASTBuilderUtil.createVariableDef(function.pos);
variableDef.var = simpleVariable;
variableDef.setBType(type);
if (isAnnotationClosure) {
annotationClosureReferences.add(variableDef);
return varSymbol;
}
queue.add(variableDef);
BLangSimpleVariableDef variableDef = createSimpleVariableDef(pos, name.value, type, lambdaFunction, varSymbol);
addToQueue(variableDef, isAnnotationClosure);
return varSymbol;
}

public BVarSymbol createSimpleVariable(BInvokableSymbol invokableSymbol, boolean isAnnotationClosure) {
private BVarSymbol createSimpleVariable(BInvokableSymbol invokableSymbol, boolean isAnnotationClosure) {
BType type = invokableSymbol.retType;
Location pos = invokableSymbol.pos;
Name name = invokableSymbol.name;
BVarSymbol varSymbol = new BVarSymbol(0, name, invokableSymbol.originalName, invokableSymbol.pkgID, type,
invokableSymbol.owner, pos, VIRTUAL);
BLangSimpleVariable simpleVariable = ASTBuilderUtil.createVariable(pos, name.value, type,
getInvocation(invokableSymbol), varSymbol);
BLangSimpleVariableDef variableDef = createSimpleVariableDef(pos, name.value, type,
getInvocation(invokableSymbol), varSymbol);
addToQueue(variableDef, isAnnotationClosure);
return varSymbol;
}

private BLangSimpleVariableDef createSimpleVariableDef(Location pos, String name, BType type, BLangExpression expr,
BVarSymbol varSymbol) {
BLangSimpleVariable simpleVariable = ASTBuilderUtil.createVariable(pos, name, type, expr, varSymbol);
BLangSimpleVariableDef variableDef = ASTBuilderUtil.createVariableDef(pos);
variableDef.var = simpleVariable;
variableDef.setBType(type);
return variableDef;
}

private void addToQueue(BLangSimpleVariableDef variableDef, boolean isAnnotationClosure) {
if (isAnnotationClosure) {
annotationClosureReferences.add(variableDef);
return varSymbol;
return;
}
queue.add(variableDef);
return varSymbol;
}

private BLangInvocation getInvocation(BInvokableSymbol symbol) {
Expand Down Expand Up @@ -910,7 +914,10 @@ public void visit(BLangRecordLiteral recordLiteral) {
(BLangRecordLiteral.BLangRecordKeyValueField) field;
keyValueField.key.expr = rewriteExpr(keyValueField.key.expr);
keyValueField.valueExpr = rewriteExpr(keyValueField.valueExpr);
} else if (field.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
} else if (field.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRefField = (BLangSimpleVarRef) field;
rewriteExpr(varRefField);
} else {
BLangRecordLiteral.BLangRecordSpreadOperatorField spreadOpField =
(BLangRecordLiteral.BLangRecordSpreadOperatorField) field;
spreadOpField.expr = rewriteExpr(spreadOpField.expr);
Expand Down Expand Up @@ -1194,10 +1201,6 @@ public void visit(BLangWorkerFlushExpr workerFlushExpr) {
public void visit(BLangSimpleVarRef.BLangLocalVarRef localVarRef) {
BLangInvokableNode encInvokable = env.enclInvokable;
BSymbol symbol = localVarRef.symbol;
if (encInvokable == null || (symbol.tag & SymTag.VARIABLE) != SymTag.VARIABLE) {
result = localVarRef;
return;
}
updateClosureVariable((BVarSymbol) symbol, encInvokable, localVarRef.pos);
result = localVarRef;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2023, 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 @@ -16,12 +16,9 @@
*/
package org.ballerinalang.test.annotations;

import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.internal.TypeChecker;
import io.ballerina.runtime.internal.types.BAnnotatableType;
import io.ballerina.runtime.internal.values.TupleValueImpl;
import io.ballerina.runtime.internal.values.TypedescValue;
import org.ballerinalang.test.BCompileUtil;
import org.ballerinalang.test.BRunUtil;
Expand Down Expand Up @@ -50,38 +47,17 @@ public void testLocalRecordAnnotations(String function) {
BRunUtil.invoke(result, function);
}

@Test(description = "Test annotations of tuple members from the type")
public void testTupleAnnotations1() {
Object returns = BRunUtil.invoke(result, "testTupleMemberAnnotations1", new Object[]{});
TupleValueImpl returnTuple = (TupleValueImpl) returns;

Object annot1 = ((BAnnotatableType) returnTuple.getType()).getAnnotation(StringUtils.fromString("$field$.0"));
Object annot2 = ((BAnnotatableType) returnTuple.getType()).getAnnotation(StringUtils.fromString("$field$.1"));

Assert.assertTrue(annot1 instanceof BMap);
Assert.assertTrue(annot2 instanceof BMap);
}

@Test(description = "Test annotations of tuple members from the type")
public void testTupleAnnotations2() {
Object returns = BRunUtil.invoke(result, "testTupleMemberAnnotations2", new Object[]{});
TupleValueImpl returnTuple = (TupleValueImpl) returns;

Object annot1 = ((BAnnotatableType) returnTuple.getType()).getAnnotation(StringUtils.fromString("$field$.0"));

Assert.assertTrue(annot1 instanceof BMap);
}

@DataProvider(name = "dataToTestAnnotationsOfLocalTuple")
public Object[] dataToTestAnnotationsOfLocalTuple() {
return new String[]{
"testAnnotationOnTupleFields",
"testAnnotationOnTupleFields2",
"testAnnotationOnTupleWithGlobalVariable",
"testMultipleAnnotationsOnLocalTuple",
"testGlobalAnnotationsOnFunctionPointerReturnType",
"testTupleAnnotationsOnFunctionPointerReturnType",
"testGlobalAnnotationsOnFunctionReturnType",
"testGlobalAnnotationsOnFunctionParameterType"
"testGlobalAnnotationsOnFunctionParameterType",
"testTupleMemberAnnotations"
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function() returns [int] x = function() returns [@annotOne {value: "foo"} int] {
function() returns [int] x2 = function() returns [@annotOne {value: gVar1} @annotTwo {value: gVar2} int] {return [1];};
function() returns [int, int] x3 = function() returns [@annotOne {value: gVar1} int, @details {name: "name", age: gVar3} int] {return [1, 1];};

function testGlobalAnnotationsOnFunctionPointerReturnType() {
function testTupleAnnotationsOnFunctionPointerReturnType() {
map<any> m1 = getLocalTupleAnnotations(typeof x(), "$field$.0");
map<any> m2 = getLocalTupleAnnotations(typeof x2(), "$field$.0");
map<any> m3 = getLocalTupleAnnotations(typeof x3(), "$field$.0");
Expand All @@ -91,6 +91,16 @@ function testTupleMemberAnnotations2() returns [@annotOne {value: gVar1} @annotT
return [1];
}

function testTupleMemberAnnotations() {
map<any> m1 = getLocalTupleAnnotations(typeof testTupleMemberAnnotations1(), "$field$.0");
map<any> m2 = getLocalTupleAnnotations(typeof testTupleMemberAnnotations1(), "$field$.1");
map<any> m3 = getLocalTupleAnnotations(typeof testTupleMemberAnnotations2(), "$field$.0");
assertEquality({value: "foo"}, <map<anydata>>m1["annotOne"]);
assertEquality({name: "name", age: 10}, <map<anydata>>m2["details"]);
assertEquality({value: "bar"}, <map<anydata>>m3["annotOne"]);
assertEquality({value: "baz"}, <map<anydata>>m3["annotTwo"]);
}

string gVar2 = "baz";
int gVar3 = 10;

Expand Down