diff options
-rw-r--r-- | compiler/parser.nim | 2 | ||||
-rw-r--r-- | compiler/semtypes.nim | 7 | ||||
-rw-r--r-- | doc/manual.txt | 15 | ||||
-rw-r--r-- | tests/typerel/tregionptrs.nim | 4 |
4 files changed, 17 insertions, 11 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim index 07f5c9de9..c68c80b46 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -220,7 +220,7 @@ proc getPrecedence(tok: TToken, strongSpaces: bool): int = of tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs: result = 5 of tkDotDot: result = considerStrongSpaces(6) of tkAnd: result = 4 - of tkOr, tkXor: result = 3 + of tkOr, tkXor, tkPtr, tkRef: result = 3 else: result = -10 proc isOperator(tok: TToken): bool = diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 679a01699..5e391cc93 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -125,11 +125,12 @@ proc semAnyRef(c: PContext; n: PNode; kind: TTypeKind; prev: PType): PType = if n.len < 1: result = newConstraint(c, kind) else: + let isCall = ord(n.kind in nkCallKinds) let n = if n[0].kind == nkBracket: n[0] else: n checkMinSonsLen(n, 1) result = newOrPrevType(kind, prev, c) # check every except the last is an object: - for i in 0 .. n.len-2: + for i in isCall .. n.len-2: let region = semTypeNode(c, n[i], nil) if region.skipTypes({tyGenericInst}).kind notin {tyError, tyObject}: message n[i].info, errGenerated, "region needs to be an object type" @@ -1107,6 +1108,10 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType = result = makeNotType(c, negated) else: localError(n.info, errGenerated, "invalid type") + elif op.id == ord(wPtr): + result = semAnyRef(c, n, tyPtr, prev) + elif op.id == ord(wRef): + result = semAnyRef(c, n, tyRef, prev) else: result = semTypeExpr(c, n) else: diff --git a/doc/manual.txt b/doc/manual.txt index 775679788..c0d4f2627 100644 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -1380,13 +1380,16 @@ development of OS kernels: Kernel = object Userspace = object - var a: ptr[Kernel, Stat] - var b: ptr[Userspace, Stat] + var a: Kernel ptr Stat + var b: Userspace ptr Stat # the following does not compile as the pointer types are incompatible: a = b -In order to make generic code easier tor write ``ptr T`` is a subtype +As the example shows ``ptr`` can also be used as a binary +operator, ``region ptr T`` is a shortcut for ``ptr[region, T]``. + +In order to make generic code easier to write ``ptr T`` is a subtype of ``ptr[R, T]`` for any ``R``. Furthermore the subtype relation of the region object types is lifted to @@ -1417,11 +1420,9 @@ not compatible to ``pointer`` to prevent the following from compiling: Future directions: * Memory regions might become available for ``string`` and ``seq`` too. -* Bultin regions like ``private``, ``global`` and ``local`` will +* Builtin regions like ``private``, ``global`` and ``local`` will prove very useful for the upcoming OpenCL target. -* Bultin "regions" can model ``lent`` and ``unique`` pointers. -* Syntactially ``ptr`` might become an infix operator so that ``region ptr T`` - is transformed into ``ptr[region, T]``. +* Builtin "regions" can model ``lent`` and ``unique`` pointers. diff --git a/tests/typerel/tregionptrs.nim b/tests/typerel/tregionptrs.nim index cc02a92e7..07387fed8 100644 --- a/tests/typerel/tregionptrs.nim +++ b/tests/typerel/tregionptrs.nim @@ -5,9 +5,9 @@ discard """ type RegionA = object - APtr = ptr[RegionA, int] + APtr = RegionA ptr int RegionB = object - BPtr = ptr[RegionB, int] + BPtr = RegionB ptr int var x,xx: APtr var y: BPtr |