1 //===-- ResourceScriptStmt.h ------------------------------------*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===---------------------------------------------------------------------===// 8 // 9 // This lists all the resource and statement types occurring in RC scripts. 10 // 11 //===---------------------------------------------------------------------===// 12 13 #ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTSTMT_H 14 #define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTSTMT_H 15 16 #include "ResourceScriptToken.h" 17 #include "ResourceVisitor.h" 18 19 #include "llvm/ADT/BitVector.h" 20 #include "llvm/ADT/StringSet.h" 21 22 namespace llvm { 23 namespace rc { 24 25 // Integer wrapper that also holds information whether the user declared 26 // the integer to be long (by appending L to the end of the integer) or not. 27 // It allows to be implicitly cast from and to uint32_t in order 28 // to be compatible with the parts of code that don't care about the integers 29 // being marked long. 30 class RCInt { 31 uint32_t Val; 32 bool Long; 33 34 public: 35 RCInt(const RCToken &Token) 36 : Val(Token.intValue()), Long(Token.isLongInt()) {} 37 RCInt(uint32_t Value) : Val(Value), Long(false) {} 38 RCInt(uint32_t Value, bool IsLong) : Val(Value), Long(IsLong) {} 39 operator uint32_t() const { return Val; } 40 bool isLong() const { return Long; } 41 42 RCInt &operator+=(const RCInt &Rhs) { 43 std::tie(Val, Long) = std::make_pair(Val + Rhs.Val, Long | Rhs.Long); 44 return *this; 45 } 46 47 RCInt &operator-=(const RCInt &Rhs) { 48 std::tie(Val, Long) = std::make_pair(Val - Rhs.Val, Long | Rhs.Long); 49 return *this; 50 } 51 52 RCInt &operator|=(const RCInt &Rhs) { 53 std::tie(Val, Long) = std::make_pair(Val | Rhs.Val, Long | Rhs.Long); 54 return *this; 55 } 56 57 RCInt &operator&=(const RCInt &Rhs) { 58 std::tie(Val, Long) = std::make_pair(Val & Rhs.Val, Long | Rhs.Long); 59 return *this; 60 } 61 62 RCInt operator-() const { return {-Val, Long}; } 63 RCInt operator~() const { return {~Val, Long}; } 64 65 friend raw_ostream &operator<<(raw_ostream &OS, const RCInt &Int) { 66 return OS << Int.Val << (Int.Long ? "L" : ""); 67 } 68 }; 69 70 class IntWithNotMask { 71 private: 72 RCInt Value; 73 int32_t NotMask; 74 75 public: 76 IntWithNotMask() : IntWithNotMask(RCInt(0)) {} 77 IntWithNotMask(RCInt Value, int32_t NotMask = 0) : Value(Value), NotMask(NotMask) {} 78 79 RCInt getValue() const { 80 return Value; 81 } 82 83 uint32_t getNotMask() const { 84 return NotMask; 85 } 86 87 IntWithNotMask &operator+=(const IntWithNotMask &Rhs) { 88 Value &= ~Rhs.NotMask; 89 Value += Rhs.Value; 90 NotMask |= Rhs.NotMask; 91 return *this; 92 } 93 94 IntWithNotMask &operator-=(const IntWithNotMask &Rhs) { 95 Value &= ~Rhs.NotMask; 96 Value -= Rhs.Value; 97 NotMask |= Rhs.NotMask; 98 return *this; 99 } 100 101 IntWithNotMask &operator|=(const IntWithNotMask &Rhs) { 102 Value &= ~Rhs.NotMask; 103 Value |= Rhs.Value; 104 NotMask |= Rhs.NotMask; 105 return *this; 106 } 107 108 IntWithNotMask &operator&=(const IntWithNotMask &Rhs) { 109 Value &= ~Rhs.NotMask; 110 Value &= Rhs.Value; 111 NotMask |= Rhs.NotMask; 112 return *this; 113 } 114 115 IntWithNotMask operator-() const { return {-Value, NotMask}; } 116 IntWithNotMask operator~() const { return {~Value, 0}; } 117 118 friend raw_ostream &operator<<(raw_ostream &OS, const IntWithNotMask &Int) { 119 return OS << Int.Value; 120 } 121 }; 122 123 // A class holding a name - either an integer or a reference to the string. 124 class IntOrString { 125 private: 126 union Data { 127 RCInt Int; 128 StringRef String; 129 Data(RCInt Value) : Int(Value) {} 130 Data(const StringRef Value) : String(Value) {} 131 Data(const RCToken &Token) { 132 if (Token.kind() == RCToken::Kind::Int) 133 Int = RCInt(Token); 134 else 135 String = Token.value(); 136 } 137 } Data; 138 bool IsInt; 139 140 public: 141 IntOrString() : IntOrString(RCInt(0)) {} 142 IntOrString(uint32_t Value) : Data(Value), IsInt(true) {} 143 IntOrString(RCInt Value) : Data(Value), IsInt(true) {} 144 IntOrString(StringRef Value) : Data(Value), IsInt(false) {} 145 IntOrString(const RCToken &Token) 146 : Data(Token), IsInt(Token.kind() == RCToken::Kind::Int) {} 147 148 bool equalsLower(const char *Str) { 149 return !IsInt && Data.String.equals_insensitive(Str); 150 } 151 152 bool isInt() const { return IsInt; } 153 154 RCInt getInt() const { 155 assert(IsInt); 156 return Data.Int; 157 } 158 159 const StringRef &getString() const { 160 assert(!IsInt); 161 return Data.String; 162 } 163 164 operator Twine() const { 165 return isInt() ? Twine(getInt()) : Twine(getString()); 166 } 167 168 friend raw_ostream &operator<<(raw_ostream &, const IntOrString &); 169 }; 170 171 enum ResourceKind { 172 // These resource kinds have corresponding .res resource type IDs 173 // (TYPE in RESOURCEHEADER structure). The numeric value assigned to each 174 // kind is equal to this type ID. 175 RkNull = 0, 176 RkSingleCursor = 1, 177 RkBitmap = 2, 178 RkSingleIcon = 3, 179 RkMenu = 4, 180 RkDialog = 5, 181 RkStringTableBundle = 6, 182 RkAccelerators = 9, 183 RkRcData = 10, 184 RkCursorGroup = 12, 185 RkIconGroup = 14, 186 RkVersionInfo = 16, 187 RkHTML = 23, 188 189 // These kinds don't have assigned type IDs (they might be the resources 190 // of invalid kind, expand to many resource structures in .res files, 191 // or have variable type ID). In order to avoid ID clashes with IDs above, 192 // we assign the kinds the values 256 and larger. 193 RkInvalid = 256, 194 RkBase, 195 RkCursor, 196 RkIcon, 197 RkStringTable, 198 RkUser, 199 RkSingleCursorOrIconRes, 200 RkCursorOrIconGroupRes, 201 }; 202 203 // Non-zero memory flags. 204 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648027(v=vs.85).aspx 205 enum MemoryFlags { 206 MfMoveable = 0x10, 207 MfPure = 0x20, 208 MfPreload = 0x40, 209 MfDiscardable = 0x1000 210 }; 211 212 // Base resource. All the resources should derive from this base. 213 class RCResource { 214 public: 215 IntOrString ResName; 216 uint16_t MemoryFlags = getDefaultMemoryFlags(); 217 void setName(const IntOrString &Name) { ResName = Name; } 218 virtual raw_ostream &log(raw_ostream &OS) const { 219 return OS << "Base statement\n"; 220 }; 221 RCResource() {} 222 RCResource(uint16_t Flags) : MemoryFlags(Flags) {} 223 virtual ~RCResource() {} 224 225 virtual Error visit(Visitor *) const { 226 llvm_unreachable("This is unable to call methods from Visitor base"); 227 } 228 229 // Apply the statements attached to this resource. Generic resources 230 // don't have any. 231 virtual Error applyStmts(Visitor *) const { return Error::success(); } 232 233 // By default, memory flags are DISCARDABLE | PURE | MOVEABLE. 234 static uint16_t getDefaultMemoryFlags() { 235 return MfDiscardable | MfPure | MfMoveable; 236 } 237 238 virtual ResourceKind getKind() const { return RkBase; } 239 static bool classof(const RCResource *Res) { return true; } 240 241 virtual IntOrString getResourceType() const { 242 llvm_unreachable("This cannot be called on objects without types."); 243 } 244 virtual Twine getResourceTypeName() const { 245 llvm_unreachable("This cannot be called on objects without types."); 246 }; 247 }; 248 249 // An empty resource. It has no content, type 0, ID 0 and all of its 250 // characteristics are equal to 0. 251 class NullResource : public RCResource { 252 public: 253 NullResource() : RCResource(0) {} 254 raw_ostream &log(raw_ostream &OS) const override { 255 return OS << "Null resource\n"; 256 } 257 Error visit(Visitor *V) const override { return V->visitNullResource(this); } 258 IntOrString getResourceType() const override { return 0; } 259 Twine getResourceTypeName() const override { return "(NULL)"; } 260 }; 261 262 // Optional statement base. All such statements should derive from this base. 263 class OptionalStmt : public RCResource {}; 264 265 class OptionalStmtList : public OptionalStmt { 266 std::vector<std::unique_ptr<OptionalStmt>> Statements; 267 268 public: 269 OptionalStmtList() {} 270 raw_ostream &log(raw_ostream &OS) const override; 271 272 void addStmt(std::unique_ptr<OptionalStmt> Stmt) { 273 Statements.push_back(std::move(Stmt)); 274 } 275 276 Error visit(Visitor *V) const override { 277 for (auto &StmtPtr : Statements) 278 if (auto Err = StmtPtr->visit(V)) 279 return Err; 280 return Error::success(); 281 } 282 }; 283 284 class OptStatementsRCResource : public RCResource { 285 public: 286 std::unique_ptr<OptionalStmtList> OptStatements; 287 288 OptStatementsRCResource(OptionalStmtList &&Stmts, 289 uint16_t Flags = RCResource::getDefaultMemoryFlags()) 290 : RCResource(Flags), 291 OptStatements(std::make_unique<OptionalStmtList>(std::move(Stmts))) {} 292 293 Error applyStmts(Visitor *V) const override { 294 return OptStatements->visit(V); 295 } 296 }; 297 298 // LANGUAGE statement. It can occur both as a top-level statement (in such 299 // a situation, it changes the default language until the end of the file) 300 // and as an optional resource statement (then it changes the language 301 // of a single resource). 302 // 303 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381019(v=vs.85).aspx 304 class LanguageResource : public OptionalStmt { 305 public: 306 uint32_t Lang, SubLang; 307 308 LanguageResource(uint32_t LangId, uint32_t SubLangId) 309 : Lang(LangId), SubLang(SubLangId) {} 310 raw_ostream &log(raw_ostream &) const override; 311 312 // This is not a regular top-level statement; when it occurs, it just 313 // modifies the language context. 314 Error visit(Visitor *V) const override { return V->visitLanguageStmt(this); } 315 Twine getResourceTypeName() const override { return "LANGUAGE"; } 316 }; 317 318 // ACCELERATORS resource. Defines a named table of accelerators for the app. 319 // 320 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380610(v=vs.85).aspx 321 class AcceleratorsResource : public OptStatementsRCResource { 322 public: 323 class Accelerator { 324 public: 325 IntOrString Event; 326 uint32_t Id; 327 uint16_t Flags; 328 329 enum Options { 330 // This is actually 0x0000 (accelerator is assumed to be ASCII if it's 331 // not VIRTKEY). However, rc.exe behavior is different in situations 332 // "only ASCII defined" and "neither ASCII nor VIRTKEY defined". 333 // Therefore, we include ASCII as another flag. This must be zeroed 334 // when serialized. 335 ASCII = 0x8000, 336 VIRTKEY = 0x0001, 337 NOINVERT = 0x0002, 338 ALT = 0x0010, 339 SHIFT = 0x0004, 340 CONTROL = 0x0008 341 }; 342 343 static constexpr size_t NumFlags = 6; 344 static StringRef OptionsStr[NumFlags]; 345 static uint32_t OptionsFlags[NumFlags]; 346 }; 347 348 AcceleratorsResource(OptionalStmtList &&List, uint16_t Flags) 349 : OptStatementsRCResource(std::move(List), Flags) {} 350 351 std::vector<Accelerator> Accelerators; 352 353 void addAccelerator(IntOrString Event, uint32_t Id, uint16_t Flags) { 354 Accelerators.push_back(Accelerator{Event, Id, Flags}); 355 } 356 raw_ostream &log(raw_ostream &) const override; 357 358 IntOrString getResourceType() const override { return RkAccelerators; } 359 static uint16_t getDefaultMemoryFlags() { return MfPure | MfMoveable; } 360 Twine getResourceTypeName() const override { return "ACCELERATORS"; } 361 362 Error visit(Visitor *V) const override { 363 return V->visitAcceleratorsResource(this); 364 } 365 ResourceKind getKind() const override { return RkAccelerators; } 366 static bool classof(const RCResource *Res) { 367 return Res->getKind() == RkAccelerators; 368 } 369 }; 370 371 // BITMAP resource. Represents a bitmap (".bmp") file. 372 // 373 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380680(v=vs.85).aspx 374 class BitmapResource : public RCResource { 375 public: 376 StringRef BitmapLoc; 377 378 BitmapResource(StringRef Location, uint16_t Flags) 379 : RCResource(Flags), BitmapLoc(Location) {} 380 raw_ostream &log(raw_ostream &) const override; 381 382 IntOrString getResourceType() const override { return RkBitmap; } 383 static uint16_t getDefaultMemoryFlags() { return MfPure | MfMoveable; } 384 385 Twine getResourceTypeName() const override { return "BITMAP"; } 386 Error visit(Visitor *V) const override { 387 return V->visitBitmapResource(this); 388 } 389 ResourceKind getKind() const override { return RkBitmap; } 390 static bool classof(const RCResource *Res) { 391 return Res->getKind() == RkBitmap; 392 } 393 }; 394 395 // CURSOR resource. Represents a single cursor (".cur") file. 396 // 397 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380920(v=vs.85).aspx 398 class CursorResource : public RCResource { 399 public: 400 StringRef CursorLoc; 401 402 CursorResource(StringRef Location, uint16_t Flags) 403 : RCResource(Flags), CursorLoc(Location) {} 404 raw_ostream &log(raw_ostream &) const override; 405 406 Twine getResourceTypeName() const override { return "CURSOR"; } 407 static uint16_t getDefaultMemoryFlags() { return MfDiscardable | MfMoveable; } 408 Error visit(Visitor *V) const override { 409 return V->visitCursorResource(this); 410 } 411 ResourceKind getKind() const override { return RkCursor; } 412 static bool classof(const RCResource *Res) { 413 return Res->getKind() == RkCursor; 414 } 415 }; 416 417 // ICON resource. Represents a single ".ico" file containing a group of icons. 418 // 419 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381018(v=vs.85).aspx 420 class IconResource : public RCResource { 421 public: 422 StringRef IconLoc; 423 424 IconResource(StringRef Location, uint16_t Flags) 425 : RCResource(Flags), IconLoc(Location) {} 426 raw_ostream &log(raw_ostream &) const override; 427 428 Twine getResourceTypeName() const override { return "ICON"; } 429 static uint16_t getDefaultMemoryFlags() { return MfDiscardable | MfMoveable; } 430 Error visit(Visitor *V) const override { return V->visitIconResource(this); } 431 ResourceKind getKind() const override { return RkIcon; } 432 static bool classof(const RCResource *Res) { 433 return Res->getKind() == RkIcon; 434 } 435 }; 436 437 // HTML resource. Represents a local webpage that is to be embedded into the 438 // resulting resource file. It embeds a file only - no additional resources 439 // (images etc.) are included with this resource. 440 // 441 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa966018(v=vs.85).aspx 442 class HTMLResource : public RCResource { 443 public: 444 StringRef HTMLLoc; 445 446 HTMLResource(StringRef Location, uint16_t Flags) 447 : RCResource(Flags), HTMLLoc(Location) {} 448 raw_ostream &log(raw_ostream &) const override; 449 450 Error visit(Visitor *V) const override { return V->visitHTMLResource(this); } 451 452 // Curiously, file resources don't have DISCARDABLE flag set. 453 static uint16_t getDefaultMemoryFlags() { return MfPure | MfMoveable; } 454 IntOrString getResourceType() const override { return RkHTML; } 455 Twine getResourceTypeName() const override { return "HTML"; } 456 ResourceKind getKind() const override { return RkHTML; } 457 static bool classof(const RCResource *Res) { 458 return Res->getKind() == RkHTML; 459 } 460 }; 461 462 // -- MENU resource and its helper classes -- 463 // This resource describes the contents of an application menu 464 // (usually located in the upper part of the dialog.) 465 // 466 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381025(v=vs.85).aspx 467 468 // Description of a single submenu item. 469 class MenuDefinition { 470 public: 471 enum Options { 472 CHECKED = 0x0008, 473 GRAYED = 0x0001, 474 HELP = 0x4000, 475 INACTIVE = 0x0002, 476 MENUBARBREAK = 0x0020, 477 MENUBREAK = 0x0040 478 }; 479 480 enum MenuDefKind { MkBase, MkSeparator, MkMenuItem, MkPopup }; 481 482 static constexpr size_t NumFlags = 6; 483 static StringRef OptionsStr[NumFlags]; 484 static uint32_t OptionsFlags[NumFlags]; 485 static raw_ostream &logFlags(raw_ostream &, uint16_t Flags); 486 virtual raw_ostream &log(raw_ostream &OS) const { 487 return OS << "Base menu definition\n"; 488 } 489 virtual ~MenuDefinition() {} 490 491 virtual uint16_t getResFlags() const { return 0; } 492 virtual MenuDefKind getKind() const { return MkBase; } 493 }; 494 495 // Recursive description of a whole submenu. 496 class MenuDefinitionList : public MenuDefinition { 497 public: 498 std::vector<std::unique_ptr<MenuDefinition>> Definitions; 499 500 void addDefinition(std::unique_ptr<MenuDefinition> Def) { 501 Definitions.push_back(std::move(Def)); 502 } 503 raw_ostream &log(raw_ostream &) const override; 504 }; 505 506 // Separator in MENU definition (MENUITEM SEPARATOR). 507 // 508 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381024(v=vs.85).aspx 509 class MenuSeparator : public MenuDefinition { 510 public: 511 raw_ostream &log(raw_ostream &) const override; 512 513 MenuDefKind getKind() const override { return MkSeparator; } 514 static bool classof(const MenuDefinition *D) { 515 return D->getKind() == MkSeparator; 516 } 517 }; 518 519 // MENUITEM statement definition. 520 // 521 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381024(v=vs.85).aspx 522 class MenuItem : public MenuDefinition { 523 public: 524 StringRef Name; 525 uint32_t Id; 526 uint16_t Flags; 527 528 MenuItem(StringRef Caption, uint32_t ItemId, uint16_t ItemFlags) 529 : Name(Caption), Id(ItemId), Flags(ItemFlags) {} 530 raw_ostream &log(raw_ostream &) const override; 531 532 uint16_t getResFlags() const override { return Flags; } 533 MenuDefKind getKind() const override { return MkMenuItem; } 534 static bool classof(const MenuDefinition *D) { 535 return D->getKind() == MkMenuItem; 536 } 537 }; 538 539 // POPUP statement definition. 540 // 541 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381030(v=vs.85).aspx 542 class PopupItem : public MenuDefinition { 543 public: 544 StringRef Name; 545 uint16_t Flags; 546 MenuDefinitionList SubItems; 547 548 PopupItem(StringRef Caption, uint16_t ItemFlags, 549 MenuDefinitionList &&SubItemsList) 550 : Name(Caption), Flags(ItemFlags), SubItems(std::move(SubItemsList)) {} 551 raw_ostream &log(raw_ostream &) const override; 552 553 // This has an additional (0x10) flag. It doesn't match with documented 554 // 0x01 flag, though. 555 uint16_t getResFlags() const override { return Flags | 0x10; } 556 MenuDefKind getKind() const override { return MkPopup; } 557 static bool classof(const MenuDefinition *D) { 558 return D->getKind() == MkPopup; 559 } 560 }; 561 562 // Menu resource definition. 563 class MenuResource : public OptStatementsRCResource { 564 public: 565 MenuDefinitionList Elements; 566 567 MenuResource(OptionalStmtList &&OptStmts, MenuDefinitionList &&Items, 568 uint16_t Flags) 569 : OptStatementsRCResource(std::move(OptStmts), Flags), 570 Elements(std::move(Items)) {} 571 raw_ostream &log(raw_ostream &) const override; 572 573 IntOrString getResourceType() const override { return RkMenu; } 574 Twine getResourceTypeName() const override { return "MENU"; } 575 Error visit(Visitor *V) const override { return V->visitMenuResource(this); } 576 ResourceKind getKind() const override { return RkMenu; } 577 static bool classof(const RCResource *Res) { 578 return Res->getKind() == RkMenu; 579 } 580 }; 581 582 // STRINGTABLE resource. Contains a list of strings, each having its unique ID. 583 // 584 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381050(v=vs.85).aspx 585 class StringTableResource : public OptStatementsRCResource { 586 public: 587 std::vector<std::pair<uint32_t, std::vector<StringRef>>> Table; 588 589 StringTableResource(OptionalStmtList &&List, uint16_t Flags) 590 : OptStatementsRCResource(std::move(List), Flags) {} 591 void addStrings(uint32_t ID, std::vector<StringRef> &&Strings) { 592 Table.emplace_back(ID, Strings); 593 } 594 raw_ostream &log(raw_ostream &) const override; 595 Twine getResourceTypeName() const override { return "STRINGTABLE"; } 596 Error visit(Visitor *V) const override { 597 return V->visitStringTableResource(this); 598 } 599 }; 600 601 // -- DIALOG(EX) resource and its helper classes -- 602 // 603 // This resource describes dialog boxes and controls residing inside them. 604 // 605 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381003(v=vs.85).aspx 606 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381002(v=vs.85).aspx 607 608 // Single control definition. 609 class Control { 610 public: 611 StringRef Type; 612 IntOrString Title; 613 uint32_t ID, X, Y, Width, Height; 614 Optional<IntWithNotMask> Style; 615 Optional<uint32_t> ExtStyle, HelpID; 616 IntOrString Class; 617 618 // Control classes as described in DLGITEMTEMPLATEEX documentation. 619 // 620 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms645389.aspx 621 enum CtlClasses { 622 ClsButton = 0x80, 623 ClsEdit = 0x81, 624 ClsStatic = 0x82, 625 ClsListBox = 0x83, 626 ClsScrollBar = 0x84, 627 ClsComboBox = 0x85 628 }; 629 630 // Simple information about a single control type. 631 struct CtlInfo { 632 uint32_t Style; 633 uint16_t CtlClass; 634 bool HasTitle; 635 }; 636 637 Control(StringRef CtlType, IntOrString CtlTitle, uint32_t CtlID, 638 uint32_t PosX, uint32_t PosY, uint32_t ItemWidth, uint32_t ItemHeight, 639 Optional<IntWithNotMask> ItemStyle, Optional<uint32_t> ExtItemStyle, 640 Optional<uint32_t> CtlHelpID, IntOrString CtlClass) 641 : Type(CtlType), Title(CtlTitle), ID(CtlID), X(PosX), Y(PosY), 642 Width(ItemWidth), Height(ItemHeight), Style(ItemStyle), 643 ExtStyle(ExtItemStyle), HelpID(CtlHelpID), Class(CtlClass) {} 644 645 static const StringMap<CtlInfo> SupportedCtls; 646 647 raw_ostream &log(raw_ostream &) const; 648 }; 649 650 // Single dialog definition. We don't create distinct classes for DIALOG and 651 // DIALOGEX because of their being too similar to each other. We only have a 652 // flag determining the type of the dialog box. 653 class DialogResource : public OptStatementsRCResource { 654 public: 655 uint32_t X, Y, Width, Height, HelpID; 656 std::vector<Control> Controls; 657 bool IsExtended; 658 659 DialogResource(uint32_t PosX, uint32_t PosY, uint32_t DlgWidth, 660 uint32_t DlgHeight, uint32_t DlgHelpID, 661 OptionalStmtList &&OptStmts, bool IsDialogEx, uint16_t Flags) 662 : OptStatementsRCResource(std::move(OptStmts), Flags), X(PosX), Y(PosY), 663 Width(DlgWidth), Height(DlgHeight), HelpID(DlgHelpID), 664 IsExtended(IsDialogEx) {} 665 666 void addControl(Control &&Ctl) { Controls.push_back(std::move(Ctl)); } 667 668 raw_ostream &log(raw_ostream &) const override; 669 670 // It was a weird design decision to assign the same resource type number 671 // both for DIALOG and DIALOGEX (and the same structure version number). 672 // It makes it possible for DIALOG to be mistaken for DIALOGEX. 673 IntOrString getResourceType() const override { return RkDialog; } 674 Twine getResourceTypeName() const override { 675 return "DIALOG" + Twine(IsExtended ? "EX" : ""); 676 } 677 Error visit(Visitor *V) const override { 678 return V->visitDialogResource(this); 679 } 680 ResourceKind getKind() const override { return RkDialog; } 681 static bool classof(const RCResource *Res) { 682 return Res->getKind() == RkDialog; 683 } 684 }; 685 686 // User-defined resource. It is either: 687 // * a link to the file, e.g. NAME TYPE "filename", 688 // * or contains a list of integers and strings, e.g. NAME TYPE {1, "a", 2}. 689 class UserDefinedResource : public RCResource { 690 public: 691 IntOrString Type; 692 StringRef FileLoc; 693 std::vector<IntOrString> Contents; 694 bool IsFileResource; 695 696 UserDefinedResource(IntOrString ResourceType, StringRef FileLocation, 697 uint16_t Flags) 698 : RCResource(Flags), Type(ResourceType), FileLoc(FileLocation), 699 IsFileResource(true) {} 700 UserDefinedResource(IntOrString ResourceType, std::vector<IntOrString> &&Data, 701 uint16_t Flags) 702 : RCResource(Flags), Type(ResourceType), Contents(std::move(Data)), 703 IsFileResource(false) {} 704 705 raw_ostream &log(raw_ostream &) const override; 706 IntOrString getResourceType() const override { return Type; } 707 Twine getResourceTypeName() const override { return Type; } 708 static uint16_t getDefaultMemoryFlags() { return MfPure | MfMoveable; } 709 710 Error visit(Visitor *V) const override { 711 return V->visitUserDefinedResource(this); 712 } 713 ResourceKind getKind() const override { return RkUser; } 714 static bool classof(const RCResource *Res) { 715 return Res->getKind() == RkUser; 716 } 717 }; 718 719 // -- VERSIONINFO resource and its helper classes -- 720 // 721 // This resource lists the version information on the executable/library. 722 // The declaration consists of the following items: 723 // * A number of fixed optional version statements (e.g. FILEVERSION, FILEOS) 724 // * BEGIN 725 // * A number of BLOCK and/or VALUE statements. BLOCK recursively defines 726 // another block of version information, whereas VALUE defines a 727 // key -> value correspondence. There might be more than one value 728 // corresponding to the single key. 729 // * END 730 // 731 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381058(v=vs.85).aspx 732 733 // A single VERSIONINFO statement; 734 class VersionInfoStmt { 735 public: 736 enum StmtKind { StBase = 0, StBlock = 1, StValue = 2 }; 737 738 virtual raw_ostream &log(raw_ostream &OS) const { return OS << "VI stmt\n"; } 739 virtual ~VersionInfoStmt() {} 740 741 virtual StmtKind getKind() const { return StBase; } 742 static bool classof(const VersionInfoStmt *S) { 743 return S->getKind() == StBase; 744 } 745 }; 746 747 // BLOCK definition; also the main VERSIONINFO declaration is considered a 748 // BLOCK, although it has no name. 749 // The correct top-level blocks are "VarFileInfo" and "StringFileInfo". We don't 750 // care about them at the parsing phase. 751 class VersionInfoBlock : public VersionInfoStmt { 752 public: 753 std::vector<std::unique_ptr<VersionInfoStmt>> Stmts; 754 StringRef Name; 755 756 VersionInfoBlock(StringRef BlockName) : Name(BlockName) {} 757 void addStmt(std::unique_ptr<VersionInfoStmt> Stmt) { 758 Stmts.push_back(std::move(Stmt)); 759 } 760 raw_ostream &log(raw_ostream &) const override; 761 762 StmtKind getKind() const override { return StBlock; } 763 static bool classof(const VersionInfoStmt *S) { 764 return S->getKind() == StBlock; 765 } 766 }; 767 768 class VersionInfoValue : public VersionInfoStmt { 769 public: 770 StringRef Key; 771 std::vector<IntOrString> Values; 772 BitVector HasPrecedingComma; 773 774 VersionInfoValue(StringRef InfoKey, std::vector<IntOrString> &&Vals, 775 BitVector &&CommasBeforeVals) 776 : Key(InfoKey), Values(std::move(Vals)), 777 HasPrecedingComma(std::move(CommasBeforeVals)) {} 778 raw_ostream &log(raw_ostream &) const override; 779 780 StmtKind getKind() const override { return StValue; } 781 static bool classof(const VersionInfoStmt *S) { 782 return S->getKind() == StValue; 783 } 784 }; 785 786 class VersionInfoResource : public RCResource { 787 public: 788 // A class listing fixed VERSIONINFO statements (occuring before main BEGIN). 789 // If any of these is not specified, it is assumed by the original tool to 790 // be equal to 0. 791 class VersionInfoFixed { 792 public: 793 enum VersionInfoFixedType { 794 FtUnknown, 795 FtFileVersion, 796 FtProductVersion, 797 FtFileFlagsMask, 798 FtFileFlags, 799 FtFileOS, 800 FtFileType, 801 FtFileSubtype, 802 FtNumTypes 803 }; 804 805 private: 806 static const StringMap<VersionInfoFixedType> FixedFieldsInfoMap; 807 static const StringRef FixedFieldsNames[FtNumTypes]; 808 809 public: 810 SmallVector<uint32_t, 4> FixedInfo[FtNumTypes]; 811 SmallVector<bool, FtNumTypes> IsTypePresent; 812 813 static VersionInfoFixedType getFixedType(StringRef Type); 814 static bool isTypeSupported(VersionInfoFixedType Type); 815 static bool isVersionType(VersionInfoFixedType Type); 816 817 VersionInfoFixed() : IsTypePresent(FtNumTypes, false) {} 818 819 void setValue(VersionInfoFixedType Type, ArrayRef<uint32_t> Value) { 820 FixedInfo[Type] = SmallVector<uint32_t, 4>(Value.begin(), Value.end()); 821 IsTypePresent[Type] = true; 822 } 823 824 raw_ostream &log(raw_ostream &) const; 825 }; 826 827 VersionInfoBlock MainBlock; 828 VersionInfoFixed FixedData; 829 830 VersionInfoResource(VersionInfoBlock &&TopLevelBlock, 831 VersionInfoFixed &&FixedInfo, uint16_t Flags) 832 : RCResource(Flags), MainBlock(std::move(TopLevelBlock)), 833 FixedData(std::move(FixedInfo)) {} 834 835 raw_ostream &log(raw_ostream &) const override; 836 IntOrString getResourceType() const override { return RkVersionInfo; } 837 static uint16_t getDefaultMemoryFlags() { return MfMoveable | MfPure; } 838 Twine getResourceTypeName() const override { return "VERSIONINFO"; } 839 Error visit(Visitor *V) const override { 840 return V->visitVersionInfoResource(this); 841 } 842 ResourceKind getKind() const override { return RkVersionInfo; } 843 static bool classof(const RCResource *Res) { 844 return Res->getKind() == RkVersionInfo; 845 } 846 }; 847 848 // CHARACTERISTICS optional statement. 849 // 850 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380872(v=vs.85).aspx 851 class CharacteristicsStmt : public OptionalStmt { 852 public: 853 uint32_t Value; 854 855 CharacteristicsStmt(uint32_t Characteristic) : Value(Characteristic) {} 856 raw_ostream &log(raw_ostream &) const override; 857 858 Twine getResourceTypeName() const override { return "CHARACTERISTICS"; } 859 Error visit(Visitor *V) const override { 860 return V->visitCharacteristicsStmt(this); 861 } 862 }; 863 864 // VERSION optional statement. 865 // 866 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381059(v=vs.85).aspx 867 class VersionStmt : public OptionalStmt { 868 public: 869 uint32_t Value; 870 871 VersionStmt(uint32_t Version) : Value(Version) {} 872 raw_ostream &log(raw_ostream &) const override; 873 874 Twine getResourceTypeName() const override { return "VERSION"; } 875 Error visit(Visitor *V) const override { return V->visitVersionStmt(this); } 876 }; 877 878 // CAPTION optional statement. 879 // 880 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380778(v=vs.85).aspx 881 class CaptionStmt : public OptionalStmt { 882 public: 883 StringRef Value; 884 885 CaptionStmt(StringRef Caption) : Value(Caption) {} 886 raw_ostream &log(raw_ostream &) const override; 887 Twine getResourceTypeName() const override { return "CAPTION"; } 888 Error visit(Visitor *V) const override { return V->visitCaptionStmt(this); } 889 }; 890 891 // FONT optional statement. 892 // Note that the documentation is inaccurate: it expects five arguments to be 893 // given, however the example provides only two. In fact, the original tool 894 // expects two arguments - point size and name of the typeface. 895 // 896 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381013(v=vs.85).aspx 897 class FontStmt : public OptionalStmt { 898 public: 899 uint32_t Size, Weight, Charset; 900 StringRef Name; 901 bool Italic; 902 903 FontStmt(uint32_t FontSize, StringRef FontName, uint32_t FontWeight, 904 bool FontItalic, uint32_t FontCharset) 905 : Size(FontSize), Weight(FontWeight), Charset(FontCharset), 906 Name(FontName), Italic(FontItalic) {} 907 raw_ostream &log(raw_ostream &) const override; 908 Twine getResourceTypeName() const override { return "FONT"; } 909 Error visit(Visitor *V) const override { return V->visitFontStmt(this); } 910 }; 911 912 // STYLE optional statement. 913 // 914 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa381051(v=vs.85).aspx 915 class StyleStmt : public OptionalStmt { 916 public: 917 uint32_t Value; 918 919 StyleStmt(uint32_t Style) : Value(Style) {} 920 raw_ostream &log(raw_ostream &) const override; 921 Twine getResourceTypeName() const override { return "STYLE"; } 922 Error visit(Visitor *V) const override { return V->visitStyleStmt(this); } 923 }; 924 925 // EXSTYLE optional statement. 926 // 927 // Ref: docs.microsoft.com/en-us/windows/desktop/menurc/exstyle-statement 928 class ExStyleStmt : public OptionalStmt { 929 public: 930 uint32_t Value; 931 932 ExStyleStmt(uint32_t ExStyle) : Value(ExStyle) {} 933 raw_ostream &log(raw_ostream &) const override; 934 Twine getResourceTypeName() const override { return "EXSTYLE"; } 935 Error visit(Visitor *V) const override { return V->visitExStyleStmt(this); } 936 }; 937 938 // CLASS optional statement. 939 // 940 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380883(v=vs.85).aspx 941 class ClassStmt : public OptionalStmt { 942 public: 943 IntOrString Value; 944 945 ClassStmt(IntOrString Class) : Value(Class) {} 946 raw_ostream &log(raw_ostream &) const override; 947 Twine getResourceTypeName() const override { return "CLASS"; } 948 Error visit(Visitor *V) const override { return V->visitClassStmt(this); } 949 }; 950 951 } // namespace rc 952 } // namespace llvm 953 954 #endif 955