torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Linker.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef TORC_GENERIC_EDIF_LINKER_HPP
17 #define TORC_GENERIC_EDIF_LINKER_HPP
18 
19 #include <algorithm>
20 #include <string>
21 #include <vector>
22 
23 //BOOST
24 #include <boost/shared_ptr.hpp>
25 
28 #include "torc/generic/Error.hpp"
29 #include "torc/generic/SymTab.hpp"
30 
31 namespace torc { namespace generic { class View; } }
32 namespace torc { namespace generic { class Instance; } }
33 namespace torc { namespace generic { class Root; } }
34 
35 
36 namespace torc {
37 namespace generic {
38 
39 /**
40  * @brief Represents a repository of unresolved usage references.
41  *
42  * The Linker class represents a repository of unresolved instances, that is instances that are yet
43  * to to be linked to concrete views. When an instance is created by the parser, it tries to find a
44  * concrete view using the name specification present in the EDIF file. If no such references were
45  * found, the instance is attached to a Extern view, that is a view that is only a place holder. As
46  * newer cells are discovered by the parser, the unresolved instances are linked to the concrete
47  * views and removed from the linker.
48  *
49  * @note This class is typically used by EdifParser.
50  */
51 class Linker {
52 public:
53  /**
54  * The name specification of a view consisting of a library name, cell name and a view name.
55  */
56  class NameSpec {
57  public:
58  NameSpec();
59 
60  NameSpec(const std::string& inLibraryName, const std::string& inCellName,
61  const std::string& inViewName);
62 
63  ~NameSpec() throw ();
64 
65  NameSpec(const NameSpec& inSource);
66 
67  NameSpec& operator=(const NameSpec& inSource);
68 
69  inline bool operator <(const NameSpec& inRhs) const;
70 
71  inline std::string getLibraryName() const;
72 
73  void setLibraryName(const std::string& inValue);
74 
75  inline std::string getCellName() const;
76 
77  void setCellName(const std::string& inValue);
78 
79  inline std::string getViewName() const;
80 
81  void setViewName(const std::string& inValue);
82 
83  private:
87 
88  };
89 
90  /**
91  * Represents a collection of instances that are connected to an extern.
92  */
94  public:
95  inline const ViewSharedPtr getExternView() const;
96 
97  void setExternView(const ViewSharedPtr& inValue);
98 
99  void addInstance(const InstanceSharedPtr& inInstance);
100 
101  inline const std::vector<InstanceSharedPtr>& getInstances() const;
102 
103  void setInstances(const std::vector<InstanceSharedPtr>& value);
104 
105  template <typename _Tp> void applyActionOnInstances(const _Tp& action) {
106  std::vector<InstanceSharedPtr>::iterator first = mInstances.begin();
107  std::vector<InstanceSharedPtr>::iterator last = mInstances.end();
108  for(; first != last; ++first) {
109  try {
110  action(*first);
111  } catch(Error& e) {
112  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
113  throw;
114  }
115  }
116  }
117 
119 
120  ~UnresolvedInstances() throw ();
121 
122  UnresolvedInstances(const UnresolvedInstances& inSource);
123 
124  UnresolvedInstances& operator=(const UnresolvedInstances& inSource);
125 
126  private:
129  };
130 
131  typedef boost::shared_ptr<UnresolvedInstances> UnresolvedInstancesPtr;
132 
133  typedef SymTab<NameSpec, UnresolvedInstancesPtr> UnresolvedInfoMap;
134 
135 public:
136  /**
137  * Find the extern View if already present.
138  *
139  * @param[in] inNameSpec Name of the view
140  *
141  * @return Extern view
142  */
143  ViewSharedPtr findExternView(const NameSpec& inNameSpec);
144 
145  /**
146  * Set an extern View
147  *
148  * @param[in] inNameSpec Name of the view
149  * @param[in] inExternView an Extern View
150  */
151  void setExternView(const NameSpec& inNameSpec, const ViewSharedPtr& inExternView);
152 
153  /**
154  * Find the list of instances that are waiting for a view with the specified name.
155  *
156  * @param[in] inNameSpec Name of the view to be linked with.
157  * @param[out] outInstances Pointer to unresolved object
158  *
159  * @return True if any such instances exist
160  */
161  bool findUnresolvedInstances(const NameSpec& inNameSpec, UnresolvedInstancesPtr& outInstances)
162  throw (Error);
163 
164  /**
165  * Add an instance to the list of unresolved instances for a given name specification. If a new
166  * UnresolvedInstances object is to be created, this method will also call the setExternView()
167  * method to update the externView, with the view set for this object.
168  *
169  * @param[in] inNameSpec name specification for a view.
170  * @param[in] inInstance instance to be added.
171  *
172  * @exception Error The master for this instance is not an extern or no extern is set for this
173  * view.
174  */
175  void registerUnresolvedInstance(const NameSpec& inNameSpec, const InstanceSharedPtr& inInstance)
176  throw (Error);
177 
178  /**
179  * Remove all unresolved objects for the given name specification.
180  *
181  * @param[in] inNameSpec Name specification
182  */
183  void removeUnresolvedInstances(const NameSpec& inNameSpec) throw (Error);
184 
185  /**
186  * Resolves all unlinked references for the given namespec to the given view
187  * @param[in] inNameSpec Name specification for unresolved view
188  * @param[in] inView View to be bound to
189  */
190 
191  void linkUnresolved(const NameSpec& inNameSpec, ViewSharedPtr& inView) throw (Error);
192 
193  /**
194  * Constructor
195  *
196  * @param[in] root Root of the OM.
197  */
198  Linker(const RootSharedPtr& root);
199 
200  ~Linker() throw ();
201 
202  Linker(const Linker& source);
203 
204  Linker& operator=(const Linker& inSource);
205 
206 private:
208  UnresolvedInfoMap mInfos;
209 };
210 
211 inline bool Linker::NameSpec::operator <(const Linker::NameSpec& inRhs) const {
212  return mLibraryName < inRhs.mLibraryName
213  || (mLibraryName == inRhs.mLibraryName && mCellName < inRhs.mCellName)
214  || (mLibraryName == inRhs.mLibraryName && mCellName == inRhs.mCellName
215  && mViewName < inRhs.mViewName);
216 }
217 
219  return mLibraryName;
220 }
221 
223  return mCellName;
224 }
225 
227  return mViewName;
228 }
229 
231  return mExternView;
232 }
233 
234 inline const std::vector<InstanceSharedPtr>& Linker::UnresolvedInstances::getInstances() const {
235  return mInstances;
236 }
237 
238 } // namespace generic
239 } // namespace torc
240 
241 #endif // TORC_GENERIC_EDIF_LINKER_HPP
std::string getLibraryName() const
Definition: Linker.hpp:218
boost::shared_ptr< Instance > InstanceSharedPtr
bool findUnresolvedInstances(const NameSpec &inNameSpec, UnresolvedInstancesPtr &outInstances)
Definition: Linker.cpp:190
NameSpec & operator=(const NameSpec &inSource)
Definition: Linker.cpp:73
std::string getViewName() const
Definition: Linker.hpp:226
std::string string
void linkUnresolved(const NameSpec &inNameSpec, ViewSharedPtr &inView)
Definition: Linker.cpp:231
RootSharedPtr mRoot
Definition: Linker.hpp:207
void applyActionOnInstances(const _Tp &action)
Definition: Linker.hpp:105
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
void setExternView(const ViewSharedPtr &inValue)
Definition: Linker.cpp:101
void addInstance(const InstanceSharedPtr &inInstance)
Definition: Linker.cpp:107
const ViewSharedPtr getExternView() const
Definition: Linker.hpp:230
Represents a repository of unresolved usage references.
Definition: Linker.hpp:51
bool operator<(const NameSpec &inRhs) const
Definition: Linker.hpp:211
void setCellName(const std::string &inValue)
Definition: Linker.cpp:91
boost::shared_ptr< View > ViewSharedPtr
ViewSharedPtr findExternView(const NameSpec &inNameSpec)
Definition: Linker.cpp:149
void setLibraryName(const std::string &inValue)
Definition: Linker.cpp:85
void removeUnresolvedInstances(const NameSpec &inNameSpec)
Definition: Linker.cpp:225
A symbol table.
Definition: SymTab.hpp:38
std::vector< InstanceSharedPtr > mInstances
Definition: Linker.hpp:128
void setViewName(const std::string &inValue)
Definition: Linker.cpp:96
boost::shared_ptr< UnresolvedInstances > UnresolvedInstancesPtr
Definition: Linker.hpp:131
void registerUnresolvedInstance(const NameSpec &inNameSpec, const InstanceSharedPtr &inInstance)
Definition: Linker.cpp:205
const std::vector< InstanceSharedPtr > & getInstances() const
Definition: Linker.hpp:234
std::string getCellName() const
Definition: Linker.hpp:222
UnresolvedInfoMap mInfos
Definition: Linker.hpp:208
void setInstances(const std::vector< InstanceSharedPtr > &value)
Definition: Linker.cpp:113
boost::shared_ptr< Root > RootSharedPtr
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73