From 1917ebf082289fbdf8acbd9d66a8d204b00e06dc Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Fri, 27 Dec 2019 08:29:40 +0100 Subject: [PATCH] minor refactorings --- compiler/ast.nim | 18 ++++++++++++++++++ compiler/cgen.nim | 8 ++++---- compiler/dfa.nim | 10 +--------- compiler/sempass2.nim | 2 ++ lib/system/io.nim | 18 +++++++++--------- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index a4868a9e6f..7bcb7570d0 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -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)) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 70dae5ffa6..1e454f25b5 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -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 diff --git a/compiler/dfa.nim b/compiler/dfa.nim index cc0b8f7ae4..cec45eaeca 100644 --- a/compiler/dfa.nim +++ b/compiler/dfa.nim @@ -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) diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 1a4204ec62..6a61956003 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -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: diff --git a/lib/system/io.nim b/lib/system/io.nim index 7010a7fdbe..ead8deb246 100644 --- a/lib/system/io.nim +++ b/lib/system/io.nim @@ -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: "".} var - O_BINARY {.importc: "_O_BINARY", header:"".}: cint + O_BINARY {.importc: "_O_BINARY", header: "".}: 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`.