int Function::addVar(Var v) { if(!vars.contains(v.getName())) { vars.insert(v.getName(), v); varsOrder.append(v.getName()); return 1; } else { return 0; } }
void Environment::addExtern(const Var& var) { iassert(!hasExtern(var.getName())) << var << " already in environment"; content->externs.push_back(var); size_t loc = content->externs.size()-1; content->externLocationByName.insert({var.getName(), loc}); // TODO: Change so that variables are not mapped to themselves. This means // lowering must map (sparse/dense) tensor value storage to arrays. addExternMapping(var, var); }
Stmt find(const Var &result, const std::vector<Expr> &exprs, string name, function<Expr(Expr,Expr)> compare) { iassert(exprs.size() > 0); Stmt resultStmt; if (exprs.size() == 1) { resultStmt = AssignStmt::make(result, exprs[0]); } else if (exprs.size() == 2) { resultStmt = IfThenElse::make(compare(exprs[0], exprs[1]), AssignStmt::make(result, exprs[0]), AssignStmt::make(result, exprs[1])); } else { resultStmt = AssignStmt::make(result, exprs[0]); vector<Stmt> tests; for (size_t i=1; i < exprs.size(); ++i) { Stmt test = IfThenElse::make(compare(exprs[i], result), AssignStmt::make(result, exprs[i])); tests.push_back(test); } resultStmt = Block::make(resultStmt, Block::make(tests)); } string commentString = result.getName() + " = " + name + "(" + util::join(exprs) + ")"; return Comment::make(commentString, resultStmt); }
// class PathExpressionPrinter void PathExpressionPrinter::print(const Var &v) { simit_iassert(v.defined()) << "attempting to print undefined var"; std::string name; if (util::contains(names, v)) { name = names.at(v); } else { if (v.getName() != "") { name = nameGenerator.getName(v.getName()); } else { name = nameGenerator.getName("e"); } names[v] = name; } os << name; }
void Var::insert(Var var) { if(Type=="map") { Map.insert(var.getName(),var); } else qDebug()<<"Var::insert - type is not map; Name = "<<Name<<"Type = "<<Type; }
void GlobalVariableList::mergePublic(const GlobalVariableList& old) { map_var_t::iterator last = vmap.begin(); for (map_var_t::const_iterator i = old.vmap.begin(), e = old.vmap.end(); i != e; ++i) { if (!i->second->isPublic()) continue; Var* v = new Var(const_cast<Var*>(i->second)); last = vmap.insert(last, map_var_t::value_type(v->getName(), v)); } }
Var* GlobalVariableList::parseCreatePendingVar(const char* name, const QoreTypeInfo* typeInfo) { assert(!parseFindVar(name)); Var* var = new Var(name, typeInfo); pending_vmap[var->getName()] = var; printd(5, "GlobalVariableList::parseCreatePendingVar(): %s (%p) added (resolved type %s)\n", name, var, typeInfo->getName()); return var; }
Var Var::fromXML(QXmlStreamReader& xml) { while (xml.tokenType() != QXmlStreamReader::StartElement && !xml.atEnd() && !xml.hasError()) { xml.readNext(); } QString root = xml.name().toString(); setParam("map",root); while (!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == root) && !xml.atEnd() && !xml.hasError()) { xml.readNext(); if (xml.tokenType() == QXmlStreamReader::StartElement) { Var el; el.fromXML(xml); if(getType()=="map") { if(!Map.contains(el.getName())) { Map.insert(el.getName(),el); } else { setType("list"); List<<Map.value(el.getName()); } } if(getType()=="list") { List<<el; } } if (xml.tokenType() == QXmlStreamReader::Characters) { QString text=xml.text().toString(); if(!text.startsWith('\n')) { setType("string"); String = text; } } } return *this; }
Var* GlobalVariableList::runtimeCreateVar(const char* name, const QoreTypeInfo* typeInfo) { if (parseFindVar(name)) return 0; Var* var = new Var(name, typeInfo); vmap[var->getName()] = var; printd(5, "GlobalVariableList::runtimeCreateVar(): %s (%p) added (resolved type %s)\n", name, var, typeInfo->getName()); return var; }
// adds directly to committed list Var* GlobalVariableList::import(Var* v, ExceptionSink* xsink, bool readonly) { map_var_t::iterator i = vmap.find(v->getName()); if (i != vmap.end()) { xsink->raiseException("PROGRAM-IMPORTGLOBALVARIABLE-EXCEPTION", "'%s' already exists in the target namespace", v->getName()); return 0; } Var* var = new Var(v, readonly); pending_vmap[var->getName()] = var; printd(5, "GlobalVariableList::import(): reference to %s (%p) added\n", v->getName(), var); return var; }
GlobalVariableList::GlobalVariableList(const GlobalVariableList& old, int64 po) { // don't inherit any global vars if the appropriate flag is not already set if ((po & PO_NO_INHERIT_GLOBAL_VARS)) return; map_var_t::iterator last = vmap.begin(); for (map_var_t::const_iterator i = old.vmap.begin(), e = old.vmap.end(); i != e; ++i) { //printd(5, "GlobalVariableList::GlobalVariableList() this: %p v: %p '%s' pub: %d\n", this, i->second, i->second->getName(), i->second->isPublic()); if (!i->second->isPublic()) continue; Var* v = new Var(const_cast<Var*>(i->second)); last = vmap.insert(last, map_var_t::value_type(v->getName(), v)); } }
void Environment::addTensorIndex(const StencilLayout& stencil, const Var& var) { iassert(var.defined()) << "attempting to add a tensor index to an undefined var"; string name = var.getName(); // Lazily create a new index if no index with the given pexpr exist. // TODO: Maybe rename indices as they get used by multiple tensors if (!hasTensorIndex(stencil)) { TensorIndex ti(name+"_index", stencil); content->tensorIndices.push_back(ti); size_t loc = content->tensorIndices.size() - 1; content->locationOfTensorIndexStencil.insert({stencil, loc}); } content->tensorIndexOfVar.insert({var, getTensorIndex(stencil)}); }
void Environment::addTensorIndex(const pe::PathExpression& pexpr, const Var& var) { iassert(pexpr.defined()) << "Attempting to add tensor " << util::quote(var) << " index with an undefined path expression"; iassert(var.defined()) << "attempting to add a tensor index to an undefined var"; string name = var.getName(); // Lazily create a new index if no index with the given pexpr exist. // TODO: Maybe rename indices as they get used by multiple tensors if (!hasTensorIndex(pexpr)) { TensorIndex ti(name+"_index", pexpr); content->tensorIndices.push_back(ti); size_t loc = content->tensorIndices.size() - 1; content->locationOfTensorIndex.insert({pexpr, loc}); } content->tensorIndexOfVar.insert({var, getTensorIndex(pexpr)}); }
// When this function returns, tree must point to the last node in the SUB // header (the return type). bool Component::paramListsEqual(Token *&tree, Sub *sub) const { unsigned int index = 0; Var *var = sub->getVar(0); // Can't use an early return because of pre-requirement (see above). bool iseq = true; while (tree->id == DEF) { // Check if the pre-existing list is shorter if (!var) { iseq = false; tree = tree->next; continue; } const Type *type = getType(tree->child); std::string newname; if (!type) iseq = false; else if (*type != *var->getType()) iseq = false; else if (!strEqualIdLookup(var->getName(), newname = tree->child->next->text)) { printf("warning: param names differ from DECLARE to SUB, " "using '%s' instead of '%s' (line %d)\n", tree->child->next->text, var->getName().c_str(), tree->child->next->loc.first_line); // MUST NOT call var->setName, otherwise sub's var map won't // get updated. if (!sub->setVarName(var->getName(), newname)) { printf("Just kidding; there was a conflict with that " "name. I'm still gonna try to compile this, but you " "should really consider synchronizing the names.\n"); } } var = sub->getVar(++index); tree = tree->next; } SubType *subty = const_cast<SubType *>( static_cast<const SubType *>(sub->getType()) ); // Does it have var-args? if (tree->id == ELLIPSIS) { if ((subty->getFlags() & SubType::VARARGS) == 0) iseq = false; else if (tree->child) { // Is it an implicit len or null? if (tree->child->id == LEN && ((subty->getFlags() & SubType::VA_IMPLICIT_LEN) == 0)) iseq = false; else if (tree->child->id == INTCONST && ((subty->getFlags() & SubType::VA_IMPLICIT_NULL) == 0)) iseq = false; } tree = tree->next; } else { if ((subty->getFlags() & SubType::VARARGS) != 0) iseq = false; } assert(tree->id == AS && "no return type for sub"); // If any of the params were not equal, it doesn't matter what the return // type is, and we can just return false here. Also make sure the // pre-existing list is the same length. if (!iseq || var) return false; // Make sure the return types are equal also. const Type *other_rettype = subty->getReturnType(); // If you left off the return type from the SUB, it is carried over from // the declare, but a warning is shown. if (tree->child == 0) { if (*other_rettype != *void_type) { printf("warning: omitted return type, using '%s' (line %d)\n", other_rettype->getName().c_str(), tree->loc.first_line); } return true; } const Type *rettype = getType(tree->child); return rettype && *rettype == *other_rettype; }
void Environment::addTemporary(const Var& var) { iassert(!hasExtern(var.getName())) << var << " already in environment"; content->temporaries.push_back(var); content->temporarySet.insert(var); }
void Environment::addExternMapping(const Var& var, const Var& mapping) { iassert(hasExtern(var.getName())); size_t loc = content->externLocationByName.at(var.getName()); content->externs.at(loc).addMapping(mapping); }