implemented 'R ptr T' syntax
This commit is contained in:
parent
2e9950afe8
commit
d96f25619a
4 changed files with 17 additions and 11 deletions
|
|
@ -220,7 +220,7 @@ proc getPrecedence(tok: TToken, strongSpaces: bool): int =
|
||||||
of tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs: result = 5
|
of tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs: result = 5
|
||||||
of tkDotDot: result = considerStrongSpaces(6)
|
of tkDotDot: result = considerStrongSpaces(6)
|
||||||
of tkAnd: result = 4
|
of tkAnd: result = 4
|
||||||
of tkOr, tkXor: result = 3
|
of tkOr, tkXor, tkPtr, tkRef: result = 3
|
||||||
else: result = -10
|
else: result = -10
|
||||||
|
|
||||||
proc isOperator(tok: TToken): bool =
|
proc isOperator(tok: TToken): bool =
|
||||||
|
|
|
||||||
|
|
@ -125,11 +125,12 @@ proc semAnyRef(c: PContext; n: PNode; kind: TTypeKind; prev: PType): PType =
|
||||||
if n.len < 1:
|
if n.len < 1:
|
||||||
result = newConstraint(c, kind)
|
result = newConstraint(c, kind)
|
||||||
else:
|
else:
|
||||||
|
let isCall = ord(n.kind in nkCallKinds)
|
||||||
let n = if n[0].kind == nkBracket: n[0] else: n
|
let n = if n[0].kind == nkBracket: n[0] else: n
|
||||||
checkMinSonsLen(n, 1)
|
checkMinSonsLen(n, 1)
|
||||||
result = newOrPrevType(kind, prev, c)
|
result = newOrPrevType(kind, prev, c)
|
||||||
# check every except the last is an object:
|
# 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)
|
let region = semTypeNode(c, n[i], nil)
|
||||||
if region.skipTypes({tyGenericInst}).kind notin {tyError, tyObject}:
|
if region.skipTypes({tyGenericInst}).kind notin {tyError, tyObject}:
|
||||||
message n[i].info, errGenerated, "region needs to be an object type"
|
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)
|
result = makeNotType(c, negated)
|
||||||
else:
|
else:
|
||||||
localError(n.info, errGenerated, "invalid type")
|
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:
|
else:
|
||||||
result = semTypeExpr(c, n)
|
result = semTypeExpr(c, n)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -1380,13 +1380,16 @@ development of OS kernels:
|
||||||
Kernel = object
|
Kernel = object
|
||||||
Userspace = object
|
Userspace = object
|
||||||
|
|
||||||
var a: ptr[Kernel, Stat]
|
var a: Kernel ptr Stat
|
||||||
var b: ptr[Userspace, Stat]
|
var b: Userspace ptr Stat
|
||||||
|
|
||||||
# the following does not compile as the pointer types are incompatible:
|
# the following does not compile as the pointer types are incompatible:
|
||||||
a = b
|
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``.
|
of ``ptr[R, T]`` for any ``R``.
|
||||||
|
|
||||||
Furthermore the subtype relation of the region object types is lifted to
|
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:
|
Future directions:
|
||||||
|
|
||||||
* Memory regions might become available for ``string`` and ``seq`` too.
|
* 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.
|
prove very useful for the upcoming OpenCL target.
|
||||||
* Bultin "regions" can model ``lent`` and ``unique`` pointers.
|
* Builtin "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]``.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ discard """
|
||||||
|
|
||||||
type
|
type
|
||||||
RegionA = object
|
RegionA = object
|
||||||
APtr = ptr[RegionA, int]
|
APtr = RegionA ptr int
|
||||||
RegionB = object
|
RegionB = object
|
||||||
BPtr = ptr[RegionB, int]
|
BPtr = RegionB ptr int
|
||||||
|
|
||||||
var x,xx: APtr
|
var x,xx: APtr
|
||||||
var y: BPtr
|
var y: BPtr
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue