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 
23 namespace SeExpr2 {
24 
26 
28  _parent = parent;
29 }
30 
31 ExprLocalVar* ExprVarEnv::find(const std::string& name) {
32  VarDictType::iterator iter = _map.find(name);
33  if (iter != _map.end())
34  return iter->second.get();
35  else if (_parent)
36  return _parent->find(name);
37  else
38  return 0;
39 }
40 
42  FuncDictType::iterator iter = _functions.find(name);
43  if (iter != _functions.end())
44  return iter->second;
45  else if (_parent)
46  return _parent->findFunction(name);
47  else
48  return 0;
49 }
50 
51 ExprLocalVar const* ExprVarEnv::lookup(const std::string& name) const {
52  VarDictType::const_iterator iter = _map.find(name);
53  if (iter != _map.end())
54  return iter->second.get();
55  else if (_parent)
56  return _parent->lookup(name);
57  return 0;
58 }
59 
60 void ExprVarEnv::addFunction(const std::string& name, ExprLocalFunctionNode* prototype) {
61  // go to parent until we are at root (all functions globally declared)
62  if (_parent)
63  _parent->addFunction(name, prototype);
64  else {
65  FuncDictType::iterator iter = _functions.find(name);
66  if (iter != _functions.end())
67  iter->second = prototype;
68  else
69  _functions.insert(std::make_pair(name, prototype));
70  }
71 }
72 
73 void ExprVarEnv::add(const std::string& name, std::unique_ptr<ExprLocalVar> var) {
74  VarDictType::iterator iter = _map.find(name);
75  if (iter != _map.end()){
76  //throw std::runtime_error("Invalid creation of existing variable in same scope!");
77  shadowedVariables.emplace_back(std::move(iter->second));
78  iter->second=std::move(var);
79  }else
80  _map.insert(std::make_pair(name, std::move(var)));
81 }
82 
83 size_t ExprVarEnv::mergeBranches(const ExprType& type, ExprVarEnv& env1, ExprVarEnv& env2) {
84  typedef std::map<std::pair<ExprLocalVar*, ExprLocalVar*>, std::string> MakeMap;
85  MakeMap phisToMake;
87  for (auto& ienv: env1._map){
88  const std::string& name = ienv.first;
89  ExprLocalVar* var = ienv.second.get();
90  if(ExprLocalVar* env2Var = env2.find(name)){
91  phisToMake[std::make_pair(var, env2Var)] = name;
92  }
93  }
95  for (auto& ienv: env2._map){
96  const std::string& name = ienv.first;
97  ExprLocalVar* var = ienv.second.get();
98  if (ExprLocalVar* env1Var = env1.find(name)) {
99  phisToMake[std::make_pair(env1Var, var)] = name;
100  }
101  }
102 
103  std::vector<std::pair<std::string,ExprLocalVarPhi*>> mergedVariablesInThisCall;
104  for (MakeMap::iterator it = phisToMake.begin(); it != phisToMake.end(); ++it) {
105  std::unique_ptr<ExprLocalVar> newVar(new ExprLocalVarPhi(type, it->first.first, it->first.second));
106  mergedVariablesInThisCall.emplace_back(it->second,static_cast<ExprLocalVarPhi*>(newVar.get()));
107  add(it->second, std::move(newVar));
108  }
109  _mergedVariables.emplace_back(std::move(mergedVariablesInThisCall));
110  return _mergedVariables.size()-1;
111 }
112 }
FuncDictType _functions
Definition: ExprEnv.h:102
std::vector< std::vector< std::pair< std::string, ExprLocalVarPhi * > > > _mergedVariables
Keep track of all merged variables in.
Definition: ExprEnv.h:109
VarDictType _map
Definition: ExprEnv.h:100
Node that contains local function.
Definition: ExprNode.h:307
std::vector< std::unique_ptr< ExprLocalVar > > shadowedVariables
Variables that have been superceded (and thus are inaccessible)
Definition: ExprEnv.h:106
void add(const std::string &name, std::unique_ptr< ExprLocalVar > var)
Add a variable refernece.
Definition: ExprEnv.cpp:73
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&#39;s lifetime...
Definition: ExprEnv.cpp:83
ExprLocalVar reference, all local variables in seexpr are subclasses of this or this itself...
Definition: ExprEnv.h:38
Variable scope for tracking variable lookup.
Definition: ExprEnv.h:97
ExprLocalVar * find(const std::string &name)
Find a variable name by name (recursive to parents)
Definition: ExprEnv.cpp:31
ExprLocalFunctionNode * findFunction(const std::string &name)
Find a function by name (recursive to parents)
Definition: ExprEnv.cpp:41
ExprLocalVar const * lookup(const std::string &name) const
Find a const variable reference name by name (recursive to parents)
Definition: ExprEnv.cpp:51
ExprVarEnv * _parent
Parent variable environment has all variablesf rom previou scope (for lookup)
Definition: ExprEnv.h:112
void resetAndSetParent(ExprVarEnv *parent)
Resets the scope (deletes all variables) and sets parent.
Definition: ExprEnv.cpp:27
ExprLocalVar join (merge) references. Remembers which variables are possible assigners to this...
Definition: ExprEnv.h:68
you may not use this file except in compliance with the License and the following modification to it
Definition: license.txt:10
if((BISON_EXE STREQUAL"BISON_EXE-NOTFOUND") OR(FLEX_EXE STREQUAL"FLEX_EXE-NOTFOUND") OR(SED_EXE STREQUAL"SED_EXE-NOTFOUND")) else((BISON_EXE STREQUAL"BISON_EXE-NOTFOUND") OR(FLEX_EXE STREQUAL"FLEX_EXE-NOTFOUND") OR(SED_EXE STREQUAL"SED_EXE-NOTFOUND")) ADD_CUSTOM_COMMAND(SOURCE"ExprParserLex.l"COMMAND"flex"ARGS"-oExprParserLexIn.cpp""$
Definition: CMakeLists.txt:33
void addFunction(const std::string &name, ExprLocalFunctionNode *prototype)
Add a function.
Definition: ExprEnv.cpp:60