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