mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-04 20:17:42 +00:00
minor refactorings
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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`.
|
||||
|
||||
Reference in New Issue
Block a user