Skip to content

Commit

Permalink
Merge pull request #43583 from lochana-chathura/semIntegration/direct…
Browse files Browse the repository at this point in the history
…_resolve

Refactor semtype creation
  • Loading branch information
lochana-chathura authored Nov 19, 2024
2 parents 8e3f67a + b38c20b commit 802f909
Show file tree
Hide file tree
Showing 75 changed files with 606 additions and 889 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private BInvokableType duplicateType(BInvokableType original, List<BVarSymbol> n
}

BInvokableType duplicate =
new BInvokableType(original.env, paramTypes, original.restType, original.retType, null);
new BInvokableType(types.typeEnv(), paramTypes, original.restType, original.retType, null);
BInvokableTypeSymbol originalSym = (BInvokableTypeSymbol) original.tsymbol;
BInvokableTypeSymbol duplicateTSym = new BInvokableTypeSymbol(original.tag, originalSym.flags,
originalSym.pkgID, duplicate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BAnyType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BAnydataType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BBuiltInRefType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BErrorType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BField;
import org.wso2.ballerinalang.compiler.semantics.model.types.BFiniteType;
Expand Down Expand Up @@ -98,10 +97,6 @@ public void visit(BArrayType bArrayType) {
find(bArrayType.eType);
}

@Override
public void visit(BBuiltInRefType bBuiltInRefType) {
}

@Override
public void visit(BAnyType bAnyType) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.compiler.api.impl;

import io.ballerina.types.Env;
import org.ballerinalang.model.symbols.AnnotationAttachmentSymbol;
import org.ballerinalang.model.symbols.SymbolKind;
import org.wso2.ballerinalang.compiler.semantics.analyzer.Types;
Expand All @@ -30,7 +31,6 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BAnyType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BAnydataType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BBuiltInRefType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BErrorType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BField;
import org.wso2.ballerinalang.compiler.semantics.model.types.BFiniteType;
Expand Down Expand Up @@ -74,9 +74,12 @@ public class TypeParamResolver implements BTypeVisitor<BType, BType> {
private final Map<BType, BType> boundTypes = new HashMap<>();
private final BType typeParam;
private final Types types;
private final Env typeEnv;

public TypeParamResolver(BType typeParam, CompilerContext context) {
this.typeParam = typeParam;
types = Types.getInstance(context);
this.typeEnv = types.typeEnv();
}

/**
Expand Down Expand Up @@ -114,11 +117,6 @@ public BType visit(BType typeInSymbol, BType boundType) {
return typeInSymbol;
}

@Override
public BType visit(BBuiltInRefType typeInSymbol, BType boundType) {
return typeInSymbol;
}

@Override
public BType visit(BAnyType typeInSymbol, BType boundType) {
return typeInSymbol;
Expand All @@ -137,7 +135,7 @@ public BType visit(BMapType typeInSymbol, BType boundType) {
return typeInSymbol;
}

return new BMapType(typeInSymbol.env, typeInSymbol.tag, boundConstraintType, typeInSymbol.tsymbol,
return new BMapType(typeEnv, typeInSymbol.tag, boundConstraintType, typeInSymbol.tsymbol,
typeInSymbol.getFlags());
}

Expand Down Expand Up @@ -165,7 +163,8 @@ public BType visit(BArrayType typeInSymbol, BType boundType) {
return typeInSymbol;
}

return new BArrayType(typeInSymbol.env, boundElemType, typeInSymbol.tsymbol, typeInSymbol.getSize(),
return new BArrayType(typeEnv, boundElemType, typeInSymbol.tsymbol,
typeInSymbol.getSize(),
typeInSymbol.state, typeInSymbol.getFlags());
}

Expand Down Expand Up @@ -198,7 +197,7 @@ public BType visit(BObjectType typeInSymbol, BType boundType) {
BObjectTypeSymbol newTypeSymbol = new BObjectTypeSymbol(objectTypeSymbol.tag, objectTypeSymbol.flags,
objectTypeSymbol.name, objectTypeSymbol.pkgID, objectTypeSymbol.getType(), objectTypeSymbol.owner,
objectTypeSymbol.pos, objectTypeSymbol.origin);
BObjectType newObjectType = new BObjectType(typeInSymbol.env, newTypeSymbol, typeInSymbol.getFlags());
BObjectType newObjectType = new BObjectType(typeEnv, newTypeSymbol, typeInSymbol.getFlags());

newObjectType.fields = newObjectFields;
newTypeSymbol.attachedFuncs = newAttachedFuncs;
Expand All @@ -218,7 +217,7 @@ public BType visit(BRecordType typeInSymbol, BType boundType) {
}

BType newRestType = resolve(typeInSymbol.restFieldType, boundType);
BRecordType newRecordType = new BRecordType(typeInSymbol.env, typeInSymbol.tsymbol, typeInSymbol.getFlags());
BRecordType newRecordType = new BRecordType(typeEnv, typeInSymbol.tsymbol, typeInSymbol.getFlags());

newRecordType.fields = newRecordFields;
newRecordType.restFieldType = newRestType;
Expand All @@ -245,7 +244,7 @@ public BType visit(BTupleType typeInSymbol, BType boundType) {
return typeInSymbol;
}

return new BTupleType(typeInSymbol.env, typeInSymbol.tsymbol, newTupleMembers, newRestType,
return new BTupleType(typeEnv, typeInSymbol.tsymbol, newTupleMembers, newRestType,
typeInSymbol.getFlags(), typeInSymbol.isCyclic);
}

Expand All @@ -258,7 +257,7 @@ public BType visit(BStreamType typeInSymbol, BType boundType) {
return typeInSymbol;
}

return new BStreamType(types.typeEnv(), typeInSymbol.tag, boundConstraintType, typeInSymbol.completionType,
return new BStreamType(typeEnv, typeInSymbol.tag, boundConstraintType, typeInSymbol.completionType,
typeInSymbol.tsymbol);
}

Expand All @@ -271,7 +270,7 @@ public BType visit(BTableType typeInSymbol, BType boundType) {
return typeInSymbol;
}

BTableType bTableType = new BTableType(types.typeEnv(), typeInSymbol.tag, boundConstraintType,
BTableType bTableType = new BTableType(typeEnv, boundConstraintType,
typeInSymbol.tsymbol,
typeInSymbol.getFlags());
bTableType.keyTypeConstraint = typeInSymbol.keyTypeConstraint;
Expand Down Expand Up @@ -313,7 +312,7 @@ public BType visit(BInvokableType typeInSymbol, BType boundType) {
}

invokableTypeSymbol.returnType = newReturnType;
BInvokableType type = new BInvokableType(typeInSymbol.env, newParamTypes, newRestParamType, newReturnType,
BInvokableType type = new BInvokableType(typeEnv, newParamTypes, newRestParamType, newReturnType,
invokableTypeSymbol);
invokableTypeSymbol.type = type;

Expand All @@ -339,7 +338,7 @@ public BType visit(BUnionType typeInSymbol, BType boundType) {
return typeInSymbol;
}

return BUnionType.create(types.typeEnv(), typeInSymbol.tsymbol, newMembers);
return BUnionType.create(typeEnv, typeInSymbol.tsymbol, newMembers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BType;
import org.wso2.ballerinalang.compiler.util.CompilerContext;
import org.wso2.ballerinalang.compiler.util.Names;
import org.wso2.ballerinalang.compiler.util.TypeTags;
import org.wso2.ballerinalang.util.Flags;

import static org.ballerinalang.model.symbols.SymbolOrigin.COMPILED_SOURCE;
Expand Down Expand Up @@ -63,8 +62,7 @@ public FutureTypeSymbol build() {
BTypeSymbol futureTSymbol = Symbols.createTypeSymbol(SymTag.TYPE, Flags.PUBLIC, Names.EMPTY,
symTable.rootPkgSymbol.pkgID, null, symTable.rootPkgSymbol, symTable.builtinPos, COMPILED_SOURCE);

BFutureType futureType = new BFutureType(symTable.typeEnv(), TypeTags.FUTURE, getBType(typeParam),
futureTSymbol);
BFutureType futureType = new BFutureType(symTable.typeEnv(), getBType(typeParam), futureTSymbol);
futureTSymbol.type = futureType;
FutureTypeSymbol futureTypeSymbol = (FutureTypeSymbol) typesFactory.getTypeDescriptor(futureType);
this.typeParam = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BType;
import org.wso2.ballerinalang.compiler.util.CompilerContext;
import org.wso2.ballerinalang.compiler.util.Names;
import org.wso2.ballerinalang.compiler.util.TypeTags;
import org.wso2.ballerinalang.util.Flags;

import java.util.ArrayList;
Expand Down Expand Up @@ -105,7 +104,7 @@ public TableTypeSymbol build() {
symTable.rootPkgSymbol.pkgID, null, symTable.rootPkgSymbol, symTable.builtinPos,
symTable.rootPkgSymbol.origin);

BTableType tableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, rowBType, tableSymbol);
BTableType tableType = new BTableType(symTable.typeEnv(), rowBType, tableSymbol);
tableSymbol.type = tableType;
if (!keyTypes.isEmpty()) {
tableType.keyTypeConstraint = getKeyConstraintBType(keyTypes, rowType);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* @since 0.94
*/
public interface ConstrainedType extends ReferenceType {
public interface ConstrainedType {

Type getConstraint();
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols;
import org.wso2.ballerinalang.compiler.semantics.model.types.BAnnotationType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BAnyType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BErrorType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BField;
Expand Down Expand Up @@ -1396,7 +1397,7 @@ public BType readType(int cpI) throws IOException {
bStreamType.setFlags(flags);
return bStreamType;
case TypeTags.TABLE:
BTableType bTableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, null,
BTableType bTableType = new BTableType(symTable.typeEnv(), null,
symTable.tableType.tsymbol, flags);
bTableType.constraint = readTypeFromCp();

Expand Down Expand Up @@ -1451,11 +1452,7 @@ public BType readType(int cpI) throws IOException {
return setTSymbolForInvokableType(bInvokableType, bInvokableType.retType);
// All the above types are branded types
case TypeTags.ANY:
BType anyNominalType = typeParamAnalyzer.getNominalType(symTable.anyType, name, flags);
return isImmutable(flags) ? getEffectiveImmutableType(anyNominalType,
symTable.anyType.tsymbol.pkgID,
symTable.anyType.tsymbol.owner) :
anyNominalType;
return isImmutable(flags) ? BAnyType.newImmutableBAnyType() : new BAnyType(name, flags);
case TypeTags.HANDLE:
return symTable.handleType;
case TypeTags.READONLY:
Expand Down Expand Up @@ -1632,8 +1629,7 @@ public BType readType(int cpI) throws IOException {

return bTupleType;
case TypeTags.FUTURE:
BFutureType bFutureType = new BFutureType(symTable.typeEnv(), TypeTags.FUTURE, null,
symTable.futureType.tsymbol);
BFutureType bFutureType = new BFutureType(symTable.typeEnv(), null, symTable.futureType.tsymbol);
bFutureType.constraint = readTypeFromCp();
bFutureType.setFlags(flags);
return bFutureType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public void visit(BLangFunction astFunc) {

// TODO: Return variable with NIL type should be written to BIR
// Special %0 location for storing return values
BType retType = unifier.build(astFunc.symbol.type.getReturnType());
BType retType = unifier.build(symTable.typeEnv(), astFunc.symbol.type.getReturnType());
birFunc.returnVariable = new BIRVariableDcl(astFunc.pos, retType, this.env.nextLocalVarId(names),
VarScope.FUNCTION, VarKind.RETURN, null);
birFunc.localVars.add(0, birFunc.returnVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.wso2.ballerinalang.compiler.bir.codegen;

import io.ballerina.types.Env;
import org.ballerinalang.compiler.BLangCompilerException;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
Expand Down Expand Up @@ -633,7 +634,7 @@ private void generateJCastToBAny(MethodVisitor mv, BIRVarToJVMIndexMap indexMap,
mv.visitTypeInsn(INSTANCEOF, SIMPLE_VALUE);
mv.visitJumpInsn(IFNE, afterHandle);
}
if (isNillable(targetType)) {
if (targetType.isNullable()) {
mv.visitInsn(DUP);
mv.visitJumpInsn(IFNULL, afterHandle);
}
Expand All @@ -654,15 +655,6 @@ private void generateJCastToBAny(MethodVisitor mv, BIRVarToJVMIndexMap indexMap,
}
}

private static boolean isNillable(BType targetType) {
return switch (targetType.tag) {
case TypeTags.NIL, TypeTags.NEVER, TypeTags.JSON, TypeTags.ANY, TypeTags.ANYDATA, TypeTags.READONLY -> true;
case TypeTags.UNION, TypeTags.INTERSECTION, TypeTags.FINITE -> targetType.isNullable();
case TypeTags.TYPEREFDESC -> isNillable(JvmCodeGenUtil.getImpliedType(targetType));
default -> false;
};
}

private void generateCheckCastJToBJSON(MethodVisitor mv, BIRVarToJVMIndexMap indexMap, JType sourceType) {
if (sourceType.jTag == JTypeTags.JREF || sourceType.jTag == JTypeTags.JARRAY) {
return;
Expand Down Expand Up @@ -1407,4 +1399,9 @@ private void generateCastToAny(MethodVisitor mv, BType type) {
private void generateXMLToAttributesMap(MethodVisitor mv) {
mv.visitMethodInsn(INVOKEVIRTUAL, XML_VALUE, "getAttributesMap", GET_ATTRAIBUTE_MAP, false);
}

public Env typeEnv() {
assert types.typeEnv() != null;
return types.typeEnv();
}
}
Loading

0 comments on commit 802f909

Please sign in to comment.