summary refs log blame commit diff stats
path: root/lib/system/platforms.nim
blob: 1c35eeb0d0e9316e1a76a913ed937675aea81514 (plain) (tree)
an class="p">, cl: var ReprClosure) {.benign.} proc reprArray(result: var string, p: pointer, typ: PNimType, cl: var ReprClosure) = add result, "[" var bs = typ.base.size for i in 0..typ.size div bs - 1: if i > 0: add result, ", " reprAux(result, cast[pointer](cast[ByteAddress](p) + i*bs), typ.base, cl) add result, "]" when defined(gcDestructors): type GenericSeq = object len: int p: pointer PGenericSeq = ptr GenericSeq const payloadOffset = sizeof(int) + sizeof(pointer) # see seqs.nim: cap: int # region: Allocator template payloadPtr(x: untyped): untyped = cast[PGenericSeq](x).p else: const payloadOffset = GenericSeqSize template payloadPtr(x: untyped): untyped = x proc reprSequence(result: var string, p: pointer, typ: PNimType, cl: var ReprClosure) = if p == nil: add result, "[]" return result.add(reprPointer(p)) result.add "@[" var bs = typ.base.size for i in 0..cast[PGenericSeq](p).len-1: if i > 0: add result, ", " reprAux(result, cast[pointer](cast[ByteAddress](payloadPtr(p)) + payloadOffset + i*bs), typ.base, cl) add result, "]" proc reprRecordAux(result: var string, p: pointer, n: ptr TNimNode, cl: var ReprClosure) {.benign.} = case n.kind of nkNone: sysAssert(false, "reprRecordAux") of nkSlot: add result, $n.name add result, " = " reprAux(result, cast[pointer](cast[ByteAddress](p) + n.offset), n.typ, cl) of nkList: for i in 0..n.len-1: if i > 0: add result, ",\n" reprRecordAux(result, p, n.sons[i], cl) of nkCase: var m = selectBranch(p, n) reprAux(result, cast[pointer](cast[ByteAddress](p) + n.offset), n.typ, cl) if m != nil: reprRecordAux(result, p, m, cl) proc reprRecord(result: var string, p: pointer, typ: PNimType, cl: var ReprClosure) = add result, "[" var curTyp = typ var first = true while curTyp != nil: var part = "" reprRecordAux(part, p, curTyp.node, cl) if part.len > 0: if not first: add result, ",\n" add result, part first = false curTyp = curTyp.base add result, "]" proc reprRef(result: var string, p: pointer, typ: PNimType, cl: var ReprClosure) = # we know that p is not nil here: when declared(CellSet): when defined(boehmGC) or defined(gogc) or defined(nogc): var cell = cast[PCell](p) else: var cell = usrToCell(p) add result, "ref " & reprPointer(p) if cell notin cl.marked: # only the address is shown: incl(cl.marked, cell) add result, " --> " reprAux(result, p, typ.base, cl) proc getInt(p: pointer, size: int): int = case size of 1: return int(cast[ptr uint8](p)[]) of 2: return int(cast[ptr uint16](p)[]) of 4: return int(cast[ptr uint32](p)[]) of 8: return int(cast[ptr uint64](p)[]) else: discard proc reprAux(result: var string, p: pointer, typ: PNimType, cl: var ReprClosure) = if cl.recdepth == 0: add result, "..." return dec(cl.recdepth) case typ.kind of tySet: reprSetAux(result, p, typ) of tyArray, tyArrayConstr: reprArray(result, p, typ, cl) of tyTuple: reprRecord(result, p, typ, cl) of tyObject: var t = cast[ptr PNimType](p)[] reprRecord(result, p, t, cl) of tyRef, tyPtr: sysAssert(p != nil, "reprAux") if cast[PPointer](p)[] == nil: add result, "nil" else: reprRef(result, cast[PPointer](p)[], typ, cl) of tySequence: reprSequence(result, cast[PPointer](p)[], typ, cl) of tyInt: add result, $(cast[ptr int](p)[]) of tyInt8: add result, $int(cast[ptr int8](p)[]) of tyInt16: add result, $int(cast[ptr int16](p)[]) of tyInt32: add result, $int(cast[ptr int32](p)[]) of tyInt64: add result, $(cast[ptr int64](p)[]) of tyUInt: add result, $(cast[ptr uint](p)[]) of tyUInt8: add result, $(cast[ptr uint8](p)[]) of tyUInt16: add result, $(cast[ptr uint16](p)[]) of tyUInt32: add result, $(cast[ptr uint32](p)[]) of tyUInt64: add result, $(cast[ptr uint64](p)[]) of tyFloat: add result, $(cast[ptr float](p)[]) of tyFloat32: add result, $(cast[ptr float32](p)[]) of tyFloat64: add result, $(cast[ptr float64](p)[]) of tyEnum: add result, reprEnum(getInt(p, typ.size), typ) of tyBool: add result, reprBool(cast[ptr bool](p)[]) of tyChar: add result, reprChar(cast[ptr char](p)[]) of tyString: let sp = cast[ptr string](p) reprStrAux(result, sp[].cstring, sp[].len) of tyCString: let cs = cast[ptr cstring](p)[] if cs.isNil: add result, "nil" else: reprStrAux(result, cs, cs.len) of tyRange: reprAux(result, p, typ.base, cl) of tyProc, tyPointer: if cast[PPointer](p)[] == nil: add result, "nil" else: add result, reprPointer(cast[PPointer](p)[]) of tyUncheckedArray: add result, "[...]" else: add result, "(invalid data!)" inc(cl.recdepth) proc reprOpenArray(p: pointer, length: int, elemtyp: PNimType): string {. compilerRtl.} = var cl: ReprClosure initReprClosure(cl) result = "[" var bs = elemtyp.size for i in 0..length - 1: if i > 0: add result, ", " reprAux(result, cast[pointer](cast[ByteAddress](p) + i*bs), elemtyp, cl) add result, "]" deinitReprClosure(cl) when not defined(useNimRtl): proc reprAny(p: pointer, typ: PNimType): string = var cl: ReprClosure initReprClosure(cl) result = "" if typ.kind in {tyObject, tyTuple, tyArray, tyArrayConstr, tySet}: reprAux(result, p, typ, cl) else: var p = p reprAux(result, addr(p), typ, cl) add result, "\n" deinitReprClosure(cl)