Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions modules/standard/String.enc
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ BODY
// This function is called in the very beginning of the program to
// build an array containing the arguments of the program.
array_t *_init_argv(pony_ctx_t** ctx, size_t argc, char **argv) {
array_t *arr = array_mk(ctx, argc, &_enc__read_String_String_type);
array_t *arr = array_mk(ctx, argc, &_enc__class_String_String_type);
for(int i = 0; i < argc; i++) {
_enc__read_String_String_t* s =
encore_alloc(*ctx, sizeof(_enc__read_String_String_t));
s->_enc__self_type = &_enc__read_String_String_type;
_enc__class_String_String_t* s =
encore_alloc(*ctx, sizeof(_enc__class_String_String_t));
s->_enc__self_type = &_enc__class_String_String_type;
_enc__method_String_String_init(ctx, s, NULL, argv[i]);
array_set(arr, i, (encore_arg_t){.p = s});
}
Expand Down
9 changes: 5 additions & 4 deletions src/back/CodeGen/CCodeNames.hs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ methodImplNameStr clazz mname =

forwardingMethodImplNameStr :: Ty.Type -> ID.Name -> String
forwardingMethodImplNameStr clazz mname =
encoreName "forwarding_method" $ qualifyRefType clazz ++ "_" ++ show mname
encoreName "method" $ qualifyRefType clazz ++ "_" ++ show mname ++ "_async"

callMethodFutureNameStr :: Ty.Type -> ID.Name -> String
callMethodFutureNameStr clazz mname =
Expand Down Expand Up @@ -404,9 +404,7 @@ oneWayMsgId cls mname =
typeNamePrefix :: Ty.Type -> String
typeNamePrefix ref
| Ty.isTraitType ref = encoreName "trait" qname
| Ty.isRefAtomType ref = if Ty.isModeless ref
then encoreName "passive" qname
else encoreName (showModeOf ref) qname
| Ty.isRefAtomType ref = encoreName "class" qname
| otherwise = error $ "type_name_prefix Type '" ++ show ref ++
"' isnt reference type!"
where
Expand Down Expand Up @@ -451,6 +449,9 @@ futureGetActor = Nam "future_get_actor"
futureChainActor :: CCode Name
futureChainActor = Nam "future_chain_actor"

futureChainActorForward :: CCode Name
futureChainActorForward = Nam "future_chain_forward"

actorSuspend :: CCode Name
actorSuspend = Nam "actor_suspend"

Expand Down
6 changes: 3 additions & 3 deletions src/back/CodeGen/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module CodeGen.Context (
setFunCtx,
setClsCtx,
getExecCtx,
withForwarding
isAsyncForward
) where

import Identifiers
Expand Down Expand Up @@ -74,8 +74,8 @@ newWithForwarding subs table = Context {
,withForward = True
}

withForwarding :: Context -> Bool
withForwarding Context{withForward} = withForward
isAsyncForward :: Context -> Bool
isAsyncForward Context{withForward} = withForward

genNamedSym :: String -> State Context String
genNamedSym name = do
Expand Down
109 changes: 62 additions & 47 deletions src/back/CodeGen/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -663,26 +663,17 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
result <- Ctx.genNamedSym (fromJust sym)
return (Var result, Assign (Decl (translate retTy, Var result)))

translate w@(A.DoWhile {A.cond, A.body}) =
do (ncond,tcond) <- translate cond
(nbody,tbody) <- translate body
tmp <- Ctx.genNamedSym "while";
let exportBody = Seq $ tbody : [Assign (Var tmp) nbody]
return (Var tmp,
Seq [Statement $ Decl (translate (A.getType w), Var tmp),
DoWhile (StatAsExpr ncond tcond) (Statement exportBody)])

translate w@(A.While {A.cond, A.body}) =
do (ncond,tcond) <- translate cond
(nbody,tbody) <- translate body
tmp <- Ctx.genNamedSym "while";
let exportBody = Seq $ tbody : [Assign (Var tmp) nbody]
return (Var tmp,
Seq [Statement $ Decl (translate (A.getType w), Var tmp),
While (StatAsExpr ncond tcond) (Statement exportBody)])
translate w@(A.DoWhile {A.cond, A.body}) = do
(ncond,tcond) <- translate cond
(_,tbody) <- translate body
return (unit, DoWhile (StatAsExpr ncond tcond) (Statement tbody))

translate w@(A.While {A.cond, A.body}) = do
(ncond,tcond) <- translate cond
(_,tbody) <- translate body
return (unit, While (StatAsExpr ncond tcond) (Statement tbody))

translate for@(A.For {A.name, A.step, A.src, A.body}) = do
tmpVar <- Var <$> Ctx.genNamedSym "for";
indexVar <- Var <$> Ctx.genNamedSym "index"
eltVar <- Var <$> Ctx.genNamedSym (show name)
startVar <- Var <$> Ctx.genNamedSym "start"
Expand Down Expand Up @@ -736,12 +727,10 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
then AsExpr indexVar
else AsExpr $ fromEncoreArgT eltType (Call arrayGet [srcN, indexVar]))
inc = Assign indexVar (BinOp (translate ID.PLUS) indexVar stepVar)
theBody = Seq [eltDecl, Statement bodyT, Assign tmpVar bodyN, inc]
theBody = Seq [eltDecl, Statement bodyT, inc]
theLoop = While cond theBody
tmpDecl = Statement $ Decl (translate (A.getType for), tmpVar)

return (tmpVar, Seq [tmpDecl
,srcT
return (unit, Seq [srcT
,srcStartT
,srcStopT
,srcStepT
Expand Down Expand Up @@ -774,7 +763,7 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
(narg, targ) <- translate arg
let argty = A.getType arg
mType = translate (A.getType m)
tIfChain <- ifChain clauses narg argty retTmp mType
tIfChain <- ifChain clauses narg argty retTmp mType (A.getPos m)
let lRetDecl = Decl (mType, Var retTmp)
eZeroInit = Cast mType (Int 0)
tRetDecl = Assign lRetDecl eZeroInit
Expand Down Expand Up @@ -941,18 +930,18 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
tAssign = Assign (Var handlerReturnVar) eCast
return tAssign

ifChain [] _ _ _ _ = do
ifChain [] _ _ _ _ pos = do
let errorCode = Int 1
exitCall = Statement $ Call (Nam "exit") [errorCode]
errorMsg = String "*** Runtime error: No matching clause was found ***\n"
errorMsg = String $ "*** Runtime error: No matching clause was found at " ++ show pos ++ " ***\n"
errorPrint = Statement $ Call (Nam "fprintf") [AsExpr C.stderr, errorMsg]
return $ Seq [errorPrint, exitCall]

ifChain (clause:rest) narg argty retTmp retTy = do
ifChain (clause:rest) narg argty retTmp retTy pos = do
let freeVars = Util.foldrExp (\e a -> getExprVars e ++ a) [] (A.mcpattern clause)
assocs <- mapM createAssoc freeVars
thenExpr <- translateHandler clause retTmp assocs retTy
elseExpr <- ifChain rest narg argty retTmp retTy
elseExpr <- ifChain rest narg argty retTmp retTy pos
eCond <- translateIfCond clause narg argty assocs
let tIf = Statement $ If eCond thenExpr elseExpr
tDecls = Seq $ map (fwdDecl assocs) freeVars
Expand Down Expand Up @@ -1015,18 +1004,10 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
,A.name
,A.typeArguments
,A.args}} = do
withForwarding <- gets Ctx.withForwarding
isAsyncForward <- gets Ctx.isAsyncForward
eCtx <- gets Ctx.getExecCtx
let dtraceExit =
case eCtx of
Ctx.FunctionContext fun ->
dtraceFunctionExit (A.functionName fun)
Ctx.MethodContext mdecl ->
dtraceMethodExit thisVar (A.methodName mdecl)
Ctx.ClosureContext clos ->
dtraceClosureExit
_ -> error "Expr.hs: No context to forward from"
if withForwarding
let dtraceExit = getDtraceExit eCtx
if isAsyncForward
then do
(ntarget, ttarget) <- translate target
let targetType = A.getType target
Expand All @@ -1049,8 +1030,7 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
dtraceExit,
Return Skip])

else if Ty.isFutureType $ A.getType expr
then do
else do
(sendn, sendt) <- translate A.MessageSend{A.emeta
,A.target
,A.name
Expand All @@ -1059,14 +1039,39 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
let resultType = translate (Ty.getResultType $ A.getType expr)
theGet = fromEncoreArgT resultType (Call futureGetActor [encoreCtxVar, sendn])
return (unit, Seq [sendt, dtraceExit, Return theGet])
else
error $ "Expr.hs: Cannot translate forward of ''" ++ show expr ++ "'"

translate A.Forward{A.forwardExpr = A.FutureChain{}} =
error "Expr.hs: Forwarding of chaining not implemented"
translate A.Forward{A.forwardExpr} =
error $ "Expr.hs: Target of forward is not method call or future chain: '" ++
show forwardExpr ++ "'"
translate A.Forward{A.emeta, A.forwardExpr = fchain@A.FutureChain{A.future, A.chain}} = do
(nfuture,tfuture) <- translate future
(nchain, tchain) <- translate chain
eCtx <- gets $ Ctx.getExecCtx
isAsyncForward <- gets Ctx.isAsyncForward
let ty = getRuntimeType chain
dtraceExit = getDtraceExit eCtx
if isAsyncForward
then do
return (unit, Seq $
[tfuture,
tchain,
(Statement $
Call futureChainActorForward
[AsExpr encoreCtxVar, AsExpr nfuture, ty, AsExpr nchain, AsExpr futVar]
)] ++
[dtraceExit,
Return Skip])
else do
tmp <- Ctx.genSym
result <- Ctx.genSym
let nfchain = Var result
resultType = translate (Ty.getResultType $ A.getType fchain)
theGet = fromEncoreArgT resultType (Call futureGetActor [encoreCtxVar, nfchain])
return $ (Var tmp, Seq $
[tfuture,
tchain,
(Assign (Decl (C.future, Var result))
(Call futureChainActor
[AsExpr encoreCtxVar, AsExpr nfuture, ty, AsExpr nchain]
)),
Assign (Decl (resultType, Var tmp)) theGet])

translate yield@(A.Yield{A.val}) =
do (nval, tval) <- translate val
Expand Down Expand Up @@ -1187,6 +1192,16 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where

translate other = error $ "Expr.hs: can't translate: '" ++ show other ++ "'"

getDtraceExit eCtx =
case eCtx of
Ctx.FunctionContext fun ->
dtraceFunctionExit (A.functionName fun)
Ctx.MethodContext mdecl ->
dtraceMethodExit thisVar (A.methodName mdecl)
Ctx.ClosureContext clos ->
dtraceClosureExit
_ -> error "Expr.hs: No context to forward from"

closureCall :: CCode Lval -> A.Expr ->
State Ctx.Context (CCode Lval, CCode Stat)
closureCall clos fcall@A.FunctionCall{A.qname, A.args} = do
Expand Down
15 changes: 10 additions & 5 deletions src/front/ModuleExpander.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Types(setRefSourceFile, setRefNamespace)
import SystemUtils
import Control.Monad
import Control.Arrow((&&&))
import System.Directory(doesFileExist)
import System.Directory(doesFileExist, makeAbsolute)
import Data.Map.Strict(Map)
import qualified Data.Map.Strict as Map
import Data.List
Expand Down Expand Up @@ -47,7 +47,7 @@ shortenPrelude preludePaths source =

stdLib source = [lib "String", lib "Std"]
where
lib s = Import{imeta = meta $ initialPos source
lib s = Import{imeta = meta $ newPos (initialPos source)
,itarget = explicitNamespace [Name s]
,isource = Nothing
,iqualified = False
Expand Down Expand Up @@ -121,9 +121,14 @@ buildModulePath (NSExplicit ns) =
findSource :: [FilePath] -> FilePath -> ImportDecl -> IO FilePath
findSource importDirs sourceDir Import{itarget} = do
let modulePath = buildModulePath itarget
sources = nub $
sourceDir </> modulePath :
map (</> modulePath) importDirs
imports = map (</> modulePath) importDirs
sourceModulePath = sourceDir </> modulePath
expandedSourceModulePath <- makeAbsolute $ sourceModulePath
let sources = if expandedSourceModulePath `elem` imports then
-- if directory of target is in imports, remove it to avoid ambiguous import error
nub $ imports
else
nub $ sourceModulePath : imports
candidates <- filterM doesFileExist sources
case candidates of
[] -> abort $ "Module " ++ show itarget ++
Expand Down
28 changes: 25 additions & 3 deletions src/ir/AST/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ class Show a => HasMeta a where

setMeta :: a -> Meta a -> a

getPos :: a -> SourcePos
getPos :: a -> Position
getPos = Meta.getPos . getMeta

setEndPos :: SourcePos -> a -> a
setEndPos end x =
let oldMeta = getMeta x
newMeta = Meta.setEndPos end oldMeta
in setMeta x newMeta

getType :: a -> Type
getType = Meta.getType . getMeta

Expand Down Expand Up @@ -85,6 +91,14 @@ data EmbedTL = EmbedTL {
etlbody :: String
} deriving (Show)

instance HasMeta EmbedTL where
getMeta = etlmeta

setMeta etl etlmeta = etl{etlmeta}

setType ty i =
error "AST.hs: Cannot set the type of a EmbedTL"

data ModuleDecl = Module {
modmeta :: Meta ModuleDecl,
modname :: Name,
Expand Down Expand Up @@ -410,7 +424,8 @@ data FieldDecl = Field {
fmeta :: Meta FieldDecl,
fmut :: Mutability,
fname :: Name,
ftype :: Type
ftype :: Type,
fexpr :: Maybe Expr
}

instance Show FieldDecl where
Expand All @@ -429,11 +444,15 @@ instance HasMeta FieldDecl where
isValField :: FieldDecl -> Bool
isValField = (== Val) . fmut

isVarField :: FieldDecl -> Bool
isVarField = (== Var) . fmut

data ParamDecl = Param {
pmeta :: Meta ParamDecl,
pmut :: Mutability,
pname :: Name,
ptype :: Type
ptype :: Type,
pdefault :: Maybe Expr
} deriving (Show, Eq)

instance HasMeta ParamDecl where
Expand Down Expand Up @@ -466,6 +485,9 @@ isConstructor m = methodName m == constructorName

isImplicitMethod = mimplicit

hasConstructor :: ClassDecl -> Bool
hasConstructor Class{cmethods} = filter isConstructor cmethods /= []

emptyConstructor :: ClassDecl -> MethodDecl
emptyConstructor cdecl =
let pos = AST.AST.getPos cdecl
Expand Down
Loading