implemented 'R ptr T' syntax

This commit is contained in:
Araq
2014-04-13 22:31:49 +02:00
parent 2e9950afe8
commit d96f25619a
4 changed files with 17 additions and 11 deletions

View File

@@ -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 =

View File

@@ -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:

View File

@@ -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.

View File

@@ -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