Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ina-lang into completion-engine-revamp
  • Loading branch information
nadeeshaan committed Jan 27, 2020
2 parents 0040abe + bf906ee commit 80c4306
Show file tree
Hide file tree
Showing 19 changed files with 367 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -818,29 +818,29 @@ private void processNonStringValue(ValueType type) throws JsonParserException {
if (ch == 't' && TRUE.equals(str)) {
switch (type) {
case ARRAY_ELEMENT:
((ArrayValue) this.currentJsonNode).append(new Boolean(true));
((ArrayValue) this.currentJsonNode).append(Boolean.TRUE);
break;
case FIELD:
((MapValueImpl<String, Object>) this.currentJsonNode).put(this.fieldNames.pop(),
new Boolean(true));
Boolean.TRUE);
break;
case VALUE:
currentJsonNode = new Boolean(true);
currentJsonNode = Boolean.TRUE;
break;
default:
break;
}
} else if (ch == 'f' && FALSE.equals(str)) {
switch (type) {
case ARRAY_ELEMENT:
((ArrayValue) this.currentJsonNode).append(new Boolean(false));
((ArrayValue) this.currentJsonNode).append(Boolean.FALSE);
break;
case FIELD:
((MapValueImpl<String, Object>) this.currentJsonNode).put(this.fieldNames.pop(),
new Boolean(false));
Boolean.FALSE);
break;
case VALUE:
currentJsonNode = new Boolean(false);
currentJsonNode = Boolean.FALSE;
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ private static ArrayValue convertBooleanArrayToJSON(ArrayValue booleanArray) {
ArrayValue json = new ArrayValueImpl(new BArrayType(BTypes.typeJSON));
for (int i = 0; i < booleanArray.size(); i++) {
boolean value = booleanArray.getBoolean(i);
json.append(new Boolean(value));
json.append(Boolean.valueOf(value));
}
return json;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static Object get(ArrayValue array, long index) {

switch (((BArrayType) array.getType()).getElementType().getTag()) {
case TypeTags.BOOLEAN_TAG:
return new Boolean(array.getBoolean(index));
return Boolean.valueOf(array.getBoolean(index));
case TypeTags.BYTE_TAG:
return new Long(array.getByte(index));
case TypeTags.FLOAT_TAG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BRecordTypeSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructureTypeSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol;
Expand Down Expand Up @@ -349,9 +348,10 @@ private void addAttachedFunctionsToPackageLevel(BLangPackage pkgNode, SymbolEnv
pkgNode.topLevelNodes.add(objectTypeNode.initFunction);
}
} else if (typeDef.symbol.tag == SymTag.RECORD) {
BLangRecordTypeNode recordTypeNod = (BLangRecordTypeNode) typeDef.typeNode;
pkgNode.functions.add(recordTypeNod.initFunction);
pkgNode.topLevelNodes.add(recordTypeNod.initFunction);
BLangRecordTypeNode recordTypeNode = (BLangRecordTypeNode) typeDef.typeNode;
recordTypeNode.initFunction = createInitFunctionForRecordType(recordTypeNode, env);
pkgNode.functions.add(recordTypeNode.initFunction);
pkgNode.topLevelNodes.add(recordTypeNode.initFunction);
}
}
}
Expand Down Expand Up @@ -5956,38 +5956,41 @@ private boolean isDefaultableMappingType(BType type) {

private BLangFunction createInitFunctionForStructureType(BLangStructureTypeNode structureTypeNode, SymbolEnv env,
Name suffix) {
String structTypeName = structureTypeNode.type.tsymbol.name.value;
BLangFunction initFunction = ASTBuilderUtil
.createInitFunctionWithNilReturn(structureTypeNode.pos, Names.EMPTY.value, suffix);
.createInitFunctionWithNilReturn(structureTypeNode.pos, structTypeName, suffix);

// Create the receiver
// Create the receiver and add receiver details to the node
initFunction.receiver = ASTBuilderUtil.createReceiver(structureTypeNode.pos, structureTypeNode.type);
BVarSymbol receiverSymbol = new BVarSymbol(Flags.asMask(EnumSet.noneOf(Flag.class)),
names.fromIdNode(initFunction.receiver.name),
env.enclPkg.symbol.pkgID, structureTypeNode.type, null);
names.fromIdNode(initFunction.receiver.name),
env.enclPkg.symbol.pkgID, structureTypeNode.type, null);
initFunction.receiver.symbol = receiverSymbol;

initFunction.type = new BInvokableType(new ArrayList<>(), symTable.nilType, null);
initFunction.attachedFunction = true;
initFunction.flagSet.add(Flag.ATTACHED);

// Create function symbol
Name funcSymbolName = names.fromString(Symbols.getAttachedFuncSymbolName(
structureTypeNode.type.tsymbol.name.value, suffix.value));
// Create the function type
initFunction.type = new BInvokableType(new ArrayList<>(), symTable.nilType, null);

// Create the function symbol
Name funcSymbolName = names.fromString(Symbols.getAttachedFuncSymbolName(structTypeName, suffix.value));
initFunction.symbol = Symbols
.createFunctionSymbol(Flags.asMask(initFunction.flagSet), funcSymbolName, env.enclPkg.symbol.pkgID,
initFunction.type, structureTypeNode.symbol.scope.owner,
initFunction.body != null);
initFunction.type, structureTypeNode.symbol, initFunction.body != null);
initFunction.symbol.scope = new Scope(initFunction.symbol);
initFunction.symbol.scope.define(receiverSymbol.name, receiverSymbol);
initFunction.symbol.receiverSymbol = receiverSymbol;
initFunction.name = ASTBuilderUtil.createIdentifier(structureTypeNode.pos, funcSymbolName.value);

// Create the function type symbol
BInvokableTypeSymbol tsymbol = Symbols.createInvokableTypeSymbol(SymTag.FUNCTION_TYPE,
initFunction.symbol.flags, env.enclPkg.packageID, null,
initFunction.symbol);
initFunction.type.tsymbol = tsymbol;
initFunction.symbol.flags,
env.enclPkg.packageID, initFunction.type,
initFunction.symbol);
tsymbol.params = initFunction.symbol.params;
tsymbol.restParam = initFunction.symbol.restParam;
tsymbol.returnType = initFunction.symbol.retType;
initFunction.type.tsymbol = tsymbol;

receiverSymbol.owner = initFunction.symbol;

Expand All @@ -6012,13 +6015,13 @@ private BLangFunction createInitFunctionForObjectType(BLangObjectTypeNode struct
return rewrite(initFunction, env);
}

private BLangFunction createInitFunctionForRecordType(BLangRecordTypeNode structureTypeNode, SymbolEnv env) {
BLangFunction initFunction = createInitFunctionForStructureType(structureTypeNode, env,
Names.INIT_FUNCTION_SUFFIX);
BStructureTypeSymbol typeSymbol = ((BStructureTypeSymbol) structureTypeNode.type.tsymbol);
typeSymbol.initializerFunc = new BAttachedFunction(Names.INIT_FUNCTION_SUFFIX, initFunction.symbol,
(BInvokableType) initFunction.type);
structureTypeNode.initFunction = initFunction;
private BLangFunction createInitFunctionForRecordType(BLangRecordTypeNode recordTypeNode, SymbolEnv env) {
BLangFunction initFunction = createInitFunctionForStructureType(recordTypeNode, env,
Names.INIT_FUNCTION_SUFFIX);
BRecordTypeSymbol typeSymbol = ((BRecordTypeSymbol) recordTypeNode.type.tsymbol);
typeSymbol.initializerFunc = new BAttachedFunction(initFunction.symbol.name, initFunction.symbol,
(BInvokableType) initFunction.type);
recordTypeNode.initFunction = initFunction;
return rewrite(initFunction, env);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public class SemanticAnalyzer extends BLangNodeVisitor {
private DiagnosticCode diagCode;
private BType resType;

private Map<BVarSymbol, BType.NarrowedTypes> narrowedTypeInfo;
// Stack holding the fall-back environments. fall-back env is the env to go back
// after visiting the current env.
private Stack<SymbolEnv> prevEnvs = new Stack<>();
Expand Down Expand Up @@ -445,7 +446,6 @@ public void visit(BLangObjectTypeNode objectTypeNode) {
public void visit(BLangRecordTypeNode recordTypeNode) {
SymbolEnv recordEnv = SymbolEnv.createTypeEnv(recordTypeNode, recordTypeNode.symbol.scope, env);
recordTypeNode.fields.forEach(field -> analyzeDef(field, recordEnv));
analyzeDef(recordTypeNode.initFunction, recordEnv);
validateDefaultable(recordTypeNode);
}

Expand Down Expand Up @@ -2097,13 +2097,27 @@ public void visit(BLangIf ifNode) {
dlog.error(ifNode.expr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, symTable.booleanType, actualType);
}

Map<BVarSymbol, BType.NarrowedTypes> prevNarrowedTypeInfo = this.narrowedTypeInfo;

SymbolEnv ifEnv = typeNarrower.evaluateTruth(ifNode.expr, ifNode.body, env);

this.narrowedTypeInfo = new HashMap<>();

analyzeStmt(ifNode.body, ifEnv);

if (ifNode.expr.narrowedTypeInfo == null || ifNode.expr.narrowedTypeInfo.isEmpty()) {
ifNode.expr.narrowedTypeInfo = this.narrowedTypeInfo;
}

if (prevNarrowedTypeInfo != null) {
prevNarrowedTypeInfo.putAll(this.narrowedTypeInfo);
}

if (ifNode.elseStmt != null) {
SymbolEnv elseEnv = typeNarrower.evaluateFalsity(ifNode.expr, ifNode.elseStmt, env);
analyzeStmt(ifNode.elseStmt, elseEnv);
}
this.narrowedTypeInfo = prevNarrowedTypeInfo;
}

@Override
Expand Down Expand Up @@ -2851,16 +2865,24 @@ private void resetTypeNarrowing(BLangExpression lhsExpr, BType rhsType) {
// then the type narrowing will no longer hold. Thus define the original
// symbol in all the scopes that are affected by this assignment.
if (!types.isAssignable(rhsType, varSymbol.type)) {
defineOriginalSymbol(lhsExpr, varSymbol.originalSymbol, env);
env = prevEnvs.peek();
if (this.narrowedTypeInfo != null) {
// Record the vars for which type narrowing was unset, to define relevant shadowed symbols in branches.
BType currentType = ((BLangSimpleVarRef) lhsExpr).symbol.type;
this.narrowedTypeInfo.put(typeNarrower.getOriginalVarSymbol(varSymbol),
new BType.NarrowedTypes(currentType, currentType));
}

defineOriginalSymbol(lhsExpr, typeNarrower.getOriginalVarSymbol(varSymbol), env);
env = prevEnvs.pop();
}
}

private void defineOriginalSymbol(BLangExpression lhsExpr, BVarSymbol varSymbol, SymbolEnv env) {
BSymbol foundSym = symResolver.lookupSymbol(env, varSymbol.name, varSymbol.tag);

// Terminate if we reach the env where the original symbol available
// Terminate if we reach the env where the original symbol is available.
if (foundSym == varSymbol) {
prevEnvs.push(env);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1306,11 +1306,6 @@ private void defineMembers(List<BLangTypeDefinition> typeDefNodes, SymbolEnv pkg
defineReferencedFunction(typeDef, objMethodsEnv, typeRef, function, referencedFunctions);
}
}
} else if (typeDef.symbol.kind == SymbolKind.RECORD) {
// Create typeDef type
BLangRecordTypeNode recordTypeNode = (BLangRecordTypeNode) typeDef.typeNode;
SymbolEnv typeDefEnv = SymbolEnv.createPkgLevelSymbolEnv(recordTypeNode, typeDef.symbol.scope, pkgEnv);
defineRecordInitFunction(typeDef, typeDefEnv);
}
}
}
Expand Down Expand Up @@ -1486,20 +1481,6 @@ private void defineObjectInitFunction(BLangObjectTypeNode object, SymbolEnv conE
defineNode(initFunction, conEnv);
}

private void defineRecordInitFunction(BLangTypeDefinition typeDef, SymbolEnv conEnv) {
BLangRecordTypeNode recordTypeNode = (BLangRecordTypeNode) typeDef.typeNode;
recordTypeNode.initFunction = ASTBuilderUtil.createInitFunctionWithNilReturn(typeDef.pos, "",
Names.INIT_FUNCTION_SUFFIX);

recordTypeNode.initFunction.receiver = createReceiver(typeDef.pos, typeDef.name);
recordTypeNode.initFunction.attachedFunction = true;
recordTypeNode.initFunction.flagSet.add(Flag.ATTACHED);

// Adding record level variables to the init function is done at desugar phase

defineNode(recordTypeNode.initFunction, conEnv);
}

private void defineAttachedFunctions(BLangFunction funcNode, BInvokableSymbol funcSymbol,
SymbolEnv invokableEnv, boolean isValidAttachedFunc) {
BTypeSymbol typeSymbol = funcNode.receiver.type.tsymbol;
Expand Down Expand Up @@ -1599,19 +1580,6 @@ private StatementNode createAssignmentStmt(BLangSimpleVariable variable, BVarSym
return assignmentStmt;
}

private BLangSimpleVariable createReceiver(DiagnosticPos pos, BLangIdentifier name) {
BLangSimpleVariable receiver = (BLangSimpleVariable) TreeBuilder.createSimpleVariableNode();
receiver.pos = pos;
BLangIdentifier identifier = (BLangIdentifier) createIdentifier(Names.SELF.getValue());
identifier.pos = pos;
receiver.setName(identifier);
BLangUserDefinedType structTypeNode = (BLangUserDefinedType) TreeBuilder.createUserDefinedTypeNode();
structTypeNode.pkgAlias = new BLangIdentifier();
structTypeNode.typeName = name;
receiver.setTypeNode(structTypeNode);
return receiver;
}

private IdentifierNode createIdentifier(String value) {
IdentifierNode node = TreeBuilder.createIdentifierNode();
if (value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ public void visit(BLangRecordTypeNode recordNode) {
BSymbol objectSymbol = recordNode.symbol;
SymbolEnv objectEnv = SymbolEnv.createPkgLevelSymbolEnv(recordNode, objectSymbol.scope, env);
recordNode.fields.forEach(field -> analyzeNode(field, objectEnv));
analyzeNode(recordNode.initFunction, objectEnv);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ public SymbolEnv evaluateTruth(BLangExpression expr, BLangNode targetNode, Symbo
* @return target environment
*/
public SymbolEnv evaluateFalsity(BLangExpression expr, BLangNode targetNode, SymbolEnv env) {
Map<BVarSymbol, NarrowedTypes> narroedTypes = getNarrowedTypes(expr, env);
if (narroedTypes.isEmpty()) {
Map<BVarSymbol, NarrowedTypes> narrowedTypes = getNarrowedTypes(expr, env);
if (narrowedTypes.isEmpty()) {
return env;
}

SymbolEnv targetEnv = getTargetEnv(targetNode, env);
narroedTypes.forEach((symbol, typeInfo) -> {
narrowedTypes.forEach((symbol, typeInfo) -> {
symbolEnter.defineTypeNarrowedSymbol(expr.pos, targetEnv, getOriginalVarSymbol(symbol), typeInfo.falseType);
});

Expand Down Expand Up @@ -194,13 +194,15 @@ private void analyzeExpr(BLangExpression expr, SymbolEnv env) {
case UNARY_EXPR:
break;
default:
expr.narrowedTypeInfo = new HashMap<>();
if (expr.narrowedTypeInfo == null) {
expr.narrowedTypeInfo = new HashMap<>();
}
return;
}

SymbolEnv prevEnv = this.env;
this.env = env;
if (expr != null && expr.narrowedTypeInfo == null) {
if (expr.narrowedTypeInfo == null) {
expr.narrowedTypeInfo = new HashMap<>();
expr.accept(this);
}
Expand Down Expand Up @@ -298,7 +300,7 @@ private BType getTypeUnion(BType currentType, BType targetType) {
return BUnionType.create(null, union);
}

private BVarSymbol getOriginalVarSymbol(BVarSymbol varSymbol) {
BVarSymbol getOriginalVarSymbol(BVarSymbol varSymbol) {
if (varSymbol.originalSymbol == null) {
return varSymbol;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/object-methods/object_methods.bal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ballerina/io;

// Defines an object called `Person`. It has method definitions both inside and outside the object.
// Defines an object called `Person`.
type Person object {
public int age;
public string firstName;
Expand All @@ -13,7 +13,7 @@ type Person object {
self.lastName = lastName;
}

// A method defined within the object.
// A method returning the full name value of the `Person` object.
function getFullName() returns string {
return self.firstName + " " + self.lastName;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/record-type-reference/record_type_reference.bal
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type Manager record {

public function main() {
// `Employee` has all the fields of `Person`.
Employee john = { name: "John Doe", designation: "Software Engineer" };
Employee jane = { name: "Jane Doe", designation: "UX Engineer" };
Employee john = {name: "John Doe", designation: "Software Engineer"};
Employee jane = {name: "Jane Doe", designation: "UX Engineer"};

// Type referencing copies the fields including their properties
// (e.g., type, default value, optional status). As it can be seen
Expand All @@ -32,7 +32,7 @@ public function main() {
io:println(john);
io:println(jane);

Manager mgr = { name: "Mark", age: 35, designation: "Engineering Manager" };
Manager mgr = {name: "Mark", age: 35, designation: "Engineering Manager"};
mgr.team = [john, jane];
mgr.company = "XYZ Inc.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ public static ArrayValue slice(Strand strand, ArrayValue arr, long startIndex, l

switch (arrType.getTag()) {
case TypeTags.ARRAY_TAG:
slicedArr = new ArrayValueImpl((BArrayType) arrType);
elemTypeTag = ((BArrayType) arrType).getElementType().getTag();
BType elementType = ((BArrayType) arrType).getElementType();
slicedArr = new ArrayValueImpl(new BArrayType(elementType));
elemTypeTag = elementType.getTag();
getFn = ArrayValue::get;
break;
case TypeTags.TUPLE_TAG:
Expand Down
Loading

0 comments on commit 80c4306

Please sign in to comment.