void SynthLib2ESolver::VisitSortDefCmd(const SortDefCmd* Cmd)
 {
     InSortDef = true;
     SortName = Cmd->GetName();
     try {
         ASTVisitorBase::VisitSortDefCmd(Cmd);
     } catch (const ESException& Ex) {
         ReThrow(Ex, Cmd->GetLocation());
     }
     // The type should already have been bound
     InSortDef = false;
 }
 void SynthLib2ESolver::VisitEnumSortExpr(const EnumSortExpr* Sort)
 {
     if (!InSortDef) {
         throw SynthLib2Exception(Sort->GetLocation().GetLineNum(),
                                  Sort->GetLocation().GetColNum(),
                                  "Anonymous enumerated sorts are meaningless and are disallowed");
     } else {
         try {
             Solver->CreateEnumType(SortName, Sort->GetConstructors());
         } catch (ESException& Ex) {
             ReThrow(Ex, Sort->GetLocation());
         }
     }
 }
 void SynthLib2ESolver::VisitVarDeclCmd(const VarDeclCmd* Cmd)
 {
     ASTVisitorBase::VisitVarDeclCmd(Cmd);
     // Register the variable
     if (SortStack.size() != 1) {
         throw InternalError((string)"Internal Error: Expected to see exactly one sort on stack!");
     }
     try {
         Solver->CreateQuantifiedVariable(Cmd->GetName(), SortStack[0]);
     } catch(const ESException& Ex) {
         ReThrow(Ex, Cmd->GetLocation());
     }
     SortStack.pop_back();
 }
 void SynthLib2ESolver::VisitSymbolTerm(const SymbolTerm* TheTerm)
 {
     // We need to resolve this symbol
     // Try the let var binding stack first
     auto Exp = GetVarExpression(TheTerm->GetSymbol());
     if (Exp != nullptr) {
         ProcessedTermStack.push_back(Exp);
         return;
     }
     // We need to resolve this operator with the solver
     try {
         auto Exp2 = Solver->CreateExpression(TheTerm->GetSymbol());
         ProcessedTermStack.push_back(Exp2);
     } catch (const ESException& Ex) {
         ReThrow(Ex, TheTerm->GetLocation());
     }
 }
