Skip to content

Commit c04c58f

Browse files
committed
Feature(compiler): PropertyAccessExpressionCodeGenerator - initial support
1 parent e4f66be commit c04c58f

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
import * as ts from "typescript";
3+
import * as llvm from 'llvm-node';
4+
import {NodeGenerateInterface} from "../node-generate.interface";
5+
import {Context} from "../context";
6+
import {NativeType} from "../native-type";
7+
import {Value} from "../value";
8+
import UnsupportedError from "../../error";
9+
import {buildFromExpression} from "../index";
10+
11+
export class PropertyAccessExpressionCodeGenerator implements NodeGenerateInterface<ts.PropertyAccessExpression, Value> {
12+
generate(node: ts.PropertyAccessExpression, ctx: Context, builder: llvm.IRBuilder, nativeType?: NativeType): Value {
13+
const object = buildFromExpression(node.expression, ctx, builder);
14+
if (object) {
15+
return object;
16+
}
17+
18+
throw new UnsupportedError(
19+
node,
20+
'Unsupported...'
21+
);
22+
}
23+
}

src/backend/llvm/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {ArrayLiteralExpressionCodeGenerator} from "./code-generation/array-liter
3131
import {ArrayLiteralExpression} from "typescript";
3232
import {IfStatementCodeGenerator} from "./code-generation/if-statement";
3333
import {CallExpressionCodeGenerator} from "./code-generation/call-expression";
34-
import {StringLiteral} from "typescript";
34+
import {PropertyAccessExpressionCodeGenerator} from "./code-generation/property-access-expression";
3535

3636
export function emitCondition(
3737
condition: ts.Expression,
@@ -223,6 +223,19 @@ function buildFromPostfixUnaryExpression(
223223
}
224224
}
225225

226+
function extractNameFromObjectType(
227+
type: ts.ObjectType
228+
): string {
229+
const name = <string>type.symbol.escapedName;
230+
231+
if (type.isClassOrInterface() && type.typeParameters) {
232+
return name + type.typeParameters.map((typeParameter: ts.TypeParameter) => {
233+
return <string>typeParameter.symbol.escapedName;
234+
}).join('');
235+
}
236+
237+
return name;
238+
}
226239

227240
function mangleNameFromDeclaration(
228241
declaration: ts.SignatureDeclaration,
@@ -233,7 +246,7 @@ function mangleNameFromDeclaration(
233246
const left = ctx.typeChecker.getTypeAtLocation(declaration.parent!) as ts.ObjectType;
234247

235248
return mangler.getMethodName(
236-
<string>left.symbol.escapedName,
249+
extractNameFromObjectType(left),
237250
<string>(<ts.Identifier>declaration.name).escapedText,
238251
declaration.parameters
239252
);
@@ -243,7 +256,7 @@ function mangleNameFromDeclaration(
243256
const left = ctx.typeChecker.getTypeAtLocation(declaration.parent!) as ts.ObjectType;
244257

245258
return mangler.getMethodName(
246-
<string>left.symbol.escapedName,
259+
extractNameFromObjectType(left),
247260
<string>(<any>declaration.name),
248261
declaration.parameters
249262
);
@@ -376,6 +389,8 @@ export function buildFromExpression(block: ts.Expression, ctx: Context, builder:
376389
switch (block.kind) {
377390
case ts.SyntaxKind.NewExpression:
378391
return new NewExpressionGenerator().generate(<any>block, ctx, builder);
392+
case ts.SyntaxKind.PropertyAccessExpression:
393+
return new PropertyAccessExpressionCodeGenerator().generate(<any>block, ctx, builder);
379394
case ts.SyntaxKind.Identifier:
380395
return buildFromIdentifier(<any>block, ctx, builder);
381396
case ts.SyntaxKind.NumericLiteral:

0 commit comments

Comments
 (0)