Skip to content

Commit

Permalink
Add a CPP flag for each available DAML-LF feature. (digital-asset#2896)
Browse files Browse the repository at this point in the history
* Expose Daml LF features as a CPP flags.

* Add missing text dependency
  • Loading branch information
associahedron authored Sep 16, 2019
1 parent 249e8a6 commit dc32abb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Version.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ supportedInputVersions = version1_5 : supportedOutputVersions
data Feature = Feature
{ featureName :: !T.Text
, featureMinVersion :: !Version
, featureCppFlag :: !T.Text
-- ^ CPP flag to test for availability of the feature.
}

-- NOTE(MH): We comment this out to leave an example how to deal with features.
Expand All @@ -59,8 +61,17 @@ featureNumeric :: Feature
featureNumeric = Feature
{ featureName = "Numeric type"
, featureMinVersion = version1_7
, featureCppFlag = "DAML_NUMERIC"
}

allFeatures :: [Feature]
allFeatures =
[ featureNumeric
]

allFeaturesForVersion :: Version -> [Feature]
allFeaturesForVersion version = filter (supports version) allFeatures

supports :: Version -> Feature -> Bool
supports version feature = version >= featureMinVersion feature

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ _whenRuntimeSupports version feature t e
| otherwise = runtimeUnsupported feature t

runtimeUnsupported :: Feature -> Type -> Expr
runtimeUnsupported (Feature name version) t =
runtimeUnsupported (Feature name version _) t =
ETmApp
(ETyApp (EBuiltin BEError) t)
(EBuiltin (BEText (name <> " only supported when compiling to DAML-LF " <> T.pack (renderVersion version) <> " or later")))
1 change: 1 addition & 0 deletions compiler/damlc/daml-opts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ da_haskell_library(
"ghc-lib-parser",
"ghcide",
"mtl",
"text",
],
src_strip_prefix = "daml-opts",
visibility = ["//visibility:public"],
Expand Down
6 changes: 5 additions & 1 deletion compiler/damlc/daml-opts/daml-opts/DA/Daml/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Data.Bifunctor
import Data.IORef
import Data.List
import DynFlags (parseDynamicFilePragma)
import qualified Data.Text as T
import qualified Platform as P
import qualified EnumSet
import GHC hiding (convertLit)
Expand All @@ -29,6 +30,7 @@ import HscMain
import Panic (throwGhcExceptionIO)
import System.Directory
import System.FilePath
import qualified DA.Daml.LF.Ast.Version as LF

import DA.Daml.Options.Types
import DA.Daml.Preprocessor
Expand Down Expand Up @@ -244,7 +246,7 @@ adjustDynFlags options@Options{..} tmpDir dflags
Nothing -> id
Just cppPath -> alterSettings $ \s -> s
{ sPgm_P = (cppPath, [])
, sOpt_P = ["-P"]
, sOpt_P = "-P" : ["-D" <> T.unpack flag | flag <- cppFlags]
-- We add "-P" here to suppress #line pragmas from the
-- preprocessor (hpp, specifically) because the daml
-- parser can't handle them. This is a non-issue right now
Expand All @@ -257,6 +259,8 @@ adjustDynFlags options@Options{..} tmpDir dflags
-- sometimes this is required by CPP?
}

cppFlags = map LF.featureCppFlag (LF.allFeaturesForVersion optDamlLfVersion)

-- We need to add platform info in order to run CPP. To prevent
-- .hi file incompatibilities, we set the platform the same way
-- for everyone even if they don't use CPP.
Expand Down

0 comments on commit dc32abb

Please sign in to comment.