diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/LangLibFunctionBinder.java b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/LangLibFunctionBinder.java index 256f6ef1b6ba..5bb6b0339c2c 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/LangLibFunctionBinder.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/LangLibFunctionBinder.java @@ -131,7 +131,7 @@ private BInvokableType duplicateType(BInvokableType original, List 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, diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/TypeParamFinder.java b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/TypeParamFinder.java index 52f4d845e8e0..d664cb2c237b 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/TypeParamFinder.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/TypeParamFinder.java @@ -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; @@ -98,10 +97,6 @@ public void visit(BArrayType bArrayType) { find(bArrayType.eType); } - @Override - public void visit(BBuiltInRefType bBuiltInRefType) { - } - @Override public void visit(BAnyType bAnyType) { } diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/TypeParamResolver.java b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/TypeParamResolver.java index f237f8800f7e..a15d5428db91 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/TypeParamResolver.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/TypeParamResolver.java @@ -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; @@ -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; @@ -74,9 +74,12 @@ public class TypeParamResolver implements BTypeVisitor { private final Map 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(); } /** @@ -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; @@ -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()); } @@ -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()); } @@ -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; @@ -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; @@ -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); } @@ -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); } @@ -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; @@ -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; @@ -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 diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/type/builders/BallerinaFutureTypeBuilder.java b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/type/builders/BallerinaFutureTypeBuilder.java index 7b6650da5df7..af0fcab472d5 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/type/builders/BallerinaFutureTypeBuilder.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/type/builders/BallerinaFutureTypeBuilder.java @@ -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; @@ -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; diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/type/builders/BallerinaTableTypeBuilder.java b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/type/builders/BallerinaTableTypeBuilder.java index c465bf106147..a749896d70ae 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/type/builders/BallerinaTableTypeBuilder.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/type/builders/BallerinaTableTypeBuilder.java @@ -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; @@ -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); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ConnectorType.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ConnectorType.java deleted file mode 100644 index c6f9a6e9212b..000000000000 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ConnectorType.java +++ /dev/null @@ -1,26 +0,0 @@ -/* -* Copyright (c) 2017, 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 -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ -package org.ballerinalang.model.types; - -/** - * {@code ConnectorType} represents the type of a connector in Ballerina. - * - * @since 0.94 - */ -public interface ConnectorType extends ReferenceType { -} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ConstrainedType.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ConstrainedType.java index 394001fbf8cc..8cd4c1a858d3 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ConstrainedType.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ConstrainedType.java @@ -20,7 +20,7 @@ /** * @since 0.94 */ -public interface ConstrainedType extends ReferenceType { +public interface ConstrainedType { Type getConstraint(); } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/NilType.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/NilType.java deleted file mode 100644 index 6f9667927726..000000000000 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/NilType.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2018, 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 - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.ballerinalang.model.types; - -/** - * {@code NilType} represents the singleton type returns by functions with no declared value. - * The value of the {@code NilType} is written as '()' - * - * @since 0.970.0 - */ -public interface NilType extends ReferenceType { -} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/NullType.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/NullType.java deleted file mode 100644 index 144a5036682d..000000000000 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/NullType.java +++ /dev/null @@ -1,26 +0,0 @@ -/* -* Copyright (c) 2017, 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 -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ -package org.ballerinalang.model.types; - -/** - * {@code NullType} represents the type of the expression 'null'. - * - * @since 0.94 - */ -public interface NullType extends ReferenceType { -} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ReferenceType.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ReferenceType.java deleted file mode 100644 index ebe029307e5a..000000000000 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/types/ReferenceType.java +++ /dev/null @@ -1,27 +0,0 @@ -/* -* Copyright (c) 2017, 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 -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ -package org.ballerinalang.model.types; - -/** - * {@code ReferenceType} represents a reference type in Ballerina. - * These includes structs, connectors, array types, xml, json etc. - * - * @since 0.94 - */ -public interface ReferenceType extends Type { -} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/BIRPackageSymbolEnter.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/BIRPackageSymbolEnter.java index 10a130533543..044548c34fde 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/BIRPackageSymbolEnter.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/BIRPackageSymbolEnter.java @@ -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; @@ -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(); @@ -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: @@ -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; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 44746e4cf7f4..21665336fe6b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -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); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java index bbb3ad6cd9de..d24945da2fc8 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java @@ -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; @@ -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); } @@ -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; @@ -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(); + } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java index d1b4237ae9d4..379aef0495af 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java @@ -20,6 +20,7 @@ import io.ballerina.identifier.Utils; import io.ballerina.tools.diagnostics.Location; +import io.ballerina.types.Env; import org.apache.commons.lang3.StringUtils; import org.ballerinalang.compiler.BLangCompilerException; import org.ballerinalang.model.elements.PackageID; @@ -333,18 +334,19 @@ private static String cleanupSourceFileName(String name) { return name.replace(".", FILE_NAME_PERIOD_SEPERATOR); } - public static String getMethodDesc(List paramTypes, BType retType) { - return INITIAL_METHOD_DESC + getMethodDescParams(paramTypes) + generateReturnType(retType); + public static String getMethodDesc(Env typeEnv, List paramTypes, BType retType) { + return INITIAL_METHOD_DESC + getMethodDescParams(paramTypes) + generateReturnType(retType, typeEnv); } - public static String getMethodDesc(List paramTypes, BType retType, BType attachedType) { + public static String getMethodDesc(Env typeEnv, List paramTypes, BType retType, BType attachedType) { return INITIAL_METHOD_DESC + getArgTypeSignature(attachedType) + getMethodDescParams(paramTypes) + - generateReturnType(retType); + generateReturnType(retType, typeEnv); } - public static String getMethodDesc(List paramTypes, BType retType, String attachedTypeClassName) { + public static String getMethodDesc(Env typeEnv, List paramTypes, BType retType, + String attachedTypeClassName) { return INITIAL_METHOD_DESC + "L" + attachedTypeClassName + ";" + getMethodDescParams(paramTypes) + - generateReturnType(retType); + generateReturnType(retType, typeEnv); } public static String getMethodDescParams(List paramTypes) { @@ -387,13 +389,13 @@ public static String getArgTypeSignature(BType bType) { }; } - public static String generateReturnType(BType bType) { + public static String generateReturnType(BType bType, Env typeEnv) { bType = JvmCodeGenUtil.getImpliedType(bType); if (bType == null) { return RETURN_JOBJECT; } - bType = JvmCodeGenUtil.UNIFIER.build(bType); + bType = JvmCodeGenUtil.UNIFIER.build(typeEnv, bType); if (bType == null || bType.tag == TypeTags.NIL || bType.tag == TypeTags.NEVER) { return RETURN_JOBJECT; } else if (TypeTags.isIntegerTypeTag(bType.tag)) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java index 737d887525c3..f5066bc4f697 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java @@ -19,6 +19,7 @@ package org.wso2.ballerinalang.compiler.bir.codegen; import io.ballerina.identifier.Utils; +import io.ballerina.types.Env; import org.ballerinalang.compiler.BLangCompilerException; import org.ballerinalang.model.elements.PackageID; import org.ballerinalang.model.symbols.SymbolKind; @@ -148,6 +149,7 @@ public class JvmPackageGen { private final BLangDiagnosticLog dlog; private final Types types; private final boolean isRemoteMgtEnabled; + private final Env typeEnv; JvmPackageGen(SymbolTable symbolTable, PackageCache packageCache, BLangDiagnosticLog dlog, Types types, boolean isRemoteMgtEnabled) { @@ -162,6 +164,7 @@ public class JvmPackageGen { initMethodGen = new InitMethodGen(symbolTable); configMethodGen = new ConfigMethodGen(); JvmInstructionGen.anyType = symbolTable.anyType; + this.typeEnv = symbolTable.typeEnv(); } private static String getBvmAlias(String orgName, String moduleName) { @@ -314,20 +317,21 @@ private static void setCurrentModuleField(ClassWriter cw, MethodVisitor mv, JvmC mv.visitFieldInsn(PUTSTATIC, moduleInitClass, CURRENT_MODULE_VAR_NAME, GET_MODULE); } - public static BIRFunctionWrapper getFunctionWrapper(BIRFunction currentFunc, PackageID packageID, + public static BIRFunctionWrapper getFunctionWrapper(Env typeEnv, BIRFunction currentFunc, PackageID packageID, String moduleClass) { BInvokableType functionTypeDesc = currentFunc.type; BIRVariableDcl receiver = currentFunc.receiver; BType retType = functionTypeDesc.retType; if (isExternFunc(currentFunc) && Symbols.isFlagOn(retType.getFlags(), Flags.PARAMETERIZED)) { - retType = unifier.build(retType); + retType = unifier.build(typeEnv, retType); } String jvmMethodDescription; if (receiver == null) { - jvmMethodDescription = JvmCodeGenUtil.getMethodDesc(functionTypeDesc.paramTypes, retType); + jvmMethodDescription = JvmCodeGenUtil.getMethodDesc(typeEnv, functionTypeDesc.paramTypes, retType); } else { - jvmMethodDescription = JvmCodeGenUtil.getMethodDesc(functionTypeDesc.paramTypes, retType, receiver.type); + jvmMethodDescription = JvmCodeGenUtil.getMethodDesc(typeEnv, functionTypeDesc.paramTypes, retType, + receiver.type); } return new BIRFunctionWrapper(packageID, currentFunc, moduleClass, jvmMethodDescription); } @@ -487,11 +491,11 @@ private void linkTypeDefinitions(BIRPackage module, boolean isEntry) { private void linkModuleFunction(PackageID packageID, String initClass, String funcName) { BInvokableType funcType = - new BInvokableType(symbolTable.typeEnv(), Collections.emptyList(), null, symbolTable.nilType, null); + new BInvokableType(typeEnv, Collections.emptyList(), null, symbolTable.nilType, null); BIRFunction moduleStopFunction = new BIRFunction(null, new Name(funcName), 0, funcType, new Name(""), 0, VIRTUAL); birFunctionMap.put(JvmCodeGenUtil.getPackageName(packageID) + funcName, - getFunctionWrapper(moduleStopFunction, packageID, initClass)); + getFunctionWrapper(typeEnv, moduleStopFunction, packageID, initClass)); } private void linkModuleFunctions(BIRPackage birPackage, String initClass, boolean isEntry, @@ -513,20 +517,20 @@ private void linkModuleFunctions(BIRPackage birPackage, String initClass, boolea PackageID packageID = birPackage.packageID; jvmClassMap.put(initClass, klass); String pkgName = JvmCodeGenUtil.getPackageName(packageID); - birFunctionMap.put(pkgName + functionName, getFunctionWrapper(initFunc, packageID, initClass)); + birFunctionMap.put(pkgName + functionName, getFunctionWrapper(typeEnv, initFunc, packageID, initClass)); count += 1; // Add start function BIRFunction startFunc = functions.get(1); functionName = Utils.encodeFunctionIdentifier(startFunc.name.value); - birFunctionMap.put(pkgName + functionName, getFunctionWrapper(startFunc, packageID, initClass)); + birFunctionMap.put(pkgName + functionName, getFunctionWrapper(typeEnv, startFunc, packageID, initClass)); klass.functions.add(1, startFunc); count += 1; // Add stop function BIRFunction stopFunc = functions.get(2); functionName = Utils.encodeFunctionIdentifier(stopFunc.name.value); - birFunctionMap.put(pkgName + functionName, getFunctionWrapper(stopFunc, packageID, initClass)); + birFunctionMap.put(pkgName + functionName, getFunctionWrapper(typeEnv, stopFunc, packageID, initClass)); klass.functions.add(2, stopFunc); count += 1; int genMethodsCount = 0; @@ -585,13 +589,13 @@ private BIRFunctionWrapper getBirFunctionWrapper(boolean isEntry, PackageID pack BIRFunction birFunc, String birModuleClassName) { BIRFunctionWrapper birFuncWrapperOrError; if (isExternFunc(birFunc) && isEntry) { - birFuncWrapperOrError = createExternalFunctionWrapper(symbolTable.typeEnv(), true, birFunc, packageID, + birFuncWrapperOrError = createExternalFunctionWrapper(typeEnv, true, birFunc, packageID, birModuleClassName); } else { if (isEntry && birFunc.receiver == null) { - addDefaultableBooleanVarsToSignature(symbolTable.typeEnv(), birFunc); + addDefaultableBooleanVarsToSignature(typeEnv, birFunc); } - birFuncWrapperOrError = getFunctionWrapper(birFunc, packageID, birModuleClassName); + birFuncWrapperOrError = getFunctionWrapper(typeEnv, birFunc, packageID, birModuleClassName); } return birFuncWrapperOrError; } @@ -686,8 +690,8 @@ CompiledJarFile generate(BIRPackage module) { // use a ByteArrayOutputStream to store class byte values final JarEntries jarEntries = compiledJarFile.jarEntries; // desugar parameter initialization - injectDefaultParamInits(module, initMethodGen); - injectDefaultParamInitsToAttachedFuncs(symbolTable.typeEnv(), module, initMethodGen); + injectDefaultParamInits(typeEnv, module, initMethodGen); + injectDefaultParamInitsToAttachedFuncs(typeEnv, module, initMethodGen); BIRFunction mainFunc = getMainFunction(module); BIRFunction testExecuteFunc = getTestExecuteFunction(module); @@ -718,7 +722,7 @@ CompiledJarFile generate(BIRPackage module) { removeSourceAnnotationTypeDefs(module.typeDefs); // desugar the record init function - rewriteRecordInits(symbolTable.typeEnv(), module.typeDefs); + rewriteRecordInits(typeEnv, module.typeDefs); // generate object/record value classes JvmValueGen valueGen = new JvmValueGen(module, this, methodGen, typeHashVisitor, types); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTerminatorGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTerminatorGen.java index 088e937ba560..42097b0a202c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTerminatorGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTerminatorGen.java @@ -297,7 +297,7 @@ private void genGoToTerm(BIRTerminator.GOTO gotoIns, String funcName) { public void genReturnTerm(int returnVarRefIndex, BIRNode.BIRFunction func, int channelMapVarIndex, int sendWorkerChannelNamesVar, int receiveWorkerChannelNamesVar, int localVarOffset) { - BType bType = unifier.build(func.type.retType); + BType bType = unifier.build(symbolTable.typeEnv(), func.type.retType); generateReturnTermFromType(bType, func, returnVarRefIndex, channelMapVarIndex, sendWorkerChannelNamesVar, receiveWorkerChannelNamesVar, localVarOffset); } @@ -606,8 +606,8 @@ private void genStaticCall(BIRTerminator.Call callIns, PackageID packageID, int jvmClass = JvmCodeGenUtil.getModuleLevelClassName(packageID, JvmCodeGenUtil.cleanupPathSeparators(balFileName)); //TODO: add receiver: BType attachedType = type.r != null ? receiver.type : null; - BType retType = unifier.build(type.retType); - methodDesc = JvmCodeGenUtil.getMethodDesc(params, retType); + BType retType = unifier.build(symbolTable.typeEnv(), type.retType); + methodDesc = JvmCodeGenUtil.getMethodDesc(symbolTable.typeEnv(), params, retType); } this.mv.visitMethodInsn(INVOKESTATIC, jvmClass, encodedMethodName, methodDesc, false); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java index dd323fbf4bf0..e8266b89bc75 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java @@ -154,7 +154,7 @@ private static void desugarObjectMethods(Env env, List attachedFunc for (BIRNode.BIRFunction birFunc : attachedFuncs) { if (JvmCodeGenUtil.isExternFunc(birFunc)) { if (birFunc instanceof JMethodBIRFunction jMethodBIRFunction) { - desugarInteropFuncs(jMethodBIRFunction, initMethodGen); + desugarInteropFuncs(env, jMethodBIRFunction, initMethodGen); initMethodGen.resetIds(); } else if (!(birFunc instanceof JFieldBIRFunction)) { initMethodGen.resetIds(); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/ExternalMethodGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/ExternalMethodGen.java index 31c80f9e042a..65286884f9f3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/ExternalMethodGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/ExternalMethodGen.java @@ -64,7 +64,7 @@ public static void genJMethodForBExternalFunc(BIRFunction birFunc, ClassWriter c } } - public static void injectDefaultParamInits(BIRPackage module, InitMethodGen initMethodGen) { + public static void injectDefaultParamInits(Env typeEnv, BIRPackage module, InitMethodGen initMethodGen) { // filter out functions. List functions = module.functions; if (!functions.isEmpty()) { @@ -75,7 +75,7 @@ public static void injectDefaultParamInits(BIRPackage module, InitMethodGen init BIRFunction birFunc = functions.get(count); count = count + 1; if (birFunc instanceof JMethodBIRFunction jMethodBIRFunction) { - desugarInteropFuncs(jMethodBIRFunction, initMethodGen); + desugarInteropFuncs(typeEnv, jMethodBIRFunction, initMethodGen); initMethodGen.resetIds(); } else if (!(birFunc instanceof JFieldBIRFunction)) { initMethodGen.resetIds(); @@ -89,7 +89,7 @@ public static BIRFunctionWrapper createExternalFunctionWrapper(Env env, boolean if (isEntry) { addDefaultableBooleanVarsToSignature(env, birFunc); } - return getFunctionWrapper(birFunc, packageID, birModuleClassName); + return getFunctionWrapper(env, birFunc, packageID, birModuleClassName); } private ExternalMethodGen() { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/InteropMethodGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/InteropMethodGen.java index 7a851418cc17..37c764a58496 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/InteropMethodGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/InteropMethodGen.java @@ -18,6 +18,7 @@ package org.wso2.ballerinalang.compiler.bir.codegen.interop; +import io.ballerina.types.Env; import org.ballerinalang.compiler.BLangCompilerException; import org.ballerinalang.model.elements.PackageID; import org.objectweb.asm.ClassWriter; @@ -142,10 +143,10 @@ static void genJFieldForInteropField(JFieldBIRFunction birFunc, ClassWriter clas BType retType = birFunc.type.retType; if (Symbols.isFlagOn(retType.getFlags(), Flags.PARAMETERIZED)) { - retType = JvmCodeGenUtil.UNIFIER.build(birFunc.type.retType); + retType = JvmCodeGenUtil.UNIFIER.build(types.typeEnv(), birFunc.type.retType); } - String desc = JvmCodeGenUtil.getMethodDesc(birFunc.type.paramTypes, retType); + String desc = JvmCodeGenUtil.getMethodDesc(types.typeEnv(), birFunc.type.paramTypes, retType); int access = birFunc.receiver != null ? ACC_PUBLIC : ACC_PUBLIC + ACC_STATIC; MethodVisitor mv = classWriter.visitMethod(access, birFunc.name.value, desc, null, null); JvmInstructionGen instGen = new JvmInstructionGen(mv, indexMap, birModule, jvmPackageGen, jvmTypeGen, @@ -257,11 +258,11 @@ static void genJFieldForInteropField(JFieldBIRFunction birFunc, ClassWriter clas mv.visitEnd(); } - public static void desugarInteropFuncs(JMethodBIRFunction birFunc, InitMethodGen initMethodGen) { + public static void desugarInteropFuncs(Env typeEnv, JMethodBIRFunction birFunc, InitMethodGen initMethodGen) { // resetting the variable generation index BType retType = birFunc.type.retType; if (Symbols.isFlagOn(retType.getFlags(), Flags.PARAMETERIZED)) { - retType = JvmCodeGenUtil.UNIFIER.build(birFunc.type.retType); + retType = JvmCodeGenUtil.UNIFIER.build(typeEnv, birFunc.type.retType); } JMethod jMethod = birFunc.jMethod; Class[] jMethodParamTypes = jMethod.getParamTypes(); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/InteropValidator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/InteropValidator.java index 59be56de92d3..f20f50ef2e2a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/InteropValidator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/InteropValidator.java @@ -204,7 +204,8 @@ private ClassLoader makeClassLoader(Set moduleDependencies) { JMethod validateAndGetJMethod(InteropValidationRequest.MethodValidationRequest methodValidationRequest, ClassLoader classLoader) { // Populate JMethodRequest from the BValue - JMethodRequest jMethodRequest = JMethodRequest.build(methodValidationRequest, classLoader); + JMethodRequest jMethodRequest = JMethodRequest.build(symbolTable.typeEnv(), methodValidationRequest, + classLoader); // Find the most specific Java method or constructor for the given request JMethodResolver methodResolver = new JMethodResolver(classLoader, symbolTable); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/JMethodRequest.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/JMethodRequest.java index fedbe0f81b44..c52cd1f4ab49 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/JMethodRequest.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/JMethodRequest.java @@ -17,6 +17,7 @@ */ package org.wso2.ballerinalang.compiler.bir.codegen.interop; +import io.ballerina.types.Env; import io.ballerina.types.PredefinedType; import io.ballerina.types.SemTypes; import org.ballerinalang.model.symbols.SymbolKind; @@ -59,7 +60,7 @@ private JMethodRequest() { } - static JMethodRequest build(InteropValidationRequest.MethodValidationRequest methodValidationRequest, + static JMethodRequest build(Env typeEnv, InteropValidationRequest.MethodValidationRequest methodValidationRequest, ClassLoader classLoader) { JMethodRequest jMethodReq = new JMethodRequest(); @@ -94,7 +95,7 @@ static JMethodRequest build(InteropValidationRequest.MethodValidationRequest met jMethodReq.bParamTypes = paramTypes.toArray(new BType[0]); jMethodReq.pathParamSymbols = pathParams; - BType returnType = unifier.build(bFuncType.retType); + BType returnType = unifier.build(typeEnv, bFuncType.retType); jMethodReq.bReturnType = returnType; jMethodReq.returnsBErrorType = SemTypes.containsBasicType(returnType.semType(), PredefinedType.ERROR); jMethodReq.restParamExist = methodValidationRequest.restParamExist; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/InitMethodGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/InitMethodGen.java index ff605ea76f64..2dd4a90d648c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/InitMethodGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/InitMethodGen.java @@ -186,7 +186,7 @@ public void generateLambdaForModuleExecuteFunction(ClassWriter cw, String initCl jvmCastGen.addUnboxInsn(mv, paramType); paramIndex += 1; } - methodDesc = JvmCodeGenUtil.getMethodDesc(paramTypes, returnType); + methodDesc = JvmCodeGenUtil.getMethodDesc(symbolTable.typeEnv(), paramTypes, returnType); } mv.visitMethodInsn(INVOKESTATIC, initClass, MODULE_EXECUTE_METHOD, methodDesc, false); @@ -332,21 +332,21 @@ public void enrichPkgWithInitializers(Map birFunctio javaClass.functions.add(initFunc); pkg.functions.add(initFunc); birFunctionMap.put(JvmCodeGenUtil.getPackageName(pkg.packageID) + MODULE_INIT_METHOD, - JvmPackageGen.getFunctionWrapper(initFunc, pkg.packageID, typeOwnerClass)); + JvmPackageGen.getFunctionWrapper(symbolTable.typeEnv(), initFunc, pkg.packageID, typeOwnerClass)); BIRNode.BIRFunction startFunc = generateDefaultFunction(moduleImports, pkg, MODULE_START_METHOD, MethodGenUtils.START_FUNCTION_SUFFIX); javaClass.functions.add(startFunc); pkg.functions.add(startFunc); birFunctionMap.put(JvmCodeGenUtil.getPackageName(pkg.packageID) + MODULE_START_METHOD, - JvmPackageGen.getFunctionWrapper(startFunc, pkg.packageID, typeOwnerClass)); + JvmPackageGen.getFunctionWrapper(symbolTable.typeEnv(), startFunc, pkg.packageID, typeOwnerClass)); BIRNode.BIRFunction execFunc = generateExecuteFunction(pkg, mainFunc, testExecuteFunc ); javaClass.functions.add(execFunc); pkg.functions.add(execFunc); birFunctionMap.put(JvmCodeGenUtil.getPackageName(pkg.packageID) + MODULE_EXECUTE_METHOD, - JvmPackageGen.getFunctionWrapper(execFunc, pkg.packageID, typeOwnerClass)); + JvmPackageGen.getFunctionWrapper(symbolTable.typeEnv(), execFunc, pkg.packageID, typeOwnerClass)); } private BIRNode.BIRFunction generateExecuteFunction(BIRNode.BIRPackage pkg, diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/LambdaGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/LambdaGen.java index d013ccdf566f..888175ed2f4b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/LambdaGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/LambdaGen.java @@ -456,7 +456,7 @@ private String getLambdaMethodDesc(List paramTypes, BType retType, int cl StringBuilder desc = new StringBuilder(INITIAL_METHOD_DESC); appendClosureMaps(closureMapsCount, desc); appendParamTypes(paramTypes, desc); - desc.append(JvmCodeGenUtil.generateReturnType(retType)); + desc.append(JvmCodeGenUtil.generateReturnType(retType, jvmCastGen.typeEnv())); return desc.toString(); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MethodGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MethodGen.java index f859ce24df1f..d4ab30941346 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MethodGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MethodGen.java @@ -19,6 +19,7 @@ package org.wso2.ballerinalang.compiler.bir.codegen.methodgen; import io.ballerina.identifier.Utils; +import io.ballerina.types.Env; import org.ballerinalang.compiler.BLangCompilerException; import org.ballerinalang.model.elements.PackageID; import org.objectweb.asm.ClassWriter; @@ -147,11 +148,13 @@ public class MethodGen { private final JvmPackageGen jvmPackageGen; private final SymbolTable symbolTable; private final Types types; + private final Env typeEnv; public MethodGen(JvmPackageGen jvmPackageGen, Types types) { this.jvmPackageGen = jvmPackageGen; this.symbolTable = jvmPackageGen.symbolTable; this.types = types; + this.typeEnv = types.typeEnv(); } public void generateMethod(BIRFunction birFunc, ClassWriter cw, BIRPackage birModule, BType attachedType, @@ -177,7 +180,7 @@ public void genJMethodWithBObjectMethodCall(BIRFunction func, ClassWriter cw, BI indexMap.addIfNotExists(STRAND, symbolTable.stringType); String funcName = func.name.value; BType retType = getReturnType(func); - String desc = JvmCodeGenUtil.getMethodDesc(func.type.paramTypes, retType); + String desc = JvmCodeGenUtil.getMethodDesc(typeEnv, func.type.paramTypes, retType); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, funcName, desc, null, null); mv.visitCode(); Label methodStartLabel = new Label(); @@ -190,7 +193,7 @@ public void genJMethodWithBObjectMethodCall(BIRFunction func, ClassWriter cw, BI for (BIRNode.BIRFunctionParameter parameter : func.parameters) { instGen.generateVarLoad(mv, parameter, indexMap.addIfNotExists(parameter.name.value, parameter.type)); } - String methodDesc = JvmCodeGenUtil.getMethodDesc(func.type.paramTypes, retType, moduleClassName); + String methodDesc = JvmCodeGenUtil.getMethodDesc(typeEnv, func.type.paramTypes, retType, moduleClassName); mv.visitMethodInsn(INVOKESTATIC, splitClassName, encodedMethodName, methodDesc, false); Label methodEndLabel = new Label(); mv.visitLabel(methodEndLabel); @@ -248,9 +251,9 @@ public void genJMethodForBFunc(BIRFunction func, ClassWriter cw, BIRPackage modu BType retType = getReturnType(func); String desc; if (isObjectMethodSplit) { - desc = JvmCodeGenUtil.getMethodDesc(func.type.paramTypes, retType, moduleClassName); + desc = JvmCodeGenUtil.getMethodDesc(typeEnv, func.type.paramTypes, retType, moduleClassName); } else { - desc = JvmCodeGenUtil.getMethodDesc(func.type.paramTypes, retType); + desc = JvmCodeGenUtil.getMethodDesc(typeEnv, func.type.paramTypes, retType); } MethodVisitor mv = cw.visitMethod(access, funcName, desc, null, null); mv.visitCode(); @@ -337,7 +340,7 @@ private void handleParentModuleStart(MethodVisitor mv, PackageID packageID, Stri private BType getReturnType(BIRFunction func) { BType retType = func.type.retType; if (JvmCodeGenUtil.isExternFunc(func) && Symbols.isFlagOn(retType.getFlags(), Flags.PARAMETERIZED)) { - retType = JvmCodeGenUtil.UNIFIER.build(func.type.retType); + retType = JvmCodeGenUtil.UNIFIER.build(typeEnv, func.type.retType); } return retType; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/values/JvmObjectGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/values/JvmObjectGen.java index a893241794b9..a87a368ecd65 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/values/JvmObjectGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/values/JvmObjectGen.java @@ -126,7 +126,7 @@ public void createAndSplitCallMethod(ClassWriter cw, List f String methodSig; // use index access, since retType can be nil. - methodSig = JvmCodeGenUtil.getMethodDesc(paramTypes, retType); + methodSig = JvmCodeGenUtil.getMethodDesc(jvmCastGen.typeEnv(), paramTypes, retType); // load self mv.visitVarInsn(ALOAD, 0); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRTypeWriter.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRTypeWriter.java index 2f17d657f4d3..27beec2068be 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRTypeWriter.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRTypeWriter.java @@ -71,7 +71,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; @@ -153,11 +152,6 @@ public void visit(BArrayType bArrayType) { writeTypeCpIndex(bArrayType.getElementType()); } - @Override - public void visit(BBuiltInRefType bBuiltInRefType) { - throwUnimplementedError(bBuiltInRefType); - } - @Override public void visit(BAnyType bAnyType) { } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ASTBuilderUtil.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ASTBuilderUtil.java index 9200ac1e3858..205071c02a21 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ASTBuilderUtil.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ASTBuilderUtil.java @@ -17,6 +17,7 @@ package org.wso2.ballerinalang.compiler.desugar; import io.ballerina.tools.diagnostics.Location; +import io.ballerina.types.Env; import org.ballerinalang.model.TreeBuilder; import org.ballerinalang.model.elements.Flag; import org.ballerinalang.model.elements.PackageID; @@ -844,7 +845,7 @@ private static IdentifierNode createIdentifier(String value) { return node; } - public static BInvokableSymbol duplicateInvokableSymbol(BInvokableSymbol invokableSymbol) { + public static BInvokableSymbol duplicateInvokableSymbol(Env typeEnv, BInvokableSymbol invokableSymbol) { BInvokableSymbol dupFuncSymbol = Symbols.createFunctionSymbol(invokableSymbol.flags, invokableSymbol.name, invokableSymbol.originalName, invokableSymbol.pkgID, invokableSymbol.type, invokableSymbol.owner, @@ -865,7 +866,7 @@ public static BInvokableSymbol duplicateInvokableSymbol(BInvokableSymbol invokab BInvokableType prevFuncType = (BInvokableType) invokableSymbol.type; BInvokableType dupInvokableType = - new BInvokableType(invokableSymbol.getType().env, List.copyOf(prevFuncType.paramTypes), + new BInvokableType(typeEnv, List.copyOf(prevFuncType.paramTypes), prevFuncType.restType, prevFuncType.retType, prevFuncType.tsymbol); if (Symbols.isFlagOn(invokableSymbol.flags, Flags.ISOLATED)) { @@ -884,7 +885,8 @@ public static BInvokableSymbol duplicateInvokableSymbol(BInvokableSymbol invokab return dupFuncSymbol; } - public static BInvokableSymbol duplicateFunctionDeclarationSymbol(BInvokableSymbol invokableSymbol, + public static BInvokableSymbol duplicateFunctionDeclarationSymbol(Env typeEnv, + BInvokableSymbol invokableSymbol, BSymbol owner, Name newName, PackageID newPkgID, @@ -914,7 +916,7 @@ public static BInvokableSymbol duplicateFunctionDeclarationSymbol(BInvokableSymb dupFuncSymbol.markdownDocumentation = invokableSymbol.markdownDocumentation; BInvokableType prevFuncType = (BInvokableType) invokableSymbol.type; - BType newFuncType = new BInvokableType(invokableSymbol.getType().env, List.copyOf(prevFuncType.paramTypes), + BType newFuncType = new BInvokableType(typeEnv, List.copyOf(prevFuncType.paramTypes), prevFuncType.restType, prevFuncType.retType, prevFuncType.tsymbol); newFuncType.addFlags(prevFuncType.getFlags()); dupFuncSymbol.type = newFuncType; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureDesugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureDesugar.java index a4f552ba4804..b83ce79d4df1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureDesugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureDesugar.java @@ -18,6 +18,7 @@ package org.wso2.ballerinalang.compiler.desugar; import io.ballerina.tools.diagnostics.Location; +import io.ballerina.types.Env; import org.ballerinalang.model.TreeBuilder; import org.ballerinalang.model.elements.Flag; import org.ballerinalang.model.symbols.SymbolKind; @@ -266,7 +267,7 @@ public void visit(BLangPackage pkgNode) { // Update function parameters. for (BLangFunction function : pkgNode.functions) { - updateFunctionParams(function); + updateFunctionParams(types.typeEnv(), function); } result = pkgNode; } @@ -644,11 +645,12 @@ private void createFunctionMap(BLangFunction funcNode, SymbolEnv funcEnv) { /** * Update the function parameters with closure parameter maps passed. * + * @param typeEnv type environment * @param funcNode function node */ - private static void updateFunctionParams(BLangFunction funcNode) { + private static void updateFunctionParams(Env typeEnv, BLangFunction funcNode) { // Add closure params to the required param list if there are any. - BInvokableSymbol dupFuncSymbol = ASTBuilderUtil.duplicateInvokableSymbol(funcNode.symbol); + BInvokableSymbol dupFuncSymbol = ASTBuilderUtil.duplicateInvokableSymbol(typeEnv, funcNode.symbol); funcNode.symbol = dupFuncSymbol; BInvokableType dupFuncType = (BInvokableType) dupFuncSymbol.type; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index cfb97adc936b..4b166c7b7b1a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -1380,7 +1380,7 @@ public void visit(BLangFunction funcNode) { // Duplicate the invokable symbol and the invokable type. funcNode.originalFuncSymbol = funcNode.symbol; - funcNode.symbol = ASTBuilderUtil.duplicateInvokableSymbol(funcNode.symbol); + funcNode.symbol = ASTBuilderUtil.duplicateInvokableSymbol(types.typeEnv(), funcNode.symbol); funcNode.requiredParams = rewrite(funcNode.requiredParams, funcEnv); funcNode.restParam = rewrite(funcNode.restParam, funcEnv); @@ -7108,9 +7108,9 @@ private BLangExpression rewriteInvocation(BLangInvocation invocation, boolean as BInvokableSymbol invSym = (BInvokableSymbol) invocation.symbol; if (Symbols.isFlagOn(invSym.retType.getFlags(), Flags.PARAMETERIZED)) { - BType retType = unifier.build(invSym.retType); + BType retType = unifier.build(symTable.typeEnv(), invSym.retType); invocation.setBType(invocation.async ? - new BFutureType(symTable.typeEnv(), TypeTags.FUTURE, retType, null) : retType); + new BFutureType(symTable.typeEnv(), retType, null) : retType); } if (invocation.expr == null) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java index 274a43021631..07a643dfa4b1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java @@ -53,7 +53,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; @@ -2203,11 +2202,6 @@ public void visit(BArrayType arrayType) { data.resultType = resultTupleType; } - @Override - public void visit(BBuiltInRefType bBuiltInRefType) { - - } - @Override public void visit(BAnyType bAnyType) { data.resultType = symTable.nilType; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/EffectiveTypePopulator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/EffectiveTypePopulator.java index 2e3c3e718350..4f25fd82f9fd 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/EffectiveTypePopulator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/EffectiveTypePopulator.java @@ -33,7 +33,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; @@ -150,11 +149,6 @@ public void visit(BArrayType bArrayType) { } } - @Override - public void visit(BBuiltInRefType bBuiltInRefType) { - - } - @Override public void visit(BAnyType bAnyType) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/IsolationAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/IsolationAnalyzer.java index 460384944f75..182ec315066f 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/IsolationAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/IsolationAnalyzer.java @@ -50,7 +50,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; @@ -4338,10 +4337,6 @@ public void visit(BArrayType bArrayType) { visitType(bArrayType.eType); } - @Override - public void visit(BBuiltInRefType bBuiltInRefType) { - } - @Override public void visit(BAnyType bAnyType) { } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/QueryTypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/QueryTypeChecker.java index 7e2177336ed9..73fea76ef339 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/QueryTypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/QueryTypeChecker.java @@ -493,7 +493,7 @@ void solveSelectTypeAndResolveType(BLangQueryExpr queryExpr, BLangExpression sel } private BType getQueryTableType(BLangQueryExpr queryExpr, BType constraintType, BType resolvedType, SymbolEnv env) { - final BTableType tableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, constraintType, null); + final BTableType tableType = new BTableType(symTable.typeEnv(), constraintType, null); if (!queryExpr.fieldNameIdentifierList.isEmpty()) { validateKeySpecifier(queryExpr.fieldNameIdentifierList, constraintType); markReadOnlyForConstraintType(constraintType); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index d539cfbb1619..140bb9aa8aa0 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -20,6 +20,7 @@ import io.ballerina.compiler.api.symbols.DiagnosticState; import io.ballerina.projects.ModuleDescriptor; import io.ballerina.tools.diagnostics.Location; +import io.ballerina.types.Env; import io.ballerina.types.PredefinedType; import org.ballerinalang.compiler.CompilerPhase; import org.ballerinalang.model.TreeBuilder; @@ -271,6 +272,7 @@ public class SemanticAnalyzer extends SimpleBLangNodeAnalyzer anonTypeNameSuffixes; private final CompilerContext compilerContext; + private final Env typeEnv; public static SemanticAnalyzer getInstance(CompilerContext context) { SemanticAnalyzer semAnalyzer = context.get(SYMBOL_ANALYZER_KEY); @@ -298,6 +300,7 @@ private SemanticAnalyzer(CompilerContext context) { this.anonModelHelper = BLangAnonymousModelHelper.getInstance(context); this.unifier = new Unifier(); this.anonTypeNameSuffixes = new ArrayDeque<>(); + this.typeEnv = types.typeEnv(); } public BLangPackage analyze(BLangPackage pkgNode) { @@ -564,7 +567,7 @@ public void visit(BLangFunction funcNode, AnalyzerData data) { } if (hasReturnType && Symbols.isFlagOn(returnTypeNode.getBType().getFlags(), Flags.PARAMETERIZED)) { - unifier.validate(returnTypeNode.getBType(), funcNode, symTable, currentEnv, types, dlog); + unifier.validate(typeEnv, returnTypeNode.getBType(), funcNode, symTable, currentEnv, types, dlog); } validateObjectAttachedFunction(funcNode, data); @@ -1197,7 +1200,7 @@ public void visit(BLangSimpleVariable varNode, AnalyzerData data) { if (isListenerDecl) { BType rhsType = typeChecker.checkExpr(rhsExpr, varInitEnv, - BUnionType.create(symTable.typeEnv(), null, lhsType, symTable.errorType), data.prevEnvs, + BUnionType.create(typeEnv, null, lhsType, symTable.errorType), data.prevEnvs, data.commonAnalyzerData); validateListenerCompatibility(varNode, rhsType); } else { @@ -1689,10 +1692,10 @@ private BType resolveTupleType(BLangTupleVariable varNode) { BLangVariable restVariable = varNode.restVariable; if (restVariable == null) { - return new BTupleType(symTable.typeEnv(), members); + return new BTupleType(typeEnv, members); } - return new BTupleType(symTable.typeEnv(), null, members, getTupleMemberType(restVariable), 0); + return new BTupleType(typeEnv, null, members, getTupleMemberType(restVariable), 0); } private BType getTupleMemberType(BLangVariable memberVariable) { @@ -1729,7 +1732,7 @@ public void visit(BLangErrorVariable varNode, AnalyzerData data) { // reason must be a const of subtype of string. // then we match the error with this specific reason. if (!varNode.reasonVarPrefixAvailable && varNode.getBType() == null) { - BErrorType errorType = new BErrorType(symTable.typeEnv(), varNode.getBType().tsymbol, null); + BErrorType errorType = new BErrorType(typeEnv, varNode.getBType().tsymbol, null); if (Types.getImpliedType(varNode.getBType()).tag == TypeTags.UNION) { Set members = types.expandAndGetMemberTypesRecursive(varNode.getBType()); @@ -2008,7 +2011,7 @@ private BType getListenerType(BType bType) { } else if (compatibleTypes.size() == 1) { return compatibleTypes.iterator().next(); } else { - return BUnionType.create(symTable.typeEnv(), null, compatibleTypes); + return BUnionType.create(typeEnv, null, compatibleTypes); } } @@ -2677,7 +2680,7 @@ private void checkErrorVarRefEquivalency(BLangErrorVarRef lhsRef, BType rhsType, if (lhsRef.restVar != null && !isIgnoreVar(lhsRef)) { setTypeOfVarRefInErrorBindingAssignment(lhsRef.restVar, data); checkInvalidTypeDef(lhsRef.restVar); - BMapType expRestType = new BMapType(symTable.typeEnv(), TypeTags.MAP, wideType, null); + BMapType expRestType = new BMapType(typeEnv, TypeTags.MAP, wideType, null); BType restVarType = Types.getImpliedType(lhsRef.restVar.getBType()); if (restVarType.tag != TypeTags.MAP || !types.isAssignable(wideType, ((BMapType) restVarType).constraint)) { dlog.error(lhsRef.restVar.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES, lhsRef.restVar.getBType(), @@ -2692,7 +2695,7 @@ private void checkErrorVarRefEquivalency(BLangErrorVarRef lhsRef, BType rhsType, private BType interpolateWideType(BRecordType rhsDetailType, List detailType) { Set extractedKeys = detailType.stream().map(detail -> detail.name.value).collect(Collectors.toSet()); - BUnionType wideType = BUnionType.create(symTable.typeEnv(), null); + BUnionType wideType = BUnionType.create(typeEnv, null); for (BField field : rhsDetailType.fields.values()) { // avoid fields extracted from binding pattern if (!extractedKeys.contains(field.name.value)) { @@ -2813,7 +2816,7 @@ public void visit(BLangIf ifNode, AnalyzerData data) { if (existingNarrowedTypeInfo.containsKey(key)) { BType.NarrowedTypes existingNarrowTypes = existingNarrowedTypeInfo.get(key); BUnionType unionType = - BUnionType.create(symTable.typeEnv(), null, existingNarrowTypes.trueType, + BUnionType.create(typeEnv, null, existingNarrowTypes.trueType, existingNarrowTypes.falseType); BType.NarrowedTypes newPair = new BType.NarrowedTypes(existingNarrowTypes.trueType, unionType); falseTypesOfNarrowedTypes.put(key, newPair); @@ -2987,7 +2990,7 @@ private void evaluateMatchPatternsTypeAccordingToMatchGuard(BLangMatchPattern ma BVarSymbol varSymbol = new BVarSymbol(type.getFlags(), null, null, type, null, null, null); members.add(new BTupleMember(type, varSymbol)); } - BTupleType matchPatternType = new BTupleType(symTable.typeEnv(), members); + BTupleType matchPatternType = new BTupleType(typeEnv, members); if (listMatchPattern.restMatchPattern != null) { evaluateMatchPatternsTypeAccordingToMatchGuard(listMatchPattern.restMatchPattern, env); @@ -3038,7 +3041,7 @@ public void visit(BLangMappingMatchPattern mappingMatchPattern, AnalyzerData dat fields.put(fieldName.getValue(), field); mappingMatchPattern.declaredVars.putAll(fieldMatchPattern.declaredVars); } - BRecordType recordVarType = new BRecordType(symTable.typeEnv(), recordSymbol); + BRecordType recordVarType = new BRecordType(typeEnv, recordSymbol); recordVarType.fields = fields; recordVarType.restFieldType = symTable.anyOrErrorType; if (mappingMatchPattern.restMatchPattern != null) { @@ -3046,7 +3049,7 @@ public void visit(BLangMappingMatchPattern mappingMatchPattern, AnalyzerData dat symbolEnter.createAnonRecordSymbol(currentEnv, mappingMatchPattern.pos); BLangRestMatchPattern restMatchPattern = mappingMatchPattern.restMatchPattern; BType restType = restMatchPattern.getBType(); - BRecordType matchPatternRecType = new BRecordType(symTable.typeEnv(), matchPattenRecordSym); + BRecordType matchPatternRecType = new BRecordType(typeEnv, matchPattenRecordSym); matchPatternRecType.restFieldType = restType != null ? restType : symTable.anyOrErrorType; recordVarType.restFieldType = matchPatternRecType.restFieldType; restMatchPattern.setBType(matchPatternRecType); @@ -3126,7 +3129,7 @@ private void assignTypesToMemberPatterns(BLangMatchPattern matchPattern, BType b type, null, null, null); newMembers.add(new BTupleMember(type, varSymbol)); } - BTupleType tupleType = new BTupleType(symTable.typeEnv(), newMembers); + BTupleType tupleType = new BTupleType(typeEnv, newMembers); if (listMatchPattern.restMatchPattern == null) { listMatchPattern.setBType(tupleType); @@ -3179,7 +3182,7 @@ private void assignTypesToMemberPatterns(BLangMatchPattern matchPattern, BType b private BTupleType createTupleForClosedArray(int noOfElements, BTupleMember elementType) { List members = Collections.nCopies(noOfElements, elementType); - return new BTupleType(symTable.typeEnv(), members); + return new BTupleType(typeEnv, members); } private BType createTypeForTupleRestType(int startIndex, List members, BType patternRestType) { @@ -3188,16 +3191,16 @@ private BType createTypeForTupleRestType(int startIndex, List memb remainingMembers.add(members.get(i)); } if (!remainingMembers.isEmpty()) { - BTupleType restTupleType = new BTupleType(symTable.typeEnv(), remainingMembers); + BTupleType restTupleType = new BTupleType(typeEnv, remainingMembers); if (patternRestType != null) { restTupleType.restType = patternRestType; } return restTupleType; } else { if (patternRestType != null) { - return new BArrayType(symTable.typeEnv(), patternRestType); + return new BArrayType(typeEnv, patternRestType); } else { - return new BArrayType(symTable.typeEnv(), symTable.anyOrErrorType); + return new BArrayType(typeEnv, symTable.anyOrErrorType); } } } @@ -3302,13 +3305,13 @@ public void visit(BLangListBindingPattern listBindingPattern, AnalyzerData data) listMembers.add(new BTupleMember(type, varSymbol)); listBindingPattern.declaredVars.putAll(bindingPattern.declaredVars); } - BTupleType listBindingPatternType = new BTupleType(symTable.typeEnv(), listMembers); + BTupleType listBindingPatternType = new BTupleType(typeEnv, listMembers); if (listBindingPattern.restBindingPattern != null) { BLangRestBindingPattern restBindingPattern = listBindingPattern.restBindingPattern; BType restBindingPatternType = restBindingPattern.getBType(); BType restType = restBindingPatternType != null ? restBindingPatternType : symTable.anyOrErrorType; - restBindingPattern.setBType(new BArrayType(symTable.typeEnv(), restType)); + restBindingPattern.setBType(new BArrayType(typeEnv, restType)); restBindingPattern.accept(this, data); listBindingPattern.declaredVars.put(restBindingPattern.variableName.value, restBindingPattern.symbol); listBindingPatternType.restType = restType; @@ -3405,7 +3408,7 @@ public void visit(BLangErrorFieldBindingPatterns errorFieldBindingPatterns, Anal } if (errorFieldBindingPatterns.restBindingPattern != null) { errorFieldBindingPatterns.restBindingPattern.setBType( - new BMapType(symTable.typeEnv(), TypeTags.MAP, symTable.anydataType, null)); + new BMapType(typeEnv, TypeTags.MAP, symTable.anydataType, null)); analyzeNode(errorFieldBindingPatterns.restBindingPattern, data); errorFieldBindingPatterns.declaredVars.putAll(errorFieldBindingPatterns.restBindingPattern.declaredVars); } @@ -3519,7 +3522,7 @@ public void visit(BLangErrorFieldMatchPatterns errorFieldMatchPatterns, Analyzer errorFieldMatchPatterns.declaredVars.putAll(namedArgMatchPattern.declaredVars); } if (errorFieldMatchPatterns.restMatchPattern != null) { - errorFieldMatchPatterns.restMatchPattern.setBType(new BMapType(symTable.typeEnv(), TypeTags.MAP, + errorFieldMatchPatterns.restMatchPattern.setBType(new BMapType(typeEnv, TypeTags.MAP, symTable.anydataType, null)); analyzeNode(errorFieldMatchPatterns.restMatchPattern, data); errorFieldMatchPatterns.declaredVars.putAll(errorFieldMatchPatterns.restMatchPattern.declaredVars); @@ -3575,7 +3578,7 @@ public void visit(BLangMappingBindingPattern mappingBindingPattern, AnalyzerData fields.put(fieldName.getValue(), field); mappingBindingPattern.declaredVars.putAll(fieldBindingPattern.declaredVars); } - BRecordType recordVarType = new BRecordType(symTable.typeEnv(), recordSymbol); + BRecordType recordVarType = new BRecordType(typeEnv, recordSymbol); recordVarType.fields = fields; recordVarType.restFieldType = symTable.anyOrErrorType; if (mappingBindingPattern.restBindingPattern != null) { @@ -3583,7 +3586,7 @@ public void visit(BLangMappingBindingPattern mappingBindingPattern, AnalyzerData BType restType = restBindingPattern.getBType(); BRecordTypeSymbol matchPattenRecordSym = symbolEnter.createAnonRecordSymbol(currentEnv, restBindingPattern.pos); - BRecordType matchPatternRecType = new BRecordType(symTable.typeEnv(), matchPattenRecordSym); + BRecordType matchPatternRecType = new BRecordType(typeEnv, matchPattenRecordSym); matchPatternRecType.restFieldType = restType != null ? restType : symTable.anyOrErrorType; recordVarType.restFieldType = matchPatternRecType.restFieldType; restBindingPattern.setBType(matchPatternRecType); @@ -3642,13 +3645,13 @@ public void visit(BLangListMatchPattern listMatchPattern, AnalyzerData data) { checkForSimilarVars(listMatchPattern.declaredVars, memberMatchPattern.declaredVars, memberMatchPattern.pos); listMatchPattern.declaredVars.putAll(memberMatchPattern.declaredVars); } - BTupleType matchPatternType = new BTupleType(symTable.typeEnv(), members); + BTupleType matchPatternType = new BTupleType(typeEnv, members); if (listMatchPattern.getRestMatchPattern() != null) { BLangRestMatchPattern restMatchPattern = (BLangRestMatchPattern) listMatchPattern.getRestMatchPattern(); BType restBindingPatternType = restMatchPattern.getBType(); BType restType = restBindingPatternType != null ? restBindingPatternType : symTable.anyOrErrorType; - restMatchPattern.setBType(new BArrayType(symTable.typeEnv(), restType)); + restMatchPattern.setBType(new BArrayType(typeEnv, restType)); restMatchPattern.accept(this, data); checkForSimilarVars(listMatchPattern.declaredVars, restMatchPattern.declaredVars, restMatchPattern.pos); listMatchPattern.declaredVars.put(restMatchPattern.variableName.value, restMatchPattern.symbol); @@ -3732,7 +3735,7 @@ private void assignTypesToMemberPatterns(BLangBindingPattern bindingPattern, BTy BVarSymbol varSymbol = new BVarSymbol(type.getFlags(), null, null, type, null, null, null); members.add(new BTupleMember(type, varSymbol)); } - BTupleType tupleType = new BTupleType(symTable.typeEnv(), members); + BTupleType tupleType = new BTupleType(typeEnv, members); if (listBindingPattern.restBindingPattern == null) { bindingPattern.setBType(tupleType); @@ -3858,7 +3861,7 @@ public void visit(BLangOnFailClause onFailClause, AnalyzerData data) { if (currentOnFailErrTypes.size() == 1) { failErrorType = currentOnFailErrTypes.iterator().next(); } else if (currentOnFailErrTypes.size() > 1) { - failErrorType = BUnionType.create(symTable.typeEnv(), null, currentOnFailErrTypes); + failErrorType = BUnionType.create(typeEnv, null, currentOnFailErrTypes); } else { failErrorType = symTable.neverType; } @@ -4078,7 +4081,7 @@ private void inferServiceTypeFromListeners(BLangService serviceNode, AnalyzerDat for (BType attachType : listenerTypes) { typeIdSet.add(getTypeIds(attachType)); } - inferred = BUnionType.create(symTable.typeEnv(), null, listenerTypes); + inferred = BUnionType.create(typeEnv, null, listenerTypes); } serviceNode.inferredServiceType = inferred; @@ -4201,7 +4204,7 @@ public void visit(BLangTransaction transactionNode, AnalyzerData data) { @Override public void visit(BLangRollback rollbackNode, AnalyzerData data) { if (rollbackNode.expr != null) { - BType expectedType = BUnionType.create(symTable.typeEnv(), null, symTable.errorType, symTable.nilType); + BType expectedType = BUnionType.create(typeEnv, null, symTable.errorType, symTable.nilType); this.typeChecker.checkExpr(rollbackNode.expr, data.env, expectedType, data.prevEnvs, data.commonAnalyzerData); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java index 5096d4339127..6afcb969cbf4 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java @@ -5008,8 +5008,8 @@ private void defineReferencedFunction(Location location, Set flagSet, Symb // If not, define the function symbol within the object. // Take a copy of the symbol, with the new name, and the package ID same as the object type. - BInvokableSymbol funcSymbol = ASTBuilderUtil.duplicateFunctionDeclarationSymbol(referencedFuncSymbol, - typeDefSymbol, funcName, typeDefSymbol.pkgID, typeRef.pos, getOrigin(funcName)); + BInvokableSymbol funcSymbol = ASTBuilderUtil.duplicateFunctionDeclarationSymbol(symTable.typeEnv(), + referencedFuncSymbol, typeDefSymbol, funcName, typeDefSymbol.pkgID, typeRef.pos, getOrigin(funcName)); defineSymbol(typeRef.pos, funcSymbol, objEnv); // Create and define the parameters and receiver. This should be done after defining the function symbol. diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolResolver.java index bb7f3fd18b54..c0536ed6796b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolResolver.java @@ -1046,7 +1046,7 @@ public void bootstrapAnydataType() { continue; } BUnionType type = (BUnionType) Types.getImpliedType(entry.symbol.type); - symTable.anydataType = new BAnydataType(type); + symTable.anydataType = new BAnydataType(types.semTypeCtx, type); Optional immutableType = Types.getImmutableType(symTable, PackageID.ANNOTATIONS, type); if (immutableType.isPresent()) { Types.addImmutableType(symTable, PackageID.ANNOTATIONS, symTable.anydataType, immutableType.get()); @@ -1071,7 +1071,7 @@ public void bootstrapJsonType() { continue; } BUnionType type = (BUnionType) Types.getImpliedType(entry.symbol.type); - symTable.jsonType = new BJSONType(type); + symTable.jsonType = new BJSONType(types.semTypeCtx, type); Optional immutableType = Types.getImmutableType(symTable, PackageID.ANNOTATIONS, type); if (immutableType.isPresent()) { @@ -1404,7 +1404,7 @@ public BType transform(BLangTableTypeNode tableTypeNode, AnalyzerData data) { return symTable.noType; } - BTableType tableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, constraintType, null); + BTableType tableType = new BTableType(symTable.typeEnv(), constraintType, null); BTypeSymbol typeSymbol = type.tsymbol; tableType.tsymbol = Symbols.createTypeSymbol(SymTag.TYPE, Flags.asMask(EnumSet.noneOf(Flag.class)), typeSymbol.name, typeSymbol.originalName, typeSymbol.pkgID, @@ -1548,7 +1548,7 @@ public BType transform(BLangConstrainedType constrainedTypeNode, AnalyzerData da BType constrainedType; if (type.tag == TypeTags.FUTURE) { - constrainedType = new BFutureType(symTable.typeEnv(), TypeTags.FUTURE, constraintType, null); + constrainedType = new BFutureType(symTable.typeEnv(), constraintType, null); } else if (type.tag == TypeTags.MAP) { constrainedType = new BMapType(symTable.typeEnv(), TypeTags.MAP, constraintType, null); } else if (type.tag == TypeTags.TYPEDESC) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java index a47b1184e68e..4429826f62da 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java @@ -27,6 +27,7 @@ import io.ballerina.types.EnumerableCharString; import io.ballerina.types.EnumerableString; import io.ballerina.types.EnumerableType; +import io.ballerina.types.Env; import io.ballerina.types.PredefinedType; import io.ballerina.types.SemType; import io.ballerina.types.SemTypes; @@ -276,6 +277,7 @@ public class TypeChecker extends SimpleBLangNodeAnalyzer key) { @@ -365,6 +368,7 @@ public TypeChecker(CompilerContext context, CompilerContext.Key key this.missingNodesHelper = BLangMissingNodesHelper.getInstance(context); this.unifier = new Unifier(); this.queryTypeChecker = null; + this.typeEnv = types.typeEnv(); } private BType checkExpr(BLangExpression expr, SymbolEnv env, AnalyzerData data) { @@ -929,8 +933,7 @@ public BType setLiteralValueAndGetType(BLangLiteral literalExpr, BType expType, if (referedType.tag == TypeTags.BYTE_ARRAY) { // check whether this is a byte array byte[] byteArray = types.convertToByteArray((String) literalExpr.value); - literalType = new BArrayType(symTable.typeEnv(), symTable.byteType, null, byteArray.length, - BArrayState.CLOSED); + literalType = new BArrayType(typeEnv, symTable.byteType, null, byteArray.length, BArrayState.CLOSED); if (Symbols.isFlagOn(expectedType.getFlags(), Flags.READONLY)) { literalType = ImmutableTypeCloner.getEffectiveImmutableType(literalExpr.pos, types, literalType, data.env, symTable, anonymousModelHelper, names); @@ -1150,7 +1153,7 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr, AnalyzerData d recordLiteral.setBType(inherentMemberType); } } - BTableType tableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, inherentMemberType, null); + BTableType tableType = new BTableType(typeEnv, inherentMemberType, null); if (!validateTableConstructorExpr(tableConstructorExpr, tableType, data)) { data.resultType = symTable.semanticError; return; @@ -1195,8 +1198,7 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr, AnalyzerData d return; } - BTableType tableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, inferTableMemberType(memTypes, - applicableExpType), null); + BTableType tableType = new BTableType(typeEnv, inferTableMemberType(memTypes, applicableExpType), null); if (Symbols.isFlagOn(applicableExpType.getFlags(), Flags.READONLY)) { tableType.addFlags(Flags.READONLY); @@ -1283,8 +1285,7 @@ private BType getInferredTableType(BLangTableConstructorExpr exprToLog, Analyzer } } - return new BTableType(symTable.typeEnv(), TypeTags.TABLE, inferTableMemberType(memTypes, exprToLog, data), - null); + return new BTableType(typeEnv, inferTableMemberType(memTypes, exprToLog, data), null); } private boolean checkKeySpecifier(BLangTableConstructorExpr tableConstructorExpr, BTableType tableType, @@ -1310,12 +1311,12 @@ private BType inferTableMemberType(List memTypes, BType expType) { result.add(memTypes.get(0)); - BUnionType unionType = BUnionType.create(symTable.typeEnv(), null, result); + BUnionType unionType = BUnionType.create(typeEnv, null, result); for (int i = 1; i < memTypes.size(); i++) { BType source = memTypes.get(i); if (!types.isAssignable(source, unionType)) { result.add(source); - unionType = BUnionType.create(symTable.typeEnv(), null, result); + unionType = BUnionType.create(typeEnv, null, result); } } @@ -1471,7 +1472,7 @@ private BRecordType createTableConstraintRecordType(Set inferredFields, recordSymbol.scope.define(field.name, field.symbol); } - BRecordType recordType = new BRecordType(symTable.typeEnv(), recordSymbol); + BRecordType recordType = new BRecordType(typeEnv, recordSymbol); recordType.fields = inferredFields.stream().collect(getFieldCollector()); recordSymbol.type = recordType; @@ -1784,7 +1785,7 @@ private BType createTableKeyConstraint(List fieldNames, BType constraint return memTypes.get(0).type; } - return new BTupleType(symTable.typeEnv(), memTypes); + return new BTupleType(typeEnv, memTypes); } protected BType checkListConstructorCompatibility(BType bType, BLangListConstructorExpr listConstructor, @@ -1896,13 +1897,13 @@ private BType checkListConstructorCompatibility(BType referredType, BType origin List members = new ArrayList<>(); inferredTupleDetails.fixedMemberTypes.forEach(memberType -> members.add(new BTupleMember(memberType, new BVarSymbol(memberType.getFlags(), null, null, memberType, null, null, null)))); - BTupleType tupleType = new BTupleType(symTable.typeEnv(), members); + BTupleType tupleType = new BTupleType(typeEnv, members); if (!inferredTupleDetails.restMemberTypes.isEmpty()) { tupleType.restType = getRepresentativeBroadType(inferredTupleDetails.restMemberTypes); } listConstructor.typedescType = tupleType; - return new BTypedescType(symTable.typeEnv(), listConstructor.typedescType, null); + return new BTypedescType(typeEnv, listConstructor.typedescType, null); } if (referredType == symTable.semanticError) { @@ -2155,7 +2156,7 @@ private BType checkTupleType(BLangListConstructorExpr listConstructor, BTupleTyp BLangExpression spreadOpExpr = ((BLangListConstructorSpreadOpExpr) expr).expr; BType spreadOpType; if (restType != null && restType != symTable.noType && remainNonRestCount == 0) { - BType targetType = new BArrayType(symTable.typeEnv(), restType); + BType targetType = new BArrayType(typeEnv, restType); BType possibleType = silentTypeCheckExpr(spreadOpExpr, targetType, data); if (possibleType == symTable.semanticError) { spreadOpType = checkExpr(spreadOpExpr, data); @@ -2356,7 +2357,7 @@ protected BType getInferredTupleType(BLangListConstructorExpr listConstructor, B List members = new ArrayList<>(); fixedMemberTypes.forEach(memberType -> members.add(new BTupleMember(memberType, new BVarSymbol(memberType.getFlags(), null, null, memberType, null, null, null)))); - BTupleType tupleType = new BTupleType(symTable.typeEnv(), members); + BTupleType tupleType = new BTupleType(typeEnv, members); if (!restMemberTypes.isEmpty()) { tupleType.restType = getRepresentativeBroadType(restMemberTypes); } @@ -2469,7 +2470,7 @@ public BType getEffectiveMappingType(BLangRecordLiteral recordLiteral, BType app recordSymbol.scope.define(fieldName, fieldSymbol); } - BRecordType recordType = new BRecordType(symTable.typeEnv(), recordSymbol, recordSymbol.flags); + BRecordType recordType = new BRecordType(typeEnv, recordSymbol, recordSymbol.flags); if (refType.tag == TypeTags.MAP) { recordType.sealed = false; recordType.restFieldType = ((BMapType) refType).constraint; @@ -2895,7 +2896,7 @@ public void visit(BLangWorkerFlushExpr workerFlushExpr, AnalyzerData data) { } } } - BType actualType = BUnionType.create(symTable.typeEnv(), null, symTable.errorType, symTable.nilType); + BType actualType = BUnionType.create(typeEnv, null, symTable.errorType, symTable.nilType); data.resultType = types.checkType(workerFlushExpr, actualType, data.expType); } @@ -3196,11 +3197,10 @@ public void visit(BLangSimpleVarRef varRefExpr, AnalyzerData data) { if (symbol.kind == SymbolKind.TYPE_DEF) { BTypeDefinitionSymbol typeDefSym = (BTypeDefinitionSymbol) symbol; actualType = Types.getImpliedType(symbol.type).tag == TypeTags.TYPEDESC ? - typeDefSym.referenceType - : new BTypedescType(symTable.typeEnv(), typeDefSym.referenceType, null); + typeDefSym.referenceType : new BTypedescType(typeEnv, typeDefSym.referenceType, null); } else { actualType = symbol.type.tag == TypeTags.TYPEDESC ? symbol.type - : new BTypedescType(symTable.typeEnv(), symbol.type, null); + : new BTypedescType(typeEnv, symbol.type, null); } varRefExpr.symbol = symbol; } else if ((symbol.tag & SymTag.CONSTANT) == SymTag.CONSTANT) { @@ -3292,7 +3292,7 @@ public void visit(BLangRecordVarRef varRefExpr, AnalyzerData data) { return; } - BRecordType bRecordType = new BRecordType(symTable.typeEnv(), recordSymbol); + BRecordType bRecordType = new BRecordType(typeEnv, recordSymbol); bRecordType.fields = fields; recordSymbol.type = bRecordType; varRefExpr.symbol = new BVarSymbol(0, recordSymbol.name, recordSymbol.getOriginalName(), @@ -3416,8 +3416,8 @@ public void visit(BLangErrorVarRef varRefExpr, AnalyzerData data) { BType errorDetailType = errorRefRestFieldType == symTable.anydataOrReadonly ? symTable.errorType.detailType - : new BMapType(symTable.typeEnv(), TypeTags.MAP, errorRefRestFieldType, null, Flags.PUBLIC); - data.resultType = new BErrorType(symTable.typeEnv(), symTable.errorType.tsymbol, errorDetailType); + : new BMapType(typeEnv, TypeTags.MAP, errorRefRestFieldType, null, Flags.PUBLIC); + data.resultType = new BErrorType(typeEnv, symTable.errorType.tsymbol, errorDetailType); } private void checkIndirectErrorVarRef(BLangErrorVarRef varRefExpr, AnalyzerData data) { @@ -3451,7 +3451,7 @@ public void visit(BLangTupleVarRef varRefExpr, AnalyzerData data) { null, null); results.add(new BTupleMember(memberType, varSymbol)); } - BTupleType actualType = new BTupleType(symTable.typeEnv(), results); + BTupleType actualType = new BTupleType(typeEnv, results); if (varRefExpr.restParam != null) { BLangExpression restExpr = varRefExpr.restParam; ((BLangVariableReference) restExpr).isLValue = true; @@ -3795,7 +3795,7 @@ public void visit(BLangErrorConstructorExpr errorConstructorExpr, AnalyzerData d if (errorDetailTypes.size() == 1) { detailCandidate = errorDetailTypes.get(0); } else { - detailCandidate = BUnionType.create(symTable.typeEnv(), null, new LinkedHashSet<>(errorDetailTypes)); + detailCandidate = BUnionType.create(typeEnv, null, new LinkedHashSet<>(errorDetailTypes)); } BLangRecordLiteral recordLiteral = createRecordLiteralForErrorConstructor(errorConstructorExpr); @@ -4113,7 +4113,7 @@ private BTupleType getResourcePathType(List pathSegm pathSegmentCount--; } - BTupleType resourcePathType = new BTupleType(symTable.typeEnv(), new ArrayList<>()); + BTupleType resourcePathType = new BTupleType(typeEnv, new ArrayList<>()); if (pathSegmentCount > 0 && lastPathSegmentSym.kind != SymbolKind.RESOURCE_ROOT_PATH_SEGMENT) { for (BResourcePathSegmentSymbol s : pathSegmentSymbols.subList(0, pathSegmentCount)) { BVarSymbol varSymbol = Symbols.createVarSymbolForTupleMember(s.type); @@ -4141,7 +4141,7 @@ public boolean validateResourceAccessPathSegmentTypes(BLangListConstructorExpr r BLangExpression spreadOpExpr = ((BLangListConstructorSpreadOpExpr) clonedPathSegment).expr; BType pathSegmentType = checkExpr(spreadOpExpr, data); if (!types.isAssignable(pathSegmentType, - new BArrayType(symTable.typeEnv(), symTable.pathParamAllowedType))) { + new BArrayType(typeEnv, symTable.pathParamAllowedType))) { dlog.error(clonedPathSegment.getPosition(), DiagnosticErrorCode.UNSUPPORTED_RESOURCE_ACCESS_REST_SEGMENT_TYPE, pathSegmentType); isValidResourceAccessPathSegmentTypes = false; @@ -4656,7 +4656,7 @@ private BType checkObjectType(BType actualType, BLangTypeInit cIExpr, AnalyzerDa } private BUnionType createNextReturnType(Location pos, BStreamType streamType, AnalyzerData data) { - BRecordType recordType = new BRecordType(symTable.typeEnv(), null, Flags.ANONYMOUS); + BRecordType recordType = new BRecordType(typeEnv, null, Flags.ANONYMOUS); recordType.restFieldType = symTable.noType; recordType.sealed = true; @@ -4677,7 +4677,7 @@ private BUnionType createNextReturnType(Location pos, BStreamType streamType, An retTypeMembers.add(recordType); retTypeMembers.addAll(types.getAllTypes(streamType.completionType, false)); - BUnionType unionType = BUnionType.create(symTable.typeEnv(), null); + BUnionType unionType = BUnionType.create(typeEnv, null); unionType.addAll(retTypeMembers); unionType.tsymbol = Symbols.createTypeSymbol(SymTag.UNION_TYPE, 0, Names.EMPTY, data.env.enclPkg.symbol.pkgID, unionType, data.env.scope.owner, pos, VIRTUAL); @@ -4707,7 +4707,7 @@ private BType getObjectConstructorReturnType(BType objType, BType initRetType, A retTypeMembers.addAll(((BUnionType) initRetType).getMemberTypes()); retTypeMembers.remove(symTable.nilType); - BUnionType unionType = BUnionType.create(symTable.typeEnv(), null, retTypeMembers); + BUnionType unionType = BUnionType.create(typeEnv, null, retTypeMembers); unionType.tsymbol = Symbols.createTypeSymbol(SymTag.UNION_TYPE, 0, Names.EMPTY, data.env.enclPkg.symbol.pkgID, unionType, data.env.scope.owner, symTable.builtinPos, VIRTUAL); @@ -4883,26 +4883,24 @@ private void setResultTypeForWaitForAllExpr(BLangWaitForAllExpr waitForAllExpr, checkTypesForMap(waitForAllExpr, ((BMapType) referredType).constraint, data); LinkedHashSet memberTypesForMap = collectWaitExprTypes(waitForAllExpr.keyValuePairs); if (memberTypesForMap.size() == 1) { - data.resultType = new BMapType(symTable.typeEnv(), TypeTags.MAP, + data.resultType = new BMapType(typeEnv, TypeTags.MAP, memberTypesForMap.iterator().next(), symTable.mapType.tsymbol); break; } - BUnionType constraintTypeForMap = BUnionType.create(symTable.typeEnv(), null, memberTypesForMap); - data.resultType = new BMapType(symTable.typeEnv(), TypeTags.MAP, constraintTypeForMap, - symTable.mapType.tsymbol); + BUnionType constraintTypeForMap = BUnionType.create(typeEnv, null, memberTypesForMap); + data.resultType = new BMapType(typeEnv, TypeTags.MAP, constraintTypeForMap, symTable.mapType.tsymbol); break; case TypeTags.NONE: case TypeTags.ANY: checkTypesForMap(waitForAllExpr, expType, data); LinkedHashSet memberTypes = collectWaitExprTypes(waitForAllExpr.keyValuePairs); if (memberTypes.size() == 1) { - data.resultType = new BMapType(symTable.typeEnv(), TypeTags.MAP, memberTypes.iterator().next(), + data.resultType = new BMapType(typeEnv, TypeTags.MAP, memberTypes.iterator().next(), symTable.mapType.tsymbol); break; } - BUnionType constraintType = BUnionType.create(symTable.typeEnv(), null, memberTypes); - data.resultType = new BMapType(symTable.typeEnv(), TypeTags.MAP, constraintType, - symTable.mapType.tsymbol); + BUnionType constraintType = BUnionType.create(typeEnv, null, memberTypes); + data.resultType = new BMapType(typeEnv, TypeTags.MAP, constraintType, symTable.mapType.tsymbol); break; default: dlog.error(waitForAllExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES, expType, @@ -4914,7 +4912,7 @@ private void setResultTypeForWaitForAllExpr(BLangWaitForAllExpr waitForAllExpr, private BRecordType getWaitForAllExprReturnType(BLangWaitForAllExpr waitExpr, Location pos, AnalyzerData data) { - BRecordType retType = new BRecordType(symTable.typeEnv(), null, Flags.ANONYMOUS); + BRecordType retType = new BRecordType(typeEnv, null, Flags.ANONYMOUS); List keyVals = waitExpr.keyValuePairs; for (BLangWaitForAllExpr.BLangWaitKeyValue keyVal : keyVals) { @@ -5043,7 +5041,7 @@ private void checkWaitKeyValExpr(BLangWaitForAllExpr.BLangWaitKeyValue keyVal, B } else { expr = keyVal.valueExpr; } - BFutureType futureType = new BFutureType(symTable.typeEnv(), TypeTags.FUTURE, type, null); + BFutureType futureType = new BFutureType(typeEnv, type, null); checkExpr(expr, futureType, data); setEventualTypeForExpression(expr, type, data); } @@ -5070,14 +5068,14 @@ private void setEventualTypeForExpression(BLangExpression expression, return; } - BUnionType eventualType = BUnionType.create(symTable.typeEnv(), null, currentType, symTable.errorType); + BUnionType eventualType = BUnionType.create(typeEnv, null, currentType, symTable.errorType); BType referredExpType = Types.getImpliedType(currentExpectedType); if (((referredExpType.tag != TypeTags.NONE) && (referredExpType.tag != TypeTags.NIL)) && !types.isAssignable(eventualType, currentExpectedType)) { dlog.error(expression.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPE_WAIT_FUTURE_EXPR, currentExpectedType, eventualType, expression); } - futureType.constraint = eventualType; + futureType.setConstraint(eventualType); } private void setEventualTypeForWaitExpression(BLangExpression expression, Location pos, AnalyzerData data) { @@ -5090,7 +5088,7 @@ private void setEventualTypeForWaitExpression(BLangExpression expression, Locati } BType currentExpectedType = ((BFutureType) data.expType).constraint; BType referredExpType = Types.getImpliedType(currentExpectedType); - BUnionType eventualType = BUnionType.create(symTable.typeEnv(), null, data.resultType, symTable.errorType); + BUnionType eventualType = BUnionType.create(typeEnv, null, data.resultType, symTable.errorType); if ((referredExpType.tag == TypeTags.NONE) || (referredExpType.tag == TypeTags.NIL)) { data.resultType = eventualType; return; @@ -5105,7 +5103,7 @@ private void setEventualTypeForWaitExpression(BLangExpression expression, Locati BType referredResultType = Types.getImpliedType(data.resultType); if (referredResultType.tag == TypeTags.FUTURE) { - ((BFutureType) data.resultType).constraint = eventualType; + ((BFutureType) data.resultType).setConstraint(eventualType); } else { data.resultType = eventualType; } @@ -5127,7 +5125,7 @@ private void setEventualTypeForAlternateWaitExpression(BLangExpression expressio BType currentExpectedType = ((BFutureType) data.expType).constraint; BType referredExpType = Types.getImpliedType(currentExpectedType); - BUnionType eventualType = BUnionType.create(symTable.typeEnv(), null, data.resultType, symTable.errorType); + BUnionType eventualType = BUnionType.create(typeEnv, null, data.resultType, symTable.errorType); if ((referredExpType.tag == TypeTags.NONE) || (referredExpType.tag == TypeTags.NIL)) { data.resultType = eventualType; return; @@ -5142,7 +5140,7 @@ private void setEventualTypeForAlternateWaitExpression(BLangExpression expressio BType referredResultType = Types.getImpliedType(data.resultType); if (referredResultType.tag == TypeTags.FUTURE) { - ((BFutureType) referredResultType).constraint = eventualType; + ((BFutureType) referredResultType).setConstraint(eventualType); } else { data.resultType = eventualType; } @@ -5217,12 +5215,12 @@ private BType getConditionalExprType(BType lhsType, BType rhsType) { if (types.isAssignable(lhsType, rhsType)) { return rhsType; } - return BUnionType.create(symTable.typeEnv(), null, lhsType, rhsType); + return BUnionType.create(typeEnv, null, lhsType, rhsType); } @Override public void visit(BLangWaitExpr waitExpr, AnalyzerData data) { - data.expType = new BFutureType(symTable.typeEnv(), TypeTags.FUTURE, data.expType, null); + data.expType = new BFutureType(typeEnv, data.expType, null); checkExpr(waitExpr.getExpression(), data.expType, data); // Handle union types in lhs BType referredResultType = Types.getImpliedType(data.resultType); @@ -5232,7 +5230,7 @@ public void visit(BLangWaitExpr waitExpr, AnalyzerData data) { if (memberTypes.size() == 1) { data.resultType = memberTypes.toArray(new BType[0])[0]; } else { - data.resultType = BUnionType.create(symTable.typeEnv(), null, memberTypes); + data.resultType = BUnionType.create(typeEnv, null, memberTypes); } } else if (data.resultType != symTable.semanticError) { // Handle other types except for semantic errors @@ -5293,7 +5291,7 @@ public void visit(BLangTrapExpr trapExpr, AnalyzerData data) { resultTypes.add(exprType); } resultTypes.add(symTable.errorType); - actualType = BUnionType.create(symTable.typeEnv(), null, resultTypes); + actualType = BUnionType.create(typeEnv, null, resultTypes); } data.resultType = types.checkType(trapExpr, actualType, data.expType); @@ -5314,7 +5312,7 @@ public void visit(BLangBinaryExpr binaryExpr, AnalyzerData data) { data.resultType = symTable.semanticError; return; } - data.resultType = BUnionType.create(symTable.typeEnv(), null, lhsResultType, rhsResultType); + data.resultType = BUnionType.create(typeEnv, null, lhsResultType, rhsResultType); return; } @@ -5357,8 +5355,7 @@ public void visit(BLangBinaryExpr binaryExpr, AnalyzerData data) { if (leftConstituent != null && rightConstituent != null) { actualType = - new BXMLType(BUnionType.create(symTable.typeEnv(), null, leftConstituent, rightConstituent), - null); + new BXMLType(BUnionType.create(typeEnv, null, leftConstituent, rightConstituent), null); break; } else if (leftConstituent != null || rightConstituent != null) { if (leftConstituent != null && types.isAssignable(rhsType, symTable.stringType)) { @@ -5431,8 +5428,7 @@ private BType getXmlStringBinaryOpResultType(BType opType, BType constituentType Symbols.createTypeSymbol(SymTag.UNION_TYPE, 0, Names.EMPTY, env.enclPkg.symbol.pkgID, null, env.scope.owner, pos, VIRTUAL); BType type = - new BXMLType(BUnionType.create(symTable.typeEnv(), typeSymbol, constituentType, symTable.xmlTextType), - null); + new BXMLType(BUnionType.create(typeEnv, typeSymbol, constituentType, symTable.xmlTextType), null); typeSymbol.type = type; return type; } @@ -5475,7 +5471,7 @@ public void visit(BLangTransactionalExpr transactionalExpr, AnalyzerData data) { @Override public void visit(BLangCommitExpr commitExpr, AnalyzerData data) { - BType actualType = BUnionType.create(symTable.typeEnv(), null, symTable.errorType, symTable.nilType); + BType actualType = BUnionType.create(typeEnv, null, symTable.errorType, symTable.nilType); data.resultType = types.checkType(commitExpr, actualType, data.expType); } @@ -5524,7 +5520,7 @@ public void visit(BLangTypedescExpr accessExpr, AnalyzerData data) { int resolveTypeTag = Types.getImpliedType(accessExpr.resolvedType).tag; final BType actualType; if (resolveTypeTag != TypeTags.TYPEDESC && resolveTypeTag != TypeTags.NONE) { - actualType = new BTypedescType(symTable.typeEnv(), accessExpr.resolvedType, null); + actualType = new BTypedescType(typeEnv, accessExpr.resolvedType, null); } else { actualType = accessExpr.resolvedType; } @@ -5571,7 +5567,7 @@ public BType getNewExpectedTypeForFiniteAndUnion(Set numericTypes, BType if (basicNumericTypes.size() == 1) { newExpectedType = basicNumericTypes.iterator().next(); } else if (basicNumericTypes.size() > 1) { - newExpectedType = BUnionType.create(symTable.typeEnv(), null, basicNumericTypes); + newExpectedType = BUnionType.create(typeEnv, null, basicNumericTypes); } return newExpectedType; } @@ -5584,13 +5580,13 @@ public BType setExpectedTypeForSubtractionOperator(AnalyzerData data) { if (TypeTags.isIntegerTypeTag(referredTypeTag)) { newExpectedType = types.getTypeIntersection(Types.IntersectionContext.compilerInternalIntersectionTestContext(), - BUnionType.create(symTable.typeEnv(), null, symTable.intType, symTable.floatType, + BUnionType.create(typeEnv, null, symTable.intType, symTable.floatType, symTable.decimalType), symTable.intType, data.env); } else if (referredTypeTag == TypeTags.FLOAT || referredTypeTag == TypeTags.DECIMAL) { newExpectedType = types.getTypeIntersection(Types.IntersectionContext.compilerInternalIntersectionTestContext(), - BUnionType.create(symTable.typeEnv(), null, symTable.intType, symTable.floatType, + BUnionType.create(typeEnv, null, symTable.intType, symTable.floatType, symTable.decimalType), referredType, data.env); } else if (referredTypeTag == TypeTags.FINITE) { @@ -5601,7 +5597,7 @@ public BType setExpectedTypeForSubtractionOperator(AnalyzerData data) { newExpectedType); } else if (referredTypeTag == TypeTags.JSON || referredTypeTag == TypeTags.ANYDATA || referredTypeTag == TypeTags.ANY) { - newExpectedType = BUnionType.create(symTable.typeEnv(), null, symTable.intType, symTable.floatType, + newExpectedType = BUnionType.create(typeEnv, null, symTable.intType, symTable.floatType, symTable.decimalType); } return newExpectedType; @@ -5659,7 +5655,7 @@ public BType getActualTypeForOtherUnaryExpr(BLangUnaryExpr unaryExpr, AnalyzerDa // basic type (int) when checking the expression. LinkedHashSet intTypesInUnion = getIntSubtypesInUnionType((BUnionType) referredType); if (!intTypesInUnion.isEmpty()) { - BType newReferredType = BUnionType.create(symTable.typeEnv(), null, intTypesInUnion); + BType newReferredType = BUnionType.create(typeEnv, null, intTypesInUnion); BType tempActualType = checkCompatibilityWithConstructedNumericLiteral(unaryExpr, newReferredType, data); if (tempActualType != symTable.semanticError) { @@ -5754,7 +5750,7 @@ public void visit(BLangUnaryExpr unaryExpr, AnalyzerData data) { } else if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) { exprType = checkExpr(unaryExpr.expr, data); if (exprType != symTable.semanticError) { - actualType = new BTypedescType(symTable.typeEnv(), exprType, null); + actualType = new BTypedescType(typeEnv, exprType, null); } } else { actualType = getActualTypeForOtherUnaryExpr(unaryExpr, data); @@ -6439,7 +6435,7 @@ protected void visitCheckAndCheckPanicExpr(BLangCheckedExpr checkedExpr, Analyze } else { BType exprType = getCandidateType(checkedExpr, data.expType, data); if (exprType == symTable.semanticError) { - checkExprCandidateType = BUnionType.create(symTable.typeEnv(), null, data.expType, symTable.errorType); + checkExprCandidateType = BUnionType.create(typeEnv, null, data.expType, symTable.errorType); } else { checkExprCandidateType = addDefaultErrorIfNoErrorComponentFound(data.expType); } @@ -6530,7 +6526,7 @@ protected void visitCheckAndCheckPanicExpr(BLangCheckedExpr checkedExpr, Analyze } else if (nonErrorTypes.size() == 1) { actualType = nonErrorTypes.get(0); } else { - actualType = BUnionType.create(symTable.typeEnv(), null, new LinkedHashSet<>(nonErrorTypes)); + actualType = BUnionType.create(typeEnv, null, new LinkedHashSet<>(nonErrorTypes)); } data.resultType = types.checkType(checkedExpr, actualType, data.expType); @@ -6551,7 +6547,7 @@ private void rewriteWithEnsureTypeFunc(BLangCheckedExpr checkedExpr, BType type, return; } ArrayList argExprs = new ArrayList<>(); - BType typedescType = new BTypedescType(symTable.typeEnv(), data.expType, null); + BType typedescType = new BTypedescType(typeEnv, data.expType, null); BLangTypedescExpr typedescExpr = new BLangTypedescExpr(); typedescExpr.resolvedType = data.expType; typedescExpr.setBType(typedescType); @@ -6600,7 +6596,7 @@ private BType addDefaultErrorIfNoErrorComponentFound(BType type) { return type; } } - return BUnionType.create(symTable.typeEnv(), null, type, symTable.errorType); + return BUnionType.create(typeEnv, null, type, symTable.errorType); } @Override @@ -6632,7 +6628,7 @@ public void visit(BLangAnnotAccessExpr annotAccessExpr, AnalyzerData data) { annotAccessExpr.annotationSymbol = (BAnnotationSymbol) symbol; BType annotType = ((BAnnotationSymbol) symbol).attachedType == null ? symTable.trueType : ((BAnnotationSymbol) symbol).attachedType; - actualType = BUnionType.create(symTable.typeEnv(), null, annotType, symTable.nilType); + actualType = BUnionType.create(typeEnv, null, annotType, symTable.nilType); } data.resultType = this.types.checkType(annotAccessExpr, actualType, data.expType); @@ -6694,7 +6690,7 @@ private BType getEffectiveReadOnlyType(Location pos, BType type, AnalyzerData da return type; } - BUnionType nonReadOnlyUnion = BUnionType.create(symTable.typeEnv(), null, nonReadOnlyTypes); + BUnionType nonReadOnlyUnion = BUnionType.create(typeEnv, null, nonReadOnlyTypes); nonReadOnlyUnion.add(ImmutableTypeCloner.getImmutableIntersectionType(pos, types, data.expType, data.env, symTable, anonymousModelHelper, names, new HashSet<>())); @@ -7452,7 +7448,7 @@ private BType checkInvocationArgs(BLangInvocation iExpr, List paramTypes, PackageID pkgID = data.env.enclPkg.symbol.pkgID; List tupleMembers = new ArrayList<>(); BRecordTypeSymbol recordSymbol = createRecordTypeSymbol(pkgID, null, VIRTUAL, data); - mappingTypeRestArg = new BRecordType(symTable.typeEnv(), recordSymbol); + mappingTypeRestArg = new BRecordType(typeEnv, recordSymbol); LinkedHashMap fields = new LinkedHashMap<>(); BType tupleRestType = null; BVarSymbol fieldSymbol; @@ -7484,7 +7480,7 @@ private BType checkInvocationArgs(BLangInvocation iExpr, List paramTypes, } } - BTupleType tupleType = new BTupleType(symTable.typeEnv(), tupleMembers); + BTupleType tupleType = new BTupleType(typeEnv, tupleMembers); tupleType.restType = tupleRestType; listTypeRestArg = tupleType; referredListTypeRestArg = tupleType; @@ -7520,7 +7516,7 @@ private BType checkInvocationArgs(BLangInvocation iExpr, List paramTypes, LinkedHashSet restTypes = new LinkedHashSet<>(); restTypes.add(listTypeRestArg); restTypes.add(mappingTypeRestArg); - BType actualType = BUnionType.create(symTable.typeEnv(), null, restTypes); + BType actualType = BUnionType.create(typeEnv, null, restTypes); checkTypeParamExpr(vararg, actualType, iExpr.langLibInvocation, data); } else { checkTypeParamExpr(vararg, listTypeRestArg, iExpr.langLibInvocation, data); @@ -7563,7 +7559,7 @@ private BType checkInvocationArgs(BLangInvocation iExpr, List paramTypes, if (restType != symTable.semanticError && (Symbols.isFlagOn(invokableSymbolFlags, Flags.INTERFACE) || Symbols.isFlagOn(invokableSymbolFlags, Flags.NATIVE)) && Symbols.isFlagOn(retType.getFlags(), Flags.PARAMETERIZED)) { - retType = unifier.build(retType, data.expType, iExpr, types, symTable, dlog); + retType = unifier.build(typeEnv, retType, data.expType, iExpr, types, symTable, dlog); } // check argument types in arr:sort function @@ -7706,7 +7702,7 @@ private BVarSymbol checkParameterNameForDefaultArgument(BLangIdentifier argName, private BFutureType generateFutureType(BInvokableSymbol invocableSymbol, BType retType) { boolean isWorkerStart = Symbols.isFlagOn(invocableSymbol.flags, Flags.WORKER); - return new BFutureType(symTable.typeEnv(), TypeTags.FUTURE, retType, null, isWorkerStart); + return new BFutureType(typeEnv, retType, null, isWorkerStart); } protected void checkTypeParamExpr(BLangExpression arg, BType expectedType, AnalyzerData data) { @@ -7994,7 +7990,7 @@ private TypeSymbolPair checkRecordLiteralKeyExpr(BLangExpression keyExpr, boolea fieldTypes.add(recordType.restFieldType); } - return new TypeSymbolPair(null, BUnionType.create(symTable.typeEnv(), null, fieldTypes)); + return new TypeSymbolPair(null, BUnionType.create(typeEnv, null, fieldTypes)); } else if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) { BLangSimpleVarRef varRef = (BLangSimpleVarRef) keyExpr; fieldName = names.fromIdNode(varRef.variableName); @@ -8040,7 +8036,7 @@ private BType getAllFieldType(BRecordType recordType) { possibleTypes.add(restFieldType); } - return BUnionType.create(symTable.typeEnv(), null, possibleTypes); + return BUnionType.create(typeEnv, null, possibleTypes); } private boolean checkValidJsonOrMapLiteralKeyExpr(BLangExpression keyExpr, boolean computedKey, AnalyzerData data) { @@ -8129,7 +8125,7 @@ private BType checkObjectFieldAccess(BLangFieldBasedAccess bLangFieldBasedAccess if (Symbols.isFlagOn(fieldSymbol.type.getFlags(), Flags.ISOLATED) && !Symbols.isFlagOn(objectType.getFlags(), Flags.ISOLATED)) { - fieldSymbol = ASTBuilderUtil.duplicateInvokableSymbol((BInvokableSymbol) fieldSymbol); + fieldSymbol = ASTBuilderUtil.duplicateInvokableSymbol(typeEnv, (BInvokableSymbol) fieldSymbol); fieldSymbol.flags &= ~Flags.ISOLATED; fieldSymbol.type.setFlags(fieldSymbol.type.getFlags() & ~Flags.ISOLATED); @@ -8186,7 +8182,7 @@ private void checkStringTemplateExprs(List exprs, Ana if (!types.isNonNilSimpleBasicTypeOrString(type)) { dlog.error(expr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES, - BUnionType.create(symTable.typeEnv(), null, symTable.intType, symTable.floatType, + BUnionType.create(typeEnv, null, symTable.intType, symTable.floatType, symTable.decimalType, symTable.stringType, symTable.booleanType), type); } @@ -8243,7 +8239,7 @@ private List concatSimilarKindXMLNodes(List ex !TypeTags.isIntegerTypeTag(referredType.tag) && !TypeTags.isStringTypeTag(referredType.tag)) { if (referredType != symTable.semanticError && !TypeTags.isXMLTypeTag(referredType.tag)) { dlog.error(expr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES, - BUnionType.create(symTable.typeEnv(), null, symTable.intType, symTable.floatType, + BUnionType.create(typeEnv, null, symTable.intType, symTable.floatType, symTable.decimalType, symTable.stringType, symTable.booleanType, symTable.xmlType), type); } @@ -8320,7 +8316,7 @@ private BType checkObjectFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr, return fieldTypeMembers.iterator().next(); } - return BUnionType.create(symTable.typeEnv(), null, fieldTypeMembers); + return BUnionType.create(typeEnv, null, fieldTypeMembers); } private BType checkRecordFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr, BType type, Name fieldName, @@ -8367,7 +8363,7 @@ private BType checkRecordFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr, return fieldTypeMembers.iterator().next(); } - return BUnionType.create(symTable.typeEnv(), null, fieldTypeMembers); + return BUnionType.create(typeEnv, null, fieldTypeMembers); } private boolean isFieldOptionalInRecords(BUnionType unionType, Name fieldName, @@ -8417,7 +8413,7 @@ private BType checkRecordFieldAccessLhsExpr(BLangFieldBasedAccess fieldAccessExp return fieldTypeMembers.iterator().next(); } - return BUnionType.create(symTable.typeEnv(), null, fieldTypeMembers); + return BUnionType.create(typeEnv, null, fieldTypeMembers); } private BType checkOptionalRecordFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr, BType varRefType, @@ -8464,7 +8460,7 @@ private BType checkOptionalRecordFieldAccessExpr(BLangFieldBasedAccess fieldAcce if (fieldTypeMembers.size() == 1) { fieldType = fieldTypeMembers.iterator().next(); } else { - fieldType = BUnionType.create(symTable.typeEnv(), null, fieldTypeMembers); + fieldType = BUnionType.create(typeEnv, null, fieldTypeMembers); } return nonMatchedRecordExists ? types.addNilForNillableAccessType(fieldType) : fieldType; @@ -8588,7 +8584,7 @@ private BType checkFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr, BType resolveXMLNamespace((BLangFieldBasedAccess.BLangPrefixedFieldBasedAccess) fieldAccessExpr, data); } BType laxFieldAccessType = getLaxFieldAccessType(varRefType); - actualType = BUnionType.create(symTable.typeEnv(), null, laxFieldAccessType, symTable.errorType); + actualType = BUnionType.create(typeEnv, null, laxFieldAccessType, symTable.errorType); fieldAccessExpr.originalType = laxFieldAccessType; } else if (fieldAccessExpr.expr.getKind() == NodeKind.FIELD_BASED_ACCESS_EXPR && hasLaxOriginalType(((BLangFieldBasedAccess) fieldAccessExpr.expr))) { @@ -8597,7 +8593,7 @@ private BType checkFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr, BType if (fieldAccessExpr.fieldKind == FieldKind.WITH_NS) { resolveXMLNamespace((BLangFieldBasedAccess.BLangPrefixedFieldBasedAccess) fieldAccessExpr, data); } - actualType = BUnionType.create(symTable.typeEnv(), null, laxFieldAccessType, symTable.errorType); + actualType = BUnionType.create(typeEnv, null, laxFieldAccessType, symTable.errorType); fieldAccessExpr.errorSafeNavigation = true; fieldAccessExpr.originalType = laxFieldAccessType; } else if (TypeTags.isXMLTypeTag(referredVarRefType.tag)) { @@ -8659,7 +8655,7 @@ private BType getLaxFieldAccessType(BType exprType) { LinkedHashSet memberTypes = new LinkedHashSet<>(); unionType.getMemberTypes().forEach(bType -> memberTypes.add(getLaxFieldAccessType(bType))); return memberTypes.size() == 1 ? memberTypes.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, memberTypes); + BUnionType.create(typeEnv, null, memberTypes); } return symTable.semanticError; } @@ -8684,7 +8680,7 @@ private BType checkOptionalFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr } referredType = nilRemovedSet.size() == 1 ? nilRemovedSet.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, nilRemovedSet); + BUnionType.create(typeEnv, null, nilRemovedSet); } } @@ -8701,7 +8697,7 @@ private BType checkOptionalFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr } else if (types.isLaxFieldAccessAllowed(referredType)) { BType laxFieldAccessType = getLaxFieldAccessType(referredType); actualType = accessCouldResultInError(referredType) ? - BUnionType.create(symTable.typeEnv(), null, laxFieldAccessType, symTable.errorType) : + BUnionType.create(typeEnv, null, laxFieldAccessType, symTable.errorType) : laxFieldAccessType; if (fieldAccessExpr.fieldKind == FieldKind.WITH_NS) { resolveXMLNamespace((BLangFieldBasedAccess.BLangPrefixedFieldBasedAccess) fieldAccessExpr, data); @@ -8714,7 +8710,7 @@ private BType checkOptionalFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr BType laxFieldAccessType = getLaxFieldAccessType(((BLangFieldBasedAccess) fieldAccessExpr.expr).originalType); actualType = accessCouldResultInError(referredType) ? - BUnionType.create(symTable.typeEnv(), null, laxFieldAccessType, symTable.errorType) : + BUnionType.create(typeEnv, null, laxFieldAccessType, symTable.errorType) : laxFieldAccessType; if (fieldAccessExpr.fieldKind == FieldKind.WITH_NS) { resolveXMLNamespace((BLangFieldBasedAccess.BLangPrefixedFieldBasedAccess) fieldAccessExpr, data); @@ -8729,7 +8725,7 @@ private BType checkOptionalFieldAccessExpr(BLangFieldBasedAccess fieldAccessExpr } if (nillableExprType && actualType != symTable.semanticError && !actualType.isNullable()) { - actualType = BUnionType.create(symTable.typeEnv(), null, actualType, symTable.nilType); + actualType = BUnionType.create(typeEnv, null, actualType, symTable.nilType); } return actualType; @@ -8761,7 +8757,7 @@ private BType checkIndexAccessExpr(BLangIndexBasedAccess indexBasedAccessExpr, A if (nillableExprType) { varRefType = nilRemovedSet.size() == 1 ? nilRemovedSet.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, nilRemovedSet); + BUnionType.create(typeEnv, null, nilRemovedSet); if (!types.isSubTypeOfMapping(varRefType.semType())) { // Member access is allowed on optional types only with mappings. @@ -8904,7 +8900,7 @@ private BType checkIndexAccessExpr(BLangIndexBasedAccess indexBasedAccessExpr, A } if (nillableExprType && !actualType.isNullable()) { - actualType = BUnionType.create(symTable.typeEnv(), null, actualType, symTable.nilType); + actualType = BUnionType.create(typeEnv, null, actualType, symTable.nilType); } return actualType; @@ -8925,7 +8921,7 @@ private BType getXmlMemberAccessType(BType varRefType) { effectiveMemberTypes.add(getXMLConstituents(memberType)); } xmlMemberAccessType = effectiveMemberTypes.size() == 1 ? effectiveMemberTypes.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, effectiveMemberTypes); + BUnionType.create(typeEnv, null, effectiveMemberTypes); } else { xmlMemberAccessType = getXMLConstituents(varRefType); } @@ -8935,7 +8931,7 @@ private BType getXmlMemberAccessType(BType varRefType) { } else if (types.isAssignable(symTable.xmlNeverType, xmlMemberAccessType)) { return xmlMemberAccessType; } - return BUnionType.create(symTable.typeEnv(), null, xmlMemberAccessType, symTable.xmlNeverType); + return BUnionType.create(typeEnv, null, xmlMemberAccessType, symTable.xmlNeverType); } private Long getConstIndex(BLangExpression indexExpr) { @@ -9058,7 +9054,7 @@ private BType checkListIndexBasedAccess(BLangIndexBasedAccess accessExpr, BType if (fieldTypeMembers.size() == 1) { return fieldTypeMembers.iterator().next(); } - return BUnionType.create(symTable.typeEnv(), null, fieldTypeMembers); + return BUnionType.create(typeEnv, null, fieldTypeMembers); } private BType checkTupleIndexBasedAccess(BLangIndexBasedAccess accessExpr, BTupleType tuple, BType currentType) { @@ -9074,7 +9070,7 @@ private BType checkTupleIndexBasedAccess(BLangIndexBasedAccess accessExpr, BTupl LinkedHashSet tupleTypes = collectTupleFieldTypes(tuple, new LinkedHashSet<>()); return tupleTypes.size() == 1 ? tupleTypes.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, tupleTypes); + BUnionType.create(typeEnv, null, tupleTypes); } switch (tag) { @@ -9101,7 +9097,7 @@ private BType checkTupleIndexBasedAccess(BLangIndexBasedAccess accessExpr, BTupl return symTable.semanticError; } actualType = possibleTypes.size() == 1 ? possibleTypes.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, possibleTypes); + BUnionType.create(typeEnv, null, possibleTypes); break; case TypeTags.UNION: LinkedHashSet possibleTypesByMember = new LinkedHashSet<>(); @@ -9139,7 +9135,7 @@ private BType checkTupleIndexBasedAccess(BLangIndexBasedAccess accessExpr, BTupl return symTable.semanticError; } actualType = possibleTypesByMember.size() == 1 ? possibleTypesByMember.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, possibleTypesByMember); + BUnionType.create(typeEnv, null, possibleTypesByMember); } return actualType; } @@ -9197,7 +9193,7 @@ private BType checkMappingIndexBasedAccess(BLangIndexBasedAccess accessExpr, BTy if (fieldTypeMembers.size() == 1) { fieldType = fieldTypeMembers.iterator().next(); } else { - fieldType = BUnionType.create(symTable.typeEnv(), null, fieldTypeMembers); + fieldType = BUnionType.create(typeEnv, null, fieldTypeMembers); } return nonMatchedRecordExists ? types.addNilForNillableAccessType(fieldType) : fieldType; @@ -9249,7 +9245,7 @@ private BType checkRecordIndexBasedAccess(BLangIndexBasedAccess accessExpr, BRec } actualType = fieldTypes.size() == 1 ? fieldTypes.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, fieldTypes); + BUnionType.create(typeEnv, null, fieldTypes); break; case TypeTags.FINITE: LinkedHashSet possibleTypes = new LinkedHashSet<>(); @@ -9292,7 +9288,7 @@ private BType checkRecordIndexBasedAccess(BLangIndexBasedAccess accessExpr, BRec } actualType = possibleTypes.size() == 1 ? possibleTypes.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, possibleTypes); + BUnionType.create(typeEnv, null, possibleTypes); break; case TypeTags.UNION: LinkedHashSet possibleTypesByMember = new LinkedHashSet<>(); @@ -9329,7 +9325,7 @@ private BType checkRecordIndexBasedAccess(BLangIndexBasedAccess accessExpr, BRec return symTable.semanticError; } actualType = possibleTypesByMember.size() == 1 ? possibleTypesByMember.iterator().next() : - BUnionType.create(symTable.typeEnv(), null, possibleTypesByMember); + BUnionType.create(typeEnv, null, possibleTypesByMember); } return actualType; } @@ -9430,7 +9426,7 @@ private BType getRepresentativeBroadType(List inferredTypeList) { return inferredTypeList.get(0); } - return BUnionType.create(symTable.typeEnv(), null, inferredTypeList.toArray(new BType[0])); + return BUnionType.create(typeEnv, null, inferredTypeList.toArray(new BType[0])); } public BType defineInferredRecordType(BLangRecordLiteral recordLiteral, BType expType, AnalyzerData data) { @@ -9511,7 +9507,7 @@ public BType defineInferredRecordType(BLangRecordLiteral recordLiteral, BType ex String key = entry.getKey(); Name fieldName = Names.fromString(key); BType type = types.size() == 1 ? types.get(0) : - BUnionType.create(symTable.typeEnv(), null, types.toArray(new BType[0])); + BUnionType.create(typeEnv, null, types.toArray(new BType[0])); Set flags = new HashSet<>(); @@ -9533,7 +9529,7 @@ public BType defineInferredRecordType(BLangRecordLiteral recordLiteral, BType ex recordSymbol.scope.define(fieldName, fieldSymbol); } - BRecordType recordType = new BRecordType(symTable.typeEnv(), recordSymbol); + BRecordType recordType = new BRecordType(typeEnv, recordSymbol); recordType.fields = fields; if (restFieldTypes.contains(symTable.semanticError)) { @@ -9547,7 +9543,7 @@ public BType defineInferredRecordType(BLangRecordLiteral recordLiteral, BType ex recordType.restFieldType = restFieldTypes.get(0); } else { recordType.restFieldType = - BUnionType.create(symTable.typeEnv(), null, restFieldTypes.toArray(new BType[0])); + BUnionType.create(typeEnv, null, restFieldTypes.toArray(new BType[0])); } recordSymbol.type = recordType; recordType.tsymbol = recordSymbol; @@ -9796,7 +9792,7 @@ private BType validateElvisExprLhsExpr(BLangElvisExpr elvisExpr, BType lhsType) } else if (size == 1) { actualType = memberTypes.iterator().next(); } else { - actualType = BUnionType.create(symTable.typeEnv(), null, memberTypes); + actualType = BUnionType.create(typeEnv, null, memberTypes); } } else { // We should get here only for `any` and nil. We use the type as is since we don't have a way to diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeHashVisitor.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeHashVisitor.java index 9d940a6957df..5e54c8a473a3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeHashVisitor.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeHashVisitor.java @@ -24,7 +24,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; @@ -205,7 +204,7 @@ public Integer visit(BArrayType type) { } @Override - public Integer visit(BBuiltInRefType type) { + public Integer visit(BReadonlyType type) { if (isVisited(type)) { return visited.get(type); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeParamAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeParamAnalyzer.java index 2612260cb3e6..7b3eda99d419 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeParamAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeParamAnalyzer.java @@ -278,9 +278,9 @@ private BType createBuiltInType(BType type, Name name, long flags) { TypeTags.DECIMAL, TypeTags.STRING, TypeTags.BOOLEAN -> new BType(tag, null, name, flags); - case TypeTags.ANY -> new BAnyType(null, name, flags); + case TypeTags.ANY -> new BAnyType(name, flags); case TypeTags.ANYDATA -> createAnydataType((BUnionType) referredType, name, flags); - case TypeTags.READONLY -> new BReadonlyType(null, name, flags); + case TypeTags.READONLY -> new BReadonlyType(flags); // For others, we will use TSymbol. default -> type; }; @@ -298,11 +298,11 @@ private BType createTypeParamType(BSymbol symbol, BType type, Name name, long fl case TypeTags.BOOLEAN: return new BType(type.tag, null, name, flags); case TypeTags.ANY: - return new BAnyType(null, name, flags); + return new BAnyType(name, flags); case TypeTags.ANYDATA: return createAnydataType((BUnionType) type, name, flags); case TypeTags.READONLY: - return new BReadonlyType(null, name, flags); + return new BReadonlyType(flags); case TypeTags.UNION: if (types.isCloneableType((BUnionType) refType)) { BUnionType cloneableType = @@ -325,7 +325,7 @@ private BType createTypeParamType(BSymbol symbol, BType type, Name name, long fl } private BAnydataType createAnydataType(BUnionType unionType, Name name, long flags) { - BAnydataType anydataType = new BAnydataType(unionType); + BAnydataType anydataType = new BAnydataType(types.typeCtx(), unionType); Optional immutableType = Types.getImmutableType(symTable, PackageID.ANNOTATIONS, unionType); immutableType.ifPresent(bIntersectionType -> @@ -338,7 +338,7 @@ private BAnydataType createAnydataType(BUnionType unionType, Name name, long fla private void addCyclicArrayMapTableOfMapMembers(BUnionType unionType) { BArrayType arrayCloneableType = new BArrayType(symTable.typeEnv(), unionType); BMapType mapCloneableType = new BMapType(symTable.typeEnv(), TypeTags.MAP, unionType, null); - BType tableMapCloneableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, mapCloneableType, null); + BType tableMapCloneableType = new BTableType(symTable.typeEnv(), mapCloneableType, null); unionType.add(arrayCloneableType); unionType.add(mapCloneableType); unionType.add(tableMapCloneableType); @@ -848,7 +848,7 @@ private BType getMatchingBoundType(BType expType, SymbolEnv env, HashSet return expTableType; } - BTableType tableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, tableConstraint, + BTableType tableType = new BTableType(symTable.typeEnv(), tableConstraint, symTable.tableType.tsymbol); if (expTableKeyTypeConstraint != null) { tableType.keyTypeConstraint = keyTypeConstraint; @@ -1032,7 +1032,7 @@ private BInvokableType getMatchingFunctionBoundType(BInvokableType expType, Symb return expType; } - BInvokableType invokableType = new BInvokableType(expType.env, paramTypes, restType, + BInvokableType invokableType = new BInvokableType(symTable.typeEnv(), paramTypes, restType, matchingBoundType, invokableTypeSymbol); invokableTypeSymbol.returnType = invokableType.retType; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeResolver.java index 9977c7280e95..9812114fd3fb 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeResolver.java @@ -783,7 +783,7 @@ private BType resolveFutureTypeDesc(BLangConstrainedType td, ResolverData data) SymbolEnv symEnv = data.env; BType type = resolveTypeDesc(symEnv, data.typeDefinition, data.depth + 1, td.type, data); - BFutureType constrainedType = new BFutureType(symTable.typeEnv(), TypeTags.FUTURE, symTable.empty, null); + BFutureType constrainedType = new BFutureType(symTable.typeEnv(), symTable.empty, null); BTypeSymbol typeSymbol = type.tsymbol; constrainedType.tsymbol = Symbols.createTypeSymbol(typeSymbol.tag, typeSymbol.flags, typeSymbol.name, typeSymbol.originalName, symEnv.enclPkg.symbol.pkgID, constrainedType, typeSymbol.owner, @@ -1679,7 +1679,7 @@ private BType resolveTypeDesc(BLangTableTypeNode td, ResolverData data) { SymbolEnv symEnv = data.env; BType type = resolveTypeDesc(symEnv, data.typeDefinition, data.depth + 1, td.type, data); - BTableType tableType = new BTableType(symTable.typeEnv(), TypeTags.TABLE, symTable.empty, null); + BTableType tableType = new BTableType(symTable.typeEnv(), symTable.empty, null); BTypeSymbol typeSymbol = type.tsymbol; tableType.tsymbol = Symbols.createTypeSymbol(SymTag.TYPE, Flags.asMask(EnumSet.noneOf(Flag.class)), typeSymbol.name, typeSymbol.originalName, symEnv.enclPkg.symbol.pkgID, diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index 2e61b0993502..308347df0d2b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -113,6 +113,7 @@ import org.wso2.ballerinalang.compiler.util.BArrayState; import org.wso2.ballerinalang.compiler.util.CompilerContext; import org.wso2.ballerinalang.compiler.util.ImmutableTypeCloner; +import org.wso2.ballerinalang.compiler.util.Name; import org.wso2.ballerinalang.compiler.util.Names; import org.wso2.ballerinalang.compiler.util.NumericLiteralSupport; import org.wso2.ballerinalang.compiler.util.TypeDefBuilderHelper; @@ -194,6 +195,8 @@ public class Types { private static final BigDecimal MIN_DECIMAL_MAGNITUDE = new BigDecimal("1.000000000000000000000000000000000e-6143", MathContext.DECIMAL128); + public static final String AND_READONLY_SUFFIX = " & readonly"; + public static Types getInstance(CompilerContext context) { Types types = context.get(TYPES_KEY); if (types == null) { @@ -218,16 +221,6 @@ public Types(CompilerContext context, Env typeEnv) { this.anonymousModelHelper = BLangAnonymousModelHelper.getInstance(context); } - public List checkTypes(BLangExpression node, - List actualTypes, - List expTypes) { - List resTypes = new ArrayList<>(); - for (int i = 0; i < actualTypes.size(); i++) { - resTypes.add(checkType(node, actualTypes.get(i), expTypes.size() > i ? expTypes.get(i) : symTable.noType)); - } - return resTypes; - } - public BType checkType(BLangExpression node, BType actualType, BType expType) { @@ -2770,7 +2763,7 @@ private BType createArrayAndTupleIntersection(IntersectionContext intersectionCo } if (tupleType.restType == null) { - return new BTupleType(tupleType.env, tupleMemberTypes); + return new BTupleType(typeEnv(), tupleMemberTypes); } BType restIntersectionType = getTypeIntersection(intersectionContext, tupleType.restType, eType, env, @@ -3213,16 +3206,16 @@ public BType getSafeType(BType bType, boolean liftNil, boolean liftError) { if (liftNil) { switch (type.tag) { case TypeTags.JSON: - return new BJSONType((BJSONType) type, false); + return BJSONType.newNilLiftedBJSONType((BJSONType) type); case TypeTags.ANY: - return BAnyType.newNilLiftedBAnyType(type.tsymbol); + return BAnyType.newNilLiftedBAnyType(); case TypeTags.ANYDATA: - return new BAnydataType((BAnydataType) type, false); + return BAnydataType.newNilLiftedBAnydataType((BAnydataType) type); case TypeTags.READONLY: if (liftError) { return symTable.anyAndReadonly; } - return BReadonlyType.newNilLiftedBReadonlyType(type.tsymbol); + return BReadonlyType.newNilLiftedBReadonlyType(); } } @@ -3891,6 +3884,14 @@ public static Optional getImmutableType(SymbolTable symTable, return Optional.empty(); } + public static Name getImmutableTypeName(String origName) { + if (origName.isEmpty()) { + return Names.EMPTY; + } + + return Names.fromString("(".concat(origName).concat(AND_READONLY_SUFFIX).concat(")")); + } + public static String getPackageIdString(PackageID packageID) { return packageID.isTestPkg ? packageID.toString() + "_testable" : packageID.toString(); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/SymbolTable.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/SymbolTable.java index b321df56e737..a1109b10a97c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/SymbolTable.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/SymbolTable.java @@ -122,7 +122,7 @@ public class SymbolTable { public final BType stringType = new BType(TypeTags.STRING, null, Flags.READONLY, PredefinedType.STRING); public final BType booleanType = new BType(TypeTags.BOOLEAN, null, Flags.READONLY, PredefinedType.BOOLEAN); - public final BType anyType = new BAnyType(null); + public final BType anyType = new BAnyType(); public final BMapType mapType; public final BMapType mapStringType; @@ -135,9 +135,9 @@ public class SymbolTable { public final BType tupleType; public final BType recordType; public final BType stringArrayType; - public final BType handleType = new BHandleType(TypeTags.HANDLE, null, PredefinedType.HANDLE); + public final BType handleType = new BHandleType(); public final BTypedescType typeDesc; - public final BType readonlyType = new BReadonlyType(null); + public final BType readonlyType = new BReadonlyType(); public final BType pathParamAllowedType; public final BIntersectionType anyAndReadonly; public BUnionType anyAndReadonlyOrError; @@ -159,24 +159,21 @@ public class SymbolTable { public BObjectType iterableType; // builtin subtypes - public final BIntSubType signed32IntType = new BIntSubType(TypeTags.SIGNED32_INT, Names.SIGNED32, SemTypes.SINT32); - public final BIntSubType signed16IntType = new BIntSubType(TypeTags.SIGNED16_INT, Names.SIGNED16, SemTypes.SINT16); - public final BIntSubType signed8IntType = new BIntSubType(TypeTags.SIGNED8_INT, Names.SIGNED8, SemTypes.SINT8); - public final BIntSubType unsigned32IntType = new BIntSubType(TypeTags.UNSIGNED32_INT, Names.UNSIGNED32, - SemTypes.UINT32); - public final BIntSubType unsigned16IntType = new BIntSubType(TypeTags.UNSIGNED16_INT, Names.UNSIGNED16, - SemTypes.UINT16); - public final BIntSubType unsigned8IntType = new BIntSubType(TypeTags.UNSIGNED8_INT, Names.UNSIGNED8, - SemTypes.UINT8); - public final BStringSubType charStringType = new BStringSubType(TypeTags.CHAR_STRING, Names.CHAR, SemTypes.CHAR); - public final BXMLSubType xmlElementType = new BXMLSubType(TypeTags.XML_ELEMENT, Names.XML_ELEMENT, - SemTypes.XML_ELEMENT); - public final BXMLSubType xmlPIType = new BXMLSubType(TypeTags.XML_PI, Names.XML_PI, SemTypes.XML_PI); - public final BXMLSubType xmlCommentType = new BXMLSubType(TypeTags.XML_COMMENT, Names.XML_COMMENT, - SemTypes.XML_COMMENT); - public final BXMLSubType xmlTextType = new BXMLSubType(TypeTags.XML_TEXT, Names.XML_TEXT, Flags.READONLY, - SemTypes.XML_TEXT); - public final BRegexpType regExpType = new BRegexpType(TypeTags.REGEXP, Names.REGEXP_TYPE, PredefinedType.REGEXP); + public final BIntSubType signed32IntType = BIntSubType.SIGNED32; + public final BIntSubType signed16IntType = BIntSubType.SIGNED16; + public final BIntSubType signed8IntType = BIntSubType.SIGNED8; + public final BIntSubType unsigned32IntType = BIntSubType.UNSIGNED32; + public final BIntSubType unsigned16IntType = BIntSubType.UNSIGNED16; + public final BIntSubType unsigned8IntType = BIntSubType.UNSIGNED8; + + public final BStringSubType charStringType = BStringSubType.CHAR; + + public final BXMLSubType xmlElementType = BXMLSubType.XML_ELEMENT; + public final BXMLSubType xmlPIType = BXMLSubType.XML_PI; + public final BXMLSubType xmlCommentType = BXMLSubType.XML_COMMENT; + public final BXMLSubType xmlTextType = BXMLSubType.XML_TEXT; + + public final BRegexpType regExpType = new BRegexpType(); public final BType xmlNeverType = new BXMLType(neverType, null); public final BType xmlType; @@ -310,18 +307,14 @@ private SymbolTable(CompilerContext context) { xmlType = new BXMLType(BUnionType.create(types.typeEnv(), null, xmlElementType, xmlCommentType, xmlPIType, xmlTextType), null); - futureType = new BFutureType(types.typeEnv(), TypeTags.FUTURE, nilType, null); - typeDesc = new BTypedescType(types.typeEnv(), this.anyType, null); + futureType = new BFutureType(types.typeEnv(), nilType, null, PredefinedType.FUTURE); + typeDesc = new BTypedescType(types.typeEnv(), this.anyType, null, PredefinedType.TYPEDESC); initializeType(xmlType, TypeKind.XML.typeName(), BUILTIN); initializeType(futureType, TypeKind.FUTURE.typeName(), BUILTIN); initializeType(typeDesc, TypeKind.TYPEDESC.typeName(), BUILTIN); defineCyclicUnionBasedInternalTypes(); - BTypeSymbol finiteTypeSymbol = Symbols.createTypeSymbol(SymTag.FINITE_TYPE, Flags.PUBLIC, - Names.fromString("$anonType$TRUE"), - rootPkgNode.packageID, null, rootPkgNode.symbol.owner, - this.builtinPos, VIRTUAL); BTypeSymbol trueFiniteTypeSymbol = Symbols.createTypeSymbol(SymTag.FINITE_TYPE, Flags.PUBLIC, Names.fromString("$anonType$TRUE"), rootPkgNode.packageID, null, rootPkgNode.symbol.owner, this.builtinPos, VIRTUAL); @@ -1164,7 +1157,7 @@ private void defineCloneableCyclicTypeAndDependentTypes() { pureType = BUnionType.create(typeEnv(), null, anydataType, errorType); streamType = new BStreamType(typeEnv(), TypeTags.STREAM, pureType, nilType, null); - tableType = new BTableType(typeEnv(), TypeTags.TABLE, pureType, null); + tableType = new BTableType(typeEnv(), pureType, null); initializeType(streamType, TypeKind.STREAM.typeName(), BUILTIN); initializeType(tableType, TypeKind.TABLE.typeName(), BUILTIN); @@ -1173,7 +1166,7 @@ private void defineCloneableCyclicTypeAndDependentTypes() { private void addCyclicArrayMapTableOfMapMembers(BUnionType unionType) { BArrayType arrayCloneableType = new BArrayType(typeEnv(), unionType); BMapType mapCloneableType = new BMapType(typeEnv(), TypeTags.MAP, unionType, null); - BType tableMapCloneableType = new BTableType(typeEnv(), TypeTags.TABLE, mapCloneableType, null); + BType tableMapCloneableType = new BTableType(typeEnv(), mapCloneableType, null); unionType.add(arrayCloneableType); unionType.add(mapCloneableType); unionType.add(tableMapCloneableType); @@ -1190,7 +1183,7 @@ private void defineJsonCyclicTypeAndDependentTypes() { jsonInternal.add(mapJsonTypeInternal); jsonInternal.isCyclic = true; - jsonType = new BJSONType(jsonInternal); + jsonType = new BJSONType(types.typeCtx(), jsonInternal); PackageID pkgID = rootPkgSymbol.pkgID; Optional immutableType = Types.getImmutableType(this, pkgID, jsonInternal); if (immutableType.isPresent()) { @@ -1209,7 +1202,7 @@ private void defineAnydataCyclicTypeAndDependentTypes() { stringType, xmlType); addCyclicArrayMapTableOfMapMembers(anyDataInternal); - anydataType = new BAnydataType(anyDataInternal); + anydataType = new BAnydataType(types.typeCtx(), anyDataInternal); PackageID pkgID = rootPkgSymbol.pkgID; Optional immutableType = Types.getImmutableType(this, pkgID, anyDataInternal); if (immutableType.isPresent()) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/TypeVisitor.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/TypeVisitor.java index 6085bc8c02da..9cf5163e54c2 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/TypeVisitor.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/TypeVisitor.java @@ -22,7 +22,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.BFiniteType; import org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType; @@ -59,8 +58,6 @@ public abstract class TypeVisitor { public abstract void visit(BArrayType bArrayType); - public abstract void visit(BBuiltInRefType bBuiltInRefType); - public abstract void visit(BAnyType bAnyType); public abstract void visit(BAnydataType bAnydataType); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/UniqueTypeVisitor.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/UniqueTypeVisitor.java index 1d3830023e3b..273ca80f2dc6 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/UniqueTypeVisitor.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/UniqueTypeVisitor.java @@ -22,7 +22,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.BFiniteType; import org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType; @@ -37,6 +36,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.types.BObjectType; import org.wso2.ballerinalang.compiler.semantics.model.types.BPackageType; import org.wso2.ballerinalang.compiler.semantics.model.types.BParameterizedType; +import org.wso2.ballerinalang.compiler.semantics.model.types.BReadonlyType; import org.wso2.ballerinalang.compiler.semantics.model.types.BRecordType; import org.wso2.ballerinalang.compiler.semantics.model.types.BStreamType; import org.wso2.ballerinalang.compiler.semantics.model.types.BStructureType; @@ -69,7 +69,7 @@ R visit(UniqueTypeVisitor visitor) { public abstract R visit(BArrayType bArrayType); - public abstract R visit(BBuiltInRefType bBuiltInRefType); + public abstract R visit(BReadonlyType bReadonlyType); public abstract R visit(BAnyType bAnyType); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnyType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnyType.java index 97e2ec3a59a5..ff9a6f4efe09 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnyType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnyType.java @@ -23,8 +23,8 @@ import org.ballerinalang.model.Name; import org.ballerinalang.model.types.SelectivelyImmutableReferenceType; import org.ballerinalang.model.types.TypeKind; +import org.wso2.ballerinalang.compiler.semantics.analyzer.Types; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; -import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; @@ -35,50 +35,36 @@ /** * @since 0.94 */ -public class BAnyType extends BBuiltInRefType implements SelectivelyImmutableReferenceType { +public class BAnyType extends BType implements SelectivelyImmutableReferenceType { private boolean nullable = true; - private boolean isNilLifted = false; - public BAnyType(BTypeSymbol tsymbol) { - this(tsymbol, ANY); + public BAnyType() { + this(ANY); } - public BAnyType(int tag, BTypeSymbol tsymbol, boolean nullable) { - super(tag, tsymbol, ANY); - this.nullable = nullable; + public BAnyType(Name name, long flag) { + this(name, flag, Symbols.isFlagOn(flag, Flags.READONLY) ? Core.intersect(ANY, VAL_READONLY) : ANY); } - public BAnyType(int tag, BTypeSymbol tsymbol, Name name, long flags, boolean nullable) { - super(tag, tsymbol, flags, ANY); + private BAnyType(Name name, long flags, SemType semType) { + super(TypeTags.ANY, null, semType); this.name = name; - this.nullable = nullable; - } - - public BAnyType(int tag, BTypeSymbol tsymbol, Name name, long flags, SemType semType) { - super(tag, tsymbol, flags, ANY); - this.name = name; - this.semType = semType; - } - - private BAnyType(BTypeSymbol tsymbol, SemType semType) { - super(TypeTags.ANY, tsymbol, semType); + this.setFlags(flags); } - public BAnyType(BTypeSymbol tsymbol, Name name, long flag) { - this(tsymbol, name, flag, ANY); + private BAnyType(SemType semType) { + super(TypeTags.ANY, null, semType); } - public BAnyType(BTypeSymbol tsymbol, Name name, long flags, SemType semType) { - super(TypeTags.ANY, tsymbol, semType); - this.name = name; - this.setFlags(flags); + public static BAnyType newNilLiftedBAnyType() { + BAnyType result = new BAnyType(Core.diff(ANY, PredefinedType.NIL)); + result.nullable = false; + return result; } - public static BAnyType newNilLiftedBAnyType(BTypeSymbol tsymbol) { - BAnyType result = new BAnyType(tsymbol, Core.diff(ANY, PredefinedType.NIL)); - result.isNilLifted = true; - return result; + public static BAnyType newImmutableBAnyType() { + return new BAnyType(Types.getImmutableTypeName(TypeKind.ANY.typeName()), Flags.READONLY); } @Override @@ -106,17 +92,4 @@ public String toString() { return !Symbols.isFlagOn(getFlags(), Flags.READONLY) ? getKind().typeName() : getKind().typeName().concat(" & readonly"); } - - @Override - public SemType semType() { - SemType semType = ANY; - if (Symbols.isFlagOn(getFlags(), Flags.READONLY)) { - semType = Core.intersect(semType, VAL_READONLY); - } - - if (isNilLifted) { - semType = Core.diff(semType, PredefinedType.NIL); - } - return semType; - } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnydataType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnydataType.java index cd99b6bc055d..4c2c3b9b08ca 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnydataType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnydataType.java @@ -19,7 +19,7 @@ import io.ballerina.types.Context; import io.ballerina.types.Core; -import io.ballerina.types.Env; +import io.ballerina.types.PredefinedType; import io.ballerina.types.SemType; import org.ballerinalang.model.Name; import org.ballerinalang.model.types.TypeKind; @@ -31,8 +31,6 @@ import java.util.LinkedHashSet; -import static io.ballerina.types.PredefinedType.VAL_READONLY; - /** * {@code BAnydataType} represents the data types in Ballerina. * @@ -41,17 +39,19 @@ public class BAnydataType extends BUnionType { private boolean nullable; private static final int INITIAL_CAPACITY = 10; + private final Context typeCtx; - public BAnydataType(Env env, BTypeSymbol tsymbol, Name name, long flags, boolean nullable) { - super(env, tsymbol, new LinkedHashSet<>(INITIAL_CAPACITY), false); + private BAnydataType(Context typeCtx, BTypeSymbol tsymbol, Name name, long flags, boolean nullable) { + super(typeCtx.env, tsymbol, new LinkedHashSet<>(INITIAL_CAPACITY), false); this.tag = TypeTags.ANYDATA; this.setFlags(flags); this.name = name; this.isCyclic = true; this.nullable = nullable; + this.typeCtx = typeCtx; } - public BAnydataType(BUnionType type) { + public BAnydataType(Context typeCtx, BUnionType type) { super(type.env, type.tsymbol, new LinkedHashSet<>(type.memberTypes.size()), Symbols.isFlagOn(type.getFlags(), Flags.READONLY)); this.tag = TypeTags.ANYDATA; @@ -60,16 +60,18 @@ public BAnydataType(BUnionType type) { this.setFlags(type.getFlags()); this.nullable = type.isNullable(); mergeUnionType(type); + this.typeCtx = typeCtx; } - public BAnydataType(BAnydataType type, boolean nullable) { - super(type.env, type.tsymbol, new LinkedHashSet<>(INITIAL_CAPACITY), - Symbols.isFlagOn(type.getFlags(), Flags.READONLY)); - this.setFlags(type.getFlags()); - this.tag = TypeTags.ANYDATA; - this.isCyclic = true; - this.nullable = nullable; - mergeUnionType(type); + public static BAnydataType newNilLiftedBAnydataType(BAnydataType type) { + BAnydataType result = new BAnydataType(type.typeCtx, type); + result.nullable = false; + return result; + } + + public static BAnydataType newImmutableBAnydataType(BAnydataType type, BTypeSymbol typeSymbol, Name name, + boolean nullable) { + return new BAnydataType(type.typeCtx, typeSymbol, name, type.getFlags() | Flags.READONLY, nullable); } @Override @@ -100,10 +102,13 @@ public R accept(BTypeVisitor visitor, T t) { @Override public SemType semType() { - SemType s = Core.createAnydata(Context.from(env)); + SemType anydata = Core.createAnydata(typeCtx); + if (!nullable) { + anydata = Core.diff(anydata, PredefinedType.NIL); + } if (Symbols.isFlagOn(getFlags(), Flags.READONLY)) { - return Core.intersect(s, VAL_READONLY); + anydata = Core.intersect(anydata, PredefinedType.VAL_READONLY); } - return s; + return anydata; } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BArrayType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BArrayType.java index 9e432d722ed3..855dfe0c1919 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BArrayType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BArrayType.java @@ -50,7 +50,7 @@ public class BArrayType extends BType implements ArrayType { public BArrayState state = BArrayState.OPEN; public BArrayType mutableType; - public final Env env; + private final Env env; private ListDefinition ld = null; public BArrayType(Env env, BType elementType) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BBuiltInRefType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BBuiltInRefType.java deleted file mode 100644 index 2d9aab05ca17..000000000000 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BBuiltInRefType.java +++ /dev/null @@ -1,76 +0,0 @@ -/* -* Copyright (c) 2017, 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 -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ -package org.wso2.ballerinalang.compiler.semantics.model.types; - -import io.ballerina.types.SemType; -import org.ballerinalang.model.types.ReferenceType; -import org.ballerinalang.model.types.TypeKind; -import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; - -import static org.wso2.ballerinalang.compiler.util.TypeTags.ANY; -import static org.wso2.ballerinalang.compiler.util.TypeTags.ANYDATA; -import static org.wso2.ballerinalang.compiler.util.TypeTags.FUTURE; -import static org.wso2.ballerinalang.compiler.util.TypeTags.JSON; -import static org.wso2.ballerinalang.compiler.util.TypeTags.MAP; -import static org.wso2.ballerinalang.compiler.util.TypeTags.STREAM; -import static org.wso2.ballerinalang.compiler.util.TypeTags.TABLE; -import static org.wso2.ballerinalang.compiler.util.TypeTags.TYPEDESC; -import static org.wso2.ballerinalang.compiler.util.TypeTags.XML; - -/** - * @since 0.94 - */ -public class BBuiltInRefType extends BType implements ReferenceType { - - public BBuiltInRefType(int tag, BTypeSymbol tsymbol) { - super(tag, tsymbol); - } - - public BBuiltInRefType(int tag, BTypeSymbol tsymbol, SemType semType) { - super(tag, tsymbol, semType); - } - - public BBuiltInRefType(int tag, BTypeSymbol tsymbol, long flags) { - super(tag, tsymbol, flags); - } - - public BBuiltInRefType(int tag, BTypeSymbol tsymbol, long flags, SemType semType) { - super(tag, tsymbol, flags, semType); - } - - @Override - public R accept(BTypeVisitor visitor, T t) { - return visitor.visit(this, t); - } - - @Override - public TypeKind getKind() { - return switch (tag) { - case JSON -> TypeKind.JSON; - case XML -> TypeKind.XML; - case STREAM -> TypeKind.STREAM; - case TABLE -> TypeKind.TABLE; - case ANY -> TypeKind.ANY; - case ANYDATA -> TypeKind.ANYDATA; - case MAP -> TypeKind.MAP; - case FUTURE -> TypeKind.FUTURE; - case TYPEDESC -> TypeKind.TYPEDESC; - default -> TypeKind.OTHER; - }; - } -} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BErrorType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BErrorType.java index 03d1a8f06531..972299acb5db 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BErrorType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BErrorType.java @@ -49,7 +49,7 @@ public class BErrorType extends BType implements ErrorType { private static final String ERROR = "error<"; private static final String CLOSE_ERROR = ">"; - public final Env env; + private final Env env; public int distinctId = -1; private final DistinctIdSupplier distinctIdSupplier; @@ -108,13 +108,18 @@ SemType distinctIdWrapper(SemType semTypeInner) { } private SemType semTypeInner() { + if (this.semType != null) { + return this.semType; + } + if (detailType == null || detailType.semType() == null) { // semtype will be null for semantic error - return PredefinedType.ERROR; + this.semType = PredefinedType.ERROR; } else { SemType detail = detailType.semType(); - return SemTypes.errorDetail(detail); + this.semType = SemTypes.errorDetail(detail); } + return this.semType; } private final class DistinctIdSupplier implements Supplier> { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BFiniteType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BFiniteType.java index 577197f6a1ff..100134cd1fec 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BFiniteType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BFiniteType.java @@ -27,7 +27,6 @@ import io.ballerina.types.subtypedata.FloatSubtype; import io.ballerina.types.subtypedata.IntSubtype; import io.ballerina.types.subtypedata.StringSubtype; -import org.ballerinalang.model.types.ReferenceType; import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; @@ -51,13 +50,12 @@ * {@code BFiniteType} represents the finite type in Ballerina. * */ -public class BFiniteType extends BType implements ReferenceType { +public class BFiniteType extends BType { public SemNamedType[] valueSpace; public BFiniteType(BTypeSymbol tsymbol, SemNamedType[] valueSpace) { - super(TypeTags.FINITE, tsymbol); - this.addFlags(Flags.READONLY); + super(TypeTags.FINITE, tsymbol, Flags.READONLY); this.valueSpace = valueSpace; assert validValueSpace(valueSpace); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BFutureType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BFutureType.java index 77659e810032..33065fa3c05b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BFutureType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BFutureType.java @@ -21,6 +21,7 @@ import io.ballerina.types.SemType; import io.ballerina.types.SemTypes; import org.ballerinalang.model.types.ConstrainedType; +import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; import org.wso2.ballerinalang.compiler.util.TypeTags; @@ -30,33 +31,48 @@ * * @since 0.965.0 */ -public class BFutureType extends BBuiltInRefType implements ConstrainedType { +public class BFutureType extends BType implements ConstrainedType { public BType constraint; public boolean workerDerivative; - public final Env env; + private final Env env; - public BFutureType(Env env, int tag, BType constraint, BTypeSymbol tsymbol) { - super(tag, tsymbol); + public BFutureType(Env env, BType constraint, BTypeSymbol tsymbol) { + super(TypeTags.FUTURE, tsymbol); this.constraint = constraint; this.env = env; } - public BFutureType(Env env, int tag, BType constraint, BTypeSymbol tsymbol, boolean workerDerivative) { - this(env, tag, constraint, tsymbol); + public BFutureType(Env env, BType constraint, BTypeSymbol tsymbol, boolean workerDerivative) { + this(env, constraint, tsymbol); this.workerDerivative = workerDerivative; } + public BFutureType(Env env, BType constraint, BTypeSymbol tsymbol, SemType semType) { + this(env, constraint, tsymbol); + this.semType = semType; + } + @Override public BType getConstraint() { return constraint; } + public void setConstraint(BType constraint) { + this.constraint = constraint; + this.semType = null; + } + @Override public R accept(BTypeVisitor visitor, T t) { return visitor.visit(this, t); } - + + @Override + public TypeKind getKind() { + return TypeKind.FUTURE; + } + @Override public String toString() { if (constraint.tag == TypeTags.NONE || constraint.tag == TypeTags.SEMANTIC_ERROR @@ -74,10 +90,15 @@ public void accept(TypeVisitor visitor) { @Override public SemType semType() { - if (constraint == null || constraint instanceof BNoType) { - return PredefinedType.FUTURE; + if (this.semType != null) { + return this.semType; } - return SemTypes.futureContaining(env, constraint.semType()); + if (constraint == null || constraint instanceof BNoType) { + this.semType = PredefinedType.FUTURE; + } else { + this.semType = SemTypes.futureContaining(env, constraint.semType()); + } + return this.semType; } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BHandleType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BHandleType.java index 1109631f7007..dcc4540cb8e9 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BHandleType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BHandleType.java @@ -17,10 +17,10 @@ */ package org.wso2.ballerinalang.compiler.semantics.model.types; -import io.ballerina.types.SemType; +import io.ballerina.types.PredefinedType; import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; -import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; +import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; /** @@ -30,10 +30,10 @@ * * @since 1.0.0 */ -public class BHandleType extends BBuiltInRefType { +public class BHandleType extends BType { - public BHandleType(int tag, BTypeSymbol tsymbol, SemType semType) { - super(tag, tsymbol, Flags.READONLY, semType); + public BHandleType() { + super(TypeTags.HANDLE, null, Flags.READONLY, PredefinedType.HANDLE); } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BIntSubType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BIntSubType.java index c9949e1902d2..675fa600ccc2 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BIntSubType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BIntSubType.java @@ -18,10 +18,11 @@ package org.wso2.ballerinalang.compiler.semantics.model.types; import io.ballerina.types.SemType; +import io.ballerina.types.SemTypes; import org.ballerinalang.model.Name; import org.ballerinalang.model.types.TypeKind; -import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.util.Names; +import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; /** @@ -31,58 +32,44 @@ */ public class BIntSubType extends BType { - public BIntSubType(int tag, Name name) { - this(tag, name, null); - } + public static final BIntSubType SIGNED32 = new BIntSubType(TypeTags.SIGNED32_INT, Names.SIGNED32, SemTypes.SINT32); + public static final BIntSubType SIGNED16 = new BIntSubType(TypeTags.SIGNED16_INT, Names.SIGNED16, SemTypes.SINT16); + public static final BIntSubType SIGNED8 = new BIntSubType(TypeTags.SIGNED8_INT, Names.SIGNED8, SemTypes.SINT8); + + public static final BIntSubType UNSIGNED32 = new BIntSubType(TypeTags.UNSIGNED32_INT, Names.UNSIGNED32, + SemTypes.UINT32); + public static final BIntSubType UNSIGNED16 = new BIntSubType(TypeTags.UNSIGNED16_INT, Names.UNSIGNED16, + SemTypes.UINT16); + public static final BIntSubType UNSIGNED8 = new BIntSubType(TypeTags.UNSIGNED8_INT, Names.UNSIGNED8, + SemTypes.UINT8); - public BIntSubType(int tag, Name name, SemType semType) { + private BIntSubType(int tag, Name name, SemType semType) { super(tag, null, name, Flags.READONLY, semType); } @Override public boolean isNullable() { - return false; } @Override public R accept(BTypeVisitor visitor, T t) { - return visitor.visit(this, t); } @Override public TypeKind getKind() { - return TypeKind.INT; } - @Override - public void accept(TypeVisitor visitor) { - - visitor.visit(this); - } - @Override public String toString() { - return Names.INT.value + Names.ALIAS_SEPARATOR + name; } @Override public String getQualifiedTypeName() { - return Names.BALLERINA_ORG.value + Names.ORG_NAME_SEPARATOR.value + Names.LANG.value + Names.DOT.value + Names.INT.value + Names.ALIAS_SEPARATOR + name; } - - public boolean isAnydata() { - - return true; - } - - public boolean isPureType() { - - return true; - } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BInvokableType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BInvokableType.java index 37f600c2831d..86d23257ed77 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BInvokableType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BInvokableType.java @@ -49,7 +49,7 @@ public class BInvokableType extends BType implements InvokableType { // TODO: make these final public BType restType; public BType retType; - public final Env env; + private final Env env; private FunctionDefinition defn; public BInvokableType(Env env, List paramTypes, BType restType, BType retType, BTypeSymbol tsymbol) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BJSONType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BJSONType.java index 027c7cfec1f6..78f81599f2e3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BJSONType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BJSONType.java @@ -19,7 +19,7 @@ import io.ballerina.types.Context; import io.ballerina.types.Core; -import io.ballerina.types.Env; +import io.ballerina.types.PredefinedType; import io.ballerina.types.SemType; import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; @@ -30,38 +30,40 @@ import java.util.LinkedHashSet; -import static io.ballerina.types.PredefinedType.VAL_READONLY; - /** * @since 0.94 */ public class BJSONType extends BUnionType { - private boolean nullable; + private boolean nullable = true; private static final int INITIAL_CAPACITY = 8; + private final Context typeCtx; - public BJSONType(BJSONType type, boolean nullable) { + public BJSONType(Context typeCtx, BUnionType type) { super(type.env, type.tsymbol, new LinkedHashSet<>(INITIAL_CAPACITY), Symbols.isFlagOn(type.getFlags(), Flags.READONLY)); mergeUnionType(type); this.tag = TypeTags.JSON; this.isCyclic = true; - this.nullable = nullable; - } - - public BJSONType(BUnionType type) { - super(type.env, type.tsymbol, new LinkedHashSet<>(INITIAL_CAPACITY), - Symbols.isFlagOn(type.getFlags(), Flags.READONLY)); - mergeUnionType(type); - this.tag = TypeTags.JSON; - this.nullable = true; + this.typeCtx = typeCtx; } - public BJSONType(Env env, BTypeSymbol typeSymbol, boolean nullable, long flags) { - super(env, typeSymbol, new LinkedHashSet<>(INITIAL_CAPACITY), Symbols.isFlagOn(flags, Flags.READONLY)); + private BJSONType(Context typeCtx, BTypeSymbol typeSymbol, boolean nullable, long flags) { + super(typeCtx.env, typeSymbol, new LinkedHashSet<>(INITIAL_CAPACITY), Symbols.isFlagOn(flags, Flags.READONLY)); this.setFlags(flags); this.tag = TypeTags.JSON; this.isCyclic = true; this.nullable = nullable; + this.typeCtx = typeCtx; + } + + public static BJSONType newNilLiftedBJSONType(BJSONType type) { + BJSONType result = new BJSONType(type.typeCtx, type); + result.nullable = false; + return result; + } + + public static BJSONType newImmutableBJSONType(BJSONType type, BTypeSymbol typeSymbol, boolean nullable) { + return new BJSONType(type.typeCtx, typeSymbol, nullable, type.getFlags() | Flags.READONLY); } @Override @@ -92,10 +94,14 @@ public R accept(BTypeVisitor visitor, T t) { @Override public SemType semType() { - SemType s = Core.createJson(Context.from(env)); + SemType json = Core.createJson(typeCtx); + // TODO: refer to https://github.com/ballerina-platform/ballerina-lang/issues/43343#issuecomment-2485247172 +// if (!nullable) { +// json = Core.diff(json, PredefinedType.NIL); +// } if (Symbols.isFlagOn(getFlags(), Flags.READONLY)) { - return Core.intersect(s, VAL_READONLY); + json = Core.intersect(json, PredefinedType.VAL_READONLY); } - return s; + return json; } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BMapType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BMapType.java index eca371bf534d..6efc4a020d33 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BMapType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BMapType.java @@ -23,6 +23,7 @@ import io.ballerina.types.definition.MappingDefinition; import org.ballerinalang.model.types.ConstrainedType; import org.ballerinalang.model.types.SelectivelyImmutableReferenceType; +import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; @@ -39,11 +40,11 @@ /** * @since 0.94 */ -public class BMapType extends BBuiltInRefType implements ConstrainedType, SelectivelyImmutableReferenceType { +public class BMapType extends BType implements ConstrainedType, SelectivelyImmutableReferenceType { public BType constraint; public BMapType mutableType; - public final Env env; + private final Env env; private MappingDefinition md = null; public BMapType(Env env, int tag, BType constraint, BTypeSymbol tsymbol) { @@ -76,6 +77,11 @@ public R accept(BTypeVisitor visitor, T t) { return visitor.visit(this, t); } + @Override + public TypeKind getKind() { + return TypeKind.MAP; + } + @Override public String toString() { String stringRep; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BNilType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BNilType.java index 6822a51bb97d..0b821361a5c9 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BNilType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BNilType.java @@ -18,7 +18,6 @@ package org.wso2.ballerinalang.compiler.semantics.model.types; import io.ballerina.types.PredefinedType; -import org.ballerinalang.model.types.NullType; import org.wso2.ballerinalang.compiler.util.Names; import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; @@ -29,7 +28,7 @@ * * @since 0.970.0 */ -public class BNilType extends BType implements NullType { +public class BNilType extends BType { BNilType() { super(TypeTags.NIL, null, Flags.READONLY, PredefinedType.NIL); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BNoType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BNoType.java index 71f8677075fc..f3a95d660c68 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BNoType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BNoType.java @@ -18,7 +18,6 @@ package org.wso2.ballerinalang.compiler.semantics.model.types; import io.ballerina.types.PredefinedType; -import io.ballerina.types.SemType; import org.ballerinalang.model.types.NoType; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; @@ -28,16 +27,11 @@ public class BNoType extends BType implements NoType { public BNoType(int tag) { - super(tag, null); + super(tag, null, PredefinedType.UNDEF); } @Override public void accept(TypeVisitor visitor) { visitor.visit(this); } - - @Override - public SemType semType() { - return PredefinedType.UNDEF; - } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BObjectType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BObjectType.java index 0c926259eba8..59ac8e697a79 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BObjectType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BObjectType.java @@ -61,7 +61,7 @@ public class BObjectType extends BStructureType implements ObjectType { private static final String RIGHT_CURL = "}"; private static final String SEMI_COLON = ";"; private static final String READONLY = "readonly"; - public final Env env; + private final Env env; public boolean markedIsolatedness; public BObjectType mutableType = null; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BReadonlyType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BReadonlyType.java index 4eaa940ecac1..eab400866809 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BReadonlyType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BReadonlyType.java @@ -20,9 +20,7 @@ import io.ballerina.types.Core; import io.ballerina.types.PredefinedType; import io.ballerina.types.SemType; -import org.ballerinalang.model.Name; import org.ballerinalang.model.types.TypeKind; -import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; @@ -33,34 +31,26 @@ * * @since 1.3.0 */ -public class BReadonlyType extends BBuiltInRefType { +public class BReadonlyType extends BType { private boolean nullable = true; - public BReadonlyType(BTypeSymbol tsymbol) { - this(tsymbol, VAL_READONLY); + public BReadonlyType() { + this(VAL_READONLY); } - private BReadonlyType(BTypeSymbol tsymbol, SemType semType) { - super(TypeTags.READONLY, tsymbol, semType); - this.addFlags(Flags.READONLY); + public BReadonlyType(long flag) { + super(TypeTags.READONLY, null, flag | Flags.READONLY, VAL_READONLY); } - public BReadonlyType(BTypeSymbol tsymbol, Name name, long flag) { - super(TypeTags.READONLY, tsymbol, VAL_READONLY); - this.name = name; - this.setFlags(flag); - this.addFlags(Flags.READONLY); + private BReadonlyType(SemType semType) { + super(TypeTags.READONLY, null, Flags.READONLY, semType); } - public BReadonlyType(int tag, BTypeSymbol tsymbol, boolean nullable) { - super(tag, tsymbol); - this.nullable = nullable; - this.addFlags(Flags.READONLY); - } - - public static BReadonlyType newNilLiftedBReadonlyType(BTypeSymbol tsymbol) { - return new BReadonlyType(tsymbol, Core.diff(VAL_READONLY, PredefinedType.NIL)); + public static BReadonlyType newNilLiftedBReadonlyType() { + BReadonlyType result = new BReadonlyType(Core.diff(VAL_READONLY, PredefinedType.NIL)); + result.nullable = false; + return result; } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BRecordType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BRecordType.java index d88c2f7f01bc..95200e5c6a57 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BRecordType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BRecordType.java @@ -59,7 +59,7 @@ public class BRecordType extends BStructureType implements RecordType { public BRecordType mutableType; - public final Env env; + private final Env env; private MappingDefinition md = null; public BRecordType(Env env, BTypeSymbol tSymbol) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BRegexpType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BRegexpType.java index feb616d03020..16f005bc8d06 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BRegexpType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BRegexpType.java @@ -17,11 +17,11 @@ */ package org.wso2.ballerinalang.compiler.semantics.model.types; -import io.ballerina.types.SemType; -import org.ballerinalang.model.Name; +import io.ballerina.types.PredefinedType; import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.util.Names; +import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; /** @@ -31,8 +31,8 @@ */ public class BRegexpType extends BType { - public BRegexpType(int tag, Name name, SemType semType) { - super(tag, null, name, Flags.READONLY, semType); + public BRegexpType() { + super(TypeTags.REGEXP, null, Names.REGEXP_TYPE, Flags.READONLY, PredefinedType.REGEXP); } @Override @@ -60,12 +60,4 @@ public String getQualifiedTypeName() { return Names.BALLERINA_ORG.value + Names.ORG_NAME_SEPARATOR.value + Names.LANG.value + Names.DOT.value + Names.REGEXP.value + Names.ALIAS_SEPARATOR + name; } - - public boolean isAnydata() { - return true; - } - - public boolean isPureType() { - return true; - } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BSequenceType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BSequenceType.java index 421f3723c5a1..571887ad0c55 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BSequenceType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BSequenceType.java @@ -19,8 +19,13 @@ import io.ballerina.types.Env; import io.ballerina.types.SemType; +import io.ballerina.types.definition.ListDefinition; import org.wso2.ballerinalang.compiler.util.TypeTags; +import java.util.List; + +import static io.ballerina.types.CellAtomicType.CellMutability.CELL_MUT_LIMITED; + /** * Represents type for sequence variable. * @@ -28,7 +33,7 @@ */ public class BSequenceType extends BType { public BType elementType; - public final Env env; + private final Env env; public BSequenceType(Env env, BType elementType) { super(TypeTags.SEQUENCE, null); @@ -42,6 +47,11 @@ public String toString() { } public SemType semType() { - return new BArrayType(env, elementType).semType(); + if (this.semType != null) { + return this.semType; + } + ListDefinition ld = new ListDefinition(); + this.semType = ld.defineListTypeWrapped(env, List.of(), 0, elementType.semType(), CELL_MUT_LIMITED); + return this.semType; } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BStreamType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BStreamType.java index 287c19a3783b..5f218cf9a3d6 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BStreamType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BStreamType.java @@ -24,6 +24,7 @@ import io.ballerina.types.definition.StreamDefinition; import org.ballerinalang.model.types.StreamType; import org.ballerinalang.model.types.Type; +import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; import org.wso2.ballerinalang.compiler.util.TypeTags; @@ -33,12 +34,12 @@ * * @since 1.2.0 */ -public class BStreamType extends BBuiltInRefType implements StreamType { +public class BStreamType extends BType implements StreamType { public BType constraint; public BType completionType; - public final Env env; + private final Env env; private StreamDefinition d = null; public BStreamType(Env env, int tag, BType constraint, BType completionType, BTypeSymbol tsymbol) { @@ -63,6 +64,11 @@ public R accept(BTypeVisitor visitor, T t) { return visitor.visit(this, t); } + @Override + public TypeKind getKind() { + return TypeKind.STREAM; + } + @Override public String toString() { if (constraint.tag == TypeTags.ANY) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BStringSubType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BStringSubType.java index 3ad10fec2c6d..af7beacd9ec3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BStringSubType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BStringSubType.java @@ -17,11 +17,11 @@ */ package org.wso2.ballerinalang.compiler.semantics.model.types; -import io.ballerina.types.SemType; -import org.ballerinalang.model.Name; +import io.ballerina.types.SemTypes; import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.util.Names; +import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; /** @@ -31,58 +31,40 @@ */ public class BStringSubType extends BType { - public BStringSubType(int tag, Name name) { - this(tag, name, null); - } + public static final BStringSubType CHAR = new BStringSubType(); - public BStringSubType(int tag, Name name, SemType semType) { - super(tag, null, name, Flags.READONLY, semType); + private BStringSubType() { + super(TypeTags.CHAR_STRING, null, Names.CHAR, Flags.READONLY, SemTypes.CHAR); } @Override public boolean isNullable() { - return false; } @Override public R accept(BTypeVisitor visitor, T t) { - return visitor.visit(this, t); } @Override public TypeKind getKind() { - return TypeKind.STRING; } @Override public void accept(TypeVisitor visitor) { - visitor.visit(this); } @Override public String toString() { - return Names.STRING.value + Names.ALIAS_SEPARATOR + name; } @Override public String getQualifiedTypeName() { - return Names.BALLERINA_ORG.value + Names.ORG_NAME_SEPARATOR.value + Names.LANG.value + Names.DOT.value + Names.STRING.value + Names.ALIAS_SEPARATOR + name; } - - public boolean isAnydata() { - - return true; - } - - public boolean isPureType() { - - return true; - } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTableType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTableType.java index f6c9031b56b9..f5bb090449b5 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTableType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTableType.java @@ -49,16 +49,16 @@ public class BTableType extends BType implements TableType { public Location constraintPos; public BTableType mutableType; - public final Env env; + private final Env env; - public BTableType(Env env, int tag, BType constraint, BTypeSymbol tSymbol) { - super(tag, tSymbol); + public BTableType(Env env, BType constraint, BTypeSymbol tSymbol) { + super(TypeTags.TABLE, tSymbol); this.constraint = constraint; this.env = env; } - public BTableType(Env env, int tag, BType constraint, BTypeSymbol tSymbol, long flags) { - super(tag, tSymbol, flags); + public BTableType(Env env, BType constraint, BTypeSymbol tSymbol, long flags) { + super(TypeTags.TABLE, tSymbol, flags); this.constraint = constraint; this.env = env; } @@ -67,6 +67,11 @@ public BType getConstraint() { return this.constraint; } + public void setConstraint(BType constraint) { + this.constraint = constraint; + this.semType = null; + } + @Override public R accept(BTypeVisitor visitor, T t) { return visitor.visit(this, t); @@ -109,9 +114,14 @@ public void accept(TypeVisitor visitor) { @Override public SemType semType() { - boolean readonly = Symbols.isFlagOn(this.getFlags(), Flags.READONLY); + if (this.semType != null) { + return this.semType; + } + SemType s = semTypeInner(); - return readonly ? SemTypes.intersect(PredefinedType.VAL_READONLY, s) : s; + boolean readonly = Symbols.isFlagOn(this.getFlags(), Flags.READONLY); + this.semType = readonly ? SemTypes.intersect(PredefinedType.VAL_READONLY, s) : s; + return this.semType; } private SemType semTypeInner() { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTupleType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTupleType.java index 9117910b893d..54c93ad0f200 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTupleType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTupleType.java @@ -51,7 +51,7 @@ public class BTupleType extends BType implements TupleType { public boolean isCyclic = false; public BTupleType mutableType; - public final Env env; + private final Env env; private ListDefinition ld = null; public BTupleType(Env env, List members) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypeReferenceType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypeReferenceType.java index 68cc35da1b83..91edd117538c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypeReferenceType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypeReferenceType.java @@ -18,7 +18,6 @@ package org.wso2.ballerinalang.compiler.semantics.model.types; import io.ballerina.types.SemType; -import org.ballerinalang.model.types.ReferenceType; import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; @@ -28,7 +27,7 @@ /** * @since 2.0.0 */ -public class BTypeReferenceType extends BType implements ReferenceType { +public class BTypeReferenceType extends BType { public BType referredType; public final String definitionName; @@ -64,5 +63,4 @@ public void accept(TypeVisitor visitor) { public TypeKind getKind() { return TypeKind.TYPEREFDESC; } - } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypeVisitor.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypeVisitor.java index fa8d92c78151..ac742e46f36d 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypeVisitor.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypeVisitor.java @@ -25,8 +25,6 @@ public interface BTypeVisitor { R visit(BType t, T s); - R visit(BBuiltInRefType t, T s); - R visit(BAnyType t, T s); R visit(BAnydataType t, T s); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypedescType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypedescType.java index df9b8787bc04..5c92251bfe7b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypedescType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BTypedescType.java @@ -22,6 +22,7 @@ import io.ballerina.types.SemType; import io.ballerina.types.SemTypes; import org.ballerinalang.model.types.ConstrainedType; +import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; import org.wso2.ballerinalang.compiler.util.TypeTags; @@ -30,10 +31,10 @@ /** * @since 0.94 */ -public class BTypedescType extends BBuiltInRefType implements ConstrainedType { +public class BTypedescType extends BType implements ConstrainedType { public BType constraint; - public final Env env; + private final Env env; public BTypedescType(Env env, BType constraint, BTypeSymbol tsymbol) { super(TypeTags.TYPEDESC, tsymbol, Flags.READONLY); @@ -41,21 +42,28 @@ public BTypedescType(Env env, BType constraint, BTypeSymbol tsymbol) { this.env = env; } + public BTypedescType(Env env, BType constraint, BTypeSymbol tsymbol, SemType semType) { + this(env, constraint, tsymbol); + this.semType = semType; + } + @Override public BType getConstraint() { - return constraint; } @Override public R accept(BTypeVisitor visitor, T t) { - return visitor.visit(this, t); } @Override - public String toString() { + public TypeKind getKind() { + return TypeKind.TYPEDESC; + } + @Override + public String toString() { if (constraint.tag == TypeTags.ANY) { return super.toString(); } @@ -65,16 +73,20 @@ public String toString() { @Override public void accept(TypeVisitor visitor) { - visitor.visit(this); } @Override public SemType semType() { - if (constraint == null || constraint instanceof BNoType) { - return PredefinedType.TYPEDESC; + if (this.semType != null) { + return this.semType; } - return SemTypes.typedescContaining(env, constraint.semType()); + if (constraint == null || constraint instanceof BNoType) { + this.semType = PredefinedType.TYPEDESC; + } else { + this.semType = SemTypes.typedescContaining(env, constraint.semType()); + } + return this.semType; } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BUnionType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BUnionType.java index a9451a743286..d414330d321b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BUnionType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BUnionType.java @@ -17,7 +17,6 @@ */ package org.wso2.ballerinalang.compiler.semantics.model.types; -import io.ballerina.types.Core; import io.ballerina.types.Env; import io.ballerina.types.PredefinedType; import io.ballerina.types.SemType; @@ -53,11 +52,8 @@ public class BUnionType extends BType implements UnionType { protected LinkedHashSet memberTypes; - private LinkedHashSet memberSemTypes; - public boolean isCyclic = false; - private LinkedHashSet originalMemberTypes; private static final String INT_CLONEABLE = "__Cloneable"; private static final String CLONEABLE = "Cloneable"; @@ -328,14 +324,14 @@ public void mergeUnionType(BUnionType unionType) { } } else if (member instanceof BTableType tableType) { if (getImpliedType(tableType.constraint) == unionType) { - BTableType newTableType = new BTableType(env, tableType.tag, this, tableType.tsymbol, + BTableType newTableType = new BTableType(env, this, tableType.tsymbol, tableType.getFlags()); this.add(newTableType); continue; } else if (tableType.constraint instanceof BMapType mapType) { if (getImpliedType(mapType.constraint) == unionType) { BMapType newMapType = new BMapType(env, mapType.tag, this, mapType.tsymbol, mapType.getFlags()); - BTableType newTableType = new BTableType(env, tableType.tag, newMapType, tableType.tsymbol, + BTableType newTableType = new BTableType(env, newMapType, tableType.tsymbol, tableType.getFlags()); this.add(newTableType); continue; @@ -480,37 +476,6 @@ private static boolean isNeverType(BType type) { return false; } - public void populateMemberSemTypes(boolean ignoreTypeIds) { - LinkedHashSet memberSemTypes = new LinkedHashSet<>(); - - for (BType memberType : this.memberTypes) { - populateMemberSemTypes(memberType, memberSemTypes, ignoreTypeIds); - } - - this.memberSemTypes = memberSemTypes; - this.resetSemType(); - } - - private void populateMemberSemTypes(BType memberType, LinkedHashSet memberSemTypes, - boolean ignoreTypeIds) { - memberType = getReferredType(memberType); - if (memberType == null) { // TODO: handle cyclic types via BIR - return; - } - - if (memberType.tag == TypeTags.UNION) { - BUnionType bUnionType = (BUnionType) memberType; - bUnionType.populateMemberSemTypes(ignoreTypeIds); - memberSemTypes.addAll(bUnionType.memberSemTypes); - return; - } - - SemType s = memberType.semType(); - if (!Core.isNever(s)) { - memberSemTypes.add(s); - } - } - /** * When the type is mutated we need to reset resolved semType. */ @@ -521,16 +486,15 @@ public void resetSemType() { @Override public SemType semType() { if (this.semType == null) { - populateMemberSemTypes(false); - this.semType = computeResultantUnion(memberSemTypes); + this.semType = computeSemTypeFromMemberTypes(); } return this.semType; } - private SemType computeResultantUnion(LinkedHashSet memberSemTypes) { + private SemType computeSemTypeFromMemberTypes() { SemType t = PredefinedType.NEVER; - for (SemType s : memberSemTypes) { - t = SemTypes.union(t, s); + for (BType ty : this.memberTypes) { + t = SemTypes.union(t, ty.semType()); } return t; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BXMLSubType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BXMLSubType.java index 65387c546d7b..c51c77304eec 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BXMLSubType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BXMLSubType.java @@ -17,12 +17,18 @@ */ package org.wso2.ballerinalang.compiler.semantics.model.types; +import io.ballerina.types.Core; +import io.ballerina.types.PredefinedType; import io.ballerina.types.SemType; +import io.ballerina.types.SemTypes; import org.ballerinalang.model.Name; import org.ballerinalang.model.types.SelectivelyImmutableReferenceType; import org.ballerinalang.model.types.TypeKind; -import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.util.Names; +import org.wso2.ballerinalang.compiler.util.TypeTags; +import org.wso2.ballerinalang.util.Flags; + +import static org.wso2.ballerinalang.compiler.semantics.analyzer.Types.AND_READONLY_SUFFIX; /** * Represents Builtin Subtype of integer. @@ -30,54 +36,53 @@ * @since 1.2.0 */ public class BXMLSubType extends BType implements SelectivelyImmutableReferenceType { - public BXMLSubType(int tag, Name name, SemType semType) { - super(tag, null, name, 0, semType); - } + public static final BXMLSubType XML_ELEMENT = new BXMLSubType(TypeTags.XML_ELEMENT, Names.XML_ELEMENT, + SemTypes.XML_ELEMENT); + public static final BXMLSubType XML_PI = new BXMLSubType(TypeTags.XML_PI, Names.XML_PI, SemTypes.XML_PI); + public static final BXMLSubType XML_COMMENT = new BXMLSubType(TypeTags.XML_COMMENT, Names.XML_COMMENT, + SemTypes.XML_COMMENT); + public static final BXMLSubType XML_TEXT = new BXMLSubType(TypeTags.XML_TEXT, Names.XML_TEXT, Flags.READONLY, + SemTypes.XML_TEXT); - public BXMLSubType(int tag, Name name, long flags, SemType semType) { + private BXMLSubType(int tag, Name name, SemType semType) { + this(tag, name, 0, semType); + } + private BXMLSubType(int tag, Name name, long flags, SemType semType) { super(tag, null, name, flags, semType); } + public static BXMLSubType newImmutableXMLSubType(BXMLSubType xmlSubType) { + return new BXMLSubType(xmlSubType.tag, + Names.fromString(xmlSubType.name.getValue().concat(AND_READONLY_SUFFIX)), + xmlSubType.getFlags() | Flags.READONLY, + Core.intersect(xmlSubType.semType, PredefinedType.VAL_READONLY)); + } + @Override public boolean isNullable() { - return false; } @Override public R accept(BTypeVisitor visitor, T t) { - return visitor.visit(this, t); } @Override public TypeKind getKind() { - return TypeKind.XML; } - @Override - public void accept(TypeVisitor visitor) { - - visitor.visit(this); - } - @Override public String toString() { - return Names.XML.value + Names.ALIAS_SEPARATOR + name; } @Override public String getQualifiedTypeName() { - return Names.BALLERINA_ORG.value + Names.ORG_NAME_SEPARATOR.value + Names.LANG.value + Names.DOT.value + Names.XML.value + Names.ALIAS_SEPARATOR + name; } - - public boolean isAnydata() { - return true; - } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BXMLType.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BXMLType.java index ee427b5054d0..602c52bb28d0 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BXMLType.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BXMLType.java @@ -21,6 +21,7 @@ import io.ballerina.types.SemType; import io.ballerina.types.SemTypes; import org.ballerinalang.model.types.SelectivelyImmutableReferenceType; +import org.ballerinalang.model.types.TypeKind; import org.wso2.ballerinalang.compiler.semantics.model.TypeVisitor; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; @@ -33,7 +34,7 @@ * * @since 0.961.0 */ -public class BXMLType extends BBuiltInRefType implements SelectivelyImmutableReferenceType { +public class BXMLType extends BType implements SelectivelyImmutableReferenceType { public BType constraint; public BXMLType mutableType; @@ -66,6 +67,11 @@ public R accept(BTypeVisitor visitor, T t) { return visitor.visit(this, t); } + @Override + public TypeKind getKind() { + return TypeKind.XML; + } + @Override public void accept(TypeVisitor visitor) { visitor.visit(this); @@ -73,6 +79,10 @@ public void accept(TypeVisitor visitor) { @Override public SemType semType() { + if (this.semType != null) { + return this.semType; + } + SemType s; if (constraint == null) { s = PredefinedType.XML; @@ -92,6 +102,7 @@ public SemType semType() { } boolean readonly = Symbols.isFlagOn(this.getFlags(), Flags.READONLY); - return readonly ? SemTypes.intersect(PredefinedType.VAL_READONLY, s) : s; + this.semType = readonly ? SemTypes.intersect(PredefinedType.VAL_READONLY, s) : s; + return this.semType; } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/ImmutableTypeCloner.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/ImmutableTypeCloner.java index 09a5bd663c76..fe11dda777dd 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/ImmutableTypeCloner.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/ImmutableTypeCloner.java @@ -18,8 +18,6 @@ package org.wso2.ballerinalang.compiler.util; import io.ballerina.tools.diagnostics.Location; -import io.ballerina.types.PredefinedType; -import io.ballerina.types.SemType; import org.ballerinalang.model.TreeBuilder; import org.ballerinalang.model.elements.Flag; import org.ballerinalang.model.elements.PackageID; @@ -79,7 +77,6 @@ import java.util.Optional; import java.util.Set; -import static io.ballerina.types.SemTypes.intersect; import static org.ballerinalang.model.symbols.SymbolOrigin.SOURCE; import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL; import static org.wso2.ballerinalang.compiler.util.CompilerUtils.getMajorVersion; @@ -91,8 +88,6 @@ */ public final class ImmutableTypeCloner { - private static final String AND_READONLY_SUFFIX = " & readonly"; - private ImmutableTypeCloner() { } @@ -222,13 +217,7 @@ private static BIntersectionType setImmutableType(Location pos, Types types, case TypeTags.XML_ELEMENT: case TypeTags.XML_PI: BXMLSubType origXmlSubType = (BXMLSubType) type; - - SemType xmlRoSemType = intersect(origXmlSubType.semType(), PredefinedType.VAL_READONLY); - // TODO: 4/28/20 Check tsymbol - BXMLSubType immutableXmlSubType = - new BXMLSubType(origXmlSubType.tag, - Names.fromString(origXmlSubType.name.getValue().concat(AND_READONLY_SUFFIX)), - origXmlSubType.getFlags() | Flags.READONLY, xmlRoSemType); + BXMLSubType immutableXmlSubType = BXMLSubType.newImmutableXMLSubType(origXmlSubType); BIntersectionType immutableXmlSubTypeIntersectionType = createImmutableIntersectionType(pkgId, owner, originalType, immutableXmlSubType, symTable); @@ -260,20 +249,7 @@ private static BIntersectionType setImmutableType(Location pos, Types types, unresolvedTypes, (BTableType) type, originalType); case TypeTags.ANY: BAnyType origAnyType = (BAnyType) type; - - BTypeSymbol immutableAnyTSymbol = getReadonlyTSymbol(names, origAnyType.tsymbol, env, pkgId, owner); - - BAnyType immutableAnyType; - if (immutableAnyTSymbol != null) { - immutableAnyType = new BAnyType(immutableAnyTSymbol, immutableAnyTSymbol.name, - origAnyType.getFlags() | Flags.READONLY, origAnyType.semType()); - immutableAnyTSymbol.type = immutableAnyType; - } else { - immutableAnyType = new BAnyType(origAnyType.tag, null, - getImmutableTypeName(names, TypeKind.ANY.typeName()), - origAnyType.getFlags() | Flags.READONLY, origAnyType.semType()); - } - + BAnyType immutableAnyType = BAnyType.newImmutableBAnyType(); BIntersectionType immutableAnyIntersectionType = createImmutableIntersectionType(pkgId, owner, originalType, immutableAnyType, @@ -298,13 +274,13 @@ private static BIntersectionType defineImmutableTableType(Location pos, Types ty Names names, Set unresolvedTypes, BTableType type, BType originalType) { - BTypeSymbol immutableTableTSymbol = getReadonlyTSymbol(names, type.tsymbol, env, pkgId, owner); + BTypeSymbol immutableTableTSymbol = getReadonlyTSymbol(type.tsymbol, env, pkgId, owner); Optional immutableType = Types.getImmutableType(symTable, pkgId, type); if (immutableType.isPresent()) { return immutableType.get(); } else { Types.addImmutableType(symTable, pkgId, type, createImmutableIntersectionType(pkgId, owner, - originalType, new BTableType(symTable.typeEnv(), TypeTags.TABLE, null, immutableTableTSymbol, + originalType, new BTableType(symTable.typeEnv(), null, immutableTableTSymbol, type.getFlags() | Flags.READONLY), symTable)); } @@ -340,7 +316,7 @@ private static BIntersectionType defineImmutableXMLType(Location pos, Types type Names names, Set unresolvedTypes, BXMLType type, BType originalType) { - BTypeSymbol immutableXmlTSymbol = getReadonlyTSymbol(names, type.tsymbol, env, pkgId, owner); + BTypeSymbol immutableXmlTSymbol = getReadonlyTSymbol(type.tsymbol, env, pkgId, owner); Optional immutableType = Types.getImmutableType(symTable, pkgId, type); if (immutableType.isPresent()) { return immutableType.get(); @@ -364,7 +340,7 @@ private static BIntersectionType defineImmutableArrayType(Location pos, Types ty Names names, Set unresolvedTypes, BArrayType type, BType originalType) { - BTypeSymbol immutableArrayTSymbol = getReadonlyTSymbol(names, type.tsymbol, env, pkgId, owner); + BTypeSymbol immutableArrayTSymbol = getReadonlyTSymbol(type.tsymbol, env, pkgId, owner); Optional immutableType = Types.getImmutableType(symTable, pkgId, type); if (immutableType.isPresent()) { return immutableType.get(); @@ -389,7 +365,7 @@ private static BIntersectionType defineImmutableMapType(Location pos, Types type Names names, Set unresolvedTypes, BMapType type, BType originalType) { - BTypeSymbol immutableMapTSymbol = getReadonlyTSymbol(names, type.tsymbol, env, pkgId, owner); + BTypeSymbol immutableMapTSymbol = getReadonlyTSymbol(type.tsymbol, env, pkgId, owner); Optional immutableType = Types.getImmutableType(symTable, pkgId, type); if (immutableType.isPresent()) { return immutableType.get(); @@ -437,7 +413,7 @@ private static BIntersectionType defineImmutableTupleType(Location pos, Types ty Name origTupleTypeSymbolName = Names.EMPTY; if (!originalTypeName.isEmpty()) { origTupleTypeSymbolName = origTupleTypeSymbol.name.value.isEmpty() ? Names.EMPTY : - getImmutableTypeName(names, getSymbolFQN(origTupleTypeSymbol)); + Types.getImmutableTypeName(getSymbolFQN(origTupleTypeSymbol)); tupleEffectiveImmutableType.name = origTupleTypeSymbolName; } @@ -588,7 +564,7 @@ private static BIntersectionType defineImmutableRecordType(Location pos, BRecord BTypeSymbol recordTypeSymbol = origRecordType.tsymbol; BRecordTypeSymbol recordSymbol = Symbols.createRecordSymbol(recordTypeSymbol.flags | Flags.READONLY, - getImmutableTypeName(names, getSymbolFQN(recordTypeSymbol)), + Types.getImmutableTypeName(getSymbolFQN(recordTypeSymbol)), pkgID, null, env.scope.owner, pos, VIRTUAL); BInvokableType bInvokableType = @@ -639,7 +615,7 @@ private static BIntersectionType defineImmutableObjectType(Location pos, flags &= ~Flags.CLASS; BObjectTypeSymbol objectSymbol = Symbols.createObjectSymbol(flags, - getImmutableTypeName(names, + Types.getImmutableTypeName( getSymbolFQN(origObjectTSymbol)), pkgID, null, env.scope.owner, pos, VIRTUAL); @@ -692,7 +668,8 @@ public static void defineObjectFunctions(BObjectTypeSymbol immutableObjectSymbol Name funcName = Names.fromString(Symbols.getAttachedFuncSymbolName(immutableObjectSymbol.name.value, origFunc.funcName.value)); BInvokableSymbol immutableFuncSymbol = - ASTBuilderUtil.duplicateFunctionDeclarationSymbol(origFunc.symbol, immutableObjectSymbol, + ASTBuilderUtil.duplicateFunctionDeclarationSymbol(symTable.typeEnv(), origFunc.symbol, + immutableObjectSymbol, funcName, immutableObjectSymbol.pkgID, symTable.builtinPos, VIRTUAL); immutableFuncs.add(new BAttachedFunction(origFunc.funcName, immutableFuncSymbol, @@ -770,27 +747,23 @@ private static BIntersectionType defineImmutableBuiltInUnionType(Location pos, T private static BAnydataType defineImmutableAnydataType(SymbolEnv env, PackageID pkgId, BSymbol owner, Names names, BAnydataType type) { - BTypeSymbol immutableAnydataTSymbol = getReadonlyTSymbol(names, type.tsymbol, env, pkgId, owner); + BTypeSymbol immutableAnydataTSymbol = getReadonlyTSymbol(type.tsymbol, env, pkgId, owner); if (immutableAnydataTSymbol != null) { - BAnydataType immutableAnydataType = - new BAnydataType(type.env, immutableAnydataTSymbol, - immutableAnydataTSymbol.name, type.getFlags() | Flags.READONLY, + BAnydataType immutableAnydataType = BAnydataType.newImmutableBAnydataType(type, immutableAnydataTSymbol, + immutableAnydataTSymbol.name, type.isNullable()); immutableAnydataTSymbol.type = immutableAnydataType; return immutableAnydataType; } - return new BAnydataType(type.env, null, - getImmutableTypeName(names, TypeKind.ANYDATA.typeName()), - type.getFlags() | Flags.READONLY, type.isNullable()); + return BAnydataType.newImmutableBAnydataType(type, null, + Types.getImmutableTypeName(TypeKind.ANYDATA.typeName()), type.isNullable()); } private static BJSONType defineImmutableJsonType(SymbolEnv env, PackageID pkgId, BSymbol owner, Names names, BJSONType type) { - BTypeSymbol immutableJsonTSymbol = getReadonlyTSymbol(names, type.tsymbol, env, pkgId, owner); - BJSONType immutableJsonType = new BJSONType(type.env, immutableJsonTSymbol, - type.isNullable(), - type.getFlags() | Flags.READONLY); + BTypeSymbol immutableJsonTSymbol = getReadonlyTSymbol(type.tsymbol, env, pkgId, owner); + BJSONType immutableJsonType = BJSONType.newImmutableBJSONType(type, immutableJsonTSymbol, type.isNullable()); if (immutableJsonTSymbol != null) { immutableJsonTSymbol.type = immutableJsonType; } @@ -812,7 +785,7 @@ private static BIntersectionType handleImmutableUnionType(Location pos, Types ty String originalTypeName = origUnionTypeSymbol == null ? "" : origUnionTypeSymbol.name.getValue(); if (!originalTypeName.isEmpty()) { - unionEffectiveImmutableType.name = getImmutableTypeName(names, getSymbolFQN(origUnionTypeSymbol)); + unionEffectiveImmutableType.name = Types.getImmutableTypeName(getSymbolFQN(origUnionTypeSymbol)); } for (BType memberType : originalMemberList) { @@ -837,7 +810,7 @@ private static BIntersectionType handleImmutableUnionType(Location pos, Types ty BTypeSymbol immutableUnionTSymbol = getReadonlyTSymbol(origUnionTypeSymbol, env, pkgId, owner, origUnionTypeSymbol.name.value.isEmpty() ? Names.EMPTY : - getImmutableTypeName(names, getSymbolFQN(origUnionTypeSymbol))); + Types.getImmutableTypeName(getSymbolFQN(origUnionTypeSymbol))); immutableType.effectiveType.tsymbol = immutableUnionTSymbol; immutableType.effectiveType.addFlags(type.getFlags() | Flags.READONLY); @@ -849,13 +822,13 @@ private static BIntersectionType handleImmutableUnionType(Location pos, Types ty return immutableType; } - private static BTypeSymbol getReadonlyTSymbol(Names names, BTypeSymbol originalTSymbol, SymbolEnv env, + private static BTypeSymbol getReadonlyTSymbol(BTypeSymbol originalTSymbol, SymbolEnv env, PackageID pkgId, BSymbol owner) { if (originalTSymbol == null) { return null; } - return getReadonlyTSymbol(originalTSymbol, env, pkgId, owner, getImmutableTypeName(names, originalTSymbol)); + return getReadonlyTSymbol(originalTSymbol, env, pkgId, owner, getImmutableTypeName(originalTSymbol)); } private static BTypeSymbol getReadonlyTSymbol(BTypeSymbol originalTSymbol, SymbolEnv env, PackageID pkgId, @@ -885,16 +858,8 @@ private static String getSymbolFQN(BTypeSymbol originalTSymbol) { getMajorVersion(pkgID.version.value) + ":" + originalTSymbol.name; } - private static Name getImmutableTypeName(Names names, BTypeSymbol originalTSymbol) { - return getImmutableTypeName(names, originalTSymbol.name.getValue()); - } - - private static Name getImmutableTypeName(Names names, String origName) { - if (origName.isEmpty()) { - return Names.EMPTY; - } - - return Names.fromString("(".concat(origName).concat(AND_READONLY_SUFFIX).concat(")")); + private static Name getImmutableTypeName(BTypeSymbol originalTSymbol) { + return Types.getImmutableTypeName(originalTSymbol.name.getValue()); } private static BIntersectionType createImmutableIntersectionType(SymbolEnv env, BType nonReadOnlyType, diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Unifier.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Unifier.java index cc3eec358e87..1bb911853161 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Unifier.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Unifier.java @@ -17,6 +17,7 @@ package org.wso2.ballerinalang.compiler.util; +import io.ballerina.types.Env; import org.ballerinalang.model.TreeBuilder; import org.ballerinalang.model.tree.NodeKind; import org.ballerinalang.model.types.TypeKind; @@ -31,7 +32,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.BFiniteType; import org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType; @@ -89,14 +89,16 @@ public class Unifier implements BTypeVisitor { private SymbolEnv env; private Types types; private BLangDiagnosticLog dlog; + private Env typeEnv; - public BType build(BType originalType, BType expType, BLangInvocation invocation, Types types, + public BType build(Env typeEnv, BType originalType, BType expType, BLangInvocation invocation, Types types, SymbolTable symbolTable, BLangDiagnosticLog dlog) { this.isInvocation = invocation != null; if (this.isInvocation) { this.invocation = invocation; createParamMap(invocation); } + this.typeEnv = typeEnv; this.types = types; this.symbolTable = symbolTable; this.dlog = dlog; @@ -105,12 +107,13 @@ public BType build(BType originalType, BType expType, BLangInvocation invocation return newType; } - public BType build(BType originalType) { - return build(originalType, null, null, null, null, null); + public BType build(Env typeEnv, BType originalType) { + return build(typeEnv, originalType, null, null, null, null, null); } - public void validate(BType returnType, BLangFunction function, SymbolTable symbolTable, SymbolEnv env, Types types, - BLangDiagnosticLog dlog) { + public void validate(Env typeEnv, BType returnType, BLangFunction function, SymbolTable symbolTable, SymbolEnv env, + Types types, BLangDiagnosticLog dlog) { + this.typeEnv = typeEnv; this.function = function; this.symbolTable = symbolTable; this.env = env; @@ -125,11 +128,6 @@ public BType visit(BType originalType, BType expType) { return originalType; } - @Override - public BType visit(BBuiltInRefType originalType, BType expType) { - return originalType; - } - @Override public BType visit(BAnyType originalType, BType expType) { return originalType; @@ -154,7 +152,7 @@ public BType visit(BMapType originalType, BType expType) { return symbolTable.semanticError; } - BMapType newMType = new BMapType(originalType.env, originalType.tag, newConstraint, null); + BMapType newMType = new BMapType(typeEnv, originalType.tag, newConstraint, null); setFlags(newMType, originalType.getFlags()); return newMType; } @@ -197,8 +195,8 @@ public BType visit(BArrayType originalType, BType expType) { return symbolTable.semanticError; } - BArrayType newArrayType = - new BArrayType(originalType.env, newElemType, null, originalType.getSize(), originalType.state); + BArrayType newArrayType = new BArrayType(typeEnv, newElemType, null, originalType.getSize(), + originalType.state); setFlags(newArrayType, originalType.getFlags()); return newArrayType; } @@ -285,7 +283,7 @@ public BType visit(BTupleType originalType, BType expType) { return expType != null ? expType : originalType; } - BTupleType type = new BTupleType(originalType.env, members); + BTupleType type = new BTupleType(typeEnv, members); type.restType = newRestType; setFlags(type, originalType.getFlags()); return type; @@ -314,7 +312,7 @@ public BType visit(BStreamType originalType, BType expType) { return symbolTable.semanticError; } - BStreamType type = new BStreamType(originalType.env, originalType.tag, newConstraint, newError, null); + BStreamType type = new BStreamType(typeEnv, originalType.tag, newConstraint, newError, null); setFlags(type, originalType.getFlags()); return type; } @@ -341,7 +339,7 @@ public BType visit(BTableType originalType, BType expType) { return symbolTable.semanticError; } - BTableType newTableType = new BTableType(originalType.env, TypeTags.TABLE, newConstraint, null); + BTableType newTableType = new BTableType(typeEnv, newConstraint, null); newTableType.keyTypeConstraint = null; newTableType.fieldNameList = originalType.fieldNameList; newTableType.constraintPos = originalType.constraintPos; @@ -413,7 +411,7 @@ public BType visit(BInvokableType originalType, BType expType) { } } - BType type = new BInvokableType(originalType.env, paramTypes, newRestType, retType, null); + BType type = new BInvokableType(typeEnv, paramTypes, newRestType, retType, null); setFlags(type, originalType.getFlags()); return type; } @@ -512,8 +510,7 @@ public BType visit(BFutureType originalType, BType expType) { return symbolTable.semanticError; } - BFutureType newFutureType = new BFutureType(originalType.env, originalType.tag, newConstraint, null, - originalType.workerDerivative); + BFutureType newFutureType = new BFutureType(typeEnv, newConstraint, null, originalType.workerDerivative); setFlags(newFutureType, originalType.getFlags()); return newFutureType; } @@ -538,7 +535,7 @@ public BType visit(BTypedescType originalType, BType expType) { return symbolTable.semanticError; } - BTypedescType newTypedescType = new BTypedescType(originalType.env, newConstraint, null); + BTypedescType newTypedescType = new BTypedescType(typeEnv, newConstraint, null); setFlags(newTypedescType, originalType.getFlags()); return newTypedescType; } @@ -570,8 +567,7 @@ public BType visit(BParameterizedType originalType, BType expType) { // Log an error only if the user has not explicitly passed an argument. If the passed // argument is invalid, the type checker will log the error. dlog.error(invocation.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPE_FOR_INFERRED_TYPEDESC_VALUE, - paramVarName, paramSymbolTypedescType, new BTypedescType(symbolTable.typeEnv(), - expType, null)); + paramVarName, paramSymbolTypedescType, new BTypedescType(typeEnv, expType, null)); return symbolTable.semanticError; } BType type = paramValueTypes.get(paramVarName); @@ -700,7 +696,7 @@ private BLangNamedArgsExpression createTypedescExprNamedArg(BType expType, Strin BLangTypedescExpr typedescExpr = (BLangTypedescExpr) TreeBuilder.createTypeAccessNode(); typedescExpr.pos = this.symbolTable.builtinPos; typedescExpr.resolvedType = expType; - typedescExpr.setBType(new BTypedescType(symbolTable.typeEnv(), expType, null)); + typedescExpr.setBType(new BTypedescType(typeEnv, expType, null)); BLangNamedArgsExpression namedArgsExpression = (BLangNamedArgsExpression) TreeBuilder.createNamedArgNode(); BLangIdentifier identifierNode = (BLangIdentifier) TreeBuilder.createIdentifierNode(); @@ -1187,7 +1183,7 @@ private BType getExpectedTypeForInferredTypedescMember(BUnionType originalType, return expectedTypesSet.iterator().next(); } - return BUnionType.create(symbolTable.typeEnv(), null, expectedTypesSet); + return BUnionType.create(typeEnv, null, expectedTypesSet); } private boolean isSameTypeOrError(BType newType, BType originalType) {