-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'fdedden/develop-multiple-triggers'. Close
#296. **Description** The current implementation of Copilot does not allow using the same trigger handler name multiple times, since the C99 backend produces multiple repeated declarations, which is invalid. So long as the types do not change, or so long as the target language allows for method/function overloading, then the Copilot spec should accept them. **Type** - Feature: extend valid specifications to allow multiple triggers with the same handler. **Additional context** None. **Requester** - Antanas Kalkauskas (Sensemtry). **Method to check presence of bug** Not applicable (not a bug). **Expected result** Copilot accepts specs where there are two triggers with the same name, so long as the types of the arguments (and their arity) is the same. The following dockerfile checks that a spec with the same trigger name used multiple times can be compiled correctly, and the resulting C code also compiles, after which it prints the message "Success": ``` --- Dockerfile-verify-296 FROM ubuntu:trusty RUN apt-get update RUN apt-get install --yes software-properties-common RUN add-apt-repository ppa:hvr/ghc RUN apt-get update RUN apt-get install --yes ghc-8.6.5 cabal-install-2.4 RUN apt-get install --yes libz-dev ENV PATH=/opt/ghc/8.6.5/bin:/opt/cabal/2.4/bin:$PWD/.cabal-sandbox/bin:$PATH RUN cabal update RUN cabal v1-sandbox init RUN cabal v1-install alex happy RUN apt-get install --yes git ADD MultipleTriggers.hs /tmp/MultipleTriggers.hs SHELL ["/bin/bash", "-c"] CMD git clone $REPO && cd $NAME && git checkout $COMMIT && cd .. \ && cabal v1-install $NAME/copilot**/ \ && cabal v1-exec -- runhaskell /tmp/MultipleTriggers.hs \ && gcc -c triggers.c \ && echo "Success" --- MultipleTriggers.hs import Language.Copilot import Copilot.Compile.C99 import Prelude hiding ((>), (<), div) -- External temperature as a byte, range of -50C to 100C temp :: Stream Word8 temp = extern "temperature" Nothing spec = do -- Triggers that fire when the temp is too low or too high, pass the current -- temp as an argument. trigger "adjust" (temp < 18) [arg temp] trigger "adjust" (temp > 21) [arg temp] -- Compile the spec main = reify spec >>= compile "triggers" ``` Command (substitute variables based on new path after merge): ``` $ docker run -e "REPO=https://github.com/Copilot-Language/copilot" -e "NAME=copilot" -e "COMMIT=<HASH>" -it copilot-verify-296 ``` **Solution implemented** Introduce a new type to represent triggers with a unique name in the generated C code. The unique trigger names are local to the C implementation and are not visible in the generated C header file. Modify implementation to use the unique triggers instead of the pre-existing triggers, assigning them unique names. Modify C header file generator to remove duplicate handler declarations. Modify top-level checks run prior to code generation to check that multiple triggers referring to the same handler pass arguments with the same types in both cases (since C does not support function polymorphism). **Further notes** None.
- Loading branch information
Showing
5 changed files
with
119 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
-- | C99 backend specific versions of selected `Copilot.Core` datatypes. | ||
module Copilot.Compile.C99.Representation | ||
( UniqueTrigger (..) | ||
, UniqueTriggerId | ||
, mkUniqueTriggers | ||
) | ||
where | ||
|
||
import Copilot.Core ( Trigger (..) ) | ||
|
||
-- | Internal unique name for a trigger. | ||
type UniqueTriggerId = String | ||
|
||
-- | A `Copilot.Core.Trigger` with an unique name. | ||
data UniqueTrigger = UniqueTrigger UniqueTriggerId Trigger | ||
|
||
-- | Given a list of triggers, make their names unique. | ||
mkUniqueTriggers :: [Trigger] -> [UniqueTrigger] | ||
mkUniqueTriggers ts = zipWith mkUnique ts [0..] | ||
where | ||
mkUnique t@(Trigger name _ _) n = UniqueTrigger (name ++ "_" ++ show n) t |