souffle  2.0.2-371-g6315b36
TypeConstraints.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, The Souffle Developers. All rights reserved.
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file TypeConstraints.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
21 #include "ast/utility/Utils.h"
23 
24 namespace souffle::ast::analysis {
25 
26 // -----------------------------------------------------------------------------
27 // Type Deduction Lattice
28 // -----------------------------------------------------------------------------
29 
30 /**
31  * An implementation of a meet operation between sets of types computing
32  * the set of pair-wise greatest common subtypes.
33  */
34 struct sub_type {
35  bool operator()(TypeSet& a, const TypeSet& b) const {
36  // compute result set
37  TypeSet greatestCommonSubtypes = getGreatestCommonSubtypes(a, b);
38 
39  // check whether a should change
40  if (greatestCommonSubtypes == a) {
41  return false;
42  }
43 
44  // update a
45  a = greatestCommonSubtypes;
46  return true;
47  }
48 };
49 
50 /**
51  * A factory for computing sets of types covering all potential types.
52  */
53 struct all_type_factory {
54  TypeSet operator()() const {
55  return TypeSet(true);
56  }
57 };
58 
59 /**
60  * The type lattice forming the property space for the Type analysis. The
61  * value set is given by sets of types and the meet operator is based on the
62  * pair-wise computation of greatest common subtypes. Correspondingly, the
63  * bottom element has to be the set of all types.
64  */
65 struct type_lattice : public property_space<TypeSet, sub_type, all_type_factory> {};
66 
67 /** The definition of the type of variable to be utilized in the type analysis */
69 
70 /** The definition of the type of constraint to be utilized in the type analysis */
71 using TypeConstraint = std::shared_ptr<Constraint<TypeVar>>;
72 
73 /**
74  * Constraint analysis framework for types.
75  *
76  * The analysis operates on the concept of sinks and sources.
77  * If the atom is negated or is a head then it's a sink,
78  * and we can only extract the kind constraint from it
79  * Otherwise it is a source, and the type of the element must
80  * be a subtype of source attribute.
81  */
82 class TypeConstraintsAnalysis : public ConstraintAnalysis<TypeVar> {
83 public:
85 
86 private:
87  const TranslationUnit& tu;
88  const TypeEnvironment& typeEnv = tu.getAnalysis<TypeEnvironmentAnalysis>()->getTypeEnvironment();
89  const Program& program = tu.getProgram();
92 
93  // Sinks = {head} ∪ {negated atoms}
94  std::set<const Atom*> sinks;
95 
96  /**
97  * Utility function.
98  * Iterate over atoms valid pairs of (argument, type-attribute) and apply procedure `map` for its
99  * side-effects.
100  */
101  void iterateOverAtom(const Atom& atom, std::function<void(const Argument&, const Type&)> map);
102 
103  /** Visitors */
104  void collectConstraints(const Clause& clause) override;
105  void visitSink(const Atom& atom);
106  void visitAtom(const Atom& atom) override;
107  void visitNegation(const Negation& cur) override;
108  void visitStringConstant(const StringConstant& cnst) override;
109  void visitNumericConstant(const NumericConstant& constant) override;
110  void visitBinaryConstraint(const BinaryConstraint& rel) override;
111  void visitFunctor(const Functor& fun) override;
112  void visitCounter(const Counter& counter) override;
113  void visitTypeCast(const ast::TypeCast& typeCast) override;
114  void visitRecordInit(const RecordInit& record) override;
115  void visitBranchInit(const BranchInit& adt) override;
116  void visitAggregator(const Aggregator& agg) override;
117 };
118 
119 } // namespace souffle::ast::analysis
souffle::ast::analysis::ConstraintAnalysis
A base class for ConstraintAnalysis collecting constraints for an analysis by visiting every node of ...
Definition: Constraint.h:64
souffle::ast::analysis::sub_type::operator()
bool operator()(TypeSet &a, const TypeSet &b) const
Definition: TypeConstraints.h:43
souffle::ast::analysis::TypeConstraintsAnalysis::visitTypeCast
void visitTypeCast(const ast::TypeCast &typeCast) override
Definition: TypeConstraints.cpp:544
souffle::ast::analysis::SumTypeBranchesAnalysis
Definition: SumTypeBranches.h:40
souffle::ast::analysis::ConstraintAnalysisVar
A variable type to be utilized by AST constraint analysis.
Definition: Constraint.h:45
souffle::map
auto map(const std::vector< A > &xs, F &&f)
Applies a function to each element of a vector and returns the results.
Definition: ContainerUtil.h:158
souffle::ast::analysis::TypeConstraintsAnalysis::visitFunctor
void visitFunctor(const Functor &fun) override
Definition: TypeConstraints.cpp:490
Constraint.h
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::analysis::Type
An abstract base class for types to be covered within a type environment.
Definition: TypeSystem.h:51
souffle::ast::analysis::TypeConstraintsAnalysis::tu
const TranslationUnit & tu
Definition: TypeConstraints.h:91
souffle::ast::Atom
An atom class.
Definition: Atom.h:51
souffle::ast::analysis::TypeSet
A collection to represent sets of types.
Definition: TypeSystem.h:249
souffle::ast::Counter
counter functor (incrementing a value after each invocation)
Definition: Counter.h:34
Utils.h
souffle::ast::analysis::TypeConstraintsAnalysis::visitCounter
void visitCounter(const Counter &counter) override
Definition: TypeConstraints.cpp:540
souffle::ast::analysis::getGreatestCommonSubtypes
TypeSet getGreatestCommonSubtypes(const Type &a, const Type &b)
Computes the greatest common sub types of the two given types.
Definition: TypeSystem.cpp:254
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
ConstraintSystem.h
souffle::ast::analysis::TypeEnvironmentAnalysis
Definition: TypeEnvironment.h:38
souffle::ast::Program
The program class consists of relations, clauses and types.
Definition: Program.h:52
souffle::ast::analysis::TypeConstraintsAnalysis::typeAnalysis
const TypeAnalysis & typeAnalysis
Definition: TypeConstraints.h:95
souffle::ast::analysis::TypeConstraintsAnalysis::visitBinaryConstraint
void visitBinaryConstraint(const BinaryConstraint &rel) override
Definition: TypeConstraints.cpp:483
souffle::ast::analysis::TypeConstraintsAnalysis::visitSink
void visitSink(const Atom &atom)
Definition: TypeConstraints.cpp:390
souffle::ast::TypeCast
Defines a type cast class for expressions.
Definition: TypeCast.h:46
souffle::ast::analysis::TypeConstraintsAnalysis::typeEnv
const TypeEnvironment & typeEnv
Definition: TypeConstraints.h:92
souffle::ast::Negation
Negation of an atom negated atom.
Definition: Negation.h:50
StringUtil.h
souffle::ast::analysis::TypeConstraintsAnalysis::visitStringConstant
void visitStringConstant(const StringConstant &cnst) override
Definition: TypeConstraints.cpp:419
souffle::ast::analysis::TypeEnvironment
A type environment is a set of types.
Definition: TypeSystem.h:401
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::ast::analysis::TypeConstraint
std::shared_ptr< Constraint< TypeVar > > TypeConstraint
The definition of the type of constraint to be utilized in the type analysis.
Definition: TypeConstraints.h:75
souffle::ast::analysis::property_space
A MPL type for defining a property space.
Definition: ConstraintSystem.h:95
souffle::ast::Functor
Abstract functor class.
Definition: Functor.h:36
souffle::ast::analysis::TypeConstraintsAnalysis::sinks
std::set< const Atom * > sinks
Definition: TypeConstraints.h:98
souffle::ast::analysis::TypeConstraintsAnalysis
Constraint analysis framework for types.
Definition: TypeConstraints.h:86
souffle::ast::TranslationUnit::getAnalysis
Analysis * getAnalysis() const
get analysis: analysis is generated on the fly if not present
Definition: TranslationUnit.h:60
souffle::ast::BinaryConstraint
Binary constraint class.
Definition: BinaryConstraint.h:53
souffle::ast::Aggregator
Defines the aggregator class.
Definition: Aggregator.h:53
b
l j a showGridBackground &&c b raw series this eventEmitter b
Definition: htmlJsChartistMin.h:15
souffle::ast::analysis::TypeConstraintsAnalysis::visitNegation
void visitNegation(const Negation &cur) override
Definition: TypeConstraints.cpp:415
souffle::ast::analysis::TypeConstraintsAnalysis::visitNumericConstant
void visitNumericConstant(const NumericConstant &constant) override
Definition: TypeConstraints.cpp:423
souffle::ast::analysis::TypeConstraintsAnalysis::sumTypesBranches
const SumTypeBranchesAnalysis & sumTypesBranches
Definition: TypeConstraints.h:94
souffle::ast::analysis::all_type_factory::operator()
TypeSet operator()() const
Definition: TypeConstraints.h:58
souffle::ast::analysis::TypeConstraintsAnalysis::collectConstraints
void collectConstraints(const Clause &clause) override
Visitors.
Definition: TypeConstraints.cpp:635
souffle::ast::analysis
Definition: Aggregate.cpp:39
souffle::ast::analysis::TypeConstraintsAnalysis::TypeConstraintsAnalysis
TypeConstraintsAnalysis(const TranslationUnit &tu)
Definition: TypeConstraints.h:88
souffle::ast::analysis::TypeConstraintsAnalysis::visitBranchInit
void visitBranchInit(const BranchInit &adt) override
Definition: TypeConstraints.cpp:568
souffle::ast::BranchInit
Initialization of ADT instance.
Definition: BranchInit.h:49
souffle::ast::analysis::TypeAnalysis
Definition: Type.h:45
souffle::ast::analysis::TypeConstraintsAnalysis::visitAggregator
void visitAggregator(const Aggregator &agg) override
Definition: TypeConstraints.cpp:597
souffle::ast::analysis::TypeConstraintsAnalysis::iterateOverAtom
void iterateOverAtom(const Atom &atom, std::function< void(const Argument &, const Type &)> map)
Utility function.
Definition: TypeConstraints.cpp:613
souffle::ast::NumericConstant
Numeric Constant.
Definition: NumericConstant.h:43
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
TypeEnvironment.h
souffle::ast::analysis::TypeConstraintsAnalysis::visitRecordInit
void visitRecordInit(const RecordInit &record) override
Definition: TypeConstraints.cpp:561
souffle::ast::TranslationUnit::getProgram
Program & getProgram() const
Return the program.
Definition: TranslationUnit.h:84
souffle::ast::analysis::all_type_factory
A factory for computing sets of types covering all potential types.
Definition: TypeConstraints.h:57
souffle::ast::StringConstant
String constant class.
Definition: StringConstant.h:37
souffle::ast::analysis::type_lattice
The type lattice forming the property space for the Type analysis.
Definition: TypeConstraints.h:69
souffle::ast::RecordInit
Defines a record initialization class.
Definition: RecordInit.h:42
souffle::ast::analysis::TypeConstraintsAnalysis::program
const Program & program
Definition: TypeConstraints.h:93
TypeSystem.h
souffle::ast::analysis::TypeConstraintsAnalysis::visitAtom
void visitAtom(const Atom &atom) override
Definition: TypeConstraints.cpp:404