void SocketManager::RegisterSocketService(GenericSocketService *service) {
    service->SetSocketManager(this);
    auto serviceVector = service->GetServiceDescription();
    Try {
        for (auto iter = serviceVector.begin(); iter != serviceVector.end(); ++iter)
            CreateDomainSocket(service, *iter);
    } Catch (Exception::Base) {
        for (int i =0; i < (int)m_socketDescriptionVector.size(); ++i)
        {
            auto &desc = m_socketDescriptionVector[i];
            if (desc.service == service && desc.isOpen) {
                close(i);
                desc.isOpen = false;
            }
        }
        ReThrow(Exception::Base);
    }
}
    void SynthLib2ESolver::VisitFunTerm(const FunTerm* TheTerm)
    {
        ASTVisitorBase::VisitFunTerm(TheTerm);

        // The arguments to this term must be on top of the stack now
        const uint32 NumArgs = TheTerm->GetArgs().size();
        vector<Expression> ArgExps(NumArgs);
        for (uint32 i = 0; i < NumArgs; ++i) {
            ArgExps[NumArgs - i - 1] = ProcessedTermStack.back();
            ProcessedTermStack.pop_back();
        }
        try {
            auto FunExp = Solver->CreateExpression(TheTerm->GetFunName(), ArgExps);
            ProcessedTermStack.push_back(FunExp);
        } catch (const ESException& Ex) {
            ReThrow(Ex, TheTerm->GetLocation());
        }
    }
 void SynthLib2ESolver::VisitFunDefCmd(const FunDefCmd* Cmd)
 {
     InFunDef = true;
     // Populate the arg maps
     ArgMap.clear();
     uint32 CurPosition = 0;
     vector<const ESFixedTypeBase*> ArgTypes;
     for (auto const ASPair : Cmd->GetArgs()) {
         if (ArgMap.find(ASPair->GetName()) != ArgMap.end()) {
             throw SynthLib2Exception(ASPair->GetLocation().GetLineNum(),
                                      ASPair->GetLocation().GetColNum(),
                                      (string)"Error, parameter identifer \"" + ASPair->GetName() + 
                                      "\" has been reused");
         }
         // Visit the sort
         ASPair->GetSort()->Accept(this);
         auto Type = SortStack.back();
         ArgTypes.push_back(Type);
         SortStack.pop_back();
         ArgMap[ASPair->GetName()] = Solver->CreateFreshFormalParamExpression(ASPair->GetName(),
                                                                              Type, CurPosition++);
     }
     // Process the return type
     Cmd->GetSort()->Accept(this);
     const ESFixedTypeBase* Type = SortStack.back();
     SortStack.pop_back();
     // now process the term
     Cmd->GetTerm()->Accept(this);
     // Create the function
     auto Exp = ProcessedTermStack.back();
     ProcessedTermStack.pop_back();
     try {
         Solver->CreateFunction(Cmd->GetFunName(), ArgTypes, Type, Exp);
     } catch(const ESException& Ex) {
         ReThrow(Ex, Cmd->GetLocation());
     }
     InFunDef = false;
     ArgMap.clear();
     CurPosition = 0;
 }
    void SynthLib2ESolver::VisitSynthFunCmd(const SynthFunCmd* Cmd)
    {
        InSynthFun = true;
        ArgMap.clear();
        CurArgNum = 0;
        
        // Create a grammar
        SynthGrammar = new Grammar(Cmd->GetFunName() + "_Grammar", Solver);
        GNonTerms.clear();

        // Create the appropriate non-terminals, let vars and FP vars
        for (auto const& Def : Cmd->GetGrammarRules()) {
            Def->GetSort()->Accept(this);
            auto Type = SortStack.back();
            SortStack.pop_back();
            try {
                SynthGrammar->MakeNT(Def->GetName(), Type);
                GNonTerms.insert(Def->GetName());
            } catch (const ESException& Ex) {
                ReThrow(Ex, Def->GetLocation());
            }
        }

        GArgs.clear();
        GLetVars.clear();
        vector<string> ParamNames;
        vector<const ESFixedTypeBase*> ArgTypes;
        // The formal params now
        for (auto const& ASPair : Cmd->GetArgs()) {
            ASPair->GetSort()->Accept(this);
            auto Type = SortStack.back();
            ArgTypes.push_back(Type);
            GArgs.insert(ASPair->GetName());
            ParamNames.push_back(ASPair->GetName());
            SortStack.pop_back();
            try {
                SynthGrammar->MakeFP(ASPair->GetName(), Type, CurArgNum++);
            } catch (const ESException& Ex) {
                ReThrow(Ex, ASPair->GetLocation());
            }
        }

        // The return type
        Cmd->GetSort()->Accept(this);
        auto RetType = SortStack.back();
        SortStack.pop_back();

        // The let vars now
        auto&& LetVars = GrammarLetVarGatherer::Do(Cmd->GetGrammarRules());

        for (auto const& KV : LetVars) {
            GLetVars.insert(KV.first);
            KV.second->Accept(this);
            auto Type = SortStack.back();
            SortStack.pop_back();
            if (GArgs.find(KV.first) != GArgs.end()) {
                throw SynthLib2Exception(KV.second->GetLocation().GetLineNum(),
                                         KV.second->GetLocation().GetColNum(),
                                         (string)"Error: Let var \"" + KV.first + "\" aliases a " +
                                         " formal parameter. This is disallowed in this implementation");
            }
            try {
                SynthGrammar->MakeLetVar(KV.first, Type);
            } catch (const ESException& Ex) {
                ReThrow(Ex, KV.second->GetLocation());
            }
        }
        
        // Recurse on the actual definitions
        for (auto const& Def : Cmd->GetGrammarRules()) {
            for (auto const& Exp : Def->GetExpansions()) {
                Exp->Accept(this);
                SynthGrammar->AddExpansion(Def->GetName(), ProcessedGTermStack.back());
                ProcessedGTermStack.pop_back();
            }
        }

        // Canonicalize the grammar
        SynthGrammar->Canonicalize();

        if (Solver->GetOpts().StatsLevel >= 2) {
            auto& TheLogger = Solver->GetLogger();
            TheLogger.Log2("Canonicalized Grammar:\n");
            TheLogger.Log2(SynthGrammar->ToString()).Log2("\n");
        }
        // Finally, make the synth fun operator
        Solver->CreateFunction(Cmd->GetFunName(), ArgTypes, ParamNames, RetType, SynthGrammar);

        SynthGrammar = nullptr;
        CurArgNum = 0;
        ArgMap.clear();
        InSynthFun = false;
    }