souffle  2.0.2-371-g6315b36
Type.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. 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 Type.h
12  *
13  * A collection of type analyses operating on AST constructs.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "AggregateOp.h"
20 #include "FunctorOps.h"
21 #include "ast/NumericConstant.h"
22 #include "ast/analysis/Analysis.h"
25 #include <memory>
26 #include <set>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 
31 namespace souffle::ast {
32 class Argument;
33 class Aggregator;
34 class BinaryConstraint;
35 class Clause;
36 class Functor;
37 class FunctorDeclaration;
38 class IntrinsicFunctor;
39 class NumericConstant;
40 class UserDefinedFunctor;
41 } // namespace souffle::ast
42 
43 namespace souffle::ast::analysis {
44 
45 class TypeAnalysis : public Analysis {
46 public:
47  static constexpr const char* name = "type-analysis";
48 
50 
51  void run(const TranslationUnit& translationUnit) override;
52 
53  void print(std::ostream& os) const override;
54 
55  /** Get the computed types for the given argument. */
56  TypeSet const& getTypes(const Argument* argument) const {
57  return argumentTypes.at(argument);
58  }
59 
60  /**
61  * Analyse the given clause and computes for each contained argument
62  * a set of potential types. If the set associated to an argument is empty,
63  * no consistent typing can be found and the rule can not be properly typed.
64  *
65  * @return a map mapping each contained argument to a set of types
66  */
67  static std::map<const Argument*, TypeSet> analyseTypes(
68  const TranslationUnit& tu, const Clause& clause, std::ostream* logs = nullptr);
69 
70  // Checks whether an argument has been assigned a valid type
71  bool hasValidTypeInfo(const Argument* argument) const;
72 
73  std::set<TypeAttribute> getTypeAttributes(const Argument* arg) const;
74 
75  /** -- Functor-related methods -- */
77  TypeAttribute getFunctorReturnType(const Functor* functor) const;
78  TypeAttribute getFunctorArgType(const Functor* functor, const size_t idx) const;
79  const std::vector<TypeAttribute>& getFunctorArgTypes(const UserDefinedFunctor& udf) const;
80 
81  bool isStatefulFunctor(const UserDefinedFunctor* udf) const;
82  static bool isMultiResultFunctor(const Functor& functor);
83 
84  /** -- Polymorphism-related methods -- */
86  const std::map<const NumericConstant*, NumericConstant::Type>& getNumericConstantTypes() const;
90 
91 private:
92  // General type analysis
93  std::map<const Argument*, TypeSet> argumentTypes;
95  std::stringstream analysisLogs;
96 
97  /* Return a new clause with type-annotated variables */
99  const Clause* clause, const std::map<const Argument*, TypeSet> argumentTypes);
100 
101  // Polymorphic objects analysis
102  std::map<const IntrinsicFunctor*, const IntrinsicFunctorInfo*> functorInfo;
103  std::map<std::string, const FunctorDeclaration*> udfDeclaration;
104  std::map<const NumericConstant*, NumericConstant::Type> numericConstantType;
105  std::map<const Aggregator*, AggregateOp> aggregatorType;
106  std::map<const BinaryConstraint*, BinaryConstraintOp> constraintType;
107 
108  bool analyseIntrinsicFunctors(const TranslationUnit& translationUnit);
109  bool analyseNumericConstants(const TranslationUnit& translationUnit);
110  bool analyseAggregators(const TranslationUnit& translationUnit);
111  bool analyseBinaryConstraints(const TranslationUnit& translationUnit);
112 
113  bool isFloat(const Argument* argument) const;
114  bool isUnsigned(const Argument* argument) const;
115  bool isSymbol(const Argument* argument) const;
116 };
117 
118 } // namespace souffle::ast::analysis
BinaryConstraintOps.h
souffle::ast::analysis::TypeAnalysis::name
static constexpr const char * name
Definition: Type.h:47
souffle::ast::analysis::TypeAnalysis::analyseBinaryConstraints
bool analyseBinaryConstraints(const TranslationUnit &translationUnit)
Definition: Type.cpp:392
souffle::ast::analysis::TypeAnalysis::analyseIntrinsicFunctors
bool analyseIntrinsicFunctors(const TranslationUnit &translationUnit)
Definition: Type.cpp:288
souffle::ast::analysis::TypeAnalysis::analysisLogs
std::stringstream analysisLogs
Definition: Type.h:95
TypeAttribute
Type attribute class.
AggregateOp.h
souffle::ast::analysis::TypeAnalysis::getPolymorphicOperator
AggregateOp getPolymorphicOperator(const Aggregator *agg) const
Definition: Type.cpp:278
souffle::ast::analysis::TypeAnalysis::getPolymorphicNumericConstantType
NumericConstant::Type getPolymorphicNumericConstantType(const NumericConstant *nc) const
– Polymorphism-related methods –
Definition: Type.cpp:268
souffle::ast::analysis::TypeAnalysis::analyseTypes
static std::map< const Argument *, TypeSet > analyseTypes(const TranslationUnit &tu, const Clause &clause, std::ostream *logs=nullptr)
Analyse the given clause and computes for each contained argument a set of potential types.
Definition: Type.cpp:131
souffle::AggregateOp
AggregateOp
Types of aggregation functions.
Definition: AggregateOp.h:34
souffle::ast::analysis::TypeAnalysis::functorInfo
std::map< const IntrinsicFunctor *, const IntrinsicFunctorInfo * > functorInfo
Definition: Type.h:102
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ast::analysis::TypeAnalysis::getTypes
TypeSet const & getTypes(const Argument *argument) const
Get the computed types for the given argument.
Definition: Type.h:56
souffle::ast::analysis::TypeAnalysis::run
void run(const TranslationUnit &translationUnit) override
run analysis for a Ast translation unit
Definition: Type.cpp:447
souffle::ast::analysis::TypeAnalysis::getValidIntrinsicFunctorOverloads
IntrinsicFunctors getValidIntrinsicFunctorOverloads(const IntrinsicFunctor &inf) const
– Functor-related methods –
Definition: Type.cpp:216
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::UserDefinedFunctor
User-Defined functor class.
Definition: UserDefinedFunctor.h:46
souffle::ast::analysis::TypeAnalysis::argumentTypes
std::map< const Argument *, TypeSet > argumentTypes
Definition: Type.h:93
souffle::ast::analysis::TypeAnalysis::hasValidTypeInfo
bool hasValidTypeInfo(const Argument *argument) const
Definition: Type.cpp:255
souffle::ast::analysis::TypeSet
A collection to represent sets of types.
Definition: TypeSystem.h:249
souffle::ast::analysis::TypeAnalysis::numericConstantType
std::map< const NumericConstant *, NumericConstant::Type > numericConstantType
Definition: Type.h:104
souffle::IntrinsicFunctors
std::vector< std::reference_wrapper< const IntrinsicFunctorInfo > > IntrinsicFunctors
Definition: FunctorOps.h:129
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
souffle::ast::analysis::TypeAnalysis::getFunctorReturnType
TypeAttribute getFunctorReturnType(const Functor *functor) const
Definition: Type.cpp:145
souffle::ast::analysis::TypeAnalysis::print
void print(std::ostream &os) const override
print the analysis result in HTML format
Definition: Type.cpp:136
souffle::ast::analysis::TypeAnalysis::analyseAggregators
bool analyseAggregators(const TranslationUnit &translationUnit)
Definition: Type.cpp:355
souffle::ast::analysis::TypeAnalysis::isFloat
bool isFloat(const Argument *argument) const
Definition: Type.cpp:435
souffle::ast::IntrinsicFunctor
Intrinsic Functor class for functors are in-built.
Definition: IntrinsicFunctor.h:47
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::ast::analysis::TypeAnalysis::createAnnotatedClause
static Own< Clause > createAnnotatedClause(const Clause *clause, const std::map< const Argument *, TypeSet > argumentTypes)
Definition: Type.cpp:68
souffle::ast::analysis::TypeAnalysis::analyseNumericConstants
bool analyseNumericConstants(const TranslationUnit &translationUnit)
Definition: Type.cpp:313
souffle::BinaryConstraintOp
BinaryConstraintOp
Binary Constraint Operators.
Definition: BinaryConstraintOps.h:41
souffle::ast::Functor
Abstract functor class.
Definition: Functor.h:36
souffle::ast::analysis::TypeAnalysis::getFunctorArgType
TypeAttribute getFunctorArgType(const Functor *functor, const size_t idx) const
Definition: Type.cpp:155
souffle::FunctorOp
FunctorOp
Definition: FunctorOps.h:35
souffle::ast::analysis::TypeAnalysis::annotatedClauses
VecOwn< Clause > annotatedClauses
Definition: Type.h:94
souffle::ast::analysis::Analysis
Abstract class for a AST Analysis.
Definition: Analysis.h:38
souffle::ast::analysis::TypeAnalysis::getTypeAttributes
std::set< TypeAttribute > getTypeAttributes(const Argument *arg) const
Definition: Type.cpp:189
souffle::ast::BinaryConstraint
Binary constraint class.
Definition: BinaryConstraint.h:53
souffle::ast::analysis::TypeAnalysis::aggregatorType
std::map< const Aggregator *, AggregateOp > aggregatorType
Definition: Type.h:105
souffle::ast::Aggregator
Defines the aggregator class.
Definition: Aggregator.h:53
souffle::ast::analysis::TypeAnalysis::constraintType
std::map< const BinaryConstraint *, BinaryConstraintOp > constraintType
Definition: Type.h:106
souffle::ast::analysis::TypeAnalysis::isUnsigned
bool isUnsigned(const Argument *argument) const
Definition: Type.cpp:439
souffle::ast::analysis::TypeAnalysis::getNumericConstantTypes
const std::map< const NumericConstant *, NumericConstant::Type > & getNumericConstantTypes() const
Definition: Type.cpp:174
souffle::ast::analysis::TypeAnalysis::isMultiResultFunctor
static bool isMultiResultFunctor(const Functor &functor)
Definition: Type.cpp:178
souffle::ast::analysis
Definition: Aggregate.cpp:39
souffle::ast::analysis::TypeAnalysis::getFunctorArgTypes
const std::vector< TypeAttribute > & getFunctorArgTypes(const UserDefinedFunctor &udf) const
Definition: Type.cpp:166
souffle::ast::analysis::TypeAnalysis::isSymbol
bool isSymbol(const Argument *argument) const
Definition: Type.cpp:443
souffle::ast::analysis::TypeAnalysis::udfDeclaration
std::map< std::string, const FunctorDeclaration * > udfDeclaration
Definition: Type.h:103
souffle::ast::analysis::TypeAnalysis
Definition: Type.h:45
FunctorOps.h
souffle::ast::analysis::TypeAnalysis::isStatefulFunctor
bool isStatefulFunctor(const UserDefinedFunctor *udf) const
Definition: Type.cpp:170
souffle::ast::NumericConstant::Type
Type
Definition: NumericConstant.h:51
souffle::ast::analysis::TypeAnalysis::TypeAnalysis
TypeAnalysis()
Definition: Type.h:49
souffle::ast
Definition: Aggregator.h:35
souffle::ast::NumericConstant
Numeric Constant.
Definition: NumericConstant.h:43
Analysis.h
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
NumericConstant.h
TypeSystem.h