implemented 'pure' enums

This commit is contained in:
Araq 2012-09-08 20:51:50 +02:00
commit 580d85d233
3 changed files with 21 additions and 5 deletions

View file

@ -38,6 +38,7 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
localError(n.sons[0].info, errInheritanceOnlyWithEnums) localError(n.sons[0].info, errInheritanceOnlyWithEnums)
counter = lastOrd(base) + 1 counter = lastOrd(base) + 1
rawAddSon(result, base) rawAddSon(result, base)
let isPure = result.sym != nil and sfPure in result.sym.flags
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n) - 1):
case n.sons[i].kind case n.sons[i].kind
of nkEnumFieldDef: of nkEnumFieldDef:
@ -73,12 +74,12 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
else: illFormedAst(n) else: illFormedAst(n)
e.typ = result e.typ = result
e.position = int(counter) e.position = int(counter)
if result.sym != nil and sfExported in result.sym.flags: if result.sym != nil and sfExported in result.sym.flags:
incl(e.flags, sfUsed) # BUGFIX incl(e.flags, sfUsed)
incl(e.flags, sfExported) # BUGFIX incl(e.flags, sfExported)
StrTableAdd(c.module.tab, e) # BUGFIX if not isPure: StrTableAdd(c.module.tab, e)
addSon(result.n, newSymNode(e)) addSon(result.n, newSymNode(e))
if sfGenSym notin e.flags: addDeclAt(c, e, c.tab.tos - 1) if sfGenSym notin e.flags and not isPure: addDeclAt(c, e, c.tab.tos - 1)
inc(counter) inc(counter)
proc semSet(c: PContext, n: PNode, prev: PType): PType = proc semSet(c: PContext, n: PNode, prev: PType): PType =

View file

@ -795,6 +795,19 @@ values to use:
As can be seen from the example, it is possible to both specify a field's As can be seen from the example, it is possible to both specify a field's
ordinal value and its string value by using a tuple. It is also ordinal value and its string value by using a tuple. It is also
possible to only specify one of them. possible to only specify one of them.
An enum can be marked with the ``pure`` pragma so that it's fields are not
added to the current scope, so they always need to be accessed
via ``TMyEnum.value``:
.. code-block:: nimrod
type
TMyEnum {.pure.} = enum
valueA, valueB, valueC, valueD
echo valueA # error: Unknown identifier
echo TMyEnum.valueA # works
String type String type

View file

@ -45,6 +45,8 @@ version 0.9.XX
- JS gen: - JS gen:
- fix exception handling - fix exception handling
- object branch transitions can't work with the current 'reset'; add a 'reset'
with an additional parameter
- fix remaining closure bugs: - fix remaining closure bugs:
- test evals.nim with closures - test evals.nim with closures
- what about macros with closures? - what about macros with closures?