diff --git a/backends/GenerateC.hs b/backends/GenerateC.hs index 5250a11..c5dc24e 100644 --- a/backends/GenerateC.hs +++ b/backends/GenerateC.hs @@ -91,6 +91,7 @@ data GenOps = GenOps { genConversionSymbol :: GenOps -> A.Type -> A.Type -> A.ConversionMode -> CGen (), genDecl :: GenOps -> A.AbbrevMode -> A.Type -> A.Name -> CGen (), genDeclType :: GenOps -> A.AbbrevMode -> A.Type -> CGen (), + -- | Generates a declaration of a variable of the specified type and name genDeclaration :: GenOps -> A.Type -> A.Name -> CGen (), genDirectedVariable :: GenOps -> CGen () -> A.Direction -> CGen (), genDyadic :: GenOps -> Meta -> A.DyadicOp -> A.Expression -> A.Expression -> CGen (), @@ -1168,29 +1169,30 @@ cdeclareType ops t = call genType ops t -- | Generate a declaration of a new variable. cgenDeclaration :: GenOps -> A.Type -> A.Name -> CGen () -cgenDeclaration ops (A.Chan {}) n +-- Channels are of type "Channel", but channel-ends are of type "Channel*" +cgenDeclaration ops (A.Chan A.DirUnknown _ _) n = do tell ["Channel "] genName n - tell [";\n"] + tell [";"] cgenDeclaration ops (A.Array ds t) n = do call declareType ops t tell [" "] genName n call genFlatArraySize ops ds - tell [";\n"] + tell [";"] call declareArraySizes ops ds n cgenDeclaration ops t n = do call declareType ops t tell [" "] genName n - tell [";\n"] + tell [";"] -- | Generate the size of the C array that an occam array of the given -- dimensions maps to. cgenFlatArraySize :: GenOps -> [A.Dimension] -> CGen () cgenFlatArraySize ops ds = do tell ["["] - sequence $ intersperse (tell [" * "]) + sequence $ intersperse (tell ["*"]) [case d of A.Dimension n -> tell [show n] | d <- ds] tell ["]"] diff --git a/backends/GenerateCPPCSP.hs b/backends/GenerateCPPCSP.hs index 9d49b96..4f5da9b 100644 --- a/backends/GenerateCPPCSP.hs +++ b/backends/GenerateCPPCSP.hs @@ -649,11 +649,11 @@ cppgenProcCall ops n as -- | Changed from CIF's untyped channels to C++CSP's typed (templated) channels, and changed the declaration type of an array to be a vector. cppdeclareType :: GenOps -> A.Type -> CGen () cppdeclareType ops (A.Array ds t) - = do tell [" std::vector< "] + = do tell ["std::vector<"] call genType ops t tell ["/**/>/**/"] cppdeclareType ops (A.Counted countType valueType) - = do tell [" std::vector< "] + = do tell ["std::vector<"] case valueType of --Don't nest when it's a counted array of arrays: (A.Array _ t) -> call genType ops t @@ -685,9 +685,9 @@ cppgenDeclaration ops arrType@(A.Array ds t) n = do call declareType ops arrType tell [" "] genName n - tell ["_actual ("] + tell ["_actual("] call genFlatArraySize ops ds - tell ["); "] + tell [");"] call genType ops arrType tell [" "] genName n; @@ -695,12 +695,12 @@ cppgenDeclaration ops arrType@(A.Array ds t) n genName n tell ["_actual,tockDims("] genDims ds - tell ["));\n"] + tell ["));"] cppgenDeclaration ops t n = do call declareType ops t tell [" "] genName n - tell [";\n"] + tell [";"] -- | Changed because of channel arrays. cppdeclareInit :: GenOps -> Meta -> A.Type -> A.Variable -> Maybe (CGen ()) diff --git a/backends/GenerateCTest.hs b/backends/GenerateCTest.hs index 30592a9..fb59c58 100644 --- a/backends/GenerateCTest.hs +++ b/backends/GenerateCTest.hs @@ -243,6 +243,48 @@ testArraySubscript = TestList stateTrans = defineName (simpleName "foo") $ simpleDefDecl "foo" (A.Array [A.Dimension 7,A.Dimension 8,A.Dimension 8] A.Int) m = "\"" ++ show emptyMeta ++ "\"" +testDeclaration :: Test +testDeclaration = TestList + [ + --Simple: + testBothSame "genDeclaration 0" "int foo;" (tcall2 genDeclaration A.Int foo) + + --Channels and channel-ends: + ,testBoth "genDeclaration 1" "Channel foo;" "csp::One2OneChannel foo;" (tcall2 genDeclaration (A.Chan A.DirUnknown (A.ChanAttributes False False) A.Int) foo) + ,testBoth "genDeclaration 2" "Channel foo;" "csp::Any2OneChannel foo;" (tcall2 genDeclaration (A.Chan A.DirUnknown (A.ChanAttributes True False) A.Int) foo) + ,testBoth "genDeclaration 3" "Channel foo;" "csp::One2AnyChannel foo;" (tcall2 genDeclaration (A.Chan A.DirUnknown (A.ChanAttributes False True) A.Int) foo) + ,testBoth "genDeclaration 4" "Channel foo;" "csp::Any2AnyChannel foo;" (tcall2 genDeclaration (A.Chan A.DirUnknown (A.ChanAttributes True True) A.Int) foo) + ,testBoth "genDeclaration 5" "Channel* foo;" "csp::Chanin foo;" (tcall2 genDeclaration (A.Chan A.DirInput (A.ChanAttributes False False) A.Int) foo) + ,testBoth "genDeclaration 6" "Channel* foo;" "csp::Chanin foo;" (tcall2 genDeclaration (A.Chan A.DirInput (A.ChanAttributes False True) A.Int) foo) + ,testBoth "genDeclaration 7" "Channel* foo;" "csp::Chanout foo;" (tcall2 genDeclaration (A.Chan A.DirOutput (A.ChanAttributes False False) A.Int) foo) + ,testBoth "genDeclaration 8" "Channel* foo;" "csp::Chanout foo;" (tcall2 genDeclaration (A.Chan A.DirOutput (A.ChanAttributes True False) A.Int) foo) + + --Arrays (of simple): + ,testBoth "genDeclaration 100" "int foo[8];const int foo_sizes[]={8};" "std::vector foo_actual(8);tockArrayView foo(foo_actual,tockDims(8));" + (tcall2 genDeclaration (A.Array [A.Dimension 8] A.Int) foo) + ,testBoth "genDeclaration 101" "int foo[8*9];const int foo_sizes[]={8,9};" "std::vector foo_actual(8*9);tockArrayView foo(foo_actual,tockDims(8,9));" + (tcall2 genDeclaration (A.Array [A.Dimension 8,A.Dimension 9] A.Int) foo) + ,testBoth "genDeclaration 102" "int foo[8*9*10];const int foo_sizes[]={8,9,10};" "std::vector foo_actual(8*9*10);tockArrayView foo(foo_actual,tockDims(8,9,10));" + (tcall2 genDeclaration (A.Array [A.Dimension 8,A.Dimension 9,A.Dimension 10] A.Int) foo) + + --Arrays of channels and channel-ends: + ,testBoth "genDeclaration 200" "Channel* foo[8];const int foo_sizes[]={8};" + "std::vector*> foo_actual(8);tockArrayView*,1> foo(foo_actual,tockDims(8));" + (tcall2 genDeclaration (A.Array [A.Dimension 8] $ A.Chan A.DirUnknown (A.ChanAttributes False False) A.Int) foo) + + ,testBoth "genDeclaration 201" "Channel* foo[8*9];const int foo_sizes[]={8,9};" + "std::vector*> foo_actual(8*9);tockArrayView*,2> foo(foo_actual,tockDims(8,9));" + (tcall2 genDeclaration (A.Array [A.Dimension 8, A.Dimension 9] $ A.Chan A.DirUnknown (A.ChanAttributes False False) A.Int) foo) + + ,testBoth "genDeclaration 202" "Channel* foo[8];const int foo_sizes[]={8};" + "std::vector> foo_actual(8);tockArrayView,1> foo(foo_actual,tockDims(8));" + (tcall2 genDeclaration (A.Array [A.Dimension 8] $ A.Chan A.DirInput (A.ChanAttributes False False) A.Int) foo) + + ,testBoth "genDeclaration 203" "Channel* foo[8*9];const int foo_sizes[]={8,9};" + "std::vector> foo_actual(8*9);tockArrayView,2> foo(foo_actual,tockDims(8,9));" + (tcall2 genDeclaration (A.Array [A.Dimension 8, A.Dimension 9] $ A.Chan A.DirOutput (A.ChanAttributes False False) A.Int) foo) + ] + ---Returns the list of tests: tests :: Test tests = TestList @@ -250,6 +292,7 @@ tests = TestList testActuals ,testArraySizes ,testArraySubscript + ,testDeclaration ,testGenType ,testStop ]