// Original work Copyright 2016 Alexander Stocko . // Modified work Copyright 2023 Daan Vanoverloop // See the COPYRIGHT file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #ifndef _CTABLEGEN_TABLEGEN_HPP_ #define _CTABLEGEN_TABLEGEN_HPP_ #include #include #include #include #include #include #include #include #include #include "TableGen.h" #include "Types.h" #include "llvm/Support/CBindingWrapping.h" using namespace llvm; namespace ctablegen { typedef std::map, std::less<>> RecordMap; typedef std::vector RecordVector; typedef std::pair DagPair; class TableGenParser { public: TableGenParser() {} bool addSource(const char *source); bool addSourceFile(const StringRef source); void addIncludePath(const StringRef include); RecordKeeper *parse(); SourceMgr sourceMgr; private: std::vector includeDirs; }; // Utility TableGenRecTyKind tableGenFromRecType(RecTy *rt); /// A simple raw ostream subclass that forwards write_impl calls to the /// user-supplied callback together with opaque user-supplied data. class CallbackOstream : public llvm::raw_ostream { public: CallbackOstream(std::function callback, void *opaqueData) : raw_ostream(/*unbuffered=*/true), callback(std::move(callback)), opaqueData(opaqueData), pos(0u) {} void write_impl(const char *ptr, size_t size) override { TableGenStringRef string = TableGenStringRef { .data = ptr, .len = size }; callback(string, opaqueData); pos += size; } uint64_t current_pos() const override { return pos; } private: std::function callback; void *opaqueData; uint64_t pos; }; } // namespace ctablegen DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::TableGenParser, TableGenParserRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RecordKeeper, TableGenRecordKeeperRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::RecordMap, TableGenRecordMapRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::RecordVector, TableGenRecordVectorRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ArrayRef, TableGenRecordArrayRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ArrayRef, TableGenRecordValArrayRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Record, TableGenRecordRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RecordVal, TableGenRecordValRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TypedInit, TableGenTypedInitRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::DagPair, TableGenDagPairRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::RecordMap::const_iterator, TableGenRecordKeeperIteratorRef); DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ArrayRef, TableGenSourceLocationRef); #endif