Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 1 | //===-- ResourceScriptStmt.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 lists all the resource and statement types occurring in RC scripts. |
| 11 | // |
| 12 | //===---------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTSTMT_H |
| 15 | #define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTSTMT_H |
| 16 | |
| 17 | #include "ResourceScriptToken.h" |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 18 | #include "ResourceVisitor.h" |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 19 | |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringSet.h" |
| 21 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | namespace rc { |
| 24 | |
| 25 | // A class holding a name - either an integer or a reference to the string. |
| 26 | class IntOrString { |
| 27 | private: |
| 28 | union Data { |
| 29 | uint32_t Int; |
| 30 | StringRef String; |
| 31 | Data(uint32_t Value) : Int(Value) {} |
| 32 | Data(const StringRef Value) : String(Value) {} |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 33 | Data(const RCToken &Token) { |
| 34 | if (Token.kind() == RCToken::Kind::Int) |
| 35 | Int = Token.intValue(); |
| 36 | else |
| 37 | String = Token.value(); |
| 38 | } |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 39 | } Data; |
| 40 | bool IsInt; |
| 41 | |
| 42 | public: |
| 43 | IntOrString() : IntOrString(0) {} |
| 44 | IntOrString(uint32_t Value) : Data(Value), IsInt(1) {} |
| 45 | IntOrString(StringRef Value) : Data(Value), IsInt(0) {} |
| 46 | IntOrString(const RCToken &Token) |
| 47 | : Data(Token), IsInt(Token.kind() == RCToken::Kind::Int) {} |
| 48 | |
| 49 | bool equalsLower(const char *Str) { |
| 50 | return !IsInt && Data.String.equals_lower(Str); |
| 51 | } |
| 52 | |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 53 | bool isInt() const { return IsInt; } |
| 54 | |
| 55 | uint32_t getInt() const { |
| 56 | assert(IsInt); |
| 57 | return Data.Int; |
| 58 | } |
| 59 | |
| 60 | const StringRef &getString() const { |
| 61 | assert(!IsInt); |
| 62 | return Data.String; |
| 63 | } |
| 64 | |
| 65 | operator Twine() const { |
| 66 | return isInt() ? Twine(getInt()) : Twine(getString()); |
| 67 | } |
| 68 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 69 | friend raw_ostream &operator<<(raw_ostream &, const IntOrString &); |
| 70 | }; |
| 71 | |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 72 | enum ResourceKind { |
| 73 | // These resource kinds have corresponding .res resource type IDs |
| 74 | // (TYPE in RESOURCEHEADER structure). The numeric value assigned to each |
| 75 | // kind is equal to this type ID. |
| 76 | RkNull = 0, |
| 77 | RkMenu = 4, |
| 78 | RkDialog = 5, |
| 79 | RkAccelerators = 9, |
| 80 | RkVersionInfo = 16, |
| 81 | RkHTML = 23, |
| 82 | |
| 83 | // These kinds don't have assigned type IDs (they might be the resources |
| 84 | // of invalid kind, expand to many resource structures in .res files, |
| 85 | // or have variable type ID). In order to avoid ID clashes with IDs above, |
| 86 | // we assign the kinds the values 256 and larger. |
| 87 | RkInvalid = 256, |
| 88 | RkBase, |
| 89 | RkCursor, |
| 90 | RkIcon, |
| 91 | RkUser |
| 92 | }; |
| 93 | |
| 94 | // Non-zero memory flags. |
| 95 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648027(v=vs.85).aspx |
| 96 | enum MemoryFlags { |
| 97 | MfMoveable = 0x10, |
| 98 | MfPure = 0x20, |
| 99 | MfPreload = 0x40, |
| 100 | MfDiscardable = 0x1000 |
| 101 | }; |
| 102 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 103 | // Base resource. All the resources should derive from this base. |
| 104 | class RCResource { |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 105 | public: |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 106 | IntOrString ResName; |
| 107 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 108 | RCResource() = default; |
| 109 | RCResource(RCResource &&) = default; |
| 110 | void setName(const IntOrString &Name) { ResName = Name; } |
| 111 | virtual raw_ostream &log(raw_ostream &OS) const { |
| 112 | return OS << "Base statement\n"; |
| 113 | }; |
| 114 | virtual ~RCResource() {} |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 115 | |
| 116 | virtual Error visit(Visitor *) const { |
| 117 | llvm_unreachable("This is unable to call methods from Visitor base"); |
| 118 | } |
| 119 | |
| 120 | // By default, memory flags are DISCARDABLE | PURE | MOVEABLE. |
| 121 | virtual uint16_t getMemoryFlags() const { |
| 122 | return MfDiscardable | MfPure | MfMoveable; |
| 123 | } |
| 124 | virtual ResourceKind getKind() const { return RkBase; } |
| 125 | static bool classof(const RCResource *Res) { return true; } |
| 126 | |
| 127 | virtual IntOrString getResourceType() const { |
| 128 | llvm_unreachable("This cannot be called on objects without types."); |
| 129 | } |
| 130 | virtual Twine getResourceTypeName() const { |
| 131 | llvm_unreachable("This cannot be called on objects without types."); |
| 132 | }; |
| 133 | }; |
| 134 | |
| 135 | // An empty resource. It has no content, type 0, ID 0 and all of its |
| 136 | // characteristics are equal to 0. |
| 137 | class NullResource : public RCResource { |
| 138 | public: |
| 139 | raw_ostream &log(raw_ostream &OS) const override { |
| 140 | return OS << "Null resource\n"; |
| 141 | } |
| 142 | Error visit(Visitor *V) const override { return V->visitNullResource(this); } |
| 143 | IntOrString getResourceType() const override { return 0; } |
| 144 | Twine getResourceTypeName() const override { return "(NULL)"; } |
| 145 | uint16_t getMemoryFlags() const override { return 0; } |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | // Optional statement base. All such statements should derive from this base. |
| 149 | class OptionalStmt : public RCResource {}; |
| 150 | |
| 151 | class OptionalStmtList : public OptionalStmt { |
| 152 | std::vector<std::unique_ptr<OptionalStmt>> Statements; |
| 153 | |
| 154 | public: |
| 155 | OptionalStmtList() {} |
| 156 | virtual raw_ostream &log(raw_ostream &OS) const; |
| 157 | |
| 158 | void addStmt(std::unique_ptr<OptionalStmt> Stmt) { |
| 159 | Statements.push_back(std::move(Stmt)); |
| 160 | } |
| 161 | }; |
| 162 | |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 163 | class OptStatementsRCResource : public RCResource { |
| 164 | public: |
| 165 | std::unique_ptr<OptionalStmtList> OptStatements; |
| 166 | |
| 167 | OptStatementsRCResource(OptionalStmtList &&Stmts) |
| 168 | : OptStatements(llvm::make_unique<OptionalStmtList>(std::move(Stmts))) {} |
| 169 | }; |
| 170 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 171 | // LANGUAGE statement. It can occur both as a top-level statement (in such |
| 172 | // a situation, it changes the default language until the end of the file) |
| 173 | // and as an optional resource statement (then it changes the language |
| 174 | // of a single resource). |
| 175 | // |
| 176 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381019(v=vs.85).aspx |
| 177 | class LanguageResource : public OptionalStmt { |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 178 | public: |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 179 | uint32_t Lang, SubLang; |
| 180 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 181 | LanguageResource(uint32_t LangId, uint32_t SubLangId) |
| 182 | : Lang(LangId), SubLang(SubLangId) {} |
| 183 | raw_ostream &log(raw_ostream &) const override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 184 | |
| 185 | // This is not a regular top-level statement; when it occurs, it just |
| 186 | // modifies the language context. |
| 187 | Error visit(Visitor *V) const override { return V->visitLanguageStmt(this); } |
| 188 | Twine getResourceTypeName() const override { return "LANGUAGE"; } |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 191 | // ACCELERATORS resource. Defines a named table of accelerators for the app. |
| 192 | // |
| 193 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380610(v=vs.85).aspx |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 194 | class AcceleratorsResource : public OptStatementsRCResource { |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 195 | public: |
| 196 | class Accelerator { |
| 197 | public: |
| 198 | IntOrString Event; |
| 199 | uint32_t Id; |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 200 | uint16_t Flags; |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 201 | |
| 202 | enum Options { |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 203 | // This is actually 0x0000 (accelerator is assumed to be ASCII if it's |
| 204 | // not VIRTKEY). However, rc.exe behavior is different in situations |
| 205 | // "only ASCII defined" and "neither ASCII nor VIRTKEY defined". |
| 206 | // Therefore, we include ASCII as another flag. This must be zeroed |
| 207 | // when serialized. |
| 208 | ASCII = 0x8000, |
| 209 | VIRTKEY = 0x0001, |
| 210 | NOINVERT = 0x0002, |
| 211 | ALT = 0x0010, |
| 212 | SHIFT = 0x0004, |
| 213 | CONTROL = 0x0008 |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | static constexpr size_t NumFlags = 6; |
| 217 | static StringRef OptionsStr[NumFlags]; |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 218 | static uint32_t OptionsFlags[NumFlags]; |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 221 | using OptStatementsRCResource::OptStatementsRCResource; |
| 222 | void addAccelerator(IntOrString Event, uint32_t Id, uint16_t Flags) { |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 223 | Accelerators.push_back(Accelerator{Event, Id, Flags}); |
| 224 | } |
| 225 | raw_ostream &log(raw_ostream &) const override; |
| 226 | |
| 227 | private: |
| 228 | std::vector<Accelerator> Accelerators; |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
Marek Sokolowski | 72aa937 | 2017-08-28 21:59:54 +0000 | [diff] [blame] | 231 | // CURSOR resource. Represents a single cursor (".cur") file. |
| 232 | // |
| 233 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380920(v=vs.85).aspx |
| 234 | class CursorResource : public RCResource { |
| 235 | StringRef CursorLoc; |
| 236 | |
| 237 | public: |
| 238 | CursorResource(StringRef Location) : CursorLoc(Location) {} |
| 239 | raw_ostream &log(raw_ostream &) const override; |
| 240 | }; |
| 241 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 242 | // ICON resource. Represents a single ".ico" file containing a group of icons. |
| 243 | // |
| 244 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381018(v=vs.85).aspx |
| 245 | class IconResource : public RCResource { |
| 246 | StringRef IconLoc; |
| 247 | |
| 248 | public: |
| 249 | IconResource(StringRef Location) : IconLoc(Location) {} |
| 250 | raw_ostream &log(raw_ostream &) const override; |
| 251 | }; |
| 252 | |
Marek Sokolowski | 72aa937 | 2017-08-28 21:59:54 +0000 | [diff] [blame] | 253 | // HTML resource. Represents a local webpage that is to be embedded into the |
| 254 | // resulting resource file. It embeds a file only - no additional resources |
| 255 | // (images etc.) are included with this resource. |
| 256 | // |
| 257 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa966018(v=vs.85).aspx |
| 258 | class HTMLResource : public RCResource { |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 259 | public: |
Marek Sokolowski | 72aa937 | 2017-08-28 21:59:54 +0000 | [diff] [blame] | 260 | StringRef HTMLLoc; |
| 261 | |
Marek Sokolowski | 72aa937 | 2017-08-28 21:59:54 +0000 | [diff] [blame] | 262 | HTMLResource(StringRef Location) : HTMLLoc(Location) {} |
| 263 | raw_ostream &log(raw_ostream &) const override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 264 | |
| 265 | Error visit(Visitor *V) const override { return V->visitHTMLResource(this); } |
| 266 | |
| 267 | // Curiously, file resources don't have DISCARDABLE flag set. |
| 268 | uint16_t getMemoryFlags() const override { return MfPure | MfMoveable; } |
| 269 | IntOrString getResourceType() const override { return RkHTML; } |
| 270 | Twine getResourceTypeName() const override { return "HTML"; } |
| 271 | ResourceKind getKind() const override { return RkHTML; } |
| 272 | static bool classof(const RCResource *Res) { |
| 273 | return Res->getKind() == RkHTML; |
| 274 | } |
Marek Sokolowski | 72aa937 | 2017-08-28 21:59:54 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 277 | // -- MENU resource and its helper classes -- |
| 278 | // This resource describes the contents of an application menu |
| 279 | // (usually located in the upper part of the dialog.) |
| 280 | // |
| 281 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381025(v=vs.85).aspx |
| 282 | |
| 283 | // Description of a single submenu item. |
| 284 | class MenuDefinition { |
| 285 | public: |
| 286 | enum Options { |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 287 | CHECKED = 0x0008, |
| 288 | GRAYED = 0x0001, |
| 289 | HELP = 0x4000, |
| 290 | INACTIVE = 0x0002, |
| 291 | MENUBARBREAK = 0x0020, |
| 292 | MENUBREAK = 0x0040 |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | static constexpr size_t NumFlags = 6; |
| 296 | static StringRef OptionsStr[NumFlags]; |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 297 | static uint32_t OptionsFlags[NumFlags]; |
| 298 | static raw_ostream &logFlags(raw_ostream &, uint16_t Flags); |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 299 | virtual raw_ostream &log(raw_ostream &OS) const { |
| 300 | return OS << "Base menu definition\n"; |
| 301 | } |
| 302 | virtual ~MenuDefinition() {} |
| 303 | }; |
| 304 | |
| 305 | // Recursive description of a whole submenu. |
| 306 | class MenuDefinitionList : public MenuDefinition { |
| 307 | std::vector<std::unique_ptr<MenuDefinition>> Definitions; |
| 308 | |
| 309 | public: |
| 310 | void addDefinition(std::unique_ptr<MenuDefinition> Def) { |
| 311 | Definitions.push_back(std::move(Def)); |
| 312 | } |
| 313 | raw_ostream &log(raw_ostream &) const override; |
| 314 | }; |
| 315 | |
| 316 | // Separator in MENU definition (MENUITEM SEPARATOR). |
| 317 | // |
| 318 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381024(v=vs.85).aspx |
| 319 | class MenuSeparator : public MenuDefinition { |
| 320 | public: |
| 321 | raw_ostream &log(raw_ostream &) const override; |
| 322 | }; |
| 323 | |
| 324 | // MENUITEM statement definition. |
| 325 | // |
| 326 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381024(v=vs.85).aspx |
| 327 | class MenuItem : public MenuDefinition { |
| 328 | StringRef Name; |
| 329 | uint32_t Id; |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 330 | uint16_t Flags; |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 331 | |
| 332 | public: |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 333 | MenuItem(StringRef Caption, uint32_t ItemId, uint16_t ItemFlags) |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 334 | : Name(Caption), Id(ItemId), Flags(ItemFlags) {} |
| 335 | raw_ostream &log(raw_ostream &) const override; |
| 336 | }; |
| 337 | |
| 338 | // POPUP statement definition. |
| 339 | // |
| 340 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381030(v=vs.85).aspx |
| 341 | class PopupItem : public MenuDefinition { |
| 342 | StringRef Name; |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 343 | uint16_t Flags; |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 344 | MenuDefinitionList SubItems; |
| 345 | |
| 346 | public: |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 347 | PopupItem(StringRef Caption, uint16_t ItemFlags, |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 348 | MenuDefinitionList &&SubItemsList) |
| 349 | : Name(Caption), Flags(ItemFlags), SubItems(std::move(SubItemsList)) {} |
| 350 | raw_ostream &log(raw_ostream &) const override; |
| 351 | }; |
| 352 | |
| 353 | // Menu resource definition. |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 354 | class MenuResource : public OptStatementsRCResource { |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 355 | MenuDefinitionList Elements; |
| 356 | |
| 357 | public: |
| 358 | MenuResource(OptionalStmtList &&OptStmts, MenuDefinitionList &&Items) |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 359 | : OptStatementsRCResource(std::move(OptStmts)), |
| 360 | Elements(std::move(Items)) {} |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 361 | raw_ostream &log(raw_ostream &) const override; |
| 362 | }; |
| 363 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 364 | // STRINGTABLE resource. Contains a list of strings, each having its unique ID. |
| 365 | // |
| 366 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381050(v=vs.85).aspx |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 367 | class StringTableResource : public OptStatementsRCResource { |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 368 | std::vector<std::pair<uint32_t, StringRef>> Table; |
| 369 | |
| 370 | public: |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 371 | using OptStatementsRCResource::OptStatementsRCResource; |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 372 | void addString(uint32_t ID, StringRef String) { |
| 373 | Table.emplace_back(ID, String); |
| 374 | } |
| 375 | raw_ostream &log(raw_ostream &) const override; |
| 376 | }; |
| 377 | |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 378 | // -- DIALOG(EX) resource and its helper classes -- |
| 379 | // |
| 380 | // This resource describes dialog boxes and controls residing inside them. |
| 381 | // |
| 382 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381003(v=vs.85).aspx |
| 383 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381002(v=vs.85).aspx |
| 384 | |
| 385 | // Single control definition. |
| 386 | class Control { |
| 387 | StringRef Type, Title; |
| 388 | uint32_t ID, X, Y, Width, Height; |
| 389 | Optional<uint32_t> Style, ExtStyle, HelpID; |
| 390 | |
| 391 | public: |
| 392 | Control(StringRef CtlType, StringRef CtlTitle, uint32_t CtlID, uint32_t PosX, |
| 393 | uint32_t PosY, uint32_t ItemWidth, uint32_t ItemHeight, |
| 394 | Optional<uint32_t> ItemStyle, Optional<uint32_t> ExtItemStyle, |
| 395 | Optional<uint32_t> CtlHelpID) |
| 396 | : Type(CtlType), Title(CtlTitle), ID(CtlID), X(PosX), Y(PosY), |
| 397 | Width(ItemWidth), Height(ItemHeight), Style(ItemStyle), |
| 398 | ExtStyle(ExtItemStyle), HelpID(CtlHelpID) {} |
| 399 | |
| 400 | static const StringSet<> SupportedCtls; |
| 401 | static const StringSet<> CtlsWithTitle; |
| 402 | |
| 403 | raw_ostream &log(raw_ostream &) const; |
| 404 | }; |
| 405 | |
| 406 | // Single dialog definition. We don't create distinct classes for DIALOG and |
| 407 | // DIALOGEX because of their being too similar to each other. We only have a |
| 408 | // flag determining the type of the dialog box. |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 409 | class DialogResource : public OptStatementsRCResource { |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 410 | uint32_t X, Y, Width, Height, HelpID; |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 411 | std::vector<Control> Controls; |
| 412 | bool IsExtended; |
| 413 | |
| 414 | public: |
| 415 | DialogResource(uint32_t PosX, uint32_t PosY, uint32_t DlgWidth, |
| 416 | uint32_t DlgHeight, uint32_t DlgHelpID, |
| 417 | OptionalStmtList &&OptStmts, bool IsDialogEx) |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame^] | 418 | : OptStatementsRCResource(std::move(OptStmts)), X(PosX), Y(PosY), |
| 419 | Width(DlgWidth), Height(DlgHeight), HelpID(DlgHelpID), |
| 420 | IsExtended(IsDialogEx) {} |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 421 | |
| 422 | void addControl(Control &&Ctl) { Controls.push_back(std::move(Ctl)); } |
| 423 | |
| 424 | raw_ostream &log(raw_ostream &) const override; |
| 425 | }; |
| 426 | |
Marek Sokolowski | b5f39a0 | 2017-09-29 00:14:18 +0000 | [diff] [blame] | 427 | // User-defined resource. It is either: |
| 428 | // * a link to the file, e.g. NAME TYPE "filename", |
| 429 | // * or contains a list of integers and strings, e.g. NAME TYPE {1, "a", 2}. |
| 430 | class UserDefinedResource : public RCResource { |
| 431 | IntOrString Type; |
| 432 | StringRef FileLoc; |
| 433 | std::vector<IntOrString> Contents; |
| 434 | bool IsFileResource; |
| 435 | |
| 436 | public: |
| 437 | UserDefinedResource(IntOrString ResourceType, StringRef FileLocation) |
| 438 | : Type(ResourceType), FileLoc(FileLocation), IsFileResource(true) {} |
| 439 | UserDefinedResource(IntOrString ResourceType, std::vector<IntOrString> &&Data) |
| 440 | : Type(ResourceType), Contents(std::move(Data)), IsFileResource(false) {} |
| 441 | |
| 442 | raw_ostream &log(raw_ostream &) const override; |
| 443 | }; |
| 444 | |
Marek Sokolowski | fb74cb1 | 2017-09-28 22:41:38 +0000 | [diff] [blame] | 445 | // -- VERSIONINFO resource and its helper classes -- |
| 446 | // |
| 447 | // This resource lists the version information on the executable/library. |
| 448 | // The declaration consists of the following items: |
| 449 | // * A number of fixed optional version statements (e.g. FILEVERSION, FILEOS) |
| 450 | // * BEGIN |
| 451 | // * A number of BLOCK and/or VALUE statements. BLOCK recursively defines |
| 452 | // another block of version information, whereas VALUE defines a |
| 453 | // key -> value correspondence. There might be more than one value |
| 454 | // corresponding to the single key. |
| 455 | // * END |
| 456 | // |
| 457 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381058(v=vs.85).aspx |
| 458 | |
| 459 | // A single VERSIONINFO statement; |
| 460 | class VersionInfoStmt { |
| 461 | public: |
| 462 | virtual raw_ostream &log(raw_ostream &OS) const { return OS << "VI stmt\n"; } |
| 463 | virtual ~VersionInfoStmt() {} |
| 464 | }; |
| 465 | |
| 466 | // BLOCK definition; also the main VERSIONINFO declaration is considered a |
| 467 | // BLOCK, although it has no name. |
| 468 | // The correct top-level blocks are "VarFileInfo" and "StringFileInfo". We don't |
| 469 | // care about them at the parsing phase. |
| 470 | class VersionInfoBlock : public VersionInfoStmt { |
| 471 | std::vector<std::unique_ptr<VersionInfoStmt>> Stmts; |
| 472 | StringRef Name; |
| 473 | |
| 474 | public: |
| 475 | VersionInfoBlock(StringRef BlockName) : Name(BlockName) {} |
| 476 | void addStmt(std::unique_ptr<VersionInfoStmt> Stmt) { |
| 477 | Stmts.push_back(std::move(Stmt)); |
| 478 | } |
| 479 | raw_ostream &log(raw_ostream &) const override; |
| 480 | }; |
| 481 | |
| 482 | class VersionInfoValue : public VersionInfoStmt { |
| 483 | StringRef Key; |
| 484 | std::vector<IntOrString> Values; |
| 485 | |
| 486 | public: |
| 487 | VersionInfoValue(StringRef InfoKey, std::vector<IntOrString> &&Vals) |
| 488 | : Key(InfoKey), Values(std::move(Vals)) {} |
| 489 | raw_ostream &log(raw_ostream &) const override; |
| 490 | }; |
| 491 | |
| 492 | class VersionInfoResource : public RCResource { |
| 493 | public: |
| 494 | // A class listing fixed VERSIONINFO statements (occuring before main BEGIN). |
| 495 | // If any of these is not specified, it is assumed by the original tool to |
| 496 | // be equal to 0. |
| 497 | class VersionInfoFixed { |
| 498 | public: |
| 499 | enum VersionInfoFixedType { |
| 500 | FtUnknown, |
| 501 | FtFileVersion, |
| 502 | FtProductVersion, |
| 503 | FtFileFlagsMask, |
| 504 | FtFileFlags, |
| 505 | FtFileOS, |
| 506 | FtFileType, |
| 507 | FtFileSubtype, |
| 508 | FtNumTypes |
| 509 | }; |
| 510 | |
| 511 | private: |
| 512 | static const StringMap<VersionInfoFixedType> FixedFieldsInfoMap; |
| 513 | static const StringRef FixedFieldsNames[FtNumTypes]; |
| 514 | |
| 515 | public: |
| 516 | SmallVector<uint32_t, 4> FixedInfo[FtNumTypes]; |
| 517 | SmallVector<bool, FtNumTypes> IsTypePresent; |
| 518 | |
| 519 | static VersionInfoFixedType getFixedType(StringRef Type); |
| 520 | static bool isTypeSupported(VersionInfoFixedType Type); |
| 521 | static bool isVersionType(VersionInfoFixedType Type); |
| 522 | |
| 523 | VersionInfoFixed() : IsTypePresent(FtNumTypes, false) {} |
| 524 | |
| 525 | void setValue(VersionInfoFixedType Type, ArrayRef<uint32_t> Value) { |
| 526 | FixedInfo[Type] = SmallVector<uint32_t, 4>(Value.begin(), Value.end()); |
| 527 | IsTypePresent[Type] = true; |
| 528 | } |
| 529 | |
| 530 | raw_ostream &log(raw_ostream &) const; |
| 531 | }; |
| 532 | |
| 533 | private: |
| 534 | VersionInfoBlock MainBlock; |
| 535 | VersionInfoFixed FixedData; |
| 536 | |
| 537 | public: |
| 538 | VersionInfoResource(VersionInfoBlock &&TopLevelBlock, |
| 539 | VersionInfoFixed &&FixedInfo) |
| 540 | : MainBlock(std::move(TopLevelBlock)), FixedData(std::move(FixedInfo)) {} |
| 541 | |
| 542 | raw_ostream &log(raw_ostream &) const override; |
| 543 | }; |
| 544 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 545 | // CHARACTERISTICS optional statement. |
| 546 | // |
| 547 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380872(v=vs.85).aspx |
| 548 | class CharacteristicsStmt : public OptionalStmt { |
| 549 | uint32_t Value; |
| 550 | |
| 551 | public: |
| 552 | CharacteristicsStmt(uint32_t Characteristic) : Value(Characteristic) {} |
| 553 | raw_ostream &log(raw_ostream &) const override; |
| 554 | }; |
| 555 | |
| 556 | // VERSION optional statement. |
| 557 | // |
| 558 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381059(v=vs.85).aspx |
| 559 | class VersionStmt : public OptionalStmt { |
| 560 | uint32_t Value; |
| 561 | |
| 562 | public: |
| 563 | VersionStmt(uint32_t Version) : Value(Version) {} |
| 564 | raw_ostream &log(raw_ostream &) const override; |
| 565 | }; |
| 566 | |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 567 | // CAPTION optional statement. |
| 568 | // |
| 569 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380778(v=vs.85).aspx |
| 570 | class CaptionStmt : public OptionalStmt { |
| 571 | StringRef Value; |
| 572 | |
| 573 | public: |
| 574 | CaptionStmt(StringRef Caption) : Value(Caption) {} |
| 575 | raw_ostream &log(raw_ostream &) const override; |
| 576 | }; |
| 577 | |
| 578 | // FONT optional statement. |
| 579 | // Note that the documentation is inaccurate: it expects five arguments to be |
| 580 | // given, however the example provides only two. In fact, the original tool |
| 581 | // expects two arguments - point size and name of the typeface. |
| 582 | // |
| 583 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381013(v=vs.85).aspx |
| 584 | class FontStmt : public OptionalStmt { |
| 585 | uint32_t Size; |
| 586 | StringRef Typeface; |
| 587 | |
| 588 | public: |
| 589 | FontStmt(uint32_t FontSize, StringRef FontName) |
| 590 | : Size(FontSize), Typeface(FontName) {} |
| 591 | raw_ostream &log(raw_ostream &) const override; |
| 592 | }; |
| 593 | |
| 594 | // STYLE optional statement. |
| 595 | // |
| 596 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381051(v=vs.85).aspx |
| 597 | class StyleStmt : public OptionalStmt { |
| 598 | uint32_t Value; |
| 599 | |
| 600 | public: |
| 601 | StyleStmt(uint32_t Style) : Value(Style) {} |
| 602 | raw_ostream &log(raw_ostream &) const override; |
| 603 | }; |
| 604 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 605 | } // namespace rc |
| 606 | } // namespace llvm |
| 607 | |
| 608 | #endif |