Generics are now compiled only once

This commit is contained in:
Mattia Giambirtone 2022-11-28 16:47:38 +01:00
parent 2187aa19d8
commit 62b8bae0fc
1 changed files with 2 additions and 14 deletions

View File

@ -627,6 +627,7 @@ proc insertAt(self: Compiler, where: int, opcode: OpCode, data: openarray[uint8]
proc compileDecl(self: Compiler, name: Name) =
## Internal resolve() helper
# There's no reason to compile a declaration
# unless it is used at least once: this way
# not only do we save space if a name is declared
@ -640,12 +641,7 @@ proc compileDecl(self: Compiler, name: Name) =
# compile the declaration
case name.kind:
of NameKind.Function:
# Generic functions need to be compiled at
# the call site because we need to know the
# type of the arguments, but regular functions
# can be precompiled as soon as we resolve them
if not name.isGeneric:
self.funDecl(FunDecl(name.node), name)
self.funDecl(FunDecl(name.node), name)
else:
discard
@ -2156,14 +2152,6 @@ proc callExpr(self: Compiler, node: CallExpr): Name {.discardable.} =
of identExpr:
# Calls like hi()
result = self.matchImpl(IdentExpr(node.callee).name.lexeme, Type(kind: Function, returnType: Type(kind: All), args: args), node)
if result.isGeneric:
# We can't instantiate a concrete version
# of a generic function without the types
# of its arguments, so we wait until the
# very last moment to compile it, once
# that info is available to us
result = self.specialize(result, argExpr)
self.funDecl(FunDecl(result.node), result)
# Now we call it
self.generateCall(result, argExpr, node.token.line)
of NodeKind.callExpr: