Skip to content

Commit

Permalink
Merge branch 'master' into pr_tuple_nimony
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Jan 7, 2025
2 parents bd3958e + 4ad773b commit a6e4636
Show file tree
Hide file tree
Showing 25 changed files with 353 additions and 294 deletions.
4 changes: 3 additions & 1 deletion src/lib/nifbuilder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ template addLineIgnoreZero*(b: var string; x: int32) =
addLine b, x

proc addLineInfo*(b: var Builder; col, line: int32; file = "") =
addSep b
var format = LineInfoNone
if col != 0'i32:
format = LineInfoCol
Expand All @@ -224,12 +223,15 @@ proc addLineInfo*(b: var Builder; col, line: int32; file = "") =
drainPending b
case format
of LineInfoCol:
addSep b
b.buf.addLine col
of LineInfoColLine:
addSep b
b.buf.addLineIgnoreZero col
b.buf.add ','
b.buf.addLine line
of LineInfoFile:
addSep b
b.buf.addLineIgnoreZero col
b.buf.add ','
b.buf.addLineIgnoreZero line
Expand Down
45 changes: 35 additions & 10 deletions src/nimony/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,9 @@ proc buildCallSource(buf: var TokenBuf; cs: CallState) =
buf.addSubtree cs.args[valueIndex].n
buf.addParRi()

proc semReturnType(c: var SemContext; n: var Cursor): TypeCursor =
result = semLocalType(c, n, InReturnTypeDecl)

proc resolveOverloads(c: var SemContext; it: var Item; cs: var CallState) =
let genericArgs =
if cs.hasGenericArgs: cursorAt(cs.genericDest, 0)
Expand Down Expand Up @@ -1209,7 +1212,12 @@ proc resolveOverloads(c: var SemContext; it: var Item; cs: var CallState) =
var matched = m[idx]
let inst = c.requestRoutineInstance(finalFn.sym, matched.typeArgs, matched.inferred, cs.callNode.info)
c.dest[cs.beforeCall+1].setSymId inst.targetSym
typeofCallIs c, it, cs.beforeCall, inst.returnType
var instReturnType = createTokenBuf(16)
swap c.dest, instReturnType
var subsReturnType = inst.returnType
let returnType = semReturnType(c, subsReturnType)
swap c.dest, instReturnType
typeofCallIs c, it, cs.beforeCall, returnType
else:
typeofCallIs c, it, cs.beforeCall, m[idx].returnType

Expand Down Expand Up @@ -1871,8 +1879,8 @@ proc semConceptType(c: var SemContext; n: var Cursor) =
wantParRi c, n
wantParRi c, n

proc instGenericType(c: var SemContext; dest: var TokenBuf; info: PackedLineInfo;
origin, targetSym: SymId; decl: TypeDecl; args: Cursor) =
proc subsGenericTypeFromArgs(c: var SemContext; dest: var TokenBuf; info: PackedLineInfo;
origin, targetSym: SymId; decl: TypeDecl; args: Cursor) =
#[
What we need to do is rather simple: A generic instantiation is
the typical (type :Name ex generic_params pragmas body) tuple but
Expand Down Expand Up @@ -1996,9 +2004,12 @@ proc semInvoke(c: var SemContext; n: var Cursor) =
# we have to be eager in generic type instantiations so that type-checking
# can do its job properly:
let key = typeToCanon(c.dest, typeStart)
var sym = Sym(kind: TypeY, name: SymId(0), pos: InvalidPos) # pos unused by semTypeSym
if c.instantiatedTypes.hasKey(key):
let cachedSym = c.instantiatedTypes[key]
c.dest.shrink typeStart
c.dest.add symToken(c.instantiatedTypes[key], info)
c.dest.add symToken(cachedSym, info)
sym.name = cachedSym
else:
var args = cursorAt(c.dest, beforeArgs)
if magicKind != NoType:
Expand Down Expand Up @@ -2047,13 +2058,30 @@ proc semInvoke(c: var SemContext; n: var Cursor) =
semLocalTypeImpl c, m, InLocalDecl
return
let targetSym = newSymId(c, headId)
var instance = createTokenBuf(30)
instGenericType c, instance, info, headId, targetSym, decl, args
c.instantiatedTypes[key] = targetSym
var sub = createTokenBuf(30)
subsGenericTypeFromArgs c, sub, info, headId, targetSym, decl, args
c.dest.endRead()
var phase = SemcheckTopLevelSyms
var topLevel = createTokenBuf(30)
swap c.phase, phase
swap c.dest, topLevel
var tn = beginRead(sub)
semTypeSection c, tn
swap c.dest, topLevel
c.phase = SemcheckSignatures
var instance = createTokenBuf(30)
swap c.dest, instance
tn = beginRead(topLevel)
semTypeSection c, tn
swap c.dest, instance
swap c.phase, phase
publish targetSym, ensureMove instance
c.instantiatedTypes[key] = targetSym
c.dest.shrink typeStart
c.dest.add symToken(targetSym, info)
sym.name = targetSym
assert sym.name != SymId(0)
semTypeSym c, sym, info, typeStart, InLocalDecl

