Skip to content

Commit 25b0ce4

Browse files
pmderodatraph-amiard
authored andcommitted
Adalog: remove the Var_Or_Null type
In practice, logic variables are pointers, and thus are nullable types, so there is no need for Var_Or_Null, which is an "option" wrapper: either we have a null Logic_Var (i.e. no variable), either we have a non-null one. This is a very minor optimization, as the dependency analysis primitives now only accept/return a pointer instead of a pointer + a boolean. TN: SB20-024 For libadalang/langkit#618
1 parent 779fe03 commit 25b0ce4

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

support/langkit_support-adalog-solver.adb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,12 @@ package body Langkit_Support.Adalog.Solver is
682682
-- if ``Visiting (I) and not Appended (I)``, then we have found a
683683
-- dependency cycle.
684684

685-
function Id (S : Var_Or_Null) return Natural
686-
is (if S.Exists then Id (S.Logic_Var) else 0);
687-
-- Return the Id for the ``S`` variable, or 0 if there is no variable
685+
function Id_Or_Null (Var : Logic_Var) return Natural
686+
is (if Var = null then 0 else Id (Var));
687+
-- Return the Id for the ``Var`` variable, or 0 if there is no variable
688688

689689
function Defined (S : Atomic_Relation_Type) return Natural
690-
is (Id (Defined_Var (S)));
690+
is (Id_Or_Null (Defined_Var (S)));
691691
-- Return the Id for the variable that ``S`` defines, or 0 if it
692692
-- contains no definition.
693693

@@ -968,31 +968,31 @@ package body Langkit_Support.Adalog.Solver is
968968
-- Used_Var --
969969
--------------
970970

971-
function Used_Var (Self : Atomic_Relation_Type) return Var_Or_Null
971+
function Used_Var (Self : Atomic_Relation_Type) return Logic_Var
972972
is
973973
-- We handle Unify here, even though it is not strictly treated in the
974974
-- dependency graph, so that the Unify_From variable is registered in
975975
-- the list of variables of the equation. TODO??? Might be cleaner to
976976
-- have a separate function to return all variables a relation uses?
977977
(case Self.Kind is
978-
when Assign | True | False | N_Predicate => Null_Var,
979-
when Propagate => (True, Self.From),
980-
when Predicate => (True, Self.Target),
981-
when Unify => (True, Self.Unify_From));
978+
when Assign | True | False | N_Predicate => null,
979+
when Propagate => Self.From,
980+
when Predicate => Self.Target,
981+
when Unify => Self.Unify_From);
982982

983983
-----------------
984984
-- Defined_Var --
985985
-----------------
986986

987-
function Defined_Var (Self : Atomic_Relation_Type) return Var_Or_Null
987+
function Defined_Var (Self : Atomic_Relation_Type) return Logic_Var
988988
is
989989
-- We handle Unify here, even though it is not strictly treated in the
990990
-- dependency graph, so that the Target variable is registered in
991991
-- the list of variables of the equation. TODO??? Might be cleaner to
992992
-- have a separate function to return all variables a relation defines?
993993
(case Self.Kind is
994-
when Assign | Propagate | Unify => (True, Self.Target),
995-
when Predicate | True | False | N_Predicate => Null_Var);
994+
when Assign | Propagate | Unify => Self.Target,
995+
when Predicate | True | False | N_Predicate => null);
996996

997997
-----------------
998998
-- To_Relation --

support/langkit_support-adalog-solver.ads

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ private
264264
-- Atomic relations dependency graph --
265265
----------------------------------------
266266

267-
-- In this section, we'll define types and operations on the graph of
268-
-- dependencies between atomic relations. This is what will allow us to:
267+
-- This section defines operations to explore the depenency graph between
268+
-- atomic relations. They are used to:
269269
--
270270
-- 1. Sort a list of atomic relations topologically, so that they form an
271-
-- executable list of instructions.
271+
-- executable sequence of instructions.
272272
--
273273
-- 2. Define a map from vars to atomic rels where for every logic variable
274274
-- ``V``, the map maps ``V -> [R1, R2, R3, ...]`` where
@@ -285,26 +285,15 @@ private
285285
-- treats both variables as aliases. This dataflow analysis probably
286286
-- deserves a refactoring to clarify this.
287287

288-
type Var_Or_Null (Exists : Boolean := False) is record
289-
case Exists is
290-
when True => Logic_Var : Logic_Vars.Logic_Var;
291-
when False => null;
292-
end case;
293-
end record;
294-
-- Option type for a logic variable. Used to express dependencies from an
295-
-- atomic relation to a logic variable.
296-
297-
Null_Var : constant Var_Or_Null := (Exists => False);
298-
299-
function Is_Defined_Or_Null (Logic_Var : Var_Or_Null) return Boolean
288+
function Is_Defined_Or_Null (Var : Logic_Var) return Boolean
300289
is
301-
((not Logic_Var.Exists) or else Is_Defined (Logic_Var.Logic_Var));
290+
(Var = null or else Is_Defined (Var));
302291
-- Shortcut predicate. Returns whether a variable is defined or is null
303292

304-
function Used_Var (Self : Atomic_Relation_Type) return Var_Or_Null;
293+
function Used_Var (Self : Atomic_Relation_Type) return Logic_Var;
305294
-- Return the variable that this atomic relation uses, if there is one
306295

307-
function Defined_Var (Self : Atomic_Relation_Type) return Var_Or_Null;
296+
function Defined_Var (Self : Atomic_Relation_Type) return Logic_Var;
308297
-- Return the variable that this atomic relation defines, if there is one
309298

310299
-----------------------

0 commit comments

Comments
 (0)