1 //
2 //                     The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===---------------------------------------------------------------------===//
8 //
9 // This implements methods defined in ResourceScriptStmt.h.
10 //
11 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380599(v=vs.85).aspx
12 //
13 //===---------------------------------------------------------------------===//
14 
15 #include "ResourceScriptStmt.h"
16 
17 namespace llvm {
18 namespace rc {
19 
operator <<(raw_ostream & OS,const IntOrString & Item)20 raw_ostream &operator<<(raw_ostream &OS, const IntOrString &Item) {
21   if (Item.IsInt)
22     return OS << Item.Data.Int;
23   else
24     return OS << Item.Data.String;
25 }
26 
log(raw_ostream & OS) const27 raw_ostream &OptionalStmtList::log(raw_ostream &OS) const {
28   for (const auto &Stmt : Statements) {
29     OS << "  Option: ";
30     Stmt->log(OS);
31   }
32   return OS;
33 }
34 
log(raw_ostream & OS) const35 raw_ostream &LanguageResource::log(raw_ostream &OS) const {
36   return OS << "Language: " << Lang << ", Sublanguage: " << SubLang << "\n";
37 }
38 
39 StringRef AcceleratorsResource::Accelerator::OptionsStr
40     [AcceleratorsResource::Accelerator::NumFlags] = {
41         "ASCII", "VIRTKEY", "NOINVERT", "ALT", "SHIFT", "CONTROL"};
42 
43 uint32_t AcceleratorsResource::Accelerator::OptionsFlags
44     [AcceleratorsResource::Accelerator::NumFlags] = {ASCII, VIRTKEY, NOINVERT,
45                                                      ALT,   SHIFT,   CONTROL};
46 
log(raw_ostream & OS) const47 raw_ostream &AcceleratorsResource::log(raw_ostream &OS) const {
48   OS << "Accelerators (" << ResName << "): \n";
49   OptStatements->log(OS);
50   for (const auto &Acc : Accelerators) {
51     OS << "  Accelerator: " << Acc.Event << " " << Acc.Id;
52     for (size_t i = 0; i < Accelerator::NumFlags; ++i)
53       if (Acc.Flags & Accelerator::OptionsFlags[i])
54         OS << " " << Accelerator::OptionsStr[i];
55     OS << "\n";
56   }
57   return OS;
58 }
59 
log(raw_ostream & OS) const60 raw_ostream &BitmapResource::log(raw_ostream &OS) const {
61   return OS << "Bitmap (" << ResName << "): " << BitmapLoc << "\n";
62 }
63 
log(raw_ostream & OS) const64 raw_ostream &CursorResource::log(raw_ostream &OS) const {
65   return OS << "Cursor (" << ResName << "): " << CursorLoc << "\n";
66 }
67 
log(raw_ostream & OS) const68 raw_ostream &IconResource::log(raw_ostream &OS) const {
69   return OS << "Icon (" << ResName << "): " << IconLoc << "\n";
70 }
71 
log(raw_ostream & OS) const72 raw_ostream &HTMLResource::log(raw_ostream &OS) const {
73   return OS << "HTML (" << ResName << "): " << HTMLLoc << "\n";
74 }
75 
76 StringRef MenuDefinition::OptionsStr[MenuDefinition::NumFlags] = {
77     "CHECKED", "GRAYED", "HELP", "INACTIVE", "MENUBARBREAK", "MENUBREAK"};
78 
79 uint32_t MenuDefinition::OptionsFlags[MenuDefinition::NumFlags] = {
80     CHECKED, GRAYED, HELP, INACTIVE, MENUBARBREAK, MENUBREAK};
81 
logFlags(raw_ostream & OS,uint16_t Flags)82 raw_ostream &MenuDefinition::logFlags(raw_ostream &OS, uint16_t Flags) {
83   for (size_t i = 0; i < NumFlags; ++i)
84     if (Flags & OptionsFlags[i])
85       OS << " " << OptionsStr[i];
86   return OS;
87 }
88 
log(raw_ostream & OS) const89 raw_ostream &MenuDefinitionList::log(raw_ostream &OS) const {
90   OS << "  Menu list starts\n";
91   for (auto &Item : Definitions)
92     Item->log(OS);
93   return OS << "  Menu list ends\n";
94 }
95 
log(raw_ostream & OS) const96 raw_ostream &MenuItem::log(raw_ostream &OS) const {
97   OS << "  MenuItem (" << Name << "), ID = " << Id;
98   logFlags(OS, Flags);
99   return OS << "\n";
100 }
101 
log(raw_ostream & OS) const102 raw_ostream &MenuSeparator::log(raw_ostream &OS) const {
103   return OS << "  Menu separator\n";
104 }
105 
log(raw_ostream & OS) const106 raw_ostream &PopupItem::log(raw_ostream &OS) const {
107   OS << "  Popup (" << Name << ")";
108   logFlags(OS, Flags);
109   OS << ":\n";
110   return SubItems.log(OS);
111 }
112 
log(raw_ostream & OS) const113 raw_ostream &MenuResource::log(raw_ostream &OS) const {
114   OS << "Menu (" << ResName << "):\n";
115   OptStatements->log(OS);
116   return Elements.log(OS);
117 }
118 
log(raw_ostream & OS) const119 raw_ostream &StringTableResource::log(raw_ostream &OS) const {
120   OS << "StringTable:\n";
121   OptStatements->log(OS);
122   for (const auto &String : Table)
123     OS << "  " << String.first << " => " << String.second << "\n";
124   return OS;
125 }
126 
127 const StringMap<Control::CtlInfo> Control::SupportedCtls = {
128     {"LTEXT", CtlInfo{0x50020000, ClsStatic, true}},
129     {"CTEXT", CtlInfo{0x50020001, ClsStatic, true}},
130     {"RTEXT", CtlInfo{0x50020002, ClsStatic, true}},
131     {"ICON", CtlInfo{0x50000003, ClsStatic, true}},
132     {"PUSHBUTTON", CtlInfo{0x50010000, ClsButton, true}},
133     {"DEFPUSHBUTTON", CtlInfo{0x50010001, ClsButton, true}},
134     {"AUTO3STATE", CtlInfo{0x50010006, ClsButton, true}},
135     {"AUTOCHECKBOX", CtlInfo{0x50010003, ClsButton, true}},
136     {"AUTORADIOBUTTON", CtlInfo{0x50000009, ClsButton, true}},
137     {"CHECKBOX", CtlInfo{0x50010002, ClsButton, true}},
138     {"GROUPBOX", CtlInfo{0x50000007, ClsButton, true}},
139     {"RADIOBUTTON", CtlInfo{0x50000004, ClsButton, true}},
140     {"STATE3", CtlInfo{0x50010005, ClsButton, true}},
141     {"PUSHBOX", CtlInfo{0x5001000A, ClsButton, true}},
142     {"EDITTEXT", CtlInfo{0x50810000, ClsEdit, false}},
143     {"COMBOBOX", CtlInfo{0x50000000, ClsComboBox, false}},
144     {"LISTBOX", CtlInfo{0x50800001, ClsListBox, false}},
145     {"SCROLLBAR", CtlInfo{0x50000000, ClsScrollBar, false}},
146     {"CONTROL", CtlInfo{0x50000000, 0, true}},
147 };
148 
log(raw_ostream & OS) const149 raw_ostream &Control::log(raw_ostream &OS) const {
150   OS << "  Control (" << ID << "): " << Type << ", title: " << Title
151      << ", loc: (" << X << ", " << Y << "), size: [" << Width << ", " << Height
152      << "]";
153   if (Style)
154     OS << ", style: " << *Style;
155   if (ExtStyle)
156     OS << ", ext. style: " << *ExtStyle;
157   if (HelpID)
158     OS << ", help ID: " << *HelpID;
159   return OS << "\n";
160 }
161 
log(raw_ostream & OS) const162 raw_ostream &DialogResource::log(raw_ostream &OS) const {
163   OS << "Dialog" << (IsExtended ? "Ex" : "") << " (" << ResName << "): loc: ("
164      << X << ", " << Y << "), size: [" << Width << ", " << Height
165      << "], help ID: " << HelpID << "\n";
166   OptStatements->log(OS);
167   for (auto &Ctl : Controls)
168     Ctl.log(OS);
169   return OS;
170 }
171 
log(raw_ostream & OS) const172 raw_ostream &VersionInfoBlock::log(raw_ostream &OS) const {
173   OS << "  Start of block (name: " << Name << ")\n";
174   for (auto &Stmt : Stmts)
175     Stmt->log(OS);
176   return OS << "  End of block\n";
177 }
178 
log(raw_ostream & OS) const179 raw_ostream &VersionInfoValue::log(raw_ostream &OS) const {
180   OS << "  " << Key << " =>";
181   size_t NumValues = Values.size();
182   for (size_t Id = 0; Id < NumValues; ++Id) {
183     if (Id > 0 && HasPrecedingComma[Id])
184       OS << ",";
185     OS << " " << Values[Id];
186   }
187   return OS << "\n";
188 }
189 
190 using VersionInfoFixed = VersionInfoResource::VersionInfoFixed;
191 using VersionInfoFixedType = VersionInfoFixed::VersionInfoFixedType;
192 
193 const StringRef
194     VersionInfoFixed::FixedFieldsNames[VersionInfoFixed::FtNumTypes] = {
195         "",          "FILEVERSION", "PRODUCTVERSION", "FILEFLAGSMASK",
196         "FILEFLAGS", "FILEOS",      "FILETYPE",       "FILESUBTYPE"};
197 
198 const StringMap<VersionInfoFixedType> VersionInfoFixed::FixedFieldsInfoMap = {
199     {FixedFieldsNames[FtFileVersion], FtFileVersion},
200     {FixedFieldsNames[FtProductVersion], FtProductVersion},
201     {FixedFieldsNames[FtFileFlagsMask], FtFileFlagsMask},
202     {FixedFieldsNames[FtFileFlags], FtFileFlags},
203     {FixedFieldsNames[FtFileOS], FtFileOS},
204     {FixedFieldsNames[FtFileType], FtFileType},
205     {FixedFieldsNames[FtFileSubtype], FtFileSubtype}};
206 
getFixedType(StringRef Type)207 VersionInfoFixedType VersionInfoFixed::getFixedType(StringRef Type) {
208   auto UpperType = Type.upper();
209   auto Iter = FixedFieldsInfoMap.find(UpperType);
210   if (Iter != FixedFieldsInfoMap.end())
211     return Iter->getValue();
212   return FtUnknown;
213 }
214 
isTypeSupported(VersionInfoFixedType Type)215 bool VersionInfoFixed::isTypeSupported(VersionInfoFixedType Type) {
216   return FtUnknown < Type && Type < FtNumTypes;
217 }
218 
isVersionType(VersionInfoFixedType Type)219 bool VersionInfoFixed::isVersionType(VersionInfoFixedType Type) {
220   switch (Type) {
221   case FtFileVersion:
222   case FtProductVersion:
223     return true;
224 
225   default:
226     return false;
227   }
228 }
229 
log(raw_ostream & OS) const230 raw_ostream &VersionInfoFixed::log(raw_ostream &OS) const {
231   for (int Type = FtUnknown; Type < FtNumTypes; ++Type) {
232     if (!isTypeSupported((VersionInfoFixedType)Type))
233       continue;
234     OS << "  Fixed: " << FixedFieldsNames[Type] << ":";
235     for (uint32_t Val : FixedInfo[Type])
236       OS << " " << Val;
237     OS << "\n";
238   }
239   return OS;
240 }
241 
log(raw_ostream & OS) const242 raw_ostream &VersionInfoResource::log(raw_ostream &OS) const {
243   OS << "VersionInfo (" << ResName << "):\n";
244   FixedData.log(OS);
245   return MainBlock.log(OS);
246 }
247 
log(raw_ostream & OS) const248 raw_ostream &UserDefinedResource::log(raw_ostream &OS) const {
249   OS << "User-defined (type: " << Type << ", name: " << ResName << "): ";
250   if (IsFileResource)
251     return OS << FileLoc << "\n";
252   OS << "data = ";
253   for (auto &Item : Contents)
254     OS << Item << " ";
255   return OS << "\n";
256 }
257 
log(raw_ostream & OS) const258 raw_ostream &CharacteristicsStmt::log(raw_ostream &OS) const {
259   return OS << "Characteristics: " << Value << "\n";
260 }
261 
log(raw_ostream & OS) const262 raw_ostream &VersionStmt::log(raw_ostream &OS) const {
263   return OS << "Version: " << Value << "\n";
264 }
265 
log(raw_ostream & OS) const266 raw_ostream &CaptionStmt::log(raw_ostream &OS) const {
267   return OS << "Caption: " << Value << "\n";
268 }
269 
log(raw_ostream & OS) const270 raw_ostream &ClassStmt::log(raw_ostream &OS) const {
271   return OS << "Class: " << Value << "\n";
272 }
273 
log(raw_ostream & OS) const274 raw_ostream &FontStmt::log(raw_ostream &OS) const {
275   OS << "Font: size = " << Size << ", face = " << Name
276      << ", weight = " << Weight;
277   if (Italic)
278     OS << ", italic";
279   return OS << ", charset = " << Charset << "\n";
280 }
281 
log(raw_ostream & OS) const282 raw_ostream &StyleStmt::log(raw_ostream &OS) const {
283   return OS << "Style: " << Value << "\n";
284 }
285 
286 } // namespace rc
287 } // namespace llvm
288