summary refs log tree commit diff stats
path: root/compiler/idgen.nim
blob: fbf450c903d844bffa0c1a423538136b14601675 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff
#
#
#           The Nimrod Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module contains a simple persistent id generator.

import idents, strutils, os, options

var gFrontEndId, gBackendId*: int

const
  debugIds* = false

when debugIds:
  import intsets
  
  var usedIds = InitIntSet()

proc registerID*(id: PIdObj) = 
  when debugIDs: 
    if id.id == -1 or ContainsOrIncl(usedIds, id.id): 
      InternalError("ID already used: " & $id.id)

proc getID*(): int {.inline.} = 
  result = gFrontEndId
  inc(gFrontEndId)

proc backendId*(): int {.inline.} = 
  result = gBackendId
  inc(gBackendId)

proc setId*(id: int)
gFrontEndId, id + 1) proc IDsynchronizationPoint*(idRange: int) = gFrontEndId = (gFrontEndId div IdRange + 1) * IdRange + 1 proc toGid(f: string): string = # we used to use ``f.addFileExt("gid")`` (aka ``$project.gid``), but this # will cause strange bugs if multiple projects are in the same folder, so # we simply use a project independent name: result = options.completeGeneratedFilePath("nimrod.gid") proc saveMaxIds*(project: string) = var f = open(project.toGid, fmWrite) f.writeln($gFrontEndId) f.writeln($gBackEndId) f.close() proc loadMaxIds*(project: string) = var f: TFile if open(f, project.toGid, fmRead): var line = newStringOfCap(20) if f.readLine(line): var frontEndId = parseInt(line) if f.readLine(line): var backEndId = parseInt(line) gFrontEndId = max(gFrontEndId, frontEndId) gBackEndId = max(gBackEndId, backEndId) f.close()