Skip to content

Commit

Permalink
Add doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Jun 19, 2024
1 parent 68645b9 commit 1816b2b
Show file tree
Hide file tree
Showing 29 changed files with 2,450 additions and 2,510 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

package io.ballerina.runtime.api.types.semtype;

// SEMTYPE-TODO: revisit this after fully implementing semtypes. Added this to match nBallerina where this is just a
// type alias to int. Maybe not needed here due to the way we have modeled type hierarchy (need to check if doing
// instancof checks on this is faster than checking if some is 0)

/**
* Represents a union of basic types.
*
* @since 2201.10.0
*/
public interface BasicTypeBitSet {

default int some() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

package io.ballerina.runtime.api.types.semtype;

/**
* Represent bit field that indicate which basic type a semType belongs to.
*
* @since 2201.10.0
*/
public final class BasicTypeCode {

public static final int CODE_NIL = 0x00;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,27 @@
package io.ballerina.runtime.api.types.semtype;

import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.internal.types.BType;
import io.ballerina.runtime.internal.types.semtype.BBooleanSubType;
import io.ballerina.runtime.internal.types.semtype.BDecimalSubType;
import io.ballerina.runtime.internal.types.semtype.BFloatSubType;
import io.ballerina.runtime.internal.types.semtype.BIntSubType;
import io.ballerina.runtime.internal.types.semtype.BStringSubType;
import io.ballerina.runtime.internal.values.DecimalValue;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.CODE_B_TYPE;

/**
* Utility class for creating semtypes.
*
* @since 2201.10.0
*/
public final class Builder {

private static final String[] EMPTY_STRING_ARR = new String[0];
Expand Down Expand Up @@ -98,7 +106,7 @@ public static SemType charType() {
private static final SemType NEVER = SemType.from(0);

public static SemType basicTypeUnion(int bitset) {
// TODO: may be cache single type bit sets as well as well
// TODO: may be cache single type bit sets as well
if (bitset == 0) {
return NEVER;
} else if (Integer.bitCount(bitset) == 1) {
Expand Down Expand Up @@ -163,6 +171,25 @@ static SubType[] initializeSubtypeArray() {
return new SubType[CODE_B_TYPE + 2];
}

public static Optional<SemType> typeOf(Object object) {
if (object == null) {
return Optional.of(nilType());
} else if (object instanceof DecimalValue decimalValue) {
return Optional.of(decimalConst(decimalValue.value()));
} else if (object instanceof Double doubleValue) {
return Optional.of(floatConst(doubleValue));
} else if (object instanceof Number intValue) {
long value =
intValue instanceof Byte byteValue ? Byte.toUnsignedLong(byteValue) : intValue.longValue();
return Optional.of(intConst(value));
} else if (object instanceof Boolean booleanValue) {
return Optional.of(booleanConst(booleanValue));
} else if (object instanceof BString stringValue) {
return Optional.of(stringConst(stringValue.getValue()));
}
return Optional.empty();
}

private static final class IntTypeCache {

private static final int CACHE_MAX_VALUE = 127;
Expand All @@ -184,7 +211,6 @@ private static final class BooleanTypeCache {
private static SemType createBooleanSingletonType(boolean value) {
return basicSubType(BasicTypeCode.BT_BOOLEAN, BBooleanSubType.from(value));
}

}

private static final class StringTypeCache {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

package io.ballerina.runtime.api.types.semtype;

/**
* Context in which semtype was defined in.
*
* @since 2201.10.0
*/
public class Context {

// SEMTYPE-TODO: Fill this in as needed, currently just a placeholder since basic types don't need it
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.CODE_UNDEF;
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.VT_MASK;

/**
* Contain functions defined in `core.bal` file.
*
* @since 2201.10.0
*/
public final class Core {

public static final SemType SEMTYPE_TOP = SemType.from((1 << (CODE_UNDEF + 1)) - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
import io.ballerina.runtime.internal.types.BSemTypeWrapper;
import io.ballerina.runtime.internal.types.semtype.PureSemType;

/**
* Runtime representation of SemType.
*
* @since 2201.10.0
*/
public abstract sealed class SemType implements BasicTypeBitSet permits BSemTypeWrapper, PureSemType {

final int all;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.CODE_UNDEF;
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.CODE_XML;

public final class SemTypeHelper {
/**
* Collection of utility function on {@code SemType}.
*
* @since 2201.10.0
*/
final class SemTypeHelper {

private SemTypeHelper() {
}
Expand Down

This file was deleted.

Loading

0 comments on commit 1816b2b

Please sign in to comment.