Skip to content

Commit

Permalink
interface methods: Add protobuf
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sofiafaro-da committed Sep 23, 2021
1 parent 8de162b commit 8035324
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
10 changes: 9 additions & 1 deletion compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/DecodeV1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/EncodeV1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand Down Expand Up @@ -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*
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 8035324

Please sign in to comment.