Vuo  2.3.2
VuoCompilerModule.cc
Go to the documentation of this file.
1 
11 #include "VuoCompilerNodeClass.hh"
12 #include "VuoCompilerType.hh"
13 #include "VuoJsonUtilities.hh"
14 #include "VuoNodeClass.hh"
15 #include "VuoStringUtilities.hh"
16 
22  : compatibleTargets(nullptr)
23 {
24 #if VUO_PRO
25  init_Pro();
26 #endif
27  this->base = base;
28  this->module = module;
29  this->moduleDetails = NULL;
30  this->parser = NULL;
31  this->builtIn = false;
32 
33  if (module)
34  {
35  module->setModuleIdentifier(getPseudoBase()->getModuleKey());
36  parse();
37  }
38 }
39 
44 {
45 #if VUO_PRO
46  fini_Pro();
47 #endif
48  delete parser;
49  json_object_put(moduleDetails);
50 }
51 
63 VuoCompilerModule * VuoCompilerModule::newModule(const string &moduleKey, Module *module, const string &modulePath,
64  const VuoCompilerCompatibility &moduleCompatibility)
65 {
66  VuoCompilerModule *m = NULL;
67 
68  if (isModule(module, moduleKey))
69  {
71  if (nodeClass)
72  m = nodeClass->getCompiler();
73  else
74  {
76  if (type)
77  m = type;
78  else
79  m = new VuoCompilerModule(new VuoModule(moduleKey), module);
80  }
81 
82  m->modulePath = modulePath;
83  m->compatibleTargets = m->compatibleTargets.intersection(moduleCompatibility);
84  }
85  else if (moduleKey != "libmodule")
86  VUserLog("Warning: No VuoModuleMetadata found in \"%s\" so I'm not going to load it.", moduleKey.c_str());
87 
88  return m;
89 }
90 
95 bool VuoCompilerModule::isModule(Module *module, string moduleKey)
96 {
97  return hasOriginalOrMangledGlobal("moduleDetails", module, moduleKey);
98 }
99 
105 {
108  parseMetadata();
109 }
110 
115 {
116  string moduleDetailsStr = parser->getGlobalString( nameForGlobal("moduleDetails") );
117  json_object_put(moduleDetails);
118  moduleDetails = json_tokener_parse(moduleDetailsStr.c_str());
119 
120  if (! moduleDetails)
121  {
122  VUserLog("Error: Couldn't parse VuoModuleMetadata as JSON: %s", moduleDetailsStr.c_str());
123  return;
124  }
125 
130  vector<string> dependenciesVector = VuoJsonUtilities::parseArrayOfStrings(moduleDetails, "dependencies");
131  dependencies = set<string>(dependenciesVector.begin(), dependenciesVector.end());
133 }
134 
141 {
142  json_object *compatibilityObject = nullptr;
143  if (json_object_object_get_ex(o, key.c_str(), &compatibilityObject))
144  return VuoCompilerCompatibility(compatibilityObject);
145 
147 }
148 
154 {
155  set<string> globals;
156  globals.insert("moduleDetails");
157  return globals;
158 }
159 
163 string VuoCompilerModule::nameForGlobal(string genericGlobalVarOrFuncName)
164 {
165  return nameForGlobal(genericGlobalVarOrFuncName, base->getModuleKey());
166 }
167 
171 string VuoCompilerModule::nameForGlobal(string nameBeforeCompilation, string moduleKey)
172 {
173  return VuoStringUtilities::prefixSymbolName(nameBeforeCompilation, moduleKey);
174 }
175 
180 bool VuoCompilerModule::hasOriginalOrMangledGlobal(string nameBeforeCompilation, Module *module, string moduleKey)
181 {
182  string nameAfterCompilation = nameForGlobal(nameBeforeCompilation, moduleKey);
183  return (module->getNamedValue(nameBeforeCompilation) != NULL || module->getNamedValue(nameAfterCompilation) != NULL);
184 }
185 
191 {
192  set<string> globalsAndFuncsToRename = globalsToRename();
193 
194  Module::GlobalListType& globals = module->getGlobalList();
195 
196  // Iterate through global variables
197  for (Module::GlobalListType::iterator i = globals.begin(), e = globals.end(); i != e; ++i) {
198  string curGlobalVarName = i->getName();
199  // If current global variable is in the list of those to rename, do so
200  if (globalsAndFuncsToRename.find(curGlobalVarName) != globalsAndFuncsToRename.end()) {
201  string newGlobalVarName = nameForGlobal(curGlobalVarName);
202  i->setName(newGlobalVarName);
203  }
204 
205  }
206 
207  Module::FunctionListType& functions = module->getFunctionList();
208 
209  // Iterate through functions
210  for (Module::FunctionListType::iterator i = functions.begin(), e = functions.end(); i != e; ++i) {
211  string curFuncName = i->getName();
212  // If current function is in the list of those to rename, do so
213  if (globalsAndFuncsToRename.find(curFuncName) != globalsAndFuncsToRename.end()) {
214  string newFuncName = nameForGlobal(curFuncName);
215  i->setName(newFuncName);
216  }
217 
218  }
219 }
220 
225 Function * VuoCompilerModule::declareFunctionInModule(Module *module, Function *functionSrc)
226 {
227  Function *functionDst = module->getFunction(functionSrc->getName());
228  if (! functionDst)
229  {
230  functionDst = Function::Create(functionSrc->getFunctionType(),
231  GlobalValue::ExternalLinkage,
232  functionSrc->getName(),
233  module);
234  functionDst->setAttributes( functionSrc->getAttributes() );
235  }
236  return functionDst;
237 }
238 
243 {
244  return dependencies;
245 }
246 
252 {
253  return base->getModuleKey();
254 }
255 
262 {
263  return compatibleTargets;
264 }
265 
270 {
271  return module;
272 }
273 
278 {
279  return base;
280 }
281 
286 {
287  return builtIn;
288 }
289 
294 {
295  this->builtIn = builtIn;
296 }
297 
303 {
304  return modulePath;
305 }