2009-06-20 23:00:19 +04:00
|
|
|
/*
|
2011-01-21 04:21:09 +03:00
|
|
|
* OpenSCAD (www.openscad.org)
|
|
|
|
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
|
|
|
|
* Marius Kintel <marius@kintel.net>
|
2009-06-20 23:00:19 +04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2010-02-01 12:34:18 +03:00
|
|
|
* As a special exception, you have permission to link this program
|
|
|
|
* with the CGAL library and distribute executables, as long as you
|
|
|
|
* follow the requirements of the GNU GPL in regard to all of the
|
|
|
|
* software in the executable aside from CGAL.
|
|
|
|
*
|
2009-06-20 23:00:19 +04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "module.h"
|
2012-02-04 04:43:53 +04:00
|
|
|
#include "ModuleCache.h"
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "node.h"
|
2013-04-09 08:28:16 +04:00
|
|
|
#include "modcontext.h"
|
|
|
|
#include "evalcontext.h"
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "expression.h"
|
|
|
|
#include "function.h"
|
2009-12-09 17:22:50 +03:00
|
|
|
#include "printutils.h"
|
2012-02-04 04:43:53 +04:00
|
|
|
|
2013-04-05 09:30:09 +04:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
#include "boosty.h"
|
2011-09-03 08:10:36 +04:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <sstream>
|
2012-02-18 02:05:36 +04:00
|
|
|
#include <sys/stat.h>
|
2009-06-20 23:00:19 +04:00
|
|
|
|
|
|
|
AbstractModule::~AbstractModule()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-09 08:28:16 +04:00
|
|
|
AbstractNode *AbstractModule::evaluate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *node = new AbstractNode(inst);
|
2009-06-30 00:30:43 +04:00
|
|
|
|
2013-04-09 08:28:16 +04:00
|
|
|
node->children = inst->evaluateChildren(evalctx);
|
2009-06-30 00:30:43 +04:00
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
return node;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2011-09-03 08:10:36 +04:00
|
|
|
std::string AbstractModule::dump(const std::string &indent, const std::string &name) const
|
2009-06-21 10:53:46 +04:00
|
|
|
{
|
2011-09-03 08:10:36 +04:00
|
|
|
std::stringstream dump;
|
|
|
|
dump << indent << "abstract module " << name << "();\n";
|
|
|
|
return dump.str();
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
ModuleInstantiation::~ModuleInstantiation()
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2011-09-04 22:35:23 +04:00
|
|
|
BOOST_FOREACH (Expression *v, argexpr) delete v;
|
|
|
|
BOOST_FOREACH (ModuleInstantiation *v, children) delete v;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2010-02-12 16:20:15 +03:00
|
|
|
IfElseModuleInstantiation::~IfElseModuleInstantiation()
|
|
|
|
{
|
2011-09-04 22:35:23 +04:00
|
|
|
BOOST_FOREACH (ModuleInstantiation *v, else_children) delete v;
|
2010-02-12 16:20:15 +03:00
|
|
|
}
|
|
|
|
|
2013-04-05 09:30:09 +04:00
|
|
|
/*!
|
|
|
|
Returns the absolute path to the given filename, unless it's empty.
|
|
|
|
*/
|
|
|
|
std::string ModuleInstantiation::getAbsolutePath(const std::string &filename) const
|
|
|
|
{
|
|
|
|
if (!filename.empty() && !boosty::is_absolute(fs::path(filename))) {
|
|
|
|
return boosty::absolute(fs::path(this->modpath) / filename).string();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-03 08:10:36 +04:00
|
|
|
std::string ModuleInstantiation::dump(const std::string &indent) const
|
2009-06-21 10:53:46 +04:00
|
|
|
{
|
2011-09-03 08:10:36 +04:00
|
|
|
std::stringstream dump;
|
|
|
|
dump << indent;
|
|
|
|
dump << modname + "(";
|
|
|
|
for (size_t i=0; i < argnames.size(); i++) {
|
|
|
|
if (i > 0) dump << ", ";
|
|
|
|
if (!argnames[i].empty()) dump << argnames[i] << " = ";
|
|
|
|
dump << *argexpr[i];
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
2009-06-21 12:59:53 +04:00
|
|
|
if (children.size() == 0) {
|
2011-09-03 08:10:36 +04:00
|
|
|
dump << ");\n";
|
2009-06-21 12:59:53 +04:00
|
|
|
} else if (children.size() == 1) {
|
2011-09-03 08:10:36 +04:00
|
|
|
dump << ")\n";
|
|
|
|
dump << children[0]->dump(indent + "\t");
|
2009-06-21 12:59:53 +04:00
|
|
|
} else {
|
2011-09-03 08:10:36 +04:00
|
|
|
dump << ") {\n";
|
|
|
|
for (size_t i = 0; i < children.size(); i++) {
|
|
|
|
dump << children[i]->dump(indent + "\t");
|
2009-06-21 12:59:53 +04:00
|
|
|
}
|
2011-09-03 08:10:36 +04:00
|
|
|
dump << indent << "}\n";
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
2011-09-03 08:10:36 +04:00
|
|
|
return dump.str();
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
|
2013-04-09 08:28:16 +04:00
|
|
|
AbstractNode *ModuleInstantiation::evaluate_instance(const Context *ctx) const
|
2009-06-21 12:59:53 +04:00
|
|
|
{
|
2013-04-09 08:28:16 +04:00
|
|
|
EvalContext c(ctx);
|
|
|
|
for (size_t i=0; i<argnames.size(); i++) {
|
|
|
|
c.eval_arguments.push_back(std::make_pair(argnames[i],
|
|
|
|
i < argexpr.size() && argexpr[i] ?
|
|
|
|
argexpr[i]->evaluate(ctx) :
|
|
|
|
Value()));
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
2013-04-09 08:28:16 +04:00
|
|
|
c.children = this->children;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRINT("New eval ctx:");
|
|
|
|
c.dump(NULL, this);
|
|
|
|
#endif
|
|
|
|
AbstractNode *node = ctx->evaluate_module(*this, &c); // Passes c as evalctx
|
2009-07-01 13:48:25 +04:00
|
|
|
return node;
|
2009-06-21 12:59:53 +04:00
|
|
|
}
|
|
|
|
|
2013-04-09 08:28:16 +04:00
|
|
|
std::vector<AbstractNode*> ModuleInstantiation::evaluateChildren(const Context *evalctx) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2011-09-04 22:35:23 +04:00
|
|
|
std::vector<AbstractNode*> childnodes;
|
2011-12-26 19:34:47 +04:00
|
|
|
BOOST_FOREACH (ModuleInstantiation *modinst, this->children) {
|
2013-04-09 08:28:16 +04:00
|
|
|
AbstractNode *node = modinst->evaluate_instance(evalctx);
|
2011-12-26 19:34:47 +04:00
|
|
|
if (node) childnodes.push_back(node);
|
2011-09-03 08:10:36 +04:00
|
|
|
}
|
2011-09-04 22:35:23 +04:00
|
|
|
return childnodes;
|
|
|
|
}
|
|
|
|
|
2013-04-09 08:28:16 +04:00
|
|
|
std::vector<AbstractNode*> IfElseModuleInstantiation::evaluateElseChildren(const Context *evalctx) const
|
2011-09-04 22:35:23 +04:00
|
|
|
{
|
|
|
|
std::vector<AbstractNode*> childnodes;
|
2011-12-26 19:34:47 +04:00
|
|
|
BOOST_FOREACH (ModuleInstantiation *modinst, this->else_children) {
|
2013-04-09 08:28:16 +04:00
|
|
|
AbstractNode *node = modinst->evaluate_instance(evalctx);
|
2011-12-26 19:34:47 +04:00
|
|
|
if (node != NULL) childnodes.push_back(node);
|
2011-09-03 08:10:36 +04:00
|
|
|
}
|
2011-09-04 22:35:23 +04:00
|
|
|
return childnodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::~Module()
|
|
|
|
{
|
2012-12-01 07:18:44 +04:00
|
|
|
BOOST_FOREACH (const AssignmentContainer::value_type &v, assignments) delete v.second;
|
2011-09-04 22:35:23 +04:00
|
|
|
BOOST_FOREACH (FunctionContainer::value_type &f, functions) delete f.second;
|
|
|
|
BOOST_FOREACH (AbstractModuleContainer::value_type &m, modules) delete m.second;
|
|
|
|
BOOST_FOREACH (ModuleInstantiation *v, children) delete v;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2013-04-09 08:28:16 +04:00
|
|
|
AbstractNode *Module::evaluate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2013-04-09 08:28:16 +04:00
|
|
|
ModuleContext c(this, ctx, evalctx);
|
|
|
|
// FIXME: Set document path to the path of the module
|
2010-01-13 15:24:24 +03:00
|
|
|
c.set_variable("$children", Value(double(inst->children.size())));
|
2013-04-09 08:28:16 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
c.dump(this, inst);
|
|
|
|
#endif
|
2009-06-21 15:41:11 +04:00
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *node = new AbstractNode(inst);
|
2011-09-03 08:10:36 +04:00
|
|
|
for (size_t i = 0; i < children.size(); i++) {
|
2013-04-09 08:28:16 +04:00
|
|
|
AbstractNode *n = children[i]->evaluate_instance(&c);
|
2009-06-27 11:31:40 +04:00
|
|
|
if (n != NULL)
|
2011-08-04 06:58:17 +04:00
|
|
|
node->children.push_back(n);
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2011-09-03 08:10:36 +04:00
|
|
|
std::string Module::dump(const std::string &indent, const std::string &name) const
|
2009-06-21 10:53:46 +04:00
|
|
|
{
|
2011-09-03 08:10:36 +04:00
|
|
|
std::stringstream dump;
|
|
|
|
std::string tab;
|
|
|
|
if (!name.empty()) {
|
|
|
|
dump << indent << "module " << name << "(";
|
|
|
|
for (size_t i=0; i < argnames.size(); i++) {
|
|
|
|
if (i > 0) dump << ", ";
|
|
|
|
dump << argnames[i];
|
|
|
|
if (argexpr[i]) dump << " = " << *argexpr[i];
|
2009-06-23 23:56:46 +04:00
|
|
|
}
|
2011-09-03 08:10:36 +04:00
|
|
|
dump << ") {\n";
|
2009-06-23 23:56:46 +04:00
|
|
|
tab = "\t";
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
2011-09-03 08:10:36 +04:00
|
|
|
BOOST_FOREACH(const FunctionContainer::value_type &f, functions) {
|
|
|
|
dump << f.second->dump(indent + tab, f.first);
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
2011-09-03 08:10:36 +04:00
|
|
|
BOOST_FOREACH(const AbstractModuleContainer::value_type &m, modules) {
|
|
|
|
dump << m.second->dump(indent + tab, m.first);
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
2012-12-01 07:18:44 +04:00
|
|
|
BOOST_FOREACH(const std::string &var, assignments_var) {
|
|
|
|
dump << indent << tab << var << " = " << *assignments.at(var) << ";\n";
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
2011-09-03 08:10:36 +04:00
|
|
|
for (size_t i = 0; i < children.size(); i++) {
|
|
|
|
dump << children[i]->dump(indent + tab);
|
2009-06-23 23:56:46 +04:00
|
|
|
}
|
2011-09-03 08:10:36 +04:00
|
|
|
if (!name.empty()) {
|
|
|
|
dump << indent << "}\n";
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
2011-09-03 08:10:36 +04:00
|
|
|
return dump.str();
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
2011-12-26 19:34:47 +04:00
|
|
|
|
2012-02-18 02:05:36 +04:00
|
|
|
void Module::registerInclude(const std::string &filename)
|
2011-12-26 19:34:47 +04:00
|
|
|
{
|
2012-02-18 02:05:36 +04:00
|
|
|
struct stat st;
|
|
|
|
memset(&st, 0, sizeof(struct stat));
|
|
|
|
stat(filename.c_str(), &st);
|
|
|
|
this->includes[filename] = st.st_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Check if any dependencies have been modified and recompile them.
|
|
|
|
Returns true if anything was recompiled.
|
|
|
|
*/
|
|
|
|
bool Module::handleDependencies()
|
|
|
|
{
|
2012-09-02 21:17:14 +04:00
|
|
|
if (this->is_handling_dependencies) return false;
|
|
|
|
this->is_handling_dependencies = true;
|
|
|
|
|
2012-02-18 02:05:36 +04:00
|
|
|
bool changed = false;
|
2012-02-04 04:43:53 +04:00
|
|
|
// Iterating manually since we want to modify the container while iterating
|
|
|
|
Module::ModuleContainer::iterator iter = this->usedlibs.begin();
|
|
|
|
while (iter != this->usedlibs.end()) {
|
|
|
|
Module::ModuleContainer::iterator curr = iter++;
|
2012-02-18 02:05:36 +04:00
|
|
|
Module *oldmodule = curr->second;
|
2012-02-04 04:43:53 +04:00
|
|
|
curr->second = ModuleCache::instance()->evaluate(curr->first);
|
2012-03-28 05:53:09 +04:00
|
|
|
if (curr->second != oldmodule) {
|
|
|
|
changed = true;
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRINTB_NOCACHE(" %s: %p", curr->first % curr->second);
|
|
|
|
#endif
|
|
|
|
}
|
2012-02-04 04:43:53 +04:00
|
|
|
if (!curr->second) {
|
|
|
|
PRINTB_NOCACHE("WARNING: Failed to compile library '%s'.", curr->first);
|
|
|
|
this->usedlibs.erase(curr);
|
|
|
|
}
|
|
|
|
}
|
2012-09-02 21:17:14 +04:00
|
|
|
|
|
|
|
this->is_handling_dependencies = false;
|
2012-02-18 02:05:36 +04:00
|
|
|
return changed;
|
2011-12-26 19:34:47 +04:00
|
|
|
}
|