OSDN Git Service

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