souffle  2.0.2-371-g6315b36
ComponentLookup.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 ComponentLookupAnalysis.cpp
12  *
13  * Implements the component lookup
14  *
15  ***********************************************************************/
16 
18 #include "ast/Component.h"
19 #include "ast/ComponentType.h"
20 #include "ast/Program.h"
21 #include "ast/TranslationUnit.h"
22 #include "ast/utility/Visitor.h"
24 
25 namespace souffle::ast::analysis {
26 
27 void ComponentLookupAnalysis::run(const TranslationUnit& translationUnit) {
28  Program& program = translationUnit.getProgram();
29  for (Component* component : program.getComponents()) {
30  globalScopeComponents.insert(component);
31  enclosingComponent[component] = nullptr;
32  }
33  visitDepthFirst(program, [&](const Component& cur) {
34  nestedComponents[&cur];
35  for (Component* nestedComponent : cur.getComponents()) {
36  nestedComponents[&cur].insert(nestedComponent);
37  enclosingComponent[nestedComponent] = &cur;
38  }
39  });
40 }
41 
43  const Component* scope, const std::string& name, const TypeBinding& activeBinding) const {
44  // forward according to binding (we do not do this recursively on purpose)
45  QualifiedName boundName = activeBinding.find(name);
46  if (boundName.empty()) {
47  // compName is not bound to anything => just just compName
48  boundName = name;
49  }
50 
51  // search nested scopes bottom up
52  const Component* searchScope = scope;
53  while (searchScope != nullptr) {
54  for (const Component* cur : searchScope->getComponents()) {
55  if (cur->getComponentType()->getName() == toString(boundName)) {
56  return cur;
57  }
58  }
59  auto found = enclosingComponent.find(searchScope);
60  if (found != enclosingComponent.end()) {
61  searchScope = found->second;
62  } else {
63  searchScope = nullptr;
64  break;
65  }
66  }
67 
68  // check global scope
69  for (const Component* cur : globalScopeComponents) {
70  if (cur->getComponentType()->getName() == toString(boundName)) {
71  return cur;
72  }
73  }
74 
75  // no such component in scope
76  return nullptr;
77 }
78 
79 } // namespace souffle::ast::analysis
TranslationUnit.h
souffle::ast::analysis::ComponentLookupAnalysis::getComponent
const Component * getComponent(const Component *scope, const std::string &name, const TypeBinding &activeBinding) const
Performs a lookup operation for a component with the given name within the addressed scope.
Definition: ComponentLookup.cpp:48
Component.h
souffle::ast::analysis::ComponentLookupAnalysis::run
void run(const TranslationUnit &translationUnit) override
run analysis for a Ast translation unit
Definition: ComponentLookup.cpp:33
souffle::ast::analysis::ComponentLookupAnalysis::globalScopeComponents
std::set< const Component * > globalScopeComponents
Definition: ComponentLookup.h:98
souffle::toString
const std::string & toString(const std::string &str)
A generic function converting strings into strings (trivial case).
Definition: StringUtil.h:234
souffle::ast::QualifiedName::empty
bool empty() const
check for emptiness
Definition: QualifiedName.h:61
StringUtil.h
souffle::ast::analysis::ComponentLookupAnalysis::name
static constexpr const char * name
Definition: ComponentLookup.h:80
ComponentLookup.h
ComponentType.h
souffle::ast::analysis::TypeBinding
Class that encapsulates std::map of types binding that comes from .init c = Comp<MyType> Type binding...
Definition: ComponentLookup.h:37
souffle::ast::Component
Component class.
Definition: Component.h:58
souffle::ast::analysis::ComponentLookupAnalysis::nestedComponents
std::map< const Component *, std::set< const Component * > > nestedComponents
Definition: ComponentLookup.h:100
Program.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
souffle::ast::analysis::TypeBinding::find
const QualifiedName & find(const QualifiedName &name) const
Returns binding for given name or empty string if such binding does not exist.
Definition: ComponentLookup.h:46
souffle::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39
souffle::ast::Component::getComponents
std::vector< Component * > getComponents() const
Get components.
Definition: Component.h:131
souffle::ast::analysis::ComponentLookupAnalysis::enclosingComponent
std::map< const Component *, const Component * > enclosingComponent
Definition: ComponentLookup.h:102