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