ResourceScriptStmt.h 30 KB

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