Skip to content

Commit

Permalink
minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Dec 27, 2019
1 parent 649bf32 commit 1917ebf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
18 changes: 18 additions & 0 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))
8 changes: 4 additions & 4 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions compiler/dfa.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions lib/system/io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit 1917ebf

Please sign in to comment.