Skip to content

Commit 3ed4b68

Browse files
committed
Feature(compiler): Support function overloading
1 parent 824a6b8 commit 3ed4b68

5 files changed

Lines changed: 45 additions & 40 deletions

File tree

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"commander": "^2.19.0"
2424
},
2525
"dependencies": {
26-
"@static-script/runtime": "^0.2.0",
26+
"@static-script/runtime": "^0.3.0",
2727
"llvm-node": "github:MichaReiser/llvm-node#master",
2828
"typescript": "^3.1.1"
2929
}

src/backend/llvm/cpp.mangler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class CPPMangler {
1010
case ts.SyntaxKind.NumberKeyword:
1111
return 'd';
1212
case ts.SyntaxKind.StringKeyword:
13-
return 'c';
13+
return 'PKc';
1414
default:
1515
throw new Error(
1616
`Unsupported mangling parameter type: ${parameter.type.kind}`

src/backend/llvm/index.ts

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {RUNTIME_DEFINITION_FILE} from "@static-script/runtime";
1010
import {LANGUAGE_DEFINITION_FILE} from "../../constants";
1111
import {CMangler} from "./c.mangler";
1212
import {ManglerInterface} from "./mangler.interface";
13+
import {SignatureDeclaration} from "typescript";
14+
import {FunctionDeclaration} from "typescript";
1315

1416
export function passReturnStatement(parent: ts.ReturnStatement, ctx: Context, builder: llvm.IRBuilder) {
1517
if (!parent.expression) {
@@ -144,14 +146,49 @@ function buildFromBinaryExpression(
144146
}
145147
}
146148

149+
function buildCalleFromCallExpression(
150+
expr: ts.CallExpression,
151+
ctx: Context,
152+
builder: llvm.IRBuilder
153+
) {
154+
const calleSignature = ctx.typeChecker.getResolvedSignature(expr);
155+
if (calleSignature) {
156+
const symbolDeclaration = <ts.SignatureDeclaration>calleSignature.declaration;
157+
if (symbolDeclaration.name) {
158+
const sourceFile = symbolDeclaration.getSourceFile();
159+
160+
if (sourceFile.fileName === RUNTIME_DEFINITION_FILE) {
161+
return declareFunctionFromDefinition(
162+
<ts.FunctionDeclaration>symbolDeclaration,
163+
ctx,
164+
builder,
165+
CPPMangler
166+
);
167+
}
168+
169+
if (sourceFile.fileName === LANGUAGE_DEFINITION_FILE) {
170+
return declareFunctionFromDefinition(
171+
<ts.FunctionDeclaration>symbolDeclaration,
172+
ctx,
173+
builder,
174+
CMangler
175+
);
176+
}
177+
}
178+
}
179+
180+
return buildFromExpression(expr.expression, ctx, builder);
181+
}
182+
147183
function buildFromCallExpression(
148184
expr: ts.CallExpression,
149185
ctx: Context,
150186
builder: llvm.IRBuilder
151187
) {
152-
const callle = buildFromExpression(expr.expression, ctx, builder);
188+
const callle = buildCalleFromCallExpression(expr, ctx, builder);
153189
if (!callle) {
154-
throw new Error(
190+
throw new UnsupportedError(
191+
expr,
155192
`We cannot prepare expression to call this function, ${expr.expression}`
156193
);
157194
}
@@ -206,38 +243,6 @@ function buildFromIdentifier(identifier: ts.Identifier, ctx: Context, builder: l
206243
return fn;
207244
}
208245

209-
const symbol = ctx.typeChecker.getSymbolAtLocation(identifier);
210-
if (symbol && symbol.declarations.length > 0) {
211-
if (symbol.declarations.length > 1) {
212-
throw new Error(
213-
`Multiple declarations are not supported`
214-
);
215-
}
216-
217-
const symbolDeclaration = <ts.FunctionDeclaration>symbol.declarations[0];
218-
if (symbolDeclaration.name) {
219-
const sourceFile = symbolDeclaration.getSourceFile();
220-
221-
if (sourceFile.fileName === RUNTIME_DEFINITION_FILE) {
222-
return declareFunctionFromDefinition(
223-
symbolDeclaration,
224-
ctx,
225-
builder,
226-
CPPMangler
227-
);
228-
}
229-
230-
if (sourceFile.fileName === LANGUAGE_DEFINITION_FILE) {
231-
return declareFunctionFromDefinition(
232-
symbolDeclaration,
233-
ctx,
234-
builder,
235-
CMangler
236-
);
237-
}
238-
}
239-
}
240-
241246
throw new UnsupportedError(
242247
identifier,
243248
`Unknown Identifier: "${<string>identifier.escapedText}"`

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ try {
7575
execFileSync('llc', [
7676
optimizationLevel,
7777
'-filetype=obj', path.join(outputPath, 'main.bc'),
78-
'-o', path.join(outputPath, 'main.o')
78+
'-o', path.join(outputPath, 'main.o'),
7979
]);
8080
execFileSync("cc", [
8181
optimizationLevel,

0 commit comments

Comments
 (0)