sbip-sg / tsll

Tsll is a Typescript-to-LLVM-IR compiler frontend authored by Singapore Blockchain Innovation Programme (SBIP).
https://www.comp.nus.edu.sg/~dbsystem/discover
Other
8 stars 3 forks source link

Translation of Typescript anyType to LLVM IR #14

Open lung21 opened 2 years ago

lung21 commented 2 years ago

Motivation

Similar to Javascript, Typescript also supports type any that allows a variable of that type to be assigned values of any other types. This also means that the type checking is disabled whenever any is used in Typescript. Now we are trying to find how any constructs with any could be possibly translated into LLVM IR. Below is an example of one variable and two functions that are defined with any in Typescript.

Example

let obj: any = { content: 'heymycontent' };
obj = 23;

function isView(arg: any): boolean {
    // Do something here
}

// the output of this function may be of any
function calculate(argA: string, argB: number): any {
    return argA + argB;
}
taquangtrung commented 2 years ago

Hi @lung21,

Is this the case that there will be 2 type errors (type conflict) in the below code?

let obj: any = { content: 'heymycontent' };
obj = 23;        // ==> assign an integer value to a record?
function isView(arg: any): boolean {
    // Do something here
}

// the output of this function may be of any
function calculate(argA: string, argB: number): any {
    return argA + argB;          // ===> addition of a string and a number
}
lung21 commented 2 years ago

@taquangtrung Thank you for responding.

In short, Typescript considers these not erroneous with any unless we explicitly tell it not to allow any, but unfortunately we will still encounter any in other libraries even though we do not in our .ts source file.

taquangtrung commented 2 years ago

I think for these two cases, we can do a simple type check and report errors if there are type conflicts.

If the type of one operand is a sub-type of the other, maybe we can instantiate any by the super type?