summaryrefslogtreecommitdiff
path: root/src/Language/Tag.hs
blob: 9356073c7f95b93e73226a12f66fe5aac899b82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{-# LANGUAGE BangPatterns #-}
module Language.Tag (
  Tag(..), genTag,
) where

import Data.IORef
import System.IO.Unsafe

import AST


data Tag t = Tag (STy t) Int
  deriving (Show)

{-# NOINLINE tagCounter #-}
tagCounter :: IORef Int
tagCounter = unsafePerformIO $ newIORef 1

{-# NOINLINE genTag #-}
genTag :: handle -> STy t -> Tag t
genTag !_ ty =
  unsafePerformIO $ Tag ty <$> atomicModifyIORef' tagCounter (\i -> (succ i, i))