@@ -10,6 +10,8 @@ import {RUNTIME_DEFINITION_FILE} from "@static-script/runtime";
1010import { LANGUAGE_DEFINITION_FILE } from "../../constants" ;
1111import { CMangler } from "./c.mangler" ;
1212import { ManglerInterface } from "./mangler.interface" ;
13+ import { SignatureDeclaration } from "typescript" ;
14+ import { FunctionDeclaration } from "typescript" ;
1315
1416export 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+
147183function 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 } "`
0 commit comments