File: jstypes.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (156 lines) | stat: -rw-r--r-- 6,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#
#
#           The Nim Compiler
#        (c) Copyright 2013 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# included from jsgen.nim

## Type info generation for the JS backend.

proc rope(arg: Int128): Rope = rope($arg)

proc genTypeInfo(p: PProc, typ: PType): Rope
proc genObjectFields(p: PProc, typ: PType, n: PNode): Rope =
  var
    s, u: Rope
    field: PSym
    b: PNode
  result = ""
  case n.kind
  of nkRecList:
    if n.len == 1:
      result = genObjectFields(p, typ, n[0])
    else:
      s = ""
      for i in 0..<n.len:
        if i > 0: s.add(", \L")
        s.add(genObjectFields(p, typ, n[i]))
      result = ("{kind: 2, len: $1, offset: 0, " &
          "typ: null, name: null, sons: [$2]}") % [rope(n.len), s]
  of nkSym:
    field = n.sym
    s = genTypeInfo(p, field.typ)
    result = ("{kind: 1, offset: \"$1\", len: 0, " &
        "typ: $2, name: $3, sons: null}") %
                   [mangleName(p.module, field), s,
                    makeJSString(field.name.s)]
  of nkRecCase:
    if (n[0].kind != nkSym): internalError(p.config, n.info, "genObjectFields")
    field = n[0].sym
    s = genTypeInfo(p, field.typ)
    for i in 1..<n.len:
      b = n[i]           # branch
      u = ""
      case b.kind
      of nkOfBranch:
        if b.len < 2:
          internalError(p.config, b.info, "genObjectFields; nkOfBranch broken")
        for j in 0..<b.len - 1:
          if u != "": u.add(", ")
          if b[j].kind == nkRange:
            u.addf("[$1, $2]", [rope(getOrdValue(b[j][0])),
                                 rope(getOrdValue(b[j][1]))])
          else:
            u.add(rope(getOrdValue(b[j])))
      of nkElse:
        u = rope(lengthOrd(p.config, field.typ))
      else: internalError(p.config, n.info, "genObjectFields(nkRecCase)")
      if result != "": result.add(", \L")
      result.addf("[setConstr($1), $2]",
           [u, genObjectFields(p, typ, lastSon(b))])
    result = ("{kind: 3, offset: \"$1\", len: $3, " &
        "typ: $2, name: $4, sons: [$5]}") % [
        mangleName(p.module, field), s,
        rope(lengthOrd(p.config, field.typ)), makeJSString(field.name.s), result]
  else: internalError(p.config, n.info, "genObjectFields")

proc objHasTypeField(t: PType): bool {.inline.} =
  tfInheritable in t.flags or t.baseClass != nil

proc genObjectInfo(p: PProc, typ: PType, name: Rope) =
  let kind = if objHasTypeField(typ): tyObject else: tyTuple
  var s = ("var $1 = {size: 0, kind: $2, base: null, node: null, " &
           "finalizer: null};$n") % [name, rope(ord(kind))]
  prepend(p.g.typeInfo, s)
  p.g.typeInfo.addf("var NNI$1 = $2;$n",
       [rope(typ.id), genObjectFields(p, typ, typ.n)])
  p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, rope(typ.id)])
  if (typ.kind == tyObject) and (typ.baseClass != nil):
    p.g.typeInfo.addf("$1.base = $2;$n",
         [name, genTypeInfo(p, typ.baseClass.skipTypes(skipPtrs))])

proc genTupleFields(p: PProc, typ: PType): Rope =
  var s: Rope = ""
  for i in 0..<typ.len:
    if i > 0: s.add(", \L")
    s.addf("{kind: 1, offset: \"Field$1\", len: 0, " &
           "typ: $2, name: \"Field$1\", sons: null}",
           [i.rope, genTypeInfo(p, typ[i])])
  result = ("{kind: 2, len: $1, offset: 0, " &
            "typ: null, name: null, sons: [$2]}") % [rope(typ.len), s]

proc genTupleInfo(p: PProc, typ: PType, name: Rope) =
  var s = ("var $1 = {size: 0, kind: $2, base: null, node: null, " &
           "finalizer: null};$n") % [name, rope(ord(typ.kind))]
  prepend(p.g.typeInfo, s)
  p.g.typeInfo.addf("var NNI$1 = $2;$n",
       [rope(typ.id), genTupleFields(p, typ)])
  p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, rope(typ.id)])

proc genEnumInfo(p: PProc, typ: PType, name: Rope) =
  var s: Rope = ""
  for i in 0..<typ.n.len:
    if (typ.n[i].kind != nkSym): internalError(p.config, typ.n.info, "genEnumInfo")
    let field = typ.n[i].sym
    if i > 0: s.add(", \L")
    let extName = if field.ast == nil: field.name.s else: field.ast.strVal
    s.addf("\"$1\": {kind: 1, offset: $1, typ: $2, name: $3, len: 0, sons: null}",
         [rope(field.position), name, makeJSString(extName)])
  var n = ("var NNI$1 = {kind: 2, offset: 0, typ: null, " &
      "name: null, len: $2, sons: {$3}};$n") % [rope(typ.id), rope(typ.n.len), s]
  s = ("var $1 = {size: 0, kind: $2, base: null, node: null, " &
       "finalizer: null};$n") % [name, rope(ord(typ.kind))]
  prepend(p.g.typeInfo, s)
  p.g.typeInfo.add(n)
  p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, rope(typ.id)])
  if typ.baseClass != nil:
    p.g.typeInfo.addf("$1.base = $2;$n",
         [name, genTypeInfo(p, typ.baseClass)])

proc genTypeInfo(p: PProc, typ: PType): Rope =
  let t = typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink, tyOwned})
  result = "NTI$1" % [rope(t.id)]
  if containsOrIncl(p.g.typeInfoGenerated, t.id): return
  case t.kind
  of tyDistinct:
    result = genTypeInfo(p, t.skipModifier)
  of tyPointer, tyProc, tyBool, tyChar, tyCstring, tyString, tyInt..tyUInt64:
    var s =
      "var $1 = {size: 0,kind: $2,base: null,node: null,finalizer: null};$n" %
      [result, rope(ord(t.kind))]
    prepend(p.g.typeInfo, s)
  of tyVar, tyLent, tyRef, tyPtr, tySequence, tyRange, tySet, tyOpenArray:
    var s =
      "var $1 = {size: 0, kind: $2, base: null, node: null, finalizer: null};$n" %
              [result, rope(ord(t.kind))]
    prepend(p.g.typeInfo, s)
    p.g.typeInfo.addf("$1.base = $2;$n",
         [result, genTypeInfo(p, t.elementType)])
  of tyArray:
    var s =
      "var $1 = {size: 0, kind: $2, base: null, node: null, finalizer: null};$n" %
              [result, rope(ord(t.kind))]
    prepend(p.g.typeInfo, s)
    p.g.typeInfo.addf("$1.base = $2;$n",
         [result, genTypeInfo(p, t.elementType)])
  of tyEnum: genEnumInfo(p, t, result)
  of tyObject: genObjectInfo(p, t, result)
  of tyTuple: genTupleInfo(p, t, result)
  of tyStatic:
    if t.n != nil: result = genTypeInfo(p, skipModifier t)
    else: internalError(p.config, "genTypeInfo(" & $t.kind & ')')
  else: internalError(p.config, "genTypeInfo(" & $t.kind & ')')