minor refactorings

This commit is contained in:
Andreas Rumpf
2019-12-27 08:29:40 +01:00
parent 649bf326bf
commit 1917ebf082
5 changed files with 34 additions and 22 deletions

View File

@@ -1832,3 +1832,21 @@ proc addParam*(procType: PType; param: PSym) =
template destructor*(t: PType): PSym = t.attachedOps[attachedDestructor]
template assignment*(t: PType): PSym = t.attachedOps[attachedAsgn]
template asink*(t: PType): PSym = t.attachedOps[attachedSink]
const magicsThatCanRaise = {
mNone, mSlurp, mStaticExec, mParseExprToAst, mParseStmtToAst}
proc canRaiseConservative*(fn: PNode): bool =
if fn.kind == nkSym and fn.sym.magic notin magicsThatCanRaise:
result = false
else:
result = true
proc canRaise*(fn: PNode): bool =
if fn.kind == nkSym and (fn.sym.magic notin magicsThatCanRaise or
{sfImportc, sfInfixCall} * fn.sym.flags == {sfImportc}):
result = false
else:
result = fn.typ != nil and ((fn.typ.n[0].len < effectListLen) or
(fn.typ.n[0][exceptionEffects] != nil and
fn.typ.n[0][exceptionEffects].safeLen > 0))

View File

@@ -104,17 +104,17 @@ proc getModuleDllPath(m: BModule, s: PSym): Rope =
import macros
proc cgFormatValue(result: var string; value: Rope): void =
proc cgFormatValue(result: var string; value: Rope) =
for str in leaves(value):
result.add str
proc cgFormatValue(result: var string; value: string): void =
proc cgFormatValue(result: var string; value: string) =
result.add value
proc cgFormatValue(result: var string; value: BiggestInt): void =
proc cgFormatValue(result: var string; value: BiggestInt) =
result.addInt value
proc cgFormatValue(result: var string; value: Int128): void =
proc cgFormatValue(result: var string; value: Int128) =
result.addInt128 value
# TODO: please document

View File

@@ -693,14 +693,6 @@ proc genDef(c: var Con; n: PNode) =
elif isAnalysableFieldAccess(n, c.owner):
c.code.add Instr(n: n, kind: def, sym: nil)
proc canRaise(fn: PNode): bool =
const magicsThatCanRaise = {
mNone, mSlurp, mStaticExec, mParseExprToAst, mParseStmtToAst}
if fn.kind == nkSym and fn.sym.magic notin magicsThatCanRaise:
result = false
else:
result = true
proc genCall(c: var Con; n: PNode) =
gen(c, n[0])
var t = n[0].typ
@@ -715,7 +707,7 @@ proc genCall(c: var Con; n: PNode) =
# optimizer.
genDef(c, n[i])
# every call can potentially raise:
if c.inTryStmt > 0 and canRaise(n[0]):
if c.inTryStmt > 0 and canRaiseConservative(n[0]):
# we generate the instruction sequence:
# fork lab1
# goto exceptionHandler (except or finally)

View File

@@ -708,6 +708,8 @@ proc track(tracked: PEffects, n: PNode) =
of nkCallKinds:
# p's effects are ours too:
var a = n[0]
#if canRaise(a):
# echo "this can raise ", tracked.config $ n.info
let op = a.typ
if n.typ != nil:
if tracked.owner.kind != skMacro and n.typ.skipTypes(abstractVar).kind != tyOpenArray:

View File

@@ -522,7 +522,7 @@ proc open*(f: var File, filename: string,
##
## Default mode is readonly. Returns true iff the file could be opened.
## This throws no exception if the file could not be opened.
var p: pointer = fopen(filename, FormatOpen[mode])
var p = fopen(filename, FormatOpen[mode])
if p != nil:
when defined(posix) and not defined(nimscript):
# How `fopen` handles opening a directory is not specified in ISO C and
@@ -547,15 +547,13 @@ proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
## file variables.
##
## Default mode is readonly. Returns true iff the file could be reopened.
var p: pointer = freopen(filename, FormatOpen[mode], f)
result = p != nil
result = freopen(filename, FormatOpen[mode], f) != nil
proc open*(f: var File, filehandle: FileHandle,
mode: FileMode = fmRead): bool {.tags: [], raises: [], benign.} =
## Creates a ``File`` from a `filehandle` with given `mode`.
##
## Default mode is readonly. Returns true iff the file could be opened.
f = c_fdopen(filehandle, FormatOpen[mode])
result = f != nil
@@ -582,7 +580,7 @@ proc getFilePos*(f: File): int64 {.benign.} =
proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.} =
## retrieves the file size (in bytes) of `f`.
var oldPos = getFilePos(f)
let oldPos = getFilePos(f)
discard c_fseek(f, 0, 2) # seek the end of the file
result = getFilePos(f)
setFilePos(f, oldPos)
@@ -639,7 +637,7 @@ when defined(windows) and not defined(nimscript):
importc: when defined(bcc): "setmode" else: "_setmode",
header: "<io.h>".}
var
O_BINARY {.importc: "_O_BINARY", header:"<fcntl.h>".}: cint
O_BINARY {.importc: "_O_BINARY", header: "<fcntl.h>".}: cint
# we use binary mode on Windows:
c_setmode(c_fileno(stdin), O_BINARY)
@@ -731,9 +729,11 @@ iterator lines*(filename: string): TaintedString {.tags: [ReadIOEffect].} =
## buffer.add(line.replace("a", "0") & '\x0A')
## writeFile(filename, buffer)
var f = open(filename, bufSize=8000)
defer: close(f)
var res = TaintedString(newStringOfCap(80))
while f.readLine(res): yield res
try:
var res = TaintedString(newStringOfCap(80))
while f.readLine(res): yield res
finally:
close(f)
iterator lines*(f: File): TaintedString {.tags: [ReadIOEffect].} =
## Iterate over any line in the file `f`.