Skip to content

Commit

Permalink
LF: Clean up Ast (#11786)
Browse files Browse the repository at this point in the history
- rename non-default builders `apply` to `build`
  * avoid confusing both
  * make explicit the build can crash

- make interfaceId and templateId fields consitent

- use when possible named arguments

- check for non-repetition of inherited choices

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da authored Nov 19, 2021
1 parent 25e5bbb commit cbe9c92
Show file tree
Hide file tree
Showing 15 changed files with 310 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class ReplService(
): Unit = {
val lfVer = LanguageVersion(LanguageVersion.Major.V1, LanguageVersion.Minor(req.getMinor))
val mod = archive.moduleDecoder(lfVer, homePackageId).assertFromByteString(req.getDamlLf1)
val pkg = Package((mainModules + (mod.name -> mod)).values, Seq(), lfVer, None)
val pkg = Package(mainModules.updated(mod.name, mod), Set.empty, lfVer, None)
// TODO[AH] Provide daml-script package id from REPL client.
val Some(scriptPackageId) = this.signatures.collectFirst {
case (pkgId, pkg) if pkg.modules.contains(DottedName.assertFromString("Daml.Script")) => pkgId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private[archive] class DecodeV1(minor: LV.Minor) {
val internedTypes = decodeInternedTypes(env0, lfPackage)
val env = env0.copy(internedTypes = internedTypes)

Package(
Package.build(
modules = lfPackage.getModulesList.asScala.map(env.decodeModule(_)),
directDeps = dependencyTracker.getDependencies,
languageVersion = languageVersion,
Expand Down Expand Up @@ -289,7 +289,7 @@ private[archive] class DecodeV1(minor: LV.Minor) {
}
}

Module(
Module.build(
moduleName,
defs,
templates,
Expand Down Expand Up @@ -583,16 +583,14 @@ private[archive] class DecodeV1(minor: LV.Minor) {
lfTempl.getParamInternedStr,
"DefTemplate.param.param",
)
Template(
Template.build(
param = paramName,
precond = if (lfTempl.hasPrecond) decodeExpr(lfTempl.getPrecond, s"$tpl:ensure") else ETrue,
signatories = decodeExpr(lfTempl.getSignatories, s"$tpl.signatory"),
agreementText = decodeExpr(lfTempl.getAgreement, s"$tpl:agreement"),
choices = lfTempl.getChoicesList.asScala
.map(decodeChoice(tpl, _))
.map(ch => (ch.name, ch)),
choices = lfTempl.getChoicesList.asScala.view.map(decodeChoice(tpl, _)),
observers = decodeExpr(lfTempl.getObservers, s"$tpl:observer"),
implements = lfImplements.map(decodeTemplateImplements).map(impl => (impl.interface, impl)),
implements = lfImplements.view.map(decodeTemplateImplements),
key =
if (lfTempl.hasKey) Some(decodeTemplateKey(tpl, lfTempl.getKey, paramName))
else None,
Expand All @@ -602,21 +600,19 @@ private[archive] class DecodeV1(minor: LV.Minor) {
private[this] def decodeTemplateImplements(
lfImpl: PLF.DefTemplate.Implements
): TemplateImplements =
TemplateImplements(
interface = decodeTypeConName(lfImpl.getInterface),
methods = lfImpl.getMethodsList.asScala
.map(decodeTemplateImplementsMethod)
.map(method => (method.name, method)),
inheritedChoices = lfImpl.getInheritedChoiceInternedNamesList.asScala
.map(getInternedName(_, "TemplateImplements.inheritedChoices"))
.toSet,
TemplateImplements.build(
interfaceId = decodeTypeConName(lfImpl.getInterface),
methods = lfImpl.getMethodsList.asScala.view.map(decodeTemplateImplementsMethod),
inheritedChoices = lfImpl.getInheritedChoiceInternedNamesList.asScala.view
.map(getInternedName(_, "TemplateImplements.inheritedChoices")),
)

private[this] def decodeTemplateImplementsMethod(
lfMethod: PLF.DefTemplate.ImplementsMethod
): TemplateImplementsMethod =
TemplateImplementsMethod(
name = getInternedName(lfMethod.getMethodInternedName, "TemplateImplementsMethod.name"),
methodName =
getInternedName(lfMethod.getMethodInternedName, "TemplateImplementsMethod.name"),
value = decodeExpr(lfMethod.getValue, "TemplateImplementsMethod.value"),
)

Expand Down Expand Up @@ -669,14 +665,10 @@ private[archive] class DecodeV1(minor: LV.Minor) {
id: DottedName,
lfInterface: PLF.DefInterface,
): DefInterface =
DefInterface(
DefInterface.build(
param = getInternedName(lfInterface.getParamInternedStr, "DefInterface.param"),
fixedChoices = lfInterface.getFixedChoicesList.asScala.view
.map(decodeChoice(id, _))
.map(choice => choice.name -> choice),
methods = lfInterface.getMethodsList.asScala.view
.map(decodeInterfaceMethod)
.map(method => method.name -> method),
fixedChoices = lfInterface.getFixedChoicesList.asScala.view.map(decodeChoice(id, _)),
methods = lfInterface.getMethodsList.asScala.view.map(decodeInterfaceMethod),
precond = decodeExpr(lfInterface.getPrecond, s"$id:ensure"),
)

Expand Down Expand Up @@ -1126,26 +1118,27 @@ private[archive] class DecodeV1(minor: LV.Minor) {
assertSince(LV.Features.interfaces, "Expr.to_interface")
val toInterface = lfExpr.getToInterface
EToInterface(
iface = decodeTypeConName(toInterface.getInterfaceType),
tpl = decodeTypeConName(toInterface.getTemplateType),
interfaceId = decodeTypeConName(toInterface.getInterfaceType),
templateId = decodeTypeConName(toInterface.getTemplateType),
value = decodeExpr(toInterface.getTemplateExpr, definition),
)

case PLF.Expr.SumCase.FROM_INTERFACE =>
assertSince(LV.Features.interfaces, "Expr.from_interface")
val fromInterface = lfExpr.getFromInterface
EFromInterface(
iface = decodeTypeConName(fromInterface.getInterfaceType),
tpl = decodeTypeConName(fromInterface.getTemplateType),
interfaceId = decodeTypeConName(fromInterface.getInterfaceType),
templateId = decodeTypeConName(fromInterface.getTemplateType),
value = decodeExpr(fromInterface.getInterfaceExpr, definition),
)

case PLF.Expr.SumCase.CALL_INTERFACE =>
assertSince(LV.Features.interfaces, "Expr.call_interface")
val callInterface = lfExpr.getCallInterface
ECallInterface(
iface = decodeTypeConName(callInterface.getInterfaceType),
method = getInternedName(callInterface.getMethodInternedName, "ECallInterface.method"),
interfaceId = decodeTypeConName(callInterface.getInterfaceType),
methodName =
getInternedName(callInterface.getMethodInternedName, "ECallInterface.method"),
value = decodeExpr(callInterface.getInterfaceExpr, definition),
)

Expand Down Expand Up @@ -1283,7 +1276,7 @@ private[archive] class DecodeV1(minor: LV.Minor) {
case PLF.Update.SumCase.CREATE_INTERFACE =>
val create = lfUpdate.getCreateInterface
UpdateCreateInterface(
interface = decodeTypeConName(create.getInterface),
interfaceId = decodeTypeConName(create.getInterface),
arg = decodeExpr(create.getExpr, definition),
)

Expand All @@ -1307,7 +1300,7 @@ private[archive] class DecodeV1(minor: LV.Minor) {
assertSince(LV.Features.interfaces, "exerciseInterface")
val exercise = lfUpdate.getExerciseInterface
UpdateExerciseInterface(
interface = decodeTypeConName(exercise.getInterface),
interfaceId = decodeTypeConName(exercise.getInterface),
choice = handleInternedName(exercise.getChoiceInternedStr),
cidE = decodeExpr(exercise.getCid, definition),
argE = decodeExpr(exercise.getArg, definition),
Expand Down Expand Up @@ -1340,7 +1333,7 @@ private[archive] class DecodeV1(minor: LV.Minor) {
assertSince(LV.Features.interfaces, "fetchInterface")
val fetch = lfUpdate.getFetchInterface
UpdateFetchInterface(
interface = decodeTypeConName(fetch.getInterface),
interfaceId = decodeTypeConName(fetch.getInterface),
contractId = decodeExpr(fetch.getCid, definition),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private[daml] object DamlLfEncoder extends App {
} else None

val pkg =
Ast.Package(modules, Set.empty[PackageId], parserParameters.languageVersion, metadata)
Ast.Package.build(modules, Set.empty[PackageId], parserParameters.languageVersion, metadata)
val pkgs = PackageInterface(Map(pkgId -> pkg))

Validation.checkPackage(pkgs, pkgId, pkg).left.foreach(e => error(e.pretty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2106,12 +2106,7 @@ class EngineTest
val pkgId = Ref.PackageId.assertFromString("-pkg-")

def pkg(version: LV) =
language.Ast.Package(
Iterable.empty,
Iterable.empty,
version,
None,
)
language.Ast.Package(Map.empty, Set.empty, version, None)

"reject disallow packages" in {
val negativeTestCases = Table(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class InterfaceReaderSpec extends AnyWordSpec with Matchers with Inside {
"Package metadata should be extracted if present" in {
def pkg(metadata: Option[Ast.PackageMetadata]) =
Ast.Package(
modules = Seq.empty,
modules = Map.empty,
directDeps = Set.empty,
languageVersion = LanguageVersion.default,
metadata = metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ private[lf] final class Compiler(
addDef(compileSignatories(identifier, tmpl))
addDef(compileObservers(identifier, tmpl))
tmpl.implements.values.foreach { impl =>
addDef(compileCreateByInterface(identifier, tmpl, impl.interface))
addDef(compileImplements(identifier, impl.interface))
addDef(compileCreateByInterface(identifier, tmpl, impl.interfaceId))
addDef(compileImplements(identifier, impl.interfaceId))
impl.methods.values.foreach(method =>
addDef(compileImplementsMethod(identifier, impl.interface, method))
addDef(compileImplementsMethod(identifier, impl.interfaceId, method))
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ class InterpreterTest extends AnyWordSpec with Matchers with TableDrivenProperty
PureCompiledPackages.assertBuild(
Map(
dummyPkg ->
Package(
Package.build(
List(
Module(
Module.build(
name = modName,
definitions = Map(
DottedName.assertFromString("bar") ->
Expand All @@ -211,14 +211,14 @@ class InterpreterTest extends AnyWordSpec with Matchers with TableDrivenProperty
val pkgs3 = PureCompiledPackages.assertBuild(
Map(
dummyPkg ->
Package(
Package.build(
List(
Module(
name = modName,
definitions = Map.empty,
templates = Map.empty,
exceptions = Map.empty,
interfaces = List.empty,
interfaces = Map.empty,
featureFlags = FeatureFlags.default,
)
),
Expand Down
Loading

0 comments on commit cbe9c92

Please sign in to comment.