From 80353244f48a7412b79755e1078b443e78fa84d5 Mon Sep 17 00:00:00 2001 From: Sofia Faro Date: Thu, 23 Sep 2021 11:31:31 +0100 Subject: [PATCH] interface methods: Add protobuf Adds protobuf definitions for interface methods and calling them. Encoders/decoders just ignore the extra stuff or error out. Part of #10810 (maybe) changelog_begin changelog_end --- .../src/DA/Daml/LF/Proto3/DecodeV1.hs | 10 ++++- .../src/DA/Daml/LF/Proto3/EncodeV1.hs | 8 +++- .../com/daml/daml_lf_dev/daml_lf_1.proto | 39 ++++++++++++++++++- .../daml/lf/archive/DecodeV1.scala | 11 +++++- .../daml/lf/archive/testing/EncodeV1.scala | 11 +++++- 5 files changed, 73 insertions(+), 6 deletions(-) diff --git a/compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/DecodeV1.hs b/compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/DecodeV1.hs index 9ef253ed7e1e..ea60b4ab76a4 100644 --- a/compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/DecodeV1.hs +++ b/compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/DecodeV1.hs @@ -318,7 +318,13 @@ decodeDefTemplate LF1.DefTemplate{..} = do <*> mayDecode "defTemplateAgreement" defTemplateAgreement decodeExpr <*> decodeNM DuplicateChoice decodeChoice defTemplateChoices <*> mapM (decodeDefTemplateKey tplParam) defTemplateKey - <*> traverse decodeTypeConName (V.toList defTemplateImplements) + <*> traverse decodeDefTemplateImplements (V.toList defTemplateImplements) + +-- TODO https://github.com/digital-asset/daml/issues/10810 +-- decode rest and store in AST +decodeDefTemplateImplements :: LF1.DefTemplate_Implements -> Decode (Qualified TypeConName) +decodeDefTemplateImplements LF1.DefTemplate_Implements{..} = + mayDecode "defTemplate_ImplementsInterface" defTemplate_ImplementsInterface decodeTypeConName decodeDefTemplateKey :: ExprVarName -> LF1.DefTemplate_DefKey -> Decode TemplateKey decodeDefTemplateKey templateParam LF1.DefTemplate_DefKey{..} = do @@ -644,6 +650,8 @@ decodeExprSum exprSum = mayDecode "exprSum" exprSum $ \case <$> mayDecode "expr_FromInterfaceInterfaceType" expr_FromInterfaceInterfaceType decodeTypeConName <*> mayDecode "expr_FromInterfaceTemplateType" expr_FromInterfaceTemplateType decodeTypeConName <*> mayDecode "expr_FromInterfaceInterfaceExpr" expr_FromInterfaceInterfaceExpr decodeExpr + LF1.ExprSumCallInterface LF1.Expr_CallInterface {} -> + error "ECallInterface not implemented" -- TODO https://github.com/digital-asset/daml/issues/10810 LF1.ExprSumExperimental (LF1.Expr_Experimental name mbType) -> do ty <- mayDecode "expr_Experimental" mbType decodeType pure $ EExperimental (decodeString name) ty diff --git a/compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/EncodeV1.hs b/compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/EncodeV1.hs index 2e5fca8ae84f..5fe32bd4ac58 100644 --- a/compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/EncodeV1.hs +++ b/compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/EncodeV1.hs @@ -916,8 +916,11 @@ encodeTemplate Template{..} = do defTemplateImplements <- encodeList encodeTemplateImpl tplImplements pure P.DefTemplate{..} -encodeTemplateImpl :: Qualified TypeConName -> Encode P.TypeConName -encodeTemplateImpl = encodeQualTypeConName' +encodeTemplateImpl :: Qualified TypeConName -> Encode P.DefTemplate_Implements +encodeTemplateImpl iface = do + defTemplate_ImplementsInterface <- encodeQualTypeConName iface + let defTemplate_ImplementsMethods = V.empty -- TODO https://github.com/digital-asset/daml/issues/10810 + pure P.DefTemplate_Implements {..} encodeTemplateKey :: TemplateKey -> Encode P.DefTemplate_DefKey encodeTemplateKey TemplateKey{..} = do @@ -981,6 +984,7 @@ encodeDefInterface DefInterface{..} = do defInterfaceLocation <- traverse encodeSourceLoc intLocation defInterfaceTyconInternedDname <- encodeDottedNameId unTypeConName intName defInterfaceChoices <- encodeNameMap encodeInterfaceChoice intChoices + let defInterfaceMethods = V.empty -- TODO https://github.com/digital-asset/daml/issues/10810 pure $ P.DefInterface{..} encodeInterfaceChoice :: InterfaceChoice -> Encode P.InterfaceChoice diff --git a/daml-lf/archive/src/main/protobuf/com/daml/daml_lf_dev/daml_lf_1.proto b/daml-lf/archive/src/main/protobuf/com/daml/daml_lf_dev/daml_lf_1.proto index 1d2bc9d07d25..a2d91ac2f6af 100644 --- a/daml-lf/archive/src/main/protobuf/com/daml/daml_lf_dev/daml_lf_1.proto +++ b/daml-lf/archive/src/main/protobuf/com/daml/daml_lf_dev/daml_lf_1.proto @@ -927,6 +927,14 @@ message Expr { Expr interface_expr = 3; } + // Invoke an interface method. + // *Available in versions >= 1.dev* + message CallInterface { + TypeConName interface_type = 1; + int32 method_interned_name = 2; + Expr interface_expr = 3; + } + message Experimental { string name = 1; Type type = 2 ; @@ -1045,6 +1053,10 @@ message Expr { ToInterface to_interface = 36; FromInterface from_interface = 37; + // Invoke an interface method. + // *Available in versions >= 1.dev* + CallInterface call_interface = 38; + Experimental experimental = 9999; // *Available only in 1.dev* } @@ -1433,6 +1445,20 @@ message DefTemplate { Expr maintainers = 3; // a function from the key type to [Party] } + // Implementation of an interface method. + // *Available in versions >= 1.dev* + message ImplementsMethod { + int32 method_interned_name = 1; + Expr value = 2; + } + + // Implementation of an interface. + // *Available in versions >= 1.dev* + message Implements { + TypeConName interface = 1; + repeated ImplementsMethod methods = 2; + } + // The type constructor for the template, acting as both // the name of the template and the type of the template argument. oneof tycon { @@ -1484,13 +1510,24 @@ message DefTemplate { DefKey key = 10; // optional // Interfaces that this template implements. - repeated TypeConName implements = 13; // *Available in versions >= 1.dev* + repeated Implements implements = 13; // *Available in versions >= 1.dev* +} + +// Interface method definition. +// *Available in versions >= 1.dev* +message InterfaceMethod { + Location location = 1; + int32 method_interned_name = 2; + Type type = 3; } +// Interface definition. +// *Available in versions >= 1.dev* message DefInterface { Location location = 1; int32 tycon_interned_dname = 2; repeated InterfaceChoice choices = 3; + repeated InterfaceMethod methods = 4; } // Exception definition diff --git a/daml-lf/archive/src/main/scala/com/digitalasset/daml/lf/archive/DecodeV1.scala b/daml-lf/archive/src/main/scala/com/digitalasset/daml/lf/archive/DecodeV1.scala index 7c1621e802ae..cd6a27a3c97b 100644 --- a/daml-lf/archive/src/main/scala/com/digitalasset/daml/lf/archive/DecodeV1.scala +++ b/daml-lf/archive/src/main/scala/com/digitalasset/daml/lf/archive/DecodeV1.scala @@ -591,13 +591,18 @@ private[archive] class DecodeV1(minor: LV.Minor) { .map(decodeChoice(tpl, _)) .map(ch => (ch.name, ch)), observers = decodeExpr(lfTempl.getObservers, s"$tpl:observer"), - implements = lfImplements.map(decodeTypeConName), + implements = lfImplements.map(decodeTemplateImplements), key = if (lfTempl.hasKey) Some(decodeTemplateKey(tpl, lfTempl.getKey, paramName)) else None, ) } + // TODO https://github.com/digital-asset/daml/issues/10810 + // Decode the rest and store it in the AST + private[this] def decodeTemplateImplements(impl: PLF.DefTemplate.Implements): TypeConName = + decodeTypeConName(impl.getInterface) + private[archive] def decodeChoice( tpl: DottedName, lfChoice: PLF.TemplateChoice, @@ -1114,6 +1119,10 @@ private[archive] class DecodeV1(minor: LV.Minor) { value = decodeExpr(fromInterface.getInterfaceExpr, definition), ) + case PLF.Expr.SumCase.CALL_INTERFACE => + // TODO https://github.com/digital-asset/daml/issues/10810 + throw Error.Parsing("Expr.call_interface not yet implemented") + case PLF.Expr.SumCase.SUM_NOT_SET => throw Error.Parsing("Expr.SUM_NOT_SET") diff --git a/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/EncodeV1.scala b/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/EncodeV1.scala index 2f59c2bb0c69..ab0099af5bd4 100644 --- a/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/EncodeV1.scala +++ b/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/EncodeV1.scala @@ -814,7 +814,16 @@ private[daml] class EncodeV1(minor: LV.Minor) { b.accumulateLeft(template.choices.sortByKey)(_ addChoices _) b.setObservers(template.observers) template.key.foreach(b.setKey(_)) - b.accumulateLeft(template.implements)(_ addImplements _) + b.accumulateLeft(template.implements)(_ addImplements encodeTemplateImplements(_)) + b.build() + } + + // TODO https://github.com/digital-asset/daml/issues/10810 + private implicit def encodeTemplateImplements( + name: Ref.TypeConName + ): PLF.DefTemplate.Implements = { + val b = PLF.DefTemplate.Implements.newBuilder() + b.setInterface(name) b.build() }