diff options
41 files changed, 172 insertions, 127 deletions
diff --git a/compiler/astalgo.nim b/compiler/astalgo.nim index a8fc9c8f6..2e75c7787 100755 --- a/compiler/astalgo.nim +++ b/compiler/astalgo.nim @@ -35,9 +35,10 @@ proc ObjectSetContainsOrIncl*(t: var TObjectSet, obj: PObject): bool proc TablePut*(t: var TTable, key, val: PObject) proc TableGet*(t: TTable, key: PObject): PObject type - TCmpProc* = proc (key, closure: PObject): bool # should return true if found + TCmpProc* = proc (key, closure: PObject): bool {.nimcall.} # true if found -proc TableSearch*(t: TTable, key, closure: PObject, comparator: TCmpProc): PObject +proc TableSearch*(t: TTable, key, closure: PObject, + comparator: TCmpProc): PObject # return val as soon as comparator returns true; if this never happens, # nil is returned diff --git a/compiler/ccgtrav.nim b/compiler/ccgtrav.nim index 75357ff46..1ff6346f6 100644 --- a/compiler/ccgtrav.nim +++ b/compiler/ccgtrav.nim @@ -80,8 +80,10 @@ proc genTraverseProc(c: var TTraversalClosure, accessor: PRope, typ: PType) = genTraverseProc(c, ropef("$1.Field$2", accessor, i.toRope), typ.sons[i]) of tyRef, tyString, tySequence: lineCg(p, cpsStmts, c.visitorFrmt, accessor) - else: - # no marker procs for closures yet + of tyProc: + if typ.callConv == ccClosure: + lineCg(p, cpsStmts, c.visitorFrmt, ropef("$1.ClEnv", accessor)) + else: nil proc genTraverseProcSeq(c: var TTraversalClosure, accessor: PRope, typ: PType) = diff --git a/compiler/cgen.nim b/compiler/cgen.nim index e437cbb66..2c2230be9 100755 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -418,9 +418,20 @@ proc assignLocalVar(p: BProc, s: PSym) = include ccgthreadvars +proc VarInDynamicLib(m: BModule, sym: PSym) +proc mangleDynLibProc(sym: PSym): PRope + proc assignGlobalVar(p: BProc, s: PSym) = if s.loc.k == locNone: fillLoc(s.loc, locGlobalVar, s.typ, mangleName(s), OnHeap) + + if lfDynamicLib in s.loc.flags: + var q = findPendingModule(p.module, s) + if q != nil and not ContainsOrIncl(q.declaredThings, s.id): + VarInDynamicLib(q, s) + else: + s.loc.r = mangleDynLibProc(s) + return useHeader(p.module, s) if lfNoDecl in s.loc.flags: return if sfThread in s.flags: @@ -537,6 +548,21 @@ proc SymInDynamicLib(m: BModule, sym: PSym) = "$1 = linkonce global $2 zeroinitializer$n", [sym.loc.r, getTypeDesc(m, sym.loc.t)]) +proc VarInDynamicLib(m: BModule, sym: PSym) = + var lib = sym.annex + var extname = sym.loc.r + loadDynamicLib(m, lib) + incl(sym.loc.flags, lfIndirect) + var tmp = mangleDynLibProc(sym) + sym.loc.r = tmp # from now on we only need the internal name + inc(m.labels, 2) + appcg(m, m.s[cfsDynLibInit], + "$1 = ($2*) #nimGetProcAddr($3, $4);$n", + [tmp, getTypeDesc(m, sym.typ), + lib.name, cstringLit(m, m.s[cfsDynLibInit], ropeToStr(extname))]) + appf(m.s[cfsVars], "$2* $1;$n", + [sym.loc.r, getTypeDesc(m, sym.loc.t)]) + proc SymInDynamicLibPartial(m: BModule, sym: PSym) = sym.loc.r = mangleDynLibProc(sym) sym.typ.sym = nil # generate a new name @@ -751,6 +777,7 @@ proc genVarPrototype(m: BModule, sym: PSym) = else: app(m.s[cfsVars], "extern ") app(m.s[cfsVars], getTypeDesc(m, sym.loc.t)) + if lfDynamicLib in sym.loc.flags: app(m.s[cfsVars], "*") if sfRegister in sym.flags: app(m.s[cfsVars], " register") if sfVolatile in sym.flags: app(m.s[cfsVars], " volatile") appf(m.s[cfsVars], " $1;$n", [sym.loc.r]) diff --git a/compiler/lists.nim b/compiler/lists.nim index 6a1f6c650..1998581ce 100755 --- a/compiler/lists.nim +++ b/compiler/lists.nim @@ -22,7 +22,7 @@ type head*, tail*: PListEntry Counter*: int - TCompareProc* = proc (entry: PListEntry, closure: Pointer): bool + TCompareProc* = proc (entry: PListEntry, closure: Pointer): bool {.nimcall.} proc InitLinkedList*(list: var TLinkedList) = list.Counter = 0 diff --git a/compiler/msgs.nim b/compiler/msgs.nim index 03ee28df3..a661da0e1 100755 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -102,7 +102,7 @@ type warnSmallLshouldNotBeUsed, warnUnknownMagic, warnRedefinitionOfLabel, warnUnknownSubstitutionX, warnLanguageXNotSupported, warnCommentXIgnored, warnXisPassedToProcVar, warnAnalysisLoophole, - warnDifferentHeaps, warnWriteToForeignHeap, warnImplicitNarrowing, + warnDifferentHeaps, warnWriteToForeignHeap, warnImplicitClosure, warnUser, hintSuccess, hintSuccessX, hintLineTooLong, hintXDeclaredButNotUsed, hintConvToBaseNotNeeded, @@ -345,7 +345,7 @@ const warnAnalysisLoophole: "thread analysis incomplete due to unkown call '$1' [AnalysisLoophole]", warnDifferentHeaps: "possible inconsistency of thread local heaps [DifferentHeaps]", warnWriteToForeignHeap: "write to foreign heap [WriteToForeignHeap]", - warnImplicitNarrowing: "implicit narrowing conversion: '$1' [ImplicitNarrowing]", + warnImplicitClosure: "implicit closure convention: '$1' [ImplicitClosure]", warnUser: "$1 [User]", hintSuccess: "operation successful [Success]", hintSuccessX: "operation successful ($# lines compiled; $# sec total; $#) [SuccessX]", @@ -370,7 +370,7 @@ const "RedefinitionOfLabel", "UnknownSubstitutionX", "LanguageXNotSupported", "CommentXIgnored", "XisPassedToProcVar", "AnalysisLoophole", "DifferentHeaps", "WriteToForeignHeap", - "ImplicitNarrowing,", "User"] + "ImplicitClosure,", "User"] HintsToStr*: array[0..13, string] = ["Success", "SuccessX", "LineTooLong", "XDeclaredButNotUsed", "ConvToBaseNotNeeded", "ConvFromXtoItselfNotNeeded", diff --git a/compiler/parser.nim b/compiler/parser.nim index 26ffaf59c..7f11ddb2b 100755 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -1164,7 +1164,7 @@ proc newCommentStmt(p: var TParser): PNode = result.info.line = result.info.line - int16(1) type - TDefParser = proc (p: var TParser): PNode + TDefParser = proc (p: var TParser): PNode {.nimcall.} proc parseSection(p: var TParser, kind: TNodeKind, defparser: TDefParser): PNode = diff --git a/compiler/passes.nim b/compiler/passes.nim index 24659fc4e..bedcbb16e 100755 --- a/compiler/passes.nim +++ b/compiler/passes.nim @@ -26,11 +26,11 @@ type PPassContext* = ref TPassContext TPass* = tuple[ - open: proc (module: PSym, filename: string): PPassContext, + open: proc (module: PSym, filename: string): PPassContext {.nimcall.}, openCached: proc (module: PSym, filename: string, - rd: PRodReader): PPassContext, - close: proc (p: PPassContext, n: PNode): PNode, - process: proc (p: PPassContext, topLevelStmt: PNode): PNode] + rd: PRodReader): PPassContext {.nimcall.}, + close: proc (p: PPassContext, n: PNode): PNode {.nimcall.}, + process: proc (p: PPassContext, topLevelStmt: PNode): PNode {.nimcall.}] # a pass is a tuple of procedure vars ``TPass.close`` may produce additional # nodes. These are passed to the other close procedures. @@ -47,8 +47,8 @@ proc processModule*(module: PSym, filename: string, stream: PLLStream, # the semantic checker needs these: var - gImportModule*: proc (filename: string): PSym - gIncludeFile*: proc (filename: string): PNode + gImportModule*: proc (filename: string): PSym {.nimcall.} + gIncludeFile*: proc (filename: string): PNode {.nimcall.} # implementation diff --git a/compiler/semdata.nim b/compiler/semdata.nim index b524881da..74e82db61 100755 --- a/compiler/semdata.nim +++ b/compiler/semdata.nim @@ -66,9 +66,9 @@ type converters*: TSymSeq # sequence of converters optionStack*: TLinkedList libs*: TLinkedList # all libs used by this module - semConstExpr*: proc (c: PContext, n: PNode): PNode # for the pragmas - semExpr*: proc (c: PContext, n: PNode): PNode # for the pragmas - semConstBoolExpr*: proc (c: PContext, n: PNode): PNode # XXX bite the bullet + semConstExpr*: proc (c: PContext, n: PNode): PNode {.nimcall.} # for the pragmas + semExpr*: proc (c: PContext, n: PNode): PNode {.nimcall.} # for the pragmas + semConstBoolExpr*: proc (c: PContext, n: PNode): PNode {.nimcall.} # XXX bite the bullet includedFiles*: TIntSet # used to detect recursive include files filename*: string # the module's filename userPragmas*: TStrTable diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 7e265d95c..256d19db1 100755 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -616,7 +616,7 @@ proc SemTypeSection(c: PContext, n: PNode): PNode = typeSectionFinalPass(c, n) result = n -proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) = +proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) = s.typ = semProcTypeNode(c, n, genericParams, nil, s.kind) proc addParams(c: PContext, n: PNode, kind: TSymKind) = diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index d082f8bb9..e9cb8babb 100755 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -566,7 +566,7 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode, block addImplicitGeneric: # is this a bindOnce type class already present in the param list? for i in countup(0, genericParams.len - 1): - if genericParams.sons[i].sym.name == paramTypId: + if genericParams.sons[i].sym.name.id == paramTypId.id: result = genericParams.sons[i].typ break addImplicitGeneric @@ -800,7 +800,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType = of nkPtrTy: result = semAnyRef(c, n, tyPtr, prev) of nkVarTy: result = semVarType(c, n, prev) of nkDistinctTy: result = semDistinct(c, n, prev) - of nkProcTy: + of nkProcTy: if n.sonsLen == 0: return newConstraint(c, tyProc) checkSonsLen(n, 2) openScope(c.tab) @@ -808,7 +808,12 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType = # dummy symbol for `pragma`: var s = newSymS(skProc, newIdentNode(getIdent("dummy"), n.info), c) s.typ = result - pragma(c, s, n.sons[1], procTypePragmas) + if n.sons[1].kind == nkEmpty or n.sons[1].len == 0: + if result.callConv == ccDefault: + #result.callConv = ccClosure + Message(n.info, warnImplicitClosure, renderTree(n)) + else: + pragma(c, s, n.sons[1], procTypePragmas) closeScope(c.tab) of nkEnumTy: result = semEnum(c, n, prev) of nkType: result = n.typ diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 1b60450bb..af924bafe 100755 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -457,7 +457,9 @@ proc typeRel(c: var TCandidate, f, a: PType): TTypeRelation = case a.kind of tyPointer: result = isEqual of tyNil: result = isSubtype - of tyPtr, tyProc, tyCString: result = isConvertible + of tyProc: + if a.callConv != ccClosure: result = isConvertible + of tyPtr, tyCString: result = isConvertible else: nil of tyString: case a.kind diff --git a/compiler/transf.nim b/compiler/transf.nim index 3c3ae12c9..4e799a11f 100755 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -640,14 +640,6 @@ proc dontInlineConstant(orig, cnst: PNode): bool {.inline.} = result = orig.kind == nkSym and cnst.kind in {nkCurly, nkPar, nkBracket} and cnst.len != 0 -proc warnNarrowingConversion(n: PNode) = - if n.kind == nkHiddenStdConv: - var dest = skipTypes(n.typ, abstractVarRange) - var source = skipTypes(n.sons[1].typ, abstractVarRange) - if source.kind == tyInt and - source.size > dest.size and n.sons[1].kind != nkIntLit: - Message(n.info, warnImplicitNarrowing, renderTree(n.sons[1])) - proc transform(c: PTransf, n: PNode): PTransNode = case n.kind of nkSym: diff --git a/compiler/types.nim b/compiler/types.nim index e2200882d..8b99c00e8 100755 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -24,9 +24,9 @@ proc getProcHeader*(sym: PSym): string proc base*(t: PType): PType # ------------------- type iterator: ---------------------------------------- type - TTypeIter* = proc (t: PType, closure: PObject): bool # should return true if the iteration should stop - TTypeMutator* = proc (t: PType, closure: PObject): PType # copy t and mutate it - TTypePredicate* = proc (t: PType): bool + TTypeIter* = proc (t: PType, closure: PObject): bool {.nimcall.} # true if iteration should stop + TTypeMutator* = proc (t: PType, closure: PObject): PType {.nimcall.} # copy t and mutate it + TTypePredicate* = proc (t: PType): bool {.nimcall.} proc IterOverType*(t: PType, iter: TTypeIter, closure: PObject): bool # Returns result of `iter`. diff --git a/doc/manual.txt b/doc/manual.txt index 543d5d3c1..36e444170 100755 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -1177,6 +1177,17 @@ that expects a proc of the calling convention ``closure``. Nimrod supports these `calling conventions`:idx:\: +`nimcall`:idx: + is the default convention used for a Nimrod **proc**. It is the + same as ``fastcall``, but only for C compilers that support ``fastcall``. + +`closure`:idx: + is the default calling convention for a **procedural type** that lacks + any pragma annotations. It indicates that the procedure has a hidden + implicit parameter (an *environment*). Proc vars that have the calling + convention ``closure`` take up two machine words: One for the proc pointer + and another one for the pointer to implicitely passed environment. + `stdcall`:idx: This the stdcall convention as specified by Microsoft. The generated C procedure is declared with the ``__stdcall`` keyword. @@ -1203,16 +1214,6 @@ Nimrod supports these `calling conventions`:idx:\: Fastcall means different things to different C compilers. One gets whatever the C ``__fastcall`` means. -`nimcall`:idx: - Nimcall is the default convention used for Nimrod procedures. It is the - same as ``fastcall``, but only for C compilers that support ``fastcall``. - -`closure`:idx: - indicates that the procedure has a hidden implicit parameter - (an *environment*). Proc vars that have the calling convention ``closure`` - take up two machine words: One for the proc pointer and another one for - the pointer to implicitely passed environment. - `syscall`:idx: The syscall convention is the same as ``__syscall`` in C. It is used for interrupts. @@ -3589,7 +3590,7 @@ strings automatically: Dynlib pragma for import ------------------------ -With the `dynlib`:idx: pragma a procedure can be imported from +With the `dynlib`:idx: pragma a procedure or a variable can be imported from a dynamic library (``.dll`` files for Windows, ``lib*.so`` files for UNIX). The non-optional argument has to be the name of the dynamic library: diff --git a/examples/pythonex.nim b/examples/pythonex.nim index e2664f350..310d80151 100755 --- a/examples/pythonex.nim +++ b/examples/pythonex.nim @@ -9,4 +9,3 @@ Py_Initialize() discard PyRun_SimpleString("from time import time,ctime\L" & "print 'Today is',ctime(time())\L") Py_Finalize() - diff --git a/examples/x11ex.nim b/examples/x11ex.nim index a32094be4..eb4ae9274 100755 --- a/examples/x11ex.nim +++ b/examples/x11ex.nim @@ -5,7 +5,7 @@ const WINDOW_HEIGHT = 300 var - width, height: cint + width, height: cuint display: PDisplay screen: cint depth: int @@ -29,10 +29,10 @@ proc create_window = XBlackPixel(display, screen), XWhitePixel(display, screen)) size_hints.flags = PSize or PMinSize or PMaxSize - size_hints.min_width = width - size_hints.max_width = width - size_hints.min_height = height - size_hints.max_height = height + size_hints.min_width = width.cint + size_hints.max_width = width.cint + size_hints.min_height = height.cint + size_hints.max_height = height.cint discard XSetStandardProperties(display, win, "Simple Window", "window", 0, nil, 0, addr(size_hints)) discard XSelectInput(display, win, ButtonPressMask or KeyPressMask or diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index b1064af0d..03eb7da39 100755 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -56,7 +56,7 @@ const onlySafeCode = true proc merge[T](a, b: var openArray[T], lo, m, hi: int, - cmp: proc (x, y: T): int, order: TSortOrder) = + cmp: proc (x, y: T): int {.closure.}, order: TSortOrder) = template `<-` (a, b: expr) = when false: a = b @@ -102,7 +102,7 @@ proc merge[T](a, b: var openArray[T], lo, m, hi: int, if k < j: copyMem(addr(a[k]), addr(b[i]), sizeof(T)*(j-k)) proc sort*[T](a: var openArray[T], - cmp: proc (x, y: T): int, + cmp: proc (x, y: T): int {.closure.}, order = TSortOrder.Ascending) = ## Default Nimrod sort. The sorting is guaranteed to be stable and ## the worst case is guaranteed to be O(n log n). diff --git a/lib/pure/asyncio.nim b/lib/pure/asyncio.nim index 417a220f9..ac94f2087 100644 --- a/lib/pure/asyncio.nim +++ b/lib/pure/asyncio.nim @@ -33,14 +33,14 @@ type TDelegate = object deleVal*: PObject - handleRead*: proc (h: PObject) - handleWrite*: proc (h: PObject) - handleConnect*: proc (h: PObject) + handleRead*: proc (h: PObject) {.nimcall.} + handleWrite*: proc (h: PObject) {.nimcall.} + handleConnect*: proc (h: PObject) {.nimcall.} - handleAccept*: proc (h: PObject) - getSocket*: proc (h: PObject): tuple[info: TInfo, sock: TSocket] + handleAccept*: proc (h: PObject) {.nimcall.} + getSocket*: proc (h: PObject): tuple[info: TInfo, sock: TSocket] {.nimcall.} - task*: proc (h: PObject) + task*: proc (h: PObject) {.nimcall.} mode*: TMode PDelegate* = ref TDelegate @@ -56,10 +56,10 @@ type userArg: PObject - handleRead*: proc (s: PAsyncSocket, arg: PObject) - handleConnect*: proc (s: PAsyncSocket, arg: PObject) + handleRead*: proc (s: PAsyncSocket, arg: PObject) {.nimcall.} + handleConnect*: proc (s: PAsyncSocket, arg: PObject) {.nimcall.} - handleAccept*: proc (s: PAsyncSocket, arg: PObject) + handleAccept*: proc (s: PAsyncSocket, arg: PObject) {.nimcall.} lineBuffer: TaintedString ## Temporary storage for ``recvLine`` diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index e3034d635..04eab419b 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -39,14 +39,14 @@ proc zip*[S, T](seq1: seq[S], seq2: seq[T]): seq[tuple[a: S, b: T]] = newSeq(result, m) for i in 0 .. m-1: result[i] = (seq1[i], seq2[i]) -iterator filter*[T](seq1: seq[T], pred: proc(item: T): bool): T = +iterator filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): T = ## Iterates through a sequence and yields every item that fulfills the ## predicate. for i in countup(0, len(seq1) -1): var item = seq1[i] if pred(item): yield seq1[i] -proc filter*[T](seq1: seq[T], pred: proc(item: T): bool): seq[T] = +proc filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): seq[T] = ## Returns all items in a sequence that fulfilled the predicate. accumulateResult(filter(seq1, pred)) diff --git a/lib/pure/events.nim b/lib/pure/events.nim index c3c4cb0dc..894091bf7 100644 --- a/lib/pure/events.nim +++ b/lib/pure/events.nim @@ -11,7 +11,8 @@ ## ## This module implements an event system that is not dependant on external ## graphical toolkits. It was originally called ``NimEE`` because -## it was inspired by Python's PyEE module. There are two ways you can use events: one is a python-inspired way; the other is more of a C-style way. +## it was inspired by Python's PyEE module. There are two ways you can use +## events: one is a python-inspired way; the other is more of a C-style way. ## ## .. code-block:: Nimrod ## var ee = initEventEmitter() @@ -44,22 +45,21 @@ type proc initEventHandler*(name: string): TEventHandler = ## Initializes an EventHandler with the specified name and returns it. - #new(result) result.handlers = @[] result.name = name -proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) = +proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}) = ## Adds the callback to the specified event handler. handler.handlers.add(func) -proc removeHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) = +proc removeHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}) = ## Removes the callback from the specified event handler. for i in countup(0, len(handler.handlers) -1): if func == handler.handlers[i]: handler.handlers.del(i) break -proc containsHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)): bool = +proc containsHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}): bool = ## Checks if a callback is registered to this event handler. return handler.handlers.contains(func) @@ -73,7 +73,7 @@ proc getEventhandler(emitter: var TEventEmitter, event: string): int = if emitter.s[k].name == event: return k return -1 -proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs)) = +proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs) {.closure.}) = ## Assigns a event handler with the specified callback. If the event ## doesn't exist, it will be created. var i = getEventHandler(emitter, event) @@ -99,5 +99,4 @@ proc emit*(emitter: var TEventEmitter, event: string, args: TEventArgs) = proc initEventEmitter*(): TEventEmitter = ## Creates and returns a new EventEmitter. - #new(result) result.s = @[] diff --git a/lib/pure/httpserver.nim b/lib/pure/httpserver.nim index 046844db4..afed45d39 100755 --- a/lib/pure/httpserver.nim +++ b/lib/pure/httpserver.nim @@ -347,7 +347,8 @@ proc close*(s: TServer) = ## closes the server (and the socket the server uses). close(s.socket) -proc run*(handleRequest: proc (client: TSocket, path, query: string): bool, +proc run*(handleRequest: proc (client: TSocket, + path, query: string): bool {.closure.}, port = TPort(80)) = ## encapsulates the server object and main loop var s: TServer diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 78800165a..6e2e19be5 100755 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -23,13 +23,14 @@ type ## here shouldn't be used directly. They are ## accessible so that a stream implementation ## can override them. - closeImpl*: proc (s: PStream) - atEndImpl*: proc (s: PStream): bool - setPositionImpl*: proc (s: PStream, pos: int) - getPositionImpl*: proc (s: PStream): int - readDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int): int - writeDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int) - flushImpl*: proc (s: PStream) + closeImpl*: proc (s: PStream) {.nimcall.} + atEndImpl*: proc (s: PStream): bool {.nimcall.} + setPositionImpl*: proc (s: PStream, pos: int) {.nimcall.} + getPositionImpl*: proc (s: PStream): int {.nimcall.} + readDataImpl*: proc (s: PStream, buffer: pointer, + bufLen: int): int {.nimcall.} + writeDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int) {.nimcall.} + flushImpl*: proc (s: PStream) {.nimcall.} proc flush*(s: PStream) = ## flushes the buffers that the stream `s` might use. diff --git a/lib/system.nim b/lib/system.nim index 6cb99b6cf..6e8fd00b3 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -111,7 +111,7 @@ proc new*[T](a: var ref T) {.magic: "New", noSideEffect.} proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.} ## leaked implementation detail. Do not use. -proc new*[T](a: var ref T, finalizer: proc (x: ref T)) {. +proc new*[T](a: var ref T, finalizer: proc (x: ref T) {.nimcall.}) {. magic: "NewFinalize", noSideEffect.} ## creates a new object of type ``T`` and returns a safe (traced) ## reference to it in ``a``. When the garbage collector frees the object, @@ -1416,13 +1416,13 @@ proc pop*[T](s: var seq[T]): T {.inline, noSideEffect.} = result = s[L] setLen(s, L) -proc each*[T, S](data: openArray[T], op: proc (x: T): S): seq[S] = +proc each*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] = ## The well-known ``map`` operation from functional programming. Applies ## `op` to every item in `data` and returns the result as a sequence. newSeq(result, data.len) for i in 0..data.len-1: result[i] = op(data[i]) -proc each*[T](data: var openArray[T], op: proc (x: var T)) = +proc each*[T](data: var openArray[T], op: proc (x: var T) {.closure.}) = ## The well-known ``map`` operation from functional programming. Applies ## `op` to every item in `data`. for i in 0..data.len-1: op(data[i]) @@ -1567,11 +1567,11 @@ const nimrodStackTrace = compileOption("stacktrace") # of the code var - dbgLineHook*: proc () + dbgLineHook*: proc () {.nimcall.} ## set this variable to provide a procedure that should be called before ## each executed instruction. This should only be used by debuggers! ## Only code compiled with the ``debugger:on`` switch calls this hook. - globalRaiseHook*: proc (e: ref E_Base): bool + globalRaiseHook*: proc (e: ref E_Base): bool {.nimcall.} ## with this hook you can influence exception handling on a global level. ## If not nil, every 'raise' statement ends up calling this hook. Ordinary ## application code should never set this hook! You better know what you @@ -1579,7 +1579,7 @@ var ## exception is caught and does not propagate further through the call ## stack. - localRaiseHook* {.threadvar.}: proc (e: ref E_Base): bool + localRaiseHook* {.threadvar.}: proc (e: ref E_Base): bool {.nimcall.} ## with this hook you can influence exception handling on a ## thread local level. ## If not nil, every 'raise' statement ends up calling this hook. Ordinary @@ -1587,7 +1587,7 @@ var ## do when setting this. If ``localRaiseHook`` returns false, the exception ## is caught and does not propagate further through the call stack. - outOfMemHook*: proc () + outOfMemHook*: proc () {.nimcall.} ## set this variable to provide a procedure that should be called ## in case of an `out of memory`:idx: event. The standard handler ## writes an error message and terminates the program. `outOfMemHook` can diff --git a/lib/system/gc.nim b/lib/system/gc.nim index 1c6caeb77..18a7a81ed 100755 --- a/lib/system/gc.nim +++ b/lib/system/gc.nim @@ -45,7 +45,7 @@ type TWalkOp = enum waZctDecRef, waPush, waCycleDecRef - TFinalizer {.compilerproc.} = proc (self: pointer) + TFinalizer {.compilerproc.} = proc (self: pointer) {.nimcall.} # A ref type can have a finalizer that is called before the object's # storage is freed. diff --git a/lib/system/hti.nim b/lib/system/hti.nim index d79679107..93dc79e3d 100755 --- a/lib/system/hti.nim +++ b/lib/system/hti.nim @@ -62,7 +62,7 @@ type # This should be he same as ast.TTypeKind base: ptr TNimType node: ptr TNimNode # valid for tyRecord, tyObject, tyTuple, tyEnum finalizer: pointer # the finalizer for the type - marker: proc (p: pointer, op: int) # marker proc for GC + marker: proc (p: pointer, op: int) {.nimcall.} # marker proc for GC PNimType = ptr TNimType # node.len may be the ``first`` element of a set diff --git a/lib/system/threads.nim b/lib/system/threads.nim index e0812f41c..0151755c9 100755 --- a/lib/system/threads.nim +++ b/lib/system/threads.nim @@ -255,9 +255,9 @@ type ## that **must not** be part of a message! Use ## a ``TThreadId`` for that. when TArg is void: - dataFn: proc () + dataFn: proc () {.nimcall.} else: - dataFn: proc (m: TArg) + dataFn: proc (m: TArg) {.nimcall.} data: TArg TThreadId*[TArg] = ptr TThread[TArg] ## the current implementation uses ## a pointer as a thread ID. diff --git a/lib/wrappers/gtk/atk.nim b/lib/wrappers/gtk/atk.nim index 3ae687aed..f59d95f87 100755 --- a/lib/wrappers/gtk/atk.nim +++ b/lib/wrappers/gtk/atk.nim @@ -419,7 +419,7 @@ type pad32*: TFunction TEventListener* = proc (para1: PObject){.cdecl.} - TEventListenerInitProc* = proc () + TEventListenerInitProc* = proc (){.cdecl.} TEventListenerInit* = proc (para1: TEventListenerInitProc){.cdecl.} PKeyEventStruct* = ptr TKeyEventStruct TKeyEventStruct*{.final, pure.} = object diff --git a/lib/wrappers/gtk/gdk2.nim b/lib/wrappers/gtk/gdk2.nim index 67ee5b15c..d84f7ccc3 100755 --- a/lib/wrappers/gtk/gdk2.nim +++ b/lib/wrappers/gtk/gdk2.nim @@ -291,7 +291,7 @@ type PEvent* = ptr TEvent TEventFunc* = proc (event: PEvent, data: gpointer){.cdecl.} PXEvent* = ptr TXEvent - TXEvent* = proc () + TXEvent* = proc () {.cdecl.} PFilterReturn* = ptr TFilterReturn TFilterReturn* = enum FILTER_CONTINUE, FILTER_TRANSLATE, FILTER_REMOVE @@ -775,7 +775,7 @@ type PWindowObjectClass* = ptr TWindowObjectClass TWindowObjectClass* = object of TDrawableClass window_invalidate_maybe_recurse_child_func* = proc (para1: PWindow, - para2: gpointer): gboolean + para2: gpointer): gboolean {.cdecl.} proc TYPE_COLORMAP*(): GType proc COLORMAP*(anObject: pointer): PColormap diff --git a/lib/wrappers/gtk/glib2.nim b/lib/wrappers/gtk/glib2.nim index 57d561432..4d3d32b29 100755 --- a/lib/wrappers/gtk/glib2.nim +++ b/lib/wrappers/gtk/glib2.nim @@ -2676,7 +2676,7 @@ when false: proc g_critical*(format: cstring){.varargs.} proc g_warning*(format: cstring){.varargs.} type - TGPrintFunc* = proc (str: cstring) + TGPrintFunc* = proc (str: cstring){.cdecl, varargs.} proc g_set_print_handler*(func: TGPrintFunc): TGPrintFunc{.cdecl, dynlib: gliblib, importc: "g_set_print_handler".} diff --git a/lib/wrappers/libcurl.nim b/lib/wrappers/libcurl.nim index 875f1cbc8..bd8616759 100755 --- a/lib/wrappers/libcurl.nim +++ b/lib/wrappers/libcurl.nim @@ -104,7 +104,7 @@ type Tfree_callback* = proc (p: pointer){.cdecl.} Trealloc_callback* = proc (p: pointer, size: int): pointer{.cdecl.} Tstrdup_callback* = proc (str: cstring): cstring{.cdecl.} - Tcalloc_callback* = proc (nmemb: int, size: int): pointer + Tcalloc_callback* = proc (nmemb: int, size: int): pointer{.noconv.} Tinfotype* = enum INFO_TEXT = 0, INFO_HEADER_IN, INFO_HEADER_OUT, INFO_DATA_IN, INFO_DATA_OUT, INFO_SSL_DATA_IN, INFO_SSL_DATA_OUT, INFO_END diff --git a/lib/wrappers/mysql.nim b/lib/wrappers/mysql.nim index 2a7a10c58..5a8d4c98b 100755 --- a/lib/wrappers/mysql.nim +++ b/lib/wrappers/mysql.nim @@ -8,6 +8,8 @@ # {.deadCodeElim: on.} +{.push, callconv: cdecl.} + when defined(Unix): const lib = "libmysqlclient.so.15" @@ -1071,3 +1073,5 @@ proc INTERNAL_NUM_FIELD(f: Pst_mysql_field): bool = proc reload(mysql: PMySQL): cint = result = refresh(mysql, REFRESH_GRANT) + +{.pop.} diff --git a/lib/wrappers/opengl/glu.nim b/lib/wrappers/opengl/glu.nim index 29edb0df2..e00120d83 100755 --- a/lib/wrappers/opengl/glu.nim +++ b/lib/wrappers/opengl/glu.nim @@ -12,6 +12,11 @@ import GL when defined(windows): + {.push, callconv: stdcall.} +else: + {.push, callconv: cdecl.} + +when defined(windows): const dllname = "glu32.dll" elif defined(macosx): @@ -326,4 +331,5 @@ const # Contours types -- obsolete! GLU_ERROR* = GLU_TESS_ERROR GLU_EDGE_FLAG* = GLU_TESS_EDGE_FLAG +{.pop.} # implementation diff --git a/lib/wrappers/python.nim b/lib/wrappers/python.nim index 0801b1ae5..3d0b923e2 100755 --- a/lib/wrappers/python.nim +++ b/lib/wrappers/python.nim @@ -60,12 +60,14 @@ import when defined(windows): - const dllname = "python(26|25|24|23|22|21|20|16|15).dll" + const dllname = "python(27|26|25|24|23|22|21|20|16|15).dll" elif defined(macosx): - const dllname = "libpython(2.6|2.5|2.4|2.3|2.2|2.1|2.0|1.6|1.5).dylib" + const dllname = "libpython(2.7|2.6|2.5|2.4|2.3|2.2|2.1|2.0|1.6|1.5).dylib" else: const dllver = ".1" - const dllname = "libpython(2.6|2.5|2.4|2.3|2.2|2.1|2.0|1.6|1.5).so" & dllver + const dllname = "libpython(2.7|2.6|2.5|2.4|2.3|2.2|2.1|2.0|1.6|1.5).so" & + dllver + const PYT_METHOD_BUFFER_INCREASE* = 10 @@ -1154,7 +1156,7 @@ proc PyWeakref_NewProxy*(ob, callback: PPyObject): PPyObject{.cdecl, importc, dy proc PyWeakref_NewRef*(ob, callback: PPyObject): PPyObject{.cdecl, importc, dynlib: dllname.} proc PyWrapper_New*(ob1, ob2: PPyObject): PPyObject{.cdecl, importc, dynlib: dllname.} proc PyBool_FromLong*(ok: int): PPyObject{.cdecl, importc, dynlib: dllname.} #- -proc Py_AtExit*(prc: proc ()): int{.cdecl, importc, dynlib: dllname.} #- +proc Py_AtExit*(prc: proc () {.cdecl.}): int{.cdecl, importc, dynlib: dllname.} #- #Py_Cleanup:procedure; cdecl, importc, dynlib: dllname; #- proc Py_CompileString*(s1, s2: cstring, i: int): PPyObject{.cdecl, importc, dynlib: dllname.} #- @@ -1545,26 +1547,28 @@ proc init(lib: TLibHandle) = PyEnum_Type = cast[PPyTypeObject](symAddr(lib, "PyEnum_Type")) # Unfortunately we have to duplicate the loading mechanism here, because Nimrod -# does not support variables from dynamic libraries. Well designed API's don't -# require this anyway. Python is an exception. +# used to not support variables from dynamic libraries. Well designed API's +# don't require this anyway. Python is an exception. var lib: TLibHandle -when defined(windows): +when defined(windows): const - LibNames = ["python26.dll", "python25.dll", - "python24.dll", "python23.dll", "python22.dll", "python21.dll", + LibNames = ["python27.dll", "python26.dll", "python25.dll", + "python24.dll", "python23.dll", "python22.dll", "python21.dll", "python20.dll", "python16.dll", "python15.dll"] elif defined(macosx): const - LibNames = ["libpython2.6.dylib", "libpython2.5.dylib", - "libpython2.4.dylib", "libpython2.3.dylib", "libpython2.2.dylib", - "libpython2.1.dylib", "libpython2.0.dylib", + LibNames = ["libpython2.7.dylib", "libpython2.6.dylib", + "libpython2.5.dylib", "libpython2.4.dylib", "libpython2.3.dylib", + "libpython2.2.dylib", "libpython2.1.dylib", "libpython2.0.dylib", "libpython1.6.dylib", "libpython1.5.dylib"] -else: +else: const - LibNames = ["libpython2.6.so" & dllver, + LibNames = [ + "libpython2.7.so" & dllver, + "libpython2.6.so" & dllver, "libpython2.5.so" & dllver, "libpython2.4.so" & dllver, "libpython2.3.so" & dllver, diff --git a/lib/wrappers/sdl/sdl.nim b/lib/wrappers/sdl/sdl.nim index 597a9b0b0..a6f8de5d7 100755 --- a/lib/wrappers/sdl/sdl.nim +++ b/lib/wrappers/sdl/sdl.nim @@ -1279,7 +1279,6 @@ type # This is the system-independent thread info struc TByteArray* = array[0..32767, int8] PWordArray* = ptr TWordArray TWordArray* = array[0..16383, int16] # Generic procedure pointer - TProcedure* = proc () type TEventSeq = set[TEventKind] template evconv(procName: expr, ptrName: typeDesc, assertions: TEventSeq): stmt {.immediate.} = diff --git a/lib/wrappers/x11/xresource.nim b/lib/wrappers/x11/xresource.nim index ddb2a89c2..f553b4413 100755 --- a/lib/wrappers/x11/xresource.nim +++ b/lib/wrappers/x11/xresource.nim @@ -137,7 +137,7 @@ const XrmEnumOneLevel* = 1 type - funcbool* = proc (): TBool + funcbool* = proc (): TBool {.cdecl.} proc XrmEnumerateDatabase*(para1: TXrmDatabase, para2: TXrmNameList, para3: TXrmClassList, para4: int32, para5: funcbool, diff --git a/packages/docutils/rst.nim b/packages/docutils/rst.nim index 126b21bd6..ed8e2b7a9 100755 --- a/packages/docutils/rst.nim +++ b/packages/docutils/rst.nim @@ -42,8 +42,8 @@ type mwUnsupportedLanguage TMsgHandler* = proc (filename: string, line, col: int, msgKind: TMsgKind, - arg: string) ## what to do in case of an error - TFindFileHandler* = proc (filename: string): string + arg: string) {.nimcall.} ## what to do in case of an error + TFindFileHandler* = proc (filename: string): string {.nimcall.} const messages: array [TMsgKind, string] = [ @@ -1353,10 +1353,10 @@ proc parseDoc(p: var TRstParser): PRstNode = if p.tok[p.idx].kind != tkEof: rstMessage(p, meGeneralParseError) type - TDirFlag = enum + TDirFlag = enum hasArg, hasOptions, argIsFile, argIsWord TDirFlags = set[TDirFlag] - TSectionParser = proc (p: var TRstParser): PRstNode + TSectionParser = proc (p: var TRstParser): PRstNode {.nimcall.} proc parseDirective(p: var TRstParser, flags: TDirFlags): PRstNode = result = newRstNode(rnDirective) diff --git a/tests/compile/tnewlibs.nim b/tests/compile/tnewlibs.nim index ab358f557..041316290 100755 --- a/tests/compile/tnewlibs.nim +++ b/tests/compile/tnewlibs.nim @@ -10,7 +10,8 @@ import osproc, cairowin32, cairoxlib, gl, glut, glu, glx, glext, wingl, - lua, lualib, lauxlib, mysql, sqlite3, db_mongo, md5 + lua, lualib, lauxlib, mysql, sqlite3, db_mongo, md5, asyncio, mimetypes, + cookies, events, ftpclient writeln(stdout, "test compilation of binding modules") diff --git a/tests/run/tclosure3.nim b/tests/run/tclosure3.nim index 6dee49aea..bb217387f 100644 --- a/tests/run/tclosure3.nim +++ b/tests/run/tclosure3.nim @@ -1,19 +1,18 @@ discard """ file: "tclosure3.nim" output: "success" - disabled: true """ proc main = const n = 30 for iterations in 0..50_000: - var s: seq[proc(): int {.closure.}] = @[] + var s: seq[proc(): string {.closure.}] = @[] for i in 0 .. n-1: let ii = i - s.add(proc(): int = return ii*ii) + s.add(proc(): string = return $(ii*ii)) for i in 0 .. n-1: let val = s[i]() - if val != i*i: echo "bug ", val + if val != $(i*i): echo "bug ", val if getOccupiedMem() > 3000_000: quit("still a leak!") echo "success" diff --git a/tests/run/teventemitter.nim b/tests/run/teventemitter.nim index 8394776a8..9ecf72ea2 100644 --- a/tests/run/teventemitter.nim +++ b/tests/run/teventemitter.nim @@ -7,20 +7,22 @@ import tables, lists type TEventArgs = object of TObject TEventEmitter = object of TObject - events*: TTable[string, TDoublyLinkedList[proc(e: TEventArgs)]] + events*: TTable[string, TDoublyLinkedList[proc(e: TEventArgs) {.nimcall.}]] proc emit*(emitter: TEventEmitter, event: string, args: TEventArgs) = for func in nodes(emitter.events[event]): func.value(args) #call function with args. -proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs)) = +proc on*(emitter: var TEventEmitter, event: string, + func: proc(e: TEventArgs) {.nimcall.}) = if not hasKey(emitter.events, event): - var list: TDoublyLinkedList[proc(e: TEventArgs)] + var list: TDoublyLinkedList[proc(e: TEventArgs) {.nimcall.}] add(emitter.events, event, list) #if not, add it. append(emitter.events.mget(event), func) proc initEmitter(emitter: var TEventEmitter) = - emitter.events = initTable[string, TDoublyLinkedList[proc(e: TEventArgs)]]() + emitter.events = initTable[string, + TDoublyLinkedList[proc(e: TEventArgs) {.nimcall.}]]() var ee: TEventEmitter diff --git a/todo.txt b/todo.txt index 751f8413d..5aa3193fb 100755 --- a/todo.txt +++ b/todo.txt @@ -1,10 +1,8 @@ version 0.9.0 ============= -- implement ``dynlib`` for variables - implicit deref for parameter matching - deprecate ``var x, y = 0`` as it's confusing for tuple consistency -- test sequence of closures; especially that the GC does not leak for those! New pragmas: - ``hoist`` pragma for loop hoisting @@ -18,8 +16,9 @@ New pragmas: - fix remaining closure bugs: - make toplevel but in a scope vars local; make procs there inner procs - fix evals.nim with closures - - proc types shall have closure calling convention per default - implement "closure tuple consists of a single 'ref'" optimization + - make closure default calling convention for proc types + - fix '==' for closures - document 'do' notation - rethink the syntax: distinction between expr and stmt is unfortunate; diff --git a/web/news.txt b/web/news.txt index be9e5bf08..df81b952c 100755 --- a/web/news.txt +++ b/web/news.txt @@ -112,6 +112,7 @@ Compiler Additions via the ``doc2`` command. This new generator uses all of the semantic passes of the compiler and can thus generate documentation for symbols hiding in macros. +- The compiler now supports the ``dynlib`` pragma for variables. Language Additions |