Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 1 | //===-- ResourceSerializator.h ----------------------------------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines a visitor serializing resources to a .res stream. |
| 11 | // |
| 12 | //===---------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_TOOLS_LLVMRC_RESOURCESERIALIZATOR_H |
| 15 | #define LLVM_TOOLS_LLVMRC_RESOURCESERIALIZATOR_H |
| 16 | |
| 17 | #include "ResourceScriptStmt.h" |
| 18 | #include "ResourceVisitor.h" |
| 19 | |
| 20 | #include "llvm/Support/Endian.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace rc { |
| 24 | |
| 25 | class ResourceFileWriter : public Visitor { |
| 26 | public: |
| 27 | ResourceFileWriter(std::unique_ptr<raw_fd_ostream> Stream) |
Zachary Turner | c3ab013 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 28 | : FS(std::move(Stream)), IconCursorID(1) { |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 29 | assert(FS && "Output stream needs to be provided to the serializator"); |
| 30 | } |
| 31 | |
| 32 | Error visitNullResource(const RCResource *) override; |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 33 | Error visitAcceleratorsResource(const RCResource *) override; |
Zachary Turner | c3ab013 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 34 | Error visitCursorResource(const RCResource *) override; |
Marek Sokolowski | 7f7745c | 2017-09-30 00:38:52 +0000 | [diff] [blame] | 35 | Error visitDialogResource(const RCResource *) override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 36 | Error visitHTMLResource(const RCResource *) override; |
Zachary Turner | c3ab013 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 37 | Error visitIconResource(const RCResource *) override; |
Marek Sokolowski | 42f494d | 2017-09-29 22:25:05 +0000 | [diff] [blame] | 38 | Error visitMenuResource(const RCResource *) override; |
Zachary Turner | 07bc04f | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 39 | Error visitVersionInfoResource(const RCResource *) override; |
Zachary Turner | da36669 | 2017-10-06 21:30:55 +0000 | [diff] [blame^] | 40 | Error visitStringTableResource(const RCResource *) override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 41 | |
Zachary Turner | 420090a | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 42 | Error visitCaptionStmt(const CaptionStmt *) override; |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 43 | Error visitCharacteristicsStmt(const CharacteristicsStmt *) override; |
Zachary Turner | 420090a | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 44 | Error visitFontStmt(const FontStmt *) override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 45 | Error visitLanguageStmt(const LanguageResource *) override; |
Zachary Turner | 420090a | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 46 | Error visitStyleStmt(const StyleStmt *) override; |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 47 | Error visitVersionStmt(const VersionStmt *) override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 48 | |
Zachary Turner | da36669 | 2017-10-06 21:30:55 +0000 | [diff] [blame^] | 49 | // Stringtables are output at the end of .res file. We need a separate |
| 50 | // function to do it. |
| 51 | Error dumpAllStringTables(); |
| 52 | |
| 53 | bool AppendNull; // Append '\0' to each existing STRINGTABLE element? |
| 54 | |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 55 | struct ObjectInfo { |
| 56 | uint16_t LanguageInfo; |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 57 | uint32_t Characteristics; |
| 58 | uint32_t VersionInfo; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 59 | |
Zachary Turner | 420090a | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 60 | Optional<uint32_t> Style; |
| 61 | StringRef Caption; |
| 62 | struct FontInfo { |
| 63 | uint32_t Size; |
| 64 | StringRef Typeface; |
| 65 | uint32_t Weight; |
| 66 | bool IsItalic; |
| 67 | uint32_t Charset; |
| 68 | }; |
| 69 | Optional<FontInfo> Font; |
| 70 | |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 71 | ObjectInfo() : LanguageInfo(0), Characteristics(0), VersionInfo(0) {} |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 72 | } ObjectData; |
| 73 | |
Zachary Turner | da36669 | 2017-10-06 21:30:55 +0000 | [diff] [blame^] | 74 | struct StringTableInfo { |
| 75 | // Each STRINGTABLE bundle depends on ID of the bundle and language |
| 76 | // description. |
| 77 | using BundleKey = std::pair<uint16_t, uint16_t>; |
| 78 | // Each bundle is in fact an array of 16 strings. |
| 79 | struct Bundle { |
| 80 | std::array<Optional<StringRef>, 16> Data; |
| 81 | ObjectInfo DeclTimeInfo; |
| 82 | Bundle(const ObjectInfo &Info) : DeclTimeInfo(Info) {} |
| 83 | }; |
| 84 | std::map<BundleKey, Bundle> BundleData; |
| 85 | // Bundles are listed in the order of their first occurence. |
| 86 | std::vector<BundleKey> BundleList; |
| 87 | } StringTableData; |
| 88 | |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 89 | private: |
| 90 | Error handleError(Error &&Err, const RCResource *Res); |
| 91 | |
| 92 | Error |
| 93 | writeResource(const RCResource *Res, |
| 94 | Error (ResourceFileWriter::*BodyWriter)(const RCResource *)); |
| 95 | |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 96 | // NullResource |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 97 | Error writeNullBody(const RCResource *); |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 98 | |
| 99 | // AcceleratorsResource |
| 100 | Error writeSingleAccelerator(const AcceleratorsResource::Accelerator &, |
| 101 | bool IsLastItem); |
| 102 | Error writeAcceleratorsBody(const RCResource *); |
| 103 | |
Zachary Turner | c3ab013 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 104 | // CursorResource and IconResource |
| 105 | Error visitIconOrCursorResource(const RCResource *); |
| 106 | Error visitIconOrCursorGroup(const RCResource *); |
| 107 | Error visitSingleIconOrCursor(const RCResource *); |
| 108 | Error writeSingleIconOrCursorBody(const RCResource *); |
| 109 | Error writeIconOrCursorGroupBody(const RCResource *); |
| 110 | |
Marek Sokolowski | 7f7745c | 2017-09-30 00:38:52 +0000 | [diff] [blame] | 111 | // DialogResource |
| 112 | Error writeSingleDialogControl(const Control &, bool IsExtended); |
| 113 | Error writeDialogBody(const RCResource *); |
| 114 | |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 115 | // HTMLResource |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 116 | Error writeHTMLBody(const RCResource *); |
| 117 | |
Marek Sokolowski | 42f494d | 2017-09-29 22:25:05 +0000 | [diff] [blame] | 118 | // MenuResource |
| 119 | Error writeMenuDefinition(const std::unique_ptr<MenuDefinition> &, |
| 120 | uint16_t Flags); |
| 121 | Error writeMenuDefinitionList(const MenuDefinitionList &List); |
| 122 | Error writeMenuBody(const RCResource *); |
| 123 | |
Zachary Turner | da36669 | 2017-10-06 21:30:55 +0000 | [diff] [blame^] | 124 | // StringTableResource |
| 125 | Error visitStringTableBundle(const RCResource *); |
| 126 | Error writeStringTableBundleBody(const RCResource *); |
| 127 | Error insertStringIntoBundle(StringTableInfo::Bundle &Bundle, |
| 128 | uint16_t StringID, StringRef String); |
| 129 | |
Zachary Turner | 07bc04f | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 130 | // VersionInfoResource |
| 131 | Error writeVersionInfoBody(const RCResource *); |
| 132 | Error writeVersionInfoBlock(const VersionInfoBlock &); |
| 133 | Error writeVersionInfoValue(const VersionInfoValue &); |
| 134 | |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 135 | // Output stream handling. |
| 136 | std::unique_ptr<raw_fd_ostream> FS; |
| 137 | |
| 138 | uint64_t tell() const { return FS->tell(); } |
| 139 | |
| 140 | uint64_t writeObject(const ArrayRef<uint8_t> Data); |
| 141 | |
| 142 | template <typename T> uint64_t writeInt(const T &Value) { |
| 143 | support::detail::packed_endian_specific_integral<T, support::little, |
| 144 | support::unaligned> |
| 145 | Object(Value); |
| 146 | return writeObject(Object); |
| 147 | } |
| 148 | |
| 149 | template <typename T> uint64_t writeObject(const T &Value) { |
| 150 | return writeObject(ArrayRef<uint8_t>( |
| 151 | reinterpret_cast<const uint8_t *>(&Value), sizeof(T))); |
| 152 | } |
| 153 | |
| 154 | template <typename T> void writeObjectAt(const T &Value, uint64_t Position) { |
| 155 | FS->pwrite((const char *)&Value, sizeof(T), Position); |
| 156 | } |
| 157 | |
| 158 | Error writeCString(StringRef Str, bool WriteTerminator = true); |
| 159 | |
| 160 | Error writeIdentifier(const IntOrString &Ident); |
| 161 | Error writeIntOrString(const IntOrString &Data); |
| 162 | |
Zachary Turner | 07bc04f | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 163 | void writeRCInt(RCInt); |
| 164 | |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 165 | Error appendFile(StringRef Filename); |
| 166 | |
| 167 | void padStream(uint64_t Length); |
Zachary Turner | c3ab013 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 168 | |
| 169 | // Icon and cursor IDs are allocated starting from 1 and increasing for |
| 170 | // each icon/cursor dumped. This maintains the current ID to be allocated. |
| 171 | uint16_t IconCursorID; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | } // namespace rc |
| 175 | } // namespace llvm |
| 176 | |
| 177 | #endif |