Skip to content

Commit 3cb058b

Browse files
committed
c2rust-transpile: handle builtin vector types (e.g. AArch64 SVE)
support for these was added in Clang/LLVM 11, so this handling is #ifdef'd on that
1 parent f744d1d commit 3cb058b

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
140140
cbor_encoder_close_container(encoder, &local);
141141
}
142142

143+
DiagnosticBuilder getDiagBuilder(SourceLocation Loc,
144+
DiagnosticsEngine::Level Lvl) {
145+
auto &DiagEngine = Context->getDiagnostics();
146+
// Prefix warnings with `c2rust`, so the user can distinguish
147+
// our warning messages from those generated by clang itself.
148+
const auto ID = DiagEngine.getCustomDiagID(Lvl, "c2rust: %0");
149+
return DiagEngine.Report(Loc, ID);
150+
}
151+
152+
void printWarning(std::string Message,
153+
SourceLocation S,
154+
SourceRange R = SourceRange()) {
155+
auto DiagBuilder =
156+
getDiagBuilder(S, DiagnosticsEngine::Warning);
157+
DiagBuilder.AddString(Message);
158+
DiagBuilder.AddSourceRange(
159+
CharSourceRange::getCharRange(R));
160+
}
161+
143162
public:
144163
uintptr_t encodeQualType(QualType t) {
145164
auto s = t.split();
@@ -303,9 +322,36 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
303322
TypeTag tag;
304323
auto kind = T->getKind();
305324

325+
#if CLANG_VERSION_MAJOR >= 11
326+
// Handle built-in vector types as if they're normal vector types
327+
if (kind >= BuiltinType::SveInt8 && kind <= BuiltinType::SveBool) {
328+
auto Info = Context->getBuiltinVectorTypeInfo(T);
329+
auto t = Info.ElementType;
330+
auto qt = encodeQualType(t);
331+
332+
encodeType(T, TagVectorType, [T, qt, Info](CborEncoder *local) {
333+
cbor_encode_uint(local, qt);
334+
cbor_encode_uint(local, Info.EC.getKnownMinValue() *
335+
Info.NumVectors);
336+
});
337+
338+
VisitQualType(t);
339+
return;
340+
}
341+
#endif
342+
306343
// clang-format off
307344
switch (kind) {
308-
default: tag = TagTypeUnknown; break;
345+
default: {
346+
auto pol = clang::PrintingPolicy(Context->getLangOpts());
347+
auto warning = std::string("Encountered unsupported BuiltinType kind ") +
348+
std::to_string((int)kind) + " for type " +
349+
T->getName(pol).str();
350+
printWarning(warning, clang::FullSourceLoc());
351+
tag = TagTypeUnknown;
352+
break;
353+
}
354+
309355
case BuiltinType::BuiltinFn: tag = TagBuiltinFn; break;
310356
case BuiltinType::UInt128: tag = TagUInt128; break;
311357
case BuiltinType::Int128: tag = TagInt128; break;

0 commit comments

Comments
 (0)