#pragma once #include "CodegenConfig.hpp" #include "CodegenMacros.hpp" #include "CodegenOutput.inl" #include #include #include #include #include namespace Utils { std::string ReadFileAsString(const std::filesystem::path& path) { auto file = Utils::OpenCstdioFile(path, Utils::Read); if (!file) throw std::runtime_error("Failed to open source file."); DEFER { fclose(file); }; fseek(file, 0, SEEK_END); auto fileSize = ftell(file); rewind(file); std::string result(fileSize, '\0'); fread(result.data(), fileSize, 1, file); return result; } bool WriteOutputFile(const CodegenOutput& output, std::string_view dir, std::string_view filename, std::string_view additionalSuffix = {}) { char path[2048]; snprintf(path, sizeof(path), "%.*s/%.*s%.*s", PRINTF_STRING_VIEW(dir), PRINTF_STRING_VIEW(filename), PRINTF_STRING_VIEW(additionalSuffix)); auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); if (!outputFile) { printf("[ERROR] unable to open output file %s\n", path); return false; } DEFER { fclose(outputFile); }; DEBUG_PRINTF("Writing output %s\n", path); output.Write(outputFile); return true; } std::string MakeFullName(std::string_view name, DeclNamespace* ns = nullptr) { size_t length = 0; std::vector components; if (!name.empty()) { components.push_back(name); length += name.length(); } DeclNamespace* currentNamespace = ns; while (currentNamespace) { components.push_back(currentNamespace->name); length += currentNamespace->name.size() + /*::*/ 2; currentNamespace = currentNamespace->container; } std::string fullname; fullname.reserve(length); for (auto it = components.rbegin(); it != components.rend(); ++it) { fullname += *it; fullname += "::"; } // Get rid of the last "::" fullname.pop_back(); fullname.pop_back(); return fullname; } void ProduceGeneratedHeaderFileHeader(CodegenOutput& output) { output.AddOutputThing(CodegenOutputThing{ .text = &R"""( // This file is generated. Any changes will be overidden when building. #pragma once #include #include #include )"""[1], }); } void ProduceGeneratedSourceFileHeader(CodegenOutput& output) { output.AddOutputThing(CodegenOutputThing{ // TODO we need to get the header name .text = &R"""( // This file is generated. Any changes will be overidden when building. #include #include #include #include using namespace std::literals; )"""[1], }); } } // namespace Utils