souffle  2.0.2-371-g6315b36
SumTypeBranches.cpp
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 SumTypeBranches.cpp
12  *
13  * Calculate branch to types mapping.
14  *
15  ***********************************************************************/
16 
18 #include "ast/AlgebraicDataType.h"
19 #include "ast/BranchDeclaration.h"
20 #include "ast/Program.h"
21 #include "ast/TranslationUnit.h"
22 #include "ast/Type.h"
25 #include "ast/utility/Visitor.h"
26 #include <vector>
27 
28 namespace souffle::ast::analysis {
29 
30 void SumTypeBranchesAnalysis::run(const TranslationUnit& tu) {
31  const TypeEnvironment& env = tu.getAnalysis<TypeEnvironmentAnalysis>()->getTypeEnvironment();
32 
33  Program& program = tu.getProgram();
34  visitDepthFirst(program.getTypes(), [&](const ast::AlgebraicDataType& adt) {
35  auto typeName = adt.getQualifiedName();
36  if (!env.isType(typeName)) return;
37 
38  for (auto& branch : adt.getBranches()) {
39  branchToType[branch->getConstructor()] = &env.getType(typeName);
40  }
41  });
42 }
43 
44 } // namespace souffle::ast::analysis
TranslationUnit.h
souffle::ast::analysis::SumTypeBranchesAnalysis::run
void run(const TranslationUnit &translationUnit) override
run analysis for a Ast translation unit
Definition: SumTypeBranches.cpp:36
Type.h
Program.h
SumTypeBranches.h
Visitor.h
souffle::ast::analysis
Definition: Aggregate.cpp:39
souffle::ast::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the ast rooted by the given node recursively in a depth-...
Definition: Visitor.h:273
AlgebraicDataType.h
TypeEnvironment.h
BranchDeclaration.h
TypeSystem.h