Skip to content

Commit d1c44c4

Browse files
committed
chore: xml generator support for new types + cleanup
1 parent af254c3 commit d1c44c4

File tree

10 files changed

+199
-136
lines changed

10 files changed

+199
-136
lines changed

source/-XML/CXXTags.hpp

Lines changed: 124 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -185,43 +185,138 @@ inline void write(VariableFlags0 const& bits, XMLTags& tags)
185185
fw.write(&VariableFlags0::storageClass, "storage-class");
186186
}
187187

188-
inline void writeReturnType(TypeInfo const& I, XMLTags& tags)
188+
inline
189+
void
190+
writeTemplateArg(const TArg& I, XMLTags& tags);
191+
192+
inline
193+
void
194+
writeType(
195+
TypeInfo const& I,
196+
XMLTags& tags,
197+
std::string_view type_tag = "type")
189198
{
190-
#if 0
191-
if(I.Name == "void")
192-
return;
193-
tags.write(returnTagName, {}, {
194-
{ "type", I.Name },
195-
{ I.id }
199+
visit(I, [&]<typename T>(
200+
const T& t)
201+
{
202+
Attributes attrs = {
203+
{ "class", toString(T::kind_id),
204+
T::kind_id != TypeKind::Builtin }
205+
};
206+
207+
if constexpr(requires { t.id; })
208+
{
209+
attrs.push({t.id});
210+
}
211+
212+
// KRYSTIAN FIXME: parent should is a type itself
213+
if constexpr(requires { t.ParentType; })
214+
{
215+
if(t.ParentType)
216+
attrs.push({"parent", toString(*t.ParentType)});
217+
}
218+
219+
if constexpr(requires { t.Name; })
220+
{
221+
attrs.push({"name", t.Name});
222+
}
223+
224+
if constexpr(requires { t.CVQualifiers; })
225+
{
226+
if(t.CVQualifiers != QualifierKind::None)
227+
attrs.push({"cv-qualifiers", toString(t.CVQualifiers)});
228+
}
229+
230+
if constexpr(T::isArray())
231+
{
232+
if(! t.BoundsValue.empty())
233+
attrs.push({"bounds-value", t.BoundsValue});
234+
if(t.BoundsValue != t.BoundsExpr &&
235+
! t.BoundsExpr.empty())
236+
attrs.push({"bounds-expr", t.BoundsExpr});
237+
}
238+
239+
if constexpr(T::isFunction())
240+
{
241+
if(t.RefQualifier != ReferenceKind::None)
242+
attrs.push({"ref-qualifier", toString(t.RefQualifier)});
243+
244+
if(t.ExceptionSpec != NoexceptKind::None)
245+
attrs.push({"exception-spec", toString(t.ExceptionSpec)});
246+
}
247+
248+
// ----------------------------------------------------------------
249+
250+
// no nested types; write as self closing tag
251+
if constexpr(T::isBuiltin() || T::isTag())
252+
{
253+
tags.write(type_tag, {}, std::move(attrs));
254+
return;
255+
}
256+
257+
tags.open(type_tag, std::move(attrs));
258+
259+
if constexpr(T::isSpecialization())
260+
{
261+
for(const auto& targ : t.TemplateArgs)
262+
writeTemplateArg(targ, tags);
263+
}
264+
265+
if constexpr(requires { t.PointeeType; })
266+
{
267+
writeType(*t.PointeeType, tags, "pointee-type");
268+
}
269+
270+
if constexpr(T::isPack())
271+
{
272+
writeType(*t.PatternType, tags, "pattern-type");
273+
}
274+
275+
if constexpr(T::isArray())
276+
{
277+
writeType(*t.ElementType, tags, "element-type");
278+
}
279+
280+
if constexpr(T::isFunction())
281+
{
282+
writeType(*t.ReturnType, tags, "return-type");
283+
for(const auto& p : t.ParamTypes)
284+
writeType(*p, tags, "param-type");
285+
}
286+
287+
tags.close(type_tag);
196288
});
197-
#else
198-
std::string type_str = toString(I);
199-
if(type_str == "void")
289+
}
290+
291+
inline
292+
void
293+
writeType(
294+
const std::unique_ptr<TypeInfo>& type,
295+
XMLTags& tags)
296+
{
297+
if(! type)
200298
return;
201-
tags.write(returnTagName, {}, {
202-
{ "type", type_str },
203-
// { I.id }
204-
});
205-
#endif
299+
writeType(*type, tags);
300+
}
301+
302+
inline void writeReturnType(TypeInfo const& I, XMLTags& tags)
303+
{
304+
// KRYSTIAN NOTE: we don't *have* to do this...
305+
if(toString(I) == "void")
306+
return;
307+
tags.open(returnTagName);
308+
writeType(I, tags);
309+
tags.close(returnTagName);
206310
}
207311

208312
inline void writeParam(Param const& P, XMLTags& tags)
209313
{
210-
#if 0
211-
tags.write(paramTagName, {}, {
314+
tags.open(paramTagName, {
212315
{ "name", P.Name, ! P.Name.empty() },
213-
{ "type", P.Type.Name },
214316
{ "default", P.Default, ! P.Default.empty() },
215-
{ P.Type.id } });
216-
#else
217-
std::string type_str = toString(*P.Type);
218-
tags.write(paramTagName, {}, {
219-
{ "name", P.Name, ! P.Name.empty() },
220-
{ "type", type_str },
221-
{ "default", P.Default, ! P.Default.empty() },
222-
// { P.Type.id }
223317
});
224-
#endif
318+
writeType(*P.Type, tags);
319+
tags.close(paramTagName);
225320
}
226321

227322
inline void writeTemplateParam(const TParam& I, XMLTags& tags)
@@ -231,15 +326,10 @@ inline void writeTemplateParam(const TParam& I, XMLTags& tags)
231326
case TParamKind::Type:
232327
{
233328
const auto& t = I.get<TypeTParam>();
234-
#if 0
235-
std::string_view default_val;
236-
if(t.Default)
237-
default_val = t.Default->Name;
238-
#else
329+
239330
std::string default_val;
240331
if(t.Default)
241332
default_val = toString(*t.Default);
242-
#endif
243333

244334
tags.write(tparamTagName, {}, {
245335
{ "name", I.Name, ! I.Name.empty() },
@@ -258,11 +348,8 @@ inline void writeTemplateParam(const TParam& I, XMLTags& tags)
258348
tags.write(tparamTagName, {}, {
259349
{ "name", I.Name, ! I.Name.empty() },
260350
{ "class", "non-type" },
261-
#if 0
262-
{ "type", t.Type.Name },
263-
#else
351+
// KRYSTIAN FIXME: we can use writeType if really care
264352
{ "type", toString(*t.Type) },
265-
#endif
266353
{ "default", default_val, ! default_val.empty() }
267354
});
268355
break;

source/-XML/XMLTags.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,45 @@ toString(
101101

102102
Attributes::
103103
Attributes(
104-
std::initializer_list<Attribute> init)
105-
: init_(init)
104+
std::initializer_list<Attribute> attrs)
105+
: attrs_(attrs)
106106
{
107107
}
108108

109+
Attributes::
110+
Attributes(
111+
const std::vector<Attribute>& attrs)
112+
: attrs_(attrs)
113+
{
114+
}
115+
116+
Attributes::
117+
Attributes(
118+
std::vector<Attribute>&& attrs)
119+
: attrs_(std::move(attrs))
120+
{
121+
}
122+
123+
void
124+
Attributes::
125+
push(const Attribute& attr)
126+
{
127+
attrs_.push_back(attr);
128+
}
129+
130+
void
131+
Attributes::
132+
push(Attribute&& attr)
133+
{
134+
attrs_.push_back(std::move(attr));
135+
}
136+
109137
llvm::raw_ostream&
110138
operator<<(
111139
llvm::raw_ostream& os,
112140
Attributes const& attrs)
113141
{
114-
for(auto const& attr : attrs.init_)
142+
for(auto const& attr : attrs.attrs_)
115143
if(attr.pred)
116144
os <<
117145
' ' << attr.name << '=' <<

source/-XML/XMLTags.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <llvm/ADT/StringRef.h>
2222
#include <llvm/Support/raw_ostream.h>
2323
#include <optional>
24+
#include <vector>
2425

2526
/*
2627
Object for assisting with generating
@@ -127,21 +128,27 @@ struct Attribute
127128

128129
//------------------------------------------------
129130

130-
/** An init-list of zero or more XML attributes.
131+
/** An vector of zero or more XML attributes.
131132
*/
132133
struct Attributes
133134
{
134-
std::initializer_list<Attribute> init_;
135+
std::vector<Attribute> attrs_;
135136

136137
Attributes() = default;
137-
Attributes(std::initializer_list<Attribute> init);
138+
Attributes(std::initializer_list<Attribute> attrs);
139+
Attributes(const std::vector<Attribute>& attrs);
140+
Attributes(std::vector<Attribute>&& attrs);
141+
142+
void push(const Attribute& attr);
143+
void push(Attribute&& attr);
138144

139145
friend
140146
llvm::raw_ostream& operator<<(
141147
llvm::raw_ostream& os,
142148
Attributes const& attrs);
143149
};
144150

151+
145152
//------------------------------------------------
146153

147154
/** A stream which indents just in time.

source/-XML/XMLWriter.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ writeEnum(
293293
if(I.BaseType)
294294
{
295295
tags_.open(baseTagName);
296-
writeType(I.BaseType);
296+
writeType(I.BaseType, tags_);
297297
tags_.close(baseTagName);
298298
}
299299
#endif
@@ -392,7 +392,7 @@ writeRecord(
392392
{ B.Access },
393393
{ "class", "virtual", B.IsVirtual },
394394
});
395-
writeType(B.Type);
395+
writeType(B.Type, tags_);
396396
tags_.close(baseTagName);
397397
}
398398
#endif
@@ -434,7 +434,7 @@ writeTypedef(
434434

435435
writeSourceInfo(I);
436436

437-
writeType(I.Underlying);
437+
writeType(I.Underlying, tags_);
438438
#if 0
439439
tags_.write("type", "", {
440440
{ "name", I.Underlying.Name },
@@ -466,7 +466,7 @@ writeField(
466466

467467
write(I.specs, tags_);
468468

469-
writeType(I.Type);
469+
writeType(I.Type, tags_);
470470
#if 0
471471
tags_.write("type", {}, {
472472
{ "name", I.Type.Name },
@@ -498,7 +498,7 @@ writeVar(
498498

499499
write(I.specs, tags_);
500500

501-
writeType(I.Type);
501+
writeType(I.Type, tags_);
502502
#if 0
503503
tags_.write("type", {}, {
504504
{ "name", I.Type.Name },
@@ -608,18 +608,23 @@ writeSpecialization(
608608

609609
//------------------------------------------------
610610

611+
#if 0
611612
void
612613
XMLWriter::
613614
writeType(
614615
const std::unique_ptr<TypeInfo>& type)
615616
{
616617
if(! type)
617618
return;
618-
619+
#if 0
619620
tags_.write("type", {}, {
620621
{ "name", toString(*type) },
621622
});
623+
#else
624+
625+
#endif
622626
}
627+
#endif
623628

624629
//------------------------------------------------
625630

source/-XML/XMLWriter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class XMLWriter
8181
void openTemplate(const std::unique_ptr<TemplateInfo>& I);
8282
void closeTemplate(const std::unique_ptr<TemplateInfo>& I);
8383

84-
void writeType(std::unique_ptr<TypeInfo> const& type);
84+
// void writeType(std::unique_ptr<TypeInfo> const& type);
8585

8686
template<class T>
8787
void writeNodes(doc::List<T> const& list);

source/AST/ASTVisitor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ buildTypeInfoForType(
298298
auto I = std::make_unique<ArrayTypeInfo>();
299299
I->ElementType = buildTypeInfoForType(
300300
T->getElementType());
301+
// KRYSTIAN FIXME: this is broken; cannonical
302+
// constant array types never have a size expression
301303
if(auto* E = T->getSizeExpr())
302304
I->BoundsExpr = getSourceCode(E->getSourceRange());
303305
I->BoundsValue = toString(T->getSize(), 10, false);

0 commit comments

Comments
 (0)