Improve the typeinfo module (#17625)
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
This commit is contained in:
parent
ca405167ed
commit
b9c94f22aa
1 changed files with 129 additions and 122 deletions
|
|
@ -9,17 +9,30 @@
|
||||||
|
|
||||||
## This module implements an interface to Nim's `runtime type information`:idx:
|
## This module implements an interface to Nim's `runtime type information`:idx:
|
||||||
## (`RTTI`:idx:). See the `marshal <marshal.html>`_ module for an example of
|
## (`RTTI`:idx:). See the `marshal <marshal.html>`_ module for an example of
|
||||||
## what this module allows you to do.
|
## what this allows you to do.
|
||||||
##
|
##
|
||||||
## Note that even though `Any` and its operations hide the nasty low level
|
## ..note:: Even though `Any` and its operations hide the nasty low level
|
||||||
## details from its clients, it remains inherently unsafe! Also, Nim's
|
## details from its users, it remains inherently unsafe! Also, Nim's
|
||||||
## runtime type information will evolve and may eventually be deprecated.
|
## runtime type information will evolve and may eventually be deprecated.
|
||||||
## As an alternative approach to programmatically understanding and
|
## As an alternative approach to programmatically understanding and
|
||||||
## manipulating types, consider using the `macros <macros.html>`_ package to
|
## manipulating types, consider using the `macros <macros.html>`_ module to
|
||||||
## work with the types' AST representation at compile time. See, for example,
|
## work with the types' AST representation at compile time. See for example
|
||||||
## the `getTypeImpl proc <macros.html#getTypeImpl,NimNode>`_. As an alternative
|
## the `getTypeImpl proc <macros.html#getTypeImpl,NimNode>`_. As an alternative
|
||||||
## approach to storing arbitrary types at runtime, consider using generics.
|
## approach to storing arbitrary types at runtime, consider using generics.
|
||||||
|
|
||||||
|
runnableExamples:
|
||||||
|
var x: Any
|
||||||
|
|
||||||
|
var i = 42
|
||||||
|
x = i.toAny
|
||||||
|
assert x.kind == akInt
|
||||||
|
assert x.getInt == 42
|
||||||
|
|
||||||
|
var s = @[1, 2, 3]
|
||||||
|
x = s.toAny
|
||||||
|
assert x.kind == akSequence
|
||||||
|
assert x.len == 3
|
||||||
|
|
||||||
{.push hints: off.}
|
{.push hints: off.}
|
||||||
|
|
||||||
include "system/inclrtl.nim"
|
include "system/inclrtl.nim"
|
||||||
|
|
@ -28,44 +41,45 @@ include "system/hti.nim"
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
type
|
type
|
||||||
AnyKind* = enum ## what kind of `any` it is
|
AnyKind* = enum ## The kind of `Any`.
|
||||||
akNone = 0, ## invalid any
|
akNone = 0, ## invalid
|
||||||
akBool = 1, ## any represents a `bool`
|
akBool = 1, ## bool
|
||||||
akChar = 2, ## any represents a `char`
|
akChar = 2, ## char
|
||||||
akEnum = 14, ## any represents an enum
|
akEnum = 14, ## enum
|
||||||
akArray = 16, ## any represents an array
|
akArray = 16, ## array
|
||||||
akObject = 17, ## any represents an object
|
akObject = 17, ## object
|
||||||
akTuple = 18, ## any represents a tuple
|
akTuple = 18, ## tuple
|
||||||
akSet = 19, ## any represents a set
|
akSet = 19, ## set
|
||||||
akRange = 20, ## any represents a range
|
akRange = 20, ## range
|
||||||
akPtr = 21, ## any represents a ptr
|
akPtr = 21, ## ptr
|
||||||
akRef = 22, ## any represents a ref
|
akRef = 22, ## ref
|
||||||
akSequence = 24, ## any represents a sequence
|
akSequence = 24, ## sequence
|
||||||
akProc = 25, ## any represents a proc
|
akProc = 25, ## proc
|
||||||
akPointer = 26, ## any represents a pointer
|
akPointer = 26, ## pointer
|
||||||
akString = 28, ## any represents a string
|
akString = 28, ## string
|
||||||
akCString = 29, ## any represents a cstring
|
akCString = 29, ## cstring
|
||||||
akInt = 31, ## any represents an int
|
akInt = 31, ## int
|
||||||
akInt8 = 32, ## any represents an int8
|
akInt8 = 32, ## int8
|
||||||
akInt16 = 33, ## any represents an int16
|
akInt16 = 33, ## int16
|
||||||
akInt32 = 34, ## any represents an int32
|
akInt32 = 34, ## int32
|
||||||
akInt64 = 35, ## any represents an int64
|
akInt64 = 35, ## int64
|
||||||
akFloat = 36, ## any represents a float
|
akFloat = 36, ## float
|
||||||
akFloat32 = 37, ## any represents a float32
|
akFloat32 = 37, ## float32
|
||||||
akFloat64 = 38, ## any represents a float64
|
akFloat64 = 38, ## float64
|
||||||
akFloat128 = 39, ## any represents a float128
|
akFloat128 = 39, ## float128
|
||||||
akUInt = 40, ## any represents an unsigned int
|
akUInt = 40, ## uint
|
||||||
akUInt8 = 41, ## any represents an unsigned int8
|
akUInt8 = 41, ## uint8
|
||||||
akUInt16 = 42, ## any represents an unsigned in16
|
akUInt16 = 42, ## uin16
|
||||||
akUInt32 = 43, ## any represents an unsigned int32
|
akUInt32 = 43, ## uint32
|
||||||
akUInt64 = 44, ## any represents an unsigned int64
|
akUInt64 = 44, ## uint64
|
||||||
# akOpt = 44+18 ## the builtin 'opt' type.
|
# akOpt = 44+18 ## the builtin 'opt' type.
|
||||||
|
|
||||||
Any* = object ## can represent any nim value; NOTE: the wrapped
|
Any* = object
|
||||||
## value can be modified with its wrapper! This means
|
## A type that can represent any nim value.
|
||||||
## that `Any` keeps a non-traced pointer to its
|
##
|
||||||
## wrapped value and **must not** live longer than
|
## .. danger:: The wrapped value can be modified with its wrapper! This means
|
||||||
## its wrapped value.
|
## that `Any` keeps a non-traced pointer to its wrapped value and
|
||||||
|
## **must not** live longer than its wrapped value.
|
||||||
value: pointer
|
value: pointer
|
||||||
when defined(js):
|
when defined(js):
|
||||||
rawType: PNimType
|
rawType: PNimType
|
||||||
|
|
@ -84,9 +98,9 @@ when not defined(gcDestructors):
|
||||||
PGenSeq = ptr TGenericSeq
|
PGenSeq = ptr TGenericSeq
|
||||||
|
|
||||||
when defined(gogc):
|
when defined(gogc):
|
||||||
const GenericSeqSize = (3 * sizeof(int))
|
const GenericSeqSize = 3 * sizeof(int)
|
||||||
else:
|
else:
|
||||||
const GenericSeqSize = (2 * sizeof(int))
|
const GenericSeqSize = 2 * sizeof(int)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
include system/seqs_v2_reimpl
|
include system/seqs_v2_reimpl
|
||||||
|
|
@ -103,8 +117,7 @@ when not defined(js):
|
||||||
proc genericAssign(dest, src: pointer, mt: PNimType) {.importCompilerProc.}
|
proc genericAssign(dest, src: pointer, mt: PNimType) {.importCompilerProc.}
|
||||||
|
|
||||||
when not defined(gcDestructors):
|
when not defined(gcDestructors):
|
||||||
proc genericShallowAssign(dest, src: pointer, mt: PNimType) {.
|
proc genericShallowAssign(dest, src: pointer, mt: PNimType) {.importCompilerProc.}
|
||||||
importCompilerProc.}
|
|
||||||
proc incrSeq(seq: PGenSeq, elemSize, elemAlign: int): PGenSeq {.importCompilerProc.}
|
proc incrSeq(seq: PGenSeq, elemSize, elemAlign: int): PGenSeq {.importCompilerProc.}
|
||||||
proc newObj(typ: PNimType, size: int): pointer {.importCompilerProc.}
|
proc newObj(typ: PNimType, size: int): pointer {.importCompilerProc.}
|
||||||
proc newSeq(typ: PNimType, len: int): pointer {.importCompilerProc.}
|
proc newSeq(typ: PNimType, len: int): pointer {.importCompilerProc.}
|
||||||
|
|
@ -120,7 +133,7 @@ template `+!!`(a, b): untyped = cast[pointer](cast[ByteAddress](a) + b)
|
||||||
proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
|
proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
|
||||||
assert(n.kind == nkCase)
|
assert(n.kind == nkCase)
|
||||||
var d: int
|
var d: int
|
||||||
var a = cast[ByteAddress](aa)
|
let a = cast[ByteAddress](aa)
|
||||||
case n.typ.size
|
case n.typ.size
|
||||||
of 1: d = ze(cast[ptr int8](a +% n.offset)[])
|
of 1: d = ze(cast[ptr int8](a +% n.offset)[])
|
||||||
of 2: d = ze(cast[ptr int16](a +% n.offset)[])
|
of 2: d = ze(cast[ptr int16](a +% n.offset)[])
|
||||||
|
|
@ -130,7 +143,7 @@ proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
|
||||||
return d
|
return d
|
||||||
|
|
||||||
proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode =
|
proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode =
|
||||||
var discr = getDiscriminant(aa, n)
|
let discr = getDiscriminant(aa, n)
|
||||||
if discr <% n.len:
|
if discr <% n.len:
|
||||||
result = n.sons[discr]
|
result = n.sons[discr]
|
||||||
if result == nil: result = n.sons[n.len]
|
if result == nil: result = n.sons[n.len]
|
||||||
|
|
@ -144,17 +157,17 @@ proc newAny(value: pointer, rawType: PNimType): Any {.inline.} =
|
||||||
|
|
||||||
when declared(system.VarSlot):
|
when declared(system.VarSlot):
|
||||||
proc toAny*(x: VarSlot): Any {.inline.} =
|
proc toAny*(x: VarSlot): Any {.inline.} =
|
||||||
## Constructs a `Any` object from a variable slot `x`.
|
## Constructs an `Any` object from a variable slot `x`.
|
||||||
## This captures `x`'s address, so `x` can be modified with its
|
## This captures `x`'s address, so `x` can be modified with its
|
||||||
## `Any` wrapper! The client needs to ensure that the wrapper
|
## `Any` wrapper! The caller needs to ensure that the wrapper
|
||||||
## **does not** live longer than `x`!
|
## **does not** live longer than `x`!
|
||||||
## This is provided for easier reflection capabilities of a debugger.
|
## This is provided for easier reflection capabilities of a debugger.
|
||||||
result.value = x.address
|
result.value = x.address
|
||||||
result.rawType = x.typ
|
result.rawType = x.typ
|
||||||
|
|
||||||
proc toAny*[T](x: var T): Any {.inline.} =
|
proc toAny*[T](x: var T): Any {.inline.} =
|
||||||
## Constructs a `Any` object from `x`. This captures `x`'s address, so
|
## Constructs an `Any` object from `x`. This captures `x`'s address, so
|
||||||
## `x` can be modified with its `Any` wrapper! The client needs to ensure
|
## `x` can be modified with its `Any` wrapper! The caller needs to ensure
|
||||||
## that the wrapper **does not** live longer than `x`!
|
## that the wrapper **does not** live longer than `x`!
|
||||||
newAny(addr(x), cast[PNimType](getTypeInfo(x)))
|
newAny(addr(x), cast[PNimType](getTypeInfo(x)))
|
||||||
|
|
||||||
|
|
@ -167,12 +180,12 @@ proc size*(x: Any): int {.inline.} =
|
||||||
result = x.rawType.size
|
result = x.rawType.size
|
||||||
|
|
||||||
proc baseTypeKind*(x: Any): AnyKind {.inline.} =
|
proc baseTypeKind*(x: Any): AnyKind {.inline.} =
|
||||||
## Gets the base type's kind; `akNone` is returned if `x` has no base type.
|
## Gets the base type's kind. If `x` has no base type, `akNone` is returned.
|
||||||
if x.rawType.base != nil:
|
if x.rawType.base != nil:
|
||||||
result = AnyKind(ord(x.rawType.base.kind))
|
result = AnyKind(ord(x.rawType.base.kind))
|
||||||
|
|
||||||
proc baseTypeSize*(x: Any): int {.inline.} =
|
proc baseTypeSize*(x: Any): int {.inline.} =
|
||||||
## Returns the size of `x`'s basetype.
|
## Returns the size of `x`'s base type. If `x` has no base type, 0 is returned.
|
||||||
if x.rawType.base != nil:
|
if x.rawType.base != nil:
|
||||||
result = x.rawType.base.size
|
result = x.rawType.base.size
|
||||||
|
|
||||||
|
|
@ -301,33 +314,32 @@ proc len*(x: Any): int =
|
||||||
|
|
||||||
|
|
||||||
proc base*(x: Any): Any =
|
proc base*(x: Any): Any =
|
||||||
## Returns base Any (useful for inherited object types).
|
## Returns the base type of `x` (useful for inherited object types).
|
||||||
result.rawType = x.rawType.base
|
result.rawType = x.rawType.base
|
||||||
result.value = x.value
|
result.value = x.value
|
||||||
|
|
||||||
|
|
||||||
proc isNil*(x: Any): bool =
|
proc isNil*(x: Any): bool =
|
||||||
## `isNil` for an any `x` that represents a cstring, proc or
|
## `isNil` for an `x` that represents a cstring, proc or
|
||||||
## some pointer type.
|
## some pointer type.
|
||||||
assert x.rawType.kind in {tyCString, tyRef, tyPtr, tyPointer, tyProc}
|
assert x.rawType.kind in {tyCString, tyRef, tyPtr, tyPointer, tyProc}
|
||||||
result = isNil(cast[ppointer](x.value)[])
|
result = isNil(cast[ppointer](x.value)[])
|
||||||
|
|
||||||
const
|
const pointerLike =
|
||||||
pointerLike = when defined(gcDestructors): {tyCString, tyRef, tyPtr, tyPointer, tyProc}
|
when defined(gcDestructors): {tyCString, tyRef, tyPtr, tyPointer, tyProc}
|
||||||
else: {tyString, tyCString, tyRef, tyPtr, tyPointer,
|
else: {tyString, tyCString, tyRef, tyPtr, tyPointer, tySequence, tyProc}
|
||||||
tySequence, tyProc}
|
|
||||||
|
|
||||||
proc getPointer*(x: Any): pointer =
|
proc getPointer*(x: Any): pointer =
|
||||||
## Retrieves the pointer value out of `x`. `x` needs to be of kind
|
## Retrieves the pointer value out of `x`. `x` needs to be of kind
|
||||||
## `akString`, `akCString`, `akProc`, `akRef`, `akPtr`,
|
## `akString`, `akCString`, `akProc`, `akRef`, `akPtr`,
|
||||||
## `akPointer`, `akSequence`.
|
## `akPointer` or `akSequence`.
|
||||||
assert x.rawType.kind in pointerLike
|
assert x.rawType.kind in pointerLike
|
||||||
result = cast[ppointer](x.value)[]
|
result = cast[ppointer](x.value)[]
|
||||||
|
|
||||||
proc setPointer*(x: Any, y: pointer) =
|
proc setPointer*(x: Any, y: pointer) =
|
||||||
## Sets the pointer value of `x`. `x` needs to be of kind
|
## Sets the pointer value of `x`. `x` needs to be of kind
|
||||||
## `akString`, `akCString`, `akProc`, `akRef`, `akPtr`,
|
## `akString`, `akCString`, `akProc`, `akRef`, `akPtr`,
|
||||||
## `akPointer`, `akSequence`.
|
## `akPointer` or `akSequence`.
|
||||||
assert x.rawType.kind in pointerLike
|
assert x.rawType.kind in pointerLike
|
||||||
if y != nil and x.rawType.kind != tyPointer:
|
if y != nil and x.rawType.kind != tyPointer:
|
||||||
genericAssign(x.value, y, x.rawType)
|
genericAssign(x.value, y, x.rawType)
|
||||||
|
|
@ -349,15 +361,15 @@ proc fieldsAux(p: pointer, n: ptr TNimNode,
|
||||||
if m != nil: fieldsAux(p, m, ret)
|
if m != nil: fieldsAux(p, m, ret)
|
||||||
|
|
||||||
iterator fields*(x: Any): tuple[name: string, any: Any] =
|
iterator fields*(x: Any): tuple[name: string, any: Any] =
|
||||||
## Iterates over every active field of the any `x` that represents an object
|
## Iterates over every active field of `x`. `x` needs to represent an object
|
||||||
## or a tuple.
|
## or a tuple.
|
||||||
assert x.rawType.kind in {tyTuple, tyObject}
|
assert x.rawType.kind in {tyTuple, tyObject}
|
||||||
var p = x.value
|
let p = x.value
|
||||||
var t = x.rawType
|
var t = x.rawType
|
||||||
# XXX BUG: does not work yet, however is questionable anyway
|
# XXX BUG: does not work yet, however is questionable anyway
|
||||||
when false:
|
when false:
|
||||||
if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
|
if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
|
||||||
var ret: seq[tuple[name: cstring, any: Any]] = @[]
|
var ret: seq[tuple[name: cstring, any: Any]]
|
||||||
if t.kind == tyObject:
|
if t.kind == tyObject:
|
||||||
while true:
|
while true:
|
||||||
fieldsAux(p, t.node, ret)
|
fieldsAux(p, t.node, ret)
|
||||||
|
|
@ -368,8 +380,7 @@ iterator fields*(x: Any): tuple[name: string, any: Any] =
|
||||||
for name, any in items(ret):
|
for name, any in items(ret):
|
||||||
yield ($name, any)
|
yield ($name, any)
|
||||||
|
|
||||||
proc getFieldNode(p: pointer, n: ptr TNimNode,
|
proc getFieldNode(p: pointer, n: ptr TNimNode, name: cstring): ptr TNimNode =
|
||||||
name: cstring): ptr TNimNode =
|
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkNone: assert(false)
|
of nkNone: assert(false)
|
||||||
of nkSlot:
|
of nkSlot:
|
||||||
|
|
@ -383,17 +394,17 @@ proc getFieldNode(p: pointer, n: ptr TNimNode,
|
||||||
if cmpNimIdentifier(n.name, name) == 0:
|
if cmpNimIdentifier(n.name, name) == 0:
|
||||||
result = n
|
result = n
|
||||||
else:
|
else:
|
||||||
var m = selectBranch(p, n)
|
let m = selectBranch(p, n)
|
||||||
if m != nil: result = getFieldNode(p, m, name)
|
if m != nil: result = getFieldNode(p, m, name)
|
||||||
|
|
||||||
proc `[]=`*(x: Any, fieldName: string, value: Any) =
|
proc `[]=`*(x: Any, fieldName: string, value: Any) =
|
||||||
## Sets a field of `x`; `x` represents an object or a tuple.
|
## Sets a field of `x`. `x` needs to represent an object or a tuple.
|
||||||
var t = x.rawType
|
var t = x.rawType
|
||||||
# XXX BUG: does not work yet, however is questionable anyway
|
# XXX BUG: does not work yet, however is questionable anyway
|
||||||
when false:
|
when false:
|
||||||
if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
|
if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
|
||||||
assert x.rawType.kind in {tyTuple, tyObject}
|
assert x.rawType.kind in {tyTuple, tyObject}
|
||||||
var n = getFieldNode(x.value, t.node, fieldName)
|
let n = getFieldNode(x.value, t.node, fieldName)
|
||||||
if n != nil:
|
if n != nil:
|
||||||
assert n.typ == value.rawType
|
assert n.typ == value.rawType
|
||||||
genericAssign(x.value +!! n.offset, value.value, value.rawType)
|
genericAssign(x.value +!! n.offset, value.value, value.rawType)
|
||||||
|
|
@ -401,13 +412,13 @@ proc `[]=`*(x: Any, fieldName: string, value: Any) =
|
||||||
raise newException(ValueError, "invalid field name: " & fieldName)
|
raise newException(ValueError, "invalid field name: " & fieldName)
|
||||||
|
|
||||||
proc `[]`*(x: Any, fieldName: string): Any =
|
proc `[]`*(x: Any, fieldName: string): Any =
|
||||||
## Gets a field of `x`; `x` represents an object or a tuple.
|
## Gets a field of `x`. `x` needs to represent an object or a tuple.
|
||||||
var t = x.rawType
|
var t = x.rawType
|
||||||
# XXX BUG: does not work yet, however is questionable anyway
|
# XXX BUG: does not work yet, however is questionable anyway
|
||||||
when false:
|
when false:
|
||||||
if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
|
if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
|
||||||
assert x.rawType.kind in {tyTuple, tyObject}
|
assert x.rawType.kind in {tyTuple, tyObject}
|
||||||
var n = getFieldNode(x.value, t.node, fieldName)
|
let n = getFieldNode(x.value, t.node, fieldName)
|
||||||
if n != nil:
|
if n != nil:
|
||||||
result.value = x.value +!! n.offset
|
result.value = x.value +!! n.offset
|
||||||
result.rawType = n.typ
|
result.rawType = n.typ
|
||||||
|
|
@ -417,39 +428,39 @@ proc `[]`*(x: Any, fieldName: string): Any =
|
||||||
raise newException(ValueError, "invalid field name: " & fieldName)
|
raise newException(ValueError, "invalid field name: " & fieldName)
|
||||||
|
|
||||||
proc `[]`*(x: Any): Any =
|
proc `[]`*(x: Any): Any =
|
||||||
## Dereference operation for the any `x` that represents a ptr or a ref.
|
## Dereference operator for `Any`. `x` needs to represent a ptr or a ref.
|
||||||
assert x.rawType.kind in {tyRef, tyPtr}
|
assert x.rawType.kind in {tyRef, tyPtr}
|
||||||
result.value = cast[ppointer](x.value)[]
|
result.value = cast[ppointer](x.value)[]
|
||||||
result.rawType = x.rawType.base
|
result.rawType = x.rawType.base
|
||||||
|
|
||||||
proc `[]=`*(x, y: Any) =
|
proc `[]=`*(x, y: Any) =
|
||||||
## Dereference operation for the any `x` that represents a ptr or a ref.
|
## Dereference operator for `Any`. `x` needs to represent a ptr or a ref.
|
||||||
assert x.rawType.kind in {tyRef, tyPtr}
|
assert x.rawType.kind in {tyRef, tyPtr}
|
||||||
assert y.rawType == x.rawType.base
|
assert y.rawType == x.rawType.base
|
||||||
genericAssign(cast[ppointer](x.value)[], y.value, y.rawType)
|
genericAssign(cast[ppointer](x.value)[], y.value, y.rawType)
|
||||||
|
|
||||||
proc getInt*(x: Any): int =
|
proc getInt*(x: Any): int =
|
||||||
## Retrieves the int value out of `x`. `x` needs to represent an int.
|
## Retrieves the `int` value out of `x`. `x` needs to represent an `int`.
|
||||||
assert skipRange(x.rawType).kind == tyInt
|
assert skipRange(x.rawType).kind == tyInt
|
||||||
result = cast[ptr int](x.value)[]
|
result = cast[ptr int](x.value)[]
|
||||||
|
|
||||||
proc getInt8*(x: Any): int8 =
|
proc getInt8*(x: Any): int8 =
|
||||||
## Retrieves the int8 value out of `x`. `x` needs to represent an int8.
|
## Retrieves the `int8` value out of `x`. `x` needs to represent an `int8`.
|
||||||
assert skipRange(x.rawType).kind == tyInt8
|
assert skipRange(x.rawType).kind == tyInt8
|
||||||
result = cast[ptr int8](x.value)[]
|
result = cast[ptr int8](x.value)[]
|
||||||
|
|
||||||
proc getInt16*(x: Any): int16 =
|
proc getInt16*(x: Any): int16 =
|
||||||
## Retrieves the int16 value out of `x`. `x` needs to represent an int16.
|
## Retrieves the `int16` value out of `x`. `x` needs to represent an `int16`.
|
||||||
assert skipRange(x.rawType).kind == tyInt16
|
assert skipRange(x.rawType).kind == tyInt16
|
||||||
result = cast[ptr int16](x.value)[]
|
result = cast[ptr int16](x.value)[]
|
||||||
|
|
||||||
proc getInt32*(x: Any): int32 =
|
proc getInt32*(x: Any): int32 =
|
||||||
## Retrieves the int32 value out of `x`. `x` needs to represent an int32.
|
## Retrieves the `int32` value out of `x`. `x` needs to represent an `int32`.
|
||||||
assert skipRange(x.rawType).kind == tyInt32
|
assert skipRange(x.rawType).kind == tyInt32
|
||||||
result = cast[ptr int32](x.value)[]
|
result = cast[ptr int32](x.value)[]
|
||||||
|
|
||||||
proc getInt64*(x: Any): int64 =
|
proc getInt64*(x: Any): int64 =
|
||||||
## Retrieves the int64 value out of `x`. `x` needs to represent an int64.
|
## Retrieves the `int64` value out of `x`. `x` needs to represent an `int64`.
|
||||||
assert skipRange(x.rawType).kind == tyInt64
|
assert skipRange(x.rawType).kind == tyInt64
|
||||||
result = cast[ptr int64](x.value)[]
|
result = cast[ptr int64](x.value)[]
|
||||||
|
|
||||||
|
|
@ -457,7 +468,7 @@ proc getBiggestInt*(x: Any): BiggestInt =
|
||||||
## Retrieves the integer value out of `x`. `x` needs to represent
|
## Retrieves the integer value out of `x`. `x` needs to represent
|
||||||
## some integer, a bool, a char, an enum or a small enough bit set.
|
## some integer, a bool, a char, an enum or a small enough bit set.
|
||||||
## The value might be sign-extended to `BiggestInt`.
|
## The value might be sign-extended to `BiggestInt`.
|
||||||
var t = skipRange(x.rawType)
|
let t = skipRange(x.rawType)
|
||||||
case t.kind
|
case t.kind
|
||||||
of tyInt: result = BiggestInt(cast[ptr int](x.value)[])
|
of tyInt: result = BiggestInt(cast[ptr int](x.value)[])
|
||||||
of tyInt8: result = BiggestInt(cast[ptr int8](x.value)[])
|
of tyInt8: result = BiggestInt(cast[ptr int8](x.value)[])
|
||||||
|
|
@ -482,7 +493,7 @@ proc getBiggestInt*(x: Any): BiggestInt =
|
||||||
proc setBiggestInt*(x: Any, y: BiggestInt) =
|
proc setBiggestInt*(x: Any, y: BiggestInt) =
|
||||||
## Sets the integer value of `x`. `x` needs to represent
|
## Sets the integer value of `x`. `x` needs to represent
|
||||||
## some integer, a bool, a char, an enum or a small enough bit set.
|
## some integer, a bool, a char, an enum or a small enough bit set.
|
||||||
var t = skipRange(x.rawType)
|
let t = skipRange(x.rawType)
|
||||||
case t.kind
|
case t.kind
|
||||||
of tyInt: cast[ptr int](x.value)[] = int(y)
|
of tyInt: cast[ptr int](x.value)[] = int(y)
|
||||||
of tyInt8: cast[ptr int8](x.value)[] = int8(y)
|
of tyInt8: cast[ptr int8](x.value)[] = int8(y)
|
||||||
|
|
@ -505,38 +516,34 @@ proc setBiggestInt*(x: Any, y: BiggestInt) =
|
||||||
else: assert false
|
else: assert false
|
||||||
|
|
||||||
proc getUInt*(x: Any): uint =
|
proc getUInt*(x: Any): uint =
|
||||||
## Retrieves the uint value out of `x`, `x` needs to represent an uint.
|
## Retrieves the `uint` value out of `x`. `x` needs to represent a `uint`.
|
||||||
assert skipRange(x.rawType).kind == tyUInt
|
assert skipRange(x.rawType).kind == tyUInt
|
||||||
result = cast[ptr uint](x.value)[]
|
result = cast[ptr uint](x.value)[]
|
||||||
|
|
||||||
proc getUInt8*(x: Any): uint8 =
|
proc getUInt8*(x: Any): uint8 =
|
||||||
## Retrieves the uint8 value out of `x`, `x` needs to represent an
|
## Retrieves the `uint8` value out of `x`. `x` needs to represent a `uint8`.
|
||||||
## uint8.
|
|
||||||
assert skipRange(x.rawType).kind == tyUInt8
|
assert skipRange(x.rawType).kind == tyUInt8
|
||||||
result = cast[ptr uint8](x.value)[]
|
result = cast[ptr uint8](x.value)[]
|
||||||
|
|
||||||
proc getUInt16*(x: Any): uint16 =
|
proc getUInt16*(x: Any): uint16 =
|
||||||
## Retrieves the uint16 value out of `x`, `x` needs to represent an
|
## Retrieves the `uint16` value out of `x`. `x` needs to represent a `uint16`.
|
||||||
## uint16.
|
|
||||||
assert skipRange(x.rawType).kind == tyUInt16
|
assert skipRange(x.rawType).kind == tyUInt16
|
||||||
result = cast[ptr uint16](x.value)[]
|
result = cast[ptr uint16](x.value)[]
|
||||||
|
|
||||||
proc getUInt32*(x: Any): uint32 =
|
proc getUInt32*(x: Any): uint32 =
|
||||||
## Retrieves the uint32 value out of `x`, `x` needs to represent an
|
## Retrieves the `uint32` value out of `x`. `x` needs to represent a `uint32`.
|
||||||
## uint32.
|
|
||||||
assert skipRange(x.rawType).kind == tyUInt32
|
assert skipRange(x.rawType).kind == tyUInt32
|
||||||
result = cast[ptr uint32](x.value)[]
|
result = cast[ptr uint32](x.value)[]
|
||||||
|
|
||||||
proc getUInt64*(x: Any): uint64 =
|
proc getUInt64*(x: Any): uint64 =
|
||||||
## Retrieves the uint64 value out of `x`, `x` needs to represent an
|
## Retrieves the `uint64` value out of `x`. `x` needs to represent a `uint64`.
|
||||||
## uint64.
|
|
||||||
assert skipRange(x.rawType).kind == tyUInt64
|
assert skipRange(x.rawType).kind == tyUInt64
|
||||||
result = cast[ptr uint64](x.value)[]
|
result = cast[ptr uint64](x.value)[]
|
||||||
|
|
||||||
proc getBiggestUint*(x: Any): uint64 =
|
proc getBiggestUint*(x: Any): uint64 =
|
||||||
## Retrieves the unsigned integer value out of `x`. `x` needs to
|
## Retrieves the unsigned integer value out of `x`. `x` needs to
|
||||||
## represent an unsigned integer.
|
## represent an unsigned integer.
|
||||||
var t = skipRange(x.rawType)
|
let t = skipRange(x.rawType)
|
||||||
case t.kind
|
case t.kind
|
||||||
of tyUInt: result = uint64(cast[ptr uint](x.value)[])
|
of tyUInt: result = uint64(cast[ptr uint](x.value)[])
|
||||||
of tyUInt8: result = uint64(cast[ptr uint8](x.value)[])
|
of tyUInt8: result = uint64(cast[ptr uint8](x.value)[])
|
||||||
|
|
@ -546,9 +553,9 @@ proc getBiggestUint*(x: Any): uint64 =
|
||||||
else: assert false
|
else: assert false
|
||||||
|
|
||||||
proc setBiggestUint*(x: Any; y: uint64) =
|
proc setBiggestUint*(x: Any; y: uint64) =
|
||||||
## Sets the unsigned integer value of `c`. `c` needs to represent an
|
## Sets the unsigned integer value of `x`. `x` needs to represent an
|
||||||
## unsigned integer.
|
## unsigned integer.
|
||||||
var t = skipRange(x.rawType)
|
let t = skipRange(x.rawType)
|
||||||
case t.kind:
|
case t.kind:
|
||||||
of tyUInt: cast[ptr uint](x.value)[] = uint(y)
|
of tyUInt: cast[ptr uint](x.value)[] = uint(y)
|
||||||
of tyUInt8: cast[ptr uint8](x.value)[] = uint8(y)
|
of tyUInt8: cast[ptr uint8](x.value)[] = uint8(y)
|
||||||
|
|
@ -558,14 +565,14 @@ proc setBiggestUint*(x: Any; y: uint64) =
|
||||||
else: assert false
|
else: assert false
|
||||||
|
|
||||||
proc getChar*(x: Any): char =
|
proc getChar*(x: Any): char =
|
||||||
## Retrieves the char value out of `x`. `x` needs to represent a char.
|
## Retrieves the `char` value out of `x`. `x` needs to represent a `char`.
|
||||||
var t = skipRange(x.rawType)
|
let t = skipRange(x.rawType)
|
||||||
assert t.kind == tyChar
|
assert t.kind == tyChar
|
||||||
result = cast[ptr char](x.value)[]
|
result = cast[ptr char](x.value)[]
|
||||||
|
|
||||||
proc getBool*(x: Any): bool =
|
proc getBool*(x: Any): bool =
|
||||||
## Retrieves the bool value out of `x`. `x` needs to represent a bool.
|
## Retrieves the `bool` value out of `x`. `x` needs to represent a `bool`.
|
||||||
var t = skipRange(x.rawType)
|
let t = skipRange(x.rawType)
|
||||||
assert t.kind == tyBool
|
assert t.kind == tyBool
|
||||||
result = cast[ptr bool](x.value)[]
|
result = cast[ptr bool](x.value)[]
|
||||||
|
|
||||||
|
|
@ -579,10 +586,10 @@ proc getEnumOrdinal*(x: Any, name: string): int =
|
||||||
## Gets the enum field ordinal from `name`. `x` needs to represent an enum
|
## Gets the enum field ordinal from `name`. `x` needs to represent an enum
|
||||||
## but is only used to access the type information. In case of an error
|
## but is only used to access the type information. In case of an error
|
||||||
## `low(int)` is returned.
|
## `low(int)` is returned.
|
||||||
var typ = skipRange(x.rawType)
|
let typ = skipRange(x.rawType)
|
||||||
assert typ.kind == tyEnum
|
assert typ.kind == tyEnum
|
||||||
var n = typ.node
|
let n = typ.node
|
||||||
var s = n.sons
|
let s = n.sons
|
||||||
for i in 0 .. n.len-1:
|
for i in 0 .. n.len-1:
|
||||||
if cmpNimIdentifier($s[i].name, name) == 0:
|
if cmpNimIdentifier($s[i].name, name) == 0:
|
||||||
if ntfEnumHole notin typ.flags:
|
if ntfEnumHole notin typ.flags:
|
||||||
|
|
@ -595,16 +602,16 @@ proc getEnumField*(x: Any, ordinalValue: int): string =
|
||||||
## Gets the enum field name as a string. `x` needs to represent an enum
|
## Gets the enum field name as a string. `x` needs to represent an enum
|
||||||
## but is only used to access the type information. The field name of
|
## but is only used to access the type information. The field name of
|
||||||
## `ordinalValue` is returned.
|
## `ordinalValue` is returned.
|
||||||
var typ = skipRange(x.rawType)
|
let typ = skipRange(x.rawType)
|
||||||
assert typ.kind == tyEnum
|
assert typ.kind == tyEnum
|
||||||
var e = ordinalValue
|
let e = ordinalValue
|
||||||
if ntfEnumHole notin typ.flags:
|
if ntfEnumHole notin typ.flags:
|
||||||
if e <% typ.node.len:
|
if e <% typ.node.len:
|
||||||
return $typ.node.sons[e].name
|
return $typ.node.sons[e].name
|
||||||
else:
|
else:
|
||||||
# ugh we need a slow linear search:
|
# ugh we need a slow linear search:
|
||||||
var n = typ.node
|
let n = typ.node
|
||||||
var s = n.sons
|
let s = n.sons
|
||||||
for i in 0 .. n.len-1:
|
for i in 0 .. n.len-1:
|
||||||
if s[i].offset == e: return $s[i].name
|
if s[i].offset == e: return $s[i].name
|
||||||
result = $e
|
result = $e
|
||||||
|
|
@ -614,17 +621,17 @@ proc getEnumField*(x: Any): string =
|
||||||
result = getEnumField(x, getBiggestInt(x).int)
|
result = getEnumField(x, getBiggestInt(x).int)
|
||||||
|
|
||||||
proc getFloat*(x: Any): float =
|
proc getFloat*(x: Any): float =
|
||||||
## Retrieves the float value out of `x`. `x` needs to represent an float.
|
## Retrieves the `float` value out of `x`. `x` needs to represent a `float`.
|
||||||
assert skipRange(x.rawType).kind == tyFloat
|
assert skipRange(x.rawType).kind == tyFloat
|
||||||
result = cast[ptr float](x.value)[]
|
result = cast[ptr float](x.value)[]
|
||||||
|
|
||||||
proc getFloat32*(x: Any): float32 =
|
proc getFloat32*(x: Any): float32 =
|
||||||
## Retrieves the float32 value out of `x`. `x` needs to represent an float32.
|
## Retrieves the `float32` value out of `x`. `x` needs to represent a `float32`.
|
||||||
assert skipRange(x.rawType).kind == tyFloat32
|
assert skipRange(x.rawType).kind == tyFloat32
|
||||||
result = cast[ptr float32](x.value)[]
|
result = cast[ptr float32](x.value)[]
|
||||||
|
|
||||||
proc getFloat64*(x: Any): float64 =
|
proc getFloat64*(x: Any): float64 =
|
||||||
## Retrieves the float64 value out of `x`. `x` needs to represent an float64.
|
## Retrieves the `float64` value out of `x`. `x` needs to represent a `float64`.
|
||||||
assert skipRange(x.rawType).kind == tyFloat64
|
assert skipRange(x.rawType).kind == tyFloat64
|
||||||
result = cast[ptr float64](x.value)[]
|
result = cast[ptr float64](x.value)[]
|
||||||
|
|
||||||
|
|
@ -647,7 +654,7 @@ proc setBiggestFloat*(x: Any, y: BiggestFloat) =
|
||||||
else: assert false
|
else: assert false
|
||||||
|
|
||||||
proc getString*(x: Any): string =
|
proc getString*(x: Any): string =
|
||||||
## Retrieves the string value out of `x`. `x` needs to represent a string.
|
## Retrieves the `string` value out of `x`. `x` needs to represent a `string`.
|
||||||
assert x.rawType.kind == tyString
|
assert x.rawType.kind == tyString
|
||||||
when defined(gcDestructors):
|
when defined(gcDestructors):
|
||||||
result = cast[ptr string](x.value)[]
|
result = cast[ptr string](x.value)[]
|
||||||
|
|
@ -656,12 +663,12 @@ proc getString*(x: Any): string =
|
||||||
result = cast[ptr string](x.value)[]
|
result = cast[ptr string](x.value)[]
|
||||||
|
|
||||||
proc setString*(x: Any, y: string) =
|
proc setString*(x: Any, y: string) =
|
||||||
## Sets the string value of `x`. `x` needs to represent a string.
|
## Sets the `string` value of `x`. `x` needs to represent a `string`.
|
||||||
assert x.rawType.kind == tyString
|
assert x.rawType.kind == tyString
|
||||||
cast[ptr string](x.value)[] = y # also correct for gcDestructors
|
cast[ptr string](x.value)[] = y # also correct for gcDestructors
|
||||||
|
|
||||||
proc getCString*(x: Any): cstring =
|
proc getCString*(x: Any): cstring =
|
||||||
## Retrieves the cstring value out of `x`. `x` needs to represent a cstring.
|
## Retrieves the `cstring` value out of `x`. `x` needs to represent a `cstring`.
|
||||||
assert x.rawType.kind == tyCString
|
assert x.rawType.kind == tyCString
|
||||||
result = cast[ptr cstring](x.value)[]
|
result = cast[ptr cstring](x.value)[]
|
||||||
|
|
||||||
|
|
@ -672,10 +679,10 @@ proc assign*(x, y: Any) =
|
||||||
genericAssign(x.value, y.value, y.rawType)
|
genericAssign(x.value, y.value, y.rawType)
|
||||||
|
|
||||||
iterator elements*(x: Any): int =
|
iterator elements*(x: Any): int =
|
||||||
## Iterates over every element of `x` that represents a Nim bitset.
|
## Iterates over every element of `x`. `x` needs to represent a `set`.
|
||||||
assert x.rawType.kind == tySet
|
assert x.rawType.kind == tySet
|
||||||
var typ = x.rawType
|
let typ = x.rawType
|
||||||
var p = x.value
|
let p = x.value
|
||||||
# "typ.slots.len" field is for sets the "first" field
|
# "typ.slots.len" field is for sets the "first" field
|
||||||
var u: int64
|
var u: int64
|
||||||
case typ.size
|
case typ.size
|
||||||
|
|
@ -684,7 +691,7 @@ iterator elements*(x: Any): int =
|
||||||
of 4: u = ze64(cast[ptr int32](p)[])
|
of 4: u = ze64(cast[ptr int32](p)[])
|
||||||
of 8: u = cast[ptr int64](p)[]
|
of 8: u = cast[ptr int64](p)[]
|
||||||
else:
|
else:
|
||||||
var a = cast[pbyteArray](p)
|
let a = cast[pbyteArray](p)
|
||||||
for i in 0 .. typ.size*8-1:
|
for i in 0 .. typ.size*8-1:
|
||||||
if (ze(a[i div 8]) and (1 shl (i mod 8))) != 0:
|
if (ze(a[i div 8]) and (1 shl (i mod 8))) != 0:
|
||||||
yield i + typ.node.len
|
yield i + typ.node.len
|
||||||
|
|
@ -696,10 +703,10 @@ iterator elements*(x: Any): int =
|
||||||
proc inclSetElement*(x: Any, elem: int) =
|
proc inclSetElement*(x: Any, elem: int) =
|
||||||
## Includes an element `elem` in `x`. `x` needs to represent a Nim bitset.
|
## Includes an element `elem` in `x`. `x` needs to represent a Nim bitset.
|
||||||
assert x.rawType.kind == tySet
|
assert x.rawType.kind == tySet
|
||||||
var typ = x.rawType
|
let typ = x.rawType
|
||||||
var p = x.value
|
let p = x.value
|
||||||
# "typ.slots.len" field is for sets the "first" field
|
# "typ.slots.len" field is for sets the "first" field
|
||||||
var e = elem - typ.node.len
|
let e = elem - typ.node.len
|
||||||
case typ.size
|
case typ.size
|
||||||
of 1:
|
of 1:
|
||||||
var a = cast[ptr int8](p)
|
var a = cast[ptr int8](p)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue