From 6e228428b76246e118fca5b2545d063520b09e27 Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Fri, 6 Oct 2023 15:55:40 +0200
Subject: [PATCH] Update to Rodin 3.8 sources

---
 META-INF/MANIFEST.MF                          |  2 +-
 build.gradle                                  |  2 +-
 .../eventb/core/ast/ExtendedPredicate.java    | 18 +++-------
 src/org/eventb/core/ast/FreeIdentifier.java   |  5 ++-
 src/org/eventb/core/ast/ITypeEnvironment.java | 33 ++++++++++++++++++-
 .../ast/datatype/DatatypeTranslation.java     |  9 +++--
 .../ast/extension/ExtensionTranslation.java   | 31 ++++++++++++++---
 .../core/typecheck/TypeEnvironment.java       | 13 +++++++-
 8 files changed, 86 insertions(+), 27 deletions(-)

diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
index 0d8ce7e..e9ed886 100644
--- a/META-INF/MANIFEST.MF
+++ b/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eventb.core.ast;singleton:=true
-Bundle-Version: 3.6.0.qualifier
+Bundle-Version: 3.7.0.qualifier
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
 Export-Package: org.eventb.core.ast,
diff --git a/build.gradle b/build.gradle
index d75afa8..ef14503 100644
--- a/build.gradle
+++ b/build.gradle
@@ -6,7 +6,7 @@ plugins {
 }
 
 project.group = 'de.hhu.stups'
-project.version = "3.7.0"
+project.version = "3.8.0-SNAPSHOT"
 final isSnapshot = project.version.endsWith("-SNAPSHOT")
 
 sourceSets {
diff --git a/src/org/eventb/core/ast/ExtendedPredicate.java b/src/org/eventb/core/ast/ExtendedPredicate.java
index 24a4bc3..6f06ace 100644
--- a/src/org/eventb/core/ast/ExtendedPredicate.java
+++ b/src/org/eventb/core/ast/ExtendedPredicate.java
@@ -202,22 +202,14 @@ public class ExtendedPredicate extends Predicate implements IExtendedFormula {
 			}
 		}
 
-		final FormulaFactory ff = getFactory();
-		BoundIdentDecl[] boundDecls;
-		if (boundIdents.length > 0) {
-			boundDecls = new BoundIdentDecl[boundIdents.length];
-			for (int i = 0; i < boundIdents.length; i++) {
-				boundDecls[i] = new BoundIdentDecl("b" + i, null, boundIdents[i].getType(), ff);
-			}
-		} else {
-			boundDecls = NO_BOUND_IDENT_DECL;
-		}
-
 		if (extension instanceof IPredicateExtension2) {
 			typeChecked = ((IPredicateExtension2) extension).verifyType(childExpressions, childPredicates);
 		} else {
-			TypeCheckResult tcRes = new TypeCheckResult(ff.makeTypeEnvironment().makeSnapshot());
-			typeCheck(tcRes, boundDecls);
+			final FormulaFactory ff = getFactory();
+			ITypeEnvironmentBuilder typeEnv = ff.makeTypeEnvironment();
+			typeEnv.addAll(freeIdents);
+			TypeCheckResult tcRes = new TypeCheckResult(typeEnv.makeSnapshot());
+			extension.typeCheck(this, new TypeCheckMediator(tcRes, this, false));
 			typeChecked = !tcRes.hasProblem();
 		}
 	}
diff --git a/src/org/eventb/core/ast/FreeIdentifier.java b/src/org/eventb/core/ast/FreeIdentifier.java
index 81a0241..bb65fb5 100644
--- a/src/org/eventb/core/ast/FreeIdentifier.java
+++ b/src/org/eventb/core/ast/FreeIdentifier.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2017 ETH Zurich and others.
+ * Copyright (c) 2005, 2023 ETH Zurich and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -34,8 +34,7 @@ import org.eventb.internal.core.typecheck.TypeCheckResult;
 import org.eventb.internal.core.typecheck.TypeUnifier;
 
 /**
- * This class represents either identifiers occurring free in an event-B
- * formula, or a bound identifier declaration.
+ * This class represents identifiers occurring free in an event-B formula.
  * <p>
  * Identifiers which are bound by a quantifier are instance of the class
  * {@link org.eventb.core.ast.BoundIdentifier} instead.
diff --git a/src/org/eventb/core/ast/ITypeEnvironment.java b/src/org/eventb/core/ast/ITypeEnvironment.java
index 8c72b45..55eb2a6 100644
--- a/src/org/eventb/core/ast/ITypeEnvironment.java
+++ b/src/org/eventb/core/ast/ITypeEnvironment.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2017 ETH Zurich and others.
+ * Copyright (c) 2005, 2023 ETH Zurich and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -209,6 +209,25 @@ public interface ITypeEnvironment {
 	 */
 	IExtensionTranslation makeExtensionTranslation();
 
+	/**
+	 * Returns a fresh extension translation based on a snapshot of this type
+	 * environment. The resulting translation will be applicable to any formula
+	 * which is type-checked within this type environment at the time of the call to
+	 * this method.
+	 *
+	 * The provided target factory must contain at least the extensions that are not
+	 * translatable by implementations of {@link IExtensionTranslation}. If it
+	 * contains more extensions, these will not be translated.
+	 *
+	 * @param targetFactory target factory to use
+	 * @return a fresh extension translation
+	 * @see Formula#translateExtensions(IExtensionTranslation)
+	 * @throws IllegalArgumentException if the target factory misses some
+	 *                                  untranslatable extensions
+	 * @since 3.7
+	 */
+	IExtensionTranslation makeExtensionTranslation(FormulaFactory targetFactory);
+
 	/**
 	 * Returns an iterator for traversing this type environment.
 	 * 
@@ -233,6 +252,18 @@ public interface ITypeEnvironment {
 	 */
 	Type getType(String name);
 
+	/**
+	 * Returns the list of free identifiers in this type environment.
+	 *
+	 * This is equivalent to getting all the identifiers in this type environment
+	 * with {@link #getNames()} and then getting the type of each identifier with
+	 * {@link #getType(String)}.
+	 *
+	 * @return the list of free identifiers
+	 * @since 3.7
+	 */
+	FreeIdentifier[] getFreeIdentifiers();
+
 	/**
 	 * Returns whether this type environment is empty.
 	 * 
diff --git a/src/org/eventb/internal/core/ast/datatype/DatatypeTranslation.java b/src/org/eventb/internal/core/ast/datatype/DatatypeTranslation.java
index 55f015f..cd5c4d1 100644
--- a/src/org/eventb/internal/core/ast/datatype/DatatypeTranslation.java
+++ b/src/org/eventb/internal/core/ast/datatype/DatatypeTranslation.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2013, 2016 Systerel and others.
+ * Copyright (c) 2013, 2023 Systerel and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -136,8 +136,11 @@ public class DatatypeTranslation extends AbstractTranslation implements
 	}
 
 	public Type translateParametricType(ParametricType type) {
-		assert type.getExprExtension().getOrigin() instanceof Datatype;
-		return getTranslatorFor(type).getTranslatedType();
+		if (type.getExprExtension().getOrigin() instanceof Datatype) {
+			return getTranslatorFor(type).getTranslatedType();
+		} else {
+			return type.translate(targetFactory);
+		}
 	}
 
 	public Expression translate(ExtendedExpression src,
diff --git a/src/org/eventb/internal/core/ast/extension/ExtensionTranslation.java b/src/org/eventb/internal/core/ast/extension/ExtensionTranslation.java
index f7d5e7c..00bfe41 100644
--- a/src/org/eventb/internal/core/ast/extension/ExtensionTranslation.java
+++ b/src/org/eventb/internal/core/ast/extension/ExtensionTranslation.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2014, 2016 Systerel and others.
+ * Copyright (c) 2014, 2023 Systerel and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -77,8 +77,20 @@ public class ExtensionTranslation extends AbstractTranslation implements
 	private final ITypeCheckingRewriter rewriter;
 
 	public ExtensionTranslation(ISealedTypeEnvironment srcTypenv) {
+		this(srcTypenv, null);
+	}
+
+	public ExtensionTranslation(ISealedTypeEnvironment srcTypenv, FormulaFactory trgFac) {
 		super(srcTypenv);
-		this.trgFactory = computeTargetFactory(srcTypenv.getFormulaFactory());
+		final Set<IFormulaExtension> requiredExtensions = computeTargetExtensions(srcTypenv.getFormulaFactory());
+		if (trgFac == null) {
+			trgFactory = FormulaFactory.getInstance(requiredExtensions);
+		} else {
+			if (!trgFac.getExtensions().containsAll(requiredExtensions)) {
+				throw new IllegalArgumentException("Target factory misses some untranslatable extensions");
+			}
+			trgFactory = trgFac;
+		}
 		final Set<String> usedNames = new HashSet<String>(srcTypenv.getNames());
 		this.nameSolver = new FreshNameSolver(usedNames, trgFactory);
 
@@ -90,7 +102,7 @@ public class ExtensionTranslation extends AbstractTranslation implements
 		populateTargetTypenv(typeRewriter);
 	}
 
-	private static FormulaFactory computeTargetFactory(FormulaFactory fac) {
+	private static Set<IFormulaExtension> computeTargetExtensions(FormulaFactory fac) {
 		final Set<IFormulaExtension> keptExtensions;
 		final Set<IFormulaExtension> extensions = fac.getExtensions();
 		keptExtensions = new LinkedHashSet<IFormulaExtension>();
@@ -104,7 +116,7 @@ public class ExtensionTranslation extends AbstractTranslation implements
 				keptExtensions.add(extension);
 			}
 		}
-		return FormulaFactory.getInstance(keptExtensions);
+		return keptExtensions;
 	}
 
 	private void populateTargetTypenv(TypeRewriter typeRewriter) {
@@ -128,6 +140,9 @@ public class ExtensionTranslation extends AbstractTranslation implements
 	}
 
 	public Type translate(ParametricType src) {
+		if (trgFactory.hasExtension(src.getExprExtension())) {
+			return src.translate(trgFactory);
+		}
 		final ExpressionExtSignature signature = getSignature(src);
 		final TypeExtTranslator translator = typeTranslators.get(signature);
 		return translator.translate();
@@ -135,6 +150,10 @@ public class ExtensionTranslation extends AbstractTranslation implements
 
 	public Expression translate(ExtendedExpression src,
 			Expression[] newChildExprs, Predicate[] newChildPreds) {
+		if (trgFactory.hasExtension(src.getExtension())) {
+			return trgFactory.makeExtendedExpression(src.getExtension(), newChildExprs, newChildPreds,
+					src.getSourceLocation(), src.getType().translate(trgFactory));
+		}
 		final ExpressionExtSignature signature = getSignature(src);
 		final ExpressionExtTranslator translator = exprTranslators
 				.get(signature);
@@ -143,6 +162,10 @@ public class ExtensionTranslation extends AbstractTranslation implements
 
 	public Predicate translate(ExtendedPredicate src,
 			Expression[] newChildExprs, Predicate[] newChildPreds) {
+		if (trgFactory.hasExtension(src.getExtension())) {
+			return trgFactory.makeExtendedPredicate(src.getExtension(), newChildExprs, newChildPreds,
+					src.getSourceLocation());
+		}
 		final PredicateExtSignature signature = getSignature(src);
 		final PredicateExtTranslator translator = predTranslators
 				.get(signature);
diff --git a/src/org/eventb/internal/core/typecheck/TypeEnvironment.java b/src/org/eventb/internal/core/typecheck/TypeEnvironment.java
index 26b9227..e907573 100644
--- a/src/org/eventb/internal/core/typecheck/TypeEnvironment.java
+++ b/src/org/eventb/internal/core/typecheck/TypeEnvironment.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2014 ETH Zurich and others.
+ * Copyright (c) 2005, 2023 ETH Zurich and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -184,6 +184,11 @@ public abstract class TypeEnvironment implements ITypeEnvironment{
 		return new ExtensionTranslation(this.makeSnapshot());
 	}
 
+	@Override
+	public IExtensionTranslation makeExtensionTranslation(FormulaFactory targetFactory) {
+		return new ExtensionTranslation(this.makeSnapshot(), targetFactory);
+	}
+
 	@Override
 	public IIterator getIterator(){
 		return new InternalIterator(map.entrySet().iterator());
@@ -199,6 +204,12 @@ public abstract class TypeEnvironment implements ITypeEnvironment{
 		return map.get(name);
 	}
 
+	@Override
+	public FreeIdentifier[] getFreeIdentifiers() {
+		return map.entrySet().stream().map(e -> ff.makeFreeIdentifier(e.getKey(), null, e.getValue()))
+				.toArray(FreeIdentifier[]::new);
+	}
+
 	@Override
 	public boolean isEmpty() {
 		return map.isEmpty();
-- 
GitLab