proc addVarargsParameter(c: var SemContext; paramsAt: int; info: PackedLineInfo) =
const vanon = "vanon"
Expand Down Expand Up @@ -2349,9 +2377,6 @@ proc semLocalTypeImpl(c: var SemContext; n: var Cursor; context: TypeDeclContext
c.buildErr info, "not a type", n
inc n

proc semReturnType(c: var SemContext; n: var Cursor): TypeCursor =
result = semLocalType(c, n, InReturnTypeDecl)

proc exportMarkerBecomesNifTag(c: var SemContext; insertPos: int; crucial: CrucialPragma) =
assert crucial.magic.len > 0
let info = c.dest[insertPos].info
Expand Down
15 changes: 14 additions & 1 deletion src/nimony/typenav.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ proc getTypeImpl(c: var TypeCache; n: Cursor): Cursor =
result = c.builtins.autoType # to indicate error
case exprKind(n)
of NoExpr:
if n.kind == Symbol:
case n.kind
of Symbol:
let res = tryLoadSym(n.symId)
if res.status == LacksNothing:
let local = asLocal(res.decl)
Expand All @@ -40,6 +41,18 @@ proc getTypeImpl(c: var TypeCache; n: Cursor): Cursor =
result = res.decl
else:
quit "gear3:could not find symbol: " & pool.syms[n.symId]
of IntLit:
result = c.builtins.intType
of UintLit:
result = c.builtins.uintType
of CharLit:
result = c.builtins.charType
of FloatLit:
result = c.builtins.floatType
of StringLit:
result = c.builtins.stringType
else:
discard
of AtX, PatX, ArrAtX:
result = getTypeImpl(c, n.firstSon)
case typeKind(result)
Expand Down
14 changes: 7 additions & 7 deletions tests/nimony/basics/t2.nif
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(.nif24)
,2,tests/nimony/basics/t2.nim(stmts 2,1
(type :int.0.t277k7ei1
(type :int.0.t277k7ei1
(i -1) . 5
(pragmas 2
(magic 7 Int)) .) 2,3
(type :float.0.t277k7ei1
(type :float.0.t277k7ei1
(f +64) . 7
(pragmas 2
(magic 7 Float)) .) 2,4
(type :string.0.t277k7ei1
(type :string.0.t277k7ei1
(string) . 8
(pragmas 2
(magic 7 String)) .) ,7
(proc 5 :\2B.0.t277k7ei1
(proc 5 :\2B.0.t277k7ei1
(add -3) . . 9
(params 1
(param :x.0 . . ~8,~6
Expand Down Expand Up @@ -53,9 +53,9 @@
(param :x.2 . . ~14,~17
(i -1) .)) 2,~17
(i -1) . . 31
(stmts
(stmts
(result :result.1 . . ~29,~17
(i -1) .)
(asgn result.1
(i -1) .)
(asgn result.1
(expr x.2)) ~31
(ret result.1))))
2 changes: 1 addition & 1 deletion tests/nimony/basics/tbinarytree.nif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(.nif24)
,1,tests/nimony/basics/tbinarytree.nim(stmts 5
(type :int.0.tbivahh0m
(type :int.0.tbivahh0m
(i -1) . 5
(pragmas 2
(magic 7 Int)) .) 10,3
Expand Down
36 changes: 18 additions & 18 deletions tests/nimony/basics/tbool.nif
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
(.nif24)
,1,tests/nimony/basics/tbool.nim(stmts 26
(type ~21 :bool.0.tbojn1uzv
(type ~21 :bool.0.tbojn1uzv
(bool) . ~16
(pragmas 2
(magic 7 Bool)) 2
(enum
(u +8) ~18,1
(efld ~8 :false.0.tbojn1uzv
(false) .
(bool)
(efld ~8 :false.0.tbojn1uzv
(false) .
(bool)
(tup +0 "false")) ~8,1
(efld ~7 :true.0.tbojn1uzv
(true) .
(bool)
(efld ~7 :true.0.tbojn1uzv
(true) .
(bool)
(tup +1 "true")))) 5
(proc :dollar`.bool.0.tbojn1uzv x . .
(params
(proc :dollar`.bool.0.tbojn1uzv x . .
(params
(param :e.0 . . 21
(bool) .))
(string) . .
(string) . .
(stmts 23
(result :result.0 . .
(string) .) 23
(case e.0 ~18,1
(of
(set
(false))
(stmts
(of
(set
(false))
(stmts
(ret "false"))) ~8,1
(of
(set
(true))
(stmts
(of
(set
(true))
(stmts
(ret "true"))))
(ret result.0))) 4,3
(let :x.0.tbojn1uzv . . 22,~3
Expand Down
16 changes: 8 additions & 8 deletions tests/nimony/basics/tcstring.nif
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
(.nif24)
,2,tests/nimony/basics/tcstring.nim(stmts 2,1
(type :int.0.tcs1re56a
(type :int.0.tcs1re56a
(i -1) . 5
(pragmas 2
(magic 7 Int)) .) 2,2
(type :bool.0.tcs1re56a
(type :bool.0.tcs1re56a
(bool) . 6
(pragmas 2
(magic 7 Bool)) .) 2,4
(type :cstring.0.tcs1re56a
(type :cstring.0.tcs1re56a
(cstring) . 9
(pragmas 2
(magic 7 Cstring)) .) 2,5
(type :pointer.0.tcs1re56a
(type :pointer.0.tcs1re56a
(pointer) . 9
(pragmas 2
(magic 7 Pointer)) .) ,8
(proc 5 :\2B.0.tcs1re56a
(proc 5 :\2B.0.tcs1re56a
(add -3) . . 9
(params 1
(param :x.0 . . ~8,~7
Expand All @@ -26,7 +26,7 @@
(i -1) 26
(pragmas 2
(magic 7 "AddI")) . .) ,9
(proc 5 :\2D.0.tcs1re56a
(proc 5 :\2D.0.tcs1re56a
(sub -3) . . 9
(params 1
(param :x.1 . . ~8,~8
Expand All @@ -36,7 +36,7 @@
(i -1) 26
(pragmas 2
(magic 7 "SubI")) . .) ,11
(proc 5 :<=.0.tcs1re56a
(proc 5 :<=.0.tcs1re56a
(le -3) . . 10
(params 1
(param :x.2 . . ~9,~10
Expand All @@ -56,7 +56,7 @@
(const :myconst.0.tcs1re56a . . ,~14
(cstring) 17
(conv ~17,~14
(cstring)
(cstring)
(suf "abc" "R"))) 4,20
(var :zz.0.tcs1re56a . . ~2,~16
(cstring)
Expand Down
4 changes: 2 additions & 2 deletions tests/nimony/basics/tdefinedarg.nif
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(.nif24)
,1,tests/nimony/basics/tdefinedarg.nim(stmts 5
(type :untyped.0.tdetxkbxo
(type :untyped.0.tdetxkbxo
(untyped) . 9
(pragmas 2
(magic 7 Expr)) .) ,2
(proc 5 :defined.0.tdetxkbxo
(proc 5 :defined.0.tdetxkbxo
(defined) . . 12
(params 1
(param :x.0 . . ~8,~2
Expand Down
26 changes: 13 additions & 13 deletions tests/nimony/basics/tdistinct.nif
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
(.nif24)
,2,tests/nimony/basics/tdistinct.nim(stmts 2,1
(type :int.0.tdikoa9tc1
(type :int.0.tdikoa9tc1
(i -1) . 5
(pragmas 2
(magic 7 Int)) .) 2,2
(type :bool.0.tdikoa9tc1
(type :bool.0.tdikoa9tc1
(bool) . 6
(pragmas 2
(magic 7 Bool)) .) 2,3
(type :char.0.tdikoa9tc1
(type :char.0.tdikoa9tc1
(c +8) . 6
(pragmas 2
(magic 7 Char)) .) 2,4
(type :string.0.tdikoa9tc1
(type :string.0.tdikoa9tc1
(string) . 8
(pragmas 2
(magic 7 String)) .) 2,6
(type :int8.0.tdikoa9tc1
(type :int8.0.tdikoa9tc1
(i +8) . 6
(pragmas 2
(magic 7 Int8)) .) ,8
(proc 5 :\2B.0.tdikoa9tc1
(proc 5 :\2B.0.tdikoa9tc1
(add -3) . . 9
(params 1
(param :x.0 . . ~8,~7
Expand All @@ -30,7 +30,7 @@
(i -1) 26
(pragmas 2
(magic 7 "AddI")) . .) ,9
(proc 5 :\2D.0.tdikoa9tc1
(proc 5 :\2D.0.tdikoa9tc1
(sub -3) . . 9
(params 1
(param :x.1 . . ~8,~8
Expand All @@ -40,7 +40,7 @@
(i -1) 26
(pragmas 2
(magic 7 "SubI")) . .) ,11
(proc 5 :<=.0.tdikoa9tc1
(proc 5 :<=.0.tdikoa9tc1
(le -3) . . 10
(params 1
(param :x.2 . . ~9,~10
Expand All @@ -59,13 +59,13 @@
(param :y.3 . . 3 VarId.0.tdikoa9tc1 .)) 15 VarId.0.tdikoa9tc1 30
(pragmas 2
(borrow)) . 9
(stmts
(ret
(dconv 6 VarId.0.tdikoa9tc1
(stmts
(ret
(dconv 6 VarId.0.tdikoa9tc1
(add ~7,~16
(i -1)
(i -1)
(dconv ~7,~16
(i -1) x.3)
(i -1) x.3)
(dconv ~7,~16
(i -1) y.3)))))) 4,24
(var :x.0.tdikoa9tc1 . . 3 VarId.0.tdikoa9tc1 .) 2,26
Expand Down
Loading

0 comments on commit a6e4636

Please sign in to comment.