on master help sign in
Merge branch 'master' of https://github.com/elm-lang/elm-compiler into avoidNameConflicts
commit499e6d9870d2bc08038b2cde1eede70141839fbc authorJoeyEremondi <joey@eremondi.com> dateThu 27 Aug 2015, 17:36 (11 years ago) parent0d89d62c643514
9 files changed+97−29
src/AST/Variable.hs+27−7 src/Canonicalize/Setup.hs+11−9 src/Canonicalize/Sort.hs+2 src/Canonicalize/Variable.hs+38−12 src/Generate/JavaScript.hs+8 src/Generate/JavaScript/Variable.hs+1 src/Optimize/Substitute.hs+4−1 src/Optimize/TailCalls.hs+2 src/Type/Unify.hs+4
src/AST/Variable.hs+27−7
@@ -20,6 +20,7 @@ newtype Raw = Raw String
2020data Home
2121 = BuiltIn
2222 | Module [String]
23+ | TopLevel [String]
2324 | Local
2425 deriving (Eq, Ord, Show)
2526
@@ -32,11 +33,18 @@ data Canonical = Canonical
3233
3334
3435local :: String -> Canonical
35−local x = Canonical Local x
36+local x =
37+ Canonical Local x
38+
39+
40+topLevel :: [String] -> String -> Canonical
41+topLevel names x =
42+ Canonical (TopLevel names) x
3643
3744
3845builtin :: String -> Canonical
39−builtin x = Canonical BuiltIn x
46+builtin x =
47+ Canonical BuiltIn x
4048
4149
4250fromModule :: [String] -> String -> Canonical
@@ -122,9 +130,17 @@ instance ToString Raw where
122130instance ToString Canonical where
123131 toString (Canonical home name) =
124132 case home of
125− BuiltIn -> name
126− Module path -> List.intercalate "." (path ++ [name])
127− Local -> name
133+ BuiltIn ->
134+ name
135+
136+ Module path ->
137+ List.intercalate "." (path ++ [name])
138+
139+ TopLevel _ ->
140+ name
141+
142+ Local ->
143+ name
128144
129145
130146-- LISTINGS
@@ -249,15 +265,19 @@ instance Binary Canonical where
249265 Module path ->
250266 putWord8 1 >> put path >> put name
251267
268+ TopLevel path ->
269+ putWord8 2 >> put path >> put name
270+
252271 Local ->
253− putWord8 2 >> put name
272+ putWord8 3 >> put name
254273
255274 get =
256275 do tag <- getWord8
257276 case tag of
258277 0 -> Canonical BuiltIn <$> get
259278 1 -> Canonical . Module <$> get <*> get
260− 2 -> Canonical Local <$> get
279+ 2 -> Canonical . TopLevel <$> get <*> get
280+ 3 -> Canonical Local <$> get
261281 _ -> error "Unexpected tag when deserializing canonical variable"
262282
263283
src/Canonicalize/Setup.hs+11−9
@@ -282,16 +282,18 @@ declarationsToPatches moduleName decls =
282282 (Maybe.catMaybes maybeNodes, concat patchLists)
283283
284284
285−-- When canonicalizing, all _values should be Local, but all _adts and _patterns
286−-- should be fully namespaced. With _adts, they may appear in types that can
287−-- escape the module.
285+-- When canonicalizing, it is important that:
286+-- _adts are fully namespaced (Var.Module ...)
287+-- _patterns are fully namespaced (Var.Module ...)
288+-- _values that are defined as top-level declarations are (Var.TopLevel ...)
289+-- all other _values are local (Var.Local)
288290declToPatches
289291 :: Module.Name
290292 -> D.ValidDecl
291293 -> (Maybe Node, [Env.Patch])
292294declToPatches moduleName (A.A (region,_) decl) =
293− let local mkPatch x =
294− mkPatch x (Var.local x)
295+ let topLevel mkPatch x =
296+ mkPatch x (Var.topLevel moduleName x )
295297
296298 namespaced mkPatch x =
297299 mkPatch x (Var.fromModule moduleName x)
@@ -302,7 +304,7 @@ declToPatches moduleName (A.A (region,_) decl) =
302304 case decl of
303305 D.Definition (Valid.Definition pattern _ _) ->
304306 ( Nothing
305− , map (local Env.Value) (P.boundVarList pattern)
307+ , map (topLevel Env.Value) (P.boundVarList pattern)
306308 )
307309
308310 D.Datatype name _ ctors ->
@@ -310,7 +312,7 @@ declToPatches moduleName (A.A (region,_) decl) =
310312 in
311313 ( Nothing
312314 , namespaced Env.Union name
313− : map (local Env.Value) ctorNames
315+ : map (topLevel Env.Value) ctorNames
314316 ++ map patternPatch ctors
315317 )
316318
@@ -318,14 +320,14 @@ declToPatches moduleName (A.A (region,_) decl) =
318320 ( Just (node region name tvars alias)
319321 , case alias of
320322 A.A _ (Type.RRecord _ _) ->
321− [local Env.Value name]
323+ [topLevel Env.Value name]
322324 _ ->
323325 []
324326 )
325327
326328 D.Port port ->
327329 ( Nothing
328− , [local Env.Value (D.validPortName port)]
330+ , [topLevel Env.Value (D.validPortName port)]
329331 )
330332
331333 D.Fixity _ _ _ ->
src/Canonicalize/Sort.hs+2
@@ -31,6 +31,7 @@ freeIfLocal :: V.Canonical -> State (Set.Set String) ()
3131freeIfLocal (V.Canonical home name) =
3232 case home of
3333 V.Local -> free name
34+ V.TopLevel _ -> free name
3435 V.BuiltIn -> return ()
3536 V.Module _ -> return ()
3637
@@ -164,6 +165,7 @@ ctors (A.A _ pattern) =
164165 P.Data (V.Canonical home name) ps ->
165166 case home of
166167 V.Local -> name : rest
168+ V.TopLevel _ -> name : rest
167169 V.BuiltIn -> rest
168170 V.Module _ -> rest
169171 where
src/Canonicalize/Variable.hs+38−12
@@ -30,10 +30,10 @@ variable region env var =
3030 Just [v] ->
3131 Result.var v
3232
33− Just vs ->
33+ Just vs ->
3434 preferLocals region env "variable" vs var
3535
36− Nothing ->
36+ Nothing ->
3737 notFound region "variable" (Map.keys (Env._values env)) var
3838
3939
@@ -110,30 +110,56 @@ preferLocals'
110110 -> String
111111 -> Result.ResultErr a
112112preferLocals' region env extract kind possibilities var =
113− case filter (isLocal . extract) possibilities of
113+ case filter (isLocal (Env._home env) . extract) possibilities of
114114 [] ->
115115 ambiguous possibilities
116116
117117 [v] ->
118118 Result.var' extract v
119119
120+ --Local vars shadow top-level vars
121+ [v1, v2]
122+ | isTopLevel (extract v1) ->
123+ Result.var' extract v2
124+
125+ | isTopLevel (extract v2) ->
126+ Result.var' extract v1
127+
120128 locals ->
121129 ambiguous locals
122130 where
123− isLocal :: Var.Canonical -> Bool
124− isLocal (Var.Canonical home _) =
125− case home of
126− Var.Local -> True
127− Var.BuiltIn -> False
128− Var.Module name ->
129− name == Env._home env
130−
131131 ambiguous possibleVars =
132132 Result.err (A.A region (Error.variable kind var Error.Ambiguous vars))
133133 where
134134 vars = map (Var.toString . extract) possibleVars
135135
136136
137+isLocal :: [String] -> Var.Canonical -> Bool
138+isLocal contextName (Var.Canonical home _) =
139+ case home of
140+ Var.Local ->
141+ True
142+
143+ Var.TopLevel _ ->
144+ True
145+
146+ Var.BuiltIn ->
147+ False
148+
149+ Var.Module name ->
150+ name == contextName
151+
152+
153+isTopLevel :: Var.Canonical -> Bool
154+isTopLevel (Var.Canonical home _) =
155+ case home of
156+ Var.TopLevel _ ->
157+ True
158+
159+ _ ->
160+ False
161+
162+
137163-- NOT FOUND HELPERS
138164
139165type VarName =
@@ -224,4 +250,4 @@ qualifiedProblem moduleName name allQualified =
224250 , Set.toList availableModules
225251 |> map Module.nameToString
226252 |> Error.nearbyNames id moduleNameString
227− )
253+ )
src/Generate/JavaScript.hs+8
@@ -351,6 +351,12 @@ isTailCall tcc (A.A _ func) arity =
351351 else
352352 Nothing
353353
354+ (Var (Var.Canonical (Var.TopLevel _) name), Just (tcName, tcArgs, tcLabel)) ->
355+ if name == tcName && length tcArgs == arity then
356+ Just (tcArgs, tcLabel)
357+ else
358+ Nothing
359+
354360 _ ->
355361 Nothing
356362
@@ -516,6 +522,8 @@ generateCase tcc expr branches =
516522 case expr of
517523 A.A _ (Var (Var.Canonical Var.Local x)) ->
518524 return (Case.matchSubst [(tempVar, Var.varName x)] initialMatch, [])
525+ A.A _ (Var (Var.Canonical (Var.TopLevel _) x)) ->
526+ return (Case.matchSubst [(tempVar, Var.varName x)] initialMatch, [])
519527 _ ->
520528 do expr' <- toJsExpr expr
521529 return (initialMatch, [VarDeclStmt () [varDecl tempVar expr']])
src/Generate/JavaScript/Variable.hs+1
@@ -23,6 +23,7 @@ canonical (Var.Canonical home name) =
2323 home' =
2424 case home of
2525 Var.Local -> []
26+ Var.TopLevel _ -> []
2627 Var.BuiltIn -> []
2728 Var.Module path -> [ moduleName path ]
2829
src/Optimize/Substitute.hs+4−1
@@ -58,6 +58,9 @@ subst old new expression =
5858 V.Local ->
5959 if x == old then new else expression
6060
61+ V.TopLevel _ ->
62+ if x == old then new else expression
63+
6164 Case e cases ->
6265 Case (f e) (map substCase cases)
6366 where
@@ -103,4 +106,4 @@ subst old new expression =
103106 Task name (f expr) tipe
104107
105108 Crash _ ->
106− expression
109+ expression
src/Optimize/TailCalls.hs+2
@@ -130,6 +130,8 @@ findTailCalls context annExpr@(A.A _ expression) =
130130 case (context, func) of
131131 ( Just (name, argNum), A.A _ (Var (Var.Canonical Var.Local name')) ) ->
132132 name == name' && length args == argNum
133+ ( Just (name, argNum), A.A _ (Var (Var.Canonical (Var.TopLevel _) name')) ) ->
134+ name == name' && length args == argNum
133135 _ ->
134136 False
135137
src/Type/Unify.hs+4
@@ -126,6 +126,10 @@ actuallyUnify variable1 variable2 = do
126126
127127 Var.Local | List.isPrefixOf "number" name ->
128128 flexAndUnify svar
129+
130+ Var.TopLevel _ | List.isPrefixOf "number" name ->
131+ flexAndUnify svar
132+
129133 _ ->
130134 typeError variable1 variable2 Nothing
131135