SeExpr
ExprEnv.cpp
Go to the documentation of this file.
1/*
2* Copyright Disney Enterprises, Inc. All rights reserved.
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License
6* and the following modification to it: Section 6 Trademarks.
7* deleted and replaced with:
8*
9* 6. Trademarks. This License does not grant permission to use the
10* trade names, trademarks, service marks, or product names of the
11* Licensor and its affiliates, except as required for reproducing
12* the content of the NOTICE file.
13*
14* You may obtain a copy of the License at
15* http://www.apache.org/licenses/LICENSE-2.0
16*/
17
18#include "ExprType.h"
19#include "ExprEnv.h"
20#include "Expression.h"
21#include <vector>
22
23namespace SeExpr2 {
24
26
28
29ExprLocalVar* ExprVarEnv::find(const std::string& name) {
30 VarDictType::iterator iter = _map.find(name);
31 if (iter != _map.end())
32 return iter->second.get();
33 else if (_parent)
34 return _parent->find(name);
35 else
36 return 0;
37}
38
40 FuncDictType::iterator iter = _functions.find(name);
41 if (iter != _functions.end())
42 return iter->second;
43 else if (_parent)
44 return _parent->findFunction(name);
45 else
46 return 0;
47}
48
49ExprLocalVar const* ExprVarEnv::lookup(const std::string& name) const {
50 VarDictType::const_iterator iter = _map.find(name);
51 if (iter != _map.end())
52 return iter->second.get();
53 else if (_parent)
54 return _parent->lookup(name);
55 return 0;
56}
57
58void ExprVarEnv::addFunction(const std::string& name, ExprLocalFunctionNode* prototype) {
59 // go to parent until we are at root (all functions globally declared)
60 if (_parent)
61 _parent->addFunction(name, prototype);
62 else {
63 FuncDictType::iterator iter = _functions.find(name);
64 if (iter != _functions.end())
65 iter->second = prototype;
66 else
67 _functions.insert(std::make_pair(name, prototype));
68 }
69}
70
71void ExprVarEnv::add(const std::string& name, std::unique_ptr<ExprLocalVar> var) {
72 VarDictType::iterator iter = _map.find(name);
73 if (iter != _map.end()) {
74 // throw std::runtime_error("Invalid creation of existing variable in same scope!");
75 shadowedVariables.emplace_back(std::move(iter->second));
76 iter->second = std::move(var);
77 } else
78 _map.insert(std::make_pair(name, std::move(var)));
79}
80
81size_t ExprVarEnv::mergeBranches(const ExprType& type, ExprVarEnv& env1, ExprVarEnv& env2) {
82 typedef std::map<std::pair<ExprLocalVar*, ExprLocalVar*>, std::string> MakeMap;
83 MakeMap phisToMake;
85 for (auto& ienv : env1._map) {
86 const std::string& name = ienv.first;
87 ExprLocalVar* var = ienv.second.get();
88 if (ExprLocalVar* env2Var = env2.find(name)) {
89 phisToMake[std::make_pair(var, env2Var)] = name;
90 }
91 }
93 for (auto& ienv : env2._map) {
94 const std::string& name = ienv.first;
95 ExprLocalVar* var = ienv.second.get();
96 if (ExprLocalVar* env1Var = env1.find(name)) {
97 phisToMake[std::make_pair(env1Var, var)] = name;
98 }
99 }
100
101 std::vector<std::pair<std::string, ExprLocalVarPhi*>> mergedVariablesInThisCall;
102 for (MakeMap::iterator it = phisToMake.begin(); it != phisToMake.end(); ++it) {
103 std::unique_ptr<ExprLocalVar> newVar(new ExprLocalVarPhi(type, it->first.first, it->first.second));
104 mergedVariablesInThisCall.emplace_back(it->second, static_cast<ExprLocalVarPhi*>(newVar.get()));
105 add(it->second, std::move(newVar));
106 }
107 _mergedVariables.emplace_back(std::move(mergedVariablesInThisCall));
108 return _mergedVariables.size() - 1;
109}
110}
Node that contains local function.
Definition ExprNode.h:307
ExprLocalVar join (merge) references. Remembers which variables are possible assigners to this.
Definition ExprEnv.h:67
ExprLocalVar reference, all local variables in seexpr are subclasses of this or this itself.
Definition ExprEnv.h:37
Variable scope for tracking variable lookup.
Definition ExprEnv.h:94
void add(const std::string &name, std::unique_ptr< ExprLocalVar > var)
Add a variable refernece.
Definition ExprEnv.cpp:71
std::vector< std::vector< std::pair< std::string, ExprLocalVarPhi * > > > _mergedVariables
Keep track of all merged variables in.
Definition ExprEnv.h:106
VarDictType _map
Definition ExprEnv.h:97
ExprLocalVar * find(const std::string &name)
Find a variable name by name (recursive to parents)
Definition ExprEnv.cpp:29
ExprVarEnv * _parent
Parent variable environment has all variablesf rom previou scope (for lookup)
Definition ExprEnv.h:109
FuncDictType _functions
Definition ExprEnv.h:99
std::vector< std::unique_ptr< ExprLocalVar > > shadowedVariables
Variables that have been superceded (and thus are inaccessible)
Definition ExprEnv.h:103
ExprLocalVar const * lookup(const std::string &name) const
Find a const variable reference name by name (recursive to parents)
Definition ExprEnv.cpp:49
void resetAndSetParent(ExprVarEnv *parent)
Resets the scope (deletes all variables) and sets parent.
Definition ExprEnv.cpp:27
void addFunction(const std::string &name, ExprLocalFunctionNode *prototype)
Add a function.
Definition ExprEnv.cpp:58
ExprLocalFunctionNode * findFunction(const std::string &name)
Find a function by name (recursive to parents)
Definition ExprEnv.cpp:39
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:81
you may not use this file except in compliance with the License and the following modification to it
Definition license.txt:10
</b ></td >< td > add
Definition userdoc.txt:504