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
25 ExprVarEnv::~ExprVarEnv() { resetAndSetParent(0); }
26
27 void ExprVarEnv::resetAndSetParent(ExprVarEnv* parent) { _parent = parent; }
28
29 ExprLocalVar* 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
39 ExprLocalFunctionNode* ExprVarEnv::findFunction(const std::string& name) {
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
49 ExprLocalVar 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
58 void 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
71 void 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
81 size_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 }
SeExpr2::ExprVarEnv::find
ExprLocalVar * find(const std::string &name)
Find a variable name by name (recursive to parents)
Definition: ExprEnv.cpp:29
SeExpr2::ExprVarEnv::findFunction
ExprLocalFunctionNode * findFunction(const std::string &name)
Find a function by name (recursive to parents)
Definition: ExprEnv.cpp:39
SeExpr2::ExprVarEnv::shadowedVariables
std::vector< std::unique_ptr< ExprLocalVar > > shadowedVariables
Variables that have been superceded (and thus are inaccessible)
Definition: ExprEnv.h:103
SeExpr2::ExprLocalVarPhi
ExprLocalVar join (merge) references. Remembers which variables are possible assigners to this.
Definition: ExprEnv.h:67
SeExpr2::ExprVarEnv::lookup
ExprLocalVar const * lookup(const std::string &name) const
Find a const variable reference name by name (recursive to parents)
Definition: ExprEnv.cpp:49
SeExpr2::ExprType
Definition: ExprType.h:39
SeExpr2::ExprVarEnv
Variable scope for tracking variable lookup.
Definition: ExprEnv.h:94
SeExpr2::ExprVarEnv::resetAndSetParent
void resetAndSetParent(ExprVarEnv *parent)
Resets the scope (deletes all variables) and sets parent.
Definition: ExprEnv.cpp:27
if
if(EXISTS "/usr/share/apps/cmake/modules") list(APPEND CMAKE_MODULE_PATH "/usr/share/apps/cmake/modules") endif() find_package(OpenGL) if(Qt5_FOUND OR QT4_FOUND) BuildParserScanner(ExprSpecParserLex ExprSpecParser ExprSpec editor_parser_cpp) set(EDITOR_MOC_HDRS ExprBrowser.h ExprColorCurve.h ExprColorSwatch.h ExprControlCollection.h ExprControl.h ExprCurve.h ExprDialog.h ExprEditor.h ExprFileDialog.h ExprGrapher2d.h ExprPopupDoc.h ExprShortEdit.h ExprDeepWater.h) set(EDITOR_CPPS ExprFileDialog.cpp ExprControl.cpp ExprEditor.cpp ExprMain.cpp ExprShortEdit.cpp ExprCurve.cpp ExprColorCurve.cpp ExprColorSwatch.cpp EditableExpression.cpp ExprPopupDoc.cpp ExprCompletionModel.cpp ExprDialog.cpp ExprControlCollection.cpp ExprGrapher2d.cpp ExprBrowser.cpp BasicExpression.cpp ExprDeepWater.cpp) if(ENABLE_QT5) qt5_wrap_cpp(EDITOR_MOC_SRCS $
Definition: CMakeLists.txt:16
SeExpr2
Definition: Context.h:22
SeExpr2::ExprVarEnv::_mergedVariables
std::vector< std::vector< std::pair< std::string, ExprLocalVarPhi * > > > _mergedVariables
Keep track of all merged variables in.
Definition: ExprEnv.h:106
ExprType.h
SeExpr2::ExprVarEnv::_map
VarDictType _map
Definition: ExprEnv.h:97
SeExpr2::ExprVarEnv::_functions
FuncDictType _functions
Definition: ExprEnv.h:99
it
you may not use this file except in compliance with the License and the following modification to it
Definition: license.txt:10
SeExpr2::ExprVarEnv::_parent
ExprVarEnv * _parent
Parent variable environment has all variablesf rom previou scope (for lookup)
Definition: ExprEnv.h:109
SeExpr2::ExprVarEnv::addFunction
void addFunction(const std::string &name, ExprLocalFunctionNode *prototype)
Add a function.
Definition: ExprEnv.cpp:58
SeExpr2::ExprVarEnv::add
void add(const std::string &name, std::unique_ptr< ExprLocalVar > var)
Add a variable refernece.
Definition: ExprEnv.cpp:71
ExprEnv.h
SeExpr2::ExprVarEnv::mergeBranches
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
SeExpr2::ExprLocalFunctionNode
Node that contains local function.
Definition: ExprNode.h:307
SeExpr2::ExprVarEnv::~ExprVarEnv
~ExprVarEnv()
Definition: ExprEnv.cpp:25
Expression.h
SeExpr2::ExprLocalVar
ExprLocalVar reference, all local variables in seexpr are subclasses of this or this itself.
Definition: ExprEnv.h:37