Fixed the findMain pass to use a proper nonce

This commit is contained in:
Neil Brown 2007-08-23 10:17:30 +00:00
parent 0913ab655c
commit 23650da2ff

View File

@ -122,18 +122,19 @@ findMain :: Data t => t -> PassM t
--Because findMain runs after uniquifyAndResolveVars, the types of all the process will have been recorded
--Therefore this pass doesn't actually need to walk the tree, it just has to look for a process named "main"
--in the CompState, and pull it out into csMainLocals
findMain x = do modify findMain'
everywhereM (mkM $ return . (replaceNameName "main" "____main")) x
findMain x = do newMainName <- makeNonce "main_"
modify (findMain' newMainName)
everywhereM (mkM $ return . (replaceNameName "main" newMainName)) x
where
--We have to mangle the main name because otherwise it will cause problems on some backends (including C and C++)
findMain' :: CompState -> CompState
findMain' st = case (Map.lookup "main" (csNames st)) of
Just n -> st {csNames = changeMainName (csNames st) , csMainLocals = [("____main",A.Name {A.nameName = "____main", A.nameMeta = A.ndMeta n, A.nameType = A.ndNameType n})]}
findMain' :: String -> CompState -> CompState
findMain' newn st = case (Map.lookup "main" (csNames st)) of
Just n -> st {csNames = changeMainName newn (csNames st) , csMainLocals = [(newn,A.Name {A.nameName = newn, A.nameMeta = A.ndMeta n, A.nameType = A.ndNameType n})]}
Nothing -> st
changeMainName :: Map.Map String A.NameDef -> Map.Map String A.NameDef
changeMainName m = case (Map.lookup "main" m) of
changeMainName :: String -> Map.Map String A.NameDef -> Map.Map String A.NameDef
changeMainName n m = case (Map.lookup "main" m) of
Nothing -> m
Just nd -> ((Map.insert "____main" (nd {A.ndName = "____main"})) . (Map.delete "main")) m
Just nd -> ((Map.insert n (nd {A.ndName = n})) . (Map.delete "main")) m
transformEach :: Data t => t -> PassM t