Language.C.Analysis.NameSpaceMap

language-c-0.6.1: Analysis and generation of C code

Copyright | (c) [1995..1999] Manuel M. T. Chakravarty (c) 2008 Benedikt Huber
License | BSD-style
Maintainer | benedikt.huber@gmail.com
Stability | alpha
Portability | portable
Safe Haskell | None
Language | Haskell98

Language.C.Analysis.NameSpaceMap

Contents

- name space maps
- scope modification

Description

This module manages name spaces.

- A name space map associates identifiers with their definition.
- Each name space map is organized in a hierarchical way using the notion of scopes. A name space map, at any moment, always has a global scope and may have several local scopes. Definitions in inner scopes hide definitions of the same identifier in outer scopes.

Synopsis

- data NameSpaceMap k v
- nameSpaceMap :: Ord k => NameSpaceMap k v
- nsMapToList :: Ord k => NameSpaceMap k a -> [(k, a)]
- globalNames :: Ord k => NameSpaceMap k v -> Map k v
- localNames :: Ord k => NameSpaceMap k v -> [[(k, v)]]
- hasLocalNames :: NameSpaceMap k v -> Bool
- defGlobal :: Ord k => NameSpaceMap k a -> k -> a -> (NameSpaceMap k a, Maybe a)
- enterNewScope :: Ord k => NameSpaceMap k a -> NameSpaceMap k a
- leaveScope :: Ord k => NameSpaceMap k a -> (NameSpaceMap k a, [(k, a)])
- defLocal :: Ord k => NameSpaceMap k a -> k -> a -> (NameSpaceMap k a, Maybe a)
- lookupName :: Ord k => NameSpaceMap k a -> k -> Maybe a
- lookupGlobal :: Ord k => NameSpaceMap k a -> k -> Maybe a
- lookupInnermostScope :: Ord k => NameSpaceMap k a -> k -> Maybe a
- mergeNameSpace :: Ord k => NameSpaceMap k a -> NameSpaceMap k a -> NameSpaceMap k a

# name space maps

data NameSpaceMap k v #

`NameSpaceMap a` is a Map from identifiers to `a`, which manages global and local name spaces.

nameSpaceMap :: Ord k => NameSpaceMap k v #

create a name space

nsMapToList :: Ord k => NameSpaceMap k a -> [(k, a)] #

flatten a namespace into a assoc list

    nameSpaceToList ns = (localDefInnermost ns ++ .. ++ localDefsOutermost ns) ++ globalDefs ns

globalNames :: Ord k => NameSpaceMap k v -> Map k v #

localNames :: Ord k => NameSpaceMap k v -> [[(k, v)]] #

hasLocalNames :: NameSpaceMap k v -> Bool #

# scope modification

defGlobal :: Ord k => NameSpaceMap k a -> k -> a -> (NameSpaceMap k a, Maybe a) #

Add global definition

`(ns',oldDef) = defGlobal ns ident def` adds a global definition `ident := def` to the namespace. It returns the modified namespace `ns'`. If the identifier is already declared in the global namespace, the definition is overwritten and the old definition `oldDef` is returned.

enterNewScope :: Ord k => NameSpaceMap k a -> NameSpaceMap k a #

Enter new local scope

`ns' = enterNewScope ns` creates and enters a new local scope.

leaveScope :: Ord k => NameSpaceMap k a -> (NameSpaceMap k a, [(k, a)]) #

Leave innermost local scope

`(ns',defs) = leaveScope ns` pops leaves the innermost local scope. and returns its definitions

defLocal :: Ord k => NameSpaceMap k a -> k -> a -> (NameSpaceMap k a, Maybe a) #

Add local definition

`(ns',oldDef) = defLocal ns ident def` adds the local definition `ident := def` to the innermost local scope, if there is a local scope, and to the global scope otherwise. It returns the modified name space `ns'` and the old binding of the identifier `oldDef`, which is overwritten.

lookupName :: Ord k => NameSpaceMap k a -> k -> Maybe a #

Search for a definition

`def = find ns ident` returns the definition in some scope (inner to outer), if there is one.

lookupGlobal :: Ord k => NameSpaceMap k a -> k -> Maybe a #

lookupInnermostScope :: Ord k => NameSpaceMap k a -> k -> Maybe a #

mergeNameSpace :: Ord k => NameSpaceMap k a -> NameSpaceMap k a -> NameSpaceMap k a #

Merge two namespaces. If they disagree on the types of any variables, all bets are off.