souffle  2.0.2-371-g6315b36
ComponentLookup.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 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 ComponentLookup.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ast/Component.h"
18 #include "ast/QualifiedName.h"
19 #include "ast/TranslationUnit.h"
20 #include "ast/analysis/Analysis.h"
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 namespace souffle::ast::analysis {
28 
29 /**
30  * Class that encapsulates std::map of types binding that comes from .init c = Comp<MyType>
31  * Type binding in this example would be T->MyType if the component code is .comp Comp<T> ...
32  */
33 class TypeBinding {
34 public:
35  /**
36  * Returns binding for given name or empty string if such binding does not exist.
37  */
38  const QualifiedName& find(const QualifiedName& name) const {
39  const static QualifiedName unknown;
40  auto pos = binding.find(name);
41  if (pos == binding.end()) {
42  return unknown;
43  }
44  return pos->second;
45  }
46 
47  TypeBinding extend(const std::vector<QualifiedName>& formalParams,
48  const std::vector<QualifiedName>& actualParams) const {
49  TypeBinding result;
50  if (formalParams.size() != actualParams.size()) {
51  return *this; // invalid init => will trigger a semantic error
52  }
53 
54  for (std::size_t i = 0; i < formalParams.size(); i++) {
55  auto pos = binding.find(actualParams[i]);
56  if (pos != binding.end()) {
57  result.binding[formalParams[i]] = pos->second;
58  } else {
59  result.binding[formalParams[i]] = actualParams[i];
60  }
61  }
62 
63  return result;
64  }
65 
66 private:
67  /**
68  * Key value pair. Keys are names that should be forwarded to value,
69  * which is the actual name. Example T->MyImplementation.
70  */
71  std::map<QualifiedName, QualifiedName> binding;
72 };
73 
74 class ComponentLookupAnalysis : public Analysis {
75 public:
76  static constexpr const char* name = "component-lookup";
77 
79 
80  void run(const TranslationUnit& translationUnit) override;
81 
82  /**
83  * Performs a lookup operation for a component with the given name within the addressed scope.
84  *
85  * @param scope the component scope to lookup in (null for global scope)
86  * @param name the name of the component to be looking for
87  * @return a pointer to the obtained component or null if there is no such component.
88  */
89  const Component* getComponent(
90  const Component* scope, const std::string& name, const TypeBinding& activeBinding) const;
91 
92 private:
93  // components defined outside of any components
94  std::set<const Component*> globalScopeComponents;
95  // components defined inside a component
96  std::map<const Component*, std::set<const Component*>> nestedComponents;
97  // component definition enclosing a component definition
98  std::map<const Component*, const Component*> enclosingComponent;
99 };
100 
101 } // 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::TypeBinding::extend
TypeBinding extend(const std::vector< QualifiedName > &formalParams, const std::vector< QualifiedName > &actualParams) const
Definition: ComponentLookup.h:55
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
i
size_t i
Definition: json11.h:663
souffle::ast::analysis::TypeBinding::binding
std::map< QualifiedName, QualifiedName > binding
Key value pair.
Definition: ComponentLookup.h:79
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::ast::analysis::ComponentLookupAnalysis::name
static constexpr const char * name
Definition: ComponentLookup.h:80
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
souffle::ast::analysis::Analysis
Abstract class for a AST Analysis.
Definition: Analysis.h:38
QualifiedName.h
souffle::ast::analysis
Definition: Aggregate.cpp:39
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::analysis::ComponentLookupAnalysis::ComponentLookupAnalysis
ComponentLookupAnalysis()
Definition: ComponentLookup.h:82
souffle::ast::analysis::ComponentLookupAnalysis::enclosingComponent
std::map< const Component *, const Component * > enclosingComponent
Definition: ComponentLookup.h:102
Analysis.h