BasicSymbol* BasicScope::resolve(string name){ if(isVariable(name)){ return this->variables[name]; } if(isFunction(name)){ return this->functions[name][0]; } if(isStructure(name)){ return this->structures[name]; } throw TypeException("What are you trying to resolve!?"); }
GrammarNonTerminal* Grammar::MakeNT(const string& NTName, const ESFixedTypeBase* NTType) { auto it = NonTerminalMap.find(NTName); if (it == NonTerminalMap.end()) { auto Retval = Get<GrammarNonTerminal>(this, NTName, NTType); NonTerminalMap[NTName] = Retval; return Retval; } else { if (it->second->GetType() != NTType) { throw TypeException((string)"Error: Attempted to create a grammar non-terminal \"" + StartNTName + "\" with mismatched types."); } return it->second; } }
Expression ESolver::CreateLetExpression(const LetExpBindingMap& Bindings, Expression BoundInExpression) { // Check that the lhs of each binding is indeed a let bound variable for (auto const& KV : Bindings) { auto Op = KV.first->GetOp()->As<LetBoundVarOperator>(); if (Op == nullptr) { ostringstream sstr; sstr << KV.first; throw TypeException((string)"Error: Found a weird expression where a let bound var expression " + "was expected.\nThe expression that caused the exception: " + sstr.str()); } } Expression Retval = new UserLetExpression(Bindings, BoundInExpression); return ExpMgr->GetExp(Retval); }
GrammarFunc* Grammar::MakeFunc(const string& FuncName, const vector<GrammarNode*>& Args) { // extract the types const uint32 NumArgs = Args.size(); vector<const ESFixedTypeBase*> ArgTypes(NumArgs); for (uint32 i = 0; i < NumArgs; ++i) { ArgTypes[i] = Args[i]->GetType(); } auto Op = Solver->LookupOperator(FuncName, ArgTypes); if (Op == nullptr) { throw TypeException((string)"Could not resolve operator \"" + FuncName + "\" to anything meaningful.\n" + "This could be due to " + "mismatched parameters"); } return Get<GrammarFunc>(this, Op, Args); }
GrammarFPVar* Grammar::MakeFP(const string& FPName, const ESFixedTypeBase* FPType, uint32 Position) { auto it = FormalParamVars.find(FPName); if (it == FormalParamVars.end()) { auto FPOp = Solver->CreateFormalParamOperator(FPName, FPType, Position); auto Retval = new GrammarFPVar(this, FPOp); FormalParamVars[FPName] = Retval; GNSet.insert(Retval); return Retval; } else { // make sure the types match if (it->second->GetType() != FPType) { throw TypeException((string)"Error: Attempted to create a a grammar formal param \"" + FPName + "\" with mismatched types"); } return it->second; } }
GrammarLetVar* Grammar::MakeLetVar(const string& LetVarName, const ESFixedTypeBase* LetVarType) { auto it = LetBoundVars.find(LetVarName); if (it == LetBoundVars.end()) { auto LetOp = Solver->CreateLetBoundVariable(LetVarName, LetVarType); LetOp->SetPosition(LetCounter++); auto Retval = new GrammarLetVar(this, LetOp); LetBoundVars[LetVarName] = Retval; GNSet.insert(Retval); return Retval; } else { // Make sure the types match if (it->second->GetType() != LetVarType) { throw TypeException((string)"Error: Attempted to create a a grammar let variable \"" + LetVarName + "\" with mismatched types"); } return it->second; } }
StructureSymbol* BasicScope::resolveStructure(Type *type){ string name = type->getName(); auto currentScope = this->resolveNamedScope(type->getFullName()); while(currentScope != nullptr){ if(currentScope->getTypes().count(name)){ break; } currentScope = currentScope->getParentScope(); } if(!currentScope || !currentScope->getTypes().count(name)){ throw NoticeException("Undeclared type'"+ name + "'!"); } auto typeStructures = currentScope->getTypeStructures(); if(!typeStructures.count(type)){ throw TypeException("No way to resolve type " + type->toString() + " as structure!"); } return typeStructures[type]; }
bool SumAggrLit::match(Grounder *grounder) { try { if(lower_.get()) lowerBound_ = lower_->val(grounder).number(); else lowerBound_ = std::numeric_limits<int32_t>::min(); if(upper_.get()) upperBound_ = upper_->val(grounder).number(); else upperBound_ = std::numeric_limits<int32_t>::max(); if(assign_) upperBound_ = lowerBound_; } catch(const Val *val) { std::ostringstream oss; oss << "cannot convert "; val->print(grounder, oss); oss << " to integer"; std::string str(oss.str()); oss.str(""); print(grounder, oss); throw TypeException(str, StrLoc(grounder, loc()), oss.str()); } fact_ = false; factOnly_ = true; valLower_ = valUpper_ = fixed_ = 0; checkUpperBound_ = (set() && upper_.get()); if(set()) uniques_.clear(); foreach(CondLit &lit, conds_) lit.ground(grounder); lowerBound_ = lower_.get() ? std::max(lowerBound_ - fixed_, valLower_) : valLower_; upperBound_ = upper_.get() || assign_ ? std::min(upperBound_ - fixed_, valUpper_) : valUpper_; if(head() && !factOnly_) return true; if(lowerBound_ > upperBound_) return (fact_ = sign_) || head(); if(valLower_ >= lowerBound_ && valUpper_ <= upperBound_) return (fact_ = !sign_) || head(); if(valUpper_ < lowerBound_ || valLower_ > upperBound_) return (fact_ = sign_) || head(); return true; }
inline void ESolver::CheckOperatorRedeclaration(const string& OperatorName) const { if(LookupOperatorNI(OperatorName) != NULL) { throw TypeException("Redeclaration of variable or constant \"" + OperatorName + "\"."); } }
Expression ESolver::CreateExpression(const OperatorBase* OpInfo, const vector<Expression>& Children) { Expression NewExp; // Type checks if (Children.size() > 0) { auto FuncOp = OpInfo->As<FuncOperatorBase>(); const uint32 NumChildren = Children.size(); vector<const ESFixedTypeBase*> ArgTypes(NumChildren); for (uint32 i = 0; i < NumChildren; ++i) { ArgTypes[i] = Children[i]->GetType(); } // Quick type check using name mangling auto const& ExpectedName = FuncOp->GetMangledName(); auto const&& ActualName = FuncOperatorBase::MangleName(OpInfo->GetName(), ArgTypes); if (ExpectedName != ActualName) { throw TypeException((string)"Error in application of function \"" + OpInfo->GetName() + "\".\n" + "This could be due to mismatched numbers or types of parameters"); } // We're good. Create the expression if (OpInfo->As<InterpretedFuncOperator>() != nullptr) { NewExp = new UserInterpretedFuncExpression(OpInfo->As<InterpretedFuncOperator>(), Children); } else { NewExp = new UserSynthFuncExpression(OpInfo->As<SynthFuncOperator>(), Children); } } else { // This could be a constant, a UQVariable, an aux variable, // a formal param or a let bound variable if (OpInfo->As<VarOperatorBase>() != nullptr) { if (OpInfo->As<UQVarOperator>() != nullptr) { NewExp = new UserUQVarExpression(OpInfo->As<UQVarOperator>()); } else if (OpInfo->As<FormalParamOperator>() != nullptr) { NewExp = new UserFormalParamExpression(OpInfo->As<FormalParamOperator>()); } else if (OpInfo->As<AuxVarOperator>() != nullptr) { NewExp = new UserAuxVarExpression(OpInfo->As<AuxVarOperator>()); } else if (OpInfo->As<LetBoundVarOperator>() != nullptr) { NewExp = new UserLetBoundVarExpression(OpInfo->As<LetBoundVarOperator>()); } else { throw InternalError((string)"BUG: Unhandled operator type at " + __FILE__ + ":" + to_string(__LINE__)); } } else { // This can only be a const operator now if (OpInfo->As<ConstOperator>() != nullptr) { NewExp = new UserConstExpression(OpInfo->As<ConstOperator>()); } else if (OpInfo->As<MacroOperator>() != nullptr) { // OR it can be a constant macro expression NewExp = new UserInterpretedFuncExpression(OpInfo->As<MacroOperator>(), Children); } else { throw TypeException((string)"Error: Could not find a meaningful way to construct " + "an expression with operator having name \"" + OpInfo->GetName() + "\".\nPerhaps you provided arguments where none were expected, " + "or this could be a bug"); } } } ExpMgr->GC(); return ExpMgr->GetExp(NewExp); }
void MacroExpChecker::VisitUserUQVarExpression(const UserUQVarExpression* Exp) { throw TypeException("Universally quantified variables are not allowed in macro definitions!"); }