KSeExpr 6.0.0.0
ExprEnv.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2// SPDX-License-Identifier: LicenseRef-Apache-2.0
3// SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6#include "ExprEnv.h"
7#include "ExprType.h"
8#include "Expression.h"
9
10namespace KSeExpr
11{
13{
14 _parent = parent;
15}
16
17ExprLocalVar *ExprVarEnv::find(const std::string &name)
18{
19 auto iter = _map.find(name);
20 if (iter != _map.end())
21 return iter->second.get();
22 else if (_parent)
23 return _parent->find(name);
24 else
25 return nullptr;
26}
27
29{
30 auto iter = _functions.find(name);
31 if (iter != _functions.end())
32 return iter->second;
33 else if (_parent)
34 return _parent->findFunction(name);
35 else
36 return nullptr;
37}
38
39ExprLocalVar const *ExprVarEnv::lookup(const std::string &name) const
40{
41 auto iter = _map.find(name);
42 if (iter != _map.end())
43 return iter->second.get();
44 else if (_parent)
45 return _parent->lookup(name);
46 return nullptr;
47}
48
49void ExprVarEnv::addFunction(const std::string &name, ExprLocalFunctionNode *prototype)
50{
51 // go to parent until we are at root (all functions globally declared)
52 if (_parent)
53 _parent->addFunction(name, prototype);
54 else {
55 auto iter = _functions.find(name);
56 if (iter != _functions.end())
57 iter->second = prototype;
58 else
59 _functions.insert(std::make_pair(name, prototype));
60 }
61}
62
63void ExprVarEnv::add(const std::string &name, std::unique_ptr<ExprLocalVar> var)
64{
65 auto iter = _map.find(name);
66 if (iter != _map.end()) {
67 // throw std::runtime_error("Invalid creation of existing variable in same scope!");
68 shadowedVariables.emplace_back(std::move(iter->second));
69 iter->second = std::move(var);
70 } else
71 _map.insert(std::make_pair(name, std::move(var)));
72}
73
75{
76 using MakeMap = std::map<std::pair<ExprLocalVar *, ExprLocalVar *>, std::string>;
79 for (auto &ienv : env1._map) {
80 const std::string &name = ienv.first;
81 ExprLocalVar *var = ienv.second.get();
82 if (ExprLocalVar *env2Var = env2.find(name)) {
83 phisToMake[std::make_pair(var, env2Var)] = name;
84 }
85 }
87 for (auto &ienv : env2._map) {
88 const std::string &name = ienv.first;
89 ExprLocalVar *var = ienv.second.get();
90 if (ExprLocalVar *env1Var = env1.find(name)) {
91 phisToMake[std::make_pair(env1Var, var)] = name;
92 }
93 }
94
95 std::vector<std::pair<std::string, ExprLocalVarPhi *>> mergedVariablesInThisCall;
96 for (auto& it : phisToMake) {
97 auto newVar = std::make_unique<ExprLocalVarPhi>(type, it.first.first, it.first.second);
98 mergedVariablesInThisCall.emplace_back(it.second, dynamic_cast<ExprLocalVarPhi *>(newVar.get()));
99 add(it.second, std::move(newVar));
100 }
101 _mergedVariables.emplace_back(std::move(mergedVariablesInThisCall));
102 return _mergedVariables.size() - 1;
103}
104} // namespace KSeExpr
Node that contains local function.
Definition ExprNode.h:351
ExprLocalVar join (merge) references. Remembers which variables are possible assigners to this.
Definition ExprEnv.h:84
ExprLocalVar reference, all local variables in seexpr are subclasses of this or this itself.
Definition ExprEnv.h:28
Variable scope for tracking variable lookup.
Definition ExprEnv.h:120
ExprLocalVar * find(const std::string &name)
Find a variable name by name (recursive to parents)
Definition ExprEnv.cpp:17
ExprLocalVar const * lookup(const std::string &name) const
Find a const variable reference name by name (recursive to parents)
Definition ExprEnv.cpp:39
FuncDictType _functions
Definition ExprEnv.h:125
ExprLocalFunctionNode * findFunction(const std::string &name)
Find a function by name (recursive to parents)
Definition ExprEnv.cpp:28
void addFunction(const std::string &name, ExprLocalFunctionNode *prototype)
Add a function.
Definition ExprEnv.cpp:49
VarDictType _map
Definition ExprEnv.h:123
std::vector< std::vector< std::pair< std::string, ExprLocalVarPhi * > > > _mergedVariables
Keep track of all merged variables in.
Definition ExprEnv.h:132
void add(const std::string &name, std::unique_ptr< ExprLocalVar > var)
Add a variable refernece.
Definition ExprEnv.cpp:63
std::vector< std::unique_ptr< ExprLocalVar > > shadowedVariables
Variables that have been superceded (and thus are inaccessible)
Definition ExprEnv.h:129
ExprVarEnv * _parent
Parent variable environment has all variablesf rom previou scope (for lookup)
Definition ExprEnv.h:135
void resetAndSetParent(ExprVarEnv *parent)
Resets the scope (deletes all variables) and sets parent.
Definition ExprEnv.cpp:12
size_t mergeBranches(const ExprType &type, ExprVarEnv &env1, ExprVarEnv &env2)
Add all variables into scope by name, but modify their lifetimes to the given type's lifetime.
Definition ExprEnv.cpp:74