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





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.


name space maps

data NameSpaceMap k v Source

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

nameSpaceMap :: Ord k => NameSpaceMap k vSource

create a name space

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

flatten a namespace into a assoc list

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

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

scope modification

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

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 aSource

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)])Source

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)Source

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 aSource

Search for a definition

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

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

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