// // Created by erki on 28.02.21. // #ifndef C_ANALYZER_OPERATIONLOG_HPP #define C_ANALYZER_OPERATIONLOG_HPP #include #include #include struct OperationLog { struct IEntry { [[nodiscard]] virtual nlohmann::json toJson() const = 0; }; struct FunctionCall : IEntry { static constexpr char TYPE_NAME[] = "function_call"; std::string function_name; std::string call_result_type; [[nodiscard]] nlohmann::json toJson() const override; }; struct BasicOperation : IEntry { static constexpr char TYPE_NAME[] = "basic_operation"; std::string type_lhs; std::string type_rhs; std::string type_result; [[nodiscard]] nlohmann::json toJson() const override; }; unsigned int line; std::string entry_type; std::unique_ptr entry; bool is_fallthrough; std::vector current_for_loops; OperationLog() = default; OperationLog(const OperationLog&) = delete; OperationLog(OperationLog&&) = default; void DecodeEntry(const nlohmann::json& j); }; class IOperationOutput { public: virtual ~IOperationOutput() = default; virtual void pushOperation(const std::string& filename, OperationLog&& op) = 0; }; inline void to_json(nlohmann::json& j, const OperationLog& l) { j = nlohmann::json{ {"line", l.line}, {"entry_type", l.entry_type}, {"entry", l.entry->toJson()}, {"is_fallthrough", l.is_fallthrough}, {"current_for_loops", l.current_for_loops} }; } inline void from_json(const nlohmann::json& j, OperationLog& l) { j.at("line").get_to(l.line); j.at("entry_type").get_to(l.entry_type); l.DecodeEntry(j["entry"]); j.at("is_fallthrough").get_to(l.is_fallthrough); j.at("current_for_loops").get_to(l.current_for_loops); } inline void to_json(nlohmann::json& j, const OperationLog::BasicOperation& bo) { j = nlohmann::json{ {"type_lhs", bo.type_lhs}, {"type_rhs", bo.type_rhs}, {"type_result", bo.type_result} }; } inline void from_json(const nlohmann::json& j, OperationLog::BasicOperation& bo) { j.at("type_lhs").get_to(bo.type_lhs); j.at("type_rhs").get_to(bo.type_rhs); j.at("type_result").get_to(bo.type_result); } inline void to_json(nlohmann::json& j, const OperationLog::FunctionCall& fcall) { j = nlohmann::json{ {"function_name", fcall.function_name}, {"call_result_type", fcall.call_result_type} }; } inline void from_json(const nlohmann::json& j, OperationLog::FunctionCall& fcall) { j.at("function_name").get_to(fcall.function_name); j.at("call_result_type").get_to(fcall.call_result_type); } #endif //C_ANALYZER_OPERATIONLOG_HPP