Cthulhu  0.2.10
Cthulhu compiler collection
info.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 #include "stdafx.hpp"
3 
4 #include "editor/utils.hpp"
5 
6 #include "editor/panels/info.hpp"
7 
8 #include "core/macros.h"
9 
10 #include "cthulhu/broker/broker.h"
11 #include "interop/compile.h"
12 
13 using namespace ed;
14 
15 // helpers
16 
17 static const ImGuiTableFlags kCallbackTableFlags
18  = ImGuiTableFlags_BordersV
19  | ImGuiTableFlags_BordersOuterH
20  | ImGuiTableFlags_Resizable
21  | ImGuiTableFlags_RowBg
22  | ImGuiTableFlags_NoHostExtendX
23  | ImGuiTableFlags_NoBordersInBody;
24 
25 static std::string_view from_text_view(const text_view_t& view)
26 {
27  return std::string_view(view.text, view.length);
28 }
29 
30 static void draw_version(const char *id, ctu_version_t version)
31 {
32  int major = CT_VERSION_MAJOR(version);
33  int minor = CT_VERSION_MINOR(version);
34  int patch = CT_VERSION_PATCH(version);
35 
36  ImGui::Text("%s: %d.%d.%d", id, major, minor, patch);
37 }
38 
39 static bool begin_table_node(const void *ptr_id, const char *label, int columns, ImGuiTableFlags flags)
40 {
41  if (ImGui::TreeNode(ptr_id, "%s", label))
42  {
43  if (ImGui::BeginTable(label, columns, flags))
44  {
45  return true;
46  }
47  else
48  {
49  ImGui::TreePop();
50  }
51  }
52 
53  return false;
54 }
55 
56 static void end_table_node()
57 {
58  ImGui::EndTable();
59  ImGui::TreePop();
60 }
61 
62 static void draw_diagnostics(diagnostic_list_t list)
63 {
64  if (begin_table_node((void*)&list, "Diagnostics", 4, kCallbackTableFlags))
65  {
66  ImGui::TableSetupColumn("ID");
67  ImGui::TableSetupColumn("Severity");
68  ImGui::TableSetupColumn("Brief");
69  ImGui::TableSetupColumn("Description");
70  ImGui::TableHeadersRow();
71 
72  for (size_t i = 0; i < list.count; i++)
73  {
74  const diagnostic_t *diag = list.diagnostics[i];
75  ImGui::TableNextRow();
76  ImGui::TableNextColumn();
77  ImGui::TextUnformatted(diag->id);
78 
79  ImGui::TableNextColumn();
80  ImGui::TextUnformatted(severity_string(diag->severity));
81 
82  ImGui::TableNextColumn();
83  ImGui::TextUnformatted(diag->brief ? diag->brief : "no brief");
84 
85  ImGui::TableNextColumn();
86  ImGui::TextUnformatted(diag->description ? diag->description : "no description");
87  }
88 
89  end_table_node();
90  }
91 }
92 
93 static void draw_feature(const char *name, bool supported)
94 {
95  ImGui::TextUnformatted(name);
96  ImGui::SameLine();
97  if (supported)
98  {
99  ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Available");
100  }
101  else
102  {
103  ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Not Available");
104  }
105 }
106 
107 // module info
108 
110  : IEditorPanel(info.name)
111  , info(info)
112 { }
113 
115 {
116  ImGui::Text("ID: %s", info.id);
117 
118  version_info_t version = info.version;
119  ImGui::Text("License: %s", version.license);
120  ImGui::Text("Description: %s", version.desc);
121  ImGui::Text("Author: %s", version.author);
122  draw_version("Version", version.version);
123  draw_diagnostics(info.diagnostics);
124 }
125 
126 // frontend info
127 
129  : ModuleInfoPanel(frontend.info)
130  , frontend(frontend)
131 { }
132 
133 void FrontendInfoPanel::draw_content()
134 {
136  CT_UNUSED(frontend); // TODO: once theres more info to display, use it here
137 }
138 
139 // language info
140 
142  : ModuleInfoPanel(lang.info)
143  , lang(lang)
144 {
145  builtin = from_text_view(lang.builtin.name);
146  for (char& c : builtin) if (c == '\0') c = '/';
147 
148  for (size_t i = 0; lang.exts[i]; i++)
149  {
150  if (i > 0) extensions += ", ";
151  extensions += lang.exts[i];
152  }
153 }
154 
155 static float get_tooltip_width()
156 {
157  ImVec2 ttsize = ImGui::CalcTextSize("(?)");
158  return ttsize.x * 2;
159 }
160 
161 static void draw_function_address(const void *pfn)
162 {
163  if (pfn == nullptr)
164  {
165  ImVec4 orange = ImVec4(1.0f, 0.5f, 0.0f, 1.0f);
166  ImGui::TextColored(orange, "Not provided");
167  }
168  else
169  {
170  ImGui::Text("0x%p", pfn);
171  }
172 }
173 
174 static void draw_row(const char *name, const void *pfn, const char *tooltip)
175 {
176  ImGui::TableNextRow();
177  ImGui::TableNextColumn();
178  ImGui::TextDisabled("(?)");
179  ImGui::SetItemTooltip("%s", tooltip);
180 
181  ImGui::TableNextColumn();
182  ImGui::TextUnformatted(name);
183 
184  ImGui::TableNextColumn();
185  draw_function_address(pfn);
186 }
187 
188 void LanguageInfoPanel::draw_content()
189 {
190  float ttwidth = get_tooltip_width();
192  ImGui::Text("Default extensions: %s", extensions.c_str());
193 
194  ImGui::Text("Context struct size: %zu", lang.context_size);
195  ImGui::Text("AST struct size: %zu", lang.ast_size);
196 
197  if (begin_table_node((void*)&lang.builtin, "Builtin", 2, kCallbackTableFlags))
198  {
199  ImGui::TableSetupColumn("Name");
200  ImGui::TableSetupColumn("Size");
201  ImGui::TableHeadersRow();
202 
203  for (size_t i = 0; i < lang.builtin.length; i++)
204  {
205  const char *id = "no name";
206  if (lang.builtin.names)
207  id = lang.builtin.names[i];
208  ImGui::TableNextRow();
209  ImGui::TableNextColumn();
210  ImGui::TextUnformatted(id ? id : "no name");
211  ImGui::TableNextColumn();
212  ImGui::Text("%zu", lang.builtin.decls[i]);
213  }
214  end_table_node();
215  }
216 
217  if (begin_table_node((void*)&lang, "Callbacks", 3, kCallbackTableFlags))
218  {
219  ImGui::TableSetupColumn("Info", ImGuiTableColumnFlags_WidthFixed, ttwidth);
220  ImGui::TableSetupColumn("Name");
221  ImGui::TableSetupColumn("Address");
222  ImGui::TableHeadersRow();
223 
224  draw_row("Create", (const void*)lang.fn_create,
225  "Called during initial creation of the language driver.\n"
226  "Responsible for setting up any resources required for the language.");
227 
228  draw_row("Destroy", (const void*)lang.fn_destroy,
229  "Called during destruction of the language driver.\n"
230  "Responsible for cleaning up any resources allocated during creation or runtime.");
231 
232  draw_row("Preparse", (const void*)lang.fn_preparse,
233  "Called once before parsing each source file.\n"
234  "Configures parser and scanner local values for the source file.");
235 
236  draw_row("Postparse", (const void*)lang.fn_postparse,
237  "Called once after parsing each source file.\n"
238  "Responsible for producing translation units that will later be analyzed.");
239 
240  end_table_node();
241  }
242 
243  const scan_callbacks_t *scan = lang.scanner;
244  ImGui::BeginDisabled(scan == nullptr);
245 
246  if (begin_table_node((void*)scan, ed::strfmt<64>("Scanner (0x%p)", scan), 3, kCallbackTableFlags))
247  {
248  ImGui::TableSetupColumn("Info", ImGuiTableColumnFlags_WidthFixed, ttwidth);
249  ImGui::TableSetupColumn("Name");
250  ImGui::TableSetupColumn("Address");
251  ImGui::TableHeadersRow();
252 
253  draw_row("Init", (const void*)scan->init,
254  "Called once during the creation of the scanner.\n"
255  "Responsible for setting up any resources required for the scanner.");
256 
257  draw_row("Scan", (const void*)scan->scan,
258  "Scans a source file and produces a token stream.\n"
259  "This token stream is required for parsing.");
260 
261  draw_row("Parse", (const void*)scan->parse,
262  "Parses a token stream and produces an ast.");
263 
264  draw_row("Destroy Buffer", (const void*)scan->destroy,
265  "Called during destruction of the scanner.\n"
266  "Responsible for cleaning up any resources allocated during creation or runtime.");
267 
268  draw_row("Destroy", (const void*)scan->destroy,
269  "Called during destruction of the scanner.\n"
270  "Responsible for cleaning up any resources allocated during creation or runtime.");
271 
272  end_table_node();
273  }
274  ImGui::EndDisabled();
275 
276  if (scan == nullptr)
277  {
278  ImGui::SetItemTooltip("This language does not provide a scanner");
279  }
280 
281  if (begin_table_node((void*)&lang.fn_passes, "Passes", 3, kCallbackTableFlags))
282  {
283  ImGui::TableSetupColumn("Index", ImGuiTableColumnFlags_WidthFixed, 30.f);
284  ImGui::TableSetupColumn("Name");
285  ImGui::TableSetupColumn("Address");
286  ImGui::TableHeadersRow();
287 
288  for (size_t i = 0; i < ePassCount; i++)
289  {
290  broker_pass_t pass = static_cast<broker_pass_t>(i);
291  ImGui::TableNextRow();
292  ImGui::TableNextColumn();
293  ImGui::Text("%zu", i);
294  ImGui::TableNextColumn();
295  ImGui::TextUnformatted(broker_pass_name(pass));
296  ImGui::TableNextColumn();
297  draw_function_address((const void*)lang.fn_passes[i]);
298  }
299  end_table_node();
300  }
301 }
302 
303 // plugin info
304 
306  : ModuleInfoPanel(plugin.info)
307  , plugin(plugin)
308 { }
309 
310 void PluginInfoPanel::draw_content()
311 {
313  ImGui::Text("Create: %p", plugin.fn_create);
314  ImGui::Text("Destroy: %p", plugin.fn_destroy);
315 
316  event_list_t events = plugin.events;
317  if (ImGui::TreeNode((void*)&events, "Events"))
318  {
319  for (size_t i = 0; i < events.count; i++)
320  {
321  ImGui::BulletText("Event %zu: %d", i, events.events[i].event);
322  }
323  }
324 }
325 
326 // target info
327 
329  : ModuleInfoPanel(target.info)
330  , target(target)
331 { }
332 
333 void TargetInfoPanel::draw_content()
334 {
336  ImGui::Text("Create: %p", target.fn_create);
337  ImGui::Text("Destroy: %p", target.fn_destroy);
338  draw_feature("Tree output", target.fn_tree != nullptr);
339  draw_feature("SSA output", target.fn_ssa != nullptr);
340 }
FrontendInfoPanel(const frontend_t &info)
Definition: info.cpp:128
LanguageInfoPanel(const language_t &lang)
Definition: info.cpp:141
ModuleInfoPanel(const module_info_t &info)
Definition: info.cpp:109
void draw_info()
Definition: info.cpp:114
PluginInfoPanel(const plugin_t &plugin)
Definition: info.cpp:305
TargetInfoPanel(const target_t &target)
Definition: info.cpp:328
broker_pass_t
Definition: broker.h:56
CT_CONSTFN CT_BROKER_API const char * broker_pass_name(broker_pass_t pass)
extra stuff
Definition: broker.c:597
@ ePassCount
Definition: broker.h:60
#define CT_VERSION_MAJOR(version)
returns the major version of version
Definition: version_def.h:37
uint_fast32_t ctu_version_t
underlying type for ctu_version_t
Definition: version_def.h:42
#define CT_VERSION_MINOR(version)
returns the minor version of version
Definition: version_def.h:38
#define CT_VERSION_PATCH(version)
returns the patch version of version
Definition: version_def.h:39
#define CT_UNUSED(x)
mark a variable as unused
Definition: macros.h:46
STA_RET_STRING CT_CONSTFN CT_NOTIFY_API const char * severity_string(severity_t severity)
get the name of a severity
Definition: notify.c:216
Definition: compile.hpp:13
a list of diagnostics
Definition: diagnostic.h:48
a diagnostic
Definition: diagnostic.h:27
const char * brief
a brief description of the diagnostic a single line description of the diagnostic
Definition: diagnostic.h:38
const char * id
the id of the diagnostic should be in the format [A-Z]{2,3}[0-9]{4} e.g. CLI0001 this is not enforced...
Definition: diagnostic.h:34
severity_t severity
the severity of the diagnostic
Definition: diagnostic.h:29
const char * description
a description of the diagnostic a more involved description of the diagnostic this is optional
Definition: diagnostic.h:43
the frontend running the mediator
Definition: broker.h:251
unit_id_t name
the name of the builtin module
Definition: broker.h:126
a language driver support capabilities
Definition: broker.h:143
const char *const * exts
the default file extensions this language should be used for
Definition: broker.h:155
size_t context_size
the size of the scan context for this language
Definition: broker.h:158
language_info_t builtin
builtin module configuration
Definition: broker.h:148
size_t ast_size
the size of an ast node for this language
Definition: broker.h:161
common information about anything the broker supports
Definition: broker.h:99
version_info_t version
the version of the module
Definition: broker.h:107
diagnostic_list_t diagnostics
all diagnostics associated with this module
Definition: broker.h:110
const char * id
unique id for the module
Definition: broker.h:101
plugin support capabilities
Definition: broker.h:202
event_list_t events
the events this plugin is interested in
Definition: broker.h:213
plugin_destroy_t fn_destroy
called at shutdown
Definition: broker.h:210
plugin_create_t fn_create
called once at startup
Definition: broker.h:207
scanner function callbacks for flex and bison
Definition: compile.h:26
a codegen target backend
Definition: broker.h:232
target_tree_t fn_tree
generate from the tree form
Definition: broker.h:243
target_ssa_t fn_ssa
generate from the ssa form
Definition: broker.h:246
target_destroy_t fn_destroy
called at shutdown
Definition: broker.h:240
target_create_t fn_create
called once at startup
Definition: broker.h:237
a non-owning view of text
Definition: text.h:24
size_t length
the number of characters in the text
Definition: text.h:30
version information for a driver/interface/plugin
Definition: version_def.h:48
const char * license
the license of this component
Definition: version_def.h:49
const char * author
the author of this component
Definition: version_def.h:51
const char * desc
a short description of this component
Definition: version_def.h:50
ctu_version_t version
the version info for this component
Definition: version_def.h:52