souffle  2.0.2-371-g6315b36
GroundedTermsChecker.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 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 GroundedTermsChecker.cpp
12  *
13  * Implementation of the grounded terms checker pass.
14  *
15  ***********************************************************************/
16 
18 #include "ast/BranchInit.h"
19 #include "ast/Clause.h"
20 #include "ast/Program.h"
21 #include "ast/RecordInit.h"
22 #include "ast/TranslationUnit.h"
23 #include "ast/Variable.h"
24 #include "ast/analysis/Ground.h"
25 #include "ast/utility/Utils.h"
26 #include "ast/utility/Visitor.h"
27 #include "reports/ErrorReport.h"
28 #include <map>
29 #include <set>
30 #include <utility>
31 #include <vector>
32 
33 namespace souffle::ast::transform {
34 
35 void GroundedTermsChecker::verify(TranslationUnit& translationUnit) {
36  auto&& program = translationUnit.getProgram();
37  auto&& report = translationUnit.getErrorReport();
38 
39  // -- check grounded variables and records --
40  visitDepthFirst(program.getClauses(), [&](const Clause& clause) {
41  if (isFact(clause)) return; // only interested in rules
42 
43  auto isGrounded = analysis::getGroundedTerms(translationUnit, clause);
44 
45  std::set<std::string> reportedVars;
46  // all terms in head need to be grounded
47  for (auto&& cur : getVariables(clause)) {
48  if (!isGrounded[cur] && reportedVars.insert(cur->getName()).second) {
49  report.addError("Ungrounded variable " + cur->getName(), cur->getSrcLoc());
50  }
51  }
52 
53  // all records need to be grounded
54  visitDepthFirst(clause, [&](const RecordInit& record) {
55  if (!isGrounded[&record]) {
56  report.addError("Ungrounded record", record.getSrcLoc());
57  }
58  });
59 
60  // All sums need to be grounded
61  visitDepthFirst(clause, [&](const BranchInit& adt) {
62  if (!isGrounded[&adt]) {
63  report.addError("Ungrounded ADT branch", adt.getSrcLoc());
64  }
65  });
66  });
67 }
68 
69 } // namespace souffle::ast::transform
TranslationUnit.h
GroundedTermsChecker.h
Utils.h
Ground.h
souffle::ast::transform
Definition: Program.h:45
BranchInit.h
souffle::ast::transform::GroundedTermsChecker::verify
void verify(TranslationUnit &translationUnit)
Definition: GroundedTermsChecker.cpp:41
Program.h
Visitor.h
Clause.h
souffle::ast::BranchInit
Initialization of ADT instance.
Definition: BranchInit.h:49
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
ErrorReport.h
RecordInit.h
souffle::ast::RecordInit
Defines a record initialization class.
Definition: RecordInit.h:42
Variable